blob: 6f3c00a233ecdde57e42100c8a1a83ba94a631b3 [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 Smart4f774512009-05-22 14:52:35 -0400148
James Smart2e90f4b2011-12-13 13:22:37 -0500149 /* sanity check on queue memory */
150 if (unlikely(!q))
151 return -ENOMEM;
152 temp_wqe = q->qe[q->host_index].wqe;
153
James Smart4f774512009-05-22 14:52:35 -0400154 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400155 idx = ((q->host_index + 1) % q->entry_count);
156 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400157 q->WQ_overflow++;
Dick Kennedycd22d602017-08-23 16:55:35 -0700158 return -EBUSY;
James Smartb84daac2012-08-03 12:35:13 -0400159 }
160 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400161 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500162 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400163 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smart04673e32018-01-30 15:58:45 -0800164 else
165 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
James Smartfedd3b72011-02-16 12:39:24 -0500166 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
167 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart48f8fdb2018-05-04 20:37:51 -0700168 lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
James Smart1351e692018-02-22 08:18:43 -0800169 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
170 /* write to DPP aperture taking advatage of Combined Writes */
James Smart4c066192018-03-05 10:29:03 -0800171 tmp = (uint8_t *)temp_wqe;
172#ifdef __raw_writeq
James Smart1351e692018-02-22 08:18:43 -0800173 for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
James Smart4c066192018-03-05 10:29:03 -0800174 __raw_writeq(*((uint64_t *)(tmp + i)),
175 q->dpp_regaddr + i);
176#else
177 for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
178 __raw_writel(*((uint32_t *)(tmp + i)),
179 q->dpp_regaddr + i);
180#endif
James Smart1351e692018-02-22 08:18:43 -0800181 }
182 /* ensure WQE bcopy and DPP flushed before doorbell write */
James Smart6b3b3bd2016-12-19 15:07:30 -0800183 wmb();
James Smart4f774512009-05-22 14:52:35 -0400184
185 /* Update the host index before invoking device */
186 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400187
188 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400189
190 /* Ring Doorbell */
191 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500192 if (q->db_format == LPFC_DB_LIST_FORMAT) {
James Smart1351e692018-02-22 08:18:43 -0800193 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
194 bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
195 bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
196 bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
197 q->dpp_id);
198 bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
199 q->queue_id);
200 } else {
201 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
202 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
203 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
204 }
James Smart962bc512013-01-03 15:44:00 -0500205 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
206 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
207 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
208 } else {
209 return -EINVAL;
210 }
211 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400212
213 return 0;
214}
215
216/**
217 * lpfc_sli4_wq_release - Updates internal hba index for WQ
218 * @q: The Work Queue to operate on.
219 * @index: The index to advance the hba index to.
220 *
221 * This routine will update the HBA index of a queue to reflect consumption of
222 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
223 * an entry the host calls this function to update the queue's internal
224 * pointers. This routine returns the number of entries that were consumed by
225 * the HBA.
226 **/
227static uint32_t
228lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
229{
230 uint32_t released = 0;
231
James Smart2e90f4b2011-12-13 13:22:37 -0500232 /* sanity check on queue memory */
233 if (unlikely(!q))
234 return 0;
235
James Smart4f774512009-05-22 14:52:35 -0400236 if (q->hba_index == index)
237 return 0;
238 do {
239 q->hba_index = ((q->hba_index + 1) % q->entry_count);
240 released++;
241 } while (q->hba_index != index);
242 return released;
243}
244
245/**
246 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
247 * @q: The Mailbox Queue to operate on.
248 * @wqe: The Mailbox Queue Entry to put on the Work queue.
249 *
250 * This routine will copy the contents of @mqe to the next available entry on
251 * the @q. This function will then ring the Work Queue Doorbell to signal the
252 * HBA to start processing the Work Queue Entry. This function returns 0 if
253 * successful. If no entries are available on @q then this function will return
254 * -ENOMEM.
255 * The caller is expected to hold the hbalock when calling this routine.
256 **/
257static uint32_t
258lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
259{
James Smart2e90f4b2011-12-13 13:22:37 -0500260 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400261 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400262
James Smart2e90f4b2011-12-13 13:22:37 -0500263 /* sanity check on queue memory */
264 if (unlikely(!q))
265 return -ENOMEM;
266 temp_mqe = q->qe[q->host_index].mqe;
267
James Smart4f774512009-05-22 14:52:35 -0400268 /* If the host has not yet processed the next entry then we are done */
269 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
270 return -ENOMEM;
James Smart48f8fdb2018-05-04 20:37:51 -0700271 lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
James Smart4f774512009-05-22 14:52:35 -0400272 /* Save off the mailbox pointer for completion */
273 q->phba->mbox = (MAILBOX_t *)temp_mqe;
274
275 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400276 q->host_index = ((q->host_index + 1) % q->entry_count);
277
278 /* Ring Doorbell */
279 doorbell.word0 = 0;
280 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
281 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
282 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400283 return 0;
284}
285
286/**
287 * lpfc_sli4_mq_release - Updates internal hba index for MQ
288 * @q: The Mailbox Queue to operate on.
289 *
290 * This routine will update the HBA index of a queue to reflect consumption of
291 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
292 * an entry the host calls this function to update the queue's internal
293 * pointers. This routine returns the number of entries that were consumed by
294 * the HBA.
295 **/
296static uint32_t
297lpfc_sli4_mq_release(struct lpfc_queue *q)
298{
James Smart2e90f4b2011-12-13 13:22:37 -0500299 /* sanity check on queue memory */
300 if (unlikely(!q))
301 return 0;
302
James Smart4f774512009-05-22 14:52:35 -0400303 /* Clear the mailbox pointer for completion */
304 q->phba->mbox = NULL;
305 q->hba_index = ((q->hba_index + 1) % q->entry_count);
306 return 1;
307}
308
309/**
310 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
311 * @q: The Event Queue to get the first valid EQE from
312 *
313 * This routine will get the first valid Event Queue Entry from @q, update
314 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
315 * the Queue (no more work to do), or the Queue is full of EQEs that have been
316 * processed, but not popped back to the HBA then this routine will return NULL.
317 **/
318static struct lpfc_eqe *
319lpfc_sli4_eq_get(struct lpfc_queue *q)
320{
James Smart7365f6f2018-02-22 08:18:46 -0800321 struct lpfc_hba *phba;
James Smart2e90f4b2011-12-13 13:22:37 -0500322 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400323 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500324
325 /* sanity check on queue memory */
326 if (unlikely(!q))
327 return NULL;
James Smart7365f6f2018-02-22 08:18:46 -0800328 phba = q->phba;
James Smart2e90f4b2011-12-13 13:22:37 -0500329 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400330
331 /* If the next EQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800332 if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400333 return NULL;
334 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400335 idx = ((q->hba_index + 1) % q->entry_count);
336 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400337 return NULL;
338
James Smart027140e2012-08-03 12:35:44 -0400339 q->hba_index = idx;
James Smart7365f6f2018-02-22 08:18:46 -0800340 /* if the index wrapped around, toggle the valid bit */
341 if (phba->sli4_hba.pc_sli4_params.eqav && !q->hba_index)
342 q->qe_valid = (q->qe_valid) ? 0 : 1;
343
James Smart27f344e2014-05-07 17:16:46 -0400344
345 /*
346 * insert barrier for instruction interlock : data from the hardware
347 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800348 * upon. Speculative instructions were allowing a bcopy at the start
349 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
350 * after our return, to copy data before the valid bit check above
351 * was done. As such, some of the copied data was stale. The barrier
352 * ensures the check is before any data is copied.
James Smart27f344e2014-05-07 17:16:46 -0400353 */
354 mb();
James Smart4f774512009-05-22 14:52:35 -0400355 return eqe;
356}
357
358/**
James Smartba20c852012-08-03 12:36:52 -0400359 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
360 * @q: The Event Queue to disable interrupts
361 *
362 **/
James Smartb71413d2018-02-22 08:18:40 -0800363inline void
James Smartba20c852012-08-03 12:36:52 -0400364lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
365{
366 struct lpfc_register doorbell;
367
368 doorbell.word0 = 0;
369 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
370 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
371 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
372 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
373 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800374 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smartba20c852012-08-03 12:36:52 -0400375}
376
377/**
James Smart27d6ac02018-02-22 08:18:42 -0800378 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
379 * @q: The Event Queue to disable interrupts
380 *
381 **/
382inline void
383lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
384{
385 struct lpfc_register doorbell;
386
387 doorbell.word0 = 0;
388 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
389 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
390 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
391 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
392 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
393 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
394}
395
396/**
James Smart4f774512009-05-22 14:52:35 -0400397 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
398 * @q: The Event Queue that the host has completed processing for.
399 * @arm: Indicates whether the host wants to arms this CQ.
400 *
401 * This routine will mark all Event Queue Entries on @q, from the last
402 * known completed entry to the last entry that was processed, as completed
403 * by clearing the valid bit for each completion queue entry. Then it will
404 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
405 * The internal host index in the @q will be updated by this routine to indicate
406 * that the host has finished processing the entries. The @arm parameter
407 * indicates that the queue should be rearmed when ringing the doorbell.
408 *
409 * This function will return the number of EQEs that were popped.
410 **/
411uint32_t
412lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
413{
414 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800415 struct lpfc_hba *phba;
James Smart4f774512009-05-22 14:52:35 -0400416 struct lpfc_eqe *temp_eqe;
417 struct lpfc_register doorbell;
418
James Smart2e90f4b2011-12-13 13:22:37 -0500419 /* sanity check on queue memory */
420 if (unlikely(!q))
421 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800422 phba = q->phba;
James Smart2e90f4b2011-12-13 13:22:37 -0500423
James Smart4f774512009-05-22 14:52:35 -0400424 /* while there are valid entries */
425 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800426 if (!phba->sli4_hba.pc_sli4_params.eqav) {
427 temp_eqe = q->qe[q->host_index].eqe;
428 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
429 }
James Smart4f774512009-05-22 14:52:35 -0400430 released++;
431 q->host_index = ((q->host_index + 1) % q->entry_count);
432 }
433 if (unlikely(released == 0 && !arm))
434 return 0;
435
436 /* ring doorbell for number popped */
437 doorbell.word0 = 0;
438 if (arm) {
439 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
440 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
441 }
442 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
443 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500444 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
445 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
446 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800447 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500448 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
449 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
James Smart9dd35422018-02-22 08:18:41 -0800450 readl(q->phba->sli4_hba.EQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400451 return released;
452}
453
454/**
James Smart27d6ac02018-02-22 08:18:42 -0800455 * lpfc_sli4_if6_eq_release - Indicates the host has finished processing an EQ
456 * @q: The Event Queue that the host has completed processing for.
457 * @arm: Indicates whether the host wants to arms this CQ.
458 *
459 * This routine will mark all Event Queue Entries on @q, from the last
460 * known completed entry to the last entry that was processed, as completed
461 * by clearing the valid bit for each completion queue entry. Then it will
462 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
463 * The internal host index in the @q will be updated by this routine to indicate
464 * that the host has finished processing the entries. The @arm parameter
465 * indicates that the queue should be rearmed when ringing the doorbell.
466 *
467 * This function will return the number of EQEs that were popped.
468 **/
469uint32_t
470lpfc_sli4_if6_eq_release(struct lpfc_queue *q, bool arm)
471{
472 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800473 struct lpfc_hba *phba;
James Smart27d6ac02018-02-22 08:18:42 -0800474 struct lpfc_eqe *temp_eqe;
475 struct lpfc_register doorbell;
476
477 /* sanity check on queue memory */
478 if (unlikely(!q))
479 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800480 phba = q->phba;
James Smart27d6ac02018-02-22 08:18:42 -0800481
482 /* while there are valid entries */
483 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800484 if (!phba->sli4_hba.pc_sli4_params.eqav) {
485 temp_eqe = q->qe[q->host_index].eqe;
486 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
487 }
James Smart27d6ac02018-02-22 08:18:42 -0800488 released++;
489 q->host_index = ((q->host_index + 1) % q->entry_count);
490 }
491 if (unlikely(released == 0 && !arm))
492 return 0;
493
494 /* ring doorbell for number popped */
495 doorbell.word0 = 0;
496 if (arm)
497 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
498 bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, released);
499 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
500 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
501 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
502 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
503 readl(q->phba->sli4_hba.EQDBregaddr);
504 return released;
505}
506
507/**
James Smart4f774512009-05-22 14:52:35 -0400508 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
509 * @q: The Completion Queue to get the first valid CQE from
510 *
511 * This routine will get the first valid Completion Queue Entry from @q, update
512 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
513 * the Queue (no more work to do), or the Queue is full of CQEs that have been
514 * processed, but not popped back to the HBA then this routine will return NULL.
515 **/
516static struct lpfc_cqe *
517lpfc_sli4_cq_get(struct lpfc_queue *q)
518{
James Smart7365f6f2018-02-22 08:18:46 -0800519 struct lpfc_hba *phba;
James Smart4f774512009-05-22 14:52:35 -0400520 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400521 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400522
James Smart2e90f4b2011-12-13 13:22:37 -0500523 /* sanity check on queue memory */
524 if (unlikely(!q))
525 return NULL;
James Smart7365f6f2018-02-22 08:18:46 -0800526 phba = q->phba;
527 cqe = q->qe[q->hba_index].cqe;
James Smart2e90f4b2011-12-13 13:22:37 -0500528
James Smart4f774512009-05-22 14:52:35 -0400529 /* If the next CQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800530 if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400531 return NULL;
532 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400533 idx = ((q->hba_index + 1) % q->entry_count);
534 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400535 return NULL;
536
James Smart027140e2012-08-03 12:35:44 -0400537 q->hba_index = idx;
James Smart7365f6f2018-02-22 08:18:46 -0800538 /* if the index wrapped around, toggle the valid bit */
539 if (phba->sli4_hba.pc_sli4_params.cqav && !q->hba_index)
540 q->qe_valid = (q->qe_valid) ? 0 : 1;
James Smart27f344e2014-05-07 17:16:46 -0400541
542 /*
543 * insert barrier for instruction interlock : data from the hardware
544 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800545 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
546 * instructions allowing action on content before valid bit checked,
547 * add barrier here as well. May not be needed as "content" is a
548 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400549 */
550 mb();
James Smart4f774512009-05-22 14:52:35 -0400551 return cqe;
552}
553
554/**
555 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
556 * @q: The Completion Queue that the host has completed processing for.
557 * @arm: Indicates whether the host wants to arms this CQ.
558 *
559 * This routine will mark all Completion queue entries on @q, from the last
560 * known completed entry to the last entry that was processed, as completed
561 * by clearing the valid bit for each completion queue entry. Then it will
562 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
563 * The internal host index in the @q will be updated by this routine to indicate
564 * that the host has finished processing the entries. The @arm parameter
565 * indicates that the queue should be rearmed when ringing the doorbell.
566 *
567 * This function will return the number of CQEs that were released.
568 **/
569uint32_t
570lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
571{
572 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800573 struct lpfc_hba *phba;
James Smart4f774512009-05-22 14:52:35 -0400574 struct lpfc_cqe *temp_qe;
575 struct lpfc_register doorbell;
576
James Smart2e90f4b2011-12-13 13:22:37 -0500577 /* sanity check on queue memory */
578 if (unlikely(!q))
579 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800580 phba = q->phba;
581
James Smart4f774512009-05-22 14:52:35 -0400582 /* while there are valid entries */
583 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800584 if (!phba->sli4_hba.pc_sli4_params.cqav) {
585 temp_qe = q->qe[q->host_index].cqe;
586 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
587 }
James Smart4f774512009-05-22 14:52:35 -0400588 released++;
589 q->host_index = ((q->host_index + 1) % q->entry_count);
590 }
591 if (unlikely(released == 0 && !arm))
592 return 0;
593
594 /* ring doorbell for number popped */
595 doorbell.word0 = 0;
596 if (arm)
597 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
598 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
599 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500600 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
601 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
602 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800603 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400604 return released;
605}
606
607/**
James Smart27d6ac02018-02-22 08:18:42 -0800608 * lpfc_sli4_if6_cq_release - Indicates the host has finished processing a CQ
609 * @q: The Completion Queue that the host has completed processing for.
610 * @arm: Indicates whether the host wants to arms this CQ.
611 *
612 * This routine will mark all Completion queue entries on @q, from the last
613 * known completed entry to the last entry that was processed, as completed
614 * by clearing the valid bit for each completion queue entry. Then it will
615 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
616 * The internal host index in the @q will be updated by this routine to indicate
617 * that the host has finished processing the entries. The @arm parameter
618 * indicates that the queue should be rearmed when ringing the doorbell.
619 *
620 * This function will return the number of CQEs that were released.
621 **/
622uint32_t
623lpfc_sli4_if6_cq_release(struct lpfc_queue *q, bool arm)
624{
625 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800626 struct lpfc_hba *phba;
James Smart27d6ac02018-02-22 08:18:42 -0800627 struct lpfc_cqe *temp_qe;
628 struct lpfc_register doorbell;
629
630 /* sanity check on queue memory */
631 if (unlikely(!q))
632 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800633 phba = q->phba;
634
James Smart27d6ac02018-02-22 08:18:42 -0800635 /* while there are valid entries */
636 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800637 if (!phba->sli4_hba.pc_sli4_params.cqav) {
638 temp_qe = q->qe[q->host_index].cqe;
639 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
640 }
James Smart27d6ac02018-02-22 08:18:42 -0800641 released++;
642 q->host_index = ((q->host_index + 1) % q->entry_count);
643 }
644 if (unlikely(released == 0 && !arm))
645 return 0;
646
647 /* ring doorbell for number popped */
648 doorbell.word0 = 0;
649 if (arm)
650 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
651 bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, released);
652 bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
653 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
654 return released;
655}
656
657/**
James Smart4f774512009-05-22 14:52:35 -0400658 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
659 * @q: The Header Receive Queue to operate on.
660 * @wqe: The Receive Queue Entry to put on the Receive queue.
661 *
662 * This routine will copy the contents of @wqe to the next available entry on
663 * the @q. This function will then ring the Receive Queue Doorbell to signal the
664 * HBA to start processing the Receive Queue Entry. This function returns the
665 * index that the rqe was copied to if successful. If no entries are available
666 * on @q then this function will return -ENOMEM.
667 * The caller is expected to hold the hbalock when calling this routine.
668 **/
James Smart895427b2017-02-12 13:52:30 -0800669int
James Smart4f774512009-05-22 14:52:35 -0400670lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
671 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
672{
James Smart2e90f4b2011-12-13 13:22:37 -0500673 struct lpfc_rqe *temp_hrqe;
674 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400675 struct lpfc_register doorbell;
James Smartcbc5de12017-12-08 17:18:04 -0800676 int hq_put_index;
677 int dq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400678
James Smart2e90f4b2011-12-13 13:22:37 -0500679 /* sanity check on queue memory */
680 if (unlikely(!hq) || unlikely(!dq))
681 return -ENOMEM;
James Smartcbc5de12017-12-08 17:18:04 -0800682 hq_put_index = hq->host_index;
683 dq_put_index = dq->host_index;
684 temp_hrqe = hq->qe[hq_put_index].rqe;
685 temp_drqe = dq->qe[dq_put_index].rqe;
James Smart2e90f4b2011-12-13 13:22:37 -0500686
James Smart4f774512009-05-22 14:52:35 -0400687 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
688 return -EINVAL;
James Smartcbc5de12017-12-08 17:18:04 -0800689 if (hq_put_index != dq_put_index)
James Smart4f774512009-05-22 14:52:35 -0400690 return -EINVAL;
691 /* If the host has not yet processed the next entry then we are done */
James Smartcbc5de12017-12-08 17:18:04 -0800692 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
James Smart4f774512009-05-22 14:52:35 -0400693 return -EBUSY;
James Smart48f8fdb2018-05-04 20:37:51 -0700694 lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
695 lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
James Smart4f774512009-05-22 14:52:35 -0400696
697 /* Update the host index to point to the next slot */
James Smartcbc5de12017-12-08 17:18:04 -0800698 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
699 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
James Smart61f3d4b2017-05-15 15:20:41 -0700700 hq->RQ_buf_posted++;
James Smart4f774512009-05-22 14:52:35 -0400701
702 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400703 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400704 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500705 if (hq->db_format == LPFC_DB_RING_FORMAT) {
706 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
707 hq->entry_repost);
708 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
709 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
710 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
711 hq->entry_repost);
712 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
713 hq->host_index);
714 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
715 } else {
716 return -EINVAL;
717 }
718 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400719 }
James Smartcbc5de12017-12-08 17:18:04 -0800720 return hq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400721}
722
723/**
724 * lpfc_sli4_rq_release - Updates internal hba index for RQ
725 * @q: The Header Receive Queue to operate on.
726 *
727 * This routine will update the HBA index of a queue to reflect consumption of
728 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
729 * consumed an entry the host calls this function to update the queue's
730 * internal pointers. This routine returns the number of entries that were
731 * consumed by the HBA.
732 **/
733static uint32_t
734lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
735{
James Smart2e90f4b2011-12-13 13:22:37 -0500736 /* sanity check on queue memory */
737 if (unlikely(!hq) || unlikely(!dq))
738 return 0;
739
James Smart4f774512009-05-22 14:52:35 -0400740 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
741 return 0;
742 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
743 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
744 return 1;
745}
746
James Smarte59058c2008-08-24 21:49:00 -0400747/**
James Smart3621a712009-04-06 18:47:14 -0400748 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400749 * @phba: Pointer to HBA context object.
750 * @pring: Pointer to driver SLI ring object.
751 *
752 * This function returns pointer to next command iocb entry
753 * in the command ring. The caller must hold hbalock to prevent
754 * other threads consume the next command iocb.
755 * SLI-2/SLI-3 provide different sized iocbs.
756 **/
James Smarted957682007-06-17 19:56:37 -0500757static inline IOCB_t *
758lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
759{
James Smart7e56aa22012-08-03 12:35:34 -0400760 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
761 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500762}
763
James Smarte59058c2008-08-24 21:49:00 -0400764/**
James Smart3621a712009-04-06 18:47:14 -0400765 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400766 * @phba: Pointer to HBA context object.
767 * @pring: Pointer to driver SLI ring object.
768 *
769 * This function returns pointer to next response iocb entry
770 * in the response ring. The caller must hold hbalock to make sure
771 * that no other thread consume the next response iocb.
772 * SLI-2/SLI-3 provide different sized iocbs.
773 **/
James Smarted957682007-06-17 19:56:37 -0500774static inline IOCB_t *
775lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
776{
James Smart7e56aa22012-08-03 12:35:34 -0400777 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
778 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500779}
780
James Smarte59058c2008-08-24 21:49:00 -0400781/**
James Smart3621a712009-04-06 18:47:14 -0400782 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400783 * @phba: Pointer to HBA context object.
784 *
785 * This function is called with hbalock held. This function
786 * allocates a new driver iocb object from the iocb pool. If the
787 * allocation is successful, it returns pointer to the newly
788 * allocated iocb object else it returns NULL.
789 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400790struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500791__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400792{
793 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
794 struct lpfc_iocbq * iocbq = NULL;
795
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100796 lockdep_assert_held(&phba->hbalock);
797
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400798 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400799 if (iocbq)
800 phba->iocb_cnt++;
801 if (phba->iocb_cnt > phba->iocb_max)
802 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400803 return iocbq;
804}
805
James Smarte59058c2008-08-24 21:49:00 -0400806/**
James Smartda0436e2009-05-22 14:51:39 -0400807 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
808 * @phba: Pointer to HBA context object.
809 * @xritag: XRI value.
810 *
811 * This function clears the sglq pointer from the array of acive
812 * sglq's. The xritag that is passed in is used to index into the
813 * array. Before the xritag can be used it needs to be adjusted
814 * by subtracting the xribase.
815 *
816 * Returns sglq ponter = success, NULL = Failure.
817 **/
James Smart895427b2017-02-12 13:52:30 -0800818struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400819__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
820{
James Smartda0436e2009-05-22 14:51:39 -0400821 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400822
823 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
824 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400825 return sglq;
826}
827
828/**
829 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
830 * @phba: Pointer to HBA context object.
831 * @xritag: XRI value.
832 *
833 * This function returns the sglq pointer from the array of acive
834 * sglq's. The xritag that is passed in is used to index into the
835 * array. Before the xritag can be used it needs to be adjusted
836 * by subtracting the xribase.
837 *
838 * Returns sglq ponter = success, NULL = Failure.
839 **/
James Smart0f65ff62010-02-26 14:14:23 -0500840struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400841__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
842{
James Smartda0436e2009-05-22 14:51:39 -0400843 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400844
845 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400846 return sglq;
847}
848
849/**
James Smart1151e3e2011-02-16 12:39:35 -0500850 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500851 * @phba: Pointer to HBA context object.
852 * @xritag: xri used in this exchange.
853 * @rrq: The RRQ to be cleared.
854 *
James Smart19ca7602010-11-20 23:11:55 -0500855 **/
James Smart1151e3e2011-02-16 12:39:35 -0500856void
857lpfc_clr_rrq_active(struct lpfc_hba *phba,
858 uint16_t xritag,
859 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500860{
James Smart1151e3e2011-02-16 12:39:35 -0500861 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500862
James Smart1151e3e2011-02-16 12:39:35 -0500863 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
864 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500865
866 /* The target DID could have been swapped (cable swap)
867 * we should use the ndlp from the findnode if it is
868 * available.
869 */
James Smart1151e3e2011-02-16 12:39:35 -0500870 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500871 ndlp = rrq->ndlp;
872
James Smart1151e3e2011-02-16 12:39:35 -0500873 if (!ndlp)
874 goto out;
875
James Smartcff261f2013-12-17 20:29:47 -0500876 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500877 rrq->send_rrq = 0;
878 rrq->xritag = 0;
879 rrq->rrq_stop_time = 0;
880 }
James Smart1151e3e2011-02-16 12:39:35 -0500881out:
James Smart19ca7602010-11-20 23:11:55 -0500882 mempool_free(rrq, phba->rrq_pool);
883}
884
885/**
886 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
887 * @phba: Pointer to HBA context object.
888 *
889 * This function is called with hbalock held. This function
890 * Checks if stop_time (ratov from setting rrq active) has
891 * been reached, if it has and the send_rrq flag is set then
892 * it will call lpfc_send_rrq. If the send_rrq flag is not set
893 * then it will just call the routine to clear the rrq and
894 * free the rrq resource.
895 * The timer is set to the next rrq that is going to expire before
896 * leaving the routine.
897 *
898 **/
899void
900lpfc_handle_rrq_active(struct lpfc_hba *phba)
901{
902 struct lpfc_node_rrq *rrq;
903 struct lpfc_node_rrq *nextrrq;
904 unsigned long next_time;
905 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500906 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500907
908 spin_lock_irqsave(&phba->hbalock, iflags);
909 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400910 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500911 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500912 &phba->active_rrq_list, list) {
913 if (time_after(jiffies, rrq->rrq_stop_time))
914 list_move(&rrq->list, &send_rrq);
915 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500916 next_time = rrq->rrq_stop_time;
917 }
918 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500919 if ((!list_empty(&phba->active_rrq_list)) &&
920 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500921 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500922 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
923 list_del(&rrq->list);
924 if (!rrq->send_rrq)
925 /* this call will free the rrq */
926 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
927 else if (lpfc_send_rrq(phba, rrq)) {
928 /* if we send the rrq then the completion handler
929 * will clear the bit in the xribitmap.
930 */
931 lpfc_clr_rrq_active(phba, rrq->xritag,
932 rrq);
933 }
934 }
James Smart19ca7602010-11-20 23:11:55 -0500935}
936
937/**
938 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
939 * @vport: Pointer to vport context object.
940 * @xri: The xri used in the exchange.
941 * @did: The targets DID for this exchange.
942 *
943 * returns NULL = rrq not found in the phba->active_rrq_list.
944 * rrq = rrq for this xri and target.
945 **/
946struct lpfc_node_rrq *
947lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
948{
949 struct lpfc_hba *phba = vport->phba;
950 struct lpfc_node_rrq *rrq;
951 struct lpfc_node_rrq *nextrrq;
952 unsigned long iflags;
953
954 if (phba->sli_rev != LPFC_SLI_REV4)
955 return NULL;
956 spin_lock_irqsave(&phba->hbalock, iflags);
957 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
958 if (rrq->vport == vport && rrq->xritag == xri &&
959 rrq->nlp_DID == did){
960 list_del(&rrq->list);
961 spin_unlock_irqrestore(&phba->hbalock, iflags);
962 return rrq;
963 }
964 }
965 spin_unlock_irqrestore(&phba->hbalock, iflags);
966 return NULL;
967}
968
969/**
970 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
971 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500972 * @ndlp: Pointer to the lpfc_node_list structure.
973 * If ndlp is NULL Remove all active RRQs for this vport from the
974 * phba->active_rrq_list and clear the rrq.
975 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500976 **/
977void
James Smart1151e3e2011-02-16 12:39:35 -0500978lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500979
980{
981 struct lpfc_hba *phba = vport->phba;
982 struct lpfc_node_rrq *rrq;
983 struct lpfc_node_rrq *nextrrq;
984 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500985 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500986
987 if (phba->sli_rev != LPFC_SLI_REV4)
988 return;
James Smart1151e3e2011-02-16 12:39:35 -0500989 if (!ndlp) {
990 lpfc_sli4_vport_delete_els_xri_aborted(vport);
991 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500992 }
James Smart1151e3e2011-02-16 12:39:35 -0500993 spin_lock_irqsave(&phba->hbalock, iflags);
994 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
995 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
996 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500997 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500998
999 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
1000 list_del(&rrq->list);
1001 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1002 }
James Smart19ca7602010-11-20 23:11:55 -05001003}
1004
1005/**
James Smart1151e3e2011-02-16 12:39:35 -05001006 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -05001007 * @phba: Pointer to HBA context object.
1008 * @ndlp: Targets nodelist pointer for this exchange.
1009 * @xritag the xri in the bitmap to test.
1010 *
1011 * This function is called with hbalock held. This function
1012 * returns 0 = rrq not active for this xri
1013 * 1 = rrq is valid for this xri.
1014 **/
James Smart1151e3e2011-02-16 12:39:35 -05001015int
1016lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -05001017 uint16_t xritag)
1018{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001019 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -05001020 if (!ndlp)
1021 return 0;
James Smartcff261f2013-12-17 20:29:47 -05001022 if (!ndlp->active_rrqs_xri_bitmap)
1023 return 0;
1024 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -05001025 return 1;
1026 else
1027 return 0;
1028}
1029
1030/**
1031 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1032 * @phba: Pointer to HBA context object.
1033 * @ndlp: nodelist pointer for this target.
1034 * @xritag: xri used in this exchange.
1035 * @rxid: Remote Exchange ID.
1036 * @send_rrq: Flag used to determine if we should send rrq els cmd.
1037 *
1038 * This function takes the hbalock.
1039 * The active bit is always set in the active rrq xri_bitmap even
1040 * if there is no slot avaiable for the other rrq information.
1041 *
1042 * returns 0 rrq actived for this xri
1043 * < 0 No memory or invalid ndlp.
1044 **/
1045int
1046lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -05001047 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -05001048{
James Smart19ca7602010-11-20 23:11:55 -05001049 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -05001050 struct lpfc_node_rrq *rrq;
1051 int empty;
1052
1053 if (!ndlp)
1054 return -EINVAL;
1055
1056 if (!phba->cfg_enable_rrq)
1057 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001058
1059 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001060 if (phba->pport->load_flag & FC_UNLOADING) {
1061 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1062 goto out;
1063 }
1064
1065 /*
1066 * set the active bit even if there is no mem available.
1067 */
1068 if (NLP_CHK_FREE_REQ(ndlp))
1069 goto out;
1070
1071 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1072 goto out;
1073
James Smartcff261f2013-12-17 20:29:47 -05001074 if (!ndlp->active_rrqs_xri_bitmap)
1075 goto out;
1076
1077 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -05001078 goto out;
1079
James Smart19ca7602010-11-20 23:11:55 -05001080 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001081 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
1082 if (!rrq) {
1083 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1084 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1085 " DID:0x%x Send:%d\n",
1086 xritag, rxid, ndlp->nlp_DID, send_rrq);
1087 return -EINVAL;
1088 }
James Smarte5771b42013-03-01 16:37:14 -05001089 if (phba->cfg_enable_rrq == 1)
1090 rrq->send_rrq = send_rrq;
1091 else
1092 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -05001093 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -04001094 rrq->rrq_stop_time = jiffies +
1095 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -05001096 rrq->ndlp = ndlp;
1097 rrq->nlp_DID = ndlp->nlp_DID;
1098 rrq->vport = ndlp->vport;
1099 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -05001100 spin_lock_irqsave(&phba->hbalock, iflags);
1101 empty = list_empty(&phba->active_rrq_list);
1102 list_add_tail(&rrq->list, &phba->active_rrq_list);
1103 phba->hba_flag |= HBA_RRQ_ACTIVE;
1104 if (empty)
1105 lpfc_worker_wake_up(phba);
1106 spin_unlock_irqrestore(&phba->hbalock, iflags);
1107 return 0;
1108out:
1109 spin_unlock_irqrestore(&phba->hbalock, iflags);
1110 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1111 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1112 " DID:0x%x Send:%d\n",
1113 xritag, rxid, ndlp->nlp_DID, send_rrq);
1114 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001115}
1116
1117/**
James Smart895427b2017-02-12 13:52:30 -08001118 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -04001119 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -05001120 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -04001121 *
James Smartdafe8ce2014-09-03 12:56:40 -04001122 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -04001123 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -04001124 * list is not empty then it is successful, it returns pointer to the newly
1125 * allocated sglq object else it returns NULL.
1126 **/
1127static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -08001128__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -04001129{
James Smart895427b2017-02-12 13:52:30 -08001130 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -04001131 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001132 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001133 struct lpfc_scsi_buf *lpfc_cmd;
1134 struct lpfc_nodelist *ndlp;
1135 int found = 0;
1136
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001137 lockdep_assert_held(&phba->hbalock);
1138
James Smart19ca7602010-11-20 23:11:55 -05001139 if (piocbq->iocb_flag & LPFC_IO_FCP) {
1140 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
1141 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -05001142 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -04001143 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -05001144 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -04001145 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1146 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1147 ndlp = NULL;
1148 else
1149 ndlp = piocbq->context_un.ndlp;
1150 } else {
James Smart19ca7602010-11-20 23:11:55 -05001151 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -04001152 }
James Smart19ca7602010-11-20 23:11:55 -05001153
James Smart895427b2017-02-12 13:52:30 -08001154 spin_lock(&phba->sli4_hba.sgl_list_lock);
1155 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -05001156 start_sglq = sglq;
1157 while (!found) {
1158 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -08001159 break;
James Smart895427b2017-02-12 13:52:30 -08001160 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1161 test_bit(sglq->sli4_lxritag,
1162 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -05001163 /* This xri has an rrq outstanding for this DID.
1164 * put it back in the list and get another xri.
1165 */
James Smart895427b2017-02-12 13:52:30 -08001166 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001167 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -08001168 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -05001169 struct lpfc_sglq, list);
1170 if (sglq == start_sglq) {
James Smart14041bd2017-06-01 21:07:01 -07001171 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001172 sglq = NULL;
1173 break;
1174 } else
1175 continue;
1176 }
1177 sglq->ndlp = ndlp;
1178 found = 1;
James Smart6d368e52011-05-24 11:44:12 -04001179 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -05001180 sglq->state = SGL_ALLOCATED;
1181 }
James Smart895427b2017-02-12 13:52:30 -08001182 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001183 return sglq;
1184}
1185
1186/**
James Smartf358dd02017-02-12 13:52:34 -08001187 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1188 * @phba: Pointer to HBA context object.
1189 * @piocb: Pointer to the iocbq.
1190 *
1191 * This function is called with the sgl_list lock held. This function
1192 * gets a new driver sglq object from the sglq list. If the
1193 * list is not empty then it is successful, it returns pointer to the newly
1194 * allocated sglq object else it returns NULL.
1195 **/
1196struct lpfc_sglq *
1197__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1198{
1199 struct list_head *lpfc_nvmet_sgl_list;
1200 struct lpfc_sglq *sglq = NULL;
1201
1202 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1203
1204 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1205
1206 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1207 if (!sglq)
1208 return NULL;
1209 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1210 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001211 return sglq;
1212}
1213
James Smarte59058c2008-08-24 21:49:00 -04001214/**
James Smart3621a712009-04-06 18:47:14 -04001215 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001216 * @phba: Pointer to HBA context object.
1217 *
1218 * This function is called with no lock held. This function
1219 * allocates a new driver iocb object from the iocb pool. If the
1220 * allocation is successful, it returns pointer to the newly
1221 * allocated iocb object else it returns NULL.
1222 **/
James Smart2e0fef82007-06-17 19:56:36 -05001223struct lpfc_iocbq *
1224lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001225{
James Smart2e0fef82007-06-17 19:56:36 -05001226 struct lpfc_iocbq * iocbq = NULL;
1227 unsigned long iflags;
1228
1229 spin_lock_irqsave(&phba->hbalock, iflags);
1230 iocbq = __lpfc_sli_get_iocbq(phba);
1231 spin_unlock_irqrestore(&phba->hbalock, iflags);
1232 return iocbq;
1233}
1234
James Smarte59058c2008-08-24 21:49:00 -04001235/**
James Smart4f774512009-05-22 14:52:35 -04001236 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1237 * @phba: Pointer to HBA context object.
1238 * @iocbq: Pointer to driver iocb object.
1239 *
1240 * This function is called with hbalock held to release driver
1241 * iocb object to the iocb pool. The iotag in the iocb object
1242 * does not change for each use of the iocb object. This function
1243 * clears all other fields of the iocb object when it is freed.
1244 * The sqlq structure that holds the xritag and phys and virtual
1245 * mappings for the scatter gather list is retrieved from the
1246 * active array of sglq. The get of the sglq pointer also clears
1247 * the entry in the array. If the status of the IO indiactes that
1248 * this IO was aborted then the sglq entry it put on the
1249 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1250 * IO has good status or fails for any other reason then the sglq
James Smart895427b2017-02-12 13:52:30 -08001251 * entry is added to the free list (lpfc_els_sgl_list).
James Smart4f774512009-05-22 14:52:35 -04001252 **/
1253static void
1254__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1255{
1256 struct lpfc_sglq *sglq;
1257 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001258 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001259 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001260
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001261 lockdep_assert_held(&phba->hbalock);
1262
James Smart4f774512009-05-22 14:52:35 -04001263 if (iocbq->sli4_xritag == NO_XRI)
1264 sglq = NULL;
1265 else
James Smart6d368e52011-05-24 11:44:12 -04001266 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1267
James Smart0e9bb8d2013-03-01 16:35:12 -05001268
James Smart4f774512009-05-22 14:52:35 -04001269 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001270 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1271 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1272 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001273 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001274 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001275 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001276 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1277 spin_unlock_irqrestore(
1278 &phba->sli4_hba.sgl_list_lock, iflag);
1279 goto out;
1280 }
1281
James Smart895427b2017-02-12 13:52:30 -08001282 pring = phba->sli4_hba.els_wq->pring;
James Smart4f774512009-05-22 14:52:35 -04001283 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1284 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001285 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1286 iflag);
James Smart341af102010-01-26 23:07:37 -05001287 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001288 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001289 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001290 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001291 } else {
James Smart895427b2017-02-12 13:52:30 -08001292 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1293 iflag);
James Smart4f774512009-05-22 14:52:35 -04001294 sglq->state = SGL_FREED;
1295 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001296 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001297 &phba->sli4_hba.lpfc_els_sgl_list);
1298 spin_unlock_irqrestore(
1299 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001300
1301 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001302 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001303 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001304 }
James Smart4f774512009-05-22 14:52:35 -04001305 }
1306
James Smartf358dd02017-02-12 13:52:34 -08001307out:
James Smart4f774512009-05-22 14:52:35 -04001308 /*
1309 * Clean all volatile data fields, preserve iotag and node struct.
1310 */
1311 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001312 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001313 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001314 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1315 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001316 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1317}
1318
James Smart2a9bf3d2010-06-07 15:24:45 -04001319
James Smart4f774512009-05-22 14:52:35 -04001320/**
James Smart3772a992009-05-22 14:50:54 -04001321 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1322 * @phba: Pointer to HBA context object.
1323 * @iocbq: Pointer to driver iocb object.
1324 *
1325 * This function is called with hbalock held to release driver
1326 * iocb object to the iocb pool. The iotag in the iocb object
1327 * does not change for each use of the iocb object. This function
1328 * clears all other fields of the iocb object when it is freed.
1329 **/
1330static void
1331__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1332{
1333 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1334
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001335 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001336
1337 /*
James Smart3772a992009-05-22 14:50:54 -04001338 * Clean all volatile data fields, preserve iotag and node struct.
1339 */
1340 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1341 iocbq->sli4_xritag = NO_XRI;
1342 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1343}
1344
1345/**
James Smart3621a712009-04-06 18:47:14 -04001346 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001347 * @phba: Pointer to HBA context object.
1348 * @iocbq: Pointer to driver iocb object.
1349 *
1350 * This function is called with hbalock held to release driver
1351 * iocb object to the iocb pool. The iotag in the iocb object
1352 * does not change for each use of the iocb object. This function
1353 * clears all other fields of the iocb object when it is freed.
1354 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001355static void
James Smart2e0fef82007-06-17 19:56:36 -05001356__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1357{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001358 lockdep_assert_held(&phba->hbalock);
1359
James Smart3772a992009-05-22 14:50:54 -04001360 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001361 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001362}
1363
James Smarte59058c2008-08-24 21:49:00 -04001364/**
James Smart3621a712009-04-06 18:47:14 -04001365 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001366 * @phba: Pointer to HBA context object.
1367 * @iocbq: Pointer to driver iocb object.
1368 *
1369 * This function is called with no lock held to release the iocb to
1370 * iocb pool.
1371 **/
James Smart2e0fef82007-06-17 19:56:36 -05001372void
1373lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1374{
1375 unsigned long iflags;
1376
1377 /*
1378 * Clean all volatile data fields, preserve iotag and node struct.
1379 */
1380 spin_lock_irqsave(&phba->hbalock, iflags);
1381 __lpfc_sli_release_iocbq(phba, iocbq);
1382 spin_unlock_irqrestore(&phba->hbalock, iflags);
1383}
1384
James Smarte59058c2008-08-24 21:49:00 -04001385/**
James Smarta257bf92009-04-06 18:48:10 -04001386 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1387 * @phba: Pointer to HBA context object.
1388 * @iocblist: List of IOCBs.
1389 * @ulpstatus: ULP status in IOCB command field.
1390 * @ulpWord4: ULP word-4 in IOCB command field.
1391 *
1392 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1393 * on the list by invoking the complete callback function associated with the
1394 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1395 * fields.
1396 **/
1397void
1398lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1399 uint32_t ulpstatus, uint32_t ulpWord4)
1400{
1401 struct lpfc_iocbq *piocb;
1402
1403 while (!list_empty(iocblist)) {
1404 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001405 if (!piocb->iocb_cmpl)
1406 lpfc_sli_release_iocbq(phba, piocb);
1407 else {
1408 piocb->iocb.ulpStatus = ulpstatus;
1409 piocb->iocb.un.ulpWord[4] = ulpWord4;
1410 (piocb->iocb_cmpl) (phba, piocb, piocb);
1411 }
1412 }
1413 return;
1414}
1415
1416/**
James Smart3621a712009-04-06 18:47:14 -04001417 * lpfc_sli_iocb_cmd_type - Get the iocb type
1418 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001419 *
1420 * This function is called by ring event handler function to get the iocb type.
1421 * This function translates the iocb command to an iocb command type used to
1422 * decide the final disposition of each completed IOCB.
1423 * The function returns
1424 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1425 * LPFC_SOL_IOCB if it is a solicited iocb completion
1426 * LPFC_ABORT_IOCB if it is an abort iocb
1427 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1428 *
1429 * The caller is not required to hold any lock.
1430 **/
dea31012005-04-17 16:05:31 -05001431static lpfc_iocb_type
1432lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1433{
1434 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1435
1436 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1437 return 0;
1438
1439 switch (iocb_cmnd) {
1440 case CMD_XMIT_SEQUENCE_CR:
1441 case CMD_XMIT_SEQUENCE_CX:
1442 case CMD_XMIT_BCAST_CN:
1443 case CMD_XMIT_BCAST_CX:
1444 case CMD_ELS_REQUEST_CR:
1445 case CMD_ELS_REQUEST_CX:
1446 case CMD_CREATE_XRI_CR:
1447 case CMD_CREATE_XRI_CX:
1448 case CMD_GET_RPI_CN:
1449 case CMD_XMIT_ELS_RSP_CX:
1450 case CMD_GET_RPI_CR:
1451 case CMD_FCP_IWRITE_CR:
1452 case CMD_FCP_IWRITE_CX:
1453 case CMD_FCP_IREAD_CR:
1454 case CMD_FCP_IREAD_CX:
1455 case CMD_FCP_ICMND_CR:
1456 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001457 case CMD_FCP_TSEND_CX:
1458 case CMD_FCP_TRSP_CX:
1459 case CMD_FCP_TRECEIVE_CX:
1460 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001461 case CMD_ADAPTER_MSG:
1462 case CMD_ADAPTER_DUMP:
1463 case CMD_XMIT_SEQUENCE64_CR:
1464 case CMD_XMIT_SEQUENCE64_CX:
1465 case CMD_XMIT_BCAST64_CN:
1466 case CMD_XMIT_BCAST64_CX:
1467 case CMD_ELS_REQUEST64_CR:
1468 case CMD_ELS_REQUEST64_CX:
1469 case CMD_FCP_IWRITE64_CR:
1470 case CMD_FCP_IWRITE64_CX:
1471 case CMD_FCP_IREAD64_CR:
1472 case CMD_FCP_IREAD64_CX:
1473 case CMD_FCP_ICMND64_CR:
1474 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001475 case CMD_FCP_TSEND64_CX:
1476 case CMD_FCP_TRSP64_CX:
1477 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001478 case CMD_GEN_REQUEST64_CR:
1479 case CMD_GEN_REQUEST64_CX:
1480 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001481 case DSSCMD_IWRITE64_CR:
1482 case DSSCMD_IWRITE64_CX:
1483 case DSSCMD_IREAD64_CR:
1484 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001485 type = LPFC_SOL_IOCB;
1486 break;
1487 case CMD_ABORT_XRI_CN:
1488 case CMD_ABORT_XRI_CX:
1489 case CMD_CLOSE_XRI_CN:
1490 case CMD_CLOSE_XRI_CX:
1491 case CMD_XRI_ABORTED_CX:
1492 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001493 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001494 type = LPFC_ABORT_IOCB;
1495 break;
1496 case CMD_RCV_SEQUENCE_CX:
1497 case CMD_RCV_ELS_REQ_CX:
1498 case CMD_RCV_SEQUENCE64_CX:
1499 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001500 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001501 case CMD_IOCB_RCV_SEQ64_CX:
1502 case CMD_IOCB_RCV_ELS64_CX:
1503 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001504 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001505 type = LPFC_UNSOL_IOCB;
1506 break;
James Smart3163f722008-02-08 18:50:25 -05001507 case CMD_IOCB_XMIT_MSEQ64_CR:
1508 case CMD_IOCB_XMIT_MSEQ64_CX:
1509 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1510 case CMD_IOCB_RCV_ELS_LIST64_CX:
1511 case CMD_IOCB_CLOSE_EXTENDED_CN:
1512 case CMD_IOCB_ABORT_EXTENDED_CN:
1513 case CMD_IOCB_RET_HBQE64_CN:
1514 case CMD_IOCB_FCP_IBIDIR64_CR:
1515 case CMD_IOCB_FCP_IBIDIR64_CX:
1516 case CMD_IOCB_FCP_ITASKMGT64_CX:
1517 case CMD_IOCB_LOGENTRY_CN:
1518 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1519 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001520 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001521 type = LPFC_UNKNOWN_IOCB;
1522 break;
dea31012005-04-17 16:05:31 -05001523 default:
1524 type = LPFC_UNKNOWN_IOCB;
1525 break;
1526 }
1527
1528 return type;
1529}
1530
James Smarte59058c2008-08-24 21:49:00 -04001531/**
James Smart3621a712009-04-06 18:47:14 -04001532 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001533 * @phba: Pointer to HBA context object.
1534 *
1535 * This function is called from SLI initialization code
1536 * to configure every ring of the HBA's SLI interface. The
1537 * caller is not required to hold any lock. This function issues
1538 * a config_ring mailbox command for each ring.
1539 * This function returns zero if successful else returns a negative
1540 * error code.
1541 **/
dea31012005-04-17 16:05:31 -05001542static int
James Smarted957682007-06-17 19:56:37 -05001543lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001544{
1545 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001546 LPFC_MBOXQ_t *pmb;
1547 MAILBOX_t *pmbox;
1548 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001549
James Smarted957682007-06-17 19:56:37 -05001550 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1551 if (!pmb)
1552 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001553 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001554 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001555 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001556 lpfc_config_ring(phba, i, pmb);
1557 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1558 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001560 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001561 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1562 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001563 rc, pmbox->mbxCommand,
1564 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001565 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001566 ret = -ENXIO;
1567 break;
dea31012005-04-17 16:05:31 -05001568 }
1569 }
James Smarted957682007-06-17 19:56:37 -05001570 mempool_free(pmb, phba->mbox_mem_pool);
1571 return ret;
dea31012005-04-17 16:05:31 -05001572}
1573
James Smarte59058c2008-08-24 21:49:00 -04001574/**
James Smart3621a712009-04-06 18:47:14 -04001575 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001576 * @phba: Pointer to HBA context object.
1577 * @pring: Pointer to driver SLI ring object.
1578 * @piocb: Pointer to the driver iocb object.
1579 *
1580 * This function is called with hbalock held. The function adds the
1581 * new iocb to txcmplq of the given ring. This function always returns
1582 * 0. If this function is called for ELS ring, this function checks if
1583 * there is a vport associated with the ELS command. This function also
1584 * starts els_tmofunc timer if this is an ELS command.
1585 **/
dea31012005-04-17 16:05:31 -05001586static int
James Smart2e0fef82007-06-17 19:56:36 -05001587lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1588 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001589{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001590 lockdep_assert_held(&phba->hbalock);
1591
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001592 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001593
dea31012005-04-17 16:05:31 -05001594 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001595 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001596
James Smart92d7f7b2007-06-17 19:56:38 -05001597 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1598 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001599 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1600 BUG_ON(!piocb->vport);
1601 if (!(piocb->vport->load_flag & FC_UNLOADING))
1602 mod_timer(&piocb->vport->els_tmofunc,
1603 jiffies +
1604 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1605 }
dea31012005-04-17 16:05:31 -05001606
James Smart2e0fef82007-06-17 19:56:36 -05001607 return 0;
dea31012005-04-17 16:05:31 -05001608}
1609
James Smarte59058c2008-08-24 21:49:00 -04001610/**
James Smart3621a712009-04-06 18:47:14 -04001611 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001612 * @phba: Pointer to HBA context object.
1613 * @pring: Pointer to driver SLI ring object.
1614 *
1615 * This function is called with hbalock held to get next
1616 * iocb in txq of the given ring. If there is any iocb in
1617 * the txq, the function returns first iocb in the list after
1618 * removing the iocb from the list, else it returns NULL.
1619 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001620struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001621lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001622{
dea31012005-04-17 16:05:31 -05001623 struct lpfc_iocbq *cmd_iocb;
1624
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001625 lockdep_assert_held(&phba->hbalock);
1626
James Smart858c9f62007-06-17 19:56:39 -05001627 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001628 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001629}
1630
James Smarte59058c2008-08-24 21:49:00 -04001631/**
James Smart3621a712009-04-06 18:47:14 -04001632 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001633 * @phba: Pointer to HBA context object.
1634 * @pring: Pointer to driver SLI ring object.
1635 *
1636 * This function is called with hbalock held and the caller must post the
1637 * iocb without releasing the lock. If the caller releases the lock,
1638 * iocb slot returned by the function is not guaranteed to be available.
1639 * The function returns pointer to the next available iocb slot if there
1640 * is available slot in the ring, else it returns NULL.
1641 * If the get index of the ring is ahead of the put index, the function
1642 * will post an error attention event to the worker thread to take the
1643 * HBA to offline state.
1644 **/
dea31012005-04-17 16:05:31 -05001645static IOCB_t *
1646lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1647{
James Smart34b02dc2008-08-24 21:49:55 -04001648 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001649 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001650
1651 lockdep_assert_held(&phba->hbalock);
1652
James Smart7e56aa22012-08-03 12:35:34 -04001653 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1654 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1655 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001656
James Smart7e56aa22012-08-03 12:35:34 -04001657 if (unlikely(pring->sli.sli3.local_getidx ==
1658 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001659
James Smart7e56aa22012-08-03 12:35:34 -04001660 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001661
James Smart7e56aa22012-08-03 12:35:34 -04001662 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001663 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001664 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001665 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001666 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001667 pring->sli.sli3.local_getidx,
1668 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001669
James Smart2e0fef82007-06-17 19:56:36 -05001670 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001671 /*
1672 * All error attention handlers are posted to
1673 * worker thread
1674 */
1675 phba->work_ha |= HA_ERATT;
1676 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001677
James Smart5e9d9b82008-06-14 22:52:53 -04001678 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001679
1680 return NULL;
1681 }
1682
James Smart7e56aa22012-08-03 12:35:34 -04001683 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001684 return NULL;
1685 }
1686
James Smarted957682007-06-17 19:56:37 -05001687 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001688}
1689
James Smarte59058c2008-08-24 21:49:00 -04001690/**
James Smart3621a712009-04-06 18:47:14 -04001691 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001692 * @phba: Pointer to HBA context object.
1693 * @iocbq: Pointer to driver iocb object.
1694 *
1695 * This function gets an iotag for the iocb. If there is no unused iotag and
1696 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1697 * array and assigns a new iotag.
1698 * The function returns the allocated iotag if successful, else returns zero.
1699 * Zero is not a valid iotag.
1700 * The caller is not required to hold any lock.
1701 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001702uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001703lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001704{
James Smart2e0fef82007-06-17 19:56:36 -05001705 struct lpfc_iocbq **new_arr;
1706 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001707 size_t new_len;
1708 struct lpfc_sli *psli = &phba->sli;
1709 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001710
James Smart2e0fef82007-06-17 19:56:36 -05001711 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001712 iotag = psli->last_iotag;
1713 if(++iotag < psli->iocbq_lookup_len) {
1714 psli->last_iotag = iotag;
1715 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001716 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001717 iocbq->iotag = iotag;
1718 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001719 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001720 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1721 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001722 spin_unlock_irq(&phba->hbalock);
Kees Cook6396bb22018-06-12 14:03:40 -07001723 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001724 GFP_KERNEL);
1725 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001726 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001727 old_arr = psli->iocbq_lookup;
1728 if (new_len <= psli->iocbq_lookup_len) {
1729 /* highly unprobable case */
1730 kfree(new_arr);
1731 iotag = psli->last_iotag;
1732 if(++iotag < psli->iocbq_lookup_len) {
1733 psli->last_iotag = iotag;
1734 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001735 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001736 iocbq->iotag = iotag;
1737 return iotag;
1738 }
James Smart2e0fef82007-06-17 19:56:36 -05001739 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001740 return 0;
1741 }
1742 if (psli->iocbq_lookup)
1743 memcpy(new_arr, old_arr,
1744 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001745 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001746 psli->iocbq_lookup = new_arr;
1747 psli->iocbq_lookup_len = new_len;
1748 psli->last_iotag = iotag;
1749 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001750 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001751 iocbq->iotag = iotag;
1752 kfree(old_arr);
1753 return iotag;
1754 }
James Smart8f6d98d2006-08-01 07:34:00 -04001755 } else
James Smart2e0fef82007-06-17 19:56:36 -05001756 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001757
James Smartbc739052010-08-04 16:11:18 -04001758 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001759 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1760 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001761
James Bottomley604a3e32005-10-29 10:28:33 -05001762 return 0;
dea31012005-04-17 16:05:31 -05001763}
1764
James Smarte59058c2008-08-24 21:49:00 -04001765/**
James Smart3621a712009-04-06 18:47:14 -04001766 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001767 * @phba: Pointer to HBA context object.
1768 * @pring: Pointer to driver SLI ring object.
1769 * @iocb: Pointer to iocb slot in the ring.
1770 * @nextiocb: Pointer to driver iocb object which need to be
1771 * posted to firmware.
1772 *
1773 * This function is called with hbalock held to post a new iocb to
1774 * the firmware. This function copies the new iocb to ring iocb slot and
1775 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1776 * a completion call back for this iocb else the function will free the
1777 * iocb object.
1778 **/
dea31012005-04-17 16:05:31 -05001779static void
1780lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1781 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1782{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001783 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001784 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001785 * Set up an iotag
dea31012005-04-17 16:05:31 -05001786 */
James Bottomley604a3e32005-10-29 10:28:33 -05001787 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001788
James Smarte2a0a9d2008-12-04 22:40:02 -05001789
James Smarta58cbd52007-08-02 11:09:43 -04001790 if (pring->ringno == LPFC_ELS_RING) {
1791 lpfc_debugfs_slow_ring_trc(phba,
1792 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1793 *(((uint32_t *) &nextiocb->iocb) + 4),
1794 *(((uint32_t *) &nextiocb->iocb) + 6),
1795 *(((uint32_t *) &nextiocb->iocb) + 7));
1796 }
1797
dea31012005-04-17 16:05:31 -05001798 /*
1799 * Issue iocb command to adapter
1800 */
James Smart92d7f7b2007-06-17 19:56:38 -05001801 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001802 wmb();
1803 pring->stats.iocb_cmd++;
1804
1805 /*
1806 * If there is no completion routine to call, we can release the
1807 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1808 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1809 */
1810 if (nextiocb->iocb_cmpl)
1811 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001812 else
James Smart2e0fef82007-06-17 19:56:36 -05001813 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001814
1815 /*
1816 * Let the HBA know what IOCB slot will be the next one the
1817 * driver will put a command into.
1818 */
James Smart7e56aa22012-08-03 12:35:34 -04001819 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1820 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001821}
1822
James Smarte59058c2008-08-24 21:49:00 -04001823/**
James Smart3621a712009-04-06 18:47:14 -04001824 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001825 * @phba: Pointer to HBA context object.
1826 * @pring: Pointer to driver SLI ring object.
1827 *
1828 * The caller is not required to hold any lock for calling this function.
1829 * This function updates the chip attention bits for the ring to inform firmware
1830 * that there are pending work to be done for this ring and requests an
1831 * interrupt when there is space available in the ring. This function is
1832 * called when the driver is unable to post more iocbs to the ring due
1833 * to unavailability of space in the ring.
1834 **/
dea31012005-04-17 16:05:31 -05001835static void
James Smart2e0fef82007-06-17 19:56:36 -05001836lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001837{
1838 int ringno = pring->ringno;
1839
1840 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1841
1842 wmb();
1843
1844 /*
1845 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1846 * The HBA will tell us when an IOCB entry is available.
1847 */
1848 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1849 readl(phba->CAregaddr); /* flush */
1850
1851 pring->stats.iocb_cmd_full++;
1852}
1853
James Smarte59058c2008-08-24 21:49:00 -04001854/**
James Smart3621a712009-04-06 18:47:14 -04001855 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001856 * @phba: Pointer to HBA context object.
1857 * @pring: Pointer to driver SLI ring object.
1858 *
1859 * This function updates the chip attention register bit for the
1860 * given ring to inform HBA that there is more work to be done
1861 * in this ring. The caller is not required to hold any lock.
1862 **/
dea31012005-04-17 16:05:31 -05001863static void
James Smart2e0fef82007-06-17 19:56:36 -05001864lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001865{
1866 int ringno = pring->ringno;
1867
1868 /*
1869 * Tell the HBA that there is work to do in this ring.
1870 */
James Smart34b02dc2008-08-24 21:49:55 -04001871 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1872 wmb();
1873 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1874 readl(phba->CAregaddr); /* flush */
1875 }
dea31012005-04-17 16:05:31 -05001876}
1877
James Smarte59058c2008-08-24 21:49:00 -04001878/**
James Smart3621a712009-04-06 18:47:14 -04001879 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001880 * @phba: Pointer to HBA context object.
1881 * @pring: Pointer to driver SLI ring object.
1882 *
1883 * This function is called with hbalock held to post pending iocbs
1884 * in the txq to the firmware. This function is called when driver
1885 * detects space available in the ring.
1886 **/
dea31012005-04-17 16:05:31 -05001887static void
James Smart2e0fef82007-06-17 19:56:36 -05001888lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001889{
1890 IOCB_t *iocb;
1891 struct lpfc_iocbq *nextiocb;
1892
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001893 lockdep_assert_held(&phba->hbalock);
1894
dea31012005-04-17 16:05:31 -05001895 /*
1896 * Check to see if:
1897 * (a) there is anything on the txq to send
1898 * (b) link is up
1899 * (c) link attention events can be processed (fcp ring only)
1900 * (d) IOCB processing is not blocked by the outstanding mbox command.
1901 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001902
1903 if (lpfc_is_link_up(phba) &&
1904 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001905 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001906 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001907
1908 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1909 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1910 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1911
1912 if (iocb)
1913 lpfc_sli_update_ring(phba, pring);
1914 else
1915 lpfc_sli_update_full_ring(phba, pring);
1916 }
1917
1918 return;
1919}
1920
James Smarte59058c2008-08-24 21:49:00 -04001921/**
James Smart3621a712009-04-06 18:47:14 -04001922 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001923 * @phba: Pointer to HBA context object.
1924 * @hbqno: HBQ number.
1925 *
1926 * This function is called with hbalock held to get the next
1927 * available slot for the given HBQ. If there is free slot
1928 * available for the HBQ it will return pointer to the next available
1929 * HBQ entry else it will return NULL.
1930 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001931static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001932lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1933{
1934 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1935
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001936 lockdep_assert_held(&phba->hbalock);
1937
James Smarted957682007-06-17 19:56:37 -05001938 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1939 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1940 hbqp->next_hbqPutIdx = 0;
1941
1942 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001943 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001944 uint32_t getidx = le32_to_cpu(raw_index);
1945
1946 hbqp->local_hbqGetIdx = getidx;
1947
1948 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1949 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001950 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001951 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001952 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001953 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001954 hbqp->entry_count);
1955
1956 phba->link_state = LPFC_HBA_ERROR;
1957 return NULL;
1958 }
1959
1960 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1961 return NULL;
1962 }
1963
James Smart51ef4c22007-08-02 11:10:31 -04001964 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1965 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001966}
1967
James Smarte59058c2008-08-24 21:49:00 -04001968/**
James Smart3621a712009-04-06 18:47:14 -04001969 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001970 * @phba: Pointer to HBA context object.
1971 *
1972 * This function is called with no lock held to free all the
1973 * hbq buffers while uninitializing the SLI interface. It also
1974 * frees the HBQ buffers returned by the firmware but not yet
1975 * processed by the upper layers.
1976 **/
James Smarted957682007-06-17 19:56:37 -05001977void
1978lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1979{
James Smart92d7f7b2007-06-17 19:56:38 -05001980 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1981 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001982 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001983 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001984
James Smart51ef4c22007-08-02 11:10:31 -04001985 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001986 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001987 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001988 for (i = 0; i < hbq_count; ++i) {
1989 list_for_each_entry_safe(dmabuf, next_dmabuf,
1990 &phba->hbqs[i].hbq_buffer_list, list) {
1991 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1992 list_del(&hbq_buf->dbuf.list);
1993 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1994 }
James Smarta8adb832007-10-27 13:37:53 -04001995 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001996 }
James Smart3163f722008-02-08 18:50:25 -05001997
1998 /* Mark the HBQs not in use */
1999 phba->hbq_in_use = 0;
2000 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05002001}
2002
James Smarte59058c2008-08-24 21:49:00 -04002003/**
James Smart3621a712009-04-06 18:47:14 -04002004 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002005 * @phba: Pointer to HBA context object.
2006 * @hbqno: HBQ number.
2007 * @hbq_buf: Pointer to HBQ buffer.
2008 *
2009 * This function is called with the hbalock held to post a
2010 * hbq buffer to the firmware. If the function finds an empty
2011 * slot in the HBQ, it will post the buffer. The function will return
2012 * pointer to the hbq entry if it successfully post the buffer
2013 * else it will return NULL.
2014 **/
James Smart3772a992009-05-22 14:50:54 -04002015static int
James Smarted957682007-06-17 19:56:37 -05002016lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05002017 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05002018{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002019 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002020 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2021}
2022
2023/**
2024 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2025 * @phba: Pointer to HBA context object.
2026 * @hbqno: HBQ number.
2027 * @hbq_buf: Pointer to HBQ buffer.
2028 *
2029 * This function is called with the hbalock held to post a hbq buffer to the
2030 * firmware. If the function finds an empty slot in the HBQ, it will post the
2031 * buffer and place it on the hbq_buffer_list. The function will return zero if
2032 * it successfully post the buffer else it will return an error.
2033 **/
2034static int
2035lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2036 struct hbq_dmabuf *hbq_buf)
2037{
James Smarted957682007-06-17 19:56:37 -05002038 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05002039 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05002040
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002041 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05002042 /* Get next HBQ entry slot to use */
2043 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2044 if (hbqe) {
2045 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2046
James Smart92d7f7b2007-06-17 19:56:38 -05002047 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2048 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08002049 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05002050 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002051 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2052 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2053 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05002054 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2055 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05002056 /* flush */
James Smarted957682007-06-17 19:56:37 -05002057 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04002058 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04002059 return 0;
2060 } else
2061 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05002062}
2063
James Smart4f774512009-05-22 14:52:35 -04002064/**
2065 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2066 * @phba: Pointer to HBA context object.
2067 * @hbqno: HBQ number.
2068 * @hbq_buf: Pointer to HBQ buffer.
2069 *
2070 * This function is called with the hbalock held to post an RQE to the SLI4
2071 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2072 * the hbq_buffer_list and return zero, otherwise it will return an error.
2073 **/
2074static int
2075lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2076 struct hbq_dmabuf *hbq_buf)
2077{
2078 int rc;
2079 struct lpfc_rqe hrqe;
2080 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08002081 struct lpfc_queue *hrq;
2082 struct lpfc_queue *drq;
2083
2084 if (hbqno != LPFC_ELS_HBQ)
2085 return 1;
2086 hrq = phba->sli4_hba.hdr_rq;
2087 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04002088
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002089 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04002090 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2091 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2092 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2093 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08002094 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04002095 if (rc < 0)
2096 return rc;
James Smart895427b2017-02-12 13:52:30 -08002097 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04002098 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2099 return 0;
2100}
2101
James Smarte59058c2008-08-24 21:49:00 -04002102/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05002103static struct lpfc_hbq_init lpfc_els_hbq = {
2104 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05002105 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05002106 .mask_count = 0,
2107 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04002108 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05002109 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04002110 .init_count = 40,
2111 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05002112};
James Smarted957682007-06-17 19:56:37 -05002113
James Smarte59058c2008-08-24 21:49:00 -04002114/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04002115struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05002116 &lpfc_els_hbq,
2117};
2118
James Smarte59058c2008-08-24 21:49:00 -04002119/**
James Smart3621a712009-04-06 18:47:14 -04002120 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04002121 * @phba: Pointer to HBA context object.
2122 * @hbqno: HBQ number.
2123 * @count: Number of HBQ buffers to be posted.
2124 *
James Smartd7c255b2008-08-24 21:50:00 -04002125 * This function is called with no lock held to post more hbq buffers to the
2126 * given HBQ. The function returns the number of HBQ buffers successfully
2127 * posted.
James Smarte59058c2008-08-24 21:49:00 -04002128 **/
James Smart311464e2007-08-02 11:10:37 -04002129static int
James Smart92d7f7b2007-06-17 19:56:38 -05002130lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2131{
James Smartd7c255b2008-08-24 21:50:00 -04002132 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05002133 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05002134 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04002135 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002136 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04002137 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04002138
James Smartd7c255b2008-08-24 21:50:00 -04002139 if ((phba->hbqs[hbqno].buffer_count + count) >
2140 lpfc_hbq_defs[hbqno]->entry_count)
2141 count = lpfc_hbq_defs[hbqno]->entry_count -
2142 phba->hbqs[hbqno].buffer_count;
2143 if (!count)
2144 return 0;
2145 /* Allocate HBQ entries */
2146 for (i = 0; i < count; i++) {
2147 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2148 if (!hbq_buffer)
2149 break;
2150 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2151 }
James Smart3163f722008-02-08 18:50:25 -05002152 /* Check whether HBQ is still in use */
2153 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002154 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04002155 goto err;
2156 while (!list_empty(&hbq_buf_list)) {
2157 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2158 dbuf.list);
2159 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2160 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04002161 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04002162 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04002163 posted++;
2164 } else
James Smart51ef4c22007-08-02 11:10:31 -04002165 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05002166 }
James Smart3163f722008-02-08 18:50:25 -05002167 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04002168 return posted;
2169err:
2170 spin_unlock_irqrestore(&phba->hbalock, flags);
2171 while (!list_empty(&hbq_buf_list)) {
2172 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2173 dbuf.list);
2174 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2175 }
James Smart92d7f7b2007-06-17 19:56:38 -05002176 return 0;
James Smarted957682007-06-17 19:56:37 -05002177}
2178
James Smarte59058c2008-08-24 21:49:00 -04002179/**
James Smart3621a712009-04-06 18:47:14 -04002180 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04002181 * @phba: Pointer to HBA context object.
2182 * @qno: HBQ number.
2183 *
2184 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04002185 * is called with no lock held. The function returns the number of HBQ entries
2186 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002187 **/
James Smarted957682007-06-17 19:56:37 -05002188int
James Smart92d7f7b2007-06-17 19:56:38 -05002189lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002190{
James Smartdef9c7a2009-12-21 17:02:28 -05002191 if (phba->sli_rev == LPFC_SLI_REV4)
2192 return 0;
2193 else
2194 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2195 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05002196}
2197
James Smarte59058c2008-08-24 21:49:00 -04002198/**
James Smart3621a712009-04-06 18:47:14 -04002199 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002200 * @phba: Pointer to HBA context object.
2201 * @qno: HBQ queue number.
2202 *
2203 * This function is called from SLI initialization code path with
2204 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002205 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002206 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002207static int
James Smart92d7f7b2007-06-17 19:56:38 -05002208lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002209{
James Smartdef9c7a2009-12-21 17:02:28 -05002210 if (phba->sli_rev == LPFC_SLI_REV4)
2211 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002212 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002213 else
2214 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2215 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002216}
2217
James Smarte59058c2008-08-24 21:49:00 -04002218/**
James Smart3772a992009-05-22 14:50:54 -04002219 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2220 * @phba: Pointer to HBA context object.
2221 * @hbqno: HBQ number.
2222 *
2223 * This function removes the first hbq buffer on an hbq list and returns a
2224 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2225 **/
2226static struct hbq_dmabuf *
2227lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2228{
2229 struct lpfc_dmabuf *d_buf;
2230
2231 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2232 if (!d_buf)
2233 return NULL;
2234 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2235}
2236
2237/**
James Smart2d7dbc42017-02-12 13:52:35 -08002238 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2239 * @phba: Pointer to HBA context object.
2240 * @hbqno: HBQ number.
2241 *
2242 * This function removes the first RQ buffer on an RQ buffer list and returns a
2243 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2244 **/
2245static struct rqb_dmabuf *
2246lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2247{
2248 struct lpfc_dmabuf *h_buf;
2249 struct lpfc_rqb *rqbp;
2250
2251 rqbp = hrq->rqbp;
2252 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2253 struct lpfc_dmabuf, list);
2254 if (!h_buf)
2255 return NULL;
2256 rqbp->buffer_count--;
2257 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2258}
2259
2260/**
James Smart3621a712009-04-06 18:47:14 -04002261 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002262 * @phba: Pointer to HBA context object.
2263 * @tag: Tag of the hbq buffer.
2264 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002265 * This function searches for the hbq buffer associated with the given tag in
2266 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2267 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002268 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002269static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002270lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2271{
James Smart92d7f7b2007-06-17 19:56:38 -05002272 struct lpfc_dmabuf *d_buf;
2273 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002274 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002275
James Smart51ef4c22007-08-02 11:10:31 -04002276 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002277 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002278 return NULL;
2279
James Smart3772a992009-05-22 14:50:54 -04002280 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002281 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002282 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002283 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002284 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002285 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002286 }
2287 }
James Smart3772a992009-05-22 14:50:54 -04002288 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002289 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002290 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002291 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002292 return NULL;
James Smarted957682007-06-17 19:56:37 -05002293}
2294
James Smarte59058c2008-08-24 21:49:00 -04002295/**
James Smart3621a712009-04-06 18:47:14 -04002296 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002297 * @phba: Pointer to HBA context object.
2298 * @hbq_buffer: Pointer to HBQ buffer.
2299 *
2300 * This function is called with hbalock. This function gives back
2301 * the hbq buffer to firmware. If the HBQ does not have space to
2302 * post the buffer, it will free the buffer.
2303 **/
James Smarted957682007-06-17 19:56:37 -05002304void
James Smart51ef4c22007-08-02 11:10:31 -04002305lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002306{
2307 uint32_t hbqno;
2308
James Smart51ef4c22007-08-02 11:10:31 -04002309 if (hbq_buffer) {
2310 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002311 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002312 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002313 }
2314}
2315
James Smarte59058c2008-08-24 21:49:00 -04002316/**
James Smart3621a712009-04-06 18:47:14 -04002317 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002318 * @mbxCommand: mailbox command code.
2319 *
2320 * This function is called by the mailbox event handler function to verify
2321 * that the completed mailbox command is a legitimate mailbox command. If the
2322 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2323 * and the mailbox event handler will take the HBA offline.
2324 **/
dea31012005-04-17 16:05:31 -05002325static int
2326lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2327{
2328 uint8_t ret;
2329
2330 switch (mbxCommand) {
2331 case MBX_LOAD_SM:
2332 case MBX_READ_NV:
2333 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002334 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002335 case MBX_RUN_BIU_DIAG:
2336 case MBX_INIT_LINK:
2337 case MBX_DOWN_LINK:
2338 case MBX_CONFIG_LINK:
2339 case MBX_CONFIG_RING:
2340 case MBX_RESET_RING:
2341 case MBX_READ_CONFIG:
2342 case MBX_READ_RCONFIG:
2343 case MBX_READ_SPARM:
2344 case MBX_READ_STATUS:
2345 case MBX_READ_RPI:
2346 case MBX_READ_XRI:
2347 case MBX_READ_REV:
2348 case MBX_READ_LNK_STAT:
2349 case MBX_REG_LOGIN:
2350 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002351 case MBX_CLEAR_LA:
2352 case MBX_DUMP_MEMORY:
2353 case MBX_DUMP_CONTEXT:
2354 case MBX_RUN_DIAGS:
2355 case MBX_RESTART:
2356 case MBX_UPDATE_CFG:
2357 case MBX_DOWN_LOAD:
2358 case MBX_DEL_LD_ENTRY:
2359 case MBX_RUN_PROGRAM:
2360 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002361 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002362 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002363 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002364 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002365 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002366 case MBX_LOAD_AREA:
2367 case MBX_RUN_BIU_DIAG64:
2368 case MBX_CONFIG_PORT:
2369 case MBX_READ_SPARM64:
2370 case MBX_READ_RPI64:
2371 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002372 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002373 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002374 case MBX_SET_DEBUG:
2375 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002376 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002377 case MBX_REG_VPI:
2378 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002379 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002380 case MBX_PORT_CAPABILITIES:
2381 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002382 case MBX_SLI4_CONFIG:
2383 case MBX_SLI4_REQ_FTRS:
2384 case MBX_REG_FCFI:
2385 case MBX_UNREG_FCFI:
2386 case MBX_REG_VFI:
2387 case MBX_UNREG_VFI:
2388 case MBX_INIT_VPI:
2389 case MBX_INIT_VFI:
2390 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002391 case MBX_READ_EVENT_LOG_STATUS:
2392 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002393 case MBX_SECURITY_MGMT:
2394 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002395 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002396 ret = mbxCommand;
2397 break;
2398 default:
2399 ret = MBX_SHUTDOWN;
2400 break;
2401 }
James Smart2e0fef82007-06-17 19:56:36 -05002402 return ret;
dea31012005-04-17 16:05:31 -05002403}
James Smarte59058c2008-08-24 21:49:00 -04002404
2405/**
James Smart3621a712009-04-06 18:47:14 -04002406 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002407 * @phba: Pointer to HBA context object.
2408 * @pmboxq: Pointer to mailbox command.
2409 *
2410 * This is completion handler function for mailbox commands issued from
2411 * lpfc_sli_issue_mbox_wait function. This function is called by the
2412 * mailbox event handler function with no lock held. This function
2413 * will wake up thread waiting on the wait queue pointed by context1
2414 * of the mailbox.
2415 **/
James Smart04c68492009-05-22 14:52:52 -04002416void
James Smart2e0fef82007-06-17 19:56:36 -05002417lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002418{
James Smart858c9f62007-06-17 19:56:39 -05002419 unsigned long drvr_flag;
James Smarte29d74f2018-03-05 12:04:07 -08002420 struct completion *pmbox_done;
dea31012005-04-17 16:05:31 -05002421
2422 /*
James Smarte29d74f2018-03-05 12:04:07 -08002423 * If pmbox_done is empty, the driver thread gave up waiting and
dea31012005-04-17 16:05:31 -05002424 * continued running.
2425 */
James Smart7054a602007-04-25 09:52:34 -04002426 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002427 spin_lock_irqsave(&phba->hbalock, drvr_flag);
James Smarte29d74f2018-03-05 12:04:07 -08002428 pmbox_done = (struct completion *)pmboxq->context3;
2429 if (pmbox_done)
2430 complete(pmbox_done);
James Smart858c9f62007-06-17 19:56:39 -05002431 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002432 return;
2433}
2434
James Smarte59058c2008-08-24 21:49:00 -04002435
2436/**
James Smart3621a712009-04-06 18:47:14 -04002437 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002438 * @phba: Pointer to HBA context object.
2439 * @pmb: Pointer to mailbox object.
2440 *
2441 * This function is the default mailbox completion handler. It
2442 * frees the memory resources associated with the completed mailbox
2443 * command. If the completed command is a REG_LOGIN mailbox command,
2444 * this function will issue a UREG_LOGIN to re-claim the RPI.
2445 **/
dea31012005-04-17 16:05:31 -05002446void
James Smart2e0fef82007-06-17 19:56:36 -05002447lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002448{
James Smartd439d282010-09-29 11:18:45 -04002449 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002450 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002451 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002452 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002453 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002454 int rc;
2455
dea31012005-04-17 16:05:31 -05002456 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002457
dea31012005-04-17 16:05:31 -05002458 if (mp) {
2459 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2460 kfree(mp);
2461 }
James Smart7054a602007-04-25 09:52:34 -04002462
2463 /*
2464 * If a REG_LOGIN succeeded after node is destroyed or node
2465 * is in re-discovery driver need to cleanup the RPI.
2466 */
James Smart2e0fef82007-06-17 19:56:36 -05002467 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002468 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2469 !pmb->u.mb.mbxStatus) {
2470 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002471 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002472 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002473 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002474 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002475 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2476 if (rc != MBX_NOT_FINISHED)
2477 return;
2478 }
2479
James Smart695a8142010-01-26 23:08:03 -05002480 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2481 !(phba->pport->load_flag & FC_UNLOADING) &&
2482 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002483 shost = lpfc_shost_from_vport(vport);
2484 spin_lock_irq(shost->host_lock);
2485 vport->vpi_state |= LPFC_VPI_REGISTERED;
2486 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2487 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002488 }
2489
James Smartd439d282010-09-29 11:18:45 -04002490 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2491 ndlp = (struct lpfc_nodelist *)pmb->context2;
2492 lpfc_nlp_put(ndlp);
2493 pmb->context2 = NULL;
2494 }
2495
James Smartdcf2a4e2010-09-29 11:18:53 -04002496 /* Check security permission status on INIT_LINK mailbox command */
2497 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2498 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2499 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2500 "2860 SLI authentication is required "
2501 "for INIT_LINK but has not done yet\n");
2502
James Smart04c68492009-05-22 14:52:52 -04002503 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2504 lpfc_sli4_mbox_cmd_free(phba, pmb);
2505 else
2506 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002507}
James Smartbe6bb942015-04-07 15:07:22 -04002508 /**
2509 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2510 * @phba: Pointer to HBA context object.
2511 * @pmb: Pointer to mailbox object.
2512 *
2513 * This function is the unreg rpi mailbox completion handler. It
2514 * frees the memory resources associated with the completed mailbox
2515 * command. An additional refrenece is put on the ndlp to prevent
2516 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2517 * the unreg mailbox command completes, this routine puts the
2518 * reference back.
2519 *
2520 **/
2521void
2522lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2523{
2524 struct lpfc_vport *vport = pmb->vport;
2525 struct lpfc_nodelist *ndlp;
2526
2527 ndlp = pmb->context1;
2528 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2529 if (phba->sli_rev == LPFC_SLI_REV4 &&
2530 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002531 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002532 LPFC_SLI_INTF_IF_TYPE_2)) {
2533 if (ndlp) {
2534 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2535 "0010 UNREG_LOGIN vpi:%x "
2536 "rpi:%x DID:%x map:%x %p\n",
2537 vport->vpi, ndlp->nlp_rpi,
2538 ndlp->nlp_DID,
2539 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002540 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002541 lpfc_nlp_put(ndlp);
2542 }
2543 }
2544 }
2545
2546 mempool_free(pmb, phba->mbox_mem_pool);
2547}
dea31012005-04-17 16:05:31 -05002548
James Smarte59058c2008-08-24 21:49:00 -04002549/**
James Smart3621a712009-04-06 18:47:14 -04002550 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002551 * @phba: Pointer to HBA context object.
2552 *
2553 * This function is called with no lock held. This function processes all
2554 * the completed mailbox commands and gives it to upper layers. The interrupt
2555 * service routine processes mailbox completion interrupt and adds completed
2556 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2557 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2558 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2559 * function returns the mailbox commands to the upper layer by calling the
2560 * completion handler function of each mailbox.
2561 **/
dea31012005-04-17 16:05:31 -05002562int
James Smart2e0fef82007-06-17 19:56:36 -05002563lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002564{
James Smart92d7f7b2007-06-17 19:56:38 -05002565 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002566 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002567 int rc;
2568 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002569
2570 phba->sli.slistat.mbox_event++;
2571
James Smart92d7f7b2007-06-17 19:56:38 -05002572 /* Get all completed mailboxe buffers into the cmplq */
2573 spin_lock_irq(&phba->hbalock);
2574 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2575 spin_unlock_irq(&phba->hbalock);
2576
dea31012005-04-17 16:05:31 -05002577 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002578 do {
2579 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2580 if (pmb == NULL)
2581 break;
2582
James Smart04c68492009-05-22 14:52:52 -04002583 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002584
James Smart858c9f62007-06-17 19:56:39 -05002585 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2586 if (pmb->vport) {
2587 lpfc_debugfs_disc_trc(pmb->vport,
2588 LPFC_DISC_TRC_MBOX_VPORT,
2589 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2590 (uint32_t)pmbox->mbxCommand,
2591 pmbox->un.varWords[0],
2592 pmbox->un.varWords[1]);
2593 }
2594 else {
2595 lpfc_debugfs_disc_trc(phba->pport,
2596 LPFC_DISC_TRC_MBOX,
2597 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2598 (uint32_t)pmbox->mbxCommand,
2599 pmbox->un.varWords[0],
2600 pmbox->un.varWords[1]);
2601 }
2602 }
2603
dea31012005-04-17 16:05:31 -05002604 /*
2605 * It is a fatal error if unknown mbox command completion.
2606 */
2607 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2608 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002609 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002610 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002611 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002612 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002613 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002614 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002615 lpfc_sli_config_mbox_subsys_get(phba,
2616 pmb),
2617 lpfc_sli_config_mbox_opcode_get(phba,
2618 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002619 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002620 phba->work_hs = HS_FFER3;
2621 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002622 continue;
dea31012005-04-17 16:05:31 -05002623 }
2624
dea31012005-04-17 16:05:31 -05002625 if (pmbox->mbxStatus) {
2626 phba->sli.slistat.mbox_stat_err++;
2627 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2628 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002629 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002630 LOG_MBOX | LOG_SLI,
2631 "(%d):0305 Mbox cmd cmpl "
2632 "error - RETRYing Data: x%x "
2633 "(x%x/x%x) x%x x%x x%x\n",
2634 pmb->vport ? pmb->vport->vpi : 0,
2635 pmbox->mbxCommand,
2636 lpfc_sli_config_mbox_subsys_get(phba,
2637 pmb),
2638 lpfc_sli_config_mbox_opcode_get(phba,
2639 pmb),
2640 pmbox->mbxStatus,
2641 pmbox->un.varWords[0],
2642 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002643 pmbox->mbxStatus = 0;
2644 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002645 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002646 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002647 continue;
dea31012005-04-17 16:05:31 -05002648 }
2649 }
2650
2651 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002652 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002653 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002654 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2655 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002656 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002657 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002658 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2659 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002660 pmb->mbox_cmpl,
2661 *((uint32_t *) pmbox),
2662 pmbox->un.varWords[0],
2663 pmbox->un.varWords[1],
2664 pmbox->un.varWords[2],
2665 pmbox->un.varWords[3],
2666 pmbox->un.varWords[4],
2667 pmbox->un.varWords[5],
2668 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002669 pmbox->un.varWords[7],
2670 pmbox->un.varWords[8],
2671 pmbox->un.varWords[9],
2672 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002673
James Smart92d7f7b2007-06-17 19:56:38 -05002674 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002675 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002676 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002677 return 0;
dea31012005-04-17 16:05:31 -05002678}
James Smart92d7f7b2007-06-17 19:56:38 -05002679
James Smarte59058c2008-08-24 21:49:00 -04002680/**
James Smart3621a712009-04-06 18:47:14 -04002681 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002682 * @phba: Pointer to HBA context object.
2683 * @pring: Pointer to driver SLI ring object.
2684 * @tag: buffer tag.
2685 *
2686 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2687 * is set in the tag the buffer is posted for a particular exchange,
2688 * the function will return the buffer without replacing the buffer.
2689 * If the buffer is for unsolicited ELS or CT traffic, this function
2690 * returns the buffer and also posts another buffer to the firmware.
2691 **/
James Smart76bb24e2007-10-27 13:38:00 -04002692static struct lpfc_dmabuf *
2693lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002694 struct lpfc_sli_ring *pring,
2695 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002696{
James Smart9f1e1b52008-12-04 22:39:40 -05002697 struct hbq_dmabuf *hbq_entry;
2698
James Smart76bb24e2007-10-27 13:38:00 -04002699 if (tag & QUE_BUFTAG_BIT)
2700 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002701 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2702 if (!hbq_entry)
2703 return NULL;
2704 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002705}
James Smart57127f12007-10-27 13:37:05 -04002706
James Smart3772a992009-05-22 14:50:54 -04002707/**
2708 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2709 * @phba: Pointer to HBA context object.
2710 * @pring: Pointer to driver SLI ring object.
2711 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2712 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2713 * @fch_type: the type for the first frame of the sequence.
2714 *
2715 * This function is called with no lock held. This function uses the r_ctl and
2716 * type of the received sequence to find the correct callback function to call
2717 * to process the sequence.
2718 **/
2719static int
2720lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2721 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2722 uint32_t fch_type)
2723{
2724 int i;
2725
James Smartf358dd02017-02-12 13:52:34 -08002726 switch (fch_type) {
2727 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002728 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002729 return 1;
2730 default:
2731 break;
2732 }
2733
James Smart3772a992009-05-22 14:50:54 -04002734 /* unSolicited Responses */
2735 if (pring->prt[0].profile) {
2736 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2737 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2738 saveq);
2739 return 1;
2740 }
2741 /* We must search, based on rctl / type
2742 for the right routine */
2743 for (i = 0; i < pring->num_mask; i++) {
2744 if ((pring->prt[i].rctl == fch_r_ctl) &&
2745 (pring->prt[i].type == fch_type)) {
2746 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2747 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2748 (phba, pring, saveq);
2749 return 1;
2750 }
2751 }
2752 return 0;
2753}
James Smarte59058c2008-08-24 21:49:00 -04002754
2755/**
James Smart3621a712009-04-06 18:47:14 -04002756 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002757 * @phba: Pointer to HBA context object.
2758 * @pring: Pointer to driver SLI ring object.
2759 * @saveq: Pointer to the unsolicited iocb.
2760 *
2761 * This function is called with no lock held by the ring event handler
2762 * when there is an unsolicited iocb posted to the response ring by the
2763 * firmware. This function gets the buffer associated with the iocbs
2764 * and calls the event handler for the ring. This function handles both
2765 * qring buffers and hbq buffers.
2766 * When the function returns 1 the caller can free the iocb object otherwise
2767 * upper layer functions will free the iocb objects.
2768 **/
dea31012005-04-17 16:05:31 -05002769static int
2770lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2771 struct lpfc_iocbq *saveq)
2772{
2773 IOCB_t * irsp;
2774 WORD5 * w5p;
2775 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002776 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002777 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002778
dea31012005-04-17 16:05:31 -05002779 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002780
2781 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2782 if (pring->lpfc_sli_rcv_async_status)
2783 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2784 else
2785 lpfc_printf_log(phba,
2786 KERN_WARNING,
2787 LOG_SLI,
2788 "0316 Ring %d handler: unexpected "
2789 "ASYNC_STATUS iocb received evt_code "
2790 "0x%x\n",
2791 pring->ringno,
2792 irsp->un.asyncstat.evt_code);
2793 return 1;
2794 }
2795
James Smart3163f722008-02-08 18:50:25 -05002796 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2797 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2798 if (irsp->ulpBdeCount > 0) {
2799 dmzbuf = lpfc_sli_get_buff(phba, pring,
2800 irsp->un.ulpWord[3]);
2801 lpfc_in_buf_free(phba, dmzbuf);
2802 }
2803
2804 if (irsp->ulpBdeCount > 1) {
2805 dmzbuf = lpfc_sli_get_buff(phba, pring,
2806 irsp->unsli3.sli3Words[3]);
2807 lpfc_in_buf_free(phba, dmzbuf);
2808 }
2809
2810 if (irsp->ulpBdeCount > 2) {
2811 dmzbuf = lpfc_sli_get_buff(phba, pring,
2812 irsp->unsli3.sli3Words[7]);
2813 lpfc_in_buf_free(phba, dmzbuf);
2814 }
2815
2816 return 1;
2817 }
2818
James Smart92d7f7b2007-06-17 19:56:38 -05002819 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002820 if (irsp->ulpBdeCount != 0) {
2821 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002822 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002823 if (!saveq->context2)
2824 lpfc_printf_log(phba,
2825 KERN_ERR,
2826 LOG_SLI,
2827 "0341 Ring %d Cannot find buffer for "
2828 "an unsolicited iocb. tag 0x%x\n",
2829 pring->ringno,
2830 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002831 }
2832 if (irsp->ulpBdeCount == 2) {
2833 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002834 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002835 if (!saveq->context3)
2836 lpfc_printf_log(phba,
2837 KERN_ERR,
2838 LOG_SLI,
2839 "0342 Ring %d Cannot find buffer for an"
2840 " unsolicited iocb. tag 0x%x\n",
2841 pring->ringno,
2842 irsp->unsli3.sli3Words[7]);
2843 }
2844 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002845 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002846 if (irsp->ulpBdeCount != 0) {
2847 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2848 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002849 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002850 lpfc_printf_log(phba,
2851 KERN_ERR,
2852 LOG_SLI,
2853 "0343 Ring %d Cannot find "
2854 "buffer for an unsolicited iocb"
2855 ". tag 0x%x\n", pring->ringno,
2856 irsp->un.ulpWord[3]);
2857 }
2858 if (irsp->ulpBdeCount == 2) {
2859 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2860 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002861 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002862 lpfc_printf_log(phba,
2863 KERN_ERR,
2864 LOG_SLI,
2865 "0344 Ring %d Cannot find "
2866 "buffer for an unsolicited "
2867 "iocb. tag 0x%x\n",
2868 pring->ringno,
2869 irsp->unsli3.sli3Words[7]);
2870 }
2871 }
James Smart92d7f7b2007-06-17 19:56:38 -05002872 }
James Smart9c2face2008-01-11 01:53:18 -05002873 if (irsp->ulpBdeCount != 0 &&
2874 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2875 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2876 int found = 0;
2877
2878 /* search continue save q for same XRI */
2879 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002880 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2881 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002882 list_add_tail(&saveq->list, &iocbq->list);
2883 found = 1;
2884 break;
2885 }
2886 }
2887 if (!found)
2888 list_add_tail(&saveq->clist,
2889 &pring->iocb_continue_saveq);
2890 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2891 list_del_init(&iocbq->clist);
2892 saveq = iocbq;
2893 irsp = &(saveq->iocb);
2894 } else
2895 return 0;
2896 }
2897 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2898 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2899 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002900 Rctl = FC_RCTL_ELS_REQ;
2901 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002902 } else {
2903 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2904 Rctl = w5p->hcsw.Rctl;
2905 Type = w5p->hcsw.Type;
2906
2907 /* Firmware Workaround */
2908 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2909 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2910 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002911 Rctl = FC_RCTL_ELS_REQ;
2912 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002913 w5p->hcsw.Rctl = Rctl;
2914 w5p->hcsw.Type = Type;
2915 }
2916 }
James Smart92d7f7b2007-06-17 19:56:38 -05002917
James Smart3772a992009-05-22 14:50:54 -04002918 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002919 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002920 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002921 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002922 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002923
James Smart92d7f7b2007-06-17 19:56:38 -05002924 return 1;
dea31012005-04-17 16:05:31 -05002925}
2926
James Smarte59058c2008-08-24 21:49:00 -04002927/**
James Smart3621a712009-04-06 18:47:14 -04002928 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002929 * @phba: Pointer to HBA context object.
2930 * @pring: Pointer to driver SLI ring object.
2931 * @prspiocb: Pointer to response iocb object.
2932 *
2933 * This function looks up the iocb_lookup table to get the command iocb
2934 * corresponding to the given response iocb using the iotag of the
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002935 * response iocb. This function is called with the hbalock held
2936 * for sli3 devices or the ring_lock for sli4 devices.
James Smarte59058c2008-08-24 21:49:00 -04002937 * This function returns the command iocb object if it finds the command
2938 * iocb else returns NULL.
2939 **/
dea31012005-04-17 16:05:31 -05002940static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002941lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2942 struct lpfc_sli_ring *pring,
2943 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002944{
dea31012005-04-17 16:05:31 -05002945 struct lpfc_iocbq *cmd_iocb = NULL;
2946 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002947 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002948
James Bottomley604a3e32005-10-29 10:28:33 -05002949 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002950
James Bottomley604a3e32005-10-29 10:28:33 -05002951 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2952 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002953 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002954 /* remove from txcmpl queue list */
2955 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002956 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart89533e92016-10-13 15:06:15 -07002957 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002958 }
dea31012005-04-17 16:05:31 -05002959 }
2960
dea31012005-04-17 16:05:31 -05002961 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002962 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05002963 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002964 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002965 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002966 return NULL;
2967}
2968
James Smarte59058c2008-08-24 21:49:00 -04002969/**
James Smart3772a992009-05-22 14:50:54 -04002970 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2971 * @phba: Pointer to HBA context object.
2972 * @pring: Pointer to driver SLI ring object.
2973 * @iotag: IOCB tag.
2974 *
2975 * This function looks up the iocb_lookup table to get the command iocb
2976 * corresponding to the given iotag. This function is called with the
2977 * hbalock held.
2978 * This function returns the command iocb object if it finds the command
2979 * iocb else returns NULL.
2980 **/
2981static struct lpfc_iocbq *
2982lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2983 struct lpfc_sli_ring *pring, uint16_t iotag)
2984{
James Smart895427b2017-02-12 13:52:30 -08002985 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04002986
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002987 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002988 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2989 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002990 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2991 /* remove from txcmpl queue list */
2992 list_del_init(&cmd_iocb->list);
2993 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002994 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002995 }
James Smart3772a992009-05-22 14:50:54 -04002996 }
James Smart89533e92016-10-13 15:06:15 -07002997
James Smart3772a992009-05-22 14:50:54 -04002998 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08002999 "0372 iotag x%x lookup error: max iotag (x%x) "
3000 "iocb_flag x%x\n",
3001 iotag, phba->sli.last_iotag,
3002 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04003003 return NULL;
3004}
3005
3006/**
James Smart3621a712009-04-06 18:47:14 -04003007 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04003008 * @phba: Pointer to HBA context object.
3009 * @pring: Pointer to driver SLI ring object.
3010 * @saveq: Pointer to the response iocb to be processed.
3011 *
3012 * This function is called by the ring event handler for non-fcp
3013 * rings when there is a new response iocb in the response ring.
3014 * The caller is not required to hold any locks. This function
3015 * gets the command iocb associated with the response iocb and
3016 * calls the completion handler for the command iocb. If there
3017 * is no completion handler, the function will free the resources
3018 * associated with command iocb. If the response iocb is for
3019 * an already aborted command iocb, the status of the completion
3020 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3021 * This function always returns 1.
3022 **/
dea31012005-04-17 16:05:31 -05003023static int
James Smart2e0fef82007-06-17 19:56:36 -05003024lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003025 struct lpfc_iocbq *saveq)
3026{
James Smart2e0fef82007-06-17 19:56:36 -05003027 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05003028 int rc = 1;
3029 unsigned long iflag;
3030
3031 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003032 if (phba->sli_rev == LPFC_SLI_REV4)
3033 spin_lock_irqsave(&pring->ring_lock, iflag);
3034 else
3035 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003036 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003037 if (phba->sli_rev == LPFC_SLI_REV4)
3038 spin_unlock_irqrestore(&pring->ring_lock, iflag);
3039 else
3040 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart2e0fef82007-06-17 19:56:36 -05003041
dea31012005-04-17 16:05:31 -05003042 if (cmdiocbp) {
3043 if (cmdiocbp->iocb_cmpl) {
3044 /*
James Smartea2151b2008-09-07 11:52:10 -04003045 * If an ELS command failed send an event to mgmt
3046 * application.
3047 */
3048 if (saveq->iocb.ulpStatus &&
3049 (pring->ringno == LPFC_ELS_RING) &&
3050 (cmdiocbp->iocb.ulpCommand ==
3051 CMD_ELS_REQUEST64_CR))
3052 lpfc_send_els_failure_event(phba,
3053 cmdiocbp, saveq);
3054
3055 /*
dea31012005-04-17 16:05:31 -05003056 * Post all ELS completions to the worker thread.
3057 * All other are passed to the completion callback.
3058 */
3059 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05003060 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3061 (cmdiocbp->iocb_flag &
3062 LPFC_DRIVER_ABORTED)) {
3063 spin_lock_irqsave(&phba->hbalock,
3064 iflag);
James Smart07951072007-04-25 09:51:38 -04003065 cmdiocbp->iocb_flag &=
3066 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05003067 spin_unlock_irqrestore(&phba->hbalock,
3068 iflag);
James Smart07951072007-04-25 09:51:38 -04003069 saveq->iocb.ulpStatus =
3070 IOSTAT_LOCAL_REJECT;
3071 saveq->iocb.un.ulpWord[4] =
3072 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05003073
3074 /* Firmware could still be in progress
3075 * of DMAing payload, so don't free data
3076 * buffer till after a hbeat.
3077 */
James Smart341af102010-01-26 23:07:37 -05003078 spin_lock_irqsave(&phba->hbalock,
3079 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05003080 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05003081 spin_unlock_irqrestore(&phba->hbalock,
3082 iflag);
3083 }
James Smart0f65ff62010-02-26 14:14:23 -05003084 if (phba->sli_rev == LPFC_SLI_REV4) {
3085 if (saveq->iocb_flag &
3086 LPFC_EXCHANGE_BUSY) {
3087 /* Set cmdiocb flag for the
3088 * exchange busy so sgl (xri)
3089 * will not be released until
3090 * the abort xri is received
3091 * from hba.
3092 */
3093 spin_lock_irqsave(
3094 &phba->hbalock, iflag);
3095 cmdiocbp->iocb_flag |=
3096 LPFC_EXCHANGE_BUSY;
3097 spin_unlock_irqrestore(
3098 &phba->hbalock, iflag);
3099 }
3100 if (cmdiocbp->iocb_flag &
3101 LPFC_DRIVER_ABORTED) {
3102 /*
3103 * Clear LPFC_DRIVER_ABORTED
3104 * bit in case it was driver
3105 * initiated abort.
3106 */
3107 spin_lock_irqsave(
3108 &phba->hbalock, iflag);
3109 cmdiocbp->iocb_flag &=
3110 ~LPFC_DRIVER_ABORTED;
3111 spin_unlock_irqrestore(
3112 &phba->hbalock, iflag);
3113 cmdiocbp->iocb.ulpStatus =
3114 IOSTAT_LOCAL_REJECT;
3115 cmdiocbp->iocb.un.ulpWord[4] =
3116 IOERR_ABORT_REQUESTED;
3117 /*
3118 * For SLI4, irsiocb contains
3119 * NO_XRI in sli_xritag, it
3120 * shall not affect releasing
3121 * sgl (xri) process.
3122 */
3123 saveq->iocb.ulpStatus =
3124 IOSTAT_LOCAL_REJECT;
3125 saveq->iocb.un.ulpWord[4] =
3126 IOERR_SLI_ABORTED;
3127 spin_lock_irqsave(
3128 &phba->hbalock, iflag);
3129 saveq->iocb_flag |=
3130 LPFC_DELAY_MEM_FREE;
3131 spin_unlock_irqrestore(
3132 &phba->hbalock, iflag);
3133 }
James Smart07951072007-04-25 09:51:38 -04003134 }
dea31012005-04-17 16:05:31 -05003135 }
James Smart2e0fef82007-06-17 19:56:36 -05003136 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003137 } else
3138 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003139 } else {
3140 /*
3141 * Unknown initiating command based on the response iotag.
3142 * This could be the case on the ELS ring because of
3143 * lpfc_els_abort().
3144 */
3145 if (pring->ringno != LPFC_ELS_RING) {
3146 /*
3147 * Ring <ringno> handler: unexpected completion IoTag
3148 * <IoTag>
3149 */
James Smarta257bf92009-04-06 18:48:10 -04003150 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003151 "0322 Ring %d handler: "
3152 "unexpected completion IoTag x%x "
3153 "Data: x%x x%x x%x x%x\n",
3154 pring->ringno,
3155 saveq->iocb.ulpIoTag,
3156 saveq->iocb.ulpStatus,
3157 saveq->iocb.un.ulpWord[4],
3158 saveq->iocb.ulpCommand,
3159 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003160 }
3161 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003162
dea31012005-04-17 16:05:31 -05003163 return rc;
3164}
3165
James Smarte59058c2008-08-24 21:49:00 -04003166/**
James Smart3621a712009-04-06 18:47:14 -04003167 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003168 * @phba: Pointer to HBA context object.
3169 * @pring: Pointer to driver SLI ring object.
3170 *
3171 * This function is called from the iocb ring event handlers when
3172 * put pointer is ahead of the get pointer for a ring. This function signal
3173 * an error attention condition to the worker thread and the worker
3174 * thread will transition the HBA to offline state.
3175 **/
James Smart2e0fef82007-06-17 19:56:36 -05003176static void
3177lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003178{
James Smart34b02dc2008-08-24 21:49:55 -04003179 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003180 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003181 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003182 * rsp ring <portRspMax>
3183 */
3184 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003185 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003186 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003187 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003188 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003189
James Smart2e0fef82007-06-17 19:56:36 -05003190 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003191
3192 /*
3193 * All error attention handlers are posted to
3194 * worker thread
3195 */
3196 phba->work_ha |= HA_ERATT;
3197 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003198
James Smart5e9d9b82008-06-14 22:52:53 -04003199 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003200
3201 return;
3202}
3203
James Smarte59058c2008-08-24 21:49:00 -04003204/**
James Smart3621a712009-04-06 18:47:14 -04003205 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04003206 * @ptr: Pointer to address of HBA context object.
3207 *
3208 * This function is invoked by the Error Attention polling timer when the
3209 * timer times out. It will check the SLI Error Attention register for
3210 * possible attention events. If so, it will post an Error Attention event
3211 * and wake up worker thread to process it. Otherwise, it will set up the
3212 * Error Attention polling timer for the next poll.
3213 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003214void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003215{
3216 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003217 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003218 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003219
Kees Cookf22eb4d2017-09-06 20:24:26 -07003220 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003221
James Smartaa6fbb72012-08-03 12:36:03 -04003222 /* Here we will also keep track of interrupts per sec of the hba */
3223 sli_intr = phba->sli.slistat.sli_intr;
3224
3225 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3226 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3227 sli_intr);
3228 else
3229 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3230
James Smart65791f12016-07-06 12:35:56 -07003231 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3232 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003233 phba->sli.slistat.sli_ips = cnt;
3234
3235 phba->sli.slistat.sli_prev_intr = sli_intr;
3236
James Smart93996272008-08-24 21:50:30 -04003237 /* Check chip HA register for error event */
3238 eratt = lpfc_sli_check_eratt(phba);
3239
3240 if (eratt)
3241 /* Tell the worker thread there is work to do */
3242 lpfc_worker_wake_up(phba);
3243 else
3244 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003245 mod_timer(&phba->eratt_poll,
3246 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003247 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003248 return;
3249}
3250
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003251
James Smarte59058c2008-08-24 21:49:00 -04003252/**
James Smart3621a712009-04-06 18:47:14 -04003253 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003254 * @phba: Pointer to HBA context object.
3255 * @pring: Pointer to driver SLI ring object.
3256 * @mask: Host attention register mask for this ring.
3257 *
3258 * This function is called from the interrupt context when there is a ring
3259 * event for the fcp ring. The caller does not hold any lock.
3260 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003261 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003262 * LE bit set. The function will call the completion handler of the command iocb
3263 * if the response iocb indicates a completion for a command iocb or it is
3264 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3265 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003266 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003267 * to check it explicitly.
3268 */
3269int
James Smart2e0fef82007-06-17 19:56:36 -05003270lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3271 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003272{
James Smart34b02dc2008-08-24 21:49:55 -04003273 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003274 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003275 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003276 struct lpfc_iocbq *cmdiocbq = NULL;
3277 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003278 uint32_t status;
3279 uint32_t portRspPut, portRspMax;
3280 int rc = 1;
3281 lpfc_iocb_type type;
3282 unsigned long iflag;
3283 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003284
James Smart2e0fef82007-06-17 19:56:36 -05003285 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003286 pring->stats.iocb_event++;
3287
dea31012005-04-17 16:05:31 -05003288 /*
3289 * The next available response entry should never exceed the maximum
3290 * entries. If it does, treat it as an adapter hardware error.
3291 */
James Smart7e56aa22012-08-03 12:35:34 -04003292 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003293 portRspPut = le32_to_cpu(pgp->rspPutInx);
3294 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003295 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003296 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003297 return 1;
3298 }
James Smart45ed1192009-10-02 15:17:02 -04003299 if (phba->fcp_ring_in_use) {
3300 spin_unlock_irqrestore(&phba->hbalock, iflag);
3301 return 1;
3302 } else
3303 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003304
3305 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003306 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003307 /*
3308 * Fetch an entry off the ring and copy it into a local data
3309 * structure. The copy involves a byte-swap since the
3310 * network byte order and pci byte orders are different.
3311 */
James Smarted957682007-06-17 19:56:37 -05003312 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003313 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003314
James Smart7e56aa22012-08-03 12:35:34 -04003315 if (++pring->sli.sli3.rspidx >= portRspMax)
3316 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003317
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003318 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3319 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003320 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003321 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003322 irsp = &rspiocbq.iocb;
3323
dea31012005-04-17 16:05:31 -05003324 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3325 pring->stats.iocb_rsp++;
3326 rsp_cmpl++;
3327
3328 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003329 /*
3330 * If resource errors reported from HBA, reduce
3331 * queuedepths of the SCSI device.
3332 */
3333 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003334 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3335 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003336 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003337 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003338 spin_lock_irqsave(&phba->hbalock, iflag);
3339 }
3340
dea31012005-04-17 16:05:31 -05003341 /* Rsp ring <ringno> error: IOCB */
3342 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003343 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003344 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003345 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003346 irsp->un.ulpWord[0],
3347 irsp->un.ulpWord[1],
3348 irsp->un.ulpWord[2],
3349 irsp->un.ulpWord[3],
3350 irsp->un.ulpWord[4],
3351 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003352 *(uint32_t *)&irsp->un1,
3353 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003354 }
3355
3356 switch (type) {
3357 case LPFC_ABORT_IOCB:
3358 case LPFC_SOL_IOCB:
3359 /*
3360 * Idle exchange closed via ABTS from port. No iocb
3361 * resources need to be recovered.
3362 */
3363 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003364 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003365 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003366 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003367 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003368 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003369 break;
3370 }
3371
James Bottomley604a3e32005-10-29 10:28:33 -05003372 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3373 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003374 if (unlikely(!cmdiocbq))
3375 break;
3376 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3377 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3378 if (cmdiocbq->iocb_cmpl) {
3379 spin_unlock_irqrestore(&phba->hbalock, iflag);
3380 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3381 &rspiocbq);
3382 spin_lock_irqsave(&phba->hbalock, iflag);
3383 }
dea31012005-04-17 16:05:31 -05003384 break;
James Smarta4bc3372006-12-02 13:34:16 -05003385 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003386 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003387 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003388 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003389 break;
dea31012005-04-17 16:05:31 -05003390 default:
3391 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3392 char adaptermsg[LPFC_MAX_ADPTMSG];
3393 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3394 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3395 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003396 dev_warn(&((phba->pcidev)->dev),
3397 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003398 phba->brd_no, adaptermsg);
3399 } else {
3400 /* Unknown IOCB command */
3401 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003402 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003403 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003404 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003405 irsp->ulpStatus,
3406 irsp->ulpIoTag,
3407 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003408 }
3409 break;
3410 }
3411
3412 /*
3413 * The response IOCB has been processed. Update the ring
3414 * pointer in SLIM. If the port response put pointer has not
3415 * been updated, sync the pgp->rspPutInx and fetch the new port
3416 * response put pointer.
3417 */
James Smart7e56aa22012-08-03 12:35:34 -04003418 writel(pring->sli.sli3.rspidx,
3419 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003420
James Smart7e56aa22012-08-03 12:35:34 -04003421 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003422 portRspPut = le32_to_cpu(pgp->rspPutInx);
3423 }
3424
3425 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3426 pring->stats.iocb_rsp_full++;
3427 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3428 writel(status, phba->CAregaddr);
3429 readl(phba->CAregaddr);
3430 }
3431 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3432 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3433 pring->stats.iocb_cmd_empty++;
3434
3435 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003436 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003437 lpfc_sli_resume_iocb(phba, pring);
3438
3439 if ((pring->lpfc_sli_cmd_available))
3440 (pring->lpfc_sli_cmd_available) (phba, pring);
3441
3442 }
3443
James Smart45ed1192009-10-02 15:17:02 -04003444 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003445 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003446 return rc;
3447}
3448
James Smarte59058c2008-08-24 21:49:00 -04003449/**
James Smart3772a992009-05-22 14:50:54 -04003450 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3451 * @phba: Pointer to HBA context object.
3452 * @pring: Pointer to driver SLI ring object.
3453 * @rspiocbp: Pointer to driver response IOCB object.
3454 *
3455 * This function is called from the worker thread when there is a slow-path
3456 * response IOCB to process. This function chains all the response iocbs until
3457 * seeing the iocb with the LE bit set. The function will call
3458 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3459 * completion of a command iocb. The function will call the
3460 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3461 * The function frees the resources or calls the completion handler if this
3462 * iocb is an abort completion. The function returns NULL when the response
3463 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3464 * this function shall chain the iocb on to the iocb_continueq and return the
3465 * response iocb passed in.
3466 **/
3467static struct lpfc_iocbq *
3468lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3469 struct lpfc_iocbq *rspiocbp)
3470{
3471 struct lpfc_iocbq *saveq;
3472 struct lpfc_iocbq *cmdiocbp;
3473 struct lpfc_iocbq *next_iocb;
3474 IOCB_t *irsp = NULL;
3475 uint32_t free_saveq;
3476 uint8_t iocb_cmd_type;
3477 lpfc_iocb_type type;
3478 unsigned long iflag;
3479 int rc;
3480
3481 spin_lock_irqsave(&phba->hbalock, iflag);
3482 /* First add the response iocb to the countinueq list */
3483 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3484 pring->iocb_continueq_cnt++;
3485
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003486 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003487 irsp = &rspiocbp->iocb;
3488 if (irsp->ulpLe) {
3489 /*
3490 * By default, the driver expects to free all resources
3491 * associated with this iocb completion.
3492 */
3493 free_saveq = 1;
3494 saveq = list_get_first(&pring->iocb_continueq,
3495 struct lpfc_iocbq, list);
3496 irsp = &(saveq->iocb);
3497 list_del_init(&pring->iocb_continueq);
3498 pring->iocb_continueq_cnt = 0;
3499
3500 pring->stats.iocb_rsp++;
3501
3502 /*
3503 * If resource errors reported from HBA, reduce
3504 * queuedepths of the SCSI device.
3505 */
3506 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003507 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3508 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003509 spin_unlock_irqrestore(&phba->hbalock, iflag);
3510 phba->lpfc_rampdown_queue_depth(phba);
3511 spin_lock_irqsave(&phba->hbalock, iflag);
3512 }
3513
3514 if (irsp->ulpStatus) {
3515 /* Rsp ring <ringno> error: IOCB */
3516 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3517 "0328 Rsp Ring %d error: "
3518 "IOCB Data: "
3519 "x%x x%x x%x x%x "
3520 "x%x x%x x%x x%x "
3521 "x%x x%x x%x x%x "
3522 "x%x x%x x%x x%x\n",
3523 pring->ringno,
3524 irsp->un.ulpWord[0],
3525 irsp->un.ulpWord[1],
3526 irsp->un.ulpWord[2],
3527 irsp->un.ulpWord[3],
3528 irsp->un.ulpWord[4],
3529 irsp->un.ulpWord[5],
3530 *(((uint32_t *) irsp) + 6),
3531 *(((uint32_t *) irsp) + 7),
3532 *(((uint32_t *) irsp) + 8),
3533 *(((uint32_t *) irsp) + 9),
3534 *(((uint32_t *) irsp) + 10),
3535 *(((uint32_t *) irsp) + 11),
3536 *(((uint32_t *) irsp) + 12),
3537 *(((uint32_t *) irsp) + 13),
3538 *(((uint32_t *) irsp) + 14),
3539 *(((uint32_t *) irsp) + 15));
3540 }
3541
3542 /*
3543 * Fetch the IOCB command type and call the correct completion
3544 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3545 * get freed back to the lpfc_iocb_list by the discovery
3546 * kernel thread.
3547 */
3548 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3549 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3550 switch (type) {
3551 case LPFC_SOL_IOCB:
3552 spin_unlock_irqrestore(&phba->hbalock, iflag);
3553 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3554 spin_lock_irqsave(&phba->hbalock, iflag);
3555 break;
3556
3557 case LPFC_UNSOL_IOCB:
3558 spin_unlock_irqrestore(&phba->hbalock, iflag);
3559 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3560 spin_lock_irqsave(&phba->hbalock, iflag);
3561 if (!rc)
3562 free_saveq = 0;
3563 break;
3564
3565 case LPFC_ABORT_IOCB:
3566 cmdiocbp = NULL;
3567 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3568 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3569 saveq);
3570 if (cmdiocbp) {
3571 /* Call the specified completion routine */
3572 if (cmdiocbp->iocb_cmpl) {
3573 spin_unlock_irqrestore(&phba->hbalock,
3574 iflag);
3575 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3576 saveq);
3577 spin_lock_irqsave(&phba->hbalock,
3578 iflag);
3579 } else
3580 __lpfc_sli_release_iocbq(phba,
3581 cmdiocbp);
3582 }
3583 break;
3584
3585 case LPFC_UNKNOWN_IOCB:
3586 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3587 char adaptermsg[LPFC_MAX_ADPTMSG];
3588 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3589 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3590 MAX_MSG_DATA);
3591 dev_warn(&((phba->pcidev)->dev),
3592 "lpfc%d: %s\n",
3593 phba->brd_no, adaptermsg);
3594 } else {
3595 /* Unknown IOCB command */
3596 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3597 "0335 Unknown IOCB "
3598 "command Data: x%x "
3599 "x%x x%x x%x\n",
3600 irsp->ulpCommand,
3601 irsp->ulpStatus,
3602 irsp->ulpIoTag,
3603 irsp->ulpContext);
3604 }
3605 break;
3606 }
3607
3608 if (free_saveq) {
3609 list_for_each_entry_safe(rspiocbp, next_iocb,
3610 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003611 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003612 __lpfc_sli_release_iocbq(phba, rspiocbp);
3613 }
3614 __lpfc_sli_release_iocbq(phba, saveq);
3615 }
3616 rspiocbp = NULL;
3617 }
3618 spin_unlock_irqrestore(&phba->hbalock, iflag);
3619 return rspiocbp;
3620}
3621
3622/**
3623 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003624 * @phba: Pointer to HBA context object.
3625 * @pring: Pointer to driver SLI ring object.
3626 * @mask: Host attention register mask for this ring.
3627 *
James Smart3772a992009-05-22 14:50:54 -04003628 * This routine wraps the actual slow_ring event process routine from the
3629 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003630 **/
James Smart3772a992009-05-22 14:50:54 -04003631void
James Smart2e0fef82007-06-17 19:56:36 -05003632lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3633 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003634{
James Smart3772a992009-05-22 14:50:54 -04003635 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3636}
3637
3638/**
3639 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3640 * @phba: Pointer to HBA context object.
3641 * @pring: Pointer to driver SLI ring object.
3642 * @mask: Host attention register mask for this ring.
3643 *
3644 * This function is called from the worker thread when there is a ring event
3645 * for non-fcp rings. The caller does not hold any lock. The function will
3646 * remove each response iocb in the response ring and calls the handle
3647 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3648 **/
3649static void
3650lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3651 struct lpfc_sli_ring *pring, uint32_t mask)
3652{
James Smart34b02dc2008-08-24 21:49:55 -04003653 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003654 IOCB_t *entry;
3655 IOCB_t *irsp = NULL;
3656 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003657 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003658 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003659 uint32_t status;
dea31012005-04-17 16:05:31 -05003660
James Smart34b02dc2008-08-24 21:49:55 -04003661 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003662 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003663 pring->stats.iocb_event++;
3664
dea31012005-04-17 16:05:31 -05003665 /*
3666 * The next available response entry should never exceed the maximum
3667 * entries. If it does, treat it as an adapter hardware error.
3668 */
James Smart7e56aa22012-08-03 12:35:34 -04003669 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003670 portRspPut = le32_to_cpu(pgp->rspPutInx);
3671 if (portRspPut >= portRspMax) {
3672 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003673 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003674 * rsp ring <portRspMax>
3675 */
James Smarted957682007-06-17 19:56:37 -05003676 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003677 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003678 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003679 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003680
James Smart2e0fef82007-06-17 19:56:36 -05003681 phba->link_state = LPFC_HBA_ERROR;
3682 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003683
3684 phba->work_hs = HS_FFER3;
3685 lpfc_handle_eratt(phba);
3686
James Smart3772a992009-05-22 14:50:54 -04003687 return;
dea31012005-04-17 16:05:31 -05003688 }
3689
3690 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003691 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003692 /*
3693 * Build a completion list and call the appropriate handler.
3694 * The process is to get the next available response iocb, get
3695 * a free iocb from the list, copy the response data into the
3696 * free iocb, insert to the continuation list, and update the
3697 * next response index to slim. This process makes response
3698 * iocb's in the ring available to DMA as fast as possible but
3699 * pays a penalty for a copy operation. Since the iocb is
3700 * only 32 bytes, this penalty is considered small relative to
3701 * the PCI reads for register values and a slim write. When
3702 * the ulpLe field is set, the entire Command has been
3703 * received.
3704 */
James Smarted957682007-06-17 19:56:37 -05003705 entry = lpfc_resp_iocb(phba, pring);
3706
James Smart858c9f62007-06-17 19:56:39 -05003707 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003708 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003709 if (rspiocbp == NULL) {
3710 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003711 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003712 break;
3713 }
3714
James Smarted957682007-06-17 19:56:37 -05003715 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3716 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003717 irsp = &rspiocbp->iocb;
3718
James Smart7e56aa22012-08-03 12:35:34 -04003719 if (++pring->sli.sli3.rspidx >= portRspMax)
3720 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003721
James Smarta58cbd52007-08-02 11:09:43 -04003722 if (pring->ringno == LPFC_ELS_RING) {
3723 lpfc_debugfs_slow_ring_trc(phba,
3724 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3725 *(((uint32_t *) irsp) + 4),
3726 *(((uint32_t *) irsp) + 6),
3727 *(((uint32_t *) irsp) + 7));
3728 }
3729
James Smart7e56aa22012-08-03 12:35:34 -04003730 writel(pring->sli.sli3.rspidx,
3731 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003732
James Smart3772a992009-05-22 14:50:54 -04003733 spin_unlock_irqrestore(&phba->hbalock, iflag);
3734 /* Handle the response IOCB */
3735 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3736 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003737
3738 /*
3739 * If the port response put pointer has not been updated, sync
3740 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3741 * response put pointer.
3742 */
James Smart7e56aa22012-08-03 12:35:34 -04003743 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003744 portRspPut = le32_to_cpu(pgp->rspPutInx);
3745 }
James Smart7e56aa22012-08-03 12:35:34 -04003746 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003747
James Smart92d7f7b2007-06-17 19:56:38 -05003748 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003749 /* At least one response entry has been freed */
3750 pring->stats.iocb_rsp_full++;
3751 /* SET RxRE_RSP in Chip Att register */
3752 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3753 writel(status, phba->CAregaddr);
3754 readl(phba->CAregaddr); /* flush */
3755 }
3756 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3757 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3758 pring->stats.iocb_cmd_empty++;
3759
3760 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003761 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003762 lpfc_sli_resume_iocb(phba, pring);
3763
3764 if ((pring->lpfc_sli_cmd_available))
3765 (pring->lpfc_sli_cmd_available) (phba, pring);
3766
3767 }
3768
James Smart2e0fef82007-06-17 19:56:36 -05003769 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003770 return;
dea31012005-04-17 16:05:31 -05003771}
3772
James Smarte59058c2008-08-24 21:49:00 -04003773/**
James Smart4f774512009-05-22 14:52:35 -04003774 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3775 * @phba: Pointer to HBA context object.
3776 * @pring: Pointer to driver SLI ring object.
3777 * @mask: Host attention register mask for this ring.
3778 *
3779 * This function is called from the worker thread when there is a pending
3780 * ELS response iocb on the driver internal slow-path response iocb worker
3781 * queue. The caller does not hold any lock. The function will remove each
3782 * response iocb from the response worker queue and calls the handle
3783 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3784 **/
3785static void
3786lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3787 struct lpfc_sli_ring *pring, uint32_t mask)
3788{
3789 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003790 struct hbq_dmabuf *dmabuf;
3791 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003792 unsigned long iflag;
3793
James Smart45ed1192009-10-02 15:17:02 -04003794 spin_lock_irqsave(&phba->hbalock, iflag);
3795 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3796 spin_unlock_irqrestore(&phba->hbalock, iflag);
3797 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003798 /* Get the response iocb from the head of work queue */
3799 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003800 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003801 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003802 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003803
3804 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3805 case CQE_CODE_COMPL_WQE:
3806 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3807 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003808 /* Translate ELS WCQE to response IOCBQ */
3809 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3810 irspiocbq);
3811 if (irspiocbq)
3812 lpfc_sli_sp_handle_rspiocb(phba, pring,
3813 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003814 break;
3815 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003816 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003817 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3818 cq_event);
3819 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3820 break;
3821 default:
3822 break;
3823 }
James Smart4f774512009-05-22 14:52:35 -04003824 }
3825}
3826
3827/**
James Smart3621a712009-04-06 18:47:14 -04003828 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003829 * @phba: Pointer to HBA context object.
3830 * @pring: Pointer to driver SLI ring object.
3831 *
3832 * This function aborts all iocbs in the given ring and frees all the iocb
3833 * objects in txq. This function issues an abort iocb for all the iocb commands
3834 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3835 * the return of this function. The caller is not required to hold any locks.
3836 **/
James Smart2e0fef82007-06-17 19:56:36 -05003837void
dea31012005-04-17 16:05:31 -05003838lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3839{
James Smart2534ba72007-04-25 09:52:20 -04003840 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003841 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003842
James Smart92d7f7b2007-06-17 19:56:38 -05003843 if (pring->ringno == LPFC_ELS_RING) {
3844 lpfc_fabric_abort_hba(phba);
3845 }
3846
dea31012005-04-17 16:05:31 -05003847 /* Error everything on txq and txcmplq
3848 * First do the txq.
3849 */
James Smartdb55fba2014-04-04 13:52:02 -04003850 if (phba->sli_rev >= LPFC_SLI_REV4) {
3851 spin_lock_irq(&pring->ring_lock);
3852 list_splice_init(&pring->txq, &completions);
3853 pring->txq_cnt = 0;
3854 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003855
James Smartdb55fba2014-04-04 13:52:02 -04003856 spin_lock_irq(&phba->hbalock);
3857 /* Next issue ABTS for everything on the txcmplq */
3858 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3859 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3860 spin_unlock_irq(&phba->hbalock);
3861 } else {
3862 spin_lock_irq(&phba->hbalock);
3863 list_splice_init(&pring->txq, &completions);
3864 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003865
James Smartdb55fba2014-04-04 13:52:02 -04003866 /* Next issue ABTS for everything on the txcmplq */
3867 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3868 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3869 spin_unlock_irq(&phba->hbalock);
3870 }
James Smart2534ba72007-04-25 09:52:20 -04003871
James Smarta257bf92009-04-06 18:48:10 -04003872 /* Cancel all the IOCBs from the completions list */
3873 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3874 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003875}
3876
James Smarte59058c2008-08-24 21:49:00 -04003877/**
James Smart895427b2017-02-12 13:52:30 -08003878 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3879 * @phba: Pointer to HBA context object.
3880 * @pring: Pointer to driver SLI ring object.
3881 *
3882 * This function aborts all iocbs in the given ring and frees all the iocb
3883 * objects in txq. This function issues an abort iocb for all the iocb commands
3884 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3885 * the return of this function. The caller is not required to hold any locks.
3886 **/
3887void
3888lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3889{
3890 LIST_HEAD(completions);
3891 struct lpfc_iocbq *iocb, *next_iocb;
3892
3893 if (pring->ringno == LPFC_ELS_RING)
3894 lpfc_fabric_abort_hba(phba);
3895
3896 spin_lock_irq(&phba->hbalock);
3897 /* Next issue ABTS for everything on the txcmplq */
3898 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3899 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3900 spin_unlock_irq(&phba->hbalock);
3901}
3902
3903
3904/**
James Smartdb55fba2014-04-04 13:52:02 -04003905 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3906 * @phba: Pointer to HBA context object.
3907 * @pring: Pointer to driver SLI ring object.
3908 *
3909 * This function aborts all iocbs in FCP rings and frees all the iocb
3910 * objects in txq. This function issues an abort iocb for all the iocb commands
3911 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3912 * the return of this function. The caller is not required to hold any locks.
3913 **/
3914void
3915lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3916{
3917 struct lpfc_sli *psli = &phba->sli;
3918 struct lpfc_sli_ring *pring;
3919 uint32_t i;
3920
3921 /* Look on all the FCP Rings for the iotag */
3922 if (phba->sli_rev >= LPFC_SLI_REV4) {
3923 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003924 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003925 lpfc_sli_abort_iocb_ring(phba, pring);
3926 }
3927 } else {
James Smart895427b2017-02-12 13:52:30 -08003928 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003929 lpfc_sli_abort_iocb_ring(phba, pring);
3930 }
3931}
3932
James Smart895427b2017-02-12 13:52:30 -08003933/**
3934 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3935 * @phba: Pointer to HBA context object.
3936 *
3937 * This function aborts all wqes in NVME rings. This function issues an
3938 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3939 * the txcmplq is not guaranteed to complete before the return of this
3940 * function. The caller is not required to hold any locks.
3941 **/
3942void
3943lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3944{
3945 struct lpfc_sli_ring *pring;
3946 uint32_t i;
3947
3948 if (phba->sli_rev < LPFC_SLI_REV4)
3949 return;
3950
3951 /* Abort all IO on each NVME ring. */
3952 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3953 pring = phba->sli4_hba.nvme_wq[i]->pring;
3954 lpfc_sli_abort_wqe_ring(phba, pring);
3955 }
3956}
3957
James Smartdb55fba2014-04-04 13:52:02 -04003958
3959/**
James Smart3621a712009-04-06 18:47:14 -04003960 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003961 * @phba: Pointer to HBA context object.
3962 *
3963 * This function flushes all iocbs in the fcp ring and frees all the iocb
3964 * objects in txq and txcmplq. This function will not issue abort iocbs
3965 * for all the iocb commands in txcmplq, they will just be returned with
3966 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3967 * slot has been permanently disabled.
3968 **/
3969void
3970lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3971{
3972 LIST_HEAD(txq);
3973 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003974 struct lpfc_sli *psli = &phba->sli;
3975 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003976 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08003977 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04003978
3979 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003980 /* Indicate the I/O queues are flushed */
3981 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003982 spin_unlock_irq(&phba->hbalock);
3983
James Smartdb55fba2014-04-04 13:52:02 -04003984 /* Look on all the FCP Rings for the iotag */
3985 if (phba->sli_rev >= LPFC_SLI_REV4) {
3986 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003987 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smarta8e497d2008-08-24 21:50:11 -04003988
James Smartdb55fba2014-04-04 13:52:02 -04003989 spin_lock_irq(&pring->ring_lock);
3990 /* Retrieve everything on txq */
3991 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08003992 list_for_each_entry_safe(piocb, next_iocb,
3993 &pring->txcmplq, list)
3994 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04003995 /* Retrieve everything on the txcmplq */
3996 list_splice_init(&pring->txcmplq, &txcmplq);
3997 pring->txq_cnt = 0;
3998 pring->txcmplq_cnt = 0;
3999 spin_unlock_irq(&pring->ring_lock);
4000
4001 /* Flush the txq */
4002 lpfc_sli_cancel_iocbs(phba, &txq,
4003 IOSTAT_LOCAL_REJECT,
4004 IOERR_SLI_DOWN);
4005 /* Flush the txcmpq */
4006 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4007 IOSTAT_LOCAL_REJECT,
4008 IOERR_SLI_DOWN);
4009 }
4010 } else {
James Smart895427b2017-02-12 13:52:30 -08004011 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004012
4013 spin_lock_irq(&phba->hbalock);
4014 /* Retrieve everything on txq */
4015 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004016 list_for_each_entry_safe(piocb, next_iocb,
4017 &pring->txcmplq, list)
4018 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004019 /* Retrieve everything on the txcmplq */
4020 list_splice_init(&pring->txcmplq, &txcmplq);
4021 pring->txq_cnt = 0;
4022 pring->txcmplq_cnt = 0;
4023 spin_unlock_irq(&phba->hbalock);
4024
4025 /* Flush the txq */
4026 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4027 IOERR_SLI_DOWN);
4028 /* Flush the txcmpq */
4029 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4030 IOERR_SLI_DOWN);
4031 }
James Smarta8e497d2008-08-24 21:50:11 -04004032}
4033
4034/**
James Smart895427b2017-02-12 13:52:30 -08004035 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
4036 * @phba: Pointer to HBA context object.
4037 *
4038 * This function flushes all wqes in the nvme rings and frees all resources
4039 * in the txcmplq. This function does not issue abort wqes for the IO
4040 * commands in txcmplq, they will just be returned with
4041 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4042 * slot has been permanently disabled.
4043 **/
4044void
4045lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
4046{
4047 LIST_HEAD(txcmplq);
4048 struct lpfc_sli_ring *pring;
4049 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004050 struct lpfc_iocbq *piocb, *next_iocb;
James Smart895427b2017-02-12 13:52:30 -08004051
4052 if (phba->sli_rev < LPFC_SLI_REV4)
4053 return;
4054
4055 /* Hint to other driver operations that a flush is in progress. */
4056 spin_lock_irq(&phba->hbalock);
4057 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
4058 spin_unlock_irq(&phba->hbalock);
4059
4060 /* Cycle through all NVME rings and complete each IO with
4061 * a local driver reason code. This is a flush so no
4062 * abort exchange to FW.
4063 */
4064 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
4065 pring = phba->sli4_hba.nvme_wq[i]->pring;
4066
James Smart895427b2017-02-12 13:52:30 -08004067 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -08004068 list_for_each_entry_safe(piocb, next_iocb,
4069 &pring->txcmplq, list)
4070 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4071 /* Retrieve everything on the txcmplq */
James Smart895427b2017-02-12 13:52:30 -08004072 list_splice_init(&pring->txcmplq, &txcmplq);
4073 pring->txcmplq_cnt = 0;
4074 spin_unlock_irq(&pring->ring_lock);
4075
4076 /* Flush the txcmpq &&&PAE */
4077 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4078 IOSTAT_LOCAL_REJECT,
4079 IOERR_SLI_DOWN);
4080 }
4081}
4082
4083/**
James Smart3772a992009-05-22 14:50:54 -04004084 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004085 * @phba: Pointer to HBA context object.
4086 * @mask: Bit mask to be checked.
4087 *
4088 * This function reads the host status register and compares
4089 * with the provided bit mask to check if HBA completed
4090 * the restart. This function will wait in a loop for the
4091 * HBA to complete restart. If the HBA does not restart within
4092 * 15 iterations, the function will reset the HBA again. The
4093 * function returns 1 when HBA fail to restart otherwise returns
4094 * zero.
4095 **/
James Smart3772a992009-05-22 14:50:54 -04004096static int
4097lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004098{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004099 uint32_t status;
4100 int i = 0;
4101 int retval = 0;
dea31012005-04-17 16:05:31 -05004102
Jamie Wellnitz41415862006-02-28 19:25:27 -05004103 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004104 if (lpfc_readl(phba->HSregaddr, &status))
4105 return 1;
dea31012005-04-17 16:05:31 -05004106
Jamie Wellnitz41415862006-02-28 19:25:27 -05004107 /*
4108 * Check status register every 100ms for 5 retries, then every
4109 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4110 * every 2.5 sec for 4.
4111 * Break our of the loop if errors occurred during init.
4112 */
4113 while (((status & mask) != mask) &&
4114 !(status & HS_FFERM) &&
4115 i++ < 20) {
dea31012005-04-17 16:05:31 -05004116
Jamie Wellnitz41415862006-02-28 19:25:27 -05004117 if (i <= 5)
4118 msleep(10);
4119 else if (i <= 10)
4120 msleep(500);
4121 else
4122 msleep(2500);
dea31012005-04-17 16:05:31 -05004123
Jamie Wellnitz41415862006-02-28 19:25:27 -05004124 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004125 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004126 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004127 lpfc_sli_brdrestart(phba);
4128 }
4129 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004130 if (lpfc_readl(phba->HSregaddr, &status)) {
4131 retval = 1;
4132 break;
4133 }
dea31012005-04-17 16:05:31 -05004134 }
dea31012005-04-17 16:05:31 -05004135
Jamie Wellnitz41415862006-02-28 19:25:27 -05004136 /* Check to see if any errors occurred during init */
4137 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05004138 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4139 "2751 Adapter failed to restart, "
4140 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4141 status,
4142 readl(phba->MBslimaddr + 0xa8),
4143 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004144 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004145 retval = 1;
4146 }
dea31012005-04-17 16:05:31 -05004147
Jamie Wellnitz41415862006-02-28 19:25:27 -05004148 return retval;
dea31012005-04-17 16:05:31 -05004149}
4150
James Smartda0436e2009-05-22 14:51:39 -04004151/**
4152 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4153 * @phba: Pointer to HBA context object.
4154 * @mask: Bit mask to be checked.
4155 *
4156 * This function checks the host status register to check if HBA is
4157 * ready. This function will wait in a loop for the HBA to be ready
4158 * If the HBA is not ready , the function will will reset the HBA PCI
4159 * function again. The function returns 1 when HBA fail to be ready
4160 * otherwise returns zero.
4161 **/
4162static int
4163lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4164{
4165 uint32_t status;
4166 int retval = 0;
4167
4168 /* Read the HBA Host Status Register */
4169 status = lpfc_sli4_post_status_check(phba);
4170
4171 if (status) {
4172 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4173 lpfc_sli_brdrestart(phba);
4174 status = lpfc_sli4_post_status_check(phba);
4175 }
4176
4177 /* Check to see if any errors occurred during init */
4178 if (status) {
4179 phba->link_state = LPFC_HBA_ERROR;
4180 retval = 1;
4181 } else
4182 phba->sli4_hba.intr_enable = 0;
4183
4184 return retval;
4185}
4186
4187/**
4188 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4189 * @phba: Pointer to HBA context object.
4190 * @mask: Bit mask to be checked.
4191 *
4192 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4193 * from the API jump table function pointer from the lpfc_hba struct.
4194 **/
4195int
4196lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4197{
4198 return phba->lpfc_sli_brdready(phba, mask);
4199}
4200
James Smart92908312006-03-07 15:04:13 -05004201#define BARRIER_TEST_PATTERN (0xdeadbeef)
4202
James Smarte59058c2008-08-24 21:49:00 -04004203/**
James Smart3621a712009-04-06 18:47:14 -04004204 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004205 * @phba: Pointer to HBA context object.
4206 *
James Smart1b511972011-12-13 13:23:09 -05004207 * This function is called before resetting an HBA. This function is called
4208 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004209 **/
James Smart2e0fef82007-06-17 19:56:36 -05004210void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004211{
James Smart65a29c12006-07-06 15:50:50 -04004212 uint32_t __iomem *resp_buf;
4213 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004214 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004215 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004216 int i;
4217 uint8_t hdrtype;
4218
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004219 lockdep_assert_held(&phba->hbalock);
4220
James Smart92908312006-03-07 15:04:13 -05004221 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4222 if (hdrtype != 0x80 ||
4223 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4224 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4225 return;
4226
4227 /*
4228 * Tell the other part of the chip to suspend temporarily all
4229 * its DMA activity.
4230 */
James Smart65a29c12006-07-06 15:50:50 -04004231 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004232
4233 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004234 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4235 return;
James Smart92908312006-03-07 15:04:13 -05004236 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4237 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004238 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004239
James Smart9940b972011-03-11 16:06:12 -05004240 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4241 return;
4242 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004243 /* Clear Chip error bit */
4244 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004245 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004246 }
4247
4248 mbox = 0;
4249 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4250 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4251
4252 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004253 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004254 writel(mbox, mbox_buf);
4255
James Smart9940b972011-03-11 16:06:12 -05004256 for (i = 0; i < 50; i++) {
4257 if (lpfc_readl((resp_buf + 1), &resp_data))
4258 return;
4259 if (resp_data != ~(BARRIER_TEST_PATTERN))
4260 mdelay(1);
4261 else
4262 break;
4263 }
4264 resp_data = 0;
4265 if (lpfc_readl((resp_buf + 1), &resp_data))
4266 return;
4267 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004268 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004269 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004270 goto restore_hc;
4271 else
4272 goto clear_errat;
4273 }
4274
4275 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004276 resp_data = 0;
4277 for (i = 0; i < 500; i++) {
4278 if (lpfc_readl(resp_buf, &resp_data))
4279 return;
4280 if (resp_data != mbox)
4281 mdelay(1);
4282 else
4283 break;
4284 }
James Smart92908312006-03-07 15:04:13 -05004285
4286clear_errat:
4287
James Smart9940b972011-03-11 16:06:12 -05004288 while (++i < 500) {
4289 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4290 return;
4291 if (!(ha_copy & HA_ERATT))
4292 mdelay(1);
4293 else
4294 break;
4295 }
James Smart92908312006-03-07 15:04:13 -05004296
4297 if (readl(phba->HAregaddr) & HA_ERATT) {
4298 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004299 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004300 }
4301
4302restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004303 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004304 writel(hc_copy, phba->HCregaddr);
4305 readl(phba->HCregaddr); /* flush */
4306}
4307
James Smarte59058c2008-08-24 21:49:00 -04004308/**
James Smart3621a712009-04-06 18:47:14 -04004309 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004310 * @phba: Pointer to HBA context object.
4311 *
4312 * This function issues a kill_board mailbox command and waits for
4313 * the error attention interrupt. This function is called for stopping
4314 * the firmware processing. The caller is not required to hold any
4315 * locks. This function calls lpfc_hba_down_post function to free
4316 * any pending commands after the kill. The function will return 1 when it
4317 * fails to kill the board else will return 0.
4318 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004319int
James Smart2e0fef82007-06-17 19:56:36 -05004320lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004321{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004322 struct lpfc_sli *psli;
4323 LPFC_MBOXQ_t *pmb;
4324 uint32_t status;
4325 uint32_t ha_copy;
4326 int retval;
4327 int i = 0;
4328
4329 psli = &phba->sli;
4330
4331 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004332 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004333 "0329 Kill HBA Data: x%x x%x\n",
4334 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004335
James Smart98c9ea52007-10-27 13:37:33 -04004336 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4337 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004338 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004339
4340 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004341 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004342 if (lpfc_readl(phba->HCregaddr, &status)) {
4343 spin_unlock_irq(&phba->hbalock);
4344 mempool_free(pmb, phba->mbox_mem_pool);
4345 return 1;
4346 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004347 status &= ~HC_ERINT_ENA;
4348 writel(status, phba->HCregaddr);
4349 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004350 phba->link_flag |= LS_IGNORE_ERATT;
4351 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004352
4353 lpfc_kill_board(phba, pmb);
4354 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4355 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4356
4357 if (retval != MBX_SUCCESS) {
4358 if (retval != MBX_BUSY)
4359 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004360 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4361 "2752 KILL_BOARD command failed retval %d\n",
4362 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004363 spin_lock_irq(&phba->hbalock);
4364 phba->link_flag &= ~LS_IGNORE_ERATT;
4365 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004366 return 1;
4367 }
4368
James Smartf4b4c682009-05-22 14:53:12 -04004369 spin_lock_irq(&phba->hbalock);
4370 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4371 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004372
Jamie Wellnitz41415862006-02-28 19:25:27 -05004373 mempool_free(pmb, phba->mbox_mem_pool);
4374
4375 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4376 * attention every 100ms for 3 seconds. If we don't get ERATT after
4377 * 3 seconds we still set HBA_ERROR state because the status of the
4378 * board is now undefined.
4379 */
James Smart9940b972011-03-11 16:06:12 -05004380 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4381 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004382 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4383 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004384 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4385 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004386 }
4387
4388 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004389 if (ha_copy & HA_ERATT) {
4390 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004391 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004392 }
James Smart2e0fef82007-06-17 19:56:36 -05004393 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004394 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004395 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004396 phba->link_flag &= ~LS_IGNORE_ERATT;
4397 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004398
Jamie Wellnitz41415862006-02-28 19:25:27 -05004399 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004400 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004401
James Smart2e0fef82007-06-17 19:56:36 -05004402 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004403}
4404
James Smarte59058c2008-08-24 21:49:00 -04004405/**
James Smart3772a992009-05-22 14:50:54 -04004406 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004407 * @phba: Pointer to HBA context object.
4408 *
4409 * This function resets the HBA by writing HC_INITFF to the control
4410 * register. After the HBA resets, this function resets all the iocb ring
4411 * indices. This function disables PCI layer parity checking during
4412 * the reset.
4413 * This function returns 0 always.
4414 * The caller is not required to hold any locks.
4415 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004416int
James Smart2e0fef82007-06-17 19:56:36 -05004417lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004418{
4419 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004420 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004421 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004422 int i;
dea31012005-04-17 16:05:31 -05004423
Jamie Wellnitz41415862006-02-28 19:25:27 -05004424 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004425
Jamie Wellnitz41415862006-02-28 19:25:27 -05004426 /* Reset HBA */
4427 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004428 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004429 (phba->pport) ? phba->pport->port_state : 0,
4430 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004431
4432 /* perform board reset */
4433 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004434 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004435 if (phba->pport) {
4436 phba->pport->fc_myDID = 0;
4437 phba->pport->fc_prevDID = 0;
4438 }
dea31012005-04-17 16:05:31 -05004439
Jamie Wellnitz41415862006-02-28 19:25:27 -05004440 /* Turn off parity checking and serr during the physical reset */
4441 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4442 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4443 (cfg_value &
4444 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4445
James Smart3772a992009-05-22 14:50:54 -04004446 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4447
Jamie Wellnitz41415862006-02-28 19:25:27 -05004448 /* Now toggle INITFF bit in the Host Control Register */
4449 writel(HC_INITFF, phba->HCregaddr);
4450 mdelay(1);
4451 readl(phba->HCregaddr); /* flush */
4452 writel(0, phba->HCregaddr);
4453 readl(phba->HCregaddr); /* flush */
4454
4455 /* Restore PCI cmd register */
4456 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004457
4458 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004459 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004460 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004461 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004462 pring->sli.sli3.rspidx = 0;
4463 pring->sli.sli3.next_cmdidx = 0;
4464 pring->sli.sli3.local_getidx = 0;
4465 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004466 pring->missbufcnt = 0;
4467 }
dea31012005-04-17 16:05:31 -05004468
James Smart2e0fef82007-06-17 19:56:36 -05004469 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004470 return 0;
4471}
4472
James Smarte59058c2008-08-24 21:49:00 -04004473/**
James Smartda0436e2009-05-22 14:51:39 -04004474 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4475 * @phba: Pointer to HBA context object.
4476 *
4477 * This function resets a SLI4 HBA. This function disables PCI layer parity
4478 * checking during resets the device. The caller is not required to hold
4479 * any locks.
4480 *
4481 * This function returns 0 always.
4482 **/
4483int
4484lpfc_sli4_brdreset(struct lpfc_hba *phba)
4485{
4486 struct lpfc_sli *psli = &phba->sli;
4487 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004488 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004489
4490 /* Reset HBA */
4491 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004492 "0295 Reset HBA Data: x%x x%x x%x\n",
4493 phba->pport->port_state, psli->sli_flag,
4494 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004495
4496 /* perform board reset */
4497 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004498 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004499 phba->pport->fc_myDID = 0;
4500 phba->pport->fc_prevDID = 0;
4501
James Smartda0436e2009-05-22 14:51:39 -04004502 spin_lock_irq(&phba->hbalock);
4503 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4504 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004505 spin_unlock_irq(&phba->hbalock);
4506
James Smart02936352014-04-04 13:52:12 -04004507 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4508 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4509 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4510 return rc;
4511 }
4512
James Smartda0436e2009-05-22 14:51:39 -04004513 /* Now physically reset the device */
4514 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4515 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004516
4517 /* Turn off parity checking and serr during the physical reset */
4518 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4519 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4520 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4521
James Smart88318812012-09-29 11:29:29 -04004522 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004523 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004524
James Smartbe858b62010-12-15 17:57:20 -05004525 /* Restore PCI cmd register */
4526 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4527
James Smart27b01b82012-05-09 21:19:44 -04004528 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004529}
4530
4531/**
4532 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004533 * @phba: Pointer to HBA context object.
4534 *
4535 * This function is called in the SLI initialization code path to
4536 * restart the HBA. The caller is not required to hold any lock.
4537 * This function writes MBX_RESTART mailbox command to the SLIM and
4538 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4539 * function to free any pending commands. The function enables
4540 * POST only during the first initialization. The function returns zero.
4541 * The function does not guarantee completion of MBX_RESTART mailbox
4542 * command before the return of this function.
4543 **/
James Smartda0436e2009-05-22 14:51:39 -04004544static int
4545lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004546{
4547 MAILBOX_t *mb;
4548 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004549 volatile uint32_t word0;
4550 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004551 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004552
James Smart2e0fef82007-06-17 19:56:36 -05004553 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004554
James Smart0d878412009-10-02 15:16:56 -04004555 /* Take PCIe device Advanced Error Reporting (AER) state */
4556 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4557
Jamie Wellnitz41415862006-02-28 19:25:27 -05004558 psli = &phba->sli;
4559
4560 /* Restart HBA */
4561 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004562 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004563 (phba->pport) ? phba->pport->port_state : 0,
4564 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004565
4566 word0 = 0;
4567 mb = (MAILBOX_t *) &word0;
4568 mb->mbxCommand = MBX_RESTART;
4569 mb->mbxHc = 1;
4570
James Smart92908312006-03-07 15:04:13 -05004571 lpfc_reset_barrier(phba);
4572
Jamie Wellnitz41415862006-02-28 19:25:27 -05004573 to_slim = phba->MBslimaddr;
4574 writel(*(uint32_t *) mb, to_slim);
4575 readl(to_slim); /* flush */
4576
4577 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004578 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004579 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004580 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004581 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004582 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004583 writel(*(uint32_t *) mb, to_slim);
4584 readl(to_slim); /* flush */
4585
4586 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004587 if (phba->pport)
4588 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004589 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004590 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004591 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004592
James Smart64ba8812006-08-02 15:24:34 -04004593 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4594 psli->stats_start = get_seconds();
4595
James Smarteaf15d52008-12-04 22:39:29 -05004596 /* Give the INITFF and Post time to settle. */
4597 mdelay(100);
dea31012005-04-17 16:05:31 -05004598
James Smart0d878412009-10-02 15:16:56 -04004599 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4600 if (hba_aer_enabled)
4601 pci_disable_pcie_error_reporting(phba->pcidev);
4602
Jamie Wellnitz41415862006-02-28 19:25:27 -05004603 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004604
4605 return 0;
4606}
4607
James Smarte59058c2008-08-24 21:49:00 -04004608/**
James Smartda0436e2009-05-22 14:51:39 -04004609 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4610 * @phba: Pointer to HBA context object.
4611 *
4612 * This function is called in the SLI initialization code path to restart
4613 * a SLI4 HBA. The caller is not required to hold any lock.
4614 * At the end of the function, it calls lpfc_hba_down_post function to
4615 * free any pending commands.
4616 **/
4617static int
4618lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4619{
4620 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004621 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004622 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004623
4624 /* Restart HBA */
4625 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4626 "0296 Restart HBA Data: x%x x%x\n",
4627 phba->pport->port_state, psli->sli_flag);
4628
James Smart75baf692010-06-08 18:31:21 -04004629 /* Take PCIe device Advanced Error Reporting (AER) state */
4630 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4631
James Smart27b01b82012-05-09 21:19:44 -04004632 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004633
4634 spin_lock_irq(&phba->hbalock);
4635 phba->pport->stopped = 0;
4636 phba->link_state = LPFC_INIT_START;
4637 phba->hba_flag = 0;
4638 spin_unlock_irq(&phba->hbalock);
4639
4640 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4641 psli->stats_start = get_seconds();
4642
James Smart75baf692010-06-08 18:31:21 -04004643 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4644 if (hba_aer_enabled)
4645 pci_disable_pcie_error_reporting(phba->pcidev);
4646
James Smartda0436e2009-05-22 14:51:39 -04004647 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004648 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004649
James Smart27b01b82012-05-09 21:19:44 -04004650 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004651}
4652
4653/**
4654 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4655 * @phba: Pointer to HBA context object.
4656 *
4657 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4658 * API jump table function pointer from the lpfc_hba struct.
4659**/
4660int
4661lpfc_sli_brdrestart(struct lpfc_hba *phba)
4662{
4663 return phba->lpfc_sli_brdrestart(phba);
4664}
4665
4666/**
James Smart3621a712009-04-06 18:47:14 -04004667 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004668 * @phba: Pointer to HBA context object.
4669 *
4670 * This function is called after a HBA restart to wait for successful
4671 * restart of the HBA. Successful restart of the HBA is indicated by
4672 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4673 * iteration, the function will restart the HBA again. The function returns
4674 * zero if HBA successfully restarted else returns negative error code.
4675 **/
James Smart4492b732017-04-27 15:08:26 -07004676int
dea31012005-04-17 16:05:31 -05004677lpfc_sli_chipset_init(struct lpfc_hba *phba)
4678{
4679 uint32_t status, i = 0;
4680
4681 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004682 if (lpfc_readl(phba->HSregaddr, &status))
4683 return -EIO;
dea31012005-04-17 16:05:31 -05004684
4685 /* Check status register to see what current state is */
4686 i = 0;
4687 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4688
James Smartdcf2a4e2010-09-29 11:18:53 -04004689 /* Check every 10ms for 10 retries, then every 100ms for 90
4690 * retries, then every 1 sec for 50 retires for a total of
4691 * ~60 seconds before reset the board again and check every
4692 * 1 sec for 50 retries. The up to 60 seconds before the
4693 * board ready is required by the Falcon FIPS zeroization
4694 * complete, and any reset the board in between shall cause
4695 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004696 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004697 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004698 /* Adapter failed to init, timeout, status reg
4699 <status> */
James Smarted957682007-06-17 19:56:37 -05004700 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004701 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004702 "timeout, status reg x%x, "
4703 "FW Data: A8 x%x AC x%x\n", status,
4704 readl(phba->MBslimaddr + 0xa8),
4705 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004706 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004707 return -ETIMEDOUT;
4708 }
4709
4710 /* Check to see if any errors occurred during init */
4711 if (status & HS_FFERM) {
4712 /* ERROR: During chipset initialization */
4713 /* Adapter failed to init, chipset, status reg
4714 <status> */
James Smarted957682007-06-17 19:56:37 -05004715 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004716 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004717 "chipset, status reg x%x, "
4718 "FW Data: A8 x%x AC x%x\n", status,
4719 readl(phba->MBslimaddr + 0xa8),
4720 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004721 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004722 return -EIO;
4723 }
4724
James Smartdcf2a4e2010-09-29 11:18:53 -04004725 if (i <= 10)
dea31012005-04-17 16:05:31 -05004726 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004727 else if (i <= 100)
4728 msleep(100);
4729 else
4730 msleep(1000);
dea31012005-04-17 16:05:31 -05004731
James Smartdcf2a4e2010-09-29 11:18:53 -04004732 if (i == 150) {
4733 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004734 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004735 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004736 }
4737 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004738 if (lpfc_readl(phba->HSregaddr, &status))
4739 return -EIO;
dea31012005-04-17 16:05:31 -05004740 }
4741
4742 /* Check to see if any errors occurred during init */
4743 if (status & HS_FFERM) {
4744 /* ERROR: During chipset initialization */
4745 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004747 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004748 "status reg x%x, "
4749 "FW Data: A8 x%x AC x%x\n", status,
4750 readl(phba->MBslimaddr + 0xa8),
4751 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004752 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004753 return -EIO;
4754 }
4755
4756 /* Clear all interrupt enable conditions */
4757 writel(0, phba->HCregaddr);
4758 readl(phba->HCregaddr); /* flush */
4759
4760 /* setup host attn register */
4761 writel(0xffffffff, phba->HAregaddr);
4762 readl(phba->HAregaddr); /* flush */
4763 return 0;
4764}
4765
James Smarte59058c2008-08-24 21:49:00 -04004766/**
James Smart3621a712009-04-06 18:47:14 -04004767 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004768 *
4769 * This function calculates and returns the number of HBQs required to be
4770 * configured.
4771 **/
James Smart78b2d852007-08-02 11:10:21 -04004772int
James Smarted957682007-06-17 19:56:37 -05004773lpfc_sli_hbq_count(void)
4774{
James Smart92d7f7b2007-06-17 19:56:38 -05004775 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004776}
4777
James Smarte59058c2008-08-24 21:49:00 -04004778/**
James Smart3621a712009-04-06 18:47:14 -04004779 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004780 *
4781 * This function adds the number of hbq entries in every HBQ to get
4782 * the total number of hbq entries required for the HBA and returns
4783 * the total count.
4784 **/
James Smarted957682007-06-17 19:56:37 -05004785static int
4786lpfc_sli_hbq_entry_count(void)
4787{
4788 int hbq_count = lpfc_sli_hbq_count();
4789 int count = 0;
4790 int i;
4791
4792 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004793 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004794 return count;
4795}
4796
James Smarte59058c2008-08-24 21:49:00 -04004797/**
James Smart3621a712009-04-06 18:47:14 -04004798 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004799 *
4800 * This function calculates amount of memory required for all hbq entries
4801 * to be configured and returns the total memory required.
4802 **/
dea31012005-04-17 16:05:31 -05004803int
James Smarted957682007-06-17 19:56:37 -05004804lpfc_sli_hbq_size(void)
4805{
4806 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4807}
4808
James Smarte59058c2008-08-24 21:49:00 -04004809/**
James Smart3621a712009-04-06 18:47:14 -04004810 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004811 * @phba: Pointer to HBA context object.
4812 *
4813 * This function is called during the SLI initialization to configure
4814 * all the HBQs and post buffers to the HBQ. The caller is not
4815 * required to hold any locks. This function will return zero if successful
4816 * else it will return negative error code.
4817 **/
James Smarted957682007-06-17 19:56:37 -05004818static int
4819lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4820{
4821 int hbq_count = lpfc_sli_hbq_count();
4822 LPFC_MBOXQ_t *pmb;
4823 MAILBOX_t *pmbox;
4824 uint32_t hbqno;
4825 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004826
James Smart92d7f7b2007-06-17 19:56:38 -05004827 /* Get a Mailbox buffer to setup mailbox
4828 * commands for HBA initialization
4829 */
James Smarted957682007-06-17 19:56:37 -05004830 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4831
4832 if (!pmb)
4833 return -ENOMEM;
4834
James Smart04c68492009-05-22 14:52:52 -04004835 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004836
4837 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4838 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004839 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004840
4841 hbq_entry_index = 0;
4842 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4843 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4844 phba->hbqs[hbqno].hbqPutIdx = 0;
4845 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4846 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004847 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004848 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4849 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004850 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4851
4852 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4853 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4854 mbxStatus <status>, ring <num> */
4855
4856 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004857 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004858 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004859 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004860 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004861 pmbox->mbxStatus, hbqno);
4862
4863 phba->link_state = LPFC_HBA_ERROR;
4864 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004865 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004866 }
4867 }
4868 phba->hbq_count = hbq_count;
4869
James Smarted957682007-06-17 19:56:37 -05004870 mempool_free(pmb, phba->mbox_mem_pool);
4871
James Smart92d7f7b2007-06-17 19:56:38 -05004872 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004873 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4874 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004875 return 0;
4876}
4877
James Smarte59058c2008-08-24 21:49:00 -04004878/**
James Smart4f774512009-05-22 14:52:35 -04004879 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4880 * @phba: Pointer to HBA context object.
4881 *
4882 * This function is called during the SLI initialization to configure
4883 * all the HBQs and post buffers to the HBQ. The caller is not
4884 * required to hold any locks. This function will return zero if successful
4885 * else it will return negative error code.
4886 **/
4887static int
4888lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4889{
4890 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004891 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4892 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004893 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004894 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004895 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004896 return 0;
4897}
4898
4899/**
James Smart3621a712009-04-06 18:47:14 -04004900 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004901 * @phba: Pointer to HBA context object.
4902 * @sli_mode: sli mode - 2/3
4903 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004904 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004905 * to issue config_port mailbox command. This function restarts the
4906 * HBA firmware and issues a config_port mailbox command to configure
4907 * the SLI interface in the sli mode specified by sli_mode
4908 * variable. The caller is not required to hold any locks.
4909 * The function returns 0 if successful, else returns negative error
4910 * code.
4911 **/
James Smart93996272008-08-24 21:50:30 -04004912int
4913lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004914{
4915 LPFC_MBOXQ_t *pmb;
4916 uint32_t resetcount = 0, rc = 0, done = 0;
4917
4918 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4919 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004920 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004921 return -ENOMEM;
4922 }
4923
James Smarted957682007-06-17 19:56:37 -05004924 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004925 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004926 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004927 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004928 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004929 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004930 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004931 rc = lpfc_sli_chipset_init(phba);
4932 if (rc)
4933 break;
4934
James Smart2e0fef82007-06-17 19:56:36 -05004935 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004936 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004937 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004938 resetcount++;
4939
James Smarted957682007-06-17 19:56:37 -05004940 /* Call pre CONFIG_PORT mailbox command initialization. A
4941 * value of 0 means the call was successful. Any other
4942 * nonzero value is a failure, but if ERESTART is returned,
4943 * the driver may reset the HBA and try again.
4944 */
dea31012005-04-17 16:05:31 -05004945 rc = lpfc_config_port_prep(phba);
4946 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004947 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004948 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004949 } else if (rc)
dea31012005-04-17 16:05:31 -05004950 break;
James Smart6d368e52011-05-24 11:44:12 -04004951
James Smart2e0fef82007-06-17 19:56:36 -05004952 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004953 lpfc_config_port(phba, pmb);
4954 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004955 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4956 LPFC_SLI3_HBQ_ENABLED |
4957 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004958 LPFC_SLI3_BG_ENABLED |
4959 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004960 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004961 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004962 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004963 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004964 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004965 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004966 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004967 spin_unlock_irq(&phba->hbalock);
4968 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004969 } else {
4970 /* Allow asynchronous mailbox command to go through */
4971 spin_lock_irq(&phba->hbalock);
4972 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4973 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004974 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004975
4976 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4977 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4978 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4979 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004980 }
dea31012005-04-17 16:05:31 -05004981 }
James Smarted957682007-06-17 19:56:37 -05004982 if (!done) {
4983 rc = -EINVAL;
4984 goto do_prep_failed;
4985 }
James Smart04c68492009-05-22 14:52:52 -04004986 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4987 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004988 rc = -ENXIO;
4989 goto do_prep_failed;
4990 }
James Smart04c68492009-05-22 14:52:52 -04004991 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004992 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004993 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4994 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4995 phba->max_vpi : phba->max_vports;
4996
James Smart34b02dc2008-08-24 21:49:55 -04004997 } else
4998 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004999 phba->fips_level = 0;
5000 phba->fips_spec_rev = 0;
5001 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04005002 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04005003 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
5004 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
5005 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5006 "2850 Security Crypto Active. FIPS x%d "
5007 "(Spec Rev: x%d)",
5008 phba->fips_level, phba->fips_spec_rev);
5009 }
5010 if (pmb->u.mb.un.varCfgPort.sec_err) {
5011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5012 "2856 Config Port Security Crypto "
5013 "Error: x%x ",
5014 pmb->u.mb.un.varCfgPort.sec_err);
5015 }
James Smart04c68492009-05-22 14:52:52 -04005016 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04005017 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005018 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04005019 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04005020
5021 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5022 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05005023
James Smartf44ac122018-03-05 12:04:08 -08005024 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5025 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5026 phba->cfg_enable_bg = 0;
5027 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smarte2a0a9d2008-12-04 22:40:02 -05005028 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5029 "0443 Adapter did not grant "
5030 "BlockGuard\n");
James Smartf44ac122018-03-05 12:04:08 -08005031 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005032 }
James Smart34b02dc2008-08-24 21:49:55 -04005033 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05005034 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04005035 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04005036 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05005037 }
James Smart92d7f7b2007-06-17 19:56:38 -05005038do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05005039 mempool_free(pmb, phba->mbox_mem_pool);
5040 return rc;
5041}
5042
James Smarte59058c2008-08-24 21:49:00 -04005043
5044/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08005045 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04005046 * @phba: Pointer to HBA context object.
5047 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005048 * This function is the main SLI initialization function. This function
5049 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04005050 * error attention handler code. Caller is not required to hold any
5051 * locks. This function issues config_port mailbox command to configure
5052 * the SLI, setup iocb rings and HBQ rings. In the end the function
5053 * calls the config_port_post function to issue init_link mailbox
5054 * command and to start the discovery. The function will return zero
5055 * if successful, else it will return negative error code.
5056 **/
James Smarted957682007-06-17 19:56:37 -05005057int
5058lpfc_sli_hba_setup(struct lpfc_hba *phba)
5059{
5060 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04005061 int mode = 3, i;
5062 int longs;
James Smarted957682007-06-17 19:56:37 -05005063
James Smart12247e82016-07-06 12:36:09 -07005064 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05005065 case 2:
James Smart78b2d852007-08-02 11:10:21 -04005066 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05005067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005068 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05005069 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07005070 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05005071 break;
5072 }
James Smarted957682007-06-17 19:56:37 -05005073 mode = 2;
5074 break;
5075 case 0:
5076 case 3:
5077 break;
5078 default:
James Smart92d7f7b2007-06-17 19:56:38 -05005079 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005080 "1819 Unrecognized sli_mode parameter: %d.\n",
5081 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05005082
5083 break;
5084 }
James Smartb5c53952016-03-31 14:12:30 -07005085 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005086
James Smart93996272008-08-24 21:50:30 -04005087 rc = lpfc_sli_config_port(phba, mode);
5088
James Smart12247e82016-07-06 12:36:09 -07005089 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05005090 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005091 "1820 Unable to select SLI-3. "
5092 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05005093 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04005094 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07005095 else if (rc && mode == 2)
5096 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05005097 if (rc)
dea31012005-04-17 16:05:31 -05005098 goto lpfc_sli_hba_setup_error;
5099
James Smart0d878412009-10-02 15:16:56 -04005100 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5101 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5102 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5103 if (!rc) {
5104 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5105 "2709 This device supports "
5106 "Advanced Error Reporting (AER)\n");
5107 spin_lock_irq(&phba->hbalock);
5108 phba->hba_flag |= HBA_AER_ENABLED;
5109 spin_unlock_irq(&phba->hbalock);
5110 } else {
5111 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5112 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005113 "Advanced Error Reporting (AER): %d\n",
5114 rc);
James Smart0d878412009-10-02 15:16:56 -04005115 phba->cfg_aer_support = 0;
5116 }
5117 }
5118
James Smarted957682007-06-17 19:56:37 -05005119 if (phba->sli_rev == 3) {
5120 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5121 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005122 } else {
5123 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5124 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005125 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005126 }
5127
5128 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005129 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5130 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005131 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005132
5133 if (rc)
5134 goto lpfc_sli_hba_setup_error;
5135
James Smart6d368e52011-05-24 11:44:12 -04005136 /* Initialize VPIs. */
5137 if (phba->sli_rev == LPFC_SLI_REV3) {
5138 /*
5139 * The VPI bitmask and physical ID array are allocated
5140 * and initialized once only - at driver load. A port
5141 * reset doesn't need to reinitialize this memory.
5142 */
5143 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5144 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07005145 phba->vpi_bmask = kcalloc(longs,
5146 sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005147 GFP_KERNEL);
5148 if (!phba->vpi_bmask) {
5149 rc = -ENOMEM;
5150 goto lpfc_sli_hba_setup_error;
5151 }
5152
Kees Cook6396bb22018-06-12 14:03:40 -07005153 phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5154 sizeof(uint16_t),
5155 GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -04005156 if (!phba->vpi_ids) {
5157 kfree(phba->vpi_bmask);
5158 rc = -ENOMEM;
5159 goto lpfc_sli_hba_setup_error;
5160 }
5161 for (i = 0; i < phba->max_vpi; i++)
5162 phba->vpi_ids[i] = i;
5163 }
5164 }
5165
James Smart93996272008-08-24 21:50:30 -04005166 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005167 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5168 rc = lpfc_sli_hbq_setup(phba);
5169 if (rc)
5170 goto lpfc_sli_hba_setup_error;
5171 }
James Smart04c68492009-05-22 14:52:52 -04005172 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005173 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005174 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005175
5176 rc = lpfc_config_port_post(phba);
5177 if (rc)
5178 goto lpfc_sli_hba_setup_error;
5179
James Smarted957682007-06-17 19:56:37 -05005180 return rc;
5181
James Smart92d7f7b2007-06-17 19:56:38 -05005182lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005183 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05005184 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005185 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005186 return rc;
5187}
5188
James Smartda0436e2009-05-22 14:51:39 -04005189/**
5190 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5191 * @phba: Pointer to HBA context object.
5192 * @mboxq: mailbox pointer.
5193 * This function issue a dump mailbox command to read config region
5194 * 23 and parse the records in the region and populate driver
5195 * data structure.
5196 **/
5197static int
James Smartff78d8f2011-12-13 13:21:35 -05005198lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005199{
James Smartff78d8f2011-12-13 13:21:35 -05005200 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005201 struct lpfc_dmabuf *mp;
5202 struct lpfc_mqe *mqe;
5203 uint32_t data_length;
5204 int rc;
5205
5206 /* Program the default value of vlan_id and fc_map */
5207 phba->valid_vlan = 0;
5208 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5209 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5210 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5211
James Smartff78d8f2011-12-13 13:21:35 -05005212 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5213 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005214 return -ENOMEM;
5215
James Smartff78d8f2011-12-13 13:21:35 -05005216 mqe = &mboxq->u.mqe;
5217 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5218 rc = -ENOMEM;
5219 goto out_free_mboxq;
5220 }
5221
James Smartda0436e2009-05-22 14:51:39 -04005222 mp = (struct lpfc_dmabuf *) mboxq->context1;
5223 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5224
5225 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5226 "(%d):2571 Mailbox cmd x%x Status x%x "
5227 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5228 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5229 "CQ: x%x x%x x%x x%x\n",
5230 mboxq->vport ? mboxq->vport->vpi : 0,
5231 bf_get(lpfc_mqe_command, mqe),
5232 bf_get(lpfc_mqe_status, mqe),
5233 mqe->un.mb_words[0], mqe->un.mb_words[1],
5234 mqe->un.mb_words[2], mqe->un.mb_words[3],
5235 mqe->un.mb_words[4], mqe->un.mb_words[5],
5236 mqe->un.mb_words[6], mqe->un.mb_words[7],
5237 mqe->un.mb_words[8], mqe->un.mb_words[9],
5238 mqe->un.mb_words[10], mqe->un.mb_words[11],
5239 mqe->un.mb_words[12], mqe->un.mb_words[13],
5240 mqe->un.mb_words[14], mqe->un.mb_words[15],
5241 mqe->un.mb_words[16], mqe->un.mb_words[50],
5242 mboxq->mcqe.word0,
5243 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5244 mboxq->mcqe.trailer);
5245
5246 if (rc) {
5247 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5248 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005249 rc = -EIO;
5250 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005251 }
5252 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005253 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005254 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 Smartd11e31d2009-06-10 17:23:06 -04005258 }
James Smartda0436e2009-05-22 14:51:39 -04005259
5260 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5261 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5262 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005263 rc = 0;
5264
5265out_free_mboxq:
5266 mempool_free(mboxq, phba->mbox_mem_pool);
5267 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005268}
5269
5270/**
5271 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5272 * @phba: pointer to lpfc hba data structure.
5273 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5274 * @vpd: pointer to the memory to hold resulting port vpd data.
5275 * @vpd_size: On input, the number of bytes allocated to @vpd.
5276 * On output, the number of data bytes in @vpd.
5277 *
5278 * This routine executes a READ_REV SLI4 mailbox command. In
5279 * addition, this routine gets the port vpd data.
5280 *
5281 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005282 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005283 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005284 **/
5285static int
5286lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5287 uint8_t *vpd, uint32_t *vpd_size)
5288{
5289 int rc = 0;
5290 uint32_t dma_size;
5291 struct lpfc_dmabuf *dmabuf;
5292 struct lpfc_mqe *mqe;
5293
5294 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5295 if (!dmabuf)
5296 return -ENOMEM;
5297
5298 /*
5299 * Get a DMA buffer for the vpd data resulting from the READ_REV
5300 * mailbox command.
5301 */
5302 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04005303 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5304 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005305 if (!dmabuf->virt) {
5306 kfree(dmabuf);
5307 return -ENOMEM;
5308 }
James Smartda0436e2009-05-22 14:51:39 -04005309
5310 /*
5311 * The SLI4 implementation of READ_REV conflicts at word1,
5312 * bits 31:16 and SLI4 adds vpd functionality not present
5313 * in SLI3. This code corrects the conflicts.
5314 */
5315 lpfc_read_rev(phba, mboxq);
5316 mqe = &mboxq->u.mqe;
5317 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5318 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5319 mqe->un.read_rev.word1 &= 0x0000FFFF;
5320 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5321 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5322
5323 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5324 if (rc) {
5325 dma_free_coherent(&phba->pcidev->dev, dma_size,
5326 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005327 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005328 return -EIO;
5329 }
5330
James Smartda0436e2009-05-22 14:51:39 -04005331 /*
5332 * The available vpd length cannot be bigger than the
5333 * DMA buffer passed to the port. Catch the less than
5334 * case and update the caller's size.
5335 */
5336 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5337 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5338
James Smartd7c47992010-06-08 18:31:54 -04005339 memcpy(vpd, dmabuf->virt, *vpd_size);
5340
James Smartda0436e2009-05-22 14:51:39 -04005341 dma_free_coherent(&phba->pcidev->dev, dma_size,
5342 dmabuf->virt, dmabuf->phys);
5343 kfree(dmabuf);
5344 return 0;
5345}
5346
5347/**
James Smartcd1c8302011-10-10 21:33:25 -04005348 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5349 * @phba: pointer to lpfc hba data structure.
5350 *
5351 * This routine retrieves SLI4 device physical port name this PCI function
5352 * is attached to.
5353 *
5354 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005355 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005356 * otherwise - failed to retrieve physical port name
5357 **/
5358static int
5359lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5360{
5361 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005362 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5363 struct lpfc_controller_attribute *cntl_attr;
5364 struct lpfc_mbx_get_port_name *get_port_name;
5365 void *virtaddr = NULL;
5366 uint32_t alloclen, reqlen;
5367 uint32_t shdr_status, shdr_add_status;
5368 union lpfc_sli4_cfg_shdr *shdr;
5369 char cport_name = 0;
5370 int rc;
5371
5372 /* We assume nothing at this point */
5373 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5374 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5375
5376 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5377 if (!mboxq)
5378 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005379 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005380 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5381 lpfc_sli4_read_config(phba);
5382 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5383 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005384
5385 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5386 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5387 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5388 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5389 LPFC_SLI4_MBX_NEMBED);
5390 if (alloclen < reqlen) {
5391 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5392 "3084 Allocated DMA memory size (%d) is "
5393 "less than the requested DMA memory size "
5394 "(%d)\n", alloclen, reqlen);
5395 rc = -ENOMEM;
5396 goto out_free_mboxq;
5397 }
5398 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5399 virtaddr = mboxq->sge_array->addr[0];
5400 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5401 shdr = &mbx_cntl_attr->cfg_shdr;
5402 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5403 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5404 if (shdr_status || shdr_add_status || rc) {
5405 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5406 "3085 Mailbox x%x (x%x/x%x) failed, "
5407 "rc:x%x, status:x%x, add_status:x%x\n",
5408 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5409 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5410 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5411 rc, shdr_status, shdr_add_status);
5412 rc = -ENXIO;
5413 goto out_free_mboxq;
5414 }
5415 cntl_attr = &mbx_cntl_attr->cntl_attr;
5416 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5417 phba->sli4_hba.lnk_info.lnk_tp =
5418 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5419 phba->sli4_hba.lnk_info.lnk_no =
5420 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5421 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5422 "3086 lnk_type:%d, lnk_numb:%d\n",
5423 phba->sli4_hba.lnk_info.lnk_tp,
5424 phba->sli4_hba.lnk_info.lnk_no);
5425
5426retrieve_ppname:
5427 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5428 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5429 sizeof(struct lpfc_mbx_get_port_name) -
5430 sizeof(struct lpfc_sli4_cfg_mhdr),
5431 LPFC_SLI4_MBX_EMBED);
5432 get_port_name = &mboxq->u.mqe.un.get_port_name;
5433 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5434 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5435 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5436 phba->sli4_hba.lnk_info.lnk_tp);
5437 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5438 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5439 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5440 if (shdr_status || shdr_add_status || rc) {
5441 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5442 "3087 Mailbox x%x (x%x/x%x) failed: "
5443 "rc:x%x, status:x%x, add_status:x%x\n",
5444 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5445 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5446 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5447 rc, shdr_status, shdr_add_status);
5448 rc = -ENXIO;
5449 goto out_free_mboxq;
5450 }
5451 switch (phba->sli4_hba.lnk_info.lnk_no) {
5452 case LPFC_LINK_NUMBER_0:
5453 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5454 &get_port_name->u.response);
5455 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5456 break;
5457 case LPFC_LINK_NUMBER_1:
5458 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5459 &get_port_name->u.response);
5460 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5461 break;
5462 case LPFC_LINK_NUMBER_2:
5463 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5464 &get_port_name->u.response);
5465 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5466 break;
5467 case LPFC_LINK_NUMBER_3:
5468 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5469 &get_port_name->u.response);
5470 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5471 break;
5472 default:
5473 break;
5474 }
5475
5476 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5477 phba->Port[0] = cport_name;
5478 phba->Port[1] = '\0';
5479 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5480 "3091 SLI get port name: %s\n", phba->Port);
5481 }
5482
5483out_free_mboxq:
5484 if (rc != MBX_TIMEOUT) {
5485 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5486 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5487 else
5488 mempool_free(mboxq, phba->mbox_mem_pool);
5489 }
5490 return rc;
5491}
5492
5493/**
James Smartda0436e2009-05-22 14:51:39 -04005494 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5495 * @phba: pointer to lpfc hba data structure.
5496 *
5497 * This routine is called to explicitly arm the SLI4 device's completion and
5498 * event queues
5499 **/
5500static void
5501lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5502{
James Smart895427b2017-02-12 13:52:30 -08005503 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08005504 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartda0436e2009-05-22 14:51:39 -04005505
James Smartb71413d2018-02-22 08:18:40 -08005506 sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
5507 sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
5508 if (sli4_hba->nvmels_cq)
5509 sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
James Smart895427b2017-02-12 13:52:30 -08005510 LPFC_QUEUE_REARM);
5511
James Smartb71413d2018-02-22 08:18:40 -08005512 if (sli4_hba->fcp_cq)
James Smart895427b2017-02-12 13:52:30 -08005513 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005514 sli4_hba->sli4_cq_release(sli4_hba->fcp_cq[qidx],
James Smart895427b2017-02-12 13:52:30 -08005515 LPFC_QUEUE_REARM);
5516
James Smartb71413d2018-02-22 08:18:40 -08005517 if (sli4_hba->nvme_cq)
James Smart895427b2017-02-12 13:52:30 -08005518 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005519 sli4_hba->sli4_cq_release(sli4_hba->nvme_cq[qidx],
James Smart895427b2017-02-12 13:52:30 -08005520 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005521
James Smartf38fa0b2014-04-04 13:52:21 -04005522 if (phba->cfg_fof)
James Smartb71413d2018-02-22 08:18:40 -08005523 sli4_hba->sli4_cq_release(sli4_hba->oas_cq, LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005524
James Smartb71413d2018-02-22 08:18:40 -08005525 if (sli4_hba->hba_eq)
James Smart895427b2017-02-12 13:52:30 -08005526 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005527 sli4_hba->sli4_eq_release(sli4_hba->hba_eq[qidx],
5528 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005529
James Smart2d7dbc42017-02-12 13:52:35 -08005530 if (phba->nvmet_support) {
5531 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smartb71413d2018-02-22 08:18:40 -08005532 sli4_hba->sli4_cq_release(
5533 sli4_hba->nvmet_cqset[qidx],
James Smart2d7dbc42017-02-12 13:52:35 -08005534 LPFC_QUEUE_REARM);
5535 }
James Smart2e90f4b2011-12-13 13:22:37 -05005536 }
James Smart1ba981f2014-02-20 09:56:45 -05005537
5538 if (phba->cfg_fof)
James Smartb71413d2018-02-22 08:18:40 -08005539 sli4_hba->sli4_eq_release(sli4_hba->fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005540}
5541
5542/**
James Smart6d368e52011-05-24 11:44:12 -04005543 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5544 * @phba: Pointer to HBA context object.
5545 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005546 * @extnt_count: buffer to hold port available extent count.
5547 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005548 *
James Smartb76f2dc2011-07-22 18:37:42 -04005549 * This function calls the port and retrievs the number of available
5550 * extents and their size for a particular extent type.
5551 *
5552 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005553 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005554int
James Smart6d368e52011-05-24 11:44:12 -04005555lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5556 uint16_t *extnt_count, uint16_t *extnt_size)
5557{
5558 int rc = 0;
5559 uint32_t length;
5560 uint32_t mbox_tmo;
5561 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5562 LPFC_MBOXQ_t *mbox;
5563
5564 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5565 if (!mbox)
5566 return -ENOMEM;
5567
5568 /* Find out how many extents are available for this resource type */
5569 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5570 sizeof(struct lpfc_sli4_cfg_mhdr));
5571 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5572 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5573 length, LPFC_SLI4_MBX_EMBED);
5574
5575 /* Send an extents count of 0 - the GET doesn't use it. */
5576 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5577 LPFC_SLI4_MBX_EMBED);
5578 if (unlikely(rc)) {
5579 rc = -EIO;
5580 goto err_exit;
5581 }
5582
5583 if (!phba->sli4_hba.intr_enable)
5584 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5585 else {
James Smarta183a152011-10-10 21:32:43 -04005586 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005587 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5588 }
5589 if (unlikely(rc)) {
5590 rc = -EIO;
5591 goto err_exit;
5592 }
5593
5594 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5595 if (bf_get(lpfc_mbox_hdr_status,
5596 &rsrc_info->header.cfg_shdr.response)) {
5597 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5598 "2930 Failed to get resource extents "
5599 "Status 0x%x Add'l Status 0x%x\n",
5600 bf_get(lpfc_mbox_hdr_status,
5601 &rsrc_info->header.cfg_shdr.response),
5602 bf_get(lpfc_mbox_hdr_add_status,
5603 &rsrc_info->header.cfg_shdr.response));
5604 rc = -EIO;
5605 goto err_exit;
5606 }
5607
5608 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5609 &rsrc_info->u.rsp);
5610 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5611 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005612
5613 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5614 "3162 Retrieved extents type-%d from port: count:%d, "
5615 "size:%d\n", type, *extnt_count, *extnt_size);
5616
5617err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005618 mempool_free(mbox, phba->mbox_mem_pool);
5619 return rc;
5620}
5621
5622/**
5623 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5624 * @phba: Pointer to HBA context object.
5625 * @type: The extent type to check.
5626 *
5627 * This function reads the current available extents from the port and checks
5628 * if the extent count or extent size has changed since the last access.
5629 * Callers use this routine post port reset to understand if there is a
5630 * extent reprovisioning requirement.
5631 *
5632 * Returns:
5633 * -Error: error indicates problem.
5634 * 1: Extent count or size has changed.
5635 * 0: No changes.
5636 **/
5637static int
5638lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5639{
5640 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5641 uint16_t size_diff, rsrc_ext_size;
5642 int rc = 0;
5643 struct lpfc_rsrc_blks *rsrc_entry;
5644 struct list_head *rsrc_blk_list = NULL;
5645
5646 size_diff = 0;
5647 curr_ext_cnt = 0;
5648 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5649 &rsrc_ext_cnt,
5650 &rsrc_ext_size);
5651 if (unlikely(rc))
5652 return -EIO;
5653
5654 switch (type) {
5655 case LPFC_RSC_TYPE_FCOE_RPI:
5656 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5657 break;
5658 case LPFC_RSC_TYPE_FCOE_VPI:
5659 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5660 break;
5661 case LPFC_RSC_TYPE_FCOE_XRI:
5662 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5663 break;
5664 case LPFC_RSC_TYPE_FCOE_VFI:
5665 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5666 break;
5667 default:
5668 break;
5669 }
5670
5671 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5672 curr_ext_cnt++;
5673 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5674 size_diff++;
5675 }
5676
5677 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5678 rc = 1;
5679
5680 return rc;
5681}
5682
5683/**
5684 * lpfc_sli4_cfg_post_extnts -
5685 * @phba: Pointer to HBA context object.
5686 * @extnt_cnt - number of available extents.
5687 * @type - the extent type (rpi, xri, vfi, vpi).
5688 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5689 * @mbox - pointer to the caller's allocated mailbox structure.
5690 *
5691 * This function executes the extents allocation request. It also
5692 * takes care of the amount of memory needed to allocate or get the
5693 * allocated extents. It is the caller's responsibility to evaluate
5694 * the response.
5695 *
5696 * Returns:
5697 * -Error: Error value describes the condition found.
5698 * 0: if successful
5699 **/
5700static int
James Smart8a9d2e82012-05-09 21:16:12 -04005701lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005702 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5703{
5704 int rc = 0;
5705 uint32_t req_len;
5706 uint32_t emb_len;
5707 uint32_t alloc_len, mbox_tmo;
5708
5709 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005710 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005711
5712 /*
5713 * Calculate the size of an embedded mailbox. The uint32_t
5714 * accounts for extents-specific word.
5715 */
5716 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5717 sizeof(uint32_t);
5718
5719 /*
5720 * Presume the allocation and response will fit into an embedded
5721 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5722 */
5723 *emb = LPFC_SLI4_MBX_EMBED;
5724 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005725 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005726 sizeof(union lpfc_sli4_cfg_shdr) +
5727 sizeof(uint32_t);
5728 *emb = LPFC_SLI4_MBX_NEMBED;
5729 }
5730
5731 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5732 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5733 req_len, *emb);
5734 if (alloc_len < req_len) {
5735 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005736 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005737 "less than the requested DMA memory "
5738 "size (x%x)\n", alloc_len, req_len);
5739 return -ENOMEM;
5740 }
James Smart8a9d2e82012-05-09 21:16:12 -04005741 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005742 if (unlikely(rc))
5743 return -EIO;
5744
5745 if (!phba->sli4_hba.intr_enable)
5746 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5747 else {
James Smarta183a152011-10-10 21:32:43 -04005748 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005749 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5750 }
5751
5752 if (unlikely(rc))
5753 rc = -EIO;
5754 return rc;
5755}
5756
5757/**
5758 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5759 * @phba: Pointer to HBA context object.
5760 * @type: The resource extent type to allocate.
5761 *
5762 * This function allocates the number of elements for the specified
5763 * resource type.
5764 **/
5765static int
5766lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5767{
5768 bool emb = false;
5769 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5770 uint16_t rsrc_id, rsrc_start, j, k;
5771 uint16_t *ids;
5772 int i, rc;
5773 unsigned long longs;
5774 unsigned long *bmask;
5775 struct lpfc_rsrc_blks *rsrc_blks;
5776 LPFC_MBOXQ_t *mbox;
5777 uint32_t length;
5778 struct lpfc_id_range *id_array = NULL;
5779 void *virtaddr = NULL;
5780 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5781 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5782 struct list_head *ext_blk_list;
5783
5784 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5785 &rsrc_cnt,
5786 &rsrc_size);
5787 if (unlikely(rc))
5788 return -EIO;
5789
5790 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5791 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5792 "3009 No available Resource Extents "
5793 "for resource type 0x%x: Count: 0x%x, "
5794 "Size 0x%x\n", type, rsrc_cnt,
5795 rsrc_size);
5796 return -ENOMEM;
5797 }
5798
James Smart8a9d2e82012-05-09 21:16:12 -04005799 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5800 "2903 Post resource extents type-0x%x: "
5801 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005802
5803 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5804 if (!mbox)
5805 return -ENOMEM;
5806
James Smart8a9d2e82012-05-09 21:16:12 -04005807 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005808 if (unlikely(rc)) {
5809 rc = -EIO;
5810 goto err_exit;
5811 }
5812
5813 /*
5814 * Figure out where the response is located. Then get local pointers
5815 * to the response data. The port does not guarantee to respond to
5816 * all extents counts request so update the local variable with the
5817 * allocated count from the port.
5818 */
5819 if (emb == LPFC_SLI4_MBX_EMBED) {
5820 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5821 id_array = &rsrc_ext->u.rsp.id[0];
5822 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5823 } else {
5824 virtaddr = mbox->sge_array->addr[0];
5825 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5826 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5827 id_array = &n_rsrc->id;
5828 }
5829
5830 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5831 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5832
5833 /*
5834 * Based on the resource size and count, correct the base and max
5835 * resource values.
5836 */
5837 length = sizeof(struct lpfc_rsrc_blks);
5838 switch (type) {
5839 case LPFC_RSC_TYPE_FCOE_RPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005840 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005841 sizeof(unsigned long),
5842 GFP_KERNEL);
5843 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5844 rc = -ENOMEM;
5845 goto err_exit;
5846 }
Kees Cook6396bb22018-06-12 14:03:40 -07005847 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005848 sizeof(uint16_t),
5849 GFP_KERNEL);
5850 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5851 kfree(phba->sli4_hba.rpi_bmask);
5852 rc = -ENOMEM;
5853 goto err_exit;
5854 }
5855
5856 /*
5857 * The next_rpi was initialized with the maximum available
5858 * count but the port may allocate a smaller number. Catch
5859 * that case and update the next_rpi.
5860 */
5861 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5862
5863 /* Initialize local ptrs for common extent processing later. */
5864 bmask = phba->sli4_hba.rpi_bmask;
5865 ids = phba->sli4_hba.rpi_ids;
5866 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5867 break;
5868 case LPFC_RSC_TYPE_FCOE_VPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005869 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005870 GFP_KERNEL);
5871 if (unlikely(!phba->vpi_bmask)) {
5872 rc = -ENOMEM;
5873 goto err_exit;
5874 }
Kees Cook6396bb22018-06-12 14:03:40 -07005875 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04005876 GFP_KERNEL);
5877 if (unlikely(!phba->vpi_ids)) {
5878 kfree(phba->vpi_bmask);
5879 rc = -ENOMEM;
5880 goto err_exit;
5881 }
5882
5883 /* Initialize local ptrs for common extent processing later. */
5884 bmask = phba->vpi_bmask;
5885 ids = phba->vpi_ids;
5886 ext_blk_list = &phba->lpfc_vpi_blk_list;
5887 break;
5888 case LPFC_RSC_TYPE_FCOE_XRI:
Kees Cook6396bb22018-06-12 14:03:40 -07005889 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005890 sizeof(unsigned long),
5891 GFP_KERNEL);
5892 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5893 rc = -ENOMEM;
5894 goto err_exit;
5895 }
James Smart8a9d2e82012-05-09 21:16:12 -04005896 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07005897 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005898 sizeof(uint16_t),
5899 GFP_KERNEL);
5900 if (unlikely(!phba->sli4_hba.xri_ids)) {
5901 kfree(phba->sli4_hba.xri_bmask);
5902 rc = -ENOMEM;
5903 goto err_exit;
5904 }
5905
5906 /* Initialize local ptrs for common extent processing later. */
5907 bmask = phba->sli4_hba.xri_bmask;
5908 ids = phba->sli4_hba.xri_ids;
5909 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5910 break;
5911 case LPFC_RSC_TYPE_FCOE_VFI:
Kees Cook6396bb22018-06-12 14:03:40 -07005912 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005913 sizeof(unsigned long),
5914 GFP_KERNEL);
5915 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5916 rc = -ENOMEM;
5917 goto err_exit;
5918 }
Kees Cook6396bb22018-06-12 14:03:40 -07005919 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005920 sizeof(uint16_t),
5921 GFP_KERNEL);
5922 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5923 kfree(phba->sli4_hba.vfi_bmask);
5924 rc = -ENOMEM;
5925 goto err_exit;
5926 }
5927
5928 /* Initialize local ptrs for common extent processing later. */
5929 bmask = phba->sli4_hba.vfi_bmask;
5930 ids = phba->sli4_hba.vfi_ids;
5931 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5932 break;
5933 default:
5934 /* Unsupported Opcode. Fail call. */
5935 id_array = NULL;
5936 bmask = NULL;
5937 ids = NULL;
5938 ext_blk_list = NULL;
5939 goto err_exit;
5940 }
5941
5942 /*
5943 * Complete initializing the extent configuration with the
5944 * allocated ids assigned to this function. The bitmask serves
5945 * as an index into the array and manages the available ids. The
5946 * array just stores the ids communicated to the port via the wqes.
5947 */
5948 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5949 if ((i % 2) == 0)
5950 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5951 &id_array[k]);
5952 else
5953 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5954 &id_array[k]);
5955
5956 rsrc_blks = kzalloc(length, GFP_KERNEL);
5957 if (unlikely(!rsrc_blks)) {
5958 rc = -ENOMEM;
5959 kfree(bmask);
5960 kfree(ids);
5961 goto err_exit;
5962 }
5963 rsrc_blks->rsrc_start = rsrc_id;
5964 rsrc_blks->rsrc_size = rsrc_size;
5965 list_add_tail(&rsrc_blks->list, ext_blk_list);
5966 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08005967 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart6d368e52011-05-24 11:44:12 -04005968 phba->sli4_hba.scsi_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08005969 lpfc_sli4_get_iocb_cnt(phba);
5970 phba->sli4_hba.nvme_xri_start =
5971 phba->sli4_hba.scsi_xri_start +
5972 phba->sli4_hba.scsi_xri_max;
5973 }
James Smart6d368e52011-05-24 11:44:12 -04005974
5975 while (rsrc_id < (rsrc_start + rsrc_size)) {
5976 ids[j] = rsrc_id;
5977 rsrc_id++;
5978 j++;
5979 }
5980 /* Entire word processed. Get next word.*/
5981 if ((i % 2) == 1)
5982 k++;
5983 }
5984 err_exit:
5985 lpfc_sli4_mbox_cmd_free(phba, mbox);
5986 return rc;
5987}
5988
James Smart895427b2017-02-12 13:52:30 -08005989
5990
James Smart6d368e52011-05-24 11:44:12 -04005991/**
5992 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5993 * @phba: Pointer to HBA context object.
5994 * @type: the extent's type.
5995 *
5996 * This function deallocates all extents of a particular resource type.
5997 * SLI4 does not allow for deallocating a particular extent range. It
5998 * is the caller's responsibility to release all kernel memory resources.
5999 **/
6000static int
6001lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6002{
6003 int rc;
6004 uint32_t length, mbox_tmo = 0;
6005 LPFC_MBOXQ_t *mbox;
6006 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6007 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6008
6009 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6010 if (!mbox)
6011 return -ENOMEM;
6012
6013 /*
6014 * This function sends an embedded mailbox because it only sends the
6015 * the resource type. All extents of this type are released by the
6016 * port.
6017 */
6018 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6019 sizeof(struct lpfc_sli4_cfg_mhdr));
6020 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6021 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6022 length, LPFC_SLI4_MBX_EMBED);
6023
6024 /* Send an extents count of 0 - the dealloc doesn't use it. */
6025 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6026 LPFC_SLI4_MBX_EMBED);
6027 if (unlikely(rc)) {
6028 rc = -EIO;
6029 goto out_free_mbox;
6030 }
6031 if (!phba->sli4_hba.intr_enable)
6032 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6033 else {
James Smarta183a152011-10-10 21:32:43 -04006034 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006035 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6036 }
6037 if (unlikely(rc)) {
6038 rc = -EIO;
6039 goto out_free_mbox;
6040 }
6041
6042 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6043 if (bf_get(lpfc_mbox_hdr_status,
6044 &dealloc_rsrc->header.cfg_shdr.response)) {
6045 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6046 "2919 Failed to release resource extents "
6047 "for type %d - Status 0x%x Add'l Status 0x%x. "
6048 "Resource memory not released.\n",
6049 type,
6050 bf_get(lpfc_mbox_hdr_status,
6051 &dealloc_rsrc->header.cfg_shdr.response),
6052 bf_get(lpfc_mbox_hdr_add_status,
6053 &dealloc_rsrc->header.cfg_shdr.response));
6054 rc = -EIO;
6055 goto out_free_mbox;
6056 }
6057
6058 /* Release kernel memory resources for the specific type. */
6059 switch (type) {
6060 case LPFC_RSC_TYPE_FCOE_VPI:
6061 kfree(phba->vpi_bmask);
6062 kfree(phba->vpi_ids);
6063 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6064 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6065 &phba->lpfc_vpi_blk_list, list) {
6066 list_del_init(&rsrc_blk->list);
6067 kfree(rsrc_blk);
6068 }
James Smart16a3a202013-04-17 20:14:38 -04006069 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006070 break;
6071 case LPFC_RSC_TYPE_FCOE_XRI:
6072 kfree(phba->sli4_hba.xri_bmask);
6073 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006074 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6075 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6076 list_del_init(&rsrc_blk->list);
6077 kfree(rsrc_blk);
6078 }
6079 break;
6080 case LPFC_RSC_TYPE_FCOE_VFI:
6081 kfree(phba->sli4_hba.vfi_bmask);
6082 kfree(phba->sli4_hba.vfi_ids);
6083 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6084 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6085 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6086 list_del_init(&rsrc_blk->list);
6087 kfree(rsrc_blk);
6088 }
6089 break;
6090 case LPFC_RSC_TYPE_FCOE_RPI:
6091 /* RPI bitmask and physical id array are cleaned up earlier. */
6092 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6093 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6094 list_del_init(&rsrc_blk->list);
6095 kfree(rsrc_blk);
6096 }
6097 break;
6098 default:
6099 break;
6100 }
6101
6102 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6103
6104 out_free_mbox:
6105 mempool_free(mbox, phba->mbox_mem_pool);
6106 return rc;
6107}
6108
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006109static void
James Smart7bdedb32016-07-06 12:36:00 -07006110lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6111 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006112{
James Smart65791f12016-07-06 12:35:56 -07006113 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07006114
James Smart65791f12016-07-06 12:35:56 -07006115 len = sizeof(struct lpfc_mbx_set_feature) -
6116 sizeof(struct lpfc_sli4_cfg_mhdr);
6117 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6118 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6119 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006120
James Smart7bdedb32016-07-06 12:36:00 -07006121 switch (feature) {
6122 case LPFC_SET_UE_RECOVERY:
6123 bf_set(lpfc_mbx_set_feature_UER,
6124 &mbox->u.mqe.un.set_feature, 1);
6125 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6126 mbox->u.mqe.un.set_feature.param_len = 8;
6127 break;
6128 case LPFC_SET_MDS_DIAGS:
6129 bf_set(lpfc_mbx_set_feature_mds,
6130 &mbox->u.mqe.un.set_feature, 1);
6131 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006132 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006133 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6134 mbox->u.mqe.un.set_feature.param_len = 8;
6135 break;
James Smart65791f12016-07-06 12:35:56 -07006136 }
James Smart7bdedb32016-07-06 12:36:00 -07006137
6138 return;
James Smart65791f12016-07-06 12:35:56 -07006139}
6140
James Smart6d368e52011-05-24 11:44:12 -04006141/**
6142 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6143 * @phba: Pointer to HBA context object.
6144 *
6145 * This function allocates all SLI4 resource identifiers.
6146 **/
6147int
6148lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6149{
6150 int i, rc, error = 0;
6151 uint16_t count, base;
6152 unsigned long longs;
6153
James Smartff78d8f2011-12-13 13:21:35 -05006154 if (!phba->sli4_hba.rpi_hdrs_in_use)
6155 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04006156 if (phba->sli4_hba.extents_in_use) {
6157 /*
6158 * The port supports resource extents. The XRI, VPI, VFI, RPI
6159 * resource extent count must be read and allocated before
6160 * provisioning the resource id arrays.
6161 */
6162 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6163 LPFC_IDX_RSRC_RDY) {
6164 /*
6165 * Extent-based resources are set - the driver could
6166 * be in a port reset. Figure out if any corrective
6167 * actions need to be taken.
6168 */
6169 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6170 LPFC_RSC_TYPE_FCOE_VFI);
6171 if (rc != 0)
6172 error++;
6173 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6174 LPFC_RSC_TYPE_FCOE_VPI);
6175 if (rc != 0)
6176 error++;
6177 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6178 LPFC_RSC_TYPE_FCOE_XRI);
6179 if (rc != 0)
6180 error++;
6181 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6182 LPFC_RSC_TYPE_FCOE_RPI);
6183 if (rc != 0)
6184 error++;
6185
6186 /*
6187 * It's possible that the number of resources
6188 * provided to this port instance changed between
6189 * resets. Detect this condition and reallocate
6190 * resources. Otherwise, there is no action.
6191 */
6192 if (error) {
6193 lpfc_printf_log(phba, KERN_INFO,
6194 LOG_MBOX | LOG_INIT,
6195 "2931 Detected extent resource "
6196 "change. Reallocating all "
6197 "extents.\n");
6198 rc = lpfc_sli4_dealloc_extent(phba,
6199 LPFC_RSC_TYPE_FCOE_VFI);
6200 rc = lpfc_sli4_dealloc_extent(phba,
6201 LPFC_RSC_TYPE_FCOE_VPI);
6202 rc = lpfc_sli4_dealloc_extent(phba,
6203 LPFC_RSC_TYPE_FCOE_XRI);
6204 rc = lpfc_sli4_dealloc_extent(phba,
6205 LPFC_RSC_TYPE_FCOE_RPI);
6206 } else
6207 return 0;
6208 }
6209
6210 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6211 if (unlikely(rc))
6212 goto err_exit;
6213
6214 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6215 if (unlikely(rc))
6216 goto err_exit;
6217
6218 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6219 if (unlikely(rc))
6220 goto err_exit;
6221
6222 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6223 if (unlikely(rc))
6224 goto err_exit;
6225 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6226 LPFC_IDX_RSRC_RDY);
6227 return rc;
6228 } else {
6229 /*
6230 * The port does not support resource extents. The XRI, VPI,
6231 * VFI, RPI resource ids were determined from READ_CONFIG.
6232 * Just allocate the bitmasks and provision the resource id
6233 * arrays. If a port reset is active, the resources don't
6234 * need any action - just exit.
6235 */
6236 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006237 LPFC_IDX_RSRC_RDY) {
6238 lpfc_sli4_dealloc_resource_identifiers(phba);
6239 lpfc_sli4_remove_rpis(phba);
6240 }
James Smart6d368e52011-05-24 11:44:12 -04006241 /* RPIs. */
6242 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006243 if (count <= 0) {
6244 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6245 "3279 Invalid provisioning of "
6246 "rpi:%d\n", count);
6247 rc = -EINVAL;
6248 goto err_exit;
6249 }
James Smart6d368e52011-05-24 11:44:12 -04006250 base = phba->sli4_hba.max_cfg_param.rpi_base;
6251 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006252 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006253 sizeof(unsigned long),
6254 GFP_KERNEL);
6255 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6256 rc = -ENOMEM;
6257 goto err_exit;
6258 }
Kees Cook6396bb22018-06-12 14:03:40 -07006259 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006260 GFP_KERNEL);
6261 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6262 rc = -ENOMEM;
6263 goto free_rpi_bmask;
6264 }
6265
6266 for (i = 0; i < count; i++)
6267 phba->sli4_hba.rpi_ids[i] = base + i;
6268
6269 /* VPIs. */
6270 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006271 if (count <= 0) {
6272 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6273 "3280 Invalid provisioning of "
6274 "vpi:%d\n", count);
6275 rc = -EINVAL;
6276 goto free_rpi_ids;
6277 }
James Smart6d368e52011-05-24 11:44:12 -04006278 base = phba->sli4_hba.max_cfg_param.vpi_base;
6279 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006280 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006281 GFP_KERNEL);
6282 if (unlikely(!phba->vpi_bmask)) {
6283 rc = -ENOMEM;
6284 goto free_rpi_ids;
6285 }
Kees Cook6396bb22018-06-12 14:03:40 -07006286 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006287 GFP_KERNEL);
6288 if (unlikely(!phba->vpi_ids)) {
6289 rc = -ENOMEM;
6290 goto free_vpi_bmask;
6291 }
6292
6293 for (i = 0; i < count; i++)
6294 phba->vpi_ids[i] = base + i;
6295
6296 /* XRIs. */
6297 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006298 if (count <= 0) {
6299 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6300 "3281 Invalid provisioning of "
6301 "xri:%d\n", count);
6302 rc = -EINVAL;
6303 goto free_vpi_ids;
6304 }
James Smart6d368e52011-05-24 11:44:12 -04006305 base = phba->sli4_hba.max_cfg_param.xri_base;
6306 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006307 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006308 sizeof(unsigned long),
6309 GFP_KERNEL);
6310 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6311 rc = -ENOMEM;
6312 goto free_vpi_ids;
6313 }
James Smart41899be2012-03-01 22:34:19 -05006314 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006315 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006316 GFP_KERNEL);
6317 if (unlikely(!phba->sli4_hba.xri_ids)) {
6318 rc = -ENOMEM;
6319 goto free_xri_bmask;
6320 }
6321
6322 for (i = 0; i < count; i++)
6323 phba->sli4_hba.xri_ids[i] = base + i;
6324
6325 /* VFIs. */
6326 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006327 if (count <= 0) {
6328 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6329 "3282 Invalid provisioning of "
6330 "vfi:%d\n", count);
6331 rc = -EINVAL;
6332 goto free_xri_ids;
6333 }
James Smart6d368e52011-05-24 11:44:12 -04006334 base = phba->sli4_hba.max_cfg_param.vfi_base;
6335 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006336 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006337 sizeof(unsigned long),
6338 GFP_KERNEL);
6339 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6340 rc = -ENOMEM;
6341 goto free_xri_ids;
6342 }
Kees Cook6396bb22018-06-12 14:03:40 -07006343 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006344 GFP_KERNEL);
6345 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6346 rc = -ENOMEM;
6347 goto free_vfi_bmask;
6348 }
6349
6350 for (i = 0; i < count; i++)
6351 phba->sli4_hba.vfi_ids[i] = base + i;
6352
6353 /*
6354 * Mark all resources ready. An HBA reset doesn't need
6355 * to reset the initialization.
6356 */
6357 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6358 LPFC_IDX_RSRC_RDY);
6359 return 0;
6360 }
6361
6362 free_vfi_bmask:
6363 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006364 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006365 free_xri_ids:
6366 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006367 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006368 free_xri_bmask:
6369 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006370 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006371 free_vpi_ids:
6372 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006373 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006374 free_vpi_bmask:
6375 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006376 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006377 free_rpi_ids:
6378 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006379 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006380 free_rpi_bmask:
6381 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006382 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006383 err_exit:
6384 return rc;
6385}
6386
6387/**
6388 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6389 * @phba: Pointer to HBA context object.
6390 *
6391 * This function allocates the number of elements for the specified
6392 * resource type.
6393 **/
6394int
6395lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6396{
6397 if (phba->sli4_hba.extents_in_use) {
6398 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6399 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6400 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6401 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6402 } else {
6403 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006404 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006405 kfree(phba->vpi_ids);
6406 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6407 kfree(phba->sli4_hba.xri_bmask);
6408 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006409 kfree(phba->sli4_hba.vfi_bmask);
6410 kfree(phba->sli4_hba.vfi_ids);
6411 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6412 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6413 }
6414
6415 return 0;
6416}
6417
6418/**
James Smartb76f2dc2011-07-22 18:37:42 -04006419 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6420 * @phba: Pointer to HBA context object.
6421 * @type: The resource extent type.
6422 * @extnt_count: buffer to hold port extent count response
6423 * @extnt_size: buffer to hold port extent size response.
6424 *
6425 * This function calls the port to read the host allocated extents
6426 * for a particular type.
6427 **/
6428int
6429lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6430 uint16_t *extnt_cnt, uint16_t *extnt_size)
6431{
6432 bool emb;
6433 int rc = 0;
6434 uint16_t curr_blks = 0;
6435 uint32_t req_len, emb_len;
6436 uint32_t alloc_len, mbox_tmo;
6437 struct list_head *blk_list_head;
6438 struct lpfc_rsrc_blks *rsrc_blk;
6439 LPFC_MBOXQ_t *mbox;
6440 void *virtaddr = NULL;
6441 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6442 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6443 union lpfc_sli4_cfg_shdr *shdr;
6444
6445 switch (type) {
6446 case LPFC_RSC_TYPE_FCOE_VPI:
6447 blk_list_head = &phba->lpfc_vpi_blk_list;
6448 break;
6449 case LPFC_RSC_TYPE_FCOE_XRI:
6450 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6451 break;
6452 case LPFC_RSC_TYPE_FCOE_VFI:
6453 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6454 break;
6455 case LPFC_RSC_TYPE_FCOE_RPI:
6456 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6457 break;
6458 default:
6459 return -EIO;
6460 }
6461
6462 /* Count the number of extents currently allocatd for this type. */
6463 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6464 if (curr_blks == 0) {
6465 /*
6466 * The GET_ALLOCATED mailbox does not return the size,
6467 * just the count. The size should be just the size
6468 * stored in the current allocated block and all sizes
6469 * for an extent type are the same so set the return
6470 * value now.
6471 */
6472 *extnt_size = rsrc_blk->rsrc_size;
6473 }
6474 curr_blks++;
6475 }
6476
James Smartb76f2dc2011-07-22 18:37:42 -04006477 /*
6478 * Calculate the size of an embedded mailbox. The uint32_t
6479 * accounts for extents-specific word.
6480 */
6481 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6482 sizeof(uint32_t);
6483
6484 /*
6485 * Presume the allocation and response will fit into an embedded
6486 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6487 */
6488 emb = LPFC_SLI4_MBX_EMBED;
6489 req_len = emb_len;
6490 if (req_len > emb_len) {
6491 req_len = curr_blks * sizeof(uint16_t) +
6492 sizeof(union lpfc_sli4_cfg_shdr) +
6493 sizeof(uint32_t);
6494 emb = LPFC_SLI4_MBX_NEMBED;
6495 }
6496
6497 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6498 if (!mbox)
6499 return -ENOMEM;
6500 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6501
6502 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6503 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6504 req_len, emb);
6505 if (alloc_len < req_len) {
6506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6507 "2983 Allocated DMA memory size (x%x) is "
6508 "less than the requested DMA memory "
6509 "size (x%x)\n", alloc_len, req_len);
6510 rc = -ENOMEM;
6511 goto err_exit;
6512 }
6513 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6514 if (unlikely(rc)) {
6515 rc = -EIO;
6516 goto err_exit;
6517 }
6518
6519 if (!phba->sli4_hba.intr_enable)
6520 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6521 else {
James Smarta183a152011-10-10 21:32:43 -04006522 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006523 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6524 }
6525
6526 if (unlikely(rc)) {
6527 rc = -EIO;
6528 goto err_exit;
6529 }
6530
6531 /*
6532 * Figure out where the response is located. Then get local pointers
6533 * to the response data. The port does not guarantee to respond to
6534 * all extents counts request so update the local variable with the
6535 * allocated count from the port.
6536 */
6537 if (emb == LPFC_SLI4_MBX_EMBED) {
6538 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6539 shdr = &rsrc_ext->header.cfg_shdr;
6540 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6541 } else {
6542 virtaddr = mbox->sge_array->addr[0];
6543 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6544 shdr = &n_rsrc->cfg_shdr;
6545 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6546 }
6547
6548 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6549 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6550 "2984 Failed to read allocated resources "
6551 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6552 type,
6553 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6554 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6555 rc = -EIO;
6556 goto err_exit;
6557 }
6558 err_exit:
6559 lpfc_sli4_mbox_cmd_free(phba, mbox);
6560 return rc;
6561}
6562
6563/**
James Smart0ef69962017-04-21 16:04:50 -07006564 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006565 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006566 * @pring: Pointer to driver SLI ring object.
6567 * @sgl_list: linked link of sgl buffers to post
6568 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006569 *
James Smart895427b2017-02-12 13:52:30 -08006570 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006571 * repost them to the port by using SGL block post. This is needed after a
6572 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006573 * of buffer sgls which contains contiguous xris and uses the non-embedded
6574 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006575 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6576 * mailbox command for posting.
6577 *
6578 * Returns: 0 = success, non-zero failure.
6579 **/
6580static int
James Smart895427b2017-02-12 13:52:30 -08006581lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6582 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006583{
6584 struct lpfc_sglq *sglq_entry = NULL;
6585 struct lpfc_sglq *sglq_entry_next = NULL;
6586 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006587 int status, total_cnt;
6588 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006589 int last_xritag = NO_XRI;
6590 LIST_HEAD(prep_sgl_list);
6591 LIST_HEAD(blck_sgl_list);
6592 LIST_HEAD(allc_sgl_list);
6593 LIST_HEAD(post_sgl_list);
6594 LIST_HEAD(free_sgl_list);
6595
James Smart38c20672013-03-01 16:37:44 -05006596 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006597 spin_lock(&phba->sli4_hba.sgl_list_lock);
6598 list_splice_init(sgl_list, &allc_sgl_list);
6599 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006600 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006601
James Smart895427b2017-02-12 13:52:30 -08006602 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006603 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6604 &allc_sgl_list, list) {
6605 list_del_init(&sglq_entry->list);
6606 block_cnt++;
6607 if ((last_xritag != NO_XRI) &&
6608 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6609 /* a hole in xri block, form a sgl posting block */
6610 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6611 post_cnt = block_cnt - 1;
6612 /* prepare list for next posting block */
6613 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6614 block_cnt = 1;
6615 } else {
6616 /* prepare list for next posting block */
6617 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6618 /* enough sgls for non-embed sgl mbox command */
6619 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6620 list_splice_init(&prep_sgl_list,
6621 &blck_sgl_list);
6622 post_cnt = block_cnt;
6623 block_cnt = 0;
6624 }
6625 }
6626 num_posted++;
6627
6628 /* keep track of last sgl's xritag */
6629 last_xritag = sglq_entry->sli4_xritag;
6630
James Smart895427b2017-02-12 13:52:30 -08006631 /* end of repost sgl list condition for buffers */
6632 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006633 if (post_cnt == 0) {
6634 list_splice_init(&prep_sgl_list,
6635 &blck_sgl_list);
6636 post_cnt = block_cnt;
6637 } else if (block_cnt == 1) {
6638 status = lpfc_sli4_post_sgl(phba,
6639 sglq_entry->phys, 0,
6640 sglq_entry->sli4_xritag);
6641 if (!status) {
6642 /* successful, put sgl to posted list */
6643 list_add_tail(&sglq_entry->list,
6644 &post_sgl_list);
6645 } else {
6646 /* Failure, put sgl to free list */
6647 lpfc_printf_log(phba, KERN_WARNING,
6648 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006649 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006650 "sgl, xritag:x%x\n",
6651 sglq_entry->sli4_xritag);
6652 list_add_tail(&sglq_entry->list,
6653 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006654 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006655 }
6656 }
6657 }
6658
6659 /* continue until a nembed page worth of sgls */
6660 if (post_cnt == 0)
6661 continue;
6662
James Smart895427b2017-02-12 13:52:30 -08006663 /* post the buffer list sgls as a block */
6664 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6665 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006666
6667 if (!status) {
6668 /* success, put sgl list to posted sgl list */
6669 list_splice_init(&blck_sgl_list, &post_sgl_list);
6670 } else {
6671 /* Failure, put sgl list to free sgl list */
6672 sglq_entry_first = list_first_entry(&blck_sgl_list,
6673 struct lpfc_sglq,
6674 list);
6675 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006676 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04006677 "xritag:x%x-x%x\n",
6678 sglq_entry_first->sli4_xritag,
6679 (sglq_entry_first->sli4_xritag +
6680 post_cnt - 1));
6681 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006682 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006683 }
6684
6685 /* don't reset xirtag due to hole in xri block */
6686 if (block_cnt == 0)
6687 last_xritag = NO_XRI;
6688
James Smart895427b2017-02-12 13:52:30 -08006689 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04006690 post_cnt = 0;
6691 }
6692
James Smart895427b2017-02-12 13:52:30 -08006693 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04006694 lpfc_free_sgl_list(phba, &free_sgl_list);
6695
James Smart895427b2017-02-12 13:52:30 -08006696 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04006697 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006698 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006699 spin_lock(&phba->sli4_hba.sgl_list_lock);
6700 list_splice_init(&post_sgl_list, sgl_list);
6701 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006702 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006703 } else {
6704 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006705 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006706 return -EIO;
6707 }
James Smart895427b2017-02-12 13:52:30 -08006708
6709 /* return the number of XRIs actually posted */
6710 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006711}
6712
James Smart61bda8f2016-10-13 15:06:05 -07006713void
6714lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6715{
6716 uint32_t len;
6717
6718 len = sizeof(struct lpfc_mbx_set_host_data) -
6719 sizeof(struct lpfc_sli4_cfg_mhdr);
6720 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6721 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6722 LPFC_SLI4_MBX_EMBED);
6723
6724 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08006725 mbox->u.mqe.un.set_host_data.param_len =
6726 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07006727 snprintf(mbox->u.mqe.un.set_host_data.data,
6728 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6729 "Linux %s v"LPFC_DRIVER_VERSION,
6730 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6731}
6732
James Smarta8cf5df2017-05-15 15:20:46 -07006733int
James Smart6c621a22017-05-15 15:20:45 -07006734lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07006735 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07006736{
6737 int rc, i;
6738 struct lpfc_rqe hrqe;
6739 struct lpfc_rqe drqe;
6740 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08006741 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07006742 struct rqb_dmabuf *rqb_buffer;
6743 LIST_HEAD(rqb_buf_list);
6744
James Smart411de512018-01-30 15:58:52 -08006745 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07006746 rqbp = hrq->rqbp;
6747 for (i = 0; i < count; i++) {
6748 /* IF RQ is already full, don't bother */
6749 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6750 break;
6751 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6752 if (!rqb_buffer)
6753 break;
6754 rqb_buffer->hrq = hrq;
6755 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07006756 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07006757 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6758 }
6759 while (!list_empty(&rqb_buf_list)) {
6760 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6761 hbuf.list);
6762
6763 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6764 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6765 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6766 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6767 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6768 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08006769 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6770 "6421 Cannot post to HRQ %d: %x %x %x "
6771 "DRQ %x %x\n",
6772 hrq->queue_id,
6773 hrq->host_index,
6774 hrq->hba_index,
6775 hrq->entry_count,
6776 drq->host_index,
6777 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07006778 rqbp->rqb_free_buffer(phba, rqb_buffer);
6779 } else {
6780 list_add_tail(&rqb_buffer->hbuf.list,
6781 &rqbp->rqb_buffer_list);
6782 rqbp->buffer_count++;
6783 }
6784 }
James Smart411de512018-01-30 15:58:52 -08006785 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07006786 return 1;
6787}
6788
James Smart8a9d2e82012-05-09 21:16:12 -04006789/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08006790 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04006791 * @phba: Pointer to HBA context object.
6792 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08006793 * This function is the main SLI4 device initialization PCI function. This
6794 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04006795 * HBA error attention handler code. Caller is not required to hold any
6796 * locks.
6797 **/
6798int
6799lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6800{
James Smart6c621a22017-05-15 15:20:45 -07006801 int rc, i, cnt;
James Smartda0436e2009-05-22 14:51:39 -04006802 LPFC_MBOXQ_t *mboxq;
6803 struct lpfc_mqe *mqe;
6804 uint8_t *vpd;
6805 uint32_t vpd_size;
6806 uint32_t ftr_rsp = 0;
6807 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6808 struct lpfc_vport *vport = phba->pport;
6809 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08006810 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04006811
6812 /* Perform a PCI function reset to start from clean */
6813 rc = lpfc_pci_function_reset(phba);
6814 if (unlikely(rc))
6815 return -ENODEV;
6816
6817 /* Check the HBA Host Status Register for readyness */
6818 rc = lpfc_sli4_post_status_check(phba);
6819 if (unlikely(rc))
6820 return -ENODEV;
6821 else {
6822 spin_lock_irq(&phba->hbalock);
6823 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6824 spin_unlock_irq(&phba->hbalock);
6825 }
6826
6827 /*
6828 * Allocate a single mailbox container for initializing the
6829 * port.
6830 */
6831 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6832 if (!mboxq)
6833 return -ENOMEM;
6834
James Smartda0436e2009-05-22 14:51:39 -04006835 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006836 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006837 vpd = kzalloc(vpd_size, GFP_KERNEL);
6838 if (!vpd) {
6839 rc = -ENOMEM;
6840 goto out_free_mbox;
6841 }
6842
6843 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006844 if (unlikely(rc)) {
6845 kfree(vpd);
6846 goto out_free_mbox;
6847 }
James Smart572709e2013-07-15 18:32:43 -04006848
James Smartda0436e2009-05-22 14:51:39 -04006849 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006850 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006851 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006852 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006853 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6854 } else {
James Smart76a95d72010-11-20 23:11:48 -05006855 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006856 }
James Smart45ed1192009-10-02 15:17:02 -04006857
6858 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6859 LPFC_DCBX_CEE_MODE)
6860 phba->hba_flag |= HBA_FIP_SUPPORT;
6861 else
6862 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6863
James Smart4f2e66c2012-05-09 21:17:07 -04006864 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6865
James Smartc31098c2011-04-16 11:03:33 -04006866 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006867 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6868 "0376 READ_REV Error. SLI Level %d "
6869 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006870 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006871 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006872 kfree(vpd);
6873 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006874 }
James Smartcd1c8302011-10-10 21:33:25 -04006875
6876 /*
James Smartff78d8f2011-12-13 13:21:35 -05006877 * Continue initialization with default values even if driver failed
6878 * to read FCoE param config regions, only read parameters if the
6879 * board is FCoE
6880 */
6881 if (phba->hba_flag & HBA_FCOE_MODE &&
6882 lpfc_sli4_read_fcoe_params(phba))
6883 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6884 "2570 Failed to read FCoE parameters\n");
6885
6886 /*
James Smartcd1c8302011-10-10 21:33:25 -04006887 * Retrieve sli4 device physical port name, failure of doing it
6888 * is considered as non-fatal.
6889 */
6890 rc = lpfc_sli4_retrieve_pport_name(phba);
6891 if (!rc)
6892 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6893 "3080 Successful retrieving SLI4 device "
6894 "physical port name: %s.\n", phba->Port);
6895
James Smartda0436e2009-05-22 14:51:39 -04006896 /*
6897 * Evaluate the read rev and vpd data. Populate the driver
6898 * state with the results. If this routine fails, the failure
6899 * is not fatal as the driver will use generic values.
6900 */
6901 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6902 if (unlikely(!rc)) {
6903 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6904 "0377 Error %d parsing vpd. "
6905 "Using defaults.\n", rc);
6906 rc = 0;
6907 }
James Smart76a95d72010-11-20 23:11:48 -05006908 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006909
James Smartf1126682009-06-10 17:22:44 -04006910 /* Save information as VPD data */
6911 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6912 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
James Smart4e565cf2018-02-22 08:18:50 -08006913
6914 /*
6915 * This is because first G7 ASIC doesn't support the standard
6916 * 0x5a NVME cmd descriptor type/subtype
6917 */
6918 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6919 LPFC_SLI_INTF_IF_TYPE_6) &&
6920 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
6921 (phba->vpd.rev.smRev == 0) &&
6922 (phba->cfg_nvme_embed_cmd == 1))
6923 phba->cfg_nvme_embed_cmd = 0;
6924
James Smartf1126682009-06-10 17:22:44 -04006925 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6926 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6927 &mqe->un.read_rev);
6928 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6929 &mqe->un.read_rev);
6930 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6931 &mqe->un.read_rev);
6932 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6933 &mqe->un.read_rev);
6934 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6935 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6936 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6937 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6938 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6939 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6940 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6941 "(%d):0380 READ_REV Status x%x "
6942 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6943 mboxq->vport ? mboxq->vport->vpi : 0,
6944 bf_get(lpfc_mqe_status, mqe),
6945 phba->vpd.rev.opFwName,
6946 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6947 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006948
James Smart572709e2013-07-15 18:32:43 -04006949 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6950 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6951 if (phba->pport->cfg_lun_queue_depth > rc) {
6952 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6953 "3362 LUN queue depth changed from %d to %d\n",
6954 phba->pport->cfg_lun_queue_depth, rc);
6955 phba->pport->cfg_lun_queue_depth = rc;
6956 }
6957
James Smart65791f12016-07-06 12:35:56 -07006958 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006959 LPFC_SLI_INTF_IF_TYPE_0) {
6960 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6961 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6962 if (rc == MBX_SUCCESS) {
6963 phba->hba_flag |= HBA_RECOVERABLE_UE;
6964 /* Set 1Sec interval to detect UE */
6965 phba->eratt_poll_interval = 1;
6966 phba->sli4_hba.ue_to_sr = bf_get(
6967 lpfc_mbx_set_feature_UESR,
6968 &mboxq->u.mqe.un.set_feature);
6969 phba->sli4_hba.ue_to_rp = bf_get(
6970 lpfc_mbx_set_feature_UERP,
6971 &mboxq->u.mqe.un.set_feature);
6972 }
6973 }
6974
6975 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6976 /* Enable MDS Diagnostics only if the SLI Port supports it */
6977 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6978 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6979 if (rc != MBX_SUCCESS)
6980 phba->mds_diags_support = 0;
6981 }
James Smart572709e2013-07-15 18:32:43 -04006982
James Smartda0436e2009-05-22 14:51:39 -04006983 /*
6984 * Discover the port's supported feature set and match it against the
6985 * hosts requests.
6986 */
6987 lpfc_request_features(phba, mboxq);
6988 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6989 if (unlikely(rc)) {
6990 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006991 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006992 }
6993
6994 /*
6995 * The port must support FCP initiator mode as this is the
6996 * only mode running in the host.
6997 */
6998 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6999 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7000 "0378 No support for fcpi mode.\n");
7001 ftr_rsp++;
7002 }
James Smart0bc2b7c2018-02-22 08:18:48 -08007003
7004 /* Performance Hints are ONLY for FCoE */
7005 if (phba->hba_flag & HBA_FCOE_MODE) {
7006 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7007 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7008 else
7009 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7010 }
7011
James Smartda0436e2009-05-22 14:51:39 -04007012 /*
7013 * If the port cannot support the host's requested features
7014 * then turn off the global config parameters to disable the
7015 * feature in the driver. This is not a fatal error.
7016 */
James Smartf44ac122018-03-05 12:04:08 -08007017 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7018 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7019 phba->cfg_enable_bg = 0;
7020 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smartbf086112011-08-21 21:48:13 -04007021 ftr_rsp++;
James Smartf44ac122018-03-05 12:04:08 -08007022 }
James Smartbf086112011-08-21 21:48:13 -04007023 }
James Smartda0436e2009-05-22 14:51:39 -04007024
7025 if (phba->max_vpi && phba->cfg_enable_npiv &&
7026 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7027 ftr_rsp++;
7028
7029 if (ftr_rsp) {
7030 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7031 "0379 Feature Mismatch Data: x%08x %08x "
7032 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7033 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7034 phba->cfg_enable_npiv, phba->max_vpi);
7035 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7036 phba->cfg_enable_bg = 0;
7037 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7038 phba->cfg_enable_npiv = 0;
7039 }
7040
7041 /* These SLI3 features are assumed in SLI4 */
7042 spin_lock_irq(&phba->hbalock);
7043 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7044 spin_unlock_irq(&phba->hbalock);
7045
James Smart6d368e52011-05-24 11:44:12 -04007046 /*
7047 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
7048 * calls depends on these resources to complete port setup.
7049 */
7050 rc = lpfc_sli4_alloc_resource_identifiers(phba);
7051 if (rc) {
7052 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7053 "2920 Failed to alloc Resource IDs "
7054 "rc = x%x\n", rc);
7055 goto out_free_mbox;
7056 }
7057
James Smart61bda8f2016-10-13 15:06:05 -07007058 lpfc_set_host_data(phba, mboxq);
7059
7060 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7061 if (rc) {
7062 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7063 "2134 Failed to set host os driver version %x",
7064 rc);
7065 }
7066
James Smartda0436e2009-05-22 14:51:39 -04007067 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05007068 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7069 if (rc) {
7070 phba->link_state = LPFC_HBA_ERROR;
7071 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05007072 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05007073 }
7074
James Smartda0436e2009-05-22 14:51:39 -04007075 mboxq->vport = vport;
7076 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7077 mp = (struct lpfc_dmabuf *) mboxq->context1;
7078 if (rc == MBX_SUCCESS) {
7079 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7080 rc = 0;
7081 }
7082
7083 /*
7084 * This memory was allocated by the lpfc_read_sparam routine. Release
7085 * it to the mbuf pool.
7086 */
7087 lpfc_mbuf_free(phba, mp->virt, mp->phys);
7088 kfree(mp);
7089 mboxq->context1 = NULL;
7090 if (unlikely(rc)) {
7091 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7092 "0382 READ_SPARAM command failed "
7093 "status %d, mbxStatus x%x\n",
7094 rc, bf_get(lpfc_mqe_status, mqe));
7095 phba->link_state = LPFC_HBA_ERROR;
7096 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007097 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007098 }
7099
James Smart05580562011-05-24 11:40:48 -04007100 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04007101
7102 /* Update the fc_host data structures with new wwn. */
7103 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7104 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7105
James Smart895427b2017-02-12 13:52:30 -08007106 /* Create all the SLI4 queues */
7107 rc = lpfc_sli4_queue_create(phba);
7108 if (rc) {
7109 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7110 "3089 Failed to allocate queues\n");
7111 rc = -ENODEV;
7112 goto out_free_mbox;
7113 }
7114 /* Set up all the queues to the device */
7115 rc = lpfc_sli4_queue_setup(phba);
7116 if (unlikely(rc)) {
7117 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7118 "0381 Error %d during queue setup.\n ", rc);
7119 goto out_stop_timers;
7120 }
7121 /* Initialize the driver internal SLI layer lists. */
7122 lpfc_sli4_setup(phba);
7123 lpfc_sli4_queue_init(phba);
7124
7125 /* update host els xri-sgl sizes and mappings */
7126 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007127 if (unlikely(rc)) {
7128 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7129 "1400 Failed to update xri-sgl size and "
7130 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08007131 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007132 }
7133
James Smart8a9d2e82012-05-09 21:16:12 -04007134 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08007135 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7136 phba->sli4_hba.els_xri_cnt);
7137 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04007138 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7139 "0582 Error %d during els sgl post "
7140 "operation\n", rc);
7141 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007142 goto out_destroy_queue;
7143 }
7144 phba->sli4_hba.els_xri_cnt = rc;
7145
James Smartf358dd02017-02-12 13:52:34 -08007146 if (phba->nvmet_support) {
7147 /* update host nvmet xri-sgl sizes and mappings */
7148 rc = lpfc_sli4_nvmet_sgl_update(phba);
7149 if (unlikely(rc)) {
7150 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7151 "6308 Failed to update nvmet-sgl size "
7152 "and mapping: %d\n", rc);
7153 goto out_destroy_queue;
7154 }
7155
7156 /* register the nvmet sgl pool to the port */
7157 rc = lpfc_sli4_repost_sgl_list(
7158 phba,
7159 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7160 phba->sli4_hba.nvmet_xri_cnt);
7161 if (unlikely(rc < 0)) {
7162 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7163 "3117 Error %d during nvmet "
7164 "sgl post\n", rc);
7165 rc = -ENODEV;
7166 goto out_destroy_queue;
7167 }
7168 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07007169
7170 cnt = phba->cfg_iocb_cnt * 1024;
7171 /* We need 1 iocbq for every SGL, for IO processing */
7172 cnt += phba->sli4_hba.nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08007173 } else {
James Smart895427b2017-02-12 13:52:30 -08007174 /* update host scsi xri-sgl sizes and mappings */
7175 rc = lpfc_sli4_scsi_sgl_update(phba);
7176 if (unlikely(rc)) {
7177 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7178 "6309 Failed to update scsi-sgl size "
7179 "and mapping: %d\n", rc);
7180 goto out_destroy_queue;
7181 }
7182
7183 /* update host nvme xri-sgl sizes and mappings */
7184 rc = lpfc_sli4_nvme_sgl_update(phba);
7185 if (unlikely(rc)) {
7186 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7187 "6082 Failed to update nvme-sgl size "
7188 "and mapping: %d\n", rc);
7189 goto out_destroy_queue;
7190 }
James Smart6c621a22017-05-15 15:20:45 -07007191
7192 cnt = phba->cfg_iocb_cnt * 1024;
James Smart11e644e2017-06-15 22:56:48 -07007193 }
7194
7195 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07007196 /* Initialize and populate the iocb list per host */
7197 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007198 "2821 initialize iocb list %d total %d\n",
James Smart6c621a22017-05-15 15:20:45 -07007199 phba->cfg_iocb_cnt, cnt);
7200 rc = lpfc_init_iocb_list(phba, cnt);
7201 if (rc) {
7202 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007203 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07007204 goto out_destroy_queue;
7205 }
James Smart8a9d2e82012-05-09 21:16:12 -04007206 }
7207
James Smart11e644e2017-06-15 22:56:48 -07007208 if (phba->nvmet_support)
7209 lpfc_nvmet_create_targetport(phba);
7210
James Smart2d7dbc42017-02-12 13:52:35 -08007211 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007212 /* Post initial buffers to all RQs created */
7213 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7214 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7215 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7216 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7217 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007218 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007219 rqbp->buffer_count = 0;
7220
James Smart2d7dbc42017-02-12 13:52:35 -08007221 lpfc_post_rq_buffer(
7222 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7223 phba->sli4_hba.nvmet_mrq_data[i],
James Smart2448e482018-04-09 14:24:24 -07007224 phba->cfg_nvmet_mrq_post, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007225 }
7226 }
7227
James Smart895427b2017-02-12 13:52:30 -08007228 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
7229 /* register the allocated scsi sgl pool to the port */
7230 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
7231 if (unlikely(rc)) {
7232 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7233 "0383 Error %d during scsi sgl post "
7234 "operation\n", rc);
7235 /* Some Scsi buffers were moved to abort scsi list */
7236 /* A pci function reset will repost them */
7237 rc = -ENODEV;
7238 goto out_destroy_queue;
7239 }
James Smartda0436e2009-05-22 14:51:39 -04007240 }
7241
James Smart01649562017-02-12 13:52:32 -08007242 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7243 (phba->nvmet_support == 0)) {
7244
7245 /* register the allocated nvme sgl pool to the port */
7246 rc = lpfc_repost_nvme_sgl_list(phba);
7247 if (unlikely(rc)) {
7248 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7249 "6116 Error %d during nvme sgl post "
7250 "operation\n", rc);
7251 /* Some NVME buffers were moved to abort nvme list */
7252 /* A pci function reset will repost them */
7253 rc = -ENODEV;
7254 goto out_destroy_queue;
7255 }
James Smartda0436e2009-05-22 14:51:39 -04007256 }
7257
7258 /* Post the rpi header region to the device. */
7259 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7260 if (unlikely(rc)) {
7261 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7262 "0393 Error %d during rpi post operation\n",
7263 rc);
7264 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007265 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007266 }
James Smart97f2ecf2012-03-01 22:35:23 -05007267 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007268
James Smart895427b2017-02-12 13:52:30 -08007269 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007270 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007271 /*
7272 * The FC Port needs to register FCFI (index 0)
7273 */
7274 lpfc_reg_fcfi(phba, mboxq);
7275 mboxq->vport = phba->pport;
7276 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7277 if (rc != MBX_SUCCESS)
7278 goto out_unset_queue;
7279 rc = 0;
7280 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7281 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007282 } else {
7283 /* We are a NVME Target mode with MRQ > 1 */
7284
7285 /* First register the FCFI */
7286 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7287 mboxq->vport = phba->pport;
7288 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7289 if (rc != MBX_SUCCESS)
7290 goto out_unset_queue;
7291 rc = 0;
7292 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7293 &mboxq->u.mqe.un.reg_fcfi_mrq);
7294
7295 /* Next register the MRQs */
7296 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7297 mboxq->vport = phba->pport;
7298 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7299 if (rc != MBX_SUCCESS)
7300 goto out_unset_queue;
7301 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007302 }
7303 /* Check if the port is configured to be disabled */
7304 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007305 }
7306
7307 /* Arm the CQs and then EQs on device */
7308 lpfc_sli4_arm_cqeq_intr(phba);
7309
7310 /* Indicate device interrupt mode */
7311 phba->sli4_hba.intr_enable = 1;
7312
7313 /* Allow asynchronous mailbox command to go through */
7314 spin_lock_irq(&phba->hbalock);
7315 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7316 spin_unlock_irq(&phba->hbalock);
7317
7318 /* Post receive buffers to the device */
7319 lpfc_sli4_rb_setup(phba);
7320
James Smartfc2b9892010-02-26 14:15:29 -05007321 /* Reset HBA FCF states after HBA reset */
7322 phba->fcf.fcf_flag = 0;
7323 phba->fcf.current_rec.flag = 0;
7324
James Smartda0436e2009-05-22 14:51:39 -04007325 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007326 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007327 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007328
7329 /* Start heart beat timer */
7330 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007331 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007332 phba->hb_outstanding = 0;
7333 phba->last_completion_time = jiffies;
7334
7335 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007336 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007337 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007338
James Smart75baf692010-06-08 18:31:21 -04007339 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7340 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7341 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7342 if (!rc) {
7343 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7344 "2829 This device supports "
7345 "Advanced Error Reporting (AER)\n");
7346 spin_lock_irq(&phba->hbalock);
7347 phba->hba_flag |= HBA_AER_ENABLED;
7348 spin_unlock_irq(&phba->hbalock);
7349 } else {
7350 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7351 "2830 This device does not support "
7352 "Advanced Error Reporting (AER)\n");
7353 phba->cfg_aer_support = 0;
7354 }
James Smart0a96e972011-07-22 18:37:28 -04007355 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007356 }
7357
James Smartda0436e2009-05-22 14:51:39 -04007358 /*
7359 * The port is ready, set the host's link state to LINK_DOWN
7360 * in preparation for link interrupts.
7361 */
James Smartda0436e2009-05-22 14:51:39 -04007362 spin_lock_irq(&phba->hbalock);
7363 phba->link_state = LPFC_LINK_DOWN;
7364 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05007365 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7366 (phba->hba_flag & LINK_DISABLED)) {
7367 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7368 "3103 Adapter Link is disabled.\n");
7369 lpfc_down_link(phba, mboxq);
7370 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7371 if (rc != MBX_SUCCESS) {
7372 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7373 "3104 Adapter failed to issue "
7374 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7375 goto out_unset_queue;
7376 }
7377 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007378 /* don't perform init_link on SLI4 FC port loopback test */
7379 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7380 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7381 if (rc)
7382 goto out_unset_queue;
7383 }
James Smart5350d872011-10-10 21:33:49 -04007384 }
7385 mempool_free(mboxq, phba->mbox_mem_pool);
7386 return rc;
James Smart76a95d72010-11-20 23:11:48 -05007387out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007388 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007389 lpfc_sli4_queue_unset(phba);
7390out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007391 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007392 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007393out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007394 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007395out_free_mbox:
7396 mempool_free(mboxq, phba->mbox_mem_pool);
7397 return rc;
7398}
James Smarte59058c2008-08-24 21:49:00 -04007399
7400/**
James Smart3621a712009-04-06 18:47:14 -04007401 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007402 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007403 *
James Smarte59058c2008-08-24 21:49:00 -04007404 * This is the callback function for mailbox timer. The mailbox
7405 * timer is armed when a new mailbox command is issued and the timer
7406 * is deleted when the mailbox complete. The function is called by
7407 * the kernel timer code when a mailbox does not complete within
7408 * expected time. This function wakes up the worker thread to
7409 * process the mailbox timeout and returns. All the processing is
7410 * done by the worker thread function lpfc_mbox_timeout_handler.
7411 **/
dea31012005-04-17 16:05:31 -05007412void
Kees Cookf22eb4d2017-09-06 20:24:26 -07007413lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05007414{
Kees Cookf22eb4d2017-09-06 20:24:26 -07007415 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05007416 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007417 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007418
James Smart2e0fef82007-06-17 19:56:36 -05007419 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007420 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007421 if (!tmo_posted)
7422 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7423 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7424
James Smart5e9d9b82008-06-14 22:52:53 -04007425 if (!tmo_posted)
7426 lpfc_worker_wake_up(phba);
7427 return;
dea31012005-04-17 16:05:31 -05007428}
7429
James Smarte8d3c3b2013-10-10 12:21:30 -04007430/**
7431 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7432 * are pending
7433 * @phba: Pointer to HBA context object.
7434 *
7435 * This function checks if any mailbox completions are present on the mailbox
7436 * completion queue.
7437 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007438static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007439lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7440{
7441
7442 uint32_t idx;
7443 struct lpfc_queue *mcq;
7444 struct lpfc_mcqe *mcqe;
7445 bool pending_completions = false;
James Smart7365f6f2018-02-22 08:18:46 -08007446 uint8_t qe_valid;
James Smarte8d3c3b2013-10-10 12:21:30 -04007447
7448 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7449 return false;
7450
7451 /* Check for completions on mailbox completion queue */
7452
7453 mcq = phba->sli4_hba.mbx_cq;
7454 idx = mcq->hba_index;
James Smart7365f6f2018-02-22 08:18:46 -08007455 qe_valid = mcq->qe_valid;
7456 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe) == qe_valid) {
James Smarte8d3c3b2013-10-10 12:21:30 -04007457 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7458 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7459 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7460 pending_completions = true;
7461 break;
7462 }
7463 idx = (idx + 1) % mcq->entry_count;
7464 if (mcq->hba_index == idx)
7465 break;
James Smart7365f6f2018-02-22 08:18:46 -08007466
7467 /* if the index wrapped around, toggle the valid bit */
7468 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7469 qe_valid = (qe_valid) ? 0 : 1;
James Smarte8d3c3b2013-10-10 12:21:30 -04007470 }
7471 return pending_completions;
7472
7473}
7474
7475/**
7476 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7477 * that were missed.
7478 * @phba: Pointer to HBA context object.
7479 *
7480 * For sli4, it is possible to miss an interrupt. As such mbox completions
7481 * maybe missed causing erroneous mailbox timeouts to occur. This function
7482 * checks to see if mbox completions are on the mailbox completion queue
7483 * and will process all the completions associated with the eq for the
7484 * mailbox completion queue.
7485 **/
7486bool
7487lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7488{
James Smartb71413d2018-02-22 08:18:40 -08007489 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04007490 uint32_t eqidx;
7491 struct lpfc_queue *fpeq = NULL;
7492 struct lpfc_eqe *eqe;
7493 bool mbox_pending;
7494
7495 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7496 return false;
7497
7498 /* Find the eq associated with the mcq */
7499
James Smartb71413d2018-02-22 08:18:40 -08007500 if (sli4_hba->hba_eq)
James Smart895427b2017-02-12 13:52:30 -08007501 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
James Smartb71413d2018-02-22 08:18:40 -08007502 if (sli4_hba->hba_eq[eqidx]->queue_id ==
7503 sli4_hba->mbx_cq->assoc_qid) {
7504 fpeq = sli4_hba->hba_eq[eqidx];
James Smarte8d3c3b2013-10-10 12:21:30 -04007505 break;
7506 }
7507 if (!fpeq)
7508 return false;
7509
7510 /* Turn off interrupts from this EQ */
7511
James Smartb71413d2018-02-22 08:18:40 -08007512 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04007513
7514 /* Check to see if a mbox completion is pending */
7515
7516 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7517
7518 /*
7519 * If a mbox completion is pending, process all the events on EQ
7520 * associated with the mbox completion queue (this could include
7521 * mailbox commands, async events, els commands, receive queue data
7522 * and fcp commands)
7523 */
7524
7525 if (mbox_pending)
7526 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7527 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7528 fpeq->EQ_processed++;
7529 }
7530
7531 /* Always clear and re-arm the EQ */
7532
James Smartb71413d2018-02-22 08:18:40 -08007533 sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04007534
7535 return mbox_pending;
7536
7537}
James Smarte59058c2008-08-24 21:49:00 -04007538
7539/**
James Smart3621a712009-04-06 18:47:14 -04007540 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007541 * @phba: Pointer to HBA context object.
7542 *
7543 * This function is called from worker thread when a mailbox command times out.
7544 * The caller is not required to hold any locks. This function will reset the
7545 * HBA and recover all the pending commands.
7546 **/
dea31012005-04-17 16:05:31 -05007547void
7548lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7549{
James Smart2e0fef82007-06-17 19:56:36 -05007550 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007551 MAILBOX_t *mb = NULL;
7552
James Smart1dcb58e2007-04-25 09:51:30 -04007553 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007554
James Smarte8d3c3b2013-10-10 12:21:30 -04007555 /* If the mailbox completed, process the completion and return */
7556 if (lpfc_sli4_process_missed_mbox_completions(phba))
7557 return;
7558
James Smarteb016562014-09-03 12:58:06 -04007559 if (pmbox != NULL)
7560 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007561 /* Check the pmbox pointer first. There is a race condition
7562 * between the mbox timeout handler getting executed in the
7563 * worklist and the mailbox actually completing. When this
7564 * race condition occurs, the mbox_active will be NULL.
7565 */
7566 spin_lock_irq(&phba->hbalock);
7567 if (pmbox == NULL) {
7568 lpfc_printf_log(phba, KERN_WARNING,
7569 LOG_MBOX | LOG_SLI,
7570 "0353 Active Mailbox cleared - mailbox timeout "
7571 "exiting\n");
7572 spin_unlock_irq(&phba->hbalock);
7573 return;
7574 }
7575
dea31012005-04-17 16:05:31 -05007576 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007577 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007578 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007579 mb->mbxCommand,
7580 phba->pport->port_state,
7581 phba->sli.sli_flag,
7582 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007583 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007584
James Smart1dcb58e2007-04-25 09:51:30 -04007585 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7586 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007587 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007588 */
James Smart2e0fef82007-06-17 19:56:36 -05007589 spin_lock_irq(&phba->pport->work_port_lock);
7590 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7591 spin_unlock_irq(&phba->pport->work_port_lock);
7592 spin_lock_irq(&phba->hbalock);
7593 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007594 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007595 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007596
James Smartdb55fba2014-04-04 13:52:02 -04007597 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007598
7599 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007600 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007601
7602 /* Reset the HBA device */
7603 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007604}
7605
James Smarte59058c2008-08-24 21:49:00 -04007606/**
James Smart3772a992009-05-22 14:50:54 -04007607 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007608 * @phba: Pointer to HBA context object.
7609 * @pmbox: Pointer to mailbox object.
7610 * @flag: Flag indicating how the mailbox need to be processed.
7611 *
7612 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007613 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7614 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007615 * The mailbox command can be submitted in polling mode, in which case
7616 * this function will wait in a polling loop for the completion of the
7617 * mailbox.
7618 * If the mailbox is submitted in no_wait mode (not polling) the
7619 * function will submit the command and returns immediately without waiting
7620 * for the mailbox completion. The no_wait is supported only when HBA
7621 * is in SLI2/SLI3 mode - interrupts are enabled.
7622 * The SLI interface allows only one mailbox pending at a time. If the
7623 * mailbox is issued in polling mode and there is already a mailbox
7624 * pending, then the function will return an error. If the mailbox is issued
7625 * in NO_WAIT mode and there is a mailbox pending already, the function
7626 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7627 * The sli layer owns the mailbox object until the completion of mailbox
7628 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7629 * return codes the caller owns the mailbox command after the return of
7630 * the function.
7631 **/
James Smart3772a992009-05-22 14:50:54 -04007632static int
7633lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7634 uint32_t flag)
dea31012005-04-17 16:05:31 -05007635{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007636 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007637 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007638 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007639 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007640 int i;
James Smart09372822008-01-11 01:52:54 -05007641 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007642 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007643 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007644 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007645 int processing_queue = 0;
7646
7647 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7648 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007649 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007650 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007651 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7652 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7653 return MBX_SUCCESS;
7654 }
James Smart58da1ff2008-04-07 10:15:56 -04007655 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007656 pmbox = lpfc_mbox_get(phba);
7657 if (!pmbox) {
7658 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7659 return MBX_SUCCESS;
7660 }
7661 }
dea31012005-04-17 16:05:31 -05007662
James Smarted957682007-06-17 19:56:37 -05007663 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007664 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007665 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007666 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007667 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007668 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007669 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007670 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007671 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007672 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007673 }
7674 }
7675
Linas Vepstas8d63f372007-02-14 14:28:36 -06007676 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007677 if (unlikely(pci_channel_offline(phba->pcidev))) {
7678 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7679 goto out_not_finished;
7680 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007681
James Smarta257bf92009-04-06 18:48:10 -04007682 /* If HBA has a deferred error attention, fail the iocb. */
7683 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7684 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7685 goto out_not_finished;
7686 }
7687
dea31012005-04-17 16:05:31 -05007688 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007689
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007690 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007691 status = MBX_SUCCESS;
7692
James Smart2e0fef82007-06-17 19:56:36 -05007693 if (phba->link_state == LPFC_HBA_ERROR) {
7694 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007695
7696 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007697 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7698 "(%d):0311 Mailbox command x%x cannot "
7699 "issue Data: x%x x%x\n",
7700 pmbox->vport ? pmbox->vport->vpi : 0,
7701 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007702 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007703 }
7704
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007705 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007706 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7707 !(hc_copy & HC_MBINT_ENA)) {
7708 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7709 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007710 "(%d):2528 Mailbox command x%x cannot "
7711 "issue Data: x%x x%x\n",
7712 pmbox->vport ? pmbox->vport->vpi : 0,
7713 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007714 goto out_not_finished;
7715 }
James Smart92908312006-03-07 15:04:13 -05007716 }
7717
dea31012005-04-17 16:05:31 -05007718 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7719 /* Polling for a mbox command when another one is already active
7720 * is not allowed in SLI. Also, the driver must have established
7721 * SLI2 mode to queue and process multiple mbox commands.
7722 */
7723
7724 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007725 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007726
7727 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007728 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7729 "(%d):2529 Mailbox command x%x "
7730 "cannot issue Data: x%x x%x\n",
7731 pmbox->vport ? pmbox->vport->vpi : 0,
7732 pmbox->u.mb.mbxCommand,
7733 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007734 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007735 }
7736
James Smart3772a992009-05-22 14:50:54 -04007737 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007738 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007739 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007740 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7741 "(%d):2530 Mailbox command x%x "
7742 "cannot issue Data: x%x x%x\n",
7743 pmbox->vport ? pmbox->vport->vpi : 0,
7744 pmbox->u.mb.mbxCommand,
7745 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007746 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007747 }
7748
dea31012005-04-17 16:05:31 -05007749 /* Another mailbox command is still being processed, queue this
7750 * command to be processed later.
7751 */
7752 lpfc_mbox_put(phba, pmbox);
7753
7754 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007755 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007756 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007757 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007758 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07007759 mbx->mbxCommand,
7760 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007761 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007762
7763 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007764 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007765
James Smart858c9f62007-06-17 19:56:39 -05007766 if (pmbox->vport) {
7767 lpfc_debugfs_disc_trc(pmbox->vport,
7768 LPFC_DISC_TRC_MBOX_VPORT,
7769 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007770 (uint32_t)mbx->mbxCommand,
7771 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007772 }
7773 else {
7774 lpfc_debugfs_disc_trc(phba->pport,
7775 LPFC_DISC_TRC_MBOX,
7776 "MBOX Bsy: 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
James Smart2e0fef82007-06-17 19:56:36 -05007781 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007782 }
7783
dea31012005-04-17 16:05:31 -05007784 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7785
7786 /* If we are not polling, we MUST be in SLI2 mode */
7787 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007788 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007789 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007790 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007791 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007792 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007793 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7794 "(%d):2531 Mailbox command x%x "
7795 "cannot issue Data: x%x x%x\n",
7796 pmbox->vport ? pmbox->vport->vpi : 0,
7797 pmbox->u.mb.mbxCommand,
7798 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007799 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007800 }
7801 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007802 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7803 1000);
7804 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007805 }
7806
7807 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007808 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007809 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007810 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007811 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07007812 mbx->mbxCommand,
7813 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007814 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007815
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007816 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007817 if (pmbox->vport) {
7818 lpfc_debugfs_disc_trc(pmbox->vport,
7819 LPFC_DISC_TRC_MBOX_VPORT,
7820 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007821 (uint32_t)mbx->mbxCommand,
7822 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007823 }
7824 else {
7825 lpfc_debugfs_disc_trc(phba->pport,
7826 LPFC_DISC_TRC_MBOX,
7827 "MBOX Send: 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 }
7832
dea31012005-04-17 16:05:31 -05007833 psli->slistat.mbox_cmd++;
7834 evtctr = psli->slistat.mbox_event;
7835
7836 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007837 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007838
James Smart3772a992009-05-22 14:50:54 -04007839 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007840 /* Populate mbox extension offset word. */
7841 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007842 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007843 = (uint8_t *)phba->mbox_ext
7844 - (uint8_t *)phba->mbox;
7845 }
7846
7847 /* Copy the mailbox extension data */
7848 if (pmbox->in_ext_byte_len && pmbox->context2) {
7849 lpfc_sli_pcimem_bcopy(pmbox->context2,
7850 (uint8_t *)phba->mbox_ext,
7851 pmbox->in_ext_byte_len);
7852 }
7853 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007854 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007855 } else {
James Smart7a470272010-03-15 11:25:20 -04007856 /* Populate mbox extension offset word. */
7857 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007858 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007859 = MAILBOX_HBA_EXT_OFFSET;
7860
7861 /* Copy the mailbox extension data */
James Smart895427b2017-02-12 13:52:30 -08007862 if (pmbox->in_ext_byte_len && pmbox->context2)
James Smart7a470272010-03-15 11:25:20 -04007863 lpfc_memcpy_to_slim(phba->MBslimaddr +
7864 MAILBOX_HBA_EXT_OFFSET,
7865 pmbox->context2, pmbox->in_ext_byte_len);
7866
James Smart895427b2017-02-12 13:52:30 -08007867 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007868 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08007869 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7870 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007871
7872 /* First copy mbox command data to HBA SLIM, skip past first
7873 word */
7874 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007875 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007876 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7877
7878 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007879 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007880 to_slim = phba->MBslimaddr;
7881 writel(ldata, to_slim);
7882 readl(to_slim); /* flush */
7883
James Smart895427b2017-02-12 13:52:30 -08007884 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007885 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007886 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007887 }
7888
7889 wmb();
dea31012005-04-17 16:05:31 -05007890
7891 switch (flag) {
7892 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007893 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007894 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007895 /* Interrupt board to do it */
7896 writel(CA_MBATT, phba->CAregaddr);
7897 readl(phba->CAregaddr); /* flush */
7898 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007899 break;
7900
7901 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007902 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007903 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007904 /* Interrupt board to do it */
7905 writel(CA_MBATT, phba->CAregaddr);
7906 readl(phba->CAregaddr); /* flush */
7907
James Smart3772a992009-05-22 14:50:54 -04007908 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007909 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007910 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007911 word0 = le32_to_cpu(word0);
7912 } else {
7913 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007914 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7915 spin_unlock_irqrestore(&phba->hbalock,
7916 drvr_flag);
7917 goto out_not_finished;
7918 }
dea31012005-04-17 16:05:31 -05007919 }
7920
7921 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007922 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7923 spin_unlock_irqrestore(&phba->hbalock,
7924 drvr_flag);
7925 goto out_not_finished;
7926 }
James Smarta183a152011-10-10 21:32:43 -04007927 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7928 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007929 i = 0;
dea31012005-04-17 16:05:31 -05007930 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007931 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7932 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007933 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007934 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007935 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007936 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007937 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007938 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007939 }
7940
7941 /* Check if we took a mbox interrupt while we were
7942 polling */
7943 if (((word0 & OWN_CHIP) != OWN_CHIP)
7944 && (evtctr != psli->slistat.mbox_event))
7945 break;
7946
James Smart09372822008-01-11 01:52:54 -05007947 if (i++ > 10) {
7948 spin_unlock_irqrestore(&phba->hbalock,
7949 drvr_flag);
7950 msleep(1);
7951 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7952 }
dea31012005-04-17 16:05:31 -05007953
James Smart3772a992009-05-22 14:50:54 -04007954 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007955 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007956 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007957 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007958 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007959 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007960 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007961 /* Check real SLIM for any errors */
7962 slimword0 = readl(phba->MBslimaddr);
7963 slimmb = (MAILBOX_t *) & slimword0;
7964 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7965 && slimmb->mbxStatus) {
7966 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007967 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007968 word0 = slimword0;
7969 }
7970 }
7971 } else {
7972 /* First copy command data */
7973 word0 = readl(phba->MBslimaddr);
7974 }
7975 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007976 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7977 spin_unlock_irqrestore(&phba->hbalock,
7978 drvr_flag);
7979 goto out_not_finished;
7980 }
dea31012005-04-17 16:05:31 -05007981 }
7982
James Smart3772a992009-05-22 14:50:54 -04007983 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007984 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08007985 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7986 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007987 /* Copy the mailbox extension data */
7988 if (pmbox->out_ext_byte_len && pmbox->context2) {
7989 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7990 pmbox->context2,
7991 pmbox->out_ext_byte_len);
7992 }
dea31012005-04-17 16:05:31 -05007993 } else {
7994 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007995 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08007996 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007997 /* Copy the mailbox extension data */
7998 if (pmbox->out_ext_byte_len && pmbox->context2) {
7999 lpfc_memcpy_from_slim(pmbox->context2,
8000 phba->MBslimaddr +
8001 MAILBOX_HBA_EXT_OFFSET,
8002 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05008003 }
8004 }
8005
8006 writel(HA_MBATT, phba->HAregaddr);
8007 readl(phba->HAregaddr); /* flush */
8008
8009 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008010 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05008011 }
8012
James Smart2e0fef82007-06-17 19:56:36 -05008013 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8014 return status;
James Smart58da1ff2008-04-07 10:15:56 -04008015
8016out_not_finished:
8017 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04008018 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04008019 lpfc_mbox_cmpl_put(phba, pmbox);
8020 }
8021 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008022}
8023
James Smarte59058c2008-08-24 21:49:00 -04008024/**
James Smartf1126682009-06-10 17:22:44 -04008025 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8026 * @phba: Pointer to HBA context object.
8027 *
8028 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8029 * the driver internal pending mailbox queue. It will then try to wait out the
8030 * possible outstanding mailbox command before return.
8031 *
8032 * Returns:
8033 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8034 * the outstanding mailbox command timed out.
8035 **/
8036static int
8037lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8038{
8039 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04008040 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04008041 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04008042
8043 /* Mark the asynchronous mailbox command posting as blocked */
8044 spin_lock_irq(&phba->hbalock);
8045 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04008046 /* Determine how long we might wait for the active mailbox
8047 * command to be gracefully completed by firmware.
8048 */
James Smarta183a152011-10-10 21:32:43 -04008049 if (phba->sli.mbox_active)
8050 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8051 phba->sli.mbox_active) *
8052 1000) + jiffies;
8053 spin_unlock_irq(&phba->hbalock);
8054
James Smarte8d3c3b2013-10-10 12:21:30 -04008055 /* Make sure the mailbox is really active */
8056 if (timeout)
8057 lpfc_sli4_process_missed_mbox_completions(phba);
8058
James Smartf1126682009-06-10 17:22:44 -04008059 /* Wait for the outstnading mailbox command to complete */
8060 while (phba->sli.mbox_active) {
8061 /* Check active mailbox complete status every 2ms */
8062 msleep(2);
8063 if (time_after(jiffies, timeout)) {
8064 /* Timeout, marked the outstanding cmd not complete */
8065 rc = 1;
8066 break;
8067 }
8068 }
8069
8070 /* Can not cleanly block async mailbox command, fails it */
8071 if (rc) {
8072 spin_lock_irq(&phba->hbalock);
8073 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8074 spin_unlock_irq(&phba->hbalock);
8075 }
8076 return rc;
8077}
8078
8079/**
8080 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8081 * @phba: Pointer to HBA context object.
8082 *
8083 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8084 * commands from the driver internal pending mailbox queue. It makes sure
8085 * that there is no outstanding mailbox command before resuming posting
8086 * asynchronous mailbox commands. If, for any reason, there is outstanding
8087 * mailbox command, it will try to wait it out before resuming asynchronous
8088 * mailbox command posting.
8089 **/
8090static void
8091lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8092{
8093 struct lpfc_sli *psli = &phba->sli;
8094
8095 spin_lock_irq(&phba->hbalock);
8096 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8097 /* Asynchronous mailbox posting is not blocked, do nothing */
8098 spin_unlock_irq(&phba->hbalock);
8099 return;
8100 }
8101
8102 /* Outstanding synchronous mailbox command is guaranteed to be done,
8103 * successful or timeout, after timing-out the outstanding mailbox
8104 * command shall always be removed, so just unblock posting async
8105 * mailbox command and resume
8106 */
8107 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8108 spin_unlock_irq(&phba->hbalock);
8109
8110 /* wake up worker thread to post asynchronlous mailbox command */
8111 lpfc_worker_wake_up(phba);
8112}
8113
8114/**
James Smart2d843ed2012-09-29 11:29:06 -04008115 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8116 * @phba: Pointer to HBA context object.
8117 * @mboxq: Pointer to mailbox object.
8118 *
8119 * The function waits for the bootstrap mailbox register ready bit from
8120 * port for twice the regular mailbox command timeout value.
8121 *
8122 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8123 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8124 **/
8125static int
8126lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8127{
8128 uint32_t db_ready;
8129 unsigned long timeout;
8130 struct lpfc_register bmbx_reg;
8131
8132 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8133 * 1000) + jiffies;
8134
8135 do {
8136 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8137 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8138 if (!db_ready)
8139 msleep(2);
8140
8141 if (time_after(jiffies, timeout))
8142 return MBXERR_ERROR;
8143 } while (!db_ready);
8144
8145 return 0;
8146}
8147
8148/**
James Smartda0436e2009-05-22 14:51:39 -04008149 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8150 * @phba: Pointer to HBA context object.
8151 * @mboxq: Pointer to mailbox object.
8152 *
8153 * The function posts a mailbox to the port. The mailbox is expected
8154 * to be comletely filled in and ready for the port to operate on it.
8155 * This routine executes a synchronous completion operation on the
8156 * mailbox by polling for its completion.
8157 *
8158 * The caller must not be holding any locks when calling this routine.
8159 *
8160 * Returns:
8161 * MBX_SUCCESS - mailbox posted successfully
8162 * Any of the MBX error values.
8163 **/
8164static int
8165lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8166{
8167 int rc = MBX_SUCCESS;
8168 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04008169 uint32_t mcqe_status;
8170 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04008171 struct lpfc_sli *psli = &phba->sli;
8172 struct lpfc_mqe *mb = &mboxq->u.mqe;
8173 struct lpfc_bmbx_create *mbox_rgn;
8174 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04008175
8176 /*
8177 * Only one mailbox can be active to the bootstrap mailbox region
8178 * at a time and there is no queueing provided.
8179 */
8180 spin_lock_irqsave(&phba->hbalock, iflag);
8181 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8182 spin_unlock_irqrestore(&phba->hbalock, iflag);
8183 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008184 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008185 "cannot issue Data: x%x x%x\n",
8186 mboxq->vport ? mboxq->vport->vpi : 0,
8187 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008188 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8189 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008190 psli->sli_flag, MBX_POLL);
8191 return MBXERR_ERROR;
8192 }
8193 /* The server grabs the token and owns it until release */
8194 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8195 phba->sli.mbox_active = mboxq;
8196 spin_unlock_irqrestore(&phba->hbalock, iflag);
8197
James Smart2d843ed2012-09-29 11:29:06 -04008198 /* wait for bootstrap mbox register for readyness */
8199 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8200 if (rc)
8201 goto exit;
8202
James Smartda0436e2009-05-22 14:51:39 -04008203 /*
8204 * Initialize the bootstrap memory region to avoid stale data areas
8205 * in the mailbox post. Then copy the caller's mailbox contents to
8206 * the bmbx mailbox region.
8207 */
8208 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8209 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
James Smart48f8fdb2018-05-04 20:37:51 -07008210 lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8211 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008212
8213 /* Post the high mailbox dma address to the port and wait for ready. */
8214 dma_address = &phba->sli4_hba.bmbx.dma_address;
8215 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8216
James Smart2d843ed2012-09-29 11:29:06 -04008217 /* wait for bootstrap mbox register for hi-address write done */
8218 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8219 if (rc)
8220 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008221
8222 /* Post the low mailbox dma address to the port. */
8223 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008224
James Smart2d843ed2012-09-29 11:29:06 -04008225 /* wait for bootstrap mbox register for low address write done */
8226 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8227 if (rc)
8228 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008229
8230 /*
8231 * Read the CQ to ensure the mailbox has completed.
8232 * If so, update the mailbox status so that the upper layers
8233 * can complete the request normally.
8234 */
James Smart48f8fdb2018-05-04 20:37:51 -07008235 lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8236 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008237 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
James Smart48f8fdb2018-05-04 20:37:51 -07008238 lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8239 sizeof(struct lpfc_mcqe));
James Smartda0436e2009-05-22 14:51:39 -04008240 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008241 /*
8242 * When the CQE status indicates a failure and the mailbox status
8243 * indicates success then copy the CQE status into the mailbox status
8244 * (and prefix it with x4000).
8245 */
James Smartda0436e2009-05-22 14:51:39 -04008246 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008247 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8248 bf_set(lpfc_mqe_status, mb,
8249 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008250 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008251 } else
8252 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008253
8254 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008255 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008256 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8257 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008258 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8259 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8260 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008261 bf_get(lpfc_mqe_status, mb),
8262 mb->un.mb_words[0], mb->un.mb_words[1],
8263 mb->un.mb_words[2], mb->un.mb_words[3],
8264 mb->un.mb_words[4], mb->un.mb_words[5],
8265 mb->un.mb_words[6], mb->un.mb_words[7],
8266 mb->un.mb_words[8], mb->un.mb_words[9],
8267 mb->un.mb_words[10], mb->un.mb_words[11],
8268 mb->un.mb_words[12], mboxq->mcqe.word0,
8269 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8270 mboxq->mcqe.trailer);
8271exit:
8272 /* We are holding the token, no needed for lock when release */
8273 spin_lock_irqsave(&phba->hbalock, iflag);
8274 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8275 phba->sli.mbox_active = NULL;
8276 spin_unlock_irqrestore(&phba->hbalock, iflag);
8277 return rc;
8278}
8279
8280/**
8281 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8282 * @phba: Pointer to HBA context object.
8283 * @pmbox: Pointer to mailbox object.
8284 * @flag: Flag indicating how the mailbox need to be processed.
8285 *
8286 * This function is called by discovery code and HBA management code to submit
8287 * a mailbox command to firmware with SLI-4 interface spec.
8288 *
8289 * Return codes the caller owns the mailbox command after the return of the
8290 * function.
8291 **/
8292static int
8293lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8294 uint32_t flag)
8295{
8296 struct lpfc_sli *psli = &phba->sli;
8297 unsigned long iflags;
8298 int rc;
8299
James Smartb76f2dc2011-07-22 18:37:42 -04008300 /* dump from issue mailbox command if setup */
8301 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8302
James Smart8fa38512009-07-19 10:01:03 -04008303 rc = lpfc_mbox_dev_check(phba);
8304 if (unlikely(rc)) {
8305 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008306 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008307 "cannot issue Data: x%x x%x\n",
8308 mboxq->vport ? mboxq->vport->vpi : 0,
8309 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008310 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8311 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008312 psli->sli_flag, flag);
8313 goto out_not_finished;
8314 }
8315
James Smartda0436e2009-05-22 14:51:39 -04008316 /* Detect polling mode and jump to a handler */
8317 if (!phba->sli4_hba.intr_enable) {
8318 if (flag == MBX_POLL)
8319 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8320 else
8321 rc = -EIO;
8322 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008323 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008324 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008325 "(x%x/x%x) failure: "
8326 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8327 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008328 mboxq->vport ? mboxq->vport->vpi : 0,
8329 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008330 lpfc_sli_config_mbox_subsys_get(phba,
8331 mboxq),
8332 lpfc_sli_config_mbox_opcode_get(phba,
8333 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008334 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8335 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8336 bf_get(lpfc_mcqe_ext_status,
8337 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008338 psli->sli_flag, flag);
8339 return rc;
8340 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008341 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8342 "(%d):2542 Try to issue mailbox command "
James Smart7365f6f2018-02-22 08:18:46 -08008343 "x%x (x%x/x%x) synchronously ahead of async "
James Smartf1126682009-06-10 17:22:44 -04008344 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008345 mboxq->vport ? mboxq->vport->vpi : 0,
8346 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008347 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8348 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008349 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008350 /* Try to block the asynchronous mailbox posting */
8351 rc = lpfc_sli4_async_mbox_block(phba);
8352 if (!rc) {
8353 /* Successfully blocked, now issue sync mbox cmd */
8354 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8355 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008356 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008357 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008358 "(%d):2597 Sync Mailbox command "
8359 "x%x (x%x/x%x) failure: "
8360 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8361 "Data: x%x x%x\n,",
8362 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008363 mboxq->u.mb.mbxCommand,
8364 lpfc_sli_config_mbox_subsys_get(phba,
8365 mboxq),
8366 lpfc_sli_config_mbox_opcode_get(phba,
8367 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008368 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8369 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8370 bf_get(lpfc_mcqe_ext_status,
8371 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008372 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008373 /* Unblock the async mailbox posting afterward */
8374 lpfc_sli4_async_mbox_unblock(phba);
8375 }
8376 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008377 }
8378
8379 /* Now, interrupt mode asynchrous mailbox command */
8380 rc = lpfc_mbox_cmd_check(phba, mboxq);
8381 if (rc) {
8382 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008383 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008384 "cannot issue Data: x%x x%x\n",
8385 mboxq->vport ? mboxq->vport->vpi : 0,
8386 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008387 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8388 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008389 psli->sli_flag, flag);
8390 goto out_not_finished;
8391 }
James Smartda0436e2009-05-22 14:51:39 -04008392
8393 /* Put the mailbox command to the driver internal FIFO */
8394 psli->slistat.mbox_busy++;
8395 spin_lock_irqsave(&phba->hbalock, iflags);
8396 lpfc_mbox_put(phba, mboxq);
8397 spin_unlock_irqrestore(&phba->hbalock, iflags);
8398 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8399 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008400 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008401 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8402 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008403 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8404 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008405 phba->pport->port_state,
8406 psli->sli_flag, MBX_NOWAIT);
8407 /* Wake up worker thread to transport mailbox command from head */
8408 lpfc_worker_wake_up(phba);
8409
8410 return MBX_BUSY;
8411
8412out_not_finished:
8413 return MBX_NOT_FINISHED;
8414}
8415
8416/**
8417 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8418 * @phba: Pointer to HBA context object.
8419 *
8420 * This function is called by worker thread to send a mailbox command to
8421 * SLI4 HBA firmware.
8422 *
8423 **/
8424int
8425lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8426{
8427 struct lpfc_sli *psli = &phba->sli;
8428 LPFC_MBOXQ_t *mboxq;
8429 int rc = MBX_SUCCESS;
8430 unsigned long iflags;
8431 struct lpfc_mqe *mqe;
8432 uint32_t mbx_cmnd;
8433
8434 /* Check interrupt mode before post async mailbox command */
8435 if (unlikely(!phba->sli4_hba.intr_enable))
8436 return MBX_NOT_FINISHED;
8437
8438 /* Check for mailbox command service token */
8439 spin_lock_irqsave(&phba->hbalock, iflags);
8440 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8441 spin_unlock_irqrestore(&phba->hbalock, iflags);
8442 return MBX_NOT_FINISHED;
8443 }
8444 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8445 spin_unlock_irqrestore(&phba->hbalock, iflags);
8446 return MBX_NOT_FINISHED;
8447 }
8448 if (unlikely(phba->sli.mbox_active)) {
8449 spin_unlock_irqrestore(&phba->hbalock, iflags);
8450 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8451 "0384 There is pending active mailbox cmd\n");
8452 return MBX_NOT_FINISHED;
8453 }
8454 /* Take the mailbox command service token */
8455 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8456
8457 /* Get the next mailbox command from head of queue */
8458 mboxq = lpfc_mbox_get(phba);
8459
8460 /* If no more mailbox command waiting for post, we're done */
8461 if (!mboxq) {
8462 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8463 spin_unlock_irqrestore(&phba->hbalock, iflags);
8464 return MBX_SUCCESS;
8465 }
8466 phba->sli.mbox_active = mboxq;
8467 spin_unlock_irqrestore(&phba->hbalock, iflags);
8468
8469 /* Check device readiness for posting mailbox command */
8470 rc = lpfc_mbox_dev_check(phba);
8471 if (unlikely(rc))
8472 /* Driver clean routine will clean up pending mailbox */
8473 goto out_not_finished;
8474
8475 /* Prepare the mbox command to be posted */
8476 mqe = &mboxq->u.mqe;
8477 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8478
8479 /* Start timer for the mbox_tmo and log some mailbox post messages */
8480 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008481 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008482
8483 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008484 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008485 "x%x x%x\n",
8486 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008487 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8488 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008489 phba->pport->port_state, psli->sli_flag);
8490
8491 if (mbx_cmnd != MBX_HEARTBEAT) {
8492 if (mboxq->vport) {
8493 lpfc_debugfs_disc_trc(mboxq->vport,
8494 LPFC_DISC_TRC_MBOX_VPORT,
8495 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8496 mbx_cmnd, mqe->un.mb_words[0],
8497 mqe->un.mb_words[1]);
8498 } else {
8499 lpfc_debugfs_disc_trc(phba->pport,
8500 LPFC_DISC_TRC_MBOX,
8501 "MBOX Send: cmd:x%x mb:x%x x%x",
8502 mbx_cmnd, mqe->un.mb_words[0],
8503 mqe->un.mb_words[1]);
8504 }
8505 }
8506 psli->slistat.mbox_cmd++;
8507
8508 /* Post the mailbox command to the port */
8509 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8510 if (rc != MBX_SUCCESS) {
8511 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008512 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008513 "cannot issue Data: x%x x%x\n",
8514 mboxq->vport ? mboxq->vport->vpi : 0,
8515 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008516 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8517 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008518 psli->sli_flag, MBX_NOWAIT);
8519 goto out_not_finished;
8520 }
8521
8522 return rc;
8523
8524out_not_finished:
8525 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008526 if (phba->sli.mbox_active) {
8527 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8528 __lpfc_mbox_cmpl_put(phba, mboxq);
8529 /* Release the token */
8530 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8531 phba->sli.mbox_active = NULL;
8532 }
James Smartda0436e2009-05-22 14:51:39 -04008533 spin_unlock_irqrestore(&phba->hbalock, iflags);
8534
8535 return MBX_NOT_FINISHED;
8536}
8537
8538/**
8539 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8540 * @phba: Pointer to HBA context object.
8541 * @pmbox: Pointer to mailbox object.
8542 * @flag: Flag indicating how the mailbox need to be processed.
8543 *
8544 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8545 * the API jump table function pointer from the lpfc_hba struct.
8546 *
8547 * Return codes the caller owns the mailbox command after the return of the
8548 * function.
8549 **/
8550int
8551lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8552{
8553 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8554}
8555
8556/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008557 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008558 * @phba: The hba struct for which this call is being executed.
8559 * @dev_grp: The HBA PCI-Device group number.
8560 *
8561 * This routine sets up the mbox interface API function jump table in @phba
8562 * struct.
8563 * Returns: 0 - success, -ENODEV - failure.
8564 **/
8565int
8566lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8567{
8568
8569 switch (dev_grp) {
8570 case LPFC_PCI_DEV_LP:
8571 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8572 phba->lpfc_sli_handle_slow_ring_event =
8573 lpfc_sli_handle_slow_ring_event_s3;
8574 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8575 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8576 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8577 break;
8578 case LPFC_PCI_DEV_OC:
8579 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8580 phba->lpfc_sli_handle_slow_ring_event =
8581 lpfc_sli_handle_slow_ring_event_s4;
8582 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8583 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8584 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8585 break;
8586 default:
8587 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8588 "1420 Invalid HBA PCI-device group: 0x%x\n",
8589 dev_grp);
8590 return -ENODEV;
8591 break;
8592 }
8593 return 0;
8594}
8595
8596/**
James Smart3621a712009-04-06 18:47:14 -04008597 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008598 * @phba: Pointer to HBA context object.
8599 * @pring: Pointer to driver SLI ring object.
8600 * @piocb: Pointer to address of newly added command iocb.
8601 *
8602 * This function is called with hbalock held to add a command
8603 * iocb to the txq when SLI layer cannot submit the command iocb
8604 * to the ring.
8605 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008606void
James Smart92d7f7b2007-06-17 19:56:38 -05008607__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008608 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008609{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008610 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008611 /* Insert the caller's iocb in the txq tail for later processing. */
8612 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008613}
8614
James Smarte59058c2008-08-24 21:49:00 -04008615/**
James Smart3621a712009-04-06 18:47:14 -04008616 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008617 * @phba: Pointer to HBA context object.
8618 * @pring: Pointer to driver SLI ring object.
8619 * @piocb: Pointer to address of newly added command iocb.
8620 *
8621 * This function is called with hbalock held before a new
8622 * iocb is submitted to the firmware. This function checks
8623 * txq to flush the iocbs in txq to Firmware before
8624 * submitting new iocbs to the Firmware.
8625 * If there are iocbs in the txq which need to be submitted
8626 * to firmware, lpfc_sli_next_iocb returns the first element
8627 * of the txq after dequeuing it from txq.
8628 * If there is no iocb in the txq then the function will return
8629 * *piocb and *piocb is set to NULL. Caller needs to check
8630 * *piocb to find if there are more commands in the txq.
8631 **/
dea31012005-04-17 16:05:31 -05008632static struct lpfc_iocbq *
8633lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008634 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008635{
8636 struct lpfc_iocbq * nextiocb;
8637
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008638 lockdep_assert_held(&phba->hbalock);
8639
dea31012005-04-17 16:05:31 -05008640 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8641 if (!nextiocb) {
8642 nextiocb = *piocb;
8643 *piocb = NULL;
8644 }
8645
8646 return nextiocb;
8647}
8648
James Smarte59058c2008-08-24 21:49:00 -04008649/**
James Smart3772a992009-05-22 14:50:54 -04008650 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008651 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008652 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008653 * @piocb: Pointer to command iocb.
8654 * @flag: Flag indicating if this command can be put into txq.
8655 *
James Smart3772a992009-05-22 14:50:54 -04008656 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8657 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8658 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8659 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8660 * this function allows only iocbs for posting buffers. This function finds
8661 * next available slot in the command ring and posts the command to the
8662 * available slot and writes the port attention register to request HBA start
8663 * processing new iocb. If there is no slot available in the ring and
8664 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8665 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008666 *
James Smart3772a992009-05-22 14:50:54 -04008667 * This function is called with hbalock held. The function will return success
8668 * after it successfully submit the iocb to firmware or after adding to the
8669 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008670 **/
James Smart98c9ea52007-10-27 13:37:33 -04008671static int
James Smart3772a992009-05-22 14:50:54 -04008672__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008673 struct lpfc_iocbq *piocb, uint32_t flag)
8674{
8675 struct lpfc_iocbq *nextiocb;
8676 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08008677 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05008678
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008679 lockdep_assert_held(&phba->hbalock);
8680
James Smart92d7f7b2007-06-17 19:56:38 -05008681 if (piocb->iocb_cmpl && (!piocb->vport) &&
8682 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8683 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8684 lpfc_printf_log(phba, KERN_ERR,
8685 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008686 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008687 piocb->iocb.ulpCommand);
8688 dump_stack();
8689 return IOCB_ERROR;
8690 }
8691
8692
Linas Vepstas8d63f372007-02-14 14:28:36 -06008693 /* If the PCI channel is in offline state, do not post iocbs. */
8694 if (unlikely(pci_channel_offline(phba->pcidev)))
8695 return IOCB_ERROR;
8696
James Smarta257bf92009-04-06 18:48:10 -04008697 /* If HBA has a deferred error attention, fail the iocb. */
8698 if (unlikely(phba->hba_flag & DEFER_ERATT))
8699 return IOCB_ERROR;
8700
dea31012005-04-17 16:05:31 -05008701 /*
8702 * We should never get an IOCB if we are in a < LINK_DOWN state
8703 */
James Smart2e0fef82007-06-17 19:56:36 -05008704 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008705 return IOCB_ERROR;
8706
8707 /*
8708 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008709 * outstanding event.
dea31012005-04-17 16:05:31 -05008710 */
James Smart0b727fe2007-10-27 13:37:25 -04008711 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008712 goto iocb_busy;
8713
James Smart2e0fef82007-06-17 19:56:36 -05008714 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008715 /*
James Smart2680eea2007-04-25 09:52:55 -04008716 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008717 * can be issued if the link is not up.
8718 */
8719 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008720 case CMD_GEN_REQUEST64_CR:
8721 case CMD_GEN_REQUEST64_CX:
8722 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8723 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008724 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008725 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8726 MENLO_TRANSPORT_TYPE))
8727
8728 goto iocb_busy;
8729 break;
dea31012005-04-17 16:05:31 -05008730 case CMD_QUE_RING_BUF_CN:
8731 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008732 /*
8733 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8734 * completion, iocb_cmpl MUST be 0.
8735 */
8736 if (piocb->iocb_cmpl)
8737 piocb->iocb_cmpl = NULL;
8738 /*FALLTHROUGH*/
8739 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008740 case CMD_CLOSE_XRI_CN:
8741 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008742 break;
8743 default:
8744 goto iocb_busy;
8745 }
8746
8747 /*
8748 * For FCP commands, we must be in a state where we can process link
8749 * attention events.
8750 */
James Smart895427b2017-02-12 13:52:30 -08008751 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05008752 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008753 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008754 }
dea31012005-04-17 16:05:31 -05008755
dea31012005-04-17 16:05:31 -05008756 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8757 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8758 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8759
8760 if (iocb)
8761 lpfc_sli_update_ring(phba, pring);
8762 else
8763 lpfc_sli_update_full_ring(phba, pring);
8764
8765 if (!piocb)
8766 return IOCB_SUCCESS;
8767
8768 goto out_busy;
8769
8770 iocb_busy:
8771 pring->stats.iocb_cmd_delay++;
8772
8773 out_busy:
8774
8775 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008776 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008777 return IOCB_SUCCESS;
8778 }
8779
8780 return IOCB_BUSY;
8781}
8782
James Smart3772a992009-05-22 14:50:54 -04008783/**
James Smart4f774512009-05-22 14:52:35 -04008784 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8785 * @phba: Pointer to HBA context object.
8786 * @piocb: Pointer to command iocb.
8787 * @sglq: Pointer to the scatter gather queue object.
8788 *
8789 * This routine converts the bpl or bde that is in the IOCB
8790 * to a sgl list for the sli4 hardware. The physical address
8791 * of the bpl/bde is converted back to a virtual address.
8792 * If the IOCB contains a BPL then the list of BDE's is
8793 * converted to sli4_sge's. If the IOCB contains a single
8794 * BDE then it is converted to a single sli_sge.
8795 * The IOCB is still in cpu endianess so the contents of
8796 * the bpl can be used without byte swapping.
8797 *
8798 * Returns valid XRI = Success, NO_XRI = Failure.
8799**/
8800static uint16_t
8801lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8802 struct lpfc_sglq *sglq)
8803{
8804 uint16_t xritag = NO_XRI;
8805 struct ulp_bde64 *bpl = NULL;
8806 struct ulp_bde64 bde;
8807 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008808 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008809 IOCB_t *icmd;
8810 int numBdes = 0;
8811 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008812 uint32_t offset = 0; /* accumulated offset in the sg request list */
8813 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008814
8815 if (!piocbq || !sglq)
8816 return xritag;
8817
8818 sgl = (struct sli4_sge *)sglq->sgl;
8819 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008820 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8821 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008822 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8823 numBdes = icmd->un.genreq64.bdl.bdeSize /
8824 sizeof(struct ulp_bde64);
8825 /* The addrHigh and addrLow fields within the IOCB
8826 * have not been byteswapped yet so there is no
8827 * need to swap them back.
8828 */
James Smart1b511972011-12-13 13:23:09 -05008829 if (piocbq->context3)
8830 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8831 else
8832 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008833
James Smart1b511972011-12-13 13:23:09 -05008834 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008835 if (!bpl)
8836 return xritag;
8837
8838 for (i = 0; i < numBdes; i++) {
8839 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008840 sgl->addr_hi = bpl->addrHigh;
8841 sgl->addr_lo = bpl->addrLow;
8842
James Smart05580562011-05-24 11:40:48 -04008843 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008844 if ((i+1) == numBdes)
8845 bf_set(lpfc_sli4_sge_last, sgl, 1);
8846 else
8847 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008848 /* swap the size field back to the cpu so we
8849 * can assign it to the sgl.
8850 */
8851 bde.tus.w = le32_to_cpu(bpl->tus.w);
8852 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008853 /* The offsets in the sgl need to be accumulated
8854 * separately for the request and reply lists.
8855 * The request is always first, the reply follows.
8856 */
8857 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8858 /* add up the reply sg entries */
8859 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8860 inbound++;
8861 /* first inbound? reset the offset */
8862 if (inbound == 1)
8863 offset = 0;
8864 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008865 bf_set(lpfc_sli4_sge_type, sgl,
8866 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008867 offset += bde.tus.f.bdeSize;
8868 }
James Smart546fc852011-03-11 16:06:29 -05008869 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008870 bpl++;
8871 sgl++;
8872 }
8873 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8874 /* The addrHigh and addrLow fields of the BDE have not
8875 * been byteswapped yet so they need to be swapped
8876 * before putting them in the sgl.
8877 */
8878 sgl->addr_hi =
8879 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8880 sgl->addr_lo =
8881 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008882 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008883 bf_set(lpfc_sli4_sge_last, sgl, 1);
8884 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008885 sgl->sge_len =
8886 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008887 }
8888 return sglq->sli4_xritag;
8889}
8890
8891/**
James Smart4f774512009-05-22 14:52:35 -04008892 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8893 * @phba: Pointer to HBA context object.
8894 * @piocb: Pointer to command iocb.
8895 * @wqe: Pointer to the work queue entry.
8896 *
8897 * This routine converts the iocb command to its Work Queue Entry
8898 * equivalent. The wqe pointer should not have any fields set when
8899 * this routine is called because it will memcpy over them.
8900 * This routine does not set the CQ_ID or the WQEC bits in the
8901 * wqe.
8902 *
8903 * Returns: 0 = Success, IOCB_ERROR = Failure.
8904 **/
8905static int
8906lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
James Smart205e8242018-03-05 12:04:03 -08008907 union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -04008908{
James Smart5ffc2662009-11-18 15:39:44 -05008909 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008910 uint8_t ct = 0;
8911 uint32_t fip;
8912 uint32_t abort_tag;
8913 uint8_t command_type = ELS_COMMAND_NON_FIP;
8914 uint8_t cmnd;
8915 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008916 uint16_t abrt_iotag;
8917 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008918 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008919 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008920 int numBdes, i;
8921 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008922 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008923 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008924 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008925
James Smart45ed1192009-10-02 15:17:02 -04008926 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008927 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008928 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008929 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008930 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008931 command_type = ELS_COMMAND_FIP;
8932 else
8933 command_type = ELS_COMMAND_NON_FIP;
8934
James Smartb5c53952016-03-31 14:12:30 -07008935 if (phba->fcp_embed_io)
8936 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008937 /* Some of the fields are in the right position already */
8938 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartae9e28f2017-05-15 15:20:51 -07008939 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8940 /* The ct field has moved so reset */
8941 wqe->generic.wqe_com.word7 = 0;
8942 wqe->generic.wqe_com.word10 = 0;
8943 }
James Smartb5c53952016-03-31 14:12:30 -07008944
8945 abort_tag = (uint32_t) iocbq->iotag;
8946 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008947 /* words0-2 bpl convert bde */
8948 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008949 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8950 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008951 bpl = (struct ulp_bde64 *)
8952 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8953 if (!bpl)
8954 return IOCB_ERROR;
8955
8956 /* Should already be byte swapped. */
8957 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8958 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8959 /* swap the size field back to the cpu so we
8960 * can assign it to the sgl.
8961 */
8962 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008963 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8964 total_len = 0;
8965 for (i = 0; i < numBdes; i++) {
8966 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8967 total_len += bde.tus.f.bdeSize;
8968 }
James Smart4f774512009-05-22 14:52:35 -04008969 } else
James Smart5ffc2662009-11-18 15:39:44 -05008970 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008971
8972 iocbq->iocb.ulpIoTag = iocbq->iotag;
8973 cmnd = iocbq->iocb.ulpCommand;
8974
8975 switch (iocbq->iocb.ulpCommand) {
8976 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008977 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8978 ndlp = iocbq->context_un.ndlp;
8979 else
8980 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008981 if (!iocbq->iocb.ulpLe) {
8982 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8983 "2007 Only Limited Edition cmd Format"
8984 " supported 0x%x\n",
8985 iocbq->iocb.ulpCommand);
8986 return IOCB_ERROR;
8987 }
James Smartff78d8f2011-12-13 13:21:35 -05008988
James Smart5ffc2662009-11-18 15:39:44 -05008989 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008990 /* Els_reguest64 has a TMO */
8991 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8992 iocbq->iocb.ulpTimeout);
8993 /* Need a VF for word 4 set the vf bit*/
8994 bf_set(els_req64_vf, &wqe->els_req, 0);
8995 /* And a VFID for word 12 */
8996 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008997 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008998 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8999 iocbq->iocb.ulpContext);
9000 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9001 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04009002 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05009003 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05009004 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9005 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05009006 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9007 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05009008 if_type = bf_get(lpfc_sli_intf_if_type,
9009 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009010 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05009011 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05009012 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05009013 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05009014 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05009015 *pcmd == ELS_CMD_PLOGI)) {
9016 bf_set(els_req64_sp, &wqe->els_req, 1);
9017 bf_set(els_req64_sid, &wqe->els_req,
9018 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009019 if ((*pcmd == ELS_CMD_FLOGI) &&
9020 !(phba->fc_topology ==
9021 LPFC_TOPOLOGY_LOOP))
9022 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05009023 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9024 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04009025 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05009026 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05009027 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9028 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9029 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9030 }
James Smartc8685952009-11-18 15:39:16 -05009031 }
James Smart6d368e52011-05-24 11:44:12 -04009032 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9033 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009034 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9035 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9036 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9037 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9038 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9039 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009040 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04009041 break;
James Smart5ffc2662009-11-18 15:39:44 -05009042 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04009043 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9044 iocbq->iocb.un.ulpWord[3]);
9045 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009046 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05009047 /* The entire sequence is transmitted for this IOCB */
9048 xmit_len = total_len;
9049 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05009050 if (phba->link_flag & LS_LOOPBACK_MODE)
9051 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04009052 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04009053 /* word3 iocb=io_tag32 wqe=reserved */
9054 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04009055 /* word4 relative_offset memcpy */
9056 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009057 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9058 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9059 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9060 LPFC_WQE_IOD_WRITE);
9061 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9062 LPFC_WQE_LENLOC_WORD12);
9063 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05009064 wqe->xmit_sequence.xmit_len = xmit_len;
9065 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009066 break;
James Smart4f774512009-05-22 14:52:35 -04009067 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04009068 /* word3 iocb=iotag32 wqe=seq_payload_len */
9069 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009070 /* word4 iocb=rsvd wqe=rsvd */
9071 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9072 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04009073 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009074 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04009075 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9076 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9077 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9078 LPFC_WQE_LENLOC_WORD3);
9079 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04009080 break;
James Smart4f774512009-05-22 14:52:35 -04009081 case CMD_FCP_IWRITE64_CR:
9082 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04009083 /* word3 iocb=iotag wqe=payload_offset_len */
9084 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009085 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9086 xmit_len + sizeof(struct fcp_rsp));
9087 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9088 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009089 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9090 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9091 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9092 iocbq->iocb.ulpFCP2Rcvy);
9093 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9094 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009095 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9096 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9097 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009098 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009099 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009100 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9101 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009102 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9103 if (iocbq->priority) {
9104 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9105 (iocbq->priority << 1));
9106 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009107 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9108 (phba->cfg_XLanePriority << 1));
9109 }
9110 }
James Smartb5c53952016-03-31 14:12:30 -07009111 /* Note, word 10 is already initialized to 0 */
9112
James Smart0bc2b7c2018-02-22 08:18:48 -08009113 /* Don't set PBDE for Perf hints, just fcp_embed_pbde */
9114 if (phba->fcp_embed_pbde)
9115 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9116 else
9117 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9118
James Smartb5c53952016-03-31 14:12:30 -07009119 if (phba->fcp_embed_io) {
9120 struct lpfc_scsi_buf *lpfc_cmd;
9121 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009122 struct fcp_cmnd *fcp_cmnd;
9123 uint32_t *ptr;
9124
9125 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009126
9127 lpfc_cmd = iocbq->context1;
9128 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9129 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9130
9131 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009132 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009133 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009134 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9135 wqe->generic.bde.addrHigh = 0;
9136 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009137
James Smart205e8242018-03-05 12:04:03 -08009138 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9139 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009140
9141 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009142 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009143 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9144 }
James Smart7851fe22011-07-22 18:36:52 -04009145 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04009146 case CMD_FCP_IREAD64_CR:
9147 /* word3 iocb=iotag wqe=payload_offset_len */
9148 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009149 bf_set(payload_offset_len, &wqe->fcp_iread,
9150 xmit_len + sizeof(struct fcp_rsp));
9151 bf_set(cmd_buff_len, &wqe->fcp_iread,
9152 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009153 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9154 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9155 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9156 iocbq->iocb.ulpFCP2Rcvy);
9157 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04009158 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009159 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9160 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9161 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009162 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009163 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009164 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9165 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009166 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9167 if (iocbq->priority) {
9168 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9169 (iocbq->priority << 1));
9170 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009171 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9172 (phba->cfg_XLanePriority << 1));
9173 }
9174 }
James Smartb5c53952016-03-31 14:12:30 -07009175 /* Note, word 10 is already initialized to 0 */
9176
James Smart0bc2b7c2018-02-22 08:18:48 -08009177 /* Don't set PBDE for Perf hints, just fcp_embed_pbde */
9178 if (phba->fcp_embed_pbde)
9179 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9180 else
9181 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9182
James Smartb5c53952016-03-31 14:12:30 -07009183 if (phba->fcp_embed_io) {
9184 struct lpfc_scsi_buf *lpfc_cmd;
9185 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009186 struct fcp_cmnd *fcp_cmnd;
9187 uint32_t *ptr;
9188
9189 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009190
9191 lpfc_cmd = iocbq->context1;
9192 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9193 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9194
9195 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009196 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009197 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009198 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9199 wqe->generic.bde.addrHigh = 0;
9200 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009201
James Smart205e8242018-03-05 12:04:03 -08009202 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9203 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009204
9205 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009206 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009207 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9208 }
James Smart7851fe22011-07-22 18:36:52 -04009209 break;
James Smartf1126682009-06-10 17:22:44 -04009210 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04009211 /* word3 iocb=iotag wqe=payload_offset_len */
9212 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9213 bf_set(payload_offset_len, &wqe->fcp_icmd,
9214 xmit_len + sizeof(struct fcp_rsp));
9215 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9216 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009217 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04009218 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04009219 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009220 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9221 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9222 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9223 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9224 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04009225 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9226 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009227 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9228 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009229 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9230 if (iocbq->priority) {
9231 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9232 (iocbq->priority << 1));
9233 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009234 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9235 (phba->cfg_XLanePriority << 1));
9236 }
9237 }
James Smartb5c53952016-03-31 14:12:30 -07009238 /* Note, word 10 is already initialized to 0 */
9239
9240 if (phba->fcp_embed_io) {
9241 struct lpfc_scsi_buf *lpfc_cmd;
9242 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009243 struct fcp_cmnd *fcp_cmnd;
9244 uint32_t *ptr;
9245
9246 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009247
9248 lpfc_cmd = iocbq->context1;
9249 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9250 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9251
9252 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009253 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009254 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009255 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9256 wqe->generic.bde.addrHigh = 0;
9257 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009258
James Smart205e8242018-03-05 12:04:03 -08009259 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9260 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009261
9262 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009263 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009264 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9265 }
James Smart7851fe22011-07-22 18:36:52 -04009266 break;
James Smart4f774512009-05-22 14:52:35 -04009267 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009268 /* For this command calculate the xmit length of the
9269 * request bde.
9270 */
9271 xmit_len = 0;
9272 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9273 sizeof(struct ulp_bde64);
9274 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009275 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009276 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9277 break;
James Smart63e801c2010-11-20 23:14:19 -05009278 xmit_len += bde.tus.f.bdeSize;
9279 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009280 /* word3 iocb=IO_TAG wqe=request_payload_len */
9281 wqe->gen_req.request_payload_len = xmit_len;
9282 /* word4 iocb=parameter wqe=relative_offset memcpy */
9283 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009284 /* word6 context tag copied in memcpy */
9285 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9286 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9287 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9288 "2015 Invalid CT %x command 0x%x\n",
9289 ct, iocbq->iocb.ulpCommand);
9290 return IOCB_ERROR;
9291 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009292 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9293 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9294 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9295 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9296 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9297 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9298 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9299 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009300 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009301 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009302 break;
James Smart4f774512009-05-22 14:52:35 -04009303 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009304 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009305 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009306 /* word3 iocb=iotag32 wqe=response_payload_len */
9307 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009308 /* word4 */
9309 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009310 /* word5 iocb=rsvd wge=did */
9311 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009312 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9313
9314 if_type = bf_get(lpfc_sli_intf_if_type,
9315 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009316 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -04009317 if (iocbq->vport->fc_flag & FC_PT2PT) {
9318 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9319 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9320 iocbq->vport->fc_myDID);
9321 if (iocbq->vport->fc_myDID == Fabric_DID) {
9322 bf_set(wqe_els_did,
9323 &wqe->xmit_els_rsp.wqe_dest, 0);
9324 }
9325 }
9326 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009327 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9328 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9329 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9330 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009331 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009332 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009333 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009334 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009335 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9336 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9337 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9338 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9339 LPFC_WQE_LENLOC_WORD3);
9340 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009341 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9342 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009343 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9344 iocbq->context2)->virt);
9345 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009346 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9347 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009348 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009349 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9350 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009351 phba->vpi_ids[phba->pport->vpi]);
9352 }
James Smart4f774512009-05-22 14:52:35 -04009353 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009354 break;
James Smart4f774512009-05-22 14:52:35 -04009355 case CMD_CLOSE_XRI_CN:
9356 case CMD_ABORT_XRI_CN:
9357 case CMD_ABORT_XRI_CX:
9358 /* words 0-2 memcpy should be 0 rserved */
9359 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009360 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9361 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9362 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9363 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9364 } else
9365 fip = 0;
9366
9367 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009368 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009369 * The link is down, or the command was ELS_FIP
9370 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009371 * on the wire.
9372 */
9373 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9374 else
9375 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9376 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009377 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9378 wqe->abort_cmd.rsrvd5 = 0;
9379 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009380 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9381 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009382 /*
9383 * The abort handler will send us CMD_ABORT_XRI_CN or
9384 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9385 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009386 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9387 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9388 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9389 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009390 cmnd = CMD_ABORT_XRI_CX;
9391 command_type = OTHER_COMMAND;
9392 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009393 break;
James Smart6669f9b2009-10-02 15:16:45 -04009394 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009395 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009396 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009397 * we re-construct this WQE here based on information in
9398 * iocbq from scratch.
9399 */
9400 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009401 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009402 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009403 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9404 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009405 LPFC_ABTS_UNSOL_INT) {
9406 /* ABTS sent by initiator to CT exchange, the
9407 * RX_ID field will be filled with the newly
9408 * allocated responder XRI.
9409 */
9410 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9411 iocbq->sli4_xritag);
9412 } else {
9413 /* ABTS sent by responder to CT exchange, the
9414 * RX_ID field will be filled with the responder
9415 * RX_ID from ABTS.
9416 */
9417 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009418 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009419 }
James Smart6669f9b2009-10-02 15:16:45 -04009420 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9421 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009422
9423 /* Use CT=VPI */
9424 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9425 ndlp->nlp_DID);
9426 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9427 iocbq->iocb.ulpContext);
9428 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009429 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009430 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009431 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9432 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9433 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009434 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9435 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009436 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9437 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9438 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9439 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9440 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9441 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9442 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9443 }
9444
James Smart7851fe22011-07-22 18:36:52 -04009445 break;
James Smartae9e28f2017-05-15 15:20:51 -07009446 case CMD_SEND_FRAME:
9447 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9448 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9449 return 0;
James Smart4f774512009-05-22 14:52:35 -04009450 case CMD_XRI_ABORTED_CX:
9451 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009452 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9453 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9454 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9455 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9456 default:
9457 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9458 "2014 Invalid command 0x%x\n",
9459 iocbq->iocb.ulpCommand);
9460 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009461 break;
James Smart4f774512009-05-22 14:52:35 -04009462 }
James Smart6d368e52011-05-24 11:44:12 -04009463
James Smart8012cc32012-10-31 14:44:49 -04009464 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9465 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9466 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9467 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9468 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9469 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9470 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9471 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009472 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9473 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9474 wqe->generic.wqe_com.abort_tag = abort_tag;
9475 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9476 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9477 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9478 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009479 return 0;
9480}
9481
9482/**
9483 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9484 * @phba: Pointer to HBA context object.
9485 * @ring_number: SLI ring number to issue iocb on.
9486 * @piocb: Pointer to command iocb.
9487 * @flag: Flag indicating if this command can be put into txq.
9488 *
9489 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9490 * an iocb command to an HBA with SLI-4 interface spec.
9491 *
9492 * This function is called with hbalock held. The function will return success
9493 * after it successfully submit the iocb to firmware or after adding to the
9494 * txq.
9495 **/
9496static int
9497__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9498 struct lpfc_iocbq *piocb, uint32_t flag)
9499{
9500 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -08009501 union lpfc_wqe128 wqe;
James Smart1ba981f2014-02-20 09:56:45 -05009502 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009503 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009504
James Smart895427b2017-02-12 13:52:30 -08009505 /* Get the WQ */
9506 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9507 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9508 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9509 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9510 else
9511 wq = phba->sli4_hba.oas_wq;
9512 } else {
9513 wq = phba->sli4_hba.els_wq;
9514 }
9515
9516 /* Get corresponding ring */
9517 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009518
James Smartb5c53952016-03-31 14:12:30 -07009519 /*
9520 * The WQE can be either 64 or 128 bytes,
James Smartb5c53952016-03-31 14:12:30 -07009521 */
James Smartb5c53952016-03-31 14:12:30 -07009522
James Smart895427b2017-02-12 13:52:30 -08009523 lockdep_assert_held(&phba->hbalock);
9524
James Smart4f774512009-05-22 14:52:35 -04009525 if (piocb->sli4_xritag == NO_XRI) {
9526 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009527 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009528 sglq = NULL;
9529 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009530 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009531 if (!(flag & SLI_IOCB_RET_IOCB)) {
9532 __lpfc_sli_ringtx_put(phba,
9533 pring, piocb);
9534 return IOCB_SUCCESS;
9535 } else {
9536 return IOCB_BUSY;
9537 }
9538 } else {
James Smart895427b2017-02-12 13:52:30 -08009539 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009540 if (!sglq) {
9541 if (!(flag & SLI_IOCB_RET_IOCB)) {
9542 __lpfc_sli_ringtx_put(phba,
9543 pring,
9544 piocb);
9545 return IOCB_SUCCESS;
9546 } else
9547 return IOCB_BUSY;
9548 }
9549 }
James Smart4f774512009-05-22 14:52:35 -04009550 }
James Smart2ea259e2017-02-12 13:52:27 -08009551 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009552 /* These IO's already have an XRI and a mapped sgl. */
9553 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009554 else {
James Smart6d368e52011-05-24 11:44:12 -04009555 /*
9556 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009557 * sglq is on the active list
9558 */
James Smartedccdc12013-01-03 15:43:45 -05009559 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009560 if (!sglq)
9561 return IOCB_ERROR;
9562 }
9563
9564 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009565 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009566 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009567 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009568 return IOCB_ERROR;
9569 }
9570
James Smart205e8242018-03-05 12:04:03 -08009571 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
James Smart4f774512009-05-22 14:52:35 -04009572 return IOCB_ERROR;
9573
James Smart205e8242018-03-05 12:04:03 -08009574 if (lpfc_sli4_wq_put(wq, &wqe))
James Smart895427b2017-02-12 13:52:30 -08009575 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009576 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9577
9578 return 0;
9579}
9580
9581/**
James Smart3772a992009-05-22 14:50:54 -04009582 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9583 *
9584 * This routine wraps the actual lockless version for issusing IOCB function
9585 * pointer from the lpfc_hba struct.
9586 *
9587 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009588 * IOCB_ERROR - Error
9589 * IOCB_SUCCESS - Success
9590 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009591 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009592int
James Smart3772a992009-05-22 14:50:54 -04009593__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9594 struct lpfc_iocbq *piocb, uint32_t flag)
9595{
9596 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9597}
9598
9599/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009600 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009601 * @phba: The hba struct for which this call is being executed.
9602 * @dev_grp: The HBA PCI-Device group number.
9603 *
9604 * This routine sets up the SLI interface API function jump table in @phba
9605 * struct.
9606 * Returns: 0 - success, -ENODEV - failure.
9607 **/
9608int
9609lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9610{
9611
9612 switch (dev_grp) {
9613 case LPFC_PCI_DEV_LP:
9614 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9615 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9616 break;
James Smart4f774512009-05-22 14:52:35 -04009617 case LPFC_PCI_DEV_OC:
9618 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9619 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9620 break;
James Smart3772a992009-05-22 14:50:54 -04009621 default:
9622 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9623 "1419 Invalid HBA PCI-device group: 0x%x\n",
9624 dev_grp);
9625 return -ENODEV;
9626 break;
9627 }
9628 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9629 return 0;
9630}
James Smart92d7f7b2007-06-17 19:56:38 -05009631
James Smarta1efe162015-05-21 13:55:20 -04009632/**
James Smart895427b2017-02-12 13:52:30 -08009633 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009634 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009635 * @piocb: Pointer to command iocb.
9636 *
James Smart895427b2017-02-12 13:52:30 -08009637 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9638 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009639 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009640 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009641 */
James Smart895427b2017-02-12 13:52:30 -08009642struct lpfc_sli_ring *
9643lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009644{
James Smart895427b2017-02-12 13:52:30 -08009645 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smart8b0dff12015-05-22 10:42:38 -04009646 if (!(phba->cfg_fof) ||
James Smart895427b2017-02-12 13:52:30 -08009647 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
James Smart8b0dff12015-05-22 10:42:38 -04009648 if (unlikely(!phba->sli4_hba.fcp_wq))
James Smart895427b2017-02-12 13:52:30 -08009649 return NULL;
James Smart8b0dff12015-05-22 10:42:38 -04009650 /*
James Smart895427b2017-02-12 13:52:30 -08009651 * for abort iocb hba_wqidx should already
James Smart8b0dff12015-05-22 10:42:38 -04009652 * be setup based on what work queue we used.
9653 */
Dick Kennedy8e036a92017-09-29 17:34:35 -07009654 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart895427b2017-02-12 13:52:30 -08009655 piocb->hba_wqidx =
James Smart8b0dff12015-05-22 10:42:38 -04009656 lpfc_sli4_scmd_to_wqidx_distr(phba,
9657 piocb->context1);
Dick Kennedy8e036a92017-09-29 17:34:35 -07009658 piocb->hba_wqidx = piocb->hba_wqidx %
9659 phba->cfg_fcp_io_channel;
9660 }
James Smart895427b2017-02-12 13:52:30 -08009661 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
James Smart8b0dff12015-05-22 10:42:38 -04009662 } else {
9663 if (unlikely(!phba->sli4_hba.oas_wq))
James Smart895427b2017-02-12 13:52:30 -08009664 return NULL;
9665 piocb->hba_wqidx = 0;
9666 return phba->sli4_hba.oas_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009667 }
James Smart895427b2017-02-12 13:52:30 -08009668 } else {
9669 if (unlikely(!phba->sli4_hba.els_wq))
9670 return NULL;
9671 piocb->hba_wqidx = 0;
9672 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009673 }
James Smart9bd2bff52014-09-03 12:57:30 -04009674}
9675
James Smarte59058c2008-08-24 21:49:00 -04009676/**
James Smart3621a712009-04-06 18:47:14 -04009677 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009678 * @phba: Pointer to HBA context object.
9679 * @pring: Pointer to driver SLI ring object.
9680 * @piocb: Pointer to command iocb.
9681 * @flag: Flag indicating if this command can be put into txq.
9682 *
9683 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9684 * function. This function gets the hbalock and calls
9685 * __lpfc_sli_issue_iocb function and will return the error returned
9686 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9687 * functions which do not hold hbalock.
9688 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009689int
James Smart3772a992009-05-22 14:50:54 -04009690lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009691 struct lpfc_iocbq *piocb, uint32_t flag)
9692{
James Smart895427b2017-02-12 13:52:30 -08009693 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009694 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009695 struct lpfc_queue *fpeq;
9696 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009697 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009698 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009699
James Smart7e56aa22012-08-03 12:35:34 -04009700 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08009701 pring = lpfc_sli4_calc_ring(phba, piocb);
9702 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -04009703 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -04009704
James Smart9bd2bff52014-09-03 12:57:30 -04009705 spin_lock_irqsave(&pring->ring_lock, iflags);
9706 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9707 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009708
James Smart9bd2bff52014-09-03 12:57:30 -04009709 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -08009710 idx = piocb->hba_wqidx;
9711 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009712
James Smart895427b2017-02-12 13:52:30 -08009713 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009714
James Smart9bd2bff52014-09-03 12:57:30 -04009715 /* Get associated EQ with this index */
9716 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009717
James Smart9bd2bff52014-09-03 12:57:30 -04009718 /* Turn off interrupts from this EQ */
James Smartb71413d2018-02-22 08:18:40 -08009719 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009720
James Smart9bd2bff52014-09-03 12:57:30 -04009721 /*
9722 * Process all the events on FCP EQ
9723 */
9724 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9725 lpfc_sli4_hba_handle_eqe(phba,
9726 eqe, idx);
9727 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009728 }
James Smartba20c852012-08-03 12:36:52 -04009729
James Smart9bd2bff52014-09-03 12:57:30 -04009730 /* Always clear and re-arm the EQ */
James Smartb71413d2018-02-22 08:18:40 -08009731 phba->sli4_hba.sli4_eq_release(fpeq,
James Smart9bd2bff52014-09-03 12:57:30 -04009732 LPFC_QUEUE_REARM);
9733 }
James Smart895427b2017-02-12 13:52:30 -08009734 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009735 }
James Smart7e56aa22012-08-03 12:35:34 -04009736 } else {
9737 /* For now, SLI2/3 will still use hbalock */
9738 spin_lock_irqsave(&phba->hbalock, iflags);
9739 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9740 spin_unlock_irqrestore(&phba->hbalock, iflags);
9741 }
James Smart92d7f7b2007-06-17 19:56:38 -05009742 return rc;
9743}
9744
James Smarte59058c2008-08-24 21:49:00 -04009745/**
James Smart3621a712009-04-06 18:47:14 -04009746 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009747 * @phba: Pointer to HBA context object.
9748 *
9749 * This function is called while driver attaches with the
9750 * HBA to setup the extra ring. The extra ring is used
9751 * only when driver needs to support target mode functionality
9752 * or IP over FC functionalities.
9753 *
James Smart895427b2017-02-12 13:52:30 -08009754 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04009755 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009756static int
9757lpfc_extra_ring_setup( struct lpfc_hba *phba)
9758{
9759 struct lpfc_sli *psli;
9760 struct lpfc_sli_ring *pring;
9761
9762 psli = &phba->sli;
9763
9764 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009765
9766 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -08009767 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -04009768 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9769 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9770 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9771 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009772
James Smarta4bc3372006-12-02 13:34:16 -05009773 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -08009774 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -05009775
James Smart7e56aa22012-08-03 12:35:34 -04009776 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9777 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9778 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9779 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009780
9781 /* Setup default profile for this ring */
9782 pring->iotag_max = 4096;
9783 pring->num_mask = 1;
9784 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009785 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9786 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009787 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9788 return 0;
9789}
9790
James Smartcb69f7d2011-12-13 13:21:57 -05009791/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9792 * @phba: Pointer to HBA context object.
9793 * @iocbq: Pointer to iocb object.
9794 *
9795 * The async_event handler calls this routine when it receives
9796 * an ASYNC_STATUS_CN event from the port. The port generates
9797 * this event when an Abort Sequence request to an rport fails
9798 * twice in succession. The abort could be originated by the
9799 * driver or by the port. The ABTS could have been for an ELS
9800 * or FCP IO. The port only generates this event when an ABTS
9801 * fails to complete after one retry.
9802 */
9803static void
9804lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9805 struct lpfc_iocbq *iocbq)
9806{
9807 struct lpfc_nodelist *ndlp = NULL;
9808 uint16_t rpi = 0, vpi = 0;
9809 struct lpfc_vport *vport = NULL;
9810
9811 /* The rpi in the ulpContext is vport-sensitive. */
9812 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9813 rpi = iocbq->iocb.ulpContext;
9814
9815 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9816 "3092 Port generated ABTS async event "
9817 "on vpi %d rpi %d status 0x%x\n",
9818 vpi, rpi, iocbq->iocb.ulpStatus);
9819
9820 vport = lpfc_find_vport_by_vpid(phba, vpi);
9821 if (!vport)
9822 goto err_exit;
9823 ndlp = lpfc_findnode_rpi(vport, rpi);
9824 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9825 goto err_exit;
9826
9827 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9828 lpfc_sli_abts_recover_port(vport, ndlp);
9829 return;
9830
9831 err_exit:
9832 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9833 "3095 Event Context not found, no "
9834 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9835 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9836 vpi, rpi);
9837}
9838
9839/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9840 * @phba: pointer to HBA context object.
9841 * @ndlp: nodelist pointer for the impacted rport.
9842 * @axri: pointer to the wcqe containing the failed exchange.
9843 *
9844 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9845 * port. The port generates this event when an abort exchange request to an
9846 * rport fails twice in succession with no reply. The abort could be originated
9847 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9848 */
9849void
9850lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9851 struct lpfc_nodelist *ndlp,
9852 struct sli4_wcqe_xri_aborted *axri)
9853{
9854 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009855 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009856
James Smart6b5151f2012-01-18 16:24:06 -05009857 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009858 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9859 "3115 Node Context not found, driver "
9860 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009861 return;
9862 }
9863
James Smartcb69f7d2011-12-13 13:21:57 -05009864 vport = ndlp->vport;
9865 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9866 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009867 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009868 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009869 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009870 bf_get(lpfc_wcqe_xa_status, axri),
9871 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009872
James Smart5c1db2a2012-03-01 22:34:36 -05009873 /*
9874 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9875 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9876 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9877 */
James Smarte3d2b802012-08-14 14:25:43 -04009878 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009879 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9880 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009881 lpfc_sli_abts_recover_port(vport, ndlp);
9882}
9883
James Smarte59058c2008-08-24 21:49:00 -04009884/**
James Smart3621a712009-04-06 18:47:14 -04009885 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009886 * @phba: Pointer to HBA context object.
9887 * @pring: Pointer to driver SLI ring object.
9888 * @iocbq: Pointer to iocb object.
9889 *
9890 * This function is called by the slow ring event handler
9891 * function when there is an ASYNC event iocb in the ring.
9892 * This function is called with no lock held.
9893 * Currently this function handles only temperature related
9894 * ASYNC events. The function decodes the temperature sensor
9895 * event message and posts events for the management applications.
9896 **/
James Smart98c9ea52007-10-27 13:37:33 -04009897static void
James Smart57127f12007-10-27 13:37:05 -04009898lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9899 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9900{
9901 IOCB_t *icmd;
9902 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009903 struct temp_event temp_event_data;
9904 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009905 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009906
9907 icmd = &iocbq->iocb;
9908 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009909
James Smartcb69f7d2011-12-13 13:21:57 -05009910 switch (evt_code) {
9911 case ASYNC_TEMP_WARN:
9912 case ASYNC_TEMP_SAFE:
9913 temp_event_data.data = (uint32_t) icmd->ulpContext;
9914 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9915 if (evt_code == ASYNC_TEMP_WARN) {
9916 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9917 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9918 "0347 Adapter is very hot, please take "
9919 "corrective action. temperature : %d Celsius\n",
9920 (uint32_t) icmd->ulpContext);
9921 } else {
9922 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9923 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9924 "0340 Adapter temperature is OK now. "
9925 "temperature : %d Celsius\n",
9926 (uint32_t) icmd->ulpContext);
9927 }
9928
9929 /* Send temperature change event to applications */
9930 shost = lpfc_shost_from_vport(phba->pport);
9931 fc_host_post_vendor_event(shost, fc_get_event_number(),
9932 sizeof(temp_event_data), (char *) &temp_event_data,
9933 LPFC_NL_VENDOR_ID);
9934 break;
9935 case ASYNC_STATUS_CN:
9936 lpfc_sli_abts_err_handler(phba, iocbq);
9937 break;
9938 default:
James Smarta257bf92009-04-06 18:48:10 -04009939 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009940 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009941 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009942 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009943 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9944 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9945 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9946 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009947 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009948 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9949 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9950 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9951 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9952
James Smartcb69f7d2011-12-13 13:21:57 -05009953 break;
James Smart57127f12007-10-27 13:37:05 -04009954 }
James Smart57127f12007-10-27 13:37:05 -04009955}
9956
9957
James Smarte59058c2008-08-24 21:49:00 -04009958/**
James Smart895427b2017-02-12 13:52:30 -08009959 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009960 * @phba: Pointer to HBA context object.
9961 *
9962 * lpfc_sli_setup sets up rings of the SLI interface with
9963 * number of iocbs per ring and iotags. This function is
9964 * called while driver attach to the HBA and before the
9965 * interrupts are enabled. So there is no need for locking.
9966 *
9967 * This function always returns 0.
9968 **/
dea31012005-04-17 16:05:31 -05009969int
James Smart895427b2017-02-12 13:52:30 -08009970lpfc_sli4_setup(struct lpfc_hba *phba)
9971{
9972 struct lpfc_sli_ring *pring;
9973
9974 pring = phba->sli4_hba.els_wq->pring;
9975 pring->num_mask = LPFC_MAX_RING_MASK;
9976 pring->prt[0].profile = 0; /* Mask 0 */
9977 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9978 pring->prt[0].type = FC_TYPE_ELS;
9979 pring->prt[0].lpfc_sli_rcv_unsol_event =
9980 lpfc_els_unsol_event;
9981 pring->prt[1].profile = 0; /* Mask 1 */
9982 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9983 pring->prt[1].type = FC_TYPE_ELS;
9984 pring->prt[1].lpfc_sli_rcv_unsol_event =
9985 lpfc_els_unsol_event;
9986 pring->prt[2].profile = 0; /* Mask 2 */
9987 /* NameServer Inquiry */
9988 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9989 /* NameServer */
9990 pring->prt[2].type = FC_TYPE_CT;
9991 pring->prt[2].lpfc_sli_rcv_unsol_event =
9992 lpfc_ct_unsol_event;
9993 pring->prt[3].profile = 0; /* Mask 3 */
9994 /* NameServer response */
9995 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9996 /* NameServer */
9997 pring->prt[3].type = FC_TYPE_CT;
9998 pring->prt[3].lpfc_sli_rcv_unsol_event =
9999 lpfc_ct_unsol_event;
10000 return 0;
10001}
10002
10003/**
10004 * lpfc_sli_setup - SLI ring setup function
10005 * @phba: Pointer to HBA context object.
10006 *
10007 * lpfc_sli_setup sets up rings of the SLI interface with
10008 * number of iocbs per ring and iotags. This function is
10009 * called while driver attach to the HBA and before the
10010 * interrupts are enabled. So there is no need for locking.
10011 *
10012 * This function always returns 0. SLI3 only.
10013 **/
10014int
dea31012005-04-17 16:05:31 -050010015lpfc_sli_setup(struct lpfc_hba *phba)
10016{
James Smarted957682007-06-17 19:56:37 -050010017 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -050010018 struct lpfc_sli *psli = &phba->sli;
10019 struct lpfc_sli_ring *pring;
10020
James Smart2a76a282012-08-03 12:35:54 -040010021 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -050010022 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -050010023
James Bottomley604a3e32005-10-29 10:28:33 -050010024 psli->iocbq_lookup = NULL;
10025 psli->iocbq_lookup_len = 0;
10026 psli->last_iotag = 0;
10027
dea31012005-04-17 16:05:31 -050010028 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010029 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010030 switch (i) {
10031 case LPFC_FCP_RING: /* ring 0 - FCP */
10032 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010033 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10034 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10035 pring->sli.sli3.numCiocb +=
10036 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10037 pring->sli.sli3.numRiocb +=
10038 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10039 pring->sli.sli3.numCiocb +=
10040 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10041 pring->sli.sli3.numRiocb +=
10042 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10043 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010044 SLI3_IOCB_CMD_SIZE :
10045 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010046 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010047 SLI3_IOCB_RSP_SIZE :
10048 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010049 pring->iotag_ctr = 0;
10050 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -050010051 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -050010052 pring->fast_iotag = pring->iotag_max;
10053 pring->num_mask = 0;
10054 break;
James Smarta4bc3372006-12-02 13:34:16 -050010055 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -050010056 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010057 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10058 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10059 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010060 SLI3_IOCB_CMD_SIZE :
10061 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010062 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010063 SLI3_IOCB_RSP_SIZE :
10064 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -050010065 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -050010066 pring->num_mask = 0;
10067 break;
10068 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10069 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010070 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10071 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10072 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010073 SLI3_IOCB_CMD_SIZE :
10074 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010075 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010076 SLI3_IOCB_RSP_SIZE :
10077 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010078 pring->fast_iotag = 0;
10079 pring->iotag_ctr = 0;
10080 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -040010081 pring->lpfc_sli_rcv_async_status =
10082 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -040010083 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -050010084 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -040010085 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10086 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010087 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010088 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010089 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -040010090 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10091 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010092 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010093 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010094 pring->prt[2].profile = 0; /* Mask 2 */
10095 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -040010096 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -050010097 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010098 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010099 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010100 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010101 pring->prt[3].profile = 0; /* Mask 3 */
10102 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -040010103 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -050010104 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010105 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010106 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010107 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010108 break;
10109 }
James Smart7e56aa22012-08-03 12:35:34 -040010110 totiocbsize += (pring->sli.sli3.numCiocb *
10111 pring->sli.sli3.sizeCiocb) +
10112 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050010113 }
James Smarted957682007-06-17 19:56:37 -050010114 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050010115 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040010116 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10117 "SLI2 SLIM Data: x%x x%lx\n",
10118 phba->brd_no, totiocbsize,
10119 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050010120 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010121 if (phba->cfg_multi_ring_support == 2)
10122 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050010123
10124 return 0;
10125}
10126
James Smarte59058c2008-08-24 21:49:00 -040010127/**
James Smart895427b2017-02-12 13:52:30 -080010128 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040010129 * @phba: Pointer to HBA context object.
10130 *
James Smart895427b2017-02-12 13:52:30 -080010131 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040010132 * ring. This function also initializes ring indices of each ring.
10133 * This function is called during the initialization of the SLI
10134 * interface of an HBA.
10135 * This function is called with no lock held and always returns
10136 * 1.
10137 **/
James Smart895427b2017-02-12 13:52:30 -080010138void
10139lpfc_sli4_queue_init(struct lpfc_hba *phba)
10140{
10141 struct lpfc_sli *psli;
10142 struct lpfc_sli_ring *pring;
10143 int i;
10144
10145 psli = &phba->sli;
10146 spin_lock_irq(&phba->hbalock);
10147 INIT_LIST_HEAD(&psli->mboxq);
10148 INIT_LIST_HEAD(&psli->mboxq_cmpl);
10149 /* Initialize list headers for txq and txcmplq as double linked lists */
10150 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
10151 pring = phba->sli4_hba.fcp_wq[i]->pring;
10152 pring->flag = 0;
10153 pring->ringno = LPFC_FCP_RING;
10154 INIT_LIST_HEAD(&pring->txq);
10155 INIT_LIST_HEAD(&pring->txcmplq);
10156 INIT_LIST_HEAD(&pring->iocb_continueq);
10157 spin_lock_init(&pring->ring_lock);
10158 }
10159 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
10160 pring = phba->sli4_hba.nvme_wq[i]->pring;
10161 pring->flag = 0;
10162 pring->ringno = LPFC_FCP_RING;
10163 INIT_LIST_HEAD(&pring->txq);
10164 INIT_LIST_HEAD(&pring->txcmplq);
10165 INIT_LIST_HEAD(&pring->iocb_continueq);
10166 spin_lock_init(&pring->ring_lock);
10167 }
10168 pring = phba->sli4_hba.els_wq->pring;
10169 pring->flag = 0;
10170 pring->ringno = LPFC_ELS_RING;
10171 INIT_LIST_HEAD(&pring->txq);
10172 INIT_LIST_HEAD(&pring->txcmplq);
10173 INIT_LIST_HEAD(&pring->iocb_continueq);
10174 spin_lock_init(&pring->ring_lock);
10175
10176 if (phba->cfg_nvme_io_channel) {
10177 pring = phba->sli4_hba.nvmels_wq->pring;
10178 pring->flag = 0;
10179 pring->ringno = LPFC_ELS_RING;
10180 INIT_LIST_HEAD(&pring->txq);
10181 INIT_LIST_HEAD(&pring->txcmplq);
10182 INIT_LIST_HEAD(&pring->iocb_continueq);
10183 spin_lock_init(&pring->ring_lock);
10184 }
10185
10186 if (phba->cfg_fof) {
10187 pring = phba->sli4_hba.oas_wq->pring;
10188 pring->flag = 0;
10189 pring->ringno = LPFC_FCP_RING;
10190 INIT_LIST_HEAD(&pring->txq);
10191 INIT_LIST_HEAD(&pring->txcmplq);
10192 INIT_LIST_HEAD(&pring->iocb_continueq);
10193 spin_lock_init(&pring->ring_lock);
10194 }
10195
10196 spin_unlock_irq(&phba->hbalock);
10197}
10198
10199/**
10200 * lpfc_sli_queue_init - Queue initialization function
10201 * @phba: Pointer to HBA context object.
10202 *
10203 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10204 * ring. This function also initializes ring indices of each ring.
10205 * This function is called during the initialization of the SLI
10206 * interface of an HBA.
10207 * This function is called with no lock held and always returns
10208 * 1.
10209 **/
10210void
10211lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010212{
10213 struct lpfc_sli *psli;
10214 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050010215 int i;
dea31012005-04-17 16:05:31 -050010216
10217 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050010218 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010219 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050010220 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050010221 /* Initialize list headers for txq and txcmplq as double linked lists */
10222 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010223 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010224 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040010225 pring->sli.sli3.next_cmdidx = 0;
10226 pring->sli.sli3.local_getidx = 0;
10227 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050010228 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050010229 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050010230 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080010231 pring->flag = 0;
10232 INIT_LIST_HEAD(&pring->txq);
10233 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010234 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010235 }
James Smart2e0fef82007-06-17 19:56:36 -050010236 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010237}
10238
James Smarte59058c2008-08-24 21:49:00 -040010239/**
James Smart04c68492009-05-22 14:52:52 -040010240 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10241 * @phba: Pointer to HBA context object.
10242 *
10243 * This routine flushes the mailbox command subsystem. It will unconditionally
10244 * flush all the mailbox commands in the three possible stages in the mailbox
10245 * command sub-system: pending mailbox command queue; the outstanding mailbox
10246 * command; and completed mailbox command queue. It is caller's responsibility
10247 * to make sure that the driver is in the proper state to flush the mailbox
10248 * command sub-system. Namely, the posting of mailbox commands into the
10249 * pending mailbox command queue from the various clients must be stopped;
10250 * either the HBA is in a state that it will never works on the outstanding
10251 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10252 * mailbox command has been completed.
10253 **/
10254static void
10255lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10256{
10257 LIST_HEAD(completions);
10258 struct lpfc_sli *psli = &phba->sli;
10259 LPFC_MBOXQ_t *pmb;
10260 unsigned long iflag;
10261
10262 /* Flush all the mailbox commands in the mbox system */
10263 spin_lock_irqsave(&phba->hbalock, iflag);
10264 /* The pending mailbox command queue */
10265 list_splice_init(&phba->sli.mboxq, &completions);
10266 /* The outstanding active mailbox command */
10267 if (psli->mbox_active) {
10268 list_add_tail(&psli->mbox_active->list, &completions);
10269 psli->mbox_active = NULL;
10270 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10271 }
10272 /* The completed mailbox command queue */
10273 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10274 spin_unlock_irqrestore(&phba->hbalock, iflag);
10275
10276 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10277 while (!list_empty(&completions)) {
10278 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10279 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10280 if (pmb->mbox_cmpl)
10281 pmb->mbox_cmpl(phba, pmb);
10282 }
10283}
10284
10285/**
James Smart3621a712009-04-06 18:47:14 -040010286 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010287 * @vport: Pointer to virtual port object.
10288 *
10289 * lpfc_sli_host_down is called to clean up the resources
10290 * associated with a vport before destroying virtual
10291 * port data structures.
10292 * This function does following operations:
10293 * - Free discovery resources associated with this virtual
10294 * port.
10295 * - Free iocbs associated with this virtual port in
10296 * the txq.
10297 * - Send abort for all iocb commands associated with this
10298 * vport in txcmplq.
10299 *
10300 * This function is called with no lock held and always returns 1.
10301 **/
dea31012005-04-17 16:05:31 -050010302int
James Smart92d7f7b2007-06-17 19:56:38 -050010303lpfc_sli_host_down(struct lpfc_vport *vport)
10304{
James Smart858c9f62007-06-17 19:56:39 -050010305 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010306 struct lpfc_hba *phba = vport->phba;
10307 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010308 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010309 struct lpfc_sli_ring *pring;
10310 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010311 int i;
10312 unsigned long flags = 0;
10313 uint16_t prev_pring_flag;
10314
10315 lpfc_cleanup_discovery_resources(vport);
10316
10317 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010318
James Smart895427b2017-02-12 13:52:30 -080010319 /*
10320 * Error everything on the txq since these iocbs
10321 * have not been given to the FW yet.
10322 * Also issue ABTS for everything on the txcmplq
10323 */
10324 if (phba->sli_rev != LPFC_SLI_REV4) {
10325 for (i = 0; i < psli->num_rings; i++) {
10326 pring = &psli->sli3_ring[i];
10327 prev_pring_flag = pring->flag;
10328 /* Only slow rings */
10329 if (pring->ringno == LPFC_ELS_RING) {
10330 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10331 /* Set the lpfc data pending flag */
10332 set_bit(LPFC_DATA_READY, &phba->data_flags);
10333 }
10334 list_for_each_entry_safe(iocb, next_iocb,
10335 &pring->txq, list) {
10336 if (iocb->vport != vport)
10337 continue;
10338 list_move_tail(&iocb->list, &completions);
10339 }
10340 list_for_each_entry_safe(iocb, next_iocb,
10341 &pring->txcmplq, list) {
10342 if (iocb->vport != vport)
10343 continue;
10344 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10345 }
10346 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010347 }
James Smart895427b2017-02-12 13:52:30 -080010348 } else {
10349 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10350 pring = qp->pring;
10351 if (!pring)
10352 continue;
10353 if (pring == phba->sli4_hba.els_wq->pring) {
10354 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10355 /* Set the lpfc data pending flag */
10356 set_bit(LPFC_DATA_READY, &phba->data_flags);
10357 }
10358 prev_pring_flag = pring->flag;
10359 spin_lock_irq(&pring->ring_lock);
10360 list_for_each_entry_safe(iocb, next_iocb,
10361 &pring->txq, list) {
10362 if (iocb->vport != vport)
10363 continue;
10364 list_move_tail(&iocb->list, &completions);
10365 }
10366 spin_unlock_irq(&pring->ring_lock);
10367 list_for_each_entry_safe(iocb, next_iocb,
10368 &pring->txcmplq, list) {
10369 if (iocb->vport != vport)
10370 continue;
10371 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10372 }
10373 pring->flag = prev_pring_flag;
10374 }
James Smart92d7f7b2007-06-17 19:56:38 -050010375 }
James Smart92d7f7b2007-06-17 19:56:38 -050010376 spin_unlock_irqrestore(&phba->hbalock, flags);
10377
James Smarta257bf92009-04-06 18:48:10 -040010378 /* Cancel all the IOCBs from the completions list */
10379 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10380 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010381 return 1;
10382}
10383
James Smarte59058c2008-08-24 21:49:00 -040010384/**
James Smart3621a712009-04-06 18:47:14 -040010385 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010386 * @phba: Pointer to HBA context object.
10387 *
10388 * This function cleans up all iocb, buffers, mailbox commands
10389 * while shutting down the HBA. This function is called with no
10390 * lock held and always returns 1.
10391 * This function does the following to cleanup driver resources:
10392 * - Free discovery resources for each virtual port
10393 * - Cleanup any pending fabric iocbs
10394 * - Iterate through the iocb txq and free each entry
10395 * in the list.
10396 * - Free up any buffer posted to the HBA
10397 * - Free mailbox commands in the mailbox queue.
10398 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010399int
James Smart2e0fef82007-06-17 19:56:36 -050010400lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010401{
James Smart2534ba72007-04-25 09:52:20 -040010402 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010403 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010404 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010405 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010406 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010407 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010408 int i;
10409
10410 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010411 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010412
dea31012005-04-17 16:05:31 -050010413 lpfc_hba_down_prep(phba);
10414
James Smart92d7f7b2007-06-17 19:56:38 -050010415 lpfc_fabric_abort_hba(phba);
10416
James Smart2e0fef82007-06-17 19:56:36 -050010417 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010418
James Smart895427b2017-02-12 13:52:30 -080010419 /*
10420 * Error everything on the txq since these iocbs
10421 * have not been given to the FW yet.
10422 */
10423 if (phba->sli_rev != LPFC_SLI_REV4) {
10424 for (i = 0; i < psli->num_rings; i++) {
10425 pring = &psli->sli3_ring[i];
10426 /* Only slow rings */
10427 if (pring->ringno == LPFC_ELS_RING) {
10428 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10429 /* Set the lpfc data pending flag */
10430 set_bit(LPFC_DATA_READY, &phba->data_flags);
10431 }
10432 list_splice_init(&pring->txq, &completions);
10433 }
10434 } else {
10435 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10436 pring = qp->pring;
10437 if (!pring)
10438 continue;
10439 spin_lock_irq(&pring->ring_lock);
10440 list_splice_init(&pring->txq, &completions);
10441 spin_unlock_irq(&pring->ring_lock);
10442 if (pring == phba->sli4_hba.els_wq->pring) {
10443 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10444 /* Set the lpfc data pending flag */
10445 set_bit(LPFC_DATA_READY, &phba->data_flags);
10446 }
10447 }
dea31012005-04-17 16:05:31 -050010448 }
James Smart2e0fef82007-06-17 19:56:36 -050010449 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010450
James Smarta257bf92009-04-06 18:48:10 -040010451 /* Cancel all the IOCBs from the completions list */
10452 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10453 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010454
James Smart0ff10d42008-01-11 01:52:36 -050010455 spin_lock_irqsave(&phba->hbalock, flags);
10456 list_splice_init(&phba->elsbuf, &completions);
10457 phba->elsbuf_cnt = 0;
10458 phba->elsbuf_prev_cnt = 0;
10459 spin_unlock_irqrestore(&phba->hbalock, flags);
10460
10461 while (!list_empty(&completions)) {
10462 list_remove_head(&completions, buf_ptr,
10463 struct lpfc_dmabuf, list);
10464 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10465 kfree(buf_ptr);
10466 }
10467
dea31012005-04-17 16:05:31 -050010468 /* Return any active mbox cmds */
10469 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010470
James Smartda0436e2009-05-22 14:51:39 -040010471 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010472 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010473 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010474
James Smartda0436e2009-05-22 14:51:39 -040010475 return 1;
10476}
James Smart92d7f7b2007-06-17 19:56:38 -050010477
James Smartda0436e2009-05-22 14:51:39 -040010478/**
James Smart3621a712009-04-06 18:47:14 -040010479 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010480 * @srcp: Source memory pointer.
10481 * @destp: Destination memory pointer.
10482 * @cnt: Number of words required to be copied.
10483 *
10484 * This function is used for copying data between driver memory
10485 * and the SLI memory. This function also changes the endianness
10486 * of each word if native endianness is different from SLI
10487 * endianness. This function can be called with or without
10488 * lock.
10489 **/
dea31012005-04-17 16:05:31 -050010490void
10491lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10492{
10493 uint32_t *src = srcp;
10494 uint32_t *dest = destp;
10495 uint32_t ldata;
10496 int i;
10497
10498 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10499 ldata = *src;
10500 ldata = le32_to_cpu(ldata);
10501 *dest = ldata;
10502 src++;
10503 dest++;
10504 }
10505}
10506
James Smarte59058c2008-08-24 21:49:00 -040010507
10508/**
James Smarta0c87cb2009-07-19 10:01:10 -040010509 * lpfc_sli_bemem_bcopy - SLI memory copy function
10510 * @srcp: Source memory pointer.
10511 * @destp: Destination memory pointer.
10512 * @cnt: Number of words required to be copied.
10513 *
10514 * This function is used for copying data between a data structure
10515 * with big endian representation to local endianness.
10516 * This function can be called with or without lock.
10517 **/
10518void
10519lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10520{
10521 uint32_t *src = srcp;
10522 uint32_t *dest = destp;
10523 uint32_t ldata;
10524 int i;
10525
10526 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10527 ldata = *src;
10528 ldata = be32_to_cpu(ldata);
10529 *dest = ldata;
10530 src++;
10531 dest++;
10532 }
10533}
10534
10535/**
James Smart3621a712009-04-06 18:47:14 -040010536 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010537 * @phba: Pointer to HBA context object.
10538 * @pring: Pointer to driver SLI ring object.
10539 * @mp: Pointer to driver buffer object.
10540 *
10541 * This function is called with no lock held.
10542 * It always return zero after adding the buffer to the postbufq
10543 * buffer list.
10544 **/
dea31012005-04-17 16:05:31 -050010545int
James Smart2e0fef82007-06-17 19:56:36 -050010546lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10547 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010548{
10549 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10550 later */
James Smart2e0fef82007-06-17 19:56:36 -050010551 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010552 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010553 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010554 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010555 return 0;
10556}
10557
James Smarte59058c2008-08-24 21:49:00 -040010558/**
James Smart3621a712009-04-06 18:47:14 -040010559 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010560 * @phba: Pointer to HBA context object.
10561 *
10562 * When HBQ is enabled, buffers are searched based on tags. This function
10563 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10564 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10565 * does not conflict with tags of buffer posted for unsolicited events.
10566 * The function returns the allocated tag. The function is called with
10567 * no locks held.
10568 **/
James Smart76bb24e2007-10-27 13:38:00 -040010569uint32_t
10570lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10571{
10572 spin_lock_irq(&phba->hbalock);
10573 phba->buffer_tag_count++;
10574 /*
10575 * Always set the QUE_BUFTAG_BIT to distiguish between
10576 * a tag assigned by HBQ.
10577 */
10578 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10579 spin_unlock_irq(&phba->hbalock);
10580 return phba->buffer_tag_count;
10581}
10582
James Smarte59058c2008-08-24 21:49:00 -040010583/**
James Smart3621a712009-04-06 18:47:14 -040010584 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010585 * @phba: Pointer to HBA context object.
10586 * @pring: Pointer to driver SLI ring object.
10587 * @tag: Buffer tag.
10588 *
10589 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10590 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10591 * iocb is posted to the response ring with the tag of the buffer.
10592 * This function searches the pring->postbufq list using the tag
10593 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10594 * iocb. If the buffer is found then lpfc_dmabuf object of the
10595 * buffer is returned to the caller else NULL is returned.
10596 * This function is called with no lock held.
10597 **/
James Smart76bb24e2007-10-27 13:38:00 -040010598struct lpfc_dmabuf *
10599lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10600 uint32_t tag)
10601{
10602 struct lpfc_dmabuf *mp, *next_mp;
10603 struct list_head *slp = &pring->postbufq;
10604
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010605 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010606 spin_lock_irq(&phba->hbalock);
10607 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10608 if (mp->buffer_tag == tag) {
10609 list_del_init(&mp->list);
10610 pring->postbufq_cnt--;
10611 spin_unlock_irq(&phba->hbalock);
10612 return mp;
10613 }
10614 }
10615
10616 spin_unlock_irq(&phba->hbalock);
10617 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010618 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010619 "ring %d Data x%lx x%p x%p x%x\n",
10620 pring->ringno, (unsigned long) tag,
10621 slp->next, slp->prev, pring->postbufq_cnt);
10622
10623 return NULL;
10624}
dea31012005-04-17 16:05:31 -050010625
James Smarte59058c2008-08-24 21:49:00 -040010626/**
James Smart3621a712009-04-06 18:47:14 -040010627 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010628 * @phba: Pointer to HBA context object.
10629 * @pring: Pointer to driver SLI ring object.
10630 * @phys: DMA address of the buffer.
10631 *
10632 * This function searches the buffer list using the dma_address
10633 * of unsolicited event to find the driver's lpfc_dmabuf object
10634 * corresponding to the dma_address. The function returns the
10635 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10636 * This function is called by the ct and els unsolicited event
10637 * handlers to get the buffer associated with the unsolicited
10638 * event.
10639 *
10640 * This function is called with no lock held.
10641 **/
dea31012005-04-17 16:05:31 -050010642struct lpfc_dmabuf *
10643lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10644 dma_addr_t phys)
10645{
10646 struct lpfc_dmabuf *mp, *next_mp;
10647 struct list_head *slp = &pring->postbufq;
10648
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010649 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010650 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010651 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10652 if (mp->phys == phys) {
10653 list_del_init(&mp->list);
10654 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010655 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010656 return mp;
10657 }
10658 }
10659
James Smart2e0fef82007-06-17 19:56:36 -050010660 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010661 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010662 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010663 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010664 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010665 slp->next, slp->prev, pring->postbufq_cnt);
10666 return NULL;
10667}
10668
James Smarte59058c2008-08-24 21:49:00 -040010669/**
James Smart3621a712009-04-06 18:47:14 -040010670 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010671 * @phba: Pointer to HBA context object.
10672 * @cmdiocb: Pointer to driver command iocb object.
10673 * @rspiocb: Pointer to driver response iocb object.
10674 *
10675 * This function is the completion handler for the abort iocbs for
10676 * ELS commands. This function is called from the ELS ring event
10677 * handler with no lock held. This function frees memory resources
10678 * associated with the abort iocb.
10679 **/
dea31012005-04-17 16:05:31 -050010680static void
James Smart2e0fef82007-06-17 19:56:36 -050010681lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10682 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050010683{
James Smart2e0fef82007-06-17 19:56:36 -050010684 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040010685 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050010686 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040010687
10688 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050010689
10690 /*
10691 * Assume that the port already completed and returned, or
10692 * will return the iocb. Just Log the message.
10693 */
James Smart2680eea2007-04-25 09:52:55 -040010694 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10695 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10696
James Smart2e0fef82007-06-17 19:56:36 -050010697 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040010698 if (phba->sli_rev < LPFC_SLI_REV4) {
10699 if (abort_iotag != 0 &&
10700 abort_iotag <= phba->sli.last_iotag)
10701 abort_iocb =
10702 phba->sli.iocbq_lookup[abort_iotag];
10703 } else
10704 /* For sli4 the abort_tag is the XRI,
10705 * so the abort routine puts the iotag of the iocb
10706 * being aborted in the context field of the abort
10707 * IOCB.
10708 */
10709 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040010710
James Smart2a9bf3d2010-06-07 15:24:45 -040010711 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10712 "0327 Cannot abort els iocb %p "
10713 "with tag %x context %x, abort status %x, "
10714 "abort code %x\n",
10715 abort_iocb, abort_iotag, abort_context,
10716 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040010717
James Smartff78d8f2011-12-13 13:21:35 -050010718 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040010719 }
James Bottomley604a3e32005-10-29 10:28:33 -050010720 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050010721 return;
10722}
10723
James Smarte59058c2008-08-24 21:49:00 -040010724/**
James Smart3621a712009-04-06 18:47:14 -040010725 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040010726 * @phba: Pointer to HBA context object.
10727 * @cmdiocb: Pointer to driver command iocb object.
10728 * @rspiocb: Pointer to driver response iocb object.
10729 *
10730 * The function is called from SLI ring event handler with no
10731 * lock held. This function is the completion handler for ELS commands
10732 * which are aborted. The function frees memory resources used for
10733 * the aborted ELS commands.
10734 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010735static void
10736lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10737 struct lpfc_iocbq *rspiocb)
10738{
10739 IOCB_t *irsp = &rspiocb->iocb;
10740
10741 /* ELS cmd tag <ulpIoTag> completes */
10742 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040010743 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050010744 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010745 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050010746 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050010747 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10748 lpfc_ct_free_iocb(phba, cmdiocb);
10749 else
10750 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050010751 return;
10752}
10753
James Smarte59058c2008-08-24 21:49:00 -040010754/**
James Smart5af5eee2010-10-22 11:06:38 -040010755 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040010756 * @phba: Pointer to HBA context object.
10757 * @pring: Pointer to driver SLI ring object.
10758 * @cmdiocb: Pointer to driver command iocb object.
10759 *
James Smart5af5eee2010-10-22 11:06:38 -040010760 * This function issues an abort iocb for the provided command iocb down to
10761 * the port. Other than the case the outstanding command iocb is an abort
10762 * request, this function issues abort out unconditionally. This function is
10763 * called with hbalock held. The function returns 0 when it fails due to
10764 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040010765 **/
James Smart5af5eee2010-10-22 11:06:38 -040010766static int
10767lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010768 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050010769{
James Smart2e0fef82007-06-17 19:56:36 -050010770 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010771 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050010772 IOCB_t *icmd = NULL;
10773 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010774 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040010775 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -040010776
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010777 lockdep_assert_held(&phba->hbalock);
10778
James Smart92d7f7b2007-06-17 19:56:38 -050010779 /*
10780 * There are certain command types we don't want to abort. And we
10781 * don't want to abort commands that are already in the process of
10782 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010783 */
10784 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010785 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010786 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10787 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010788 return 0;
10789
dea31012005-04-17 16:05:31 -050010790 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010791 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010792 if (abtsiocbp == NULL)
10793 return 0;
dea31012005-04-17 16:05:31 -050010794
James Smart07951072007-04-25 09:51:38 -040010795 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010796 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010797 */
10798 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10799
dea31012005-04-17 16:05:31 -050010800 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010801 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10802 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010803 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010804 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010805 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10806 }
James Smartda0436e2009-05-22 14:51:39 -040010807 else
10808 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010809 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010810 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010811
James Smart5ffc2662009-11-18 15:39:44 -050010812 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010813 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010814 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10815 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010816 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10817 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010818
James Smart2e0fef82007-06-17 19:56:36 -050010819 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010820 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10821 else
10822 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10823
10824 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010825 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010826
James Smarte8b62012007-08-02 11:10:09 -040010827 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10828 "0339 Abort xri x%x, original iotag x%x, "
10829 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010830 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010831 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010832 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010833
10834 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010835 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10836 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010837 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040010838 /* Note: both hbalock and ring_lock need to be set here */
10839 spin_lock_irqsave(&pring->ring_lock, iflags);
10840 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10841 abtsiocbp, 0);
10842 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10843 } else {
10844 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10845 abtsiocbp, 0);
10846 }
James Smart07951072007-04-25 09:51:38 -040010847
James Smartd7c255b2008-08-24 21:50:00 -040010848 if (retval)
10849 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010850
10851 /*
10852 * Caller to this routine should check for IOCB_ERROR
10853 * and handle it properly. This routine no longer removes
10854 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10855 */
10856 return retval;
10857}
10858
10859/**
10860 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10861 * @phba: Pointer to HBA context object.
10862 * @pring: Pointer to driver SLI ring object.
10863 * @cmdiocb: Pointer to driver command iocb object.
10864 *
10865 * This function issues an abort iocb for the provided command iocb. In case
10866 * of unloading, the abort iocb will not be issued to commands on the ELS
10867 * ring. Instead, the callback function shall be changed to those commands
10868 * so that nothing happens when them finishes. This function is called with
10869 * hbalock held. The function returns 0 when the command iocb is an abort
10870 * request.
10871 **/
10872int
10873lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10874 struct lpfc_iocbq *cmdiocb)
10875{
10876 struct lpfc_vport *vport = cmdiocb->vport;
10877 int retval = IOCB_ERROR;
10878 IOCB_t *icmd = NULL;
10879
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010880 lockdep_assert_held(&phba->hbalock);
10881
James Smart5af5eee2010-10-22 11:06:38 -040010882 /*
10883 * There are certain command types we don't want to abort. And we
10884 * don't want to abort commands that are already in the process of
10885 * being aborted.
10886 */
10887 icmd = &cmdiocb->iocb;
10888 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10889 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10890 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10891 return 0;
10892
Dick Kennedy1234a6d2017-09-29 17:34:29 -070010893 if (!pring) {
10894 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10895 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10896 else
10897 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10898 goto abort_iotag_exit;
10899 }
10900
James Smart5af5eee2010-10-22 11:06:38 -040010901 /*
10902 * If we're unloading, don't abort iocb on the ELS ring, but change
10903 * the callback so that nothing happens when it finishes.
10904 */
10905 if ((vport->load_flag & FC_UNLOADING) &&
10906 (pring->ringno == LPFC_ELS_RING)) {
10907 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10908 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10909 else
10910 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10911 goto abort_iotag_exit;
10912 }
10913
10914 /* Now, we try to issue the abort to the cmdiocb out */
10915 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10916
James Smart07951072007-04-25 09:51:38 -040010917abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010918 /*
10919 * Caller to this routine should check for IOCB_ERROR
10920 * and handle it properly. This routine no longer removes
10921 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010922 */
James Smart2e0fef82007-06-17 19:56:36 -050010923 return retval;
dea31012005-04-17 16:05:31 -050010924}
10925
James Smarte59058c2008-08-24 21:49:00 -040010926/**
James Smart895427b2017-02-12 13:52:30 -080010927 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10928 * @phba: Pointer to HBA context object.
10929 * @pring: Pointer to driver SLI ring object.
10930 * @cmdiocb: Pointer to driver command iocb object.
10931 *
10932 * This function issues an abort iocb for the provided command iocb down to
10933 * the port. Other than the case the outstanding command iocb is an abort
10934 * request, this function issues abort out unconditionally. This function is
10935 * called with hbalock held. The function returns 0 when it fails due to
10936 * memory allocation failure or when the command iocb is an abort request.
10937 **/
10938static int
10939lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10940 struct lpfc_iocbq *cmdiocb)
10941{
10942 struct lpfc_vport *vport = cmdiocb->vport;
10943 struct lpfc_iocbq *abtsiocbp;
James Smart205e8242018-03-05 12:04:03 -080010944 union lpfc_wqe128 *abts_wqe;
James Smart895427b2017-02-12 13:52:30 -080010945 int retval;
10946
10947 /*
10948 * There are certain command types we don't want to abort. And we
10949 * don't want to abort commands that are already in the process of
10950 * being aborted.
10951 */
10952 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10953 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10954 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10955 return 0;
10956
10957 /* issue ABTS for this io based on iotag */
10958 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10959 if (abtsiocbp == NULL)
10960 return 0;
10961
10962 /* This signals the response to set the correct status
10963 * before calling the completion handler
10964 */
10965 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10966
10967 /* Complete prepping the abort wqe and issue to the FW. */
10968 abts_wqe = &abtsiocbp->wqe;
10969 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10970 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10971
10972 /* Explicitly set reserved fields to zero.*/
10973 abts_wqe->abort_cmd.rsrvd4 = 0;
10974 abts_wqe->abort_cmd.rsrvd5 = 0;
10975
10976 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10977 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10978 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10979
10980 /* word 7 */
10981 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10982 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10983 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10984 cmdiocb->iocb.ulpClass);
10985
10986 /* word 8 - tell the FW to abort the IO associated with this
10987 * outstanding exchange ID.
10988 */
10989 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10990
10991 /* word 9 - this is the iotag for the abts_wqe completion. */
10992 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10993 abtsiocbp->iotag);
10994
10995 /* word 10 */
10996 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10997 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10998 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10999
11000 /* word 11 */
11001 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11002 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
11003 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
11004
11005 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11006 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
11007 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080011008 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart895427b2017-02-12 13:52:30 -080011009 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
Dick Kennedycd22d602017-08-23 16:55:35 -070011010 if (retval) {
James Smart895427b2017-02-12 13:52:30 -080011011 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11012 "6147 Failed abts issue_wqe with status x%x "
11013 "for oxid x%x\n",
11014 retval, cmdiocb->sli4_xritag);
11015 lpfc_sli_release_iocbq(phba, abtsiocbp);
11016 return retval;
11017 }
11018
11019 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11020 "6148 Drv Abort NVME Request Issued for "
11021 "ox_id x%x on reqtag x%x\n",
11022 cmdiocb->sli4_xritag,
11023 abtsiocbp->iotag);
11024
11025 return retval;
11026}
11027
11028/**
James Smart5af5eee2010-10-22 11:06:38 -040011029 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11030 * @phba: pointer to lpfc HBA data structure.
11031 *
11032 * This routine will abort all pending and outstanding iocbs to an HBA.
11033 **/
11034void
11035lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11036{
11037 struct lpfc_sli *psli = &phba->sli;
11038 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080011039 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011040 int i;
11041
James Smart895427b2017-02-12 13:52:30 -080011042 if (phba->sli_rev != LPFC_SLI_REV4) {
11043 for (i = 0; i < psli->num_rings; i++) {
11044 pring = &psli->sli3_ring[i];
11045 lpfc_sli_abort_iocb_ring(phba, pring);
11046 }
11047 return;
11048 }
11049 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11050 pring = qp->pring;
11051 if (!pring)
11052 continue;
James Smartdb55fba2014-04-04 13:52:02 -040011053 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040011054 }
11055}
11056
11057/**
James Smart3621a712009-04-06 18:47:14 -040011058 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040011059 * @iocbq: Pointer to driver iocb object.
11060 * @vport: Pointer to driver virtual port object.
11061 * @tgt_id: SCSI ID of the target.
11062 * @lun_id: LUN ID of the scsi device.
11063 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11064 *
James Smart3621a712009-04-06 18:47:14 -040011065 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040011066 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11067 * 0 if the filtering criteria is met for the given iocb and will return
11068 * 1 if the filtering criteria is not met.
11069 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11070 * given iocb is for the SCSI device specified by vport, tgt_id and
11071 * lun_id parameter.
11072 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11073 * given iocb is for the SCSI target specified by vport and tgt_id
11074 * parameters.
11075 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11076 * given iocb is for the SCSI host associated with the given vport.
11077 * This function is called with no locks held.
11078 **/
dea31012005-04-17 16:05:31 -050011079static int
James Smart51ef4c22007-08-02 11:10:31 -040011080lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11081 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011082 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011083{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011084 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011085 int rc = 1;
11086
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011087 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
11088 return rc;
11089
James Smart51ef4c22007-08-02 11:10:31 -040011090 if (iocbq->vport != vport)
11091 return rc;
11092
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011093 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011094
James Smart495a7142008-06-14 22:52:59 -040011095 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050011096 return rc;
11097
11098 switch (ctx_cmd) {
11099 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040011100 if ((lpfc_cmd->rdata->pnode) &&
11101 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11102 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050011103 rc = 0;
11104 break;
11105 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040011106 if ((lpfc_cmd->rdata->pnode) &&
11107 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050011108 rc = 0;
11109 break;
dea31012005-04-17 16:05:31 -050011110 case LPFC_CTX_HOST:
11111 rc = 0;
11112 break;
11113 default:
11114 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070011115 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050011116 break;
11117 }
11118
11119 return rc;
11120}
11121
James Smarte59058c2008-08-24 21:49:00 -040011122/**
James Smart3621a712009-04-06 18:47:14 -040011123 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040011124 * @vport: Pointer to virtual port.
11125 * @tgt_id: SCSI ID of the target.
11126 * @lun_id: LUN ID of the scsi device.
11127 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11128 *
11129 * This function returns number of FCP commands pending for the vport.
11130 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11131 * commands pending on the vport associated with SCSI device specified
11132 * by tgt_id and lun_id parameters.
11133 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11134 * commands pending on the vport associated with SCSI target specified
11135 * by tgt_id parameter.
11136 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11137 * commands pending on the vport.
11138 * This function returns the number of iocbs which satisfy the filter.
11139 * This function is called without any lock held.
11140 **/
dea31012005-04-17 16:05:31 -050011141int
James Smart51ef4c22007-08-02 11:10:31 -040011142lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11143 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011144{
James Smart51ef4c22007-08-02 11:10:31 -040011145 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011146 struct lpfc_iocbq *iocbq;
11147 int sum, i;
dea31012005-04-17 16:05:31 -050011148
Johannes Thumshirn31979002016-07-18 16:06:03 +020011149 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011150 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11151 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011152
James Smart51ef4c22007-08-02 11:10:31 -040011153 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11154 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011155 sum++;
dea31012005-04-17 16:05:31 -050011156 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020011157 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011158
dea31012005-04-17 16:05:31 -050011159 return sum;
11160}
11161
James Smarte59058c2008-08-24 21:49:00 -040011162/**
James Smart3621a712009-04-06 18:47:14 -040011163 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040011164 * @phba: Pointer to HBA context object
11165 * @cmdiocb: Pointer to command iocb object.
11166 * @rspiocb: Pointer to response iocb object.
11167 *
11168 * This function is called when an aborted FCP iocb completes. This
11169 * function is called by the ring event handler with no lock held.
11170 * This function frees the iocb.
11171 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011172void
James Smart2e0fef82007-06-17 19:56:36 -050011173lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11174 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011175{
James Smartcb69f7d2011-12-13 13:21:57 -050011176 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040011177 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050011178 "original iotag x%x, abort cmd iotag x%x "
11179 "status 0x%x, reason 0x%x\n",
11180 cmdiocb->iocb.un.acxri.abortContextTag,
11181 cmdiocb->iocb.un.acxri.abortIoTag,
11182 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11183 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050011184 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011185 return;
11186}
11187
James Smarte59058c2008-08-24 21:49:00 -040011188/**
James Smart3621a712009-04-06 18:47:14 -040011189 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040011190 * @vport: Pointer to virtual port.
11191 * @pring: Pointer to driver SLI ring object.
11192 * @tgt_id: SCSI ID of the target.
11193 * @lun_id: LUN ID of the scsi device.
11194 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11195 *
11196 * This function sends an abort command for every SCSI command
11197 * associated with the given virtual port pending on the ring
11198 * filtered by lpfc_sli_validate_fcp_iocb function.
11199 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11200 * FCP iocbs associated with lun specified by tgt_id and lun_id
11201 * parameters
11202 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11203 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11204 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11205 * FCP iocbs associated with virtual port.
11206 * This function returns number of iocbs it failed to abort.
11207 * This function is called with no locks held.
11208 **/
dea31012005-04-17 16:05:31 -050011209int
James Smart51ef4c22007-08-02 11:10:31 -040011210lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11211 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050011212{
James Smart51ef4c22007-08-02 11:10:31 -040011213 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011214 struct lpfc_iocbq *iocbq;
11215 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070011216 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050011217 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050011218 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011219 int i;
dea31012005-04-17 16:05:31 -050011220
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011221 for (i = 1; i <= phba->sli.last_iotag; i++) {
11222 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011223
James Smart51ef4c22007-08-02 11:10:31 -040011224 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011225 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011226 continue;
11227
James Smartafbd8d82013-09-06 12:22:13 -040011228 /*
11229 * If the iocbq is already being aborted, don't take a second
11230 * action, but do count it.
11231 */
11232 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11233 continue;
11234
dea31012005-04-17 16:05:31 -050011235 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011236 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011237 if (abtsiocb == NULL) {
11238 errcnt++;
11239 continue;
11240 }
dea31012005-04-17 16:05:31 -050011241
James Smartafbd8d82013-09-06 12:22:13 -040011242 /* indicate the IO is being aborted by the driver. */
11243 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11244
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011245 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011246 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11247 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011248 if (phba->sli_rev == LPFC_SLI_REV4)
11249 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11250 else
11251 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011252 abtsiocb->iocb.ulpLe = 1;
11253 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011254 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011255
James Smart5ffc2662009-11-18 15:39:44 -050011256 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011257 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011258 if (iocbq->iocb_flag & LPFC_IO_FCP)
11259 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011260 if (iocbq->iocb_flag & LPFC_IO_FOF)
11261 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011262
James Smart2e0fef82007-06-17 19:56:36 -050011263 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011264 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11265 else
11266 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11267
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011268 /* Setup callback routine and issue the command. */
11269 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011270 if (phba->sli_rev == LPFC_SLI_REV4) {
11271 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11272 if (!pring_s4)
11273 continue;
11274 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11275 abtsiocb, 0);
11276 } else
11277 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11278 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011279 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011280 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011281 errcnt++;
11282 continue;
11283 }
11284 }
11285
11286 return errcnt;
11287}
11288
James Smarte59058c2008-08-24 21:49:00 -040011289/**
James Smart98912dda2014-04-04 13:52:31 -040011290 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11291 * @vport: Pointer to virtual port.
11292 * @pring: Pointer to driver SLI ring object.
11293 * @tgt_id: SCSI ID of the target.
11294 * @lun_id: LUN ID of the scsi device.
11295 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11296 *
11297 * This function sends an abort command for every SCSI command
11298 * associated with the given virtual port pending on the ring
11299 * filtered by lpfc_sli_validate_fcp_iocb function.
11300 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11301 * FCP iocbs associated with lun specified by tgt_id and lun_id
11302 * parameters
11303 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11304 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11305 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11306 * FCP iocbs associated with virtual port.
11307 * This function returns number of iocbs it aborted .
11308 * This function is called with no locks held right after a taskmgmt
11309 * command is sent.
11310 **/
11311int
11312lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11313 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11314{
11315 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040011316 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011317 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011318 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011319 struct lpfc_iocbq *iocbq;
11320 IOCB_t *icmd;
11321 int sum, i, ret_val;
11322 unsigned long iflags;
11323 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040011324
James Smart59c68ea2018-04-09 14:24:25 -070011325 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011326
11327 /* all I/Os are in process of being flushed */
11328 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart59c68ea2018-04-09 14:24:25 -070011329 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011330 return 0;
11331 }
11332 sum = 0;
11333
11334 for (i = 1; i <= phba->sli.last_iotag; i++) {
11335 iocbq = phba->sli.iocbq_lookup[i];
11336
11337 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11338 cmd) != 0)
11339 continue;
11340
11341 /*
11342 * If the iocbq is already being aborted, don't take a second
11343 * action, but do count it.
11344 */
11345 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11346 continue;
11347
11348 /* issue ABTS for this IOCB based on iotag */
11349 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11350 if (abtsiocbq == NULL)
11351 continue;
11352
11353 icmd = &iocbq->iocb;
11354 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11355 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11356 if (phba->sli_rev == LPFC_SLI_REV4)
11357 abtsiocbq->iocb.un.acxri.abortIoTag =
11358 iocbq->sli4_xritag;
11359 else
11360 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11361 abtsiocbq->iocb.ulpLe = 1;
11362 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11363 abtsiocbq->vport = vport;
11364
11365 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011366 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011367 if (iocbq->iocb_flag & LPFC_IO_FCP)
11368 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011369 if (iocbq->iocb_flag & LPFC_IO_FOF)
11370 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011371
James Smart8c50d252014-09-03 12:58:16 -040011372 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11373 ndlp = lpfc_cmd->rdata->pnode;
11374
11375 if (lpfc_is_link_up(phba) &&
11376 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011377 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11378 else
11379 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11380
11381 /* Setup callback routine and issue the command. */
11382 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11383
11384 /*
11385 * Indicate the IO is being aborted by the driver and set
11386 * the caller's flag into the aborted IO.
11387 */
11388 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11389
11390 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart59c68ea2018-04-09 14:24:25 -070011391 pring_s4 = lpfc_sli4_calc_ring(phba, abtsiocbq);
11392 if (!pring_s4)
James Smart895427b2017-02-12 13:52:30 -080011393 continue;
James Smart98912dda2014-04-04 13:52:31 -040011394 /* Note: both hbalock and ring_lock must be set here */
James Smart59c68ea2018-04-09 14:24:25 -070011395 spin_lock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011396 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11397 abtsiocbq, 0);
James Smart59c68ea2018-04-09 14:24:25 -070011398 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011399 } else {
11400 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11401 abtsiocbq, 0);
11402 }
11403
11404
11405 if (ret_val == IOCB_ERROR)
11406 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11407 else
11408 sum++;
11409 }
James Smart59c68ea2018-04-09 14:24:25 -070011410 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011411 return sum;
11412}
11413
11414/**
James Smart3621a712009-04-06 18:47:14 -040011415 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011416 * @phba: Pointer to HBA context object.
11417 * @cmdiocbq: Pointer to command iocb.
11418 * @rspiocbq: Pointer to response iocb.
11419 *
11420 * This function is the completion handler for iocbs issued using
11421 * lpfc_sli_issue_iocb_wait function. This function is called by the
11422 * ring event handler function without any lock held. This function
11423 * can be called from both worker thread context and interrupt
11424 * context. This function also can be called from other thread which
11425 * cleans up the SLI layer objects.
11426 * This function copy the contents of the response iocb to the
11427 * response iocb memory object provided by the caller of
11428 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11429 * sleeps for the iocb completion.
11430 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011431static void
11432lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11433 struct lpfc_iocbq *cmdiocbq,
11434 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011435{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011436 wait_queue_head_t *pdone_q;
11437 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050011438 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011439
James Smart2e0fef82007-06-17 19:56:36 -050011440 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011441 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11442
11443 /*
11444 * A time out has occurred for the iocb. If a time out
11445 * completion handler has been supplied, call it. Otherwise,
11446 * just free the iocbq.
11447 */
11448
11449 spin_unlock_irqrestore(&phba->hbalock, iflags);
11450 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11451 cmdiocbq->wait_iocb_cmpl = NULL;
11452 if (cmdiocbq->iocb_cmpl)
11453 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11454 else
11455 lpfc_sli_release_iocbq(phba, cmdiocbq);
11456 return;
11457 }
11458
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011459 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11460 if (cmdiocbq->context2 && rspiocbq)
11461 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11462 &rspiocbq->iocb, sizeof(IOCB_t));
11463
James Smart0f65ff62010-02-26 14:14:23 -050011464 /* Set the exchange busy flag for task management commands */
11465 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11466 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11467 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11468 cur_iocbq);
11469 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11470 }
11471
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011472 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011473 if (pdone_q)
11474 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011475 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011476 return;
11477}
11478
James Smarte59058c2008-08-24 21:49:00 -040011479/**
James Smartd11e31d2009-06-10 17:23:06 -040011480 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11481 * @phba: Pointer to HBA context object..
11482 * @piocbq: Pointer to command iocb.
11483 * @flag: Flag to test.
11484 *
11485 * This routine grabs the hbalock and then test the iocb_flag to
11486 * see if the passed in flag is set.
11487 * Returns:
11488 * 1 if flag is set.
11489 * 0 if flag is not set.
11490 **/
11491static int
11492lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11493 struct lpfc_iocbq *piocbq, uint32_t flag)
11494{
11495 unsigned long iflags;
11496 int ret;
11497
11498 spin_lock_irqsave(&phba->hbalock, iflags);
11499 ret = piocbq->iocb_flag & flag;
11500 spin_unlock_irqrestore(&phba->hbalock, iflags);
11501 return ret;
11502
11503}
11504
11505/**
James Smart3621a712009-04-06 18:47:14 -040011506 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011507 * @phba: Pointer to HBA context object..
11508 * @pring: Pointer to sli ring.
11509 * @piocb: Pointer to command iocb.
11510 * @prspiocbq: Pointer to response iocb.
11511 * @timeout: Timeout in number of seconds.
11512 *
11513 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011514 * iocb to complete. The iocb_cmpl field of the shall be used
11515 * to handle iocbs which time out. If the field is NULL, the
11516 * function shall free the iocbq structure. If more clean up is
11517 * needed, the caller is expected to provide a completion function
11518 * that will provide the needed clean up. If the iocb command is
11519 * not completed within timeout seconds, the function will either
11520 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11521 * completion function set in the iocb_cmpl field and then return
11522 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11523 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011524 * The function waits for the iocb completion using an
11525 * non-interruptible wait.
11526 * This function will sleep while waiting for iocb completion.
11527 * So, this function should not be called from any context which
11528 * does not allow sleeping. Due to the same reason, this function
11529 * cannot be called with interrupt disabled.
11530 * This function assumes that the iocb completions occur while
11531 * this function sleep. So, this function cannot be called from
11532 * the thread which process iocb completion for this ring.
11533 * This function clears the iocb_flag of the iocb object before
11534 * issuing the iocb and the iocb completion handler sets this
11535 * flag and wakes this thread when the iocb completes.
11536 * The contents of the response iocb will be copied to prspiocbq
11537 * by the completion handler when the command completes.
11538 * This function returns IOCB_SUCCESS when success.
11539 * This function is called with no lock held.
11540 **/
dea31012005-04-17 16:05:31 -050011541int
James Smart2e0fef82007-06-17 19:56:36 -050011542lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011543 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011544 struct lpfc_iocbq *piocb,
11545 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011546 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011547{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011548 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011549 long timeleft, timeout_req = 0;
11550 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011551 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011552 struct lpfc_iocbq *iocb;
11553 int txq_cnt = 0;
11554 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011555 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011556 unsigned long iflags;
11557 bool iocb_completed = true;
11558
James Smart895427b2017-02-12 13:52:30 -080011559 if (phba->sli_rev >= LPFC_SLI_REV4)
11560 pring = lpfc_sli4_calc_ring(phba, piocb);
11561 else
11562 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011563 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011564 * If the caller has provided a response iocbq buffer, then context2
11565 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011566 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011567 if (prspiocbq) {
11568 if (piocb->context2)
11569 return IOCB_ERROR;
11570 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011571 }
11572
James Smart5a0916b2013-07-15 18:31:42 -040011573 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011574 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11575 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011576 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011577
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011578 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011579 if (lpfc_readl(phba->HCregaddr, &creg_val))
11580 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011581 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11582 writel(creg_val, phba->HCregaddr);
11583 readl(phba->HCregaddr); /* flush */
11584 }
11585
James Smart2a9bf3d2010-06-07 15:24:45 -040011586 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11587 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011588 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011589 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011590 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011591 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011592 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011593 spin_lock_irqsave(&phba->hbalock, iflags);
11594 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011595
James Smart5a0916b2013-07-15 18:31:42 -040011596 /*
11597 * IOCB timed out. Inform the wake iocb wait
11598 * completion function and set local status
11599 */
11600
11601 iocb_completed = false;
11602 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11603 }
11604 spin_unlock_irqrestore(&phba->hbalock, iflags);
11605 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011606 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011607 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011608 /* Note: we are not indicating if the IOCB has a success
11609 * status or not - that's for the caller to check.
11610 * IOCB_SUCCESS means just that the command was sent and
11611 * completed. Not that it completed successfully.
11612 * */
James Smart7054a602007-04-25 09:52:34 -040011613 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011614 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011615 "0338 IOCB wait timeout error - no "
11616 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011617 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011618 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011619 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011620 "0330 IOCB wake NOT set, "
11621 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011622 timeout, (timeleft / jiffies));
11623 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011624 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011625 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011626 if (phba->cfg_log_verbose & LOG_SLI) {
11627 list_for_each_entry(iocb, &pring->txq, list) {
11628 txq_cnt++;
11629 }
11630 list_for_each_entry(iocb, &pring->txcmplq, list) {
11631 txcmplq_cnt++;
11632 }
11633 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11634 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11635 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11636 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011637 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011638 } else {
11639 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011640 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011641 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011642 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011643 }
11644
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011645 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011646 if (lpfc_readl(phba->HCregaddr, &creg_val))
11647 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011648 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11649 writel(creg_val, phba->HCregaddr);
11650 readl(phba->HCregaddr); /* flush */
11651 }
11652
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011653 if (prspiocbq)
11654 piocb->context2 = NULL;
11655
11656 piocb->context_un.wait_queue = NULL;
11657 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011658 return retval;
11659}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011660
James Smarte59058c2008-08-24 21:49:00 -040011661/**
James Smart3621a712009-04-06 18:47:14 -040011662 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011663 * @phba: Pointer to HBA context object.
11664 * @pmboxq: Pointer to driver mailbox object.
11665 * @timeout: Timeout in number of seconds.
11666 *
11667 * This function issues the mailbox to firmware and waits for the
11668 * mailbox command to complete. If the mailbox command is not
11669 * completed within timeout seconds, it returns MBX_TIMEOUT.
11670 * The function waits for the mailbox completion using an
11671 * interruptible wait. If the thread is woken up due to a
11672 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11673 * should not free the mailbox resources, if this function returns
11674 * MBX_TIMEOUT.
11675 * This function will sleep while waiting for mailbox completion.
11676 * So, this function should not be called from any context which
11677 * does not allow sleeping. Due to the same reason, this function
11678 * cannot be called with interrupt disabled.
11679 * This function assumes that the mailbox completion occurs while
11680 * this function sleep. So, this function cannot be called from
11681 * the worker thread which processes mailbox completion.
11682 * This function is called in the context of HBA management
11683 * applications.
11684 * This function returns MBX_SUCCESS when successful.
11685 * This function is called with no lock held.
11686 **/
dea31012005-04-17 16:05:31 -050011687int
James Smart2e0fef82007-06-17 19:56:36 -050011688lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011689 uint32_t timeout)
11690{
James Smarte29d74f2018-03-05 12:04:07 -080011691 struct completion mbox_done;
dea31012005-04-17 16:05:31 -050011692 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011693 unsigned long flag;
dea31012005-04-17 16:05:31 -050011694
James Smart495a7142008-06-14 22:52:59 -040011695 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011696 /* setup wake call as IOCB callback */
11697 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea31012005-04-17 16:05:31 -050011698
James Smarte29d74f2018-03-05 12:04:07 -080011699 /* setup context3 field to pass wait_queue pointer to wake function */
11700 init_completion(&mbox_done);
11701 pmboxq->context3 = &mbox_done;
dea31012005-04-17 16:05:31 -050011702 /* now issue the command */
11703 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011704 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smarte29d74f2018-03-05 12:04:07 -080011705 wait_for_completion_timeout(&mbox_done,
11706 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011707
James Smart858c9f62007-06-17 19:56:39 -050011708 spin_lock_irqsave(&phba->hbalock, flag);
James Smarte29d74f2018-03-05 12:04:07 -080011709 pmboxq->context3 = NULL;
James Smart7054a602007-04-25 09:52:34 -040011710 /*
11711 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11712 * else do not free the resources.
11713 */
James Smartd7c47992010-06-08 18:31:54 -040011714 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011715 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011716 } else {
James Smart7054a602007-04-25 09:52:34 -040011717 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011718 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11719 }
11720 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050011721 }
dea31012005-04-17 16:05:31 -050011722 return retval;
11723}
11724
James Smarte59058c2008-08-24 21:49:00 -040011725/**
James Smart3772a992009-05-22 14:50:54 -040011726 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011727 * @phba: Pointer to HBA context.
11728 *
James Smart3772a992009-05-22 14:50:54 -040011729 * This function is called to shutdown the driver's mailbox sub-system.
11730 * It first marks the mailbox sub-system is in a block state to prevent
11731 * the asynchronous mailbox command from issued off the pending mailbox
11732 * command queue. If the mailbox command sub-system shutdown is due to
11733 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11734 * the mailbox sub-system flush routine to forcefully bring down the
11735 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11736 * as with offline or HBA function reset), this routine will wait for the
11737 * outstanding mailbox command to complete before invoking the mailbox
11738 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040011739 **/
James Smart3772a992009-05-22 14:50:54 -040011740void
James Smart618a5232012-06-12 13:54:36 -040011741lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040011742{
James Smart3772a992009-05-22 14:50:54 -040011743 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040011744 unsigned long timeout;
11745
James Smart618a5232012-06-12 13:54:36 -040011746 if (mbx_action == LPFC_MBX_NO_WAIT) {
11747 /* delay 100ms for port state */
11748 msleep(100);
11749 lpfc_sli_mbox_sys_flush(phba);
11750 return;
11751 }
James Smarta183a152011-10-10 21:32:43 -040011752 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050011753
James Smart3772a992009-05-22 14:50:54 -040011754 spin_lock_irq(&phba->hbalock);
11755 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040011756
11757 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040011758 /* Determine how long we might wait for the active mailbox
11759 * command to be gracefully completed by firmware.
11760 */
James Smarta183a152011-10-10 21:32:43 -040011761 if (phba->sli.mbox_active)
11762 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11763 phba->sli.mbox_active) *
11764 1000) + jiffies;
11765 spin_unlock_irq(&phba->hbalock);
11766
James Smart3772a992009-05-22 14:50:54 -040011767 while (phba->sli.mbox_active) {
11768 /* Check active mailbox complete status every 2ms */
11769 msleep(2);
11770 if (time_after(jiffies, timeout))
11771 /* Timeout, let the mailbox flush routine to
11772 * forcefully release active mailbox command
11773 */
11774 break;
11775 }
James Smartd7069f02012-03-01 22:36:29 -050011776 } else
11777 spin_unlock_irq(&phba->hbalock);
11778
James Smart3772a992009-05-22 14:50:54 -040011779 lpfc_sli_mbox_sys_flush(phba);
11780}
11781
11782/**
11783 * lpfc_sli_eratt_read - read sli-3 error attention events
11784 * @phba: Pointer to HBA context.
11785 *
11786 * This function is called to read the SLI3 device error attention registers
11787 * for possible error attention events. The caller must hold the hostlock
11788 * with spin_lock_irq().
11789 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011790 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040011791 * Register and returns 0 otherwise.
11792 **/
11793static int
11794lpfc_sli_eratt_read(struct lpfc_hba *phba)
11795{
James Smarted957682007-06-17 19:56:37 -050011796 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040011797
James Smart3772a992009-05-22 14:50:54 -040011798 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050011799 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11800 goto unplug_err;
11801
James Smart3772a992009-05-22 14:50:54 -040011802 if (ha_copy & HA_ERATT) {
11803 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050011804 if (lpfc_sli_read_hs(phba))
11805 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040011806
James Smart3772a992009-05-22 14:50:54 -040011807 /* Check if there is a deferred error condition is active */
11808 if ((HS_FFER1 & phba->work_hs) &&
11809 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011810 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040011811 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040011812 /* Clear all interrupt enable conditions */
11813 writel(0, phba->HCregaddr);
11814 readl(phba->HCregaddr);
11815 }
11816
11817 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040011818 phba->work_ha |= HA_ERATT;
11819 /* Indicate polling handles this ERATT */
11820 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011821 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011822 }
James Smart3772a992009-05-22 14:50:54 -040011823 return 0;
James Smart9940b972011-03-11 16:06:12 -050011824
11825unplug_err:
11826 /* Set the driver HS work bitmap */
11827 phba->work_hs |= UNPLUG_ERR;
11828 /* Set the driver HA work bitmap */
11829 phba->work_ha |= HA_ERATT;
11830 /* Indicate polling handles this ERATT */
11831 phba->hba_flag |= HBA_ERATT_HANDLED;
11832 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011833}
11834
James Smarte59058c2008-08-24 21:49:00 -040011835/**
James Smartda0436e2009-05-22 14:51:39 -040011836 * lpfc_sli4_eratt_read - read sli-4 error attention events
11837 * @phba: Pointer to HBA context.
11838 *
11839 * This function is called to read the SLI4 device error attention registers
11840 * for possible error attention events. The caller must hold the hostlock
11841 * with spin_lock_irq().
11842 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011843 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040011844 * Register and returns 0 otherwise.
11845 **/
11846static int
11847lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11848{
11849 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050011850 uint32_t if_type, portsmphr;
11851 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040011852
James Smart2fcee4b2010-12-15 17:57:46 -050011853 /*
11854 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040011855 * registers for error attention. This can be changed later.
11856 */
James Smart2fcee4b2010-12-15 17:57:46 -050011857 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11858 switch (if_type) {
11859 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050011860 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11861 &uerr_sta_lo) ||
11862 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11863 &uerr_sta_hi)) {
11864 phba->work_hs |= UNPLUG_ERR;
11865 phba->work_ha |= HA_ERATT;
11866 phba->hba_flag |= HBA_ERATT_HANDLED;
11867 return 1;
11868 }
James Smart2fcee4b2010-12-15 17:57:46 -050011869 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11870 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11872 "1423 HBA Unrecoverable error: "
11873 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11874 "ue_mask_lo_reg=0x%x, "
11875 "ue_mask_hi_reg=0x%x\n",
11876 uerr_sta_lo, uerr_sta_hi,
11877 phba->sli4_hba.ue_mask_lo,
11878 phba->sli4_hba.ue_mask_hi);
11879 phba->work_status[0] = uerr_sta_lo;
11880 phba->work_status[1] = uerr_sta_hi;
11881 phba->work_ha |= HA_ERATT;
11882 phba->hba_flag |= HBA_ERATT_HANDLED;
11883 return 1;
11884 }
11885 break;
11886 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080011887 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050011888 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11889 &portstat_reg.word0) ||
11890 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11891 &portsmphr)){
11892 phba->work_hs |= UNPLUG_ERR;
11893 phba->work_ha |= HA_ERATT;
11894 phba->hba_flag |= HBA_ERATT_HANDLED;
11895 return 1;
11896 }
James Smart2fcee4b2010-12-15 17:57:46 -050011897 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11898 phba->work_status[0] =
11899 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11900 phba->work_status[1] =
11901 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11902 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011903 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011904 "port status reg 0x%x, "
11905 "port smphr reg 0x%x, "
11906 "error 1=0x%x, error 2=0x%x\n",
11907 portstat_reg.word0,
11908 portsmphr,
11909 phba->work_status[0],
11910 phba->work_status[1]);
11911 phba->work_ha |= HA_ERATT;
11912 phba->hba_flag |= HBA_ERATT_HANDLED;
11913 return 1;
11914 }
11915 break;
11916 case LPFC_SLI_INTF_IF_TYPE_1:
11917 default:
James Smarta747c9c2009-11-18 15:41:10 -050011918 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011919 "2886 HBA Error Attention on unsupported "
11920 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011921 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011922 }
James Smart2fcee4b2010-12-15 17:57:46 -050011923
James Smartda0436e2009-05-22 14:51:39 -040011924 return 0;
11925}
11926
11927/**
James Smart3621a712009-04-06 18:47:14 -040011928 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011929 * @phba: Pointer to HBA context.
11930 *
James Smart3772a992009-05-22 14:50:54 -040011931 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011932 * error attention register bit for error attention events.
11933 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011934 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011935 * Register and returns 0 otherwise.
11936 **/
11937int
11938lpfc_sli_check_eratt(struct lpfc_hba *phba)
11939{
11940 uint32_t ha_copy;
11941
11942 /* If somebody is waiting to handle an eratt, don't process it
11943 * here. The brdkill function will do this.
11944 */
11945 if (phba->link_flag & LS_IGNORE_ERATT)
11946 return 0;
11947
11948 /* Check if interrupt handler handles this ERATT */
11949 spin_lock_irq(&phba->hbalock);
11950 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11951 /* Interrupt handler has handled ERATT */
11952 spin_unlock_irq(&phba->hbalock);
11953 return 0;
11954 }
11955
James Smarta257bf92009-04-06 18:48:10 -040011956 /*
11957 * If there is deferred error attention, do not check for error
11958 * attention
11959 */
11960 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11961 spin_unlock_irq(&phba->hbalock);
11962 return 0;
11963 }
11964
James Smart3772a992009-05-22 14:50:54 -040011965 /* If PCI channel is offline, don't process it */
11966 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011967 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011968 return 0;
11969 }
11970
11971 switch (phba->sli_rev) {
11972 case LPFC_SLI_REV2:
11973 case LPFC_SLI_REV3:
11974 /* Read chip Host Attention (HA) register */
11975 ha_copy = lpfc_sli_eratt_read(phba);
11976 break;
James Smartda0436e2009-05-22 14:51:39 -040011977 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011978 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011979 ha_copy = lpfc_sli4_eratt_read(phba);
11980 break;
James Smart3772a992009-05-22 14:50:54 -040011981 default:
11982 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11983 "0299 Invalid SLI revision (%d)\n",
11984 phba->sli_rev);
11985 ha_copy = 0;
11986 break;
James Smart93996272008-08-24 21:50:30 -040011987 }
11988 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011989
11990 return ha_copy;
11991}
11992
11993/**
11994 * lpfc_intr_state_check - Check device state for interrupt handling
11995 * @phba: Pointer to HBA context.
11996 *
11997 * This inline routine checks whether a device or its PCI slot is in a state
11998 * that the interrupt should be handled.
11999 *
12000 * This function returns 0 if the device or the PCI slot is in a state that
12001 * interrupt should be handled, otherwise -EIO.
12002 */
12003static inline int
12004lpfc_intr_state_check(struct lpfc_hba *phba)
12005{
12006 /* If the pci channel is offline, ignore all the interrupts */
12007 if (unlikely(pci_channel_offline(phba->pcidev)))
12008 return -EIO;
12009
12010 /* Update device level interrupt statistics */
12011 phba->sli.slistat.sli_intr++;
12012
12013 /* Ignore all interrupts during initialization. */
12014 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12015 return -EIO;
12016
James Smart93996272008-08-24 21:50:30 -040012017 return 0;
12018}
12019
12020/**
James Smart3772a992009-05-22 14:50:54 -040012021 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040012022 * @irq: Interrupt number.
12023 * @dev_id: The device context pointer.
12024 *
James Smart93996272008-08-24 21:50:30 -040012025 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012026 * service routine when device with SLI-3 interface spec is enabled with
12027 * MSI-X multi-message interrupt mode and there are slow-path events in
12028 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12029 * interrupt mode, this function is called as part of the device-level
12030 * interrupt handler. When the PCI slot is in error recovery or the HBA
12031 * is undergoing initialization, the interrupt handler will not process
12032 * the interrupt. The link attention and ELS ring attention events are
12033 * handled by the worker thread. The interrupt handler signals the worker
12034 * thread and returns for these events. This function is called without
12035 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040012036 * structures.
12037 *
12038 * This function returns IRQ_HANDLED when interrupt is handled else it
12039 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040012040 **/
dea31012005-04-17 16:05:31 -050012041irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012042lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050012043{
James Smart2e0fef82007-06-17 19:56:36 -050012044 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050012045 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050012046 uint32_t work_ha_copy;
12047 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012048 unsigned long iflag;
dea31012005-04-17 16:05:31 -050012049 uint32_t control;
12050
James Smart92d7f7b2007-06-17 19:56:38 -050012051 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050012052 struct lpfc_vport *vport;
12053 struct lpfc_nodelist *ndlp;
12054 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050012055 LPFC_MBOXQ_t *pmb;
12056 int rc;
12057
dea31012005-04-17 16:05:31 -050012058 /*
12059 * Get the driver's phba structure from the dev_id and
12060 * assume the HBA is not interrupting.
12061 */
James Smart93996272008-08-24 21:50:30 -040012062 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050012063
12064 if (unlikely(!phba))
12065 return IRQ_NONE;
12066
dea31012005-04-17 16:05:31 -050012067 /*
James Smart93996272008-08-24 21:50:30 -040012068 * Stuff needs to be attented to when this function is invoked as an
12069 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012070 */
James Smart93996272008-08-24 21:50:30 -040012071 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012072 /* Check device state for handling interrupt */
12073 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012074 return IRQ_NONE;
12075 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050012076 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012077 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12078 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040012079 /* If somebody is waiting to handle an eratt don't process it
12080 * here. The brdkill function will do this.
12081 */
12082 if (phba->link_flag & LS_IGNORE_ERATT)
12083 ha_copy &= ~HA_ERATT;
12084 /* Check the need for handling ERATT in interrupt handler */
12085 if (ha_copy & HA_ERATT) {
12086 if (phba->hba_flag & HBA_ERATT_HANDLED)
12087 /* ERATT polling has handled ERATT */
12088 ha_copy &= ~HA_ERATT;
12089 else
12090 /* Indicate interrupt handler handles ERATT */
12091 phba->hba_flag |= HBA_ERATT_HANDLED;
12092 }
James Smarta257bf92009-04-06 18:48:10 -040012093
12094 /*
12095 * If there is deferred error attention, do not check for any
12096 * interrupt.
12097 */
12098 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012099 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012100 return IRQ_NONE;
12101 }
12102
James Smart93996272008-08-24 21:50:30 -040012103 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050012104 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12105 goto unplug_error;
12106
James Smarta747c9c2009-11-18 15:41:10 -050012107 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12108 HC_LAINT_ENA | HC_ERINT_ENA),
12109 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012110 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12111 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012112 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012113 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012114 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012115 } else
12116 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050012117
dea31012005-04-17 16:05:31 -050012118 work_ha_copy = ha_copy & phba->work_ha_mask;
12119
James Smart93996272008-08-24 21:50:30 -040012120 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050012121 if (work_ha_copy & HA_LATT) {
12122 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12123 /*
12124 * Turn off Link Attention interrupts
12125 * until CLEAR_LA done
12126 */
James Smart5b75da22008-12-04 22:39:35 -050012127 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012128 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050012129 if (lpfc_readl(phba->HCregaddr, &control))
12130 goto unplug_error;
dea31012005-04-17 16:05:31 -050012131 control &= ~HC_LAINT_ENA;
12132 writel(control, phba->HCregaddr);
12133 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012134 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012135 }
12136 else
12137 work_ha_copy &= ~HA_LATT;
12138 }
12139
James Smart93996272008-08-24 21:50:30 -040012140 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050012141 /*
12142 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12143 * the only slow ring.
12144 */
12145 status = (work_ha_copy &
12146 (HA_RXMASK << (4*LPFC_ELS_RING)));
12147 status >>= (4*LPFC_ELS_RING);
12148 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050012149 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012150 if (lpfc_readl(phba->HCregaddr, &control))
12151 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040012152
12153 lpfc_debugfs_slow_ring_trc(phba,
12154 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12155 control, status,
12156 (uint32_t)phba->sli.slistat.sli_intr);
12157
James Smart858c9f62007-06-17 19:56:39 -050012158 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040012159 lpfc_debugfs_slow_ring_trc(phba,
12160 "ISR Disable ring:"
12161 "pwork:x%x hawork:x%x wait:x%x",
12162 phba->work_ha, work_ha_copy,
12163 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012164 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012165
James Smart858c9f62007-06-17 19:56:39 -050012166 control &=
12167 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050012168 writel(control, phba->HCregaddr);
12169 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050012170 }
James Smarta58cbd52007-08-02 11:09:43 -040012171 else {
12172 lpfc_debugfs_slow_ring_trc(phba,
12173 "ISR slow ring: pwork:"
12174 "x%x hawork:x%x wait:x%x",
12175 phba->work_ha, work_ha_copy,
12176 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012177 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012178 }
James Smart5b75da22008-12-04 22:39:35 -050012179 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012180 }
12181 }
James Smart5b75da22008-12-04 22:39:35 -050012182 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012183 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050012184 if (lpfc_sli_read_hs(phba))
12185 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040012186 /*
12187 * Check if there is a deferred error condition
12188 * is active
12189 */
12190 if ((HS_FFER1 & phba->work_hs) &&
12191 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012192 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12193 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040012194 phba->hba_flag |= DEFER_ERATT;
12195 /* Clear all interrupt enable conditions */
12196 writel(0, phba->HCregaddr);
12197 readl(phba->HCregaddr);
12198 }
12199 }
12200
James Smart93996272008-08-24 21:50:30 -040012201 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050012202 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040012203 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040012204 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050012205 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050012206
12207 /* First check out the status word */
12208 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12209 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050012210 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050012211 /*
12212 * Stray Mailbox Interrupt, mbxCommand <cmd>
12213 * mbxStatus <status>
12214 */
James Smart09372822008-01-11 01:52:54 -050012215 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012216 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012217 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012218 "Interrupt mbxCommand x%x "
12219 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012220 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012221 pmbox->mbxCommand,
12222 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012223 /* clear mailbox attention bit */
12224 work_ha_copy &= ~HA_MBATT;
12225 } else {
James Smart97eab632008-04-07 10:16:05 -040012226 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012227 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012228 phba->last_completion_time = jiffies;
12229 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012230 if (pmb->mbox_cmpl) {
12231 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12232 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012233 if (pmb->out_ext_byte_len &&
12234 pmb->context2)
12235 lpfc_sli_pcimem_bcopy(
12236 phba->mbox_ext,
12237 pmb->context2,
12238 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012239 }
James Smart09372822008-01-11 01:52:54 -050012240 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12241 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12242
12243 lpfc_debugfs_disc_trc(vport,
12244 LPFC_DISC_TRC_MBOX_VPORT,
12245 "MBOX dflt rpi: : "
12246 "status:x%x rpi:x%x",
12247 (uint32_t)pmbox->mbxStatus,
12248 pmbox->un.varWords[0], 0);
12249
12250 if (!pmbox->mbxStatus) {
12251 mp = (struct lpfc_dmabuf *)
12252 (pmb->context1);
12253 ndlp = (struct lpfc_nodelist *)
12254 pmb->context2;
12255
12256 /* Reg_LOGIN of dflt RPI was
12257 * successful. new lets get
12258 * rid of the RPI using the
12259 * same mbox buffer.
12260 */
12261 lpfc_unreg_login(phba,
12262 vport->vpi,
12263 pmbox->un.varWords[0],
12264 pmb);
12265 pmb->mbox_cmpl =
12266 lpfc_mbx_cmpl_dflt_rpi;
12267 pmb->context1 = mp;
12268 pmb->context2 = ndlp;
12269 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012270 rc = lpfc_sli_issue_mbox(phba,
12271 pmb,
12272 MBX_NOWAIT);
12273 if (rc != MBX_BUSY)
12274 lpfc_printf_log(phba,
12275 KERN_ERR,
12276 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012277 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012278 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012279 if (rc != MBX_NOT_FINISHED)
12280 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012281 }
12282 }
James Smart5b75da22008-12-04 22:39:35 -050012283 spin_lock_irqsave(
12284 &phba->pport->work_port_lock,
12285 iflag);
James Smart09372822008-01-11 01:52:54 -050012286 phba->pport->work_port_events &=
12287 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012288 spin_unlock_irqrestore(
12289 &phba->pport->work_port_lock,
12290 iflag);
James Smart09372822008-01-11 01:52:54 -050012291 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012292 }
James Smart97eab632008-04-07 10:16:05 -040012293 } else
James Smart5b75da22008-12-04 22:39:35 -050012294 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012295
James Smart92d7f7b2007-06-17 19:56:38 -050012296 if ((work_ha_copy & HA_MBATT) &&
12297 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012298send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012299 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012300 do {
12301 rc = lpfc_sli_issue_mbox(phba, NULL,
12302 MBX_NOWAIT);
12303 } while (rc == MBX_NOT_FINISHED);
12304 if (rc != MBX_SUCCESS)
12305 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12306 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012307 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012308 }
12309
James Smart5b75da22008-12-04 22:39:35 -050012310 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012311 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012312 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012313 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012314 }
James Smart93996272008-08-24 21:50:30 -040012315 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012316unplug_error:
12317 spin_unlock_irqrestore(&phba->hbalock, iflag);
12318 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012319
James Smart3772a992009-05-22 14:50:54 -040012320} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012321
12322/**
James Smart3772a992009-05-22 14:50:54 -040012323 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012324 * @irq: Interrupt number.
12325 * @dev_id: The device context pointer.
12326 *
12327 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012328 * service routine when device with SLI-3 interface spec is enabled with
12329 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12330 * ring event in the HBA. However, when the device is enabled with either
12331 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12332 * device-level interrupt handler. When the PCI slot is in error recovery
12333 * or the HBA is undergoing initialization, the interrupt handler will not
12334 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12335 * the intrrupt context. This function is called without any lock held.
12336 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012337 *
12338 * This function returns IRQ_HANDLED when interrupt is handled else it
12339 * returns IRQ_NONE.
12340 **/
12341irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012342lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012343{
12344 struct lpfc_hba *phba;
12345 uint32_t ha_copy;
12346 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012347 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012348 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012349
12350 /* Get the driver's phba structure from the dev_id and
12351 * assume the HBA is not interrupting.
12352 */
12353 phba = (struct lpfc_hba *) dev_id;
12354
12355 if (unlikely(!phba))
12356 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012357
12358 /*
James Smart93996272008-08-24 21:50:30 -040012359 * Stuff needs to be attented to when this function is invoked as an
12360 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012361 */
James Smart93996272008-08-24 21:50:30 -040012362 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012363 /* Check device state for handling interrupt */
12364 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012365 return IRQ_NONE;
12366 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012367 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12368 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012369 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012370 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012371 /*
12372 * If there is deferred error attention, do not check for
12373 * any interrupt.
12374 */
12375 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012376 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012377 return IRQ_NONE;
12378 }
James Smart93996272008-08-24 21:50:30 -040012379 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12380 phba->HAregaddr);
12381 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012382 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012383 } else
12384 ha_copy = phba->ha_copy;
12385
12386 /*
12387 * Process all events on FCP ring. Take the optimized path for FCP IO.
12388 */
12389 ha_copy &= ~(phba->work_ha_mask);
12390
12391 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012392 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012393 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012394 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012395 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012396
12397 if (phba->cfg_multi_ring_support == 2) {
12398 /*
James Smart93996272008-08-24 21:50:30 -040012399 * Process all events on extra ring. Take the optimized path
12400 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012401 */
James Smart93996272008-08-24 21:50:30 -040012402 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012403 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012404 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012405 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012406 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012407 status);
12408 }
12409 }
dea31012005-04-17 16:05:31 -050012410 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012411} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012412
James Smart93996272008-08-24 21:50:30 -040012413/**
James Smart3772a992009-05-22 14:50:54 -040012414 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012415 * @irq: Interrupt number.
12416 * @dev_id: The device context pointer.
12417 *
James Smart3772a992009-05-22 14:50:54 -040012418 * This function is the HBA device-level interrupt handler to device with
12419 * SLI-3 interface spec, called from the PCI layer when either MSI or
12420 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12421 * requires driver attention. This function invokes the slow-path interrupt
12422 * attention handling function and fast-path interrupt attention handling
12423 * function in turn to process the relevant HBA attention events. This
12424 * function is called without any lock held. It gets the hbalock to access
12425 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012426 *
12427 * This function returns IRQ_HANDLED when interrupt is handled, else it
12428 * returns IRQ_NONE.
12429 **/
12430irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012431lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012432{
12433 struct lpfc_hba *phba;
12434 irqreturn_t sp_irq_rc, fp_irq_rc;
12435 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012436 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012437
12438 /*
12439 * Get the driver's phba structure from the dev_id and
12440 * assume the HBA is not interrupting.
12441 */
12442 phba = (struct lpfc_hba *) dev_id;
12443
12444 if (unlikely(!phba))
12445 return IRQ_NONE;
12446
James Smart3772a992009-05-22 14:50:54 -040012447 /* Check device state for handling interrupt */
12448 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012449 return IRQ_NONE;
12450
12451 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012452 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12453 spin_unlock(&phba->hbalock);
12454 return IRQ_HANDLED;
12455 }
12456
James Smart93996272008-08-24 21:50:30 -040012457 if (unlikely(!phba->ha_copy)) {
12458 spin_unlock(&phba->hbalock);
12459 return IRQ_NONE;
12460 } else if (phba->ha_copy & HA_ERATT) {
12461 if (phba->hba_flag & HBA_ERATT_HANDLED)
12462 /* ERATT polling has handled ERATT */
12463 phba->ha_copy &= ~HA_ERATT;
12464 else
12465 /* Indicate interrupt handler handles ERATT */
12466 phba->hba_flag |= HBA_ERATT_HANDLED;
12467 }
12468
James Smarta257bf92009-04-06 18:48:10 -040012469 /*
12470 * If there is deferred error attention, do not check for any interrupt.
12471 */
12472 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012473 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012474 return IRQ_NONE;
12475 }
12476
James Smart93996272008-08-24 21:50:30 -040012477 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012478 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12479 spin_unlock(&phba->hbalock);
12480 return IRQ_HANDLED;
12481 }
James Smarta747c9c2009-11-18 15:41:10 -050012482 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12483 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12484 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012485 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012486 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012487 readl(phba->HAregaddr); /* flush */
12488 spin_unlock(&phba->hbalock);
12489
12490 /*
12491 * Invokes slow-path host attention interrupt handling as appropriate.
12492 */
12493
12494 /* status of events with mailbox and link attention */
12495 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12496
12497 /* status of events with ELS ring */
12498 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12499 status2 >>= (4*LPFC_ELS_RING);
12500
12501 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012502 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012503 else
12504 sp_irq_rc = IRQ_NONE;
12505
12506 /*
12507 * Invoke fast-path host attention interrupt handling as appropriate.
12508 */
12509
12510 /* status of events with FCP ring */
12511 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12512 status1 >>= (4*LPFC_FCP_RING);
12513
12514 /* status of events with extra ring */
12515 if (phba->cfg_multi_ring_support == 2) {
12516 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12517 status2 >>= (4*LPFC_EXTRA_RING);
12518 } else
12519 status2 = 0;
12520
12521 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012522 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012523 else
12524 fp_irq_rc = IRQ_NONE;
12525
12526 /* Return device-level interrupt handling status */
12527 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012528} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012529
12530/**
12531 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12532 * @phba: pointer to lpfc hba data structure.
12533 *
12534 * This routine is invoked by the worker thread to process all the pending
12535 * SLI4 FCP abort XRI events.
12536 **/
12537void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12538{
12539 struct lpfc_cq_event *cq_event;
12540
12541 /* First, declare the fcp xri abort event has been handled */
12542 spin_lock_irq(&phba->hbalock);
12543 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12544 spin_unlock_irq(&phba->hbalock);
12545 /* Now, handle all the fcp xri abort events */
12546 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12547 /* Get the first event from the head of the event queue */
12548 spin_lock_irq(&phba->hbalock);
12549 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12550 cq_event, struct lpfc_cq_event, list);
12551 spin_unlock_irq(&phba->hbalock);
12552 /* Notify aborted XRI for FCP work queue */
12553 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12554 /* Free the event processed back to the free pool */
12555 lpfc_sli4_cq_event_release(phba, cq_event);
12556 }
12557}
12558
12559/**
12560 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12561 * @phba: pointer to lpfc hba data structure.
12562 *
12563 * This routine is invoked by the worker thread to process all the pending
12564 * SLI4 els abort xri events.
12565 **/
12566void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12567{
12568 struct lpfc_cq_event *cq_event;
12569
12570 /* First, declare the els xri abort event has been handled */
12571 spin_lock_irq(&phba->hbalock);
12572 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12573 spin_unlock_irq(&phba->hbalock);
12574 /* Now, handle all the els xri abort events */
12575 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12576 /* Get the first event from the head of the event queue */
12577 spin_lock_irq(&phba->hbalock);
12578 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12579 cq_event, struct lpfc_cq_event, list);
12580 spin_unlock_irq(&phba->hbalock);
12581 /* Notify aborted XRI for ELS work queue */
12582 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12583 /* Free the event processed back to the free pool */
12584 lpfc_sli4_cq_event_release(phba, cq_event);
12585 }
12586}
12587
James Smart341af102010-01-26 23:07:37 -050012588/**
12589 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12590 * @phba: pointer to lpfc hba data structure
12591 * @pIocbIn: pointer to the rspiocbq
12592 * @pIocbOut: pointer to the cmdiocbq
12593 * @wcqe: pointer to the complete wcqe
12594 *
12595 * This routine transfers the fields of a command iocbq to a response iocbq
12596 * by copying all the IOCB fields from command iocbq and transferring the
12597 * completion status information from the complete wcqe.
12598 **/
James Smart4f774512009-05-22 14:52:35 -040012599static void
James Smart341af102010-01-26 23:07:37 -050012600lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12601 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012602 struct lpfc_iocbq *pIocbOut,
12603 struct lpfc_wcqe_complete *wcqe)
12604{
James Smartaf227412013-10-10 12:23:10 -040012605 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012606 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012607 uint32_t status, max_response;
12608 struct lpfc_dmabuf *dmabuf;
12609 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012610 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12611
12612 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12613 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012614 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012615 status = bf_get(lpfc_wcqe_c_status, wcqe);
12616 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012617 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12618 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12619 pIocbIn->iocb.un.fcpi.fcpi_parm =
12620 pIocbOut->iocb.un.fcpi.fcpi_parm -
12621 wcqe->total_data_placed;
12622 else
12623 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012624 else {
James Smart4f774512009-05-22 14:52:35 -040012625 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012626 switch (pIocbOut->iocb.ulpCommand) {
12627 case CMD_ELS_REQUEST64_CR:
12628 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12629 bpl = (struct ulp_bde64 *)dmabuf->virt;
12630 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12631 max_response = bde.tus.f.bdeSize;
12632 break;
12633 case CMD_GEN_REQUEST64_CR:
12634 max_response = 0;
12635 if (!pIocbOut->context3)
12636 break;
12637 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12638 sizeof(struct ulp_bde64);
12639 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12640 bpl = (struct ulp_bde64 *)dmabuf->virt;
12641 for (i = 0; i < numBdes; i++) {
12642 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12643 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12644 max_response += bde.tus.f.bdeSize;
12645 }
12646 break;
12647 default:
12648 max_response = wcqe->total_data_placed;
12649 break;
12650 }
12651 if (max_response < wcqe->total_data_placed)
12652 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12653 else
12654 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12655 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012656 }
James Smart341af102010-01-26 23:07:37 -050012657
James Smartacd68592012-01-18 16:25:09 -050012658 /* Convert BG errors for completion status */
12659 if (status == CQE_STATUS_DI_ERROR) {
12660 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12661
12662 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12663 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12664 else
12665 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12666
12667 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12668 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12669 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12670 BGS_GUARD_ERR_MASK;
12671 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12672 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12673 BGS_APPTAG_ERR_MASK;
12674 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12675 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12676 BGS_REFTAG_ERR_MASK;
12677
12678 /* Check to see if there was any good data before the error */
12679 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12680 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12681 BGS_HI_WATER_MARK_PRESENT_MASK;
12682 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12683 wcqe->total_data_placed;
12684 }
12685
12686 /*
12687 * Set ALL the error bits to indicate we don't know what
12688 * type of error it is.
12689 */
12690 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12691 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12692 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12693 BGS_GUARD_ERR_MASK);
12694 }
12695
James Smart341af102010-01-26 23:07:37 -050012696 /* Pick up HBA exchange busy condition */
12697 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12698 spin_lock_irqsave(&phba->hbalock, iflags);
12699 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12700 spin_unlock_irqrestore(&phba->hbalock, iflags);
12701 }
James Smart4f774512009-05-22 14:52:35 -040012702}
12703
12704/**
James Smart45ed1192009-10-02 15:17:02 -040012705 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12706 * @phba: Pointer to HBA context object.
12707 * @wcqe: Pointer to work-queue completion queue entry.
12708 *
12709 * This routine handles an ELS work-queue completion event and construct
12710 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12711 * discovery engine to handle.
12712 *
12713 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12714 **/
12715static struct lpfc_iocbq *
12716lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12717 struct lpfc_iocbq *irspiocbq)
12718{
James Smart895427b2017-02-12 13:52:30 -080012719 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012720 struct lpfc_iocbq *cmdiocbq;
12721 struct lpfc_wcqe_complete *wcqe;
12722 unsigned long iflags;
12723
James Smart895427b2017-02-12 13:52:30 -080012724 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070012725 if (unlikely(!pring))
12726 return NULL;
James Smart895427b2017-02-12 13:52:30 -080012727
James Smart45ed1192009-10-02 15:17:02 -040012728 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012729 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012730 pring->stats.iocb_event++;
12731 /* Look up the ELS command IOCB and create pseudo response IOCB */
12732 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12733 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040012734 if (unlikely(!cmdiocbq)) {
Dick Kennedy401bb412017-09-29 17:34:28 -070012735 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012736 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12737 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070012738 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12739 wcqe->word0, wcqe->total_data_placed,
12740 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040012741 lpfc_sli_release_iocbq(phba, irspiocbq);
12742 return NULL;
12743 }
12744
Dick Kennedy401bb412017-09-29 17:34:28 -070012745 /* Put the iocb back on the txcmplq */
12746 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12747 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12748
James Smart45ed1192009-10-02 15:17:02 -040012749 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012750 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040012751
12752 return irspiocbq;
12753}
12754
James Smart8a5ca102017-11-20 16:00:30 -080012755inline struct lpfc_cq_event *
12756lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12757{
12758 struct lpfc_cq_event *cq_event;
12759
12760 /* Allocate a new internal CQ_EVENT entry */
12761 cq_event = lpfc_sli4_cq_event_alloc(phba);
12762 if (!cq_event) {
12763 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12764 "0602 Failed to alloc CQ_EVENT entry\n");
12765 return NULL;
12766 }
12767
12768 /* Move the CQE into the event */
12769 memcpy(&cq_event->cqe, entry, size);
12770 return cq_event;
12771}
12772
James Smart45ed1192009-10-02 15:17:02 -040012773/**
James Smart04c68492009-05-22 14:52:52 -040012774 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12775 * @phba: Pointer to HBA context object.
12776 * @cqe: Pointer to mailbox completion queue entry.
12777 *
12778 * This routine process a mailbox completion queue entry with asynchrous
12779 * event.
12780 *
12781 * Return: true if work posted to worker thread, otherwise false.
12782 **/
12783static bool
12784lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12785{
12786 struct lpfc_cq_event *cq_event;
12787 unsigned long iflags;
12788
12789 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12790 "0392 Async Event: word0:x%x, word1:x%x, "
12791 "word2:x%x, word3:x%x\n", mcqe->word0,
12792 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12793
James Smart8a5ca102017-11-20 16:00:30 -080012794 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12795 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040012796 return false;
James Smart04c68492009-05-22 14:52:52 -040012797 spin_lock_irqsave(&phba->hbalock, iflags);
12798 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12799 /* Set the async event flag */
12800 phba->hba_flag |= ASYNC_EVENT;
12801 spin_unlock_irqrestore(&phba->hbalock, iflags);
12802
12803 return true;
12804}
12805
12806/**
12807 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12808 * @phba: Pointer to HBA context object.
12809 * @cqe: Pointer to mailbox completion queue entry.
12810 *
12811 * This routine process a mailbox completion queue entry with mailbox
12812 * completion event.
12813 *
12814 * Return: true if work posted to worker thread, otherwise false.
12815 **/
12816static bool
12817lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12818{
12819 uint32_t mcqe_status;
12820 MAILBOX_t *mbox, *pmbox;
12821 struct lpfc_mqe *mqe;
12822 struct lpfc_vport *vport;
12823 struct lpfc_nodelist *ndlp;
12824 struct lpfc_dmabuf *mp;
12825 unsigned long iflags;
12826 LPFC_MBOXQ_t *pmb;
12827 bool workposted = false;
12828 int rc;
12829
12830 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12831 if (!bf_get(lpfc_trailer_completed, mcqe))
12832 goto out_no_mqe_complete;
12833
12834 /* Get the reference to the active mbox command */
12835 spin_lock_irqsave(&phba->hbalock, iflags);
12836 pmb = phba->sli.mbox_active;
12837 if (unlikely(!pmb)) {
12838 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12839 "1832 No pending MBOX command to handle\n");
12840 spin_unlock_irqrestore(&phba->hbalock, iflags);
12841 goto out_no_mqe_complete;
12842 }
12843 spin_unlock_irqrestore(&phba->hbalock, iflags);
12844 mqe = &pmb->u.mqe;
12845 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12846 mbox = phba->mbox;
12847 vport = pmb->vport;
12848
12849 /* Reset heartbeat timer */
12850 phba->last_completion_time = jiffies;
12851 del_timer(&phba->sli.mbox_tmo);
12852
12853 /* Move mbox data to caller's mailbox region, do endian swapping */
12854 if (pmb->mbox_cmpl && mbox)
James Smart48f8fdb2018-05-04 20:37:51 -070012855 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040012856
James Smart73d91e52011-10-10 21:32:10 -040012857 /*
12858 * For mcqe errors, conditionally move a modified error code to
12859 * the mbox so that the error will not be missed.
12860 */
12861 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12862 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12863 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12864 bf_set(lpfc_mqe_status, mqe,
12865 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12866 }
James Smart04c68492009-05-22 14:52:52 -040012867 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12868 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12869 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12870 "MBOX dflt rpi: status:x%x rpi:x%x",
12871 mcqe_status,
12872 pmbox->un.varWords[0], 0);
12873 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12874 mp = (struct lpfc_dmabuf *)(pmb->context1);
12875 ndlp = (struct lpfc_nodelist *)pmb->context2;
12876 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12877 * RID of the PPI using the same mbox buffer.
12878 */
12879 lpfc_unreg_login(phba, vport->vpi,
12880 pmbox->un.varWords[0], pmb);
12881 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12882 pmb->context1 = mp;
12883 pmb->context2 = ndlp;
12884 pmb->vport = vport;
12885 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12886 if (rc != MBX_BUSY)
12887 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12888 LOG_SLI, "0385 rc should "
12889 "have been MBX_BUSY\n");
12890 if (rc != MBX_NOT_FINISHED)
12891 goto send_current_mbox;
12892 }
12893 }
12894 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12895 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12896 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12897
12898 /* There is mailbox completion work to do */
12899 spin_lock_irqsave(&phba->hbalock, iflags);
12900 __lpfc_mbox_cmpl_put(phba, pmb);
12901 phba->work_ha |= HA_MBATT;
12902 spin_unlock_irqrestore(&phba->hbalock, iflags);
12903 workposted = true;
12904
12905send_current_mbox:
12906 spin_lock_irqsave(&phba->hbalock, iflags);
12907 /* Release the mailbox command posting token */
12908 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12909 /* Setting active mailbox pointer need to be in sync to flag clear */
12910 phba->sli.mbox_active = NULL;
12911 spin_unlock_irqrestore(&phba->hbalock, iflags);
12912 /* Wake up worker thread to post the next pending mailbox command */
12913 lpfc_worker_wake_up(phba);
12914out_no_mqe_complete:
12915 if (bf_get(lpfc_trailer_consumed, mcqe))
12916 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12917 return workposted;
12918}
12919
12920/**
12921 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12922 * @phba: Pointer to HBA context object.
12923 * @cqe: Pointer to mailbox completion queue entry.
12924 *
12925 * This routine process a mailbox completion queue entry, it invokes the
12926 * proper mailbox complete handling or asynchrous event handling routine
12927 * according to the MCQE's async bit.
12928 *
12929 * Return: true if work posted to worker thread, otherwise false.
12930 **/
12931static bool
12932lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12933{
12934 struct lpfc_mcqe mcqe;
12935 bool workposted;
12936
12937 /* Copy the mailbox MCQE and convert endian order as needed */
James Smart48f8fdb2018-05-04 20:37:51 -070012938 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
James Smart04c68492009-05-22 14:52:52 -040012939
12940 /* Invoke the proper event handling routine */
12941 if (!bf_get(lpfc_trailer_async, &mcqe))
12942 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12943 else
12944 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12945 return workposted;
12946}
12947
12948/**
James Smart4f774512009-05-22 14:52:35 -040012949 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12950 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012951 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012952 * @wcqe: Pointer to work-queue completion queue entry.
12953 *
12954 * This routine handles an ELS work-queue completion event.
12955 *
12956 * Return: true if work posted to worker thread, otherwise false.
12957 **/
12958static bool
James Smart2a76a282012-08-03 12:35:54 -040012959lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012960 struct lpfc_wcqe_complete *wcqe)
12961{
James Smart4f774512009-05-22 14:52:35 -040012962 struct lpfc_iocbq *irspiocbq;
12963 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012964 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012965 int txq_cnt = 0;
12966 int txcmplq_cnt = 0;
12967 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012968
James Smart11f0e342018-05-04 20:37:57 -070012969 /* Check for response status */
12970 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12971 /* Log the error status */
12972 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12973 "0357 ELS CQE error: status=x%x: "
12974 "CQE: %08x %08x %08x %08x\n",
12975 bf_get(lpfc_wcqe_c_status, wcqe),
12976 wcqe->word0, wcqe->total_data_placed,
12977 wcqe->parameter, wcqe->word3);
12978 }
12979
James Smart45ed1192009-10-02 15:17:02 -040012980 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012981 irspiocbq = lpfc_sli_get_iocbq(phba);
12982 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012983 if (!list_empty(&pring->txq))
12984 txq_cnt++;
12985 if (!list_empty(&pring->txcmplq))
12986 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012987 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012988 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12989 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012990 txq_cnt, phba->iocb_cnt,
12991 fcp_txcmplq_cnt,
12992 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012993 return false;
James Smart4f774512009-05-22 14:52:35 -040012994 }
James Smart4f774512009-05-22 14:52:35 -040012995
James Smart45ed1192009-10-02 15:17:02 -040012996 /* Save off the slow-path queue event for work thread to process */
12997 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012998 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012999 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013000 &phba->sli4_hba.sp_queue_event);
13001 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040013002 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013003
James Smart45ed1192009-10-02 15:17:02 -040013004 return true;
James Smart4f774512009-05-22 14:52:35 -040013005}
13006
13007/**
13008 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13009 * @phba: Pointer to HBA context object.
13010 * @wcqe: Pointer to work-queue completion queue entry.
13011 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013012 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013013 * proper WQ release routine to the slow-path WQ.
13014 **/
13015static void
13016lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13017 struct lpfc_wcqe_release *wcqe)
13018{
James Smart2e90f4b2011-12-13 13:22:37 -050013019 /* sanity check on queue memory */
13020 if (unlikely(!phba->sli4_hba.els_wq))
13021 return;
James Smart4f774512009-05-22 14:52:35 -040013022 /* Check for the slow-path ELS work queue */
13023 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13024 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13025 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13026 else
13027 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13028 "2579 Slow-path wqe consume event carries "
13029 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13030 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13031 phba->sli4_hba.els_wq->queue_id);
13032}
13033
13034/**
13035 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13036 * @phba: Pointer to HBA context object.
13037 * @cq: Pointer to a WQ completion queue.
13038 * @wcqe: Pointer to work-queue completion queue entry.
13039 *
13040 * This routine handles an XRI abort event.
13041 *
13042 * Return: true if work posted to worker thread, otherwise false.
13043 **/
13044static bool
13045lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13046 struct lpfc_queue *cq,
13047 struct sli4_wcqe_xri_aborted *wcqe)
13048{
13049 bool workposted = false;
13050 struct lpfc_cq_event *cq_event;
13051 unsigned long iflags;
13052
James Smart4f774512009-05-22 14:52:35 -040013053 switch (cq->subtype) {
13054 case LPFC_FCP:
James Smart8a5ca102017-11-20 16:00:30 -080013055 cq_event = lpfc_cq_event_setup(
13056 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13057 if (!cq_event)
13058 return false;
James Smart4f774512009-05-22 14:52:35 -040013059 spin_lock_irqsave(&phba->hbalock, iflags);
13060 list_add_tail(&cq_event->list,
13061 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
13062 /* Set the fcp xri abort event flag */
13063 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
13064 spin_unlock_irqrestore(&phba->hbalock, iflags);
13065 workposted = true;
13066 break;
James Smart422c4cb2017-11-20 16:00:32 -080013067 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040013068 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080013069 cq_event = lpfc_cq_event_setup(
13070 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13071 if (!cq_event)
13072 return false;
James Smart4f774512009-05-22 14:52:35 -040013073 spin_lock_irqsave(&phba->hbalock, iflags);
13074 list_add_tail(&cq_event->list,
13075 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13076 /* Set the els xri abort event flag */
13077 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13078 spin_unlock_irqrestore(&phba->hbalock, iflags);
13079 workposted = true;
13080 break;
James Smart318083a2017-03-04 09:30:30 -080013081 case LPFC_NVME:
James Smart8a5ca102017-11-20 16:00:30 -080013082 /* Notify aborted XRI for NVME work queue */
13083 if (phba->nvmet_support)
13084 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13085 else
13086 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
13087
13088 workposted = false;
James Smart318083a2017-03-04 09:30:30 -080013089 break;
James Smart4f774512009-05-22 14:52:35 -040013090 default:
13091 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080013092 "0603 Invalid CQ subtype %d: "
13093 "%08x %08x %08x %08x\n",
13094 cq->subtype, wcqe->word0, wcqe->parameter,
13095 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013096 workposted = false;
13097 break;
13098 }
13099 return workposted;
13100}
13101
13102/**
James Smart4d9ab992009-10-02 15:16:39 -040013103 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040013104 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040013105 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013106 *
James Smart4d9ab992009-10-02 15:16:39 -040013107 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013108 *
13109 * Return: true if work posted to worker thread, otherwise false.
13110 **/
13111static bool
James Smart4d9ab992009-10-02 15:16:39 -040013112lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13113{
13114 bool workposted = false;
James Smart895427b2017-02-12 13:52:30 -080013115 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040013116 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13117 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070013118 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040013119 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040013120 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040013121 unsigned long iflags;
13122
James Smart2e90f4b2011-12-13 13:22:37 -050013123 /* sanity check on queue memory */
13124 if (unlikely(!hrq) || unlikely(!drq))
13125 return workposted;
13126
James Smart7851fe22011-07-22 18:36:52 -040013127 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13128 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13129 else
13130 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13131 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040013132 goto out;
13133
13134 status = bf_get(lpfc_rcqe_status, rcqe);
13135 switch (status) {
13136 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13137 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13138 "2537 Receive Frame Truncated!!\n");
13139 case FC_STATUS_RQ_SUCCESS:
13140 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013141 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040013142 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13143 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040013144 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040013145 spin_unlock_irqrestore(&phba->hbalock, iflags);
13146 goto out;
13147 }
James Smartb84daac2012-08-03 12:35:13 -040013148 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013149 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040013150 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080013151
13152 /* If a NVME LS event (type 0x28), treat it as Fast path */
13153 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13154
James Smart4d9ab992009-10-02 15:16:39 -040013155 /* save off the frame for the word thread to process */
13156 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013157 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040013158 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040013159 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040013160 spin_unlock_irqrestore(&phba->hbalock, iflags);
13161 workposted = true;
13162 break;
James Smart4d9ab992009-10-02 15:16:39 -040013163 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013164 if (phba->nvmet_support) {
13165 tgtp = phba->targetport->private;
13166 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13167 "6402 RQE Error x%x, posted %d err_cnt "
13168 "%d: %x %x %x\n",
13169 status, hrq->RQ_buf_posted,
13170 hrq->RQ_no_posted_buf,
13171 atomic_read(&tgtp->rcv_fcp_cmd_in),
13172 atomic_read(&tgtp->rcv_fcp_cmd_out),
13173 atomic_read(&tgtp->xmt_fcp_release));
13174 }
13175 /* fallthrough */
13176
13177 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040013178 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040013179 /* Post more buffers if possible */
13180 spin_lock_irqsave(&phba->hbalock, iflags);
13181 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13182 spin_unlock_irqrestore(&phba->hbalock, iflags);
13183 workposted = true;
13184 break;
13185 }
13186out:
13187 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040013188}
13189
13190/**
13191 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13192 * @phba: Pointer to HBA context object.
13193 * @cq: Pointer to the completion queue.
13194 * @wcqe: Pointer to a completion queue entry.
13195 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013196 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040013197 * entry.
13198 *
13199 * Return: true if work posted to worker thread, otherwise false.
13200 **/
13201static bool
13202lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013203 struct lpfc_cqe *cqe)
13204{
James Smart45ed1192009-10-02 15:17:02 -040013205 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040013206 bool workposted = false;
13207
13208 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013209 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013210
13211 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040013212 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040013213 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040013214 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040013215 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040013216 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013217 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013218 break;
13219 case CQE_CODE_RELEASE_WQE:
13220 /* Process the WQ release event */
13221 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013222 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013223 break;
13224 case CQE_CODE_XRI_ABORTED:
13225 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013226 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013227 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013228 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013229 break;
James Smart4d9ab992009-10-02 15:16:39 -040013230 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013231 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013232 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013233 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013234 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013235 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013236 break;
James Smart4f774512009-05-22 14:52:35 -040013237 default:
13238 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13239 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013240 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013241 break;
13242 }
13243 return workposted;
13244}
13245
13246/**
James Smart4f774512009-05-22 14:52:35 -040013247 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13248 * @phba: Pointer to HBA context object.
13249 * @eqe: Pointer to fast-path event queue entry.
13250 *
13251 * This routine process a event queue entry from the slow-path event queue.
13252 * It will check the MajorCode and MinorCode to determine this is for a
13253 * completion event on a completion queue, if not, an error shall be logged
13254 * and just return. Otherwise, it will get to the corresponding completion
13255 * queue and process all the entries on that completion queue, rearm the
13256 * completion queue, and then return.
13257 *
13258 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013259static void
James Smart67d12732012-08-03 12:36:13 -040013260lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13261 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013262{
James Smart67d12732012-08-03 12:36:13 -040013263 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013264 uint16_t cqid;
13265
James Smart4f774512009-05-22 14:52:35 -040013266 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013267 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013268
James Smart4f774512009-05-22 14:52:35 -040013269 list_for_each_entry(childq, &speq->child_list, list) {
13270 if (childq->queue_id == cqid) {
13271 cq = childq;
13272 break;
13273 }
13274 }
13275 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013276 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13277 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13278 "0365 Slow-path CQ identifier "
13279 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013280 return;
James Smart4f774512009-05-22 14:52:35 -040013281 }
13282
James Smart895427b2017-02-12 13:52:30 -080013283 /* Save EQ associated with this CQ */
13284 cq->assoc_qp = speq;
13285
Dick Kennedyf485c182017-09-29 17:34:34 -070013286 if (!queue_work(phba->wq, &cq->spwork))
13287 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13288 "0390 Cannot schedule soft IRQ "
13289 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13290 cqid, cq->queue_id, smp_processor_id());
13291}
13292
13293/**
13294 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13295 * @phba: Pointer to HBA context object.
13296 *
13297 * This routine process a event queue entry from the slow-path event queue.
13298 * It will check the MajorCode and MinorCode to determine this is for a
13299 * completion event on a completion queue, if not, an error shall be logged
13300 * and just return. Otherwise, it will get to the corresponding completion
13301 * queue and process all the entries on that completion queue, rearm the
13302 * completion queue, and then return.
13303 *
13304 **/
13305static void
13306lpfc_sli4_sp_process_cq(struct work_struct *work)
13307{
13308 struct lpfc_queue *cq =
13309 container_of(work, struct lpfc_queue, spwork);
13310 struct lpfc_hba *phba = cq->phba;
13311 struct lpfc_cqe *cqe;
13312 bool workposted = false;
13313 int ccount = 0;
13314
James Smart4f774512009-05-22 14:52:35 -040013315 /* Process all the entries to the CQ */
13316 switch (cq->type) {
13317 case LPFC_MCQ:
13318 while ((cqe = lpfc_sli4_cq_get(cq))) {
13319 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013320 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013321 break;
James Smartb84daac2012-08-03 12:35:13 -040013322 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040013323 }
13324 break;
13325 case LPFC_WCQ:
13326 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013327 if (cq->subtype == LPFC_FCP ||
13328 cq->subtype == LPFC_NVME) {
13329#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13330 if (phba->ktime_on)
13331 cq->isr_timestamp = ktime_get_ns();
13332 else
13333 cq->isr_timestamp = 0;
13334#endif
James Smart895427b2017-02-12 13:52:30 -080013335 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040013336 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013337 } else {
James Smart05580562011-05-24 11:40:48 -040013338 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13339 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013340 }
Dick Kennedyf485c182017-09-29 17:34:34 -070013341 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013342 break;
James Smart4f774512009-05-22 14:52:35 -040013343 }
James Smartb84daac2012-08-03 12:35:13 -040013344
13345 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013346 if (ccount > cq->CQ_max_cqe)
13347 cq->CQ_max_cqe = ccount;
James Smart4f774512009-05-22 14:52:35 -040013348 break;
13349 default:
13350 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13351 "0370 Invalid completion queue type (%d)\n",
13352 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013353 return;
James Smart4f774512009-05-22 14:52:35 -040013354 }
13355
13356 /* Catch the no cq entry condition, log an error */
Dick Kennedyf485c182017-09-29 17:34:34 -070013357 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013358 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13359 "0371 No entry from the CQ: identifier "
13360 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13361
13362 /* In any case, flash and re-arm the RCQ */
James Smartb71413d2018-02-22 08:18:40 -080013363 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013364
13365 /* wake up worker thread if there are works to be done */
13366 if (workposted)
13367 lpfc_worker_wake_up(phba);
13368}
13369
13370/**
13371 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013372 * @phba: Pointer to HBA context object.
13373 * @cq: Pointer to associated CQ
13374 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013375 *
13376 * This routine process a fast-path work queue completion entry from fast-path
13377 * event queue for FCP command response completion.
13378 **/
13379static void
James Smart2a76a282012-08-03 12:35:54 -040013380lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013381 struct lpfc_wcqe_complete *wcqe)
13382{
James Smart2a76a282012-08-03 12:35:54 -040013383 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013384 struct lpfc_iocbq *cmdiocbq;
13385 struct lpfc_iocbq irspiocbq;
13386 unsigned long iflags;
13387
James Smart4f774512009-05-22 14:52:35 -040013388 /* Check for response status */
13389 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13390 /* If resource errors reported from HBA, reduce queue
13391 * depth of the SCSI device.
13392 */
James Smarte3d2b802012-08-14 14:25:43 -040013393 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13394 IOSTAT_LOCAL_REJECT)) &&
13395 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13396 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013397 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013398
James Smart4f774512009-05-22 14:52:35 -040013399 /* Log the error status */
James Smart11f0e342018-05-04 20:37:57 -070013400 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13401 "0373 FCP CQE error: status=x%x: "
13402 "CQE: %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013403 bf_get(lpfc_wcqe_c_status, wcqe),
James Smart11f0e342018-05-04 20:37:57 -070013404 wcqe->word0, wcqe->total_data_placed,
13405 wcqe->parameter, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013406 }
13407
13408 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013409 spin_lock_irqsave(&pring->ring_lock, iflags);
13410 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013411 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13412 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013413 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013414 if (unlikely(!cmdiocbq)) {
13415 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13416 "0374 FCP complete with no corresponding "
13417 "cmdiocb: iotag (%d)\n",
13418 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13419 return;
13420 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013421#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13422 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13423#endif
James Smart895427b2017-02-12 13:52:30 -080013424 if (cmdiocbq->iocb_cmpl == NULL) {
13425 if (cmdiocbq->wqe_cmpl) {
13426 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13427 spin_lock_irqsave(&phba->hbalock, iflags);
13428 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13429 spin_unlock_irqrestore(&phba->hbalock, iflags);
13430 }
13431
13432 /* Pass the cmd_iocb and the wcqe to the upper layer */
13433 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13434 return;
13435 }
James Smart4f774512009-05-22 14:52:35 -040013436 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13437 "0375 FCP cmdiocb not callback function "
13438 "iotag: (%d)\n",
13439 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13440 return;
13441 }
13442
13443 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013444 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013445
James Smart0f65ff62010-02-26 14:14:23 -050013446 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13447 spin_lock_irqsave(&phba->hbalock, iflags);
13448 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13449 spin_unlock_irqrestore(&phba->hbalock, iflags);
13450 }
13451
James Smart4f774512009-05-22 14:52:35 -040013452 /* Pass the cmd_iocb and the rsp state to the upper layer */
13453 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13454}
13455
13456/**
13457 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13458 * @phba: Pointer to HBA context object.
13459 * @cq: Pointer to completion queue.
13460 * @wcqe: Pointer to work-queue completion queue entry.
13461 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013462 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013463 * proper WQ release routine to the slow-path WQ.
13464 **/
13465static void
13466lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13467 struct lpfc_wcqe_release *wcqe)
13468{
13469 struct lpfc_queue *childwq;
13470 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013471 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013472
13473 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013474 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013475 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013476 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013477 lpfc_sli4_wq_release(childwq,
13478 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080013479 if (childwq->q_flag & HBA_NVMET_WQFULL)
13480 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040013481 wqid_matched = true;
13482 break;
13483 }
13484 }
13485 /* Report warning log message if no match found */
13486 if (wqid_matched != true)
13487 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13488 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013489 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013490}
13491
13492/**
James Smart2d7dbc42017-02-12 13:52:35 -080013493 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13494 * @phba: Pointer to HBA context object.
13495 * @rcqe: Pointer to receive-queue completion queue entry.
13496 *
13497 * This routine process a receive-queue completion queue entry.
13498 *
13499 * Return: true if work posted to worker thread, otherwise false.
13500 **/
13501static bool
13502lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13503 struct lpfc_rcqe *rcqe)
13504{
13505 bool workposted = false;
13506 struct lpfc_queue *hrq;
13507 struct lpfc_queue *drq;
13508 struct rqb_dmabuf *dma_buf;
13509 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013510 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013511 uint32_t status, rq_id;
13512 unsigned long iflags;
13513 uint32_t fctl, idx;
13514
13515 if ((phba->nvmet_support == 0) ||
13516 (phba->sli4_hba.nvmet_cqset == NULL))
13517 return workposted;
13518
13519 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13520 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13521 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13522
13523 /* sanity check on queue memory */
13524 if (unlikely(!hrq) || unlikely(!drq))
13525 return workposted;
13526
13527 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13528 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13529 else
13530 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13531
13532 if ((phba->nvmet_support == 0) ||
13533 (rq_id != hrq->queue_id))
13534 return workposted;
13535
13536 status = bf_get(lpfc_rcqe_status, rcqe);
13537 switch (status) {
13538 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13539 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13540 "6126 Receive Frame Truncated!!\n");
James Smart78e1d202017-06-01 21:07:09 -070013541 /* Drop thru */
James Smart2d7dbc42017-02-12 13:52:35 -080013542 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080013543 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013544 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080013545 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13546 if (!dma_buf) {
13547 hrq->RQ_no_buf_found++;
13548 spin_unlock_irqrestore(&phba->hbalock, iflags);
13549 goto out;
13550 }
13551 spin_unlock_irqrestore(&phba->hbalock, iflags);
13552 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013553 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013554 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13555
13556 /* Just some basic sanity checks on FCP Command frame */
13557 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13558 fc_hdr->fh_f_ctl[1] << 8 |
13559 fc_hdr->fh_f_ctl[2]);
13560 if (((fctl &
13561 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13562 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13563 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13564 goto drop;
13565
13566 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13567 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013568 lpfc_nvmet_unsol_fcp_event(
Dick Kennedy66d7ce92017-08-23 16:55:42 -070013569 phba, idx, dma_buf,
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013570 cq->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013571 return false;
13572 }
13573drop:
13574 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13575 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013576 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013577 if (phba->nvmet_support) {
13578 tgtp = phba->targetport->private;
13579 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13580 "6401 RQE Error x%x, posted %d err_cnt "
13581 "%d: %x %x %x\n",
13582 status, hrq->RQ_buf_posted,
13583 hrq->RQ_no_posted_buf,
13584 atomic_read(&tgtp->rcv_fcp_cmd_in),
13585 atomic_read(&tgtp->rcv_fcp_cmd_out),
13586 atomic_read(&tgtp->xmt_fcp_release));
13587 }
13588 /* fallthrough */
13589
13590 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013591 hrq->RQ_no_posted_buf++;
13592 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013593 break;
13594 }
13595out:
13596 return workposted;
13597}
13598
13599/**
James Smart895427b2017-02-12 13:52:30 -080013600 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013601 * @cq: Pointer to the completion queue.
13602 * @eqe: Pointer to fast-path completion queue entry.
13603 *
13604 * This routine process a fast-path work queue completion entry from fast-path
13605 * event queue for FCP command response completion.
13606 **/
13607static int
James Smart895427b2017-02-12 13:52:30 -080013608lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013609 struct lpfc_cqe *cqe)
13610{
13611 struct lpfc_wcqe_release wcqe;
13612 bool workposted = false;
13613
13614 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013615 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013616
13617 /* Check and process for different type of WCQE and dispatch */
13618 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13619 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013620 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013621 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013622 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013623 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013624 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13625 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13626 (struct lpfc_wcqe_complete *)&wcqe);
13627 if (cq->subtype == LPFC_NVME_LS)
13628 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013629 (struct lpfc_wcqe_complete *)&wcqe);
13630 break;
13631 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013632 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013633 /* Process the WQ release event */
13634 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13635 (struct lpfc_wcqe_release *)&wcqe);
13636 break;
13637 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013638 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013639 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013640 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013641 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13642 (struct sli4_wcqe_xri_aborted *)&wcqe);
13643 break;
James Smart895427b2017-02-12 13:52:30 -080013644 case CQE_CODE_RECEIVE_V1:
13645 case CQE_CODE_RECEIVE:
13646 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013647 if (cq->subtype == LPFC_NVMET) {
13648 workposted = lpfc_sli4_nvmet_handle_rcqe(
13649 phba, cq, (struct lpfc_rcqe *)&wcqe);
13650 }
James Smart895427b2017-02-12 13:52:30 -080013651 break;
James Smart4f774512009-05-22 14:52:35 -040013652 default:
13653 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013654 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013655 bf_get(lpfc_wcqe_c_code, &wcqe));
13656 break;
13657 }
13658 return workposted;
13659}
13660
13661/**
James Smart67d12732012-08-03 12:36:13 -040013662 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040013663 * @phba: Pointer to HBA context object.
13664 * @eqe: Pointer to fast-path event queue entry.
13665 *
13666 * This routine process a event queue entry from the fast-path event queue.
13667 * It will check the MajorCode and MinorCode to determine this is for a
13668 * completion event on a completion queue, if not, an error shall be logged
13669 * and just return. Otherwise, it will get to the corresponding completion
13670 * queue and process all the entries on the completion queue, rearm the
13671 * completion queue, and then return.
13672 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013673static void
James Smart67d12732012-08-03 12:36:13 -040013674lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13675 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040013676{
James Smart895427b2017-02-12 13:52:30 -080013677 struct lpfc_queue *cq = NULL;
James Smart2d7dbc42017-02-12 13:52:35 -080013678 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040013679
James Smartcb5172e2010-03-15 11:25:07 -040013680 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040013681 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040013682 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040013683 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040013684 bf_get_le32(lpfc_eqe_major_code, eqe),
13685 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070013686 return;
James Smart4f774512009-05-22 14:52:35 -040013687 }
13688
James Smart67d12732012-08-03 12:36:13 -040013689 /* Get the reference to the corresponding CQ */
13690 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13691
James Smart2d7dbc42017-02-12 13:52:35 -080013692 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13693 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13694 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13695 /* Process NVMET unsol rcv */
13696 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13697 goto process_cq;
13698 }
13699 }
13700
James Smart895427b2017-02-12 13:52:30 -080013701 if (phba->sli4_hba.nvme_cq_map &&
13702 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
James Smartf358dd02017-02-12 13:52:34 -080013703 /* Process NVME / NVMET command completion */
James Smart895427b2017-02-12 13:52:30 -080013704 cq = phba->sli4_hba.nvme_cq[qidx];
13705 goto process_cq;
13706 }
13707
13708 if (phba->sli4_hba.fcp_cq_map &&
13709 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13710 /* Process FCP command completion */
13711 cq = phba->sli4_hba.fcp_cq[qidx];
13712 goto process_cq;
13713 }
13714
13715 if (phba->sli4_hba.nvmels_cq &&
13716 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13717 /* Process NVME unsol rcv */
13718 cq = phba->sli4_hba.nvmels_cq;
13719 }
13720
13721 /* Otherwise this is a Slow path event */
13722 if (cq == NULL) {
Dick Kennedyf485c182017-09-29 17:34:34 -070013723 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13724 return;
James Smart67d12732012-08-03 12:36:13 -040013725 }
13726
James Smart895427b2017-02-12 13:52:30 -080013727process_cq:
James Smart4f774512009-05-22 14:52:35 -040013728 if (unlikely(cqid != cq->queue_id)) {
13729 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13730 "0368 Miss-matched fast-path completion "
13731 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13732 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070013733 return;
James Smart4f774512009-05-22 14:52:35 -040013734 }
13735
James Smart895427b2017-02-12 13:52:30 -080013736 /* Save EQ associated with this CQ */
13737 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13738
Dick Kennedyf485c182017-09-29 17:34:34 -070013739 if (!queue_work(phba->wq, &cq->irqwork))
13740 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13741 "0363 Cannot schedule soft IRQ "
13742 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13743 cqid, cq->queue_id, smp_processor_id());
13744}
13745
13746/**
13747 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13748 * @phba: Pointer to HBA context object.
13749 * @eqe: Pointer to fast-path event queue entry.
13750 *
13751 * This routine process a event queue entry from the fast-path event queue.
13752 * It will check the MajorCode and MinorCode to determine this is for a
13753 * completion event on a completion queue, if not, an error shall be logged
13754 * and just return. Otherwise, it will get to the corresponding completion
13755 * queue and process all the entries on the completion queue, rearm the
13756 * completion queue, and then return.
13757 **/
13758static void
13759lpfc_sli4_hba_process_cq(struct work_struct *work)
13760{
13761 struct lpfc_queue *cq =
13762 container_of(work, struct lpfc_queue, irqwork);
13763 struct lpfc_hba *phba = cq->phba;
13764 struct lpfc_cqe *cqe;
13765 bool workposted = false;
13766 int ccount = 0;
13767
James Smart4f774512009-05-22 14:52:35 -040013768 /* Process all the entries to the CQ */
13769 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013770#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13771 if (phba->ktime_on)
13772 cq->isr_timestamp = ktime_get_ns();
13773 else
13774 cq->isr_timestamp = 0;
13775#endif
James Smart895427b2017-02-12 13:52:30 -080013776 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013777 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013778 break;
James Smart4f774512009-05-22 14:52:35 -040013779 }
13780
James Smartb84daac2012-08-03 12:35:13 -040013781 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013782 if (ccount > cq->CQ_max_cqe)
13783 cq->CQ_max_cqe = ccount;
13784 cq->assoc_qp->EQ_cqe_cnt += ccount;
James Smartb84daac2012-08-03 12:35:13 -040013785
James Smart4f774512009-05-22 14:52:35 -040013786 /* Catch the no cq entry condition */
Dick Kennedyf485c182017-09-29 17:34:34 -070013787 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013788 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13789 "0369 No entry from fast-path completion "
13790 "queue fcpcqid=%d\n", cq->queue_id);
13791
13792 /* In any case, flash and re-arm the CQ */
James Smartb71413d2018-02-22 08:18:40 -080013793 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013794
13795 /* wake up worker thread if there are works to be done */
13796 if (workposted)
13797 lpfc_worker_wake_up(phba);
13798}
13799
13800static void
13801lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13802{
13803 struct lpfc_eqe *eqe;
13804
13805 /* walk all the EQ entries and drop on the floor */
13806 while ((eqe = lpfc_sli4_eq_get(eq)))
13807 ;
13808
13809 /* Clear and re-arm the EQ */
James Smartb71413d2018-02-22 08:18:40 -080013810 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013811}
13812
James Smart1ba981f2014-02-20 09:56:45 -050013813
13814/**
13815 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13816 * entry
13817 * @phba: Pointer to HBA context object.
13818 * @eqe: Pointer to fast-path event queue entry.
13819 *
13820 * This routine process a event queue entry from the Flash Optimized Fabric
13821 * event queue. It will check the MajorCode and MinorCode to determine this
13822 * is for a completion event on a completion queue, if not, an error shall be
13823 * logged and just return. Otherwise, it will get to the corresponding
13824 * completion queue and process all the entries on the completion queue, rearm
13825 * the completion queue, and then return.
13826 **/
13827static void
13828lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13829{
13830 struct lpfc_queue *cq;
James Smart1ba981f2014-02-20 09:56:45 -050013831 uint16_t cqid;
James Smart1ba981f2014-02-20 09:56:45 -050013832
13833 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13834 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13835 "9147 Not a valid completion "
13836 "event: majorcode=x%x, minorcode=x%x\n",
13837 bf_get_le32(lpfc_eqe_major_code, eqe),
13838 bf_get_le32(lpfc_eqe_minor_code, eqe));
13839 return;
13840 }
13841
13842 /* Get the reference to the corresponding CQ */
13843 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13844
13845 /* Next check for OAS */
13846 cq = phba->sli4_hba.oas_cq;
13847 if (unlikely(!cq)) {
13848 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13849 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13850 "9148 OAS completion queue "
13851 "does not exist\n");
13852 return;
13853 }
13854
13855 if (unlikely(cqid != cq->queue_id)) {
13856 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13857 "9149 Miss-matched fast-path compl "
13858 "queue id: eqcqid=%d, fcpcqid=%d\n",
13859 cqid, cq->queue_id);
13860 return;
13861 }
13862
James Smartd41b65b2017-06-15 22:56:41 -070013863 /* Save EQ associated with this CQ */
13864 cq->assoc_qp = phba->sli4_hba.fof_eq;
13865
Dick Kennedyf485c182017-09-29 17:34:34 -070013866 /* CQ work will be processed on CPU affinitized to this IRQ */
13867 if (!queue_work(phba->wq, &cq->irqwork))
James Smart1ba981f2014-02-20 09:56:45 -050013868 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
Dick Kennedyf485c182017-09-29 17:34:34 -070013869 "0367 Cannot schedule soft IRQ "
13870 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13871 cqid, cq->queue_id, smp_processor_id());
James Smart1ba981f2014-02-20 09:56:45 -050013872}
13873
13874/**
13875 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13876 * @irq: Interrupt number.
13877 * @dev_id: The device context pointer.
13878 *
13879 * This function is directly called from the PCI layer as an interrupt
13880 * service routine when device with SLI-4 interface spec is enabled with
13881 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13882 * IOCB ring event in the HBA. However, when the device is enabled with either
13883 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13884 * device-level interrupt handler. When the PCI slot is in error recovery
13885 * or the HBA is undergoing initialization, the interrupt handler will not
13886 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13887 * the intrrupt context. This function is called without any lock held.
13888 * It gets the hbalock to access and update SLI data structures. Note that,
13889 * the EQ to CQ are one-to-one map such that the EQ index is
13890 * equal to that of CQ index.
13891 *
13892 * This function returns IRQ_HANDLED when interrupt is handled else it
13893 * returns IRQ_NONE.
13894 **/
13895irqreturn_t
13896lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13897{
13898 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013899 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050013900 struct lpfc_queue *eq;
13901 struct lpfc_eqe *eqe;
13902 unsigned long iflag;
13903 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013904
13905 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013906 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13907 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050013908
13909 if (unlikely(!phba))
13910 return IRQ_NONE;
13911
13912 /* Get to the EQ struct associated with this vector */
13913 eq = phba->sli4_hba.fof_eq;
13914 if (unlikely(!eq))
13915 return IRQ_NONE;
13916
13917 /* Check device state for handling interrupt */
13918 if (unlikely(lpfc_intr_state_check(phba))) {
James Smart1ba981f2014-02-20 09:56:45 -050013919 /* Check again for link_state with lock held */
13920 spin_lock_irqsave(&phba->hbalock, iflag);
13921 if (phba->link_state < LPFC_LINK_DOWN)
13922 /* Flush, clear interrupt, and rearm the EQ */
13923 lpfc_sli4_eq_flush(phba, eq);
13924 spin_unlock_irqrestore(&phba->hbalock, iflag);
13925 return IRQ_NONE;
13926 }
13927
13928 /*
13929 * Process all the event on FCP fast-path EQ
13930 */
13931 while ((eqe = lpfc_sli4_eq_get(eq))) {
13932 lpfc_sli4_fof_handle_eqe(phba, eqe);
13933 if (!(++ecount % eq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013934 break;
James Smart1ba981f2014-02-20 09:56:45 -050013935 eq->EQ_processed++;
13936 }
13937
13938 /* Track the max number of EQEs processed in 1 intr */
13939 if (ecount > eq->EQ_max_eqe)
13940 eq->EQ_max_eqe = ecount;
13941
13942
13943 if (unlikely(ecount == 0)) {
13944 eq->EQ_no_entry++;
13945
13946 if (phba->intr_type == MSIX)
13947 /* MSI-X treated interrupt served as no EQ share INT */
13948 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13949 "9145 MSI-X interrupt with no EQE\n");
13950 else {
13951 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13952 "9146 ISR interrupt with no EQE\n");
13953 /* Non MSI-X treated on interrupt as EQ share INT */
13954 return IRQ_NONE;
13955 }
13956 }
13957 /* Always clear and re-arm the fast-path EQ */
James Smartb71413d2018-02-22 08:18:40 -080013958 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -050013959 return IRQ_HANDLED;
13960}
13961
James Smart4f774512009-05-22 14:52:35 -040013962/**
James Smart67d12732012-08-03 12:36:13 -040013963 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040013964 * @irq: Interrupt number.
13965 * @dev_id: The device context pointer.
13966 *
13967 * This function is directly called from the PCI layer as an interrupt
13968 * service routine when device with SLI-4 interface spec is enabled with
13969 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13970 * ring event in the HBA. However, when the device is enabled with either
13971 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13972 * device-level interrupt handler. When the PCI slot is in error recovery
13973 * or the HBA is undergoing initialization, the interrupt handler will not
13974 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13975 * the intrrupt context. This function is called without any lock held.
13976 * It gets the hbalock to access and update SLI data structures. Note that,
13977 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13978 * equal to that of FCP CQ index.
13979 *
James Smart67d12732012-08-03 12:36:13 -040013980 * The link attention and ELS ring attention events are handled
13981 * by the worker thread. The interrupt handler signals the worker thread
13982 * and returns for these events. This function is called without any lock
13983 * held. It gets the hbalock to access and update SLI data structures.
13984 *
James Smart4f774512009-05-22 14:52:35 -040013985 * This function returns IRQ_HANDLED when interrupt is handled else it
13986 * returns IRQ_NONE.
13987 **/
13988irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040013989lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040013990{
13991 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013992 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040013993 struct lpfc_queue *fpeq;
13994 struct lpfc_eqe *eqe;
13995 unsigned long iflag;
13996 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080013997 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040013998
13999 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080014000 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14001 phba = hba_eq_hdl->phba;
14002 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040014003
14004 if (unlikely(!phba))
14005 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040014006 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040014007 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014008
14009 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080014010 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050014011 if (unlikely(!fpeq))
14012 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014013
James Smartba20c852012-08-03 12:36:52 -040014014 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080014015 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartb71413d2018-02-22 08:18:40 -080014016 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -040014017 else {
James Smart895427b2017-02-12 13:52:30 -080014018 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040014019 return IRQ_NONE;
14020 }
14021 }
14022
James Smart4f774512009-05-22 14:52:35 -040014023 /* Check device state for handling interrupt */
14024 if (unlikely(lpfc_intr_state_check(phba))) {
14025 /* Check again for link_state with lock held */
14026 spin_lock_irqsave(&phba->hbalock, iflag);
14027 if (phba->link_state < LPFC_LINK_DOWN)
14028 /* Flush, clear interrupt, and rearm the EQ */
14029 lpfc_sli4_eq_flush(phba, fpeq);
14030 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040014031 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080014032 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040014033 return IRQ_NONE;
14034 }
14035
14036 /*
14037 * Process all the event on FCP fast-path EQ
14038 */
14039 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
Dick Kennedyf485c182017-09-29 17:34:34 -070014040 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
14041 if (!(++ecount % fpeq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070014042 break;
James Smartb84daac2012-08-03 12:35:13 -040014043 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040014044 }
14045
James Smartb84daac2012-08-03 12:35:13 -040014046 /* Track the max number of EQEs processed in 1 intr */
14047 if (ecount > fpeq->EQ_max_eqe)
14048 fpeq->EQ_max_eqe = ecount;
14049
James Smart4f774512009-05-22 14:52:35 -040014050 /* Always clear and re-arm the fast-path EQ */
James Smartb71413d2018-02-22 08:18:40 -080014051 phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014052
14053 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040014054 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040014055
14056 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080014057 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040014058 return IRQ_NONE;
14059 }
14060
James Smart4f774512009-05-22 14:52:35 -040014061 if (phba->intr_type == MSIX)
14062 /* MSI-X treated interrupt served as no EQ share INT */
14063 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14064 "0358 MSI-X interrupt with no EQE\n");
14065 else
14066 /* Non MSI-X treated on interrupt as EQ share INT */
14067 return IRQ_NONE;
14068 }
14069
James Smartba20c852012-08-03 12:36:52 -040014070 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080014071 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14072
James Smart4f774512009-05-22 14:52:35 -040014073 return IRQ_HANDLED;
14074} /* lpfc_sli4_fp_intr_handler */
14075
14076/**
14077 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14078 * @irq: Interrupt number.
14079 * @dev_id: The device context pointer.
14080 *
14081 * This function is the device-level interrupt handler to device with SLI-4
14082 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14083 * interrupt mode is enabled and there is an event in the HBA which requires
14084 * driver attention. This function invokes the slow-path interrupt attention
14085 * handling function and fast-path interrupt attention handling function in
14086 * turn to process the relevant HBA attention events. This function is called
14087 * without any lock held. It gets the hbalock to access and update SLI data
14088 * structures.
14089 *
14090 * This function returns IRQ_HANDLED when interrupt is handled, else it
14091 * returns IRQ_NONE.
14092 **/
14093irqreturn_t
14094lpfc_sli4_intr_handler(int irq, void *dev_id)
14095{
14096 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040014097 irqreturn_t hba_irq_rc;
14098 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080014099 int qidx;
James Smart4f774512009-05-22 14:52:35 -040014100
14101 /* Get the driver's phba structure from the dev_id */
14102 phba = (struct lpfc_hba *)dev_id;
14103
14104 if (unlikely(!phba))
14105 return IRQ_NONE;
14106
14107 /*
James Smart4f774512009-05-22 14:52:35 -040014108 * Invoke fast-path host attention interrupt handling as appropriate.
14109 */
James Smart895427b2017-02-12 13:52:30 -080014110 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040014111 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014112 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040014113 if (hba_irq_rc == IRQ_HANDLED)
14114 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040014115 }
14116
James Smart1ba981f2014-02-20 09:56:45 -050014117 if (phba->cfg_fof) {
14118 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014119 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050014120 if (hba_irq_rc == IRQ_HANDLED)
14121 hba_handled |= true;
14122 }
14123
James Smart67d12732012-08-03 12:36:13 -040014124 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014125} /* lpfc_sli4_intr_handler */
14126
14127/**
14128 * lpfc_sli4_queue_free - free a queue structure and associated memory
14129 * @queue: The queue structure to free.
14130 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040014131 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040014132 * the host resident queue. This function must be called after destroying the
14133 * queue on the HBA.
14134 **/
14135void
14136lpfc_sli4_queue_free(struct lpfc_queue *queue)
14137{
14138 struct lpfc_dmabuf *dmabuf;
14139
14140 if (!queue)
14141 return;
14142
14143 while (!list_empty(&queue->page_list)) {
14144 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14145 list);
James Smart81b96ed2017-11-20 16:00:29 -080014146 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040014147 dmabuf->virt, dmabuf->phys);
14148 kfree(dmabuf);
14149 }
James Smart895427b2017-02-12 13:52:30 -080014150 if (queue->rqbp) {
14151 lpfc_free_rq_buffer(queue->phba, queue);
14152 kfree(queue->rqbp);
14153 }
James Smartd1f525a2017-04-21 16:04:55 -070014154
14155 if (!list_empty(&queue->wq_list))
14156 list_del(&queue->wq_list);
14157
James Smart4f774512009-05-22 14:52:35 -040014158 kfree(queue);
14159 return;
14160}
14161
14162/**
14163 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14164 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080014165 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040014166 * @entry_size: The size of each queue entry for this queue.
14167 * @entry count: The number of entries that this queue will handle.
14168 *
14169 * This function allocates a queue structure and the DMAable memory used for
14170 * the host resident queue. This function must be called before creating the
14171 * queue on the HBA.
14172 **/
14173struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080014174lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14175 uint32_t entry_size, uint32_t entry_count)
James Smart4f774512009-05-22 14:52:35 -040014176{
14177 struct lpfc_queue *queue;
14178 struct lpfc_dmabuf *dmabuf;
14179 int x, total_qe_count;
14180 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040014181 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040014182
James Smartcb5172e2010-03-15 11:25:07 -040014183 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014184 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040014185
James Smart4f774512009-05-22 14:52:35 -040014186 queue = kzalloc(sizeof(struct lpfc_queue) +
14187 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14188 if (!queue)
14189 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040014190 queue->page_count = (ALIGN(entry_size * entry_count,
14191 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080014192
14193 /* If needed, Adjust page count to match the max the adapter supports */
14194 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
14195 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14196
James Smart4f774512009-05-22 14:52:35 -040014197 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080014198 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080014199 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040014200 INIT_LIST_HEAD(&queue->page_list);
14201 INIT_LIST_HEAD(&queue->child_list);
James Smart81b96ed2017-11-20 16:00:29 -080014202
14203 /* Set queue parameters now. If the system cannot provide memory
14204 * resources, the free routine needs to know what was allocated.
14205 */
14206 queue->entry_size = entry_size;
14207 queue->entry_count = entry_count;
14208 queue->page_size = hw_page_size;
14209 queue->phba = phba;
14210
James Smart4f774512009-05-22 14:52:35 -040014211 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14212 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14213 if (!dmabuf)
14214 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040014215 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14216 hw_page_size, &dmabuf->phys,
14217 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040014218 if (!dmabuf->virt) {
14219 kfree(dmabuf);
14220 goto out_fail;
14221 }
14222 dmabuf->buffer_tag = x;
14223 list_add_tail(&dmabuf->list, &queue->page_list);
14224 /* initialize queue's entry array */
14225 dma_pointer = dmabuf->virt;
14226 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040014227 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040014228 total_qe_count++, dma_pointer += entry_size) {
14229 queue->qe[total_qe_count].address = dma_pointer;
14230 }
14231 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014232 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14233 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014234
James Smart64eb4dc2017-05-15 15:20:49 -070014235 /* entry_repost will be set during q creation */
14236
James Smart4f774512009-05-22 14:52:35 -040014237 return queue;
14238out_fail:
14239 lpfc_sli4_queue_free(queue);
14240 return NULL;
14241}
14242
14243/**
James Smart962bc512013-01-03 15:44:00 -050014244 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14245 * @phba: HBA structure that indicates port to create a queue on.
14246 * @pci_barset: PCI BAR set flag.
14247 *
14248 * This function shall perform iomap of the specified PCI BAR address to host
14249 * memory address if not already done so and return it. The returned host
14250 * memory address can be NULL.
14251 */
14252static void __iomem *
14253lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14254{
James Smart962bc512013-01-03 15:44:00 -050014255 if (!phba->pcidev)
14256 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014257
14258 switch (pci_barset) {
14259 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014260 return phba->pci_bar0_memmap_p;
14261 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014262 return phba->pci_bar2_memmap_p;
14263 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014264 return phba->pci_bar4_memmap_p;
14265 default:
14266 break;
14267 }
14268 return NULL;
14269}
14270
14271/**
James Smart895427b2017-02-12 13:52:30 -080014272 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040014273 * @phba: HBA structure that indicates port to create a queue on.
14274 * @startq: The starting FCP EQ to modify
14275 *
14276 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
James Smart43140ca2017-03-04 09:30:34 -080014277 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14278 * updated in one mailbox command.
James Smart173edbb2012-06-12 13:54:50 -040014279 *
14280 * The @phba struct is used to send mailbox command to HBA. The @startq
14281 * is used to get the starting FCP EQ to change.
14282 * This function is asynchronous and will wait for the mailbox
14283 * command to finish before continuing.
14284 *
14285 * On success this function will return a zero. If unable to allocate enough
14286 * memory this function will return -ENOMEM. If the queue create mailbox command
14287 * fails this function will return -ENXIO.
14288 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014289int
James Smart0cf07f842017-06-01 21:07:10 -070014290lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14291 uint32_t numq, uint32_t imax)
James Smart173edbb2012-06-12 13:54:50 -040014292{
14293 struct lpfc_mbx_modify_eq_delay *eq_delay;
14294 LPFC_MBOXQ_t *mbox;
14295 struct lpfc_queue *eq;
14296 int cnt, rc, length, status = 0;
14297 uint32_t shdr_status, shdr_add_status;
James Smart0cf07f842017-06-01 21:07:10 -070014298 uint32_t result, val;
James Smart895427b2017-02-12 13:52:30 -080014299 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014300 union lpfc_sli4_cfg_shdr *shdr;
14301 uint16_t dmult;
14302
James Smart895427b2017-02-12 13:52:30 -080014303 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040014304 return 0;
14305
14306 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14307 if (!mbox)
14308 return -ENOMEM;
14309 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14310 sizeof(struct lpfc_sli4_cfg_mhdr));
14311 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14312 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14313 length, LPFC_SLI4_MBX_EMBED);
14314 eq_delay = &mbox->u.mqe.un.eq_delay;
14315
14316 /* Calculate delay multiper from maximum interrupt per second */
James Smart0cf07f842017-06-01 21:07:10 -070014317 result = imax / phba->io_channel_irqs;
James Smart895427b2017-02-12 13:52:30 -080014318 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040014319 dmult = 0;
14320 else
14321 dmult = LPFC_DMULT_CONST/result - 1;
James Smart0cf07f842017-06-01 21:07:10 -070014322 if (dmult > LPFC_DMULT_MAX)
14323 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014324
14325 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080014326 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14327 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040014328 if (!eq)
14329 continue;
James Smart0cf07f842017-06-01 21:07:10 -070014330 eq->q_mode = imax;
James Smart173edbb2012-06-12 13:54:50 -040014331 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14332 eq_delay->u.request.eq[cnt].phase = 0;
14333 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14334 cnt++;
James Smart0cf07f842017-06-01 21:07:10 -070014335
14336 /* q_mode is only used for auto_imax */
14337 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14338 /* Use EQ Delay Register method for q_mode */
14339
14340 /* Convert for EQ Delay register */
14341 val = phba->cfg_fcp_imax;
14342 if (val) {
14343 /* First, interrupts per sec per EQ */
14344 val = phba->cfg_fcp_imax /
14345 phba->io_channel_irqs;
14346
14347 /* us delay between each interrupt */
14348 val = LPFC_SEC_TO_USEC / val;
14349 }
14350 eq->q_mode = val;
14351 } else {
14352 eq->q_mode = imax;
14353 }
14354
14355 if (cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014356 break;
14357 }
14358 eq_delay->u.request.num_eq = cnt;
14359
14360 mbox->vport = phba->pport;
14361 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14362 mbox->context1 = NULL;
14363 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14364 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14365 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14366 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14367 if (shdr_status || shdr_add_status || rc) {
14368 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14369 "2512 MODIFY_EQ_DELAY mailbox failed with "
14370 "status x%x add_status x%x, mbx status x%x\n",
14371 shdr_status, shdr_add_status, rc);
14372 status = -ENXIO;
14373 }
14374 mempool_free(mbox, phba->mbox_mem_pool);
14375 return status;
14376}
14377
14378/**
James Smart4f774512009-05-22 14:52:35 -040014379 * lpfc_eq_create - Create an Event Queue on the HBA
14380 * @phba: HBA structure that indicates port to create a queue on.
14381 * @eq: The queue structure to use to create the event queue.
14382 * @imax: The maximum interrupt per second limit.
14383 *
14384 * This function creates an event queue, as detailed in @eq, on a port,
14385 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14386 *
14387 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14388 * is used to get the entry count and entry size that are necessary to
14389 * determine the number of pages to allocate and use for this queue. This
14390 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14391 * event queue. This function is asynchronous and will wait for the mailbox
14392 * command to finish before continuing.
14393 *
14394 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014395 * memory this function will return -ENOMEM. If the queue create mailbox command
14396 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014397 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014398int
James Smartee020062012-09-29 11:28:52 -040014399lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014400{
14401 struct lpfc_mbx_eq_create *eq_create;
14402 LPFC_MBOXQ_t *mbox;
14403 int rc, length, status = 0;
14404 struct lpfc_dmabuf *dmabuf;
14405 uint32_t shdr_status, shdr_add_status;
14406 union lpfc_sli4_cfg_shdr *shdr;
14407 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014408 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14409
James Smart2e90f4b2011-12-13 13:22:37 -050014410 /* sanity check on queue memory */
14411 if (!eq)
14412 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014413 if (!phba->sli4_hba.pc_sli4_params.supported)
14414 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014415
14416 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14417 if (!mbox)
14418 return -ENOMEM;
14419 length = (sizeof(struct lpfc_mbx_eq_create) -
14420 sizeof(struct lpfc_sli4_cfg_mhdr));
14421 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14422 LPFC_MBOX_OPCODE_EQ_CREATE,
14423 length, LPFC_SLI4_MBX_EMBED);
14424 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080014425 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014426 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14427 eq->page_count);
14428 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14429 LPFC_EQE_SIZE);
14430 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080014431
14432 /* Use version 2 of CREATE_EQ if eqav is set */
14433 if (phba->sli4_hba.pc_sli4_params.eqav) {
14434 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14435 LPFC_Q_CREATE_VERSION_2);
14436 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14437 phba->sli4_hba.pc_sli4_params.eqav);
14438 }
14439
James Smart2c9c5a02015-04-07 15:07:15 -040014440 /* don't setup delay multiplier using EQ_CREATE */
14441 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014442 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14443 dmult);
14444 switch (eq->entry_count) {
14445 default:
14446 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14447 "0360 Unsupported EQ count. (%d)\n",
14448 eq->entry_count);
14449 if (eq->entry_count < 256)
14450 return -EINVAL;
14451 /* otherwise default to smallest count (drop through) */
14452 case 256:
14453 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14454 LPFC_EQ_CNT_256);
14455 break;
14456 case 512:
14457 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14458 LPFC_EQ_CNT_512);
14459 break;
14460 case 1024:
14461 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14462 LPFC_EQ_CNT_1024);
14463 break;
14464 case 2048:
14465 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14466 LPFC_EQ_CNT_2048);
14467 break;
14468 case 4096:
14469 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14470 LPFC_EQ_CNT_4096);
14471 break;
14472 }
14473 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014474 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014475 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14476 putPaddrLow(dmabuf->phys);
14477 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14478 putPaddrHigh(dmabuf->phys);
14479 }
14480 mbox->vport = phba->pport;
14481 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14482 mbox->context1 = NULL;
14483 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040014484 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14485 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14486 if (shdr_status || shdr_add_status || rc) {
14487 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14488 "2500 EQ_CREATE mailbox failed with "
14489 "status x%x add_status x%x, mbx status x%x\n",
14490 shdr_status, shdr_add_status, rc);
14491 status = -ENXIO;
14492 }
14493 eq->type = LPFC_EQ;
14494 eq->subtype = LPFC_NONE;
14495 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14496 if (eq->queue_id == 0xFFFF)
14497 status = -ENXIO;
14498 eq->host_index = 0;
14499 eq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014500 eq->entry_repost = LPFC_EQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014501
James Smart8fa38512009-07-19 10:01:03 -040014502 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014503 return status;
14504}
14505
14506/**
14507 * lpfc_cq_create - Create a Completion Queue on the HBA
14508 * @phba: HBA structure that indicates port to create a queue on.
14509 * @cq: The queue structure to use to create the completion queue.
14510 * @eq: The event queue to bind this completion queue to.
14511 *
14512 * This function creates a completion queue, as detailed in @wq, on a port,
14513 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14514 *
14515 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14516 * is used to get the entry count and entry size that are necessary to
14517 * determine the number of pages to allocate and use for this queue. The @eq
14518 * is used to indicate which event queue to bind this completion queue to. This
14519 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14520 * completion queue. This function is asynchronous and will wait for the mailbox
14521 * command to finish before continuing.
14522 *
14523 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014524 * memory this function will return -ENOMEM. If the queue create mailbox command
14525 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014526 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014527int
James Smart4f774512009-05-22 14:52:35 -040014528lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14529 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14530{
14531 struct lpfc_mbx_cq_create *cq_create;
14532 struct lpfc_dmabuf *dmabuf;
14533 LPFC_MBOXQ_t *mbox;
14534 int rc, length, status = 0;
14535 uint32_t shdr_status, shdr_add_status;
14536 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014537 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14538
James Smart2e90f4b2011-12-13 13:22:37 -050014539 /* sanity check on queue memory */
14540 if (!cq || !eq)
14541 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014542 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014543 hw_page_size = cq->page_size;
James Smart49198b32010-04-06 15:04:33 -040014544
James Smart4f774512009-05-22 14:52:35 -040014545 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14546 if (!mbox)
14547 return -ENOMEM;
14548 length = (sizeof(struct lpfc_mbx_cq_create) -
14549 sizeof(struct lpfc_sli4_cfg_mhdr));
14550 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14551 LPFC_MBOX_OPCODE_CQ_CREATE,
14552 length, LPFC_SLI4_MBX_EMBED);
14553 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014554 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014555 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14556 cq->page_count);
14557 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14558 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014559 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14560 phba->sli4_hba.pc_sli4_params.cqv);
14561 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080014562 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14563 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014564 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14565 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080014566 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14567 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050014568 } else {
14569 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14570 eq->queue_id);
14571 }
James Smart4f774512009-05-22 14:52:35 -040014572 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014573 case 2048:
14574 case 4096:
14575 if (phba->sli4_hba.pc_sli4_params.cqv ==
14576 LPFC_Q_CREATE_VERSION_2) {
14577 cq_create->u.request.context.lpfc_cq_context_count =
14578 cq->entry_count;
14579 bf_set(lpfc_cq_context_count,
14580 &cq_create->u.request.context,
14581 LPFC_CQ_CNT_WORD7);
14582 break;
14583 }
14584 /* Fall Thru */
James Smart4f774512009-05-22 14:52:35 -040014585 default:
14586 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014587 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014588 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014589 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014590 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014591 if (cq->entry_count < 256) {
14592 status = -EINVAL;
14593 goto out;
14594 }
James Smart4f774512009-05-22 14:52:35 -040014595 /* otherwise default to smallest count (drop through) */
14596 case 256:
14597 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14598 LPFC_CQ_CNT_256);
14599 break;
14600 case 512:
14601 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14602 LPFC_CQ_CNT_512);
14603 break;
14604 case 1024:
14605 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14606 LPFC_CQ_CNT_1024);
14607 break;
14608 }
14609 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080014610 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040014611 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14612 putPaddrLow(dmabuf->phys);
14613 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14614 putPaddrHigh(dmabuf->phys);
14615 }
14616 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14617
14618 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014619 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14620 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14621 if (shdr_status || shdr_add_status || rc) {
14622 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14623 "2501 CQ_CREATE mailbox failed with "
14624 "status x%x add_status x%x, mbx status x%x\n",
14625 shdr_status, shdr_add_status, rc);
14626 status = -ENXIO;
14627 goto out;
14628 }
14629 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14630 if (cq->queue_id == 0xFFFF) {
14631 status = -ENXIO;
14632 goto out;
14633 }
14634 /* link the cq onto the parent eq child list */
14635 list_add_tail(&cq->list, &eq->child_list);
14636 /* Set up completion queue's type and subtype */
14637 cq->type = type;
14638 cq->subtype = subtype;
14639 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014640 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014641 cq->host_index = 0;
14642 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014643 cq->entry_repost = LPFC_CQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014644
James Smart8fa38512009-07-19 10:01:03 -040014645out:
14646 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014647 return status;
14648}
14649
14650/**
James Smart2d7dbc42017-02-12 13:52:35 -080014651 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14652 * @phba: HBA structure that indicates port to create a queue on.
14653 * @cqp: The queue structure array to use to create the completion queues.
14654 * @eqp: The event queue array to bind these completion queues to.
14655 *
14656 * This function creates a set of completion queue, s to support MRQ
14657 * as detailed in @cqp, on a port,
14658 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14659 *
14660 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14661 * is used to get the entry count and entry size that are necessary to
14662 * determine the number of pages to allocate and use for this queue. The @eq
14663 * is used to indicate which event queue to bind this completion queue to. This
14664 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14665 * completion queue. This function is asynchronous and will wait for the mailbox
14666 * command to finish before continuing.
14667 *
14668 * On success this function will return a zero. If unable to allocate enough
14669 * memory this function will return -ENOMEM. If the queue create mailbox command
14670 * fails this function will return -ENXIO.
14671 **/
14672int
14673lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14674 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14675{
14676 struct lpfc_queue *cq;
14677 struct lpfc_queue *eq;
14678 struct lpfc_mbx_cq_create_set *cq_set;
14679 struct lpfc_dmabuf *dmabuf;
14680 LPFC_MBOXQ_t *mbox;
14681 int rc, length, alloclen, status = 0;
14682 int cnt, idx, numcq, page_idx = 0;
14683 uint32_t shdr_status, shdr_add_status;
14684 union lpfc_sli4_cfg_shdr *shdr;
14685 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14686
14687 /* sanity check on queue memory */
14688 numcq = phba->cfg_nvmet_mrq;
14689 if (!cqp || !eqp || !numcq)
14690 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080014691
14692 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14693 if (!mbox)
14694 return -ENOMEM;
14695
14696 length = sizeof(struct lpfc_mbx_cq_create_set);
14697 length += ((numcq * cqp[0]->page_count) *
14698 sizeof(struct dma_address));
14699 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14700 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14701 LPFC_SLI4_MBX_NEMBED);
14702 if (alloclen < length) {
14703 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14704 "3098 Allocated DMA memory size (%d) is "
14705 "less than the requested DMA memory size "
14706 "(%d)\n", alloclen, length);
14707 status = -ENOMEM;
14708 goto out;
14709 }
14710 cq_set = mbox->sge_array->addr[0];
14711 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14712 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14713
14714 for (idx = 0; idx < numcq; idx++) {
14715 cq = cqp[idx];
14716 eq = eqp[idx];
14717 if (!cq || !eq) {
14718 status = -ENOMEM;
14719 goto out;
14720 }
James Smart81b96ed2017-11-20 16:00:29 -080014721 if (!phba->sli4_hba.pc_sli4_params.supported)
14722 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080014723
14724 switch (idx) {
14725 case 0:
14726 bf_set(lpfc_mbx_cq_create_set_page_size,
14727 &cq_set->u.request,
14728 (hw_page_size / SLI4_PAGE_SIZE));
14729 bf_set(lpfc_mbx_cq_create_set_num_pages,
14730 &cq_set->u.request, cq->page_count);
14731 bf_set(lpfc_mbx_cq_create_set_evt,
14732 &cq_set->u.request, 1);
14733 bf_set(lpfc_mbx_cq_create_set_valid,
14734 &cq_set->u.request, 1);
14735 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14736 &cq_set->u.request, 0);
14737 bf_set(lpfc_mbx_cq_create_set_num_cq,
14738 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080014739 bf_set(lpfc_mbx_cq_create_set_autovalid,
14740 &cq_set->u.request,
14741 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080014742 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014743 case 2048:
14744 case 4096:
14745 if (phba->sli4_hba.pc_sli4_params.cqv ==
14746 LPFC_Q_CREATE_VERSION_2) {
14747 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14748 &cq_set->u.request,
14749 cq->entry_count);
14750 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14751 &cq_set->u.request,
14752 LPFC_CQ_CNT_WORD7);
14753 break;
14754 }
14755 /* Fall Thru */
James Smart2d7dbc42017-02-12 13:52:35 -080014756 default:
14757 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14758 "3118 Bad CQ count. (%d)\n",
14759 cq->entry_count);
14760 if (cq->entry_count < 256) {
14761 status = -EINVAL;
14762 goto out;
14763 }
14764 /* otherwise default to smallest (drop thru) */
14765 case 256:
14766 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14767 &cq_set->u.request, LPFC_CQ_CNT_256);
14768 break;
14769 case 512:
14770 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14771 &cq_set->u.request, LPFC_CQ_CNT_512);
14772 break;
14773 case 1024:
14774 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14775 &cq_set->u.request, LPFC_CQ_CNT_1024);
14776 break;
14777 }
14778 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14779 &cq_set->u.request, eq->queue_id);
14780 break;
14781 case 1:
14782 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14783 &cq_set->u.request, eq->queue_id);
14784 break;
14785 case 2:
14786 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14787 &cq_set->u.request, eq->queue_id);
14788 break;
14789 case 3:
14790 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14791 &cq_set->u.request, eq->queue_id);
14792 break;
14793 case 4:
14794 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14795 &cq_set->u.request, eq->queue_id);
14796 break;
14797 case 5:
14798 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14799 &cq_set->u.request, eq->queue_id);
14800 break;
14801 case 6:
14802 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14803 &cq_set->u.request, eq->queue_id);
14804 break;
14805 case 7:
14806 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14807 &cq_set->u.request, eq->queue_id);
14808 break;
14809 case 8:
14810 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14811 &cq_set->u.request, eq->queue_id);
14812 break;
14813 case 9:
14814 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14815 &cq_set->u.request, eq->queue_id);
14816 break;
14817 case 10:
14818 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14819 &cq_set->u.request, eq->queue_id);
14820 break;
14821 case 11:
14822 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14823 &cq_set->u.request, eq->queue_id);
14824 break;
14825 case 12:
14826 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14827 &cq_set->u.request, eq->queue_id);
14828 break;
14829 case 13:
14830 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14831 &cq_set->u.request, eq->queue_id);
14832 break;
14833 case 14:
14834 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14835 &cq_set->u.request, eq->queue_id);
14836 break;
14837 case 15:
14838 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14839 &cq_set->u.request, eq->queue_id);
14840 break;
14841 }
14842
14843 /* link the cq onto the parent eq child list */
14844 list_add_tail(&cq->list, &eq->child_list);
14845 /* Set up completion queue's type and subtype */
14846 cq->type = type;
14847 cq->subtype = subtype;
14848 cq->assoc_qid = eq->queue_id;
14849 cq->host_index = 0;
14850 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014851 cq->entry_repost = LPFC_CQ_REPOST;
James Smart81b96ed2017-11-20 16:00:29 -080014852 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080014853
14854 rc = 0;
14855 list_for_each_entry(dmabuf, &cq->page_list, list) {
14856 memset(dmabuf->virt, 0, hw_page_size);
14857 cnt = page_idx + dmabuf->buffer_tag;
14858 cq_set->u.request.page[cnt].addr_lo =
14859 putPaddrLow(dmabuf->phys);
14860 cq_set->u.request.page[cnt].addr_hi =
14861 putPaddrHigh(dmabuf->phys);
14862 rc++;
14863 }
14864 page_idx += rc;
14865 }
14866
14867 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14868
14869 /* The IOCTL status is embedded in the mailbox subheader. */
14870 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14871 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14872 if (shdr_status || shdr_add_status || rc) {
14873 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14874 "3119 CQ_CREATE_SET mailbox failed with "
14875 "status x%x add_status x%x, mbx status x%x\n",
14876 shdr_status, shdr_add_status, rc);
14877 status = -ENXIO;
14878 goto out;
14879 }
14880 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14881 if (rc == 0xFFFF) {
14882 status = -ENXIO;
14883 goto out;
14884 }
14885
14886 for (idx = 0; idx < numcq; idx++) {
14887 cq = cqp[idx];
14888 cq->queue_id = rc + idx;
14889 }
14890
14891out:
14892 lpfc_sli4_mbox_cmd_free(phba, mbox);
14893 return status;
14894}
14895
14896/**
James Smartb19a0612010-04-06 14:48:51 -040014897 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040014898 * @phba: HBA structure that indicates port to create a queue on.
14899 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040014900 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14901 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040014902 *
James Smartb19a0612010-04-06 14:48:51 -040014903 * This function provides failback (fb) functionality when the
14904 * mq_create_ext fails on older FW generations. It's purpose is identical
14905 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040014906 *
James Smartb19a0612010-04-06 14:48:51 -040014907 * This routine cannot fail as all attributes were previously accessed and
14908 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040014909 **/
James Smartb19a0612010-04-06 14:48:51 -040014910static void
14911lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14912 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040014913{
14914 struct lpfc_mbx_mq_create *mq_create;
14915 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040014916 int length;
James Smart04c68492009-05-22 14:52:52 -040014917
James Smart04c68492009-05-22 14:52:52 -040014918 length = (sizeof(struct lpfc_mbx_mq_create) -
14919 sizeof(struct lpfc_sli4_cfg_mhdr));
14920 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14921 LPFC_MBOX_OPCODE_MQ_CREATE,
14922 length, LPFC_SLI4_MBX_EMBED);
14923 mq_create = &mbox->u.mqe.un.mq_create;
14924 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040014925 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040014926 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040014927 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040014928 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14929 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040014930 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014931 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14932 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040014933 break;
14934 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014935 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14936 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040014937 break;
14938 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014939 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14940 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040014941 break;
14942 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014943 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14944 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040014945 break;
14946 }
14947 list_for_each_entry(dmabuf, &mq->page_list, list) {
14948 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040014949 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040014950 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040014951 putPaddrHigh(dmabuf->phys);
14952 }
14953}
14954
14955/**
14956 * lpfc_mq_create - Create a mailbox Queue on the HBA
14957 * @phba: HBA structure that indicates port to create a queue on.
14958 * @mq: The queue structure to use to create the mailbox queue.
14959 * @cq: The completion queue to associate with this cq.
14960 * @subtype: The queue's subtype.
14961 *
14962 * This function creates a mailbox queue, as detailed in @mq, on a port,
14963 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14964 *
14965 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14966 * is used to get the entry count and entry size that are necessary to
14967 * determine the number of pages to allocate and use for this queue. This
14968 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14969 * mailbox queue. This function is asynchronous and will wait for the mailbox
14970 * command to finish before continuing.
14971 *
14972 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014973 * memory this function will return -ENOMEM. If the queue create mailbox command
14974 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040014975 **/
14976int32_t
14977lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14978 struct lpfc_queue *cq, uint32_t subtype)
14979{
14980 struct lpfc_mbx_mq_create *mq_create;
14981 struct lpfc_mbx_mq_create_ext *mq_create_ext;
14982 struct lpfc_dmabuf *dmabuf;
14983 LPFC_MBOXQ_t *mbox;
14984 int rc, length, status = 0;
14985 uint32_t shdr_status, shdr_add_status;
14986 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014987 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040014988
James Smart2e90f4b2011-12-13 13:22:37 -050014989 /* sanity check on queue memory */
14990 if (!mq || !cq)
14991 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014992 if (!phba->sli4_hba.pc_sli4_params.supported)
14993 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040014994
14995 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14996 if (!mbox)
14997 return -ENOMEM;
14998 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
14999 sizeof(struct lpfc_sli4_cfg_mhdr));
15000 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15001 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15002 length, LPFC_SLI4_MBX_EMBED);
15003
15004 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050015005 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050015006 bf_set(lpfc_mbx_mq_create_ext_num_pages,
15007 &mq_create_ext->u.request, mq->page_count);
15008 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15009 &mq_create_ext->u.request, 1);
15010 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040015011 &mq_create_ext->u.request, 1);
15012 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15013 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050015014 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15015 &mq_create_ext->u.request, 1);
15016 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15017 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040015018 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050015019 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15020 phba->sli4_hba.pc_sli4_params.mqv);
15021 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15022 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15023 cq->queue_id);
15024 else
15025 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15026 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040015027 switch (mq->entry_count) {
15028 default:
15029 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15030 "0362 Unsupported MQ count. (%d)\n",
15031 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015032 if (mq->entry_count < 16) {
15033 status = -EINVAL;
15034 goto out;
15035 }
James Smartb19a0612010-04-06 14:48:51 -040015036 /* otherwise default to smallest count (drop through) */
15037 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015038 bf_set(lpfc_mq_context_ring_size,
15039 &mq_create_ext->u.request.context,
15040 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040015041 break;
15042 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015043 bf_set(lpfc_mq_context_ring_size,
15044 &mq_create_ext->u.request.context,
15045 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040015046 break;
15047 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015048 bf_set(lpfc_mq_context_ring_size,
15049 &mq_create_ext->u.request.context,
15050 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040015051 break;
15052 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015053 bf_set(lpfc_mq_context_ring_size,
15054 &mq_create_ext->u.request.context,
15055 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040015056 break;
15057 }
15058 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015059 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040015060 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15061 putPaddrLow(dmabuf->phys);
15062 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040015063 putPaddrHigh(dmabuf->phys);
15064 }
15065 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040015066 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15067 &mq_create_ext->u.response);
15068 if (rc != MBX_SUCCESS) {
15069 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15070 "2795 MQ_CREATE_EXT failed with "
15071 "status x%x. Failback to MQ_CREATE.\n",
15072 rc);
15073 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15074 mq_create = &mbox->u.mqe.un.mq_create;
15075 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15076 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15077 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15078 &mq_create->u.response);
15079 }
15080
James Smart04c68492009-05-22 14:52:52 -040015081 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040015082 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15083 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15084 if (shdr_status || shdr_add_status || rc) {
15085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15086 "2502 MQ_CREATE mailbox failed with "
15087 "status x%x add_status x%x, mbx status x%x\n",
15088 shdr_status, shdr_add_status, rc);
15089 status = -ENXIO;
15090 goto out;
15091 }
James Smart04c68492009-05-22 14:52:52 -040015092 if (mq->queue_id == 0xFFFF) {
15093 status = -ENXIO;
15094 goto out;
15095 }
15096 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050015097 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040015098 mq->subtype = subtype;
15099 mq->host_index = 0;
15100 mq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070015101 mq->entry_repost = LPFC_MQ_REPOST;
James Smart04c68492009-05-22 14:52:52 -040015102
15103 /* link the mq onto the parent cq child list */
15104 list_add_tail(&mq->list, &cq->child_list);
15105out:
James Smart8fa38512009-07-19 10:01:03 -040015106 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015107 return status;
15108}
15109
15110/**
James Smart4f774512009-05-22 14:52:35 -040015111 * lpfc_wq_create - Create a Work Queue on the HBA
15112 * @phba: HBA structure that indicates port to create a queue on.
15113 * @wq: The queue structure to use to create the work queue.
15114 * @cq: The completion queue to bind this work queue to.
15115 * @subtype: The subtype of the work queue indicating its functionality.
15116 *
15117 * This function creates a work queue, as detailed in @wq, on a port, described
15118 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15119 *
15120 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15121 * is used to get the entry count and entry size that are necessary to
15122 * determine the number of pages to allocate and use for this queue. The @cq
15123 * is used to indicate which completion queue to bind this work queue to. This
15124 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15125 * work queue. This function is asynchronous and will wait for the mailbox
15126 * command to finish before continuing.
15127 *
15128 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015129 * memory this function will return -ENOMEM. If the queue create mailbox command
15130 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015131 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015132int
James Smart4f774512009-05-22 14:52:35 -040015133lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15134 struct lpfc_queue *cq, uint32_t subtype)
15135{
15136 struct lpfc_mbx_wq_create *wq_create;
15137 struct lpfc_dmabuf *dmabuf;
15138 LPFC_MBOXQ_t *mbox;
15139 int rc, length, status = 0;
15140 uint32_t shdr_status, shdr_add_status;
15141 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015142 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050015143 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050015144 void __iomem *bar_memmap_p;
15145 uint32_t db_offset;
15146 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080015147 uint8_t dpp_barset;
15148 uint32_t dpp_offset;
15149 unsigned long pg_addr;
James Smart81b96ed2017-11-20 16:00:29 -080015150 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040015151
James Smart2e90f4b2011-12-13 13:22:37 -050015152 /* sanity check on queue memory */
15153 if (!wq || !cq)
15154 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015155 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015156 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040015157
15158 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15159 if (!mbox)
15160 return -ENOMEM;
15161 length = (sizeof(struct lpfc_mbx_wq_create) -
15162 sizeof(struct lpfc_sli4_cfg_mhdr));
15163 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15164 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15165 length, LPFC_SLI4_MBX_EMBED);
15166 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015167 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015168 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15169 wq->page_count);
15170 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15171 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040015172
15173 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050015174 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15175 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050015176
James Smartc176ffa2018-01-30 15:58:46 -080015177 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15178 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080015179 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15180 else
15181 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15182
James Smart0c651872013-07-15 18:33:23 -040015183
James Smart1351e692018-02-22 08:18:43 -080015184 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15185 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15186 else
15187 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15188
15189 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040015190 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050015191 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15192 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070015193 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15194 LPFC_Q_CREATE_VERSION_1);
15195
James Smart5a6f1332011-03-11 16:05:35 -050015196 switch (wq->entry_size) {
15197 default:
15198 case 64:
15199 bf_set(lpfc_mbx_wq_create_wqe_size,
15200 &wq_create->u.request_1,
15201 LPFC_WQ_WQE_SIZE_64);
15202 break;
15203 case 128:
15204 bf_set(lpfc_mbx_wq_create_wqe_size,
15205 &wq_create->u.request_1,
15206 LPFC_WQ_WQE_SIZE_128);
15207 break;
15208 }
James Smart1351e692018-02-22 08:18:43 -080015209 /* Request DPP by default */
15210 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080015211 bf_set(lpfc_mbx_wq_create_page_size,
15212 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015213 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015214 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015215 break;
15216 default:
James Smart1351e692018-02-22 08:18:43 -080015217 page = wq_create->u.request.page;
15218 break;
James Smart5a6f1332011-03-11 16:05:35 -050015219 }
James Smart0c651872013-07-15 18:33:23 -040015220
James Smart4f774512009-05-22 14:52:35 -040015221 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015222 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015223 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15224 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015225 }
James Smart962bc512013-01-03 15:44:00 -050015226
15227 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15228 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15229
James Smart4f774512009-05-22 14:52:35 -040015230 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15231 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015232 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15233 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15234 if (shdr_status || shdr_add_status || rc) {
15235 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15236 "2503 WQ_CREATE mailbox failed with "
15237 "status x%x add_status x%x, mbx status x%x\n",
15238 shdr_status, shdr_add_status, rc);
15239 status = -ENXIO;
15240 goto out;
15241 }
James Smart1351e692018-02-22 08:18:43 -080015242
15243 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15244 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15245 &wq_create->u.response);
15246 else
15247 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15248 &wq_create->u.response_1);
15249
James Smart4f774512009-05-22 14:52:35 -040015250 if (wq->queue_id == 0xFFFF) {
15251 status = -ENXIO;
15252 goto out;
15253 }
James Smart1351e692018-02-22 08:18:43 -080015254
15255 wq->db_format = LPFC_DB_LIST_FORMAT;
15256 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15257 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15258 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15259 &wq_create->u.response);
15260 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15261 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15262 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15263 "3265 WQ[%d] doorbell format "
15264 "not supported: x%x\n",
15265 wq->queue_id, wq->db_format);
15266 status = -EINVAL;
15267 goto out;
15268 }
15269 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15270 &wq_create->u.response);
15271 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15272 pci_barset);
15273 if (!bar_memmap_p) {
15274 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15275 "3263 WQ[%d] failed to memmap "
15276 "pci barset:x%x\n",
15277 wq->queue_id, pci_barset);
15278 status = -ENOMEM;
15279 goto out;
15280 }
15281 db_offset = wq_create->u.response.doorbell_offset;
15282 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15283 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15284 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15285 "3252 WQ[%d] doorbell offset "
15286 "not supported: x%x\n",
15287 wq->queue_id, db_offset);
15288 status = -EINVAL;
15289 goto out;
15290 }
15291 wq->db_regaddr = bar_memmap_p + db_offset;
15292 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15293 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15294 "format:x%x\n", wq->queue_id,
15295 pci_barset, db_offset, wq->db_format);
15296 } else
15297 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015298 } else {
James Smart1351e692018-02-22 08:18:43 -080015299 /* Check if DPP was honored by the firmware */
15300 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15301 &wq_create->u.response_1);
15302 if (wq->dpp_enable) {
15303 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15304 &wq_create->u.response_1);
15305 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15306 pci_barset);
15307 if (!bar_memmap_p) {
15308 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15309 "3267 WQ[%d] failed to memmap "
15310 "pci barset:x%x\n",
15311 wq->queue_id, pci_barset);
15312 status = -ENOMEM;
15313 goto out;
15314 }
15315 db_offset = wq_create->u.response_1.doorbell_offset;
15316 wq->db_regaddr = bar_memmap_p + db_offset;
15317 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15318 &wq_create->u.response_1);
15319 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15320 &wq_create->u.response_1);
15321 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15322 dpp_barset);
15323 if (!bar_memmap_p) {
15324 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15325 "3268 WQ[%d] failed to memmap "
15326 "pci barset:x%x\n",
15327 wq->queue_id, dpp_barset);
15328 status = -ENOMEM;
15329 goto out;
15330 }
15331 dpp_offset = wq_create->u.response_1.dpp_offset;
15332 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15333 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15334 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15335 "dpp_id:x%x dpp_barset:x%x "
15336 "dpp_offset:x%x\n",
15337 wq->queue_id, pci_barset, db_offset,
15338 wq->dpp_id, dpp_barset, dpp_offset);
15339
15340 /* Enable combined writes for DPP aperture */
15341 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15342#ifdef CONFIG_X86
15343 rc = set_memory_wc(pg_addr, 1);
15344 if (rc) {
15345 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15346 "3272 Cannot setup Combined "
15347 "Write on WQ[%d] - disable DPP\n",
15348 wq->queue_id);
15349 phba->cfg_enable_dpp = 0;
15350 }
15351#else
15352 phba->cfg_enable_dpp = 0;
15353#endif
15354 } else
15355 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015356 }
James Smart895427b2017-02-12 13:52:30 -080015357 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15358 if (wq->pring == NULL) {
15359 status = -ENOMEM;
15360 goto out;
15361 }
James Smart4f774512009-05-22 14:52:35 -040015362 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015363 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015364 wq->subtype = subtype;
15365 wq->host_index = 0;
15366 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050015367 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040015368
15369 /* link the wq onto the parent cq child list */
15370 list_add_tail(&wq->list, &cq->child_list);
15371out:
James Smart8fa38512009-07-19 10:01:03 -040015372 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015373 return status;
15374}
15375
15376/**
15377 * lpfc_rq_create - Create a Receive Queue on the HBA
15378 * @phba: HBA structure that indicates port to create a queue on.
15379 * @hrq: The queue structure to use to create the header receive queue.
15380 * @drq: The queue structure to use to create the data receive queue.
15381 * @cq: The completion queue to bind this work queue to.
15382 *
15383 * This function creates a receive buffer queue pair , as detailed in @hrq and
15384 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15385 * to the HBA.
15386 *
15387 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15388 * struct is used to get the entry count that is necessary to determine the
15389 * number of pages to use for this queue. The @cq is used to indicate which
15390 * completion queue to bind received buffers that are posted to these queues to.
15391 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15392 * receive queue pair. This function is asynchronous and will wait for the
15393 * mailbox command to finish before continuing.
15394 *
15395 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015396 * memory this function will return -ENOMEM. If the queue create mailbox command
15397 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015398 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015399int
James Smart4f774512009-05-22 14:52:35 -040015400lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15401 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15402{
15403 struct lpfc_mbx_rq_create *rq_create;
15404 struct lpfc_dmabuf *dmabuf;
15405 LPFC_MBOXQ_t *mbox;
15406 int rc, length, status = 0;
15407 uint32_t shdr_status, shdr_add_status;
15408 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015409 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015410 void __iomem *bar_memmap_p;
15411 uint32_t db_offset;
15412 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015413
James Smart2e90f4b2011-12-13 13:22:37 -050015414 /* sanity check on queue memory */
15415 if (!hrq || !drq || !cq)
15416 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015417 if (!phba->sli4_hba.pc_sli4_params.supported)
15418 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015419
15420 if (hrq->entry_count != drq->entry_count)
15421 return -EINVAL;
15422 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15423 if (!mbox)
15424 return -ENOMEM;
15425 length = (sizeof(struct lpfc_mbx_rq_create) -
15426 sizeof(struct lpfc_sli4_cfg_mhdr));
15427 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15428 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15429 length, LPFC_SLI4_MBX_EMBED);
15430 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015431 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15432 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15433 phba->sli4_hba.pc_sli4_params.rqv);
15434 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15435 bf_set(lpfc_rq_context_rqe_count_1,
15436 &rq_create->u.request.context,
15437 hrq->entry_count);
15438 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015439 bf_set(lpfc_rq_context_rqe_size,
15440 &rq_create->u.request.context,
15441 LPFC_RQE_SIZE_8);
15442 bf_set(lpfc_rq_context_page_size,
15443 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015444 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015445 } else {
15446 switch (hrq->entry_count) {
15447 default:
15448 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15449 "2535 Unsupported RQ count. (%d)\n",
15450 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015451 if (hrq->entry_count < 512) {
15452 status = -EINVAL;
15453 goto out;
15454 }
James Smart5a6f1332011-03-11 16:05:35 -050015455 /* otherwise default to smallest count (drop through) */
15456 case 512:
15457 bf_set(lpfc_rq_context_rqe_count,
15458 &rq_create->u.request.context,
15459 LPFC_RQ_RING_SIZE_512);
15460 break;
15461 case 1024:
15462 bf_set(lpfc_rq_context_rqe_count,
15463 &rq_create->u.request.context,
15464 LPFC_RQ_RING_SIZE_1024);
15465 break;
15466 case 2048:
15467 bf_set(lpfc_rq_context_rqe_count,
15468 &rq_create->u.request.context,
15469 LPFC_RQ_RING_SIZE_2048);
15470 break;
15471 case 4096:
15472 bf_set(lpfc_rq_context_rqe_count,
15473 &rq_create->u.request.context,
15474 LPFC_RQ_RING_SIZE_4096);
15475 break;
15476 }
15477 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15478 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015479 }
15480 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15481 cq->queue_id);
15482 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15483 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015484 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015485 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015486 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15487 putPaddrLow(dmabuf->phys);
15488 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15489 putPaddrHigh(dmabuf->phys);
15490 }
James Smart962bc512013-01-03 15:44:00 -050015491 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15492 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15493
James Smart4f774512009-05-22 14:52:35 -040015494 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15495 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015496 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15497 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15498 if (shdr_status || shdr_add_status || rc) {
15499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15500 "2504 RQ_CREATE mailbox failed with "
15501 "status x%x add_status x%x, mbx status x%x\n",
15502 shdr_status, shdr_add_status, rc);
15503 status = -ENXIO;
15504 goto out;
15505 }
15506 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15507 if (hrq->queue_id == 0xFFFF) {
15508 status = -ENXIO;
15509 goto out;
15510 }
James Smart962bc512013-01-03 15:44:00 -050015511
15512 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15513 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15514 &rq_create->u.response);
15515 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15516 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15518 "3262 RQ [%d] doorbell format not "
15519 "supported: x%x\n", hrq->queue_id,
15520 hrq->db_format);
15521 status = -EINVAL;
15522 goto out;
15523 }
15524
15525 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15526 &rq_create->u.response);
15527 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15528 if (!bar_memmap_p) {
15529 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15530 "3269 RQ[%d] failed to memmap pci "
15531 "barset:x%x\n", hrq->queue_id,
15532 pci_barset);
15533 status = -ENOMEM;
15534 goto out;
15535 }
15536
15537 db_offset = rq_create->u.response.doorbell_offset;
15538 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15539 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15540 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15541 "3270 RQ[%d] doorbell offset not "
15542 "supported: x%x\n", hrq->queue_id,
15543 db_offset);
15544 status = -EINVAL;
15545 goto out;
15546 }
15547 hrq->db_regaddr = bar_memmap_p + db_offset;
15548 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015549 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15550 "format:x%x\n", hrq->queue_id, pci_barset,
15551 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015552 } else {
15553 hrq->db_format = LPFC_DB_RING_FORMAT;
15554 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15555 }
James Smart4f774512009-05-22 14:52:35 -040015556 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015557 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015558 hrq->subtype = subtype;
15559 hrq->host_index = 0;
15560 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015561 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015562
15563 /* now create the data queue */
15564 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15565 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15566 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015567 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15568 phba->sli4_hba.pc_sli4_params.rqv);
15569 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15570 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015571 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015572 if (subtype == LPFC_NVMET)
15573 rq_create->u.request.context.buffer_size =
15574 LPFC_NVMET_DATA_BUF_SIZE;
15575 else
15576 rq_create->u.request.context.buffer_size =
15577 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015578 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15579 LPFC_RQE_SIZE_8);
15580 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15581 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015582 } else {
15583 switch (drq->entry_count) {
15584 default:
15585 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15586 "2536 Unsupported RQ count. (%d)\n",
15587 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015588 if (drq->entry_count < 512) {
15589 status = -EINVAL;
15590 goto out;
15591 }
James Smart5a6f1332011-03-11 16:05:35 -050015592 /* otherwise default to smallest count (drop through) */
15593 case 512:
15594 bf_set(lpfc_rq_context_rqe_count,
15595 &rq_create->u.request.context,
15596 LPFC_RQ_RING_SIZE_512);
15597 break;
15598 case 1024:
15599 bf_set(lpfc_rq_context_rqe_count,
15600 &rq_create->u.request.context,
15601 LPFC_RQ_RING_SIZE_1024);
15602 break;
15603 case 2048:
15604 bf_set(lpfc_rq_context_rqe_count,
15605 &rq_create->u.request.context,
15606 LPFC_RQ_RING_SIZE_2048);
15607 break;
15608 case 4096:
15609 bf_set(lpfc_rq_context_rqe_count,
15610 &rq_create->u.request.context,
15611 LPFC_RQ_RING_SIZE_4096);
15612 break;
15613 }
James Smart3c603be2017-05-15 15:20:44 -070015614 if (subtype == LPFC_NVMET)
15615 bf_set(lpfc_rq_context_buf_size,
15616 &rq_create->u.request.context,
15617 LPFC_NVMET_DATA_BUF_SIZE);
15618 else
15619 bf_set(lpfc_rq_context_buf_size,
15620 &rq_create->u.request.context,
15621 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015622 }
15623 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15624 cq->queue_id);
15625 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15626 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015627 list_for_each_entry(dmabuf, &drq->page_list, list) {
15628 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15629 putPaddrLow(dmabuf->phys);
15630 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15631 putPaddrHigh(dmabuf->phys);
15632 }
James Smart962bc512013-01-03 15:44:00 -050015633 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15634 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015635 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15636 /* The IOCTL status is embedded in the mailbox subheader. */
15637 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15638 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15639 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15640 if (shdr_status || shdr_add_status || rc) {
15641 status = -ENXIO;
15642 goto out;
15643 }
15644 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15645 if (drq->queue_id == 0xFFFF) {
15646 status = -ENXIO;
15647 goto out;
15648 }
15649 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015650 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015651 drq->subtype = subtype;
15652 drq->host_index = 0;
15653 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015654 drq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015655
15656 /* link the header and data RQs onto the parent cq child list */
15657 list_add_tail(&hrq->list, &cq->child_list);
15658 list_add_tail(&drq->list, &cq->child_list);
15659
15660out:
James Smart8fa38512009-07-19 10:01:03 -040015661 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015662 return status;
15663}
15664
15665/**
James Smart2d7dbc42017-02-12 13:52:35 -080015666 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15667 * @phba: HBA structure that indicates port to create a queue on.
15668 * @hrqp: The queue structure array to use to create the header receive queues.
15669 * @drqp: The queue structure array to use to create the data receive queues.
15670 * @cqp: The completion queue array to bind these receive queues to.
15671 *
15672 * This function creates a receive buffer queue pair , as detailed in @hrq and
15673 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15674 * to the HBA.
15675 *
15676 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15677 * struct is used to get the entry count that is necessary to determine the
15678 * number of pages to use for this queue. The @cq is used to indicate which
15679 * completion queue to bind received buffers that are posted to these queues to.
15680 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15681 * receive queue pair. This function is asynchronous and will wait for the
15682 * mailbox command to finish before continuing.
15683 *
15684 * On success this function will return a zero. If unable to allocate enough
15685 * memory this function will return -ENOMEM. If the queue create mailbox command
15686 * fails this function will return -ENXIO.
15687 **/
15688int
15689lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15690 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15691 uint32_t subtype)
15692{
15693 struct lpfc_queue *hrq, *drq, *cq;
15694 struct lpfc_mbx_rq_create_v2 *rq_create;
15695 struct lpfc_dmabuf *dmabuf;
15696 LPFC_MBOXQ_t *mbox;
15697 int rc, length, alloclen, status = 0;
15698 int cnt, idx, numrq, page_idx = 0;
15699 uint32_t shdr_status, shdr_add_status;
15700 union lpfc_sli4_cfg_shdr *shdr;
15701 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15702
15703 numrq = phba->cfg_nvmet_mrq;
15704 /* sanity check on array memory */
15705 if (!hrqp || !drqp || !cqp || !numrq)
15706 return -ENODEV;
15707 if (!phba->sli4_hba.pc_sli4_params.supported)
15708 hw_page_size = SLI4_PAGE_SIZE;
15709
15710 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15711 if (!mbox)
15712 return -ENOMEM;
15713
15714 length = sizeof(struct lpfc_mbx_rq_create_v2);
15715 length += ((2 * numrq * hrqp[0]->page_count) *
15716 sizeof(struct dma_address));
15717
15718 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15719 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15720 LPFC_SLI4_MBX_NEMBED);
15721 if (alloclen < length) {
15722 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15723 "3099 Allocated DMA memory size (%d) is "
15724 "less than the requested DMA memory size "
15725 "(%d)\n", alloclen, length);
15726 status = -ENOMEM;
15727 goto out;
15728 }
15729
15730
15731
15732 rq_create = mbox->sge_array->addr[0];
15733 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15734
15735 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15736 cnt = 0;
15737
15738 for (idx = 0; idx < numrq; idx++) {
15739 hrq = hrqp[idx];
15740 drq = drqp[idx];
15741 cq = cqp[idx];
15742
James Smart2d7dbc42017-02-12 13:52:35 -080015743 /* sanity check on queue memory */
15744 if (!hrq || !drq || !cq) {
15745 status = -ENODEV;
15746 goto out;
15747 }
15748
James Smart7aabe842017-03-04 09:30:22 -080015749 if (hrq->entry_count != drq->entry_count) {
15750 status = -EINVAL;
15751 goto out;
15752 }
15753
James Smart2d7dbc42017-02-12 13:52:35 -080015754 if (idx == 0) {
15755 bf_set(lpfc_mbx_rq_create_num_pages,
15756 &rq_create->u.request,
15757 hrq->page_count);
15758 bf_set(lpfc_mbx_rq_create_rq_cnt,
15759 &rq_create->u.request, (numrq * 2));
15760 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15761 1);
15762 bf_set(lpfc_rq_context_base_cq,
15763 &rq_create->u.request.context,
15764 cq->queue_id);
15765 bf_set(lpfc_rq_context_data_size,
15766 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015767 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015768 bf_set(lpfc_rq_context_hdr_size,
15769 &rq_create->u.request.context,
15770 LPFC_HDR_BUF_SIZE);
15771 bf_set(lpfc_rq_context_rqe_count_1,
15772 &rq_create->u.request.context,
15773 hrq->entry_count);
15774 bf_set(lpfc_rq_context_rqe_size,
15775 &rq_create->u.request.context,
15776 LPFC_RQE_SIZE_8);
15777 bf_set(lpfc_rq_context_page_size,
15778 &rq_create->u.request.context,
15779 (PAGE_SIZE/SLI4_PAGE_SIZE));
15780 }
15781 rc = 0;
15782 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15783 memset(dmabuf->virt, 0, hw_page_size);
15784 cnt = page_idx + dmabuf->buffer_tag;
15785 rq_create->u.request.page[cnt].addr_lo =
15786 putPaddrLow(dmabuf->phys);
15787 rq_create->u.request.page[cnt].addr_hi =
15788 putPaddrHigh(dmabuf->phys);
15789 rc++;
15790 }
15791 page_idx += rc;
15792
15793 rc = 0;
15794 list_for_each_entry(dmabuf, &drq->page_list, list) {
15795 memset(dmabuf->virt, 0, hw_page_size);
15796 cnt = page_idx + dmabuf->buffer_tag;
15797 rq_create->u.request.page[cnt].addr_lo =
15798 putPaddrLow(dmabuf->phys);
15799 rq_create->u.request.page[cnt].addr_hi =
15800 putPaddrHigh(dmabuf->phys);
15801 rc++;
15802 }
15803 page_idx += rc;
15804
15805 hrq->db_format = LPFC_DB_RING_FORMAT;
15806 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15807 hrq->type = LPFC_HRQ;
15808 hrq->assoc_qid = cq->queue_id;
15809 hrq->subtype = subtype;
15810 hrq->host_index = 0;
15811 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015812 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015813
15814 drq->db_format = LPFC_DB_RING_FORMAT;
15815 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15816 drq->type = LPFC_DRQ;
15817 drq->assoc_qid = cq->queue_id;
15818 drq->subtype = subtype;
15819 drq->host_index = 0;
15820 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015821 drq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015822
15823 list_add_tail(&hrq->list, &cq->child_list);
15824 list_add_tail(&drq->list, &cq->child_list);
15825 }
15826
15827 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15828 /* The IOCTL status is embedded in the mailbox subheader. */
15829 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15830 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15831 if (shdr_status || shdr_add_status || rc) {
15832 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15833 "3120 RQ_CREATE mailbox failed with "
15834 "status x%x add_status x%x, mbx status x%x\n",
15835 shdr_status, shdr_add_status, rc);
15836 status = -ENXIO;
15837 goto out;
15838 }
15839 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15840 if (rc == 0xFFFF) {
15841 status = -ENXIO;
15842 goto out;
15843 }
15844
15845 /* Initialize all RQs with associated queue id */
15846 for (idx = 0; idx < numrq; idx++) {
15847 hrq = hrqp[idx];
15848 hrq->queue_id = rc + (2 * idx);
15849 drq = drqp[idx];
15850 drq->queue_id = rc + (2 * idx) + 1;
15851 }
15852
15853out:
15854 lpfc_sli4_mbox_cmd_free(phba, mbox);
15855 return status;
15856}
15857
15858/**
James Smart4f774512009-05-22 14:52:35 -040015859 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15860 * @eq: The queue structure associated with the queue to destroy.
15861 *
15862 * This function destroys a queue, as detailed in @eq by sending an mailbox
15863 * command, specific to the type of queue, to the HBA.
15864 *
15865 * The @eq struct is used to get the queue ID of the queue to destroy.
15866 *
15867 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015868 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015869 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015870int
James Smart4f774512009-05-22 14:52:35 -040015871lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15872{
15873 LPFC_MBOXQ_t *mbox;
15874 int rc, length, status = 0;
15875 uint32_t shdr_status, shdr_add_status;
15876 union lpfc_sli4_cfg_shdr *shdr;
15877
James Smart2e90f4b2011-12-13 13:22:37 -050015878 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015879 if (!eq)
15880 return -ENODEV;
15881 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15882 if (!mbox)
15883 return -ENOMEM;
15884 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15885 sizeof(struct lpfc_sli4_cfg_mhdr));
15886 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15887 LPFC_MBOX_OPCODE_EQ_DESTROY,
15888 length, LPFC_SLI4_MBX_EMBED);
15889 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15890 eq->queue_id);
15891 mbox->vport = eq->phba->pport;
15892 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15893
15894 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15895 /* The IOCTL status is embedded in the mailbox subheader. */
15896 shdr = (union lpfc_sli4_cfg_shdr *)
15897 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15898 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15899 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15900 if (shdr_status || shdr_add_status || rc) {
15901 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15902 "2505 EQ_DESTROY mailbox failed with "
15903 "status x%x add_status x%x, mbx status x%x\n",
15904 shdr_status, shdr_add_status, rc);
15905 status = -ENXIO;
15906 }
15907
15908 /* Remove eq from any list */
15909 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040015910 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015911 return status;
15912}
15913
15914/**
15915 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15916 * @cq: The queue structure associated with the queue to destroy.
15917 *
15918 * This function destroys a queue, as detailed in @cq by sending an mailbox
15919 * command, specific to the type of queue, to the HBA.
15920 *
15921 * The @cq struct is used to get the queue ID of the queue to destroy.
15922 *
15923 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015924 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015925 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015926int
James Smart4f774512009-05-22 14:52:35 -040015927lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15928{
15929 LPFC_MBOXQ_t *mbox;
15930 int rc, length, status = 0;
15931 uint32_t shdr_status, shdr_add_status;
15932 union lpfc_sli4_cfg_shdr *shdr;
15933
James Smart2e90f4b2011-12-13 13:22:37 -050015934 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015935 if (!cq)
15936 return -ENODEV;
15937 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15938 if (!mbox)
15939 return -ENOMEM;
15940 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15941 sizeof(struct lpfc_sli4_cfg_mhdr));
15942 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15943 LPFC_MBOX_OPCODE_CQ_DESTROY,
15944 length, LPFC_SLI4_MBX_EMBED);
15945 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15946 cq->queue_id);
15947 mbox->vport = cq->phba->pport;
15948 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15949 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15950 /* The IOCTL status is embedded in the mailbox subheader. */
15951 shdr = (union lpfc_sli4_cfg_shdr *)
15952 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15953 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15954 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15955 if (shdr_status || shdr_add_status || rc) {
15956 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15957 "2506 CQ_DESTROY mailbox failed with "
15958 "status x%x add_status x%x, mbx status x%x\n",
15959 shdr_status, shdr_add_status, rc);
15960 status = -ENXIO;
15961 }
15962 /* Remove cq from any list */
15963 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040015964 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015965 return status;
15966}
15967
15968/**
James Smart04c68492009-05-22 14:52:52 -040015969 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15970 * @qm: The queue structure associated with the queue to destroy.
15971 *
15972 * This function destroys a queue, as detailed in @mq by sending an mailbox
15973 * command, specific to the type of queue, to the HBA.
15974 *
15975 * The @mq struct is used to get the queue ID of the queue to destroy.
15976 *
15977 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015978 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040015979 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015980int
James Smart04c68492009-05-22 14:52:52 -040015981lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15982{
15983 LPFC_MBOXQ_t *mbox;
15984 int rc, length, status = 0;
15985 uint32_t shdr_status, shdr_add_status;
15986 union lpfc_sli4_cfg_shdr *shdr;
15987
James Smart2e90f4b2011-12-13 13:22:37 -050015988 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040015989 if (!mq)
15990 return -ENODEV;
15991 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15992 if (!mbox)
15993 return -ENOMEM;
15994 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15995 sizeof(struct lpfc_sli4_cfg_mhdr));
15996 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15997 LPFC_MBOX_OPCODE_MQ_DESTROY,
15998 length, LPFC_SLI4_MBX_EMBED);
15999 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16000 mq->queue_id);
16001 mbox->vport = mq->phba->pport;
16002 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16003 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16004 /* The IOCTL status is embedded in the mailbox subheader. */
16005 shdr = (union lpfc_sli4_cfg_shdr *)
16006 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16007 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16008 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16009 if (shdr_status || shdr_add_status || rc) {
16010 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16011 "2507 MQ_DESTROY mailbox failed with "
16012 "status x%x add_status x%x, mbx status x%x\n",
16013 shdr_status, shdr_add_status, rc);
16014 status = -ENXIO;
16015 }
16016 /* Remove mq from any list */
16017 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040016018 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040016019 return status;
16020}
16021
16022/**
James Smart4f774512009-05-22 14:52:35 -040016023 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16024 * @wq: The queue structure associated with the queue to destroy.
16025 *
16026 * This function destroys a queue, as detailed in @wq by sending an mailbox
16027 * command, specific to the type of queue, to the HBA.
16028 *
16029 * The @wq struct is used to get the queue ID of the queue to destroy.
16030 *
16031 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016032 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016033 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016034int
James Smart4f774512009-05-22 14:52:35 -040016035lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16036{
16037 LPFC_MBOXQ_t *mbox;
16038 int rc, length, status = 0;
16039 uint32_t shdr_status, shdr_add_status;
16040 union lpfc_sli4_cfg_shdr *shdr;
16041
James Smart2e90f4b2011-12-13 13:22:37 -050016042 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016043 if (!wq)
16044 return -ENODEV;
16045 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16046 if (!mbox)
16047 return -ENOMEM;
16048 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16049 sizeof(struct lpfc_sli4_cfg_mhdr));
16050 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16051 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16052 length, LPFC_SLI4_MBX_EMBED);
16053 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16054 wq->queue_id);
16055 mbox->vport = wq->phba->pport;
16056 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16057 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16058 shdr = (union lpfc_sli4_cfg_shdr *)
16059 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16060 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16061 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16062 if (shdr_status || shdr_add_status || rc) {
16063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16064 "2508 WQ_DESTROY mailbox failed with "
16065 "status x%x add_status x%x, mbx status x%x\n",
16066 shdr_status, shdr_add_status, rc);
16067 status = -ENXIO;
16068 }
16069 /* Remove wq from any list */
16070 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070016071 kfree(wq->pring);
16072 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040016073 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016074 return status;
16075}
16076
16077/**
16078 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16079 * @rq: The queue structure associated with the queue to destroy.
16080 *
16081 * This function destroys a queue, as detailed in @rq by sending an mailbox
16082 * command, specific to the type of queue, to the HBA.
16083 *
16084 * The @rq struct is used to get the queue ID of the queue to destroy.
16085 *
16086 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016087 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016088 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016089int
James Smart4f774512009-05-22 14:52:35 -040016090lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16091 struct lpfc_queue *drq)
16092{
16093 LPFC_MBOXQ_t *mbox;
16094 int rc, length, status = 0;
16095 uint32_t shdr_status, shdr_add_status;
16096 union lpfc_sli4_cfg_shdr *shdr;
16097
James Smart2e90f4b2011-12-13 13:22:37 -050016098 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016099 if (!hrq || !drq)
16100 return -ENODEV;
16101 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16102 if (!mbox)
16103 return -ENOMEM;
16104 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050016105 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040016106 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16107 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16108 length, LPFC_SLI4_MBX_EMBED);
16109 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16110 hrq->queue_id);
16111 mbox->vport = hrq->phba->pport;
16112 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16113 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16114 /* The IOCTL status is embedded in the mailbox subheader. */
16115 shdr = (union lpfc_sli4_cfg_shdr *)
16116 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16117 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16118 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16119 if (shdr_status || shdr_add_status || rc) {
16120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16121 "2509 RQ_DESTROY mailbox failed with "
16122 "status x%x add_status x%x, mbx status x%x\n",
16123 shdr_status, shdr_add_status, rc);
16124 if (rc != MBX_TIMEOUT)
16125 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16126 return -ENXIO;
16127 }
16128 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16129 drq->queue_id);
16130 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16131 shdr = (union lpfc_sli4_cfg_shdr *)
16132 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16133 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16134 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16135 if (shdr_status || shdr_add_status || rc) {
16136 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16137 "2510 RQ_DESTROY mailbox failed with "
16138 "status x%x add_status x%x, mbx status x%x\n",
16139 shdr_status, shdr_add_status, rc);
16140 status = -ENXIO;
16141 }
16142 list_del_init(&hrq->list);
16143 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040016144 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016145 return status;
16146}
16147
16148/**
16149 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16150 * @phba: The virtual port for which this call being executed.
16151 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16152 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16153 * @xritag: the xritag that ties this io to the SGL pages.
16154 *
16155 * This routine will post the sgl pages for the IO that has the xritag
16156 * that is in the iocbq structure. The xritag is assigned during iocbq
16157 * creation and persists for as long as the driver is loaded.
16158 * if the caller has fewer than 256 scatter gather segments to map then
16159 * pdma_phys_addr1 should be 0.
16160 * If the caller needs to map more than 256 scatter gather segment then
16161 * pdma_phys_addr1 should be a valid physical address.
16162 * physical address for SGLs must be 64 byte aligned.
16163 * If you are going to map 2 SGL's then the first one must have 256 entries
16164 * the second sgl can have between 1 and 256 entries.
16165 *
16166 * Return codes:
16167 * 0 - Success
16168 * -ENXIO, -ENOMEM - Failure
16169 **/
16170int
16171lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16172 dma_addr_t pdma_phys_addr0,
16173 dma_addr_t pdma_phys_addr1,
16174 uint16_t xritag)
16175{
16176 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16177 LPFC_MBOXQ_t *mbox;
16178 int rc;
16179 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040016180 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040016181 union lpfc_sli4_cfg_shdr *shdr;
16182
16183 if (xritag == NO_XRI) {
16184 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16185 "0364 Invalid param:\n");
16186 return -EINVAL;
16187 }
16188
16189 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16190 if (!mbox)
16191 return -ENOMEM;
16192
16193 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16194 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16195 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050016196 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040016197
16198 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16199 &mbox->u.mqe.un.post_sgl_pages;
16200 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16201 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16202
16203 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16204 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16205 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16206 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16207
16208 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16209 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16210 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16211 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16212 if (!phba->sli4_hba.intr_enable)
16213 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040016214 else {
James Smarta183a152011-10-10 21:32:43 -040016215 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040016216 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16217 }
James Smart4f774512009-05-22 14:52:35 -040016218 /* The IOCTL status is embedded in the mailbox subheader. */
16219 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16220 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16221 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16222 if (rc != MBX_TIMEOUT)
16223 mempool_free(mbox, phba->mbox_mem_pool);
16224 if (shdr_status || shdr_add_status || rc) {
16225 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16226 "2511 POST_SGL mailbox failed with "
16227 "status x%x add_status x%x, mbx status x%x\n",
16228 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040016229 }
16230 return 0;
16231}
James Smart4f774512009-05-22 14:52:35 -040016232
16233/**
James Smart88a2cfb2011-07-22 18:36:33 -040016234 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040016235 * @phba: pointer to lpfc hba data structure.
16236 *
16237 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040016238 * HBA consistent with the SLI-4 interface spec. This routine
16239 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16240 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040016241 *
James Smart88a2cfb2011-07-22 18:36:33 -040016242 * Returns
16243 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16244 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16245 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016246static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016247lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16248{
16249 unsigned long xri;
16250
16251 /*
16252 * Fetch the next logical xri. Because this index is logical,
16253 * the driver starts at 0 each time.
16254 */
16255 spin_lock_irq(&phba->hbalock);
16256 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16257 phba->sli4_hba.max_cfg_param.max_xri, 0);
16258 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16259 spin_unlock_irq(&phba->hbalock);
16260 return NO_XRI;
16261 } else {
16262 set_bit(xri, phba->sli4_hba.xri_bmask);
16263 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040016264 }
James Smart6d368e52011-05-24 11:44:12 -040016265 spin_unlock_irq(&phba->hbalock);
16266 return xri;
16267}
16268
16269/**
16270 * lpfc_sli4_free_xri - Release an xri for reuse.
16271 * @phba: pointer to lpfc hba data structure.
16272 *
16273 * This routine is invoked to release an xri to the pool of
16274 * available rpis maintained by the driver.
16275 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016276static void
James Smart6d368e52011-05-24 11:44:12 -040016277__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16278{
16279 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016280 phba->sli4_hba.max_cfg_param.xri_used--;
16281 }
16282}
16283
16284/**
16285 * lpfc_sli4_free_xri - Release an xri for reuse.
16286 * @phba: pointer to lpfc hba data structure.
16287 *
16288 * This routine is invoked to release an xri to the pool of
16289 * available rpis maintained by the driver.
16290 **/
16291void
16292lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16293{
16294 spin_lock_irq(&phba->hbalock);
16295 __lpfc_sli4_free_xri(phba, xri);
16296 spin_unlock_irq(&phba->hbalock);
16297}
16298
16299/**
James Smart4f774512009-05-22 14:52:35 -040016300 * lpfc_sli4_next_xritag - Get an xritag for the io
16301 * @phba: Pointer to HBA context object.
16302 *
16303 * This function gets an xritag for the iocb. If there is no unused xritag
16304 * it will return 0xffff.
16305 * The function returns the allocated xritag if successful, else returns zero.
16306 * Zero is not a valid xritag.
16307 * The caller is not required to hold any lock.
16308 **/
16309uint16_t
16310lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16311{
James Smart6d368e52011-05-24 11:44:12 -040016312 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016313
James Smart6d368e52011-05-24 11:44:12 -040016314 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016315 if (xri_index == NO_XRI)
16316 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16317 "2004 Failed to allocate XRI.last XRITAG is %d"
16318 " Max XRI is %d, Used XRI is %d\n",
16319 xri_index,
16320 phba->sli4_hba.max_cfg_param.max_xri,
16321 phba->sli4_hba.max_cfg_param.xri_used);
16322 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016323}
16324
16325/**
James Smart895427b2017-02-12 13:52:30 -080016326 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016327 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016328 * @post_sgl_list: pointer to els sgl entry list.
16329 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016330 *
16331 * This routine is invoked to post a block of driver's sgl pages to the
16332 * HBA using non-embedded mailbox command. No Lock is held. This routine
16333 * is only called when the driver is loading and after all IO has been
16334 * stopped.
16335 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016336static int
James Smart895427b2017-02-12 13:52:30 -080016337lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016338 struct list_head *post_sgl_list,
16339 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016340{
James Smart8a9d2e82012-05-09 21:16:12 -040016341 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016342 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16343 struct sgl_page_pairs *sgl_pg_pairs;
16344 void *viraddr;
16345 LPFC_MBOXQ_t *mbox;
16346 uint32_t reqlen, alloclen, pg_pairs;
16347 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016348 uint16_t xritag_start = 0;
16349 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016350 uint32_t shdr_status, shdr_add_status;
16351 union lpfc_sli4_cfg_shdr *shdr;
16352
James Smart895427b2017-02-12 13:52:30 -080016353 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016354 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016355 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016357 "2559 Block sgl registration required DMA "
16358 "size (%d) great than a page\n", reqlen);
16359 return -ENOMEM;
16360 }
James Smart895427b2017-02-12 13:52:30 -080016361
James Smart4f774512009-05-22 14:52:35 -040016362 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016363 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016364 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016365
16366 /* Allocate DMA memory and set up the non-embedded mailbox command */
16367 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16368 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16369 LPFC_SLI4_MBX_NEMBED);
16370
16371 if (alloclen < reqlen) {
16372 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16373 "0285 Allocated DMA memory size (%d) is "
16374 "less than the requested DMA memory "
16375 "size (%d)\n", alloclen, reqlen);
16376 lpfc_sli4_mbox_cmd_free(phba, mbox);
16377 return -ENOMEM;
16378 }
James Smart4f774512009-05-22 14:52:35 -040016379 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016380 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016381 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16382 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16383
James Smart8a9d2e82012-05-09 21:16:12 -040016384 pg_pairs = 0;
16385 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016386 /* Set up the sge entry */
16387 sgl_pg_pairs->sgl_pg0_addr_lo =
16388 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16389 sgl_pg_pairs->sgl_pg0_addr_hi =
16390 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16391 sgl_pg_pairs->sgl_pg1_addr_lo =
16392 cpu_to_le32(putPaddrLow(0));
16393 sgl_pg_pairs->sgl_pg1_addr_hi =
16394 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016395
James Smart4f774512009-05-22 14:52:35 -040016396 /* Keep the first xritag on the list */
16397 if (pg_pairs == 0)
16398 xritag_start = sglq_entry->sli4_xritag;
16399 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016400 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016401 }
James Smart6d368e52011-05-24 11:44:12 -040016402
16403 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016404 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016405 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016406 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016407
James Smart4f774512009-05-22 14:52:35 -040016408 if (!phba->sli4_hba.intr_enable)
16409 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16410 else {
James Smarta183a152011-10-10 21:32:43 -040016411 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016412 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16413 }
16414 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16415 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16416 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16417 if (rc != MBX_TIMEOUT)
16418 lpfc_sli4_mbox_cmd_free(phba, mbox);
16419 if (shdr_status || shdr_add_status || rc) {
16420 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16421 "2513 POST_SGL_BLOCK mailbox command failed "
16422 "status x%x add_status x%x mbx status x%x\n",
16423 shdr_status, shdr_add_status, rc);
16424 rc = -ENXIO;
16425 }
16426 return rc;
16427}
16428
16429/**
16430 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16431 * @phba: pointer to lpfc hba data structure.
16432 * @sblist: pointer to scsi buffer list.
16433 * @count: number of scsi buffers on the list.
16434 *
16435 * This routine is invoked to post a block of @count scsi sgl pages from a
16436 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16437 * No Lock is held.
16438 *
16439 **/
16440int
James Smart8a9d2e82012-05-09 21:16:12 -040016441lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16442 struct list_head *sblist,
16443 int count)
James Smart4f774512009-05-22 14:52:35 -040016444{
16445 struct lpfc_scsi_buf *psb;
16446 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16447 struct sgl_page_pairs *sgl_pg_pairs;
16448 void *viraddr;
16449 LPFC_MBOXQ_t *mbox;
16450 uint32_t reqlen, alloclen, pg_pairs;
16451 uint32_t mbox_tmo;
16452 uint16_t xritag_start = 0;
16453 int rc = 0;
16454 uint32_t shdr_status, shdr_add_status;
16455 dma_addr_t pdma_phys_bpl1;
16456 union lpfc_sli4_cfg_shdr *shdr;
16457
16458 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016459 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016460 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016461 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016462 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16463 "0217 Block sgl registration required DMA "
16464 "size (%d) great than a page\n", reqlen);
16465 return -ENOMEM;
16466 }
16467 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16468 if (!mbox) {
16469 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16470 "0283 Failed to allocate mbox cmd memory\n");
16471 return -ENOMEM;
16472 }
16473
16474 /* Allocate DMA memory and set up the non-embedded mailbox command */
16475 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16476 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16477 LPFC_SLI4_MBX_NEMBED);
16478
16479 if (alloclen < reqlen) {
16480 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16481 "2561 Allocated DMA memory size (%d) is "
16482 "less than the requested DMA memory "
16483 "size (%d)\n", alloclen, reqlen);
16484 lpfc_sli4_mbox_cmd_free(phba, mbox);
16485 return -ENOMEM;
16486 }
James Smart6d368e52011-05-24 11:44:12 -040016487
James Smart4f774512009-05-22 14:52:35 -040016488 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016489 viraddr = mbox->sge_array->addr[0];
16490
16491 /* Set up the SGL pages in the non-embedded DMA pages */
16492 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16493 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16494
16495 pg_pairs = 0;
16496 list_for_each_entry(psb, sblist, list) {
16497 /* Set up the sge entry */
16498 sgl_pg_pairs->sgl_pg0_addr_lo =
16499 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16500 sgl_pg_pairs->sgl_pg0_addr_hi =
16501 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16502 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16503 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16504 else
16505 pdma_phys_bpl1 = 0;
16506 sgl_pg_pairs->sgl_pg1_addr_lo =
16507 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16508 sgl_pg_pairs->sgl_pg1_addr_hi =
16509 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16510 /* Keep the first xritag on the list */
16511 if (pg_pairs == 0)
16512 xritag_start = psb->cur_iocbq.sli4_xritag;
16513 sgl_pg_pairs++;
16514 pg_pairs++;
16515 }
16516 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16517 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16518 /* Perform endian conversion if necessary */
16519 sgl->word0 = cpu_to_le32(sgl->word0);
16520
16521 if (!phba->sli4_hba.intr_enable)
16522 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16523 else {
James Smarta183a152011-10-10 21:32:43 -040016524 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016525 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16526 }
16527 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16528 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16529 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16530 if (rc != MBX_TIMEOUT)
16531 lpfc_sli4_mbox_cmd_free(phba, mbox);
16532 if (shdr_status || shdr_add_status || rc) {
16533 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16534 "2564 POST_SGL_BLOCK mailbox command failed "
16535 "status x%x add_status x%x mbx status x%x\n",
16536 shdr_status, shdr_add_status, rc);
16537 rc = -ENXIO;
16538 }
16539 return rc;
16540}
16541
16542/**
16543 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16544 * @phba: pointer to lpfc_hba struct that the frame was received on
16545 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16546 *
16547 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16548 * valid type of frame that the LPFC driver will handle. This function will
16549 * return a zero if the frame is a valid frame or a non zero value when the
16550 * frame does not pass the check.
16551 **/
16552static int
16553lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16554{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016555 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016556 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016557 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016558
James Smartae9e28f2017-05-15 15:20:51 -070016559#define FC_RCTL_MDS_DIAGS 0xF4
16560
James Smart4f774512009-05-22 14:52:35 -040016561 switch (fc_hdr->fh_r_ctl) {
16562 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16563 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16564 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16565 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16566 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16567 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16568 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16569 case FC_RCTL_DD_CMD_STATUS: /* command status */
16570 case FC_RCTL_ELS_REQ: /* extended link services request */
16571 case FC_RCTL_ELS_REP: /* extended link services reply */
16572 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16573 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16574 case FC_RCTL_BA_NOP: /* basic link service NOP */
16575 case FC_RCTL_BA_ABTS: /* basic link service abort */
16576 case FC_RCTL_BA_RMC: /* remove connection */
16577 case FC_RCTL_BA_ACC: /* basic accept */
16578 case FC_RCTL_BA_RJT: /* basic reject */
16579 case FC_RCTL_BA_PRMT:
16580 case FC_RCTL_ACK_1: /* acknowledge_1 */
16581 case FC_RCTL_ACK_0: /* acknowledge_0 */
16582 case FC_RCTL_P_RJT: /* port reject */
16583 case FC_RCTL_F_RJT: /* fabric reject */
16584 case FC_RCTL_P_BSY: /* port busy */
16585 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16586 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16587 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016588 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016589 case FC_RCTL_END: /* end */
16590 break;
16591 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16592 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16593 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16594 return lpfc_fc_frame_check(phba, fc_hdr);
16595 default:
16596 goto drop;
16597 }
James Smartae9e28f2017-05-15 15:20:51 -070016598
16599#define FC_TYPE_VENDOR_UNIQUE 0xFF
16600
James Smart4f774512009-05-22 14:52:35 -040016601 switch (fc_hdr->fh_type) {
16602 case FC_TYPE_BLS:
16603 case FC_TYPE_ELS:
16604 case FC_TYPE_FCP:
16605 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016606 case FC_TYPE_NVME:
James Smartae9e28f2017-05-15 15:20:51 -070016607 case FC_TYPE_VENDOR_UNIQUE:
James Smart4f774512009-05-22 14:52:35 -040016608 break;
16609 case FC_TYPE_IP:
16610 case FC_TYPE_ILS:
16611 default:
16612 goto drop;
16613 }
James Smart546fc852011-03-11 16:06:29 -050016614
James Smart4f774512009-05-22 14:52:35 -040016615 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016616 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040016617 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070016618 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16619 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16620 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16621 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16622 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016623 return 0;
16624drop:
16625 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016626 "2539 Dropped frame rctl:x%x type:x%x\n",
16627 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040016628 return 1;
16629}
16630
16631/**
16632 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16633 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16634 *
16635 * This function processes the FC header to retrieve the VFI from the VF
16636 * header, if one exists. This function will return the VFI if one exists
16637 * or 0 if no VSAN Header exists.
16638 **/
16639static uint32_t
16640lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16641{
16642 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16643
16644 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16645 return 0;
16646 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16647}
16648
16649/**
16650 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16651 * @phba: Pointer to the HBA structure to search for the vport on
16652 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16653 * @fcfi: The FC Fabric ID that the frame came from
16654 *
16655 * This function searches the @phba for a vport that matches the content of the
16656 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16657 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16658 * returns the matching vport pointer or NULL if unable to match frame to a
16659 * vport.
16660 **/
16661static struct lpfc_vport *
16662lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016663 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016664{
16665 struct lpfc_vport **vports;
16666 struct lpfc_vport *vport = NULL;
16667 int i;
James Smart939723a2012-05-09 21:19:03 -040016668
James Smartbf086112011-08-21 21:48:13 -040016669 if (did == Fabric_DID)
16670 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016671 if ((phba->pport->fc_flag & FC_PT2PT) &&
16672 !(phba->link_state == LPFC_HBA_READY))
16673 return phba->pport;
16674
James Smart4f774512009-05-22 14:52:35 -040016675 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016676 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016677 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16678 if (phba->fcf.fcfi == fcfi &&
16679 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16680 vports[i]->fc_myDID == did) {
16681 vport = vports[i];
16682 break;
16683 }
16684 }
James Smart895427b2017-02-12 13:52:30 -080016685 }
James Smart4f774512009-05-22 14:52:35 -040016686 lpfc_destroy_vport_work_array(phba, vports);
16687 return vport;
16688}
16689
16690/**
James Smart45ed1192009-10-02 15:17:02 -040016691 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16692 * @vport: The vport to work on.
16693 *
16694 * This function updates the receive sequence time stamp for this vport. The
16695 * receive sequence time stamp indicates the time that the last frame of the
16696 * the sequence that has been idle for the longest amount of time was received.
16697 * the driver uses this time stamp to indicate if any received sequences have
16698 * timed out.
16699 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016700static void
James Smart45ed1192009-10-02 15:17:02 -040016701lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16702{
16703 struct lpfc_dmabuf *h_buf;
16704 struct hbq_dmabuf *dmabuf = NULL;
16705
16706 /* get the oldest sequence on the rcv list */
16707 h_buf = list_get_first(&vport->rcv_buffer_list,
16708 struct lpfc_dmabuf, list);
16709 if (!h_buf)
16710 return;
16711 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16712 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16713}
16714
16715/**
16716 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16717 * @vport: The vport that the received sequences were sent to.
16718 *
16719 * This function cleans up all outstanding received sequences. This is called
16720 * by the driver when a link event or user action invalidates all the received
16721 * sequences.
16722 **/
16723void
16724lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16725{
16726 struct lpfc_dmabuf *h_buf, *hnext;
16727 struct lpfc_dmabuf *d_buf, *dnext;
16728 struct hbq_dmabuf *dmabuf = NULL;
16729
16730 /* start with the oldest sequence on the rcv list */
16731 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16732 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16733 list_del_init(&dmabuf->hbuf.list);
16734 list_for_each_entry_safe(d_buf, dnext,
16735 &dmabuf->dbuf.list, list) {
16736 list_del_init(&d_buf->list);
16737 lpfc_in_buf_free(vport->phba, d_buf);
16738 }
16739 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16740 }
16741}
16742
16743/**
16744 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16745 * @vport: The vport that the received sequences were sent to.
16746 *
16747 * This function determines whether any received sequences have timed out by
16748 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16749 * indicates that there is at least one timed out sequence this routine will
16750 * go through the received sequences one at a time from most inactive to most
16751 * active to determine which ones need to be cleaned up. Once it has determined
16752 * that a sequence needs to be cleaned up it will simply free up the resources
16753 * without sending an abort.
16754 **/
16755void
16756lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16757{
16758 struct lpfc_dmabuf *h_buf, *hnext;
16759 struct lpfc_dmabuf *d_buf, *dnext;
16760 struct hbq_dmabuf *dmabuf = NULL;
16761 unsigned long timeout;
16762 int abort_count = 0;
16763
16764 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16765 vport->rcv_buffer_time_stamp);
16766 if (list_empty(&vport->rcv_buffer_list) ||
16767 time_before(jiffies, timeout))
16768 return;
16769 /* start with the oldest sequence on the rcv list */
16770 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16771 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16772 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16773 dmabuf->time_stamp);
16774 if (time_before(jiffies, timeout))
16775 break;
16776 abort_count++;
16777 list_del_init(&dmabuf->hbuf.list);
16778 list_for_each_entry_safe(d_buf, dnext,
16779 &dmabuf->dbuf.list, list) {
16780 list_del_init(&d_buf->list);
16781 lpfc_in_buf_free(vport->phba, d_buf);
16782 }
16783 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16784 }
16785 if (abort_count)
16786 lpfc_update_rcv_time_stamp(vport);
16787}
16788
16789/**
James Smart4f774512009-05-22 14:52:35 -040016790 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16791 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16792 *
16793 * This function searches through the existing incomplete sequences that have
16794 * been sent to this @vport. If the frame matches one of the incomplete
16795 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16796 * make up that sequence. If no sequence is found that matches this frame then
16797 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16798 * This function returns a pointer to the first dmabuf in the sequence list that
16799 * the frame was linked to.
16800 **/
16801static struct hbq_dmabuf *
16802lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16803{
16804 struct fc_frame_header *new_hdr;
16805 struct fc_frame_header *temp_hdr;
16806 struct lpfc_dmabuf *d_buf;
16807 struct lpfc_dmabuf *h_buf;
16808 struct hbq_dmabuf *seq_dmabuf = NULL;
16809 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050016810 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040016811
James Smart4d9ab992009-10-02 15:16:39 -040016812 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016813 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040016814 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050016815
James Smart4f774512009-05-22 14:52:35 -040016816 /* Use the hdr_buf to find the sequence that this frame belongs to */
16817 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16818 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16819 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16820 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16821 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16822 continue;
16823 /* found a pending sequence that matches this frame */
16824 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16825 break;
16826 }
16827 if (!seq_dmabuf) {
16828 /*
16829 * This indicates first frame received for this sequence.
16830 * Queue the buffer on the vport's rcv_buffer_list.
16831 */
16832 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040016833 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016834 return dmabuf;
16835 }
16836 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050016837 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16838 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040016839 list_del_init(&seq_dmabuf->hbuf.list);
16840 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16841 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016842 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016843 return dmabuf;
16844 }
James Smart45ed1192009-10-02 15:17:02 -040016845 /* move this sequence to the tail to indicate a young sequence */
16846 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16847 seq_dmabuf->time_stamp = jiffies;
16848 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050016849 if (list_empty(&seq_dmabuf->dbuf.list)) {
16850 temp_hdr = dmabuf->hbuf.virt;
16851 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16852 return seq_dmabuf;
16853 }
James Smart4f774512009-05-22 14:52:35 -040016854 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050016855 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16856 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040016857 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16858 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16859 /*
16860 * If the frame's sequence count is greater than the frame on
16861 * the list then insert the frame right after this frame
16862 */
James Smarteeead812009-12-21 17:01:23 -050016863 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16864 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040016865 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050016866 found = 1;
16867 break;
James Smart4f774512009-05-22 14:52:35 -040016868 }
James Smart4360ca92015-12-16 18:12:04 -050016869
16870 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16871 break;
16872 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040016873 }
James Smart4360ca92015-12-16 18:12:04 -050016874
16875 if (found)
16876 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016877 return NULL;
16878}
16879
16880/**
James Smart6669f9b2009-10-02 15:16:45 -040016881 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16882 * @vport: pointer to a vitural port
16883 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16884 *
16885 * This function tries to abort from the partially assembed sequence, described
16886 * by the information from basic abbort @dmabuf. It checks to see whether such
16887 * partially assembled sequence held by the driver. If so, it shall free up all
16888 * the frames from the partially assembled sequence.
16889 *
16890 * Return
16891 * true -- if there is matching partially assembled sequence present and all
16892 * the frames freed with the sequence;
16893 * false -- if there is no matching partially assembled sequence present so
16894 * nothing got aborted in the lower layer driver
16895 **/
16896static bool
16897lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16898 struct hbq_dmabuf *dmabuf)
16899{
16900 struct fc_frame_header *new_hdr;
16901 struct fc_frame_header *temp_hdr;
16902 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16903 struct hbq_dmabuf *seq_dmabuf = NULL;
16904
16905 /* Use the hdr_buf to find the sequence that matches this frame */
16906 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16907 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16908 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16909 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16910 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16911 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16912 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16913 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16914 continue;
16915 /* found a pending sequence that matches this frame */
16916 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16917 break;
16918 }
16919
16920 /* Free up all the frames from the partially assembled sequence */
16921 if (seq_dmabuf) {
16922 list_for_each_entry_safe(d_buf, n_buf,
16923 &seq_dmabuf->dbuf.list, list) {
16924 list_del_init(&d_buf->list);
16925 lpfc_in_buf_free(vport->phba, d_buf);
16926 }
16927 return true;
16928 }
16929 return false;
16930}
16931
16932/**
James Smart6dd9e312013-01-03 15:43:37 -050016933 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16934 * @vport: pointer to a vitural port
16935 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16936 *
16937 * This function tries to abort from the assembed sequence from upper level
16938 * protocol, described by the information from basic abbort @dmabuf. It
16939 * checks to see whether such pending context exists at upper level protocol.
16940 * If so, it shall clean up the pending context.
16941 *
16942 * Return
16943 * true -- if there is matching pending context of the sequence cleaned
16944 * at ulp;
16945 * false -- if there is no matching pending context of the sequence present
16946 * at ulp.
16947 **/
16948static bool
16949lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16950{
16951 struct lpfc_hba *phba = vport->phba;
16952 int handled;
16953
16954 /* Accepting abort at ulp with SLI4 only */
16955 if (phba->sli_rev < LPFC_SLI_REV4)
16956 return false;
16957
16958 /* Register all caring upper level protocols to attend abort */
16959 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16960 if (handled)
16961 return true;
16962
16963 return false;
16964}
16965
16966/**
James Smart546fc852011-03-11 16:06:29 -050016967 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040016968 * @phba: Pointer to HBA context object.
16969 * @cmd_iocbq: pointer to the command iocbq structure.
16970 * @rsp_iocbq: pointer to the response iocbq structure.
16971 *
James Smart546fc852011-03-11 16:06:29 -050016972 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040016973 * event. It properly releases the memory allocated to the sequence abort
16974 * accept iocb.
16975 **/
16976static void
James Smart546fc852011-03-11 16:06:29 -050016977lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040016978 struct lpfc_iocbq *cmd_iocbq,
16979 struct lpfc_iocbq *rsp_iocbq)
16980{
James Smart6dd9e312013-01-03 15:43:37 -050016981 struct lpfc_nodelist *ndlp;
16982
16983 if (cmd_iocbq) {
16984 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16985 lpfc_nlp_put(ndlp);
16986 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016987 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050016988 }
James Smart6b5151f2012-01-18 16:24:06 -050016989
16990 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16991 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16992 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16993 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16994 rsp_iocbq->iocb.ulpStatus,
16995 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040016996}
16997
16998/**
James Smart6d368e52011-05-24 11:44:12 -040016999 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
17000 * @phba: Pointer to HBA context object.
17001 * @xri: xri id in transaction.
17002 *
17003 * This function validates the xri maps to the known range of XRIs allocated an
17004 * used by the driver.
17005 **/
James Smart7851fe22011-07-22 18:36:52 -040017006uint16_t
James Smart6d368e52011-05-24 11:44:12 -040017007lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
17008 uint16_t xri)
17009{
James Smarta2fc4aef2014-09-03 12:57:55 -040017010 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040017011
17012 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
17013 if (xri == phba->sli4_hba.xri_ids[i])
17014 return i;
17015 }
17016 return NO_XRI;
17017}
17018
James Smart6d368e52011-05-24 11:44:12 -040017019/**
James Smart546fc852011-03-11 16:06:29 -050017020 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017021 * @phba: Pointer to HBA context object.
17022 * @fc_hdr: pointer to a FC frame header.
17023 *
James Smart546fc852011-03-11 16:06:29 -050017024 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017025 * event after aborting the sequence handling.
17026 **/
James Smart86c67372017-04-21 16:05:04 -070017027void
James Smart6dd9e312013-01-03 15:43:37 -050017028lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17029 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040017030{
James Smart6dd9e312013-01-03 15:43:37 -050017031 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040017032 struct lpfc_iocbq *ctiocb = NULL;
17033 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040017034 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050017035 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040017036 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050017037 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040017038
17039 if (!lpfc_is_link_up(phba))
17040 return;
17041
17042 sid = sli4_sid_from_fc_hdr(fc_hdr);
17043 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050017044 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040017045
James Smart6dd9e312013-01-03 15:43:37 -050017046 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040017047 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070017048 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050017049 if (!ndlp) {
17050 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17051 "1268 Failed to allocate ndlp for "
17052 "oxid:x%x SID:x%x\n", oxid, sid);
17053 return;
17054 }
James Smart6dd9e312013-01-03 15:43:37 -050017055 /* Put ndlp onto pport node list */
17056 lpfc_enqueue_node(vport, ndlp);
17057 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17058 /* re-setup ndlp without removing from node list */
17059 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17060 if (!ndlp) {
17061 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17062 "3275 Failed to active ndlp found "
17063 "for oxid:x%x SID:x%x\n", oxid, sid);
17064 return;
17065 }
James Smart6669f9b2009-10-02 15:16:45 -040017066 }
17067
James Smart546fc852011-03-11 16:06:29 -050017068 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040017069 ctiocb = lpfc_sli_get_iocbq(phba);
17070 if (!ctiocb)
17071 return;
17072
James Smart5ffc2662009-11-18 15:39:44 -050017073 /* Extract the F_CTL field from FC_HDR */
17074 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17075
James Smart6669f9b2009-10-02 15:16:45 -040017076 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040017077 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050017078 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040017079 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17080 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17081 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17082
17083 /* Fill in the rest of iocb fields */
17084 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17085 icmd->ulpBdeCount = 0;
17086 icmd->ulpLe = 1;
17087 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040017088 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050017089 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017090
James Smart6669f9b2009-10-02 15:16:45 -040017091 ctiocb->iocb_cmpl = NULL;
17092 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050017093 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040017094 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050017095 ctiocb->sli4_xritag = NO_XRI;
17096
James Smartee0f4fe2012-05-09 21:19:14 -040017097 if (fctl & FC_FC_EX_CTX)
17098 /* Exchange responder sent the abort so we
17099 * own the oxid.
17100 */
17101 xri = oxid;
17102 else
17103 xri = rxid;
17104 lxri = lpfc_sli4_xri_inrange(phba, xri);
17105 if (lxri != NO_XRI)
17106 lpfc_set_rrq_active(phba, ndlp, lxri,
17107 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050017108 /* For BA_ABTS from exchange responder, if the logical xri with
17109 * the oxid maps to the FCP XRI range, the port no longer has
17110 * that exchange context, send a BLS_RJT. Override the IOCB for
17111 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050017112 */
James Smart6dd9e312013-01-03 15:43:37 -050017113 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080017114 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050017115 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17116 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17117 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17118 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17119 }
17120
17121 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17122 * the driver no longer has that exchange, send a BLS_RJT. Override
17123 * the IOCB for a BA_RJT.
17124 */
17125 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050017126 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17127 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17128 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17129 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17130 }
James Smart6669f9b2009-10-02 15:16:45 -040017131
James Smart5ffc2662009-11-18 15:39:44 -050017132 if (fctl & FC_FC_EX_CTX) {
17133 /* ABTS sent by responder to CT exchange, construction
17134 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17135 * field and RX_ID from ABTS for RX_ID field.
17136 */
James Smart546fc852011-03-11 16:06:29 -050017137 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050017138 } else {
17139 /* ABTS sent by initiator to CT exchange, construction
17140 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050017141 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050017142 */
James Smart546fc852011-03-11 16:06:29 -050017143 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050017144 }
James Smartf09c3ac2012-03-01 22:33:29 -050017145 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050017146 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050017147
James Smart546fc852011-03-11 16:06:29 -050017148 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050017149 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17150 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17151 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050017152
17153 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17154 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050017155 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17156 "2925 Failed to issue CT ABTS RSP x%x on "
17157 "xri x%x, Data x%x\n",
17158 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17159 phba->link_state);
17160 lpfc_nlp_put(ndlp);
17161 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050017162 lpfc_sli_release_iocbq(phba, ctiocb);
17163 }
James Smart6669f9b2009-10-02 15:16:45 -040017164}
17165
17166/**
17167 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17168 * @vport: Pointer to the vport on which this sequence was received
17169 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17170 *
17171 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17172 * receive sequence is only partially assembed by the driver, it shall abort
17173 * the partially assembled frames for the sequence. Otherwise, if the
17174 * unsolicited receive sequence has been completely assembled and passed to
17175 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17176 * unsolicited sequence has been aborted. After that, it will issue a basic
17177 * accept to accept the abort.
17178 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017179static void
James Smart6669f9b2009-10-02 15:16:45 -040017180lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17181 struct hbq_dmabuf *dmabuf)
17182{
17183 struct lpfc_hba *phba = vport->phba;
17184 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050017185 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050017186 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040017187
James Smart6669f9b2009-10-02 15:16:45 -040017188 /* Make a copy of fc_hdr before the dmabuf being released */
17189 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050017190 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040017191
James Smart5ffc2662009-11-18 15:39:44 -050017192 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050017193 /* ABTS by responder to exchange, no cleanup needed */
17194 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050017195 } else {
James Smart6dd9e312013-01-03 15:43:37 -050017196 /* ABTS by initiator to exchange, need to do cleanup */
17197 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17198 if (aborted == false)
17199 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050017200 }
James Smart6dd9e312013-01-03 15:43:37 -050017201 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17202
James Smart86c67372017-04-21 16:05:04 -070017203 if (phba->nvmet_support) {
17204 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17205 return;
17206 }
17207
James Smart6dd9e312013-01-03 15:43:37 -050017208 /* Respond with BA_ACC or BA_RJT accordingly */
17209 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040017210}
17211
17212/**
James Smart4f774512009-05-22 14:52:35 -040017213 * lpfc_seq_complete - Indicates if a sequence is complete
17214 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17215 *
17216 * This function checks the sequence, starting with the frame described by
17217 * @dmabuf, to see if all the frames associated with this sequence are present.
17218 * the frames associated with this sequence are linked to the @dmabuf using the
17219 * dbuf list. This function looks for two major things. 1) That the first frame
17220 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17221 * set. 3) That there are no holes in the sequence count. The function will
17222 * return 1 when the sequence is complete, otherwise it will return 0.
17223 **/
17224static int
17225lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17226{
17227 struct fc_frame_header *hdr;
17228 struct lpfc_dmabuf *d_buf;
17229 struct hbq_dmabuf *seq_dmabuf;
17230 uint32_t fctl;
17231 int seq_count = 0;
17232
17233 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17234 /* make sure first fame of sequence has a sequence count of zero */
17235 if (hdr->fh_seq_cnt != seq_count)
17236 return 0;
17237 fctl = (hdr->fh_f_ctl[0] << 16 |
17238 hdr->fh_f_ctl[1] << 8 |
17239 hdr->fh_f_ctl[2]);
17240 /* If last frame of sequence we can return success. */
17241 if (fctl & FC_FC_END_SEQ)
17242 return 1;
17243 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17244 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17245 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17246 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050017247 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040017248 return 0;
17249 fctl = (hdr->fh_f_ctl[0] << 16 |
17250 hdr->fh_f_ctl[1] << 8 |
17251 hdr->fh_f_ctl[2]);
17252 /* If last frame of sequence we can return success. */
17253 if (fctl & FC_FC_END_SEQ)
17254 return 1;
17255 }
17256 return 0;
17257}
17258
17259/**
17260 * lpfc_prep_seq - Prep sequence for ULP processing
17261 * @vport: Pointer to the vport on which this sequence was received
17262 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17263 *
17264 * This function takes a sequence, described by a list of frames, and creates
17265 * a list of iocbq structures to describe the sequence. This iocbq list will be
17266 * used to issue to the generic unsolicited sequence handler. This routine
17267 * returns a pointer to the first iocbq in the list. If the function is unable
17268 * to allocate an iocbq then it throw out the received frames that were not
17269 * able to be described and return a pointer to the first iocbq. If unable to
17270 * allocate any iocbqs (including the first) this function will return NULL.
17271 **/
17272static struct lpfc_iocbq *
17273lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17274{
James Smart7851fe22011-07-22 18:36:52 -040017275 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040017276 struct lpfc_dmabuf *d_buf, *n_buf;
17277 struct lpfc_iocbq *first_iocbq, *iocbq;
17278 struct fc_frame_header *fc_hdr;
17279 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040017280 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050017281 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040017282
17283 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17284 /* remove from receive buffer list */
17285 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017286 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017287 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040017288 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040017289 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017290 /* Get an iocbq struct to fill in. */
17291 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17292 if (first_iocbq) {
17293 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040017294 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017295 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080017296 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040017297
17298 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17299 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17300 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17301 first_iocbq->iocb.un.rcvels.parmRo =
17302 sli4_did_from_fc_hdr(fc_hdr);
17303 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17304 } else
17305 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040017306 first_iocbq->iocb.ulpContext = NO_XRI;
17307 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17308 be16_to_cpu(fc_hdr->fh_ox_id);
17309 /* iocbq is prepped for internal consumption. Physical vpi. */
17310 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17311 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040017312 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040017313 tot_len = bf_get(lpfc_rcqe_length,
17314 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17315
James Smart4f774512009-05-22 14:52:35 -040017316 first_iocbq->context2 = &seq_dmabuf->dbuf;
17317 first_iocbq->context3 = NULL;
17318 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040017319 if (tot_len > LPFC_DATA_BUF_SIZE)
17320 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040017321 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040017322 else
17323 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17324
James Smart4f774512009-05-22 14:52:35 -040017325 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040017326
James Smart7851fe22011-07-22 18:36:52 -040017327 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040017328 }
17329 iocbq = first_iocbq;
17330 /*
17331 * Each IOCBq can have two Buffers assigned, so go through the list
17332 * of buffers for this sequence and save two buffers in each IOCBq
17333 */
17334 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17335 if (!iocbq) {
17336 lpfc_in_buf_free(vport->phba, d_buf);
17337 continue;
17338 }
17339 if (!iocbq->context3) {
17340 iocbq->context3 = d_buf;
17341 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040017342 /* We need to get the size out of the right CQE */
17343 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17344 len = bf_get(lpfc_rcqe_length,
17345 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017346 pbde = (struct ulp_bde64 *)
17347 &iocbq->iocb.unsli3.sli3Words[4];
17348 if (len > LPFC_DATA_BUF_SIZE)
17349 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17350 else
17351 pbde->tus.f.bdeSize = len;
17352
James Smart7851fe22011-07-22 18:36:52 -040017353 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17354 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040017355 } else {
17356 iocbq = lpfc_sli_get_iocbq(vport->phba);
17357 if (!iocbq) {
17358 if (first_iocbq) {
17359 first_iocbq->iocb.ulpStatus =
17360 IOSTAT_FCP_RSP_ERROR;
17361 first_iocbq->iocb.un.ulpWord[4] =
17362 IOERR_NO_RESOURCES;
17363 }
17364 lpfc_in_buf_free(vport->phba, d_buf);
17365 continue;
17366 }
James Smart7851fe22011-07-22 18:36:52 -040017367 /* We need to get the size out of the right CQE */
17368 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17369 len = bf_get(lpfc_rcqe_length,
17370 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017371 iocbq->context2 = d_buf;
17372 iocbq->context3 = NULL;
17373 iocbq->iocb.ulpBdeCount = 1;
17374 if (len > LPFC_DATA_BUF_SIZE)
17375 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17376 LPFC_DATA_BUF_SIZE;
17377 else
17378 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17379
James Smart7851fe22011-07-22 18:36:52 -040017380 tot_len += len;
17381 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17382
James Smart4f774512009-05-22 14:52:35 -040017383 iocbq->iocb.un.rcvels.remoteID = sid;
17384 list_add_tail(&iocbq->list, &first_iocbq->list);
17385 }
17386 }
17387 return first_iocbq;
17388}
17389
James Smart6669f9b2009-10-02 15:16:45 -040017390static void
17391lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17392 struct hbq_dmabuf *seq_dmabuf)
17393{
17394 struct fc_frame_header *fc_hdr;
17395 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17396 struct lpfc_hba *phba = vport->phba;
17397
17398 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17399 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17400 if (!iocbq) {
17401 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17402 "2707 Ring %d handler: Failed to allocate "
17403 "iocb Rctl x%x Type x%x received\n",
17404 LPFC_ELS_RING,
17405 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17406 return;
17407 }
17408 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017409 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017410 iocbq, fc_hdr->fh_r_ctl,
17411 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017412 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017413 "2540 Ring %d handler: unexpected Rctl "
17414 "x%x Type x%x received\n",
17415 LPFC_ELS_RING,
17416 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17417
17418 /* Free iocb created in lpfc_prep_seq */
17419 list_for_each_entry_safe(curr_iocb, next_iocb,
17420 &iocbq->list, list) {
17421 list_del_init(&curr_iocb->list);
17422 lpfc_sli_release_iocbq(phba, curr_iocb);
17423 }
17424 lpfc_sli_release_iocbq(phba, iocbq);
17425}
17426
James Smartae9e28f2017-05-15 15:20:51 -070017427static void
17428lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17429 struct lpfc_iocbq *rspiocb)
17430{
17431 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17432
17433 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017434 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017435 kfree(pcmd);
17436 lpfc_sli_release_iocbq(phba, cmdiocb);
17437}
17438
17439static void
17440lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17441 struct hbq_dmabuf *dmabuf)
17442{
17443 struct fc_frame_header *fc_hdr;
17444 struct lpfc_hba *phba = vport->phba;
17445 struct lpfc_iocbq *iocbq = NULL;
17446 union lpfc_wqe *wqe;
17447 struct lpfc_dmabuf *pcmd = NULL;
17448 uint32_t frame_len;
17449 int rc;
17450
17451 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17452 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17453
17454 /* Send the received frame back */
17455 iocbq = lpfc_sli_get_iocbq(phba);
17456 if (!iocbq)
17457 goto exit;
17458
17459 /* Allocate buffer for command payload */
17460 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17461 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020017462 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070017463 &pcmd->phys);
17464 if (!pcmd || !pcmd->virt)
17465 goto exit;
17466
17467 INIT_LIST_HEAD(&pcmd->list);
17468
17469 /* copyin the payload */
17470 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17471
17472 /* fill in BDE's for command */
17473 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17474 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17475 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17476 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17477
17478 iocbq->context2 = pcmd;
17479 iocbq->vport = vport;
17480 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17481 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17482
17483 /*
17484 * Setup rest of the iocb as though it were a WQE
17485 * Build the SEND_FRAME WQE
17486 */
17487 wqe = (union lpfc_wqe *)&iocbq->iocb;
17488
17489 wqe->send_frame.frame_len = frame_len;
17490 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17491 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17492 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17493 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17494 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17495 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17496
17497 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17498 iocbq->iocb.ulpLe = 1;
17499 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17500 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17501 if (rc == IOCB_ERROR)
17502 goto exit;
17503
17504 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17505 return;
17506
17507exit:
17508 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17509 "2023 Unable to process MDS loopback frame\n");
17510 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017511 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017512 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070017513 if (iocbq)
17514 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070017515 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17516}
17517
James Smart4f774512009-05-22 14:52:35 -040017518/**
17519 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17520 * @phba: Pointer to HBA context object.
17521 *
17522 * This function is called with no lock held. This function processes all
17523 * the received buffers and gives it to upper layers when a received buffer
17524 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017525 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017526 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17527 * appropriate receive function when the final frame in a sequence is received.
17528 **/
James Smart4d9ab992009-10-02 15:16:39 -040017529void
17530lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17531 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017532{
James Smart4d9ab992009-10-02 15:16:39 -040017533 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017534 struct fc_frame_header *fc_hdr;
17535 struct lpfc_vport *vport;
17536 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017537 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017538
James Smart4f774512009-05-22 14:52:35 -040017539 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017540 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017541
James Smart4d9ab992009-10-02 15:16:39 -040017542 /* check to see if this a valid type of frame */
17543 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17544 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17545 return;
17546 }
James Smart2ea259e2017-02-12 13:52:27 -080017547
James Smart7851fe22011-07-22 18:36:52 -040017548 if ((bf_get(lpfc_cqe_code,
17549 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17550 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17551 &dmabuf->cq_event.cqe.rcqe_cmpl);
17552 else
17553 fcfi = bf_get(lpfc_rcqe_fcf_id,
17554 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017555
James Smartae9e28f2017-05-15 15:20:51 -070017556 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17557 vport = phba->pport;
17558 /* Handle MDS Loopback frames */
17559 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17560 return;
17561 }
17562
James Smart895427b2017-02-12 13:52:30 -080017563 /* d_id this frame is directed to */
17564 did = sli4_did_from_fc_hdr(fc_hdr);
17565
17566 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017567 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017568 /* throw out the frame */
17569 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17570 return;
17571 }
James Smart939723a2012-05-09 21:19:03 -040017572
James Smart939723a2012-05-09 21:19:03 -040017573 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17574 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17575 (did != Fabric_DID)) {
17576 /*
17577 * Throw out the frame if we are not pt2pt.
17578 * The pt2pt protocol allows for discovery frames
17579 * to be received without a registered VPI.
17580 */
17581 if (!(vport->fc_flag & FC_PT2PT) ||
17582 (phba->link_state == LPFC_HBA_READY)) {
17583 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17584 return;
17585 }
17586 }
17587
James Smart6669f9b2009-10-02 15:16:45 -040017588 /* Handle the basic abort sequence (BA_ABTS) event */
17589 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17590 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17591 return;
17592 }
17593
James Smart4d9ab992009-10-02 15:16:39 -040017594 /* Link this frame */
17595 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17596 if (!seq_dmabuf) {
17597 /* unable to add frame to vport - throw it out */
17598 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17599 return;
17600 }
17601 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017602 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017603 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017604
James Smart6669f9b2009-10-02 15:16:45 -040017605 /* Send the complete sequence to the upper layer protocol */
17606 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017607}
James Smart6fb120a2009-05-22 14:52:59 -040017608
17609/**
17610 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17611 * @phba: pointer to lpfc hba data structure.
17612 *
17613 * This routine is invoked to post rpi header templates to the
17614 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017615 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17616 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017617 *
17618 * This routine does not require any locks. It's usage is expected
17619 * to be driver load or reset recovery when the driver is
17620 * sequential.
17621 *
17622 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017623 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017624 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017625 * When this error occurs, the driver is not guaranteed
17626 * to have any rpi regions posted to the device and
17627 * must either attempt to repost the regions or take a
17628 * fatal error.
17629 **/
17630int
17631lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17632{
17633 struct lpfc_rpi_hdr *rpi_page;
17634 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017635 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017636
James Smart6d368e52011-05-24 11:44:12 -040017637 /* SLI4 ports that support extents do not require RPI headers. */
17638 if (!phba->sli4_hba.rpi_hdrs_in_use)
17639 goto exit;
17640 if (phba->sli4_hba.extents_in_use)
17641 return -EIO;
17642
James Smart6fb120a2009-05-22 14:52:59 -040017643 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017644 /*
17645 * Assign the rpi headers a physical rpi only if the driver
17646 * has not initialized those resources. A port reset only
17647 * needs the headers posted.
17648 */
17649 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17650 LPFC_RPI_RSRC_RDY)
17651 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17652
James Smart6fb120a2009-05-22 14:52:59 -040017653 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17654 if (rc != MBX_SUCCESS) {
17655 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17656 "2008 Error %d posting all rpi "
17657 "headers\n", rc);
17658 rc = -EIO;
17659 break;
17660 }
17661 }
17662
James Smart6d368e52011-05-24 11:44:12 -040017663 exit:
17664 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17665 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017666 return rc;
17667}
17668
17669/**
17670 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17671 * @phba: pointer to lpfc hba data structure.
17672 * @rpi_page: pointer to the rpi memory region.
17673 *
17674 * This routine is invoked to post a single rpi header to the
17675 * HBA consistent with the SLI-4 interface spec. This memory region
17676 * maps up to 64 rpi context regions.
17677 *
17678 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017679 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017680 * -ENOMEM - No available memory
17681 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017682 **/
17683int
17684lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17685{
17686 LPFC_MBOXQ_t *mboxq;
17687 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17688 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017689 uint32_t shdr_status, shdr_add_status;
17690 union lpfc_sli4_cfg_shdr *shdr;
17691
James Smart6d368e52011-05-24 11:44:12 -040017692 /* SLI4 ports that support extents do not require RPI headers. */
17693 if (!phba->sli4_hba.rpi_hdrs_in_use)
17694 return rc;
17695 if (phba->sli4_hba.extents_in_use)
17696 return -EIO;
17697
James Smart6fb120a2009-05-22 14:52:59 -040017698 /* The port is notified of the header region via a mailbox command. */
17699 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17700 if (!mboxq) {
17701 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17702 "2001 Unable to allocate memory for issuing "
17703 "SLI_CONFIG_SPECIAL mailbox command\n");
17704 return -ENOMEM;
17705 }
17706
17707 /* Post all rpi memory regions to the port. */
17708 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040017709 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17710 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17711 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050017712 sizeof(struct lpfc_sli4_cfg_mhdr),
17713 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040017714
17715
17716 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040017717 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17718 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040017719 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17720 hdr_tmpl, rpi_page->page_count);
17721
James Smart6fb120a2009-05-22 14:52:59 -040017722 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17723 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040017724 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040017725 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17726 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17727 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17728 if (rc != MBX_TIMEOUT)
17729 mempool_free(mboxq, phba->mbox_mem_pool);
17730 if (shdr_status || shdr_add_status || rc) {
17731 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17732 "2514 POST_RPI_HDR mailbox failed with "
17733 "status x%x add_status x%x, mbx status x%x\n",
17734 shdr_status, shdr_add_status, rc);
17735 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070017736 } else {
17737 /*
17738 * The next_rpi stores the next logical module-64 rpi value used
17739 * to post physical rpis in subsequent rpi postings.
17740 */
17741 spin_lock_irq(&phba->hbalock);
17742 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17743 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040017744 }
17745 return rc;
17746}
17747
17748/**
17749 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17750 * @phba: pointer to lpfc hba data structure.
17751 *
17752 * This routine is invoked to post rpi header templates to the
17753 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017754 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17755 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017756 *
17757 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017758 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040017759 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17760 **/
17761int
17762lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17763{
James Smart6d368e52011-05-24 11:44:12 -040017764 unsigned long rpi;
17765 uint16_t max_rpi, rpi_limit;
17766 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017767 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040017768 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040017769
James Smart6fb120a2009-05-22 14:52:59 -040017770 /*
James Smart6d368e52011-05-24 11:44:12 -040017771 * Fetch the next logical rpi. Because this index is logical,
17772 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040017773 */
James Smart4902b382013-10-10 12:20:35 -040017774 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040017775 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17776 rpi_limit = phba->sli4_hba.next_rpi;
17777
James Smart6d368e52011-05-24 11:44:12 -040017778 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17779 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040017780 rpi = LPFC_RPI_ALLOC_ERROR;
17781 else {
17782 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17783 phba->sli4_hba.max_cfg_param.rpi_used++;
17784 phba->sli4_hba.rpi_count++;
17785 }
James Smartbe6bb942015-04-07 15:07:22 -040017786 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17787 "0001 rpi:%x max:%x lim:%x\n",
17788 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040017789
17790 /*
17791 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040017792 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040017793 */
17794 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17795 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040017796 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017797 return rpi;
17798 }
17799
17800 /*
James Smart6d368e52011-05-24 11:44:12 -040017801 * RPI header postings are not required for SLI4 ports capable of
17802 * extents.
17803 */
17804 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040017805 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040017806 return rpi;
17807 }
17808
17809 /*
James Smart6fb120a2009-05-22 14:52:59 -040017810 * If the driver is running low on rpi resources, allocate another
17811 * page now. Note that the next_rpi value is used because
17812 * it represents how many are actually in use whereas max_rpi notes
17813 * how many are supported max by the device.
17814 */
James Smart6d368e52011-05-24 11:44:12 -040017815 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040017816 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017817 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17818 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17819 if (!rpi_hdr) {
17820 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17821 "2002 Error Could not grow rpi "
17822 "count\n");
17823 } else {
James Smart6d368e52011-05-24 11:44:12 -040017824 lrpi = rpi_hdr->start_rpi;
17825 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040017826 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17827 }
17828 }
17829
17830 return rpi;
17831}
17832
17833/**
17834 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17835 * @phba: pointer to lpfc hba data structure.
17836 *
17837 * This routine is invoked to release an rpi to the pool of
17838 * available rpis maintained by the driver.
17839 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017840static void
James Smartd7c47992010-06-08 18:31:54 -040017841__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17842{
17843 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17844 phba->sli4_hba.rpi_count--;
17845 phba->sli4_hba.max_cfg_param.rpi_used--;
17846 }
17847}
17848
17849/**
17850 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17851 * @phba: pointer to lpfc hba data structure.
17852 *
17853 * This routine is invoked to release an rpi to the pool of
17854 * available rpis maintained by the driver.
17855 **/
17856void
James Smart6fb120a2009-05-22 14:52:59 -040017857lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17858{
17859 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040017860 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040017861 spin_unlock_irq(&phba->hbalock);
17862}
17863
17864/**
17865 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17866 * @phba: pointer to lpfc hba data structure.
17867 *
17868 * This routine is invoked to remove the memory region that
17869 * provided rpi via a bitmask.
17870 **/
17871void
17872lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17873{
17874 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040017875 kfree(phba->sli4_hba.rpi_ids);
17876 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040017877}
17878
17879/**
17880 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17881 * @phba: pointer to lpfc hba data structure.
17882 *
17883 * This routine is invoked to remove the memory region that
17884 * provided rpi via a bitmask.
17885 **/
17886int
James Smart6b5151f2012-01-18 16:24:06 -050017887lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17888 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040017889{
17890 LPFC_MBOXQ_t *mboxq;
17891 struct lpfc_hba *phba = ndlp->phba;
17892 int rc;
17893
17894 /* The port is notified of the header region via a mailbox command. */
17895 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17896 if (!mboxq)
17897 return -ENOMEM;
17898
17899 /* Post all rpi memory regions to the port. */
17900 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050017901 if (cmpl) {
17902 mboxq->mbox_cmpl = cmpl;
17903 mboxq->context1 = arg;
17904 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050017905 } else
17906 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050017907 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040017908 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17909 if (rc == MBX_NOT_FINISHED) {
17910 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17911 "2010 Resume RPI Mailbox failed "
17912 "status %d, mbxStatus x%x\n", rc,
17913 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17914 mempool_free(mboxq, phba->mbox_mem_pool);
17915 return -EIO;
17916 }
17917 return 0;
17918}
17919
17920/**
17921 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050017922 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040017923 *
James Smart76a95d72010-11-20 23:11:48 -050017924 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040017925 *
17926 * Returns:
17927 * 0 success
17928 * -Evalue otherwise
17929 **/
17930int
James Smart76a95d72010-11-20 23:11:48 -050017931lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040017932{
17933 LPFC_MBOXQ_t *mboxq;
17934 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040017935 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040017936 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050017937 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040017938 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17939 if (!mboxq)
17940 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050017941 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040017942 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040017943 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040017944 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050017945 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040017946 "2022 INIT VPI Mailbox failed "
17947 "status %d, mbxStatus x%x\n", rc,
17948 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040017949 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040017950 }
James Smart6a9c52c2009-10-02 15:16:51 -040017951 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050017952 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040017953
17954 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040017955}
17956
17957/**
17958 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17959 * @phba: pointer to lpfc hba data structure.
17960 * @mboxq: Pointer to mailbox object.
17961 *
17962 * This routine is invoked to manually add a single FCF record. The caller
17963 * must pass a completely initialized FCF_Record. This routine takes
17964 * care of the nonembedded mailbox operations.
17965 **/
17966static void
17967lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17968{
17969 void *virt_addr;
17970 union lpfc_sli4_cfg_shdr *shdr;
17971 uint32_t shdr_status, shdr_add_status;
17972
17973 virt_addr = mboxq->sge_array->addr[0];
17974 /* The IOCTL status is embedded in the mailbox subheader. */
17975 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17976 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17977 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17978
17979 if ((shdr_status || shdr_add_status) &&
17980 (shdr_status != STATUS_FCF_IN_USE))
17981 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17982 "2558 ADD_FCF_RECORD mailbox failed with "
17983 "status x%x add_status x%x\n",
17984 shdr_status, shdr_add_status);
17985
17986 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17987}
17988
17989/**
17990 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17991 * @phba: pointer to lpfc hba data structure.
17992 * @fcf_record: pointer to the initialized fcf record to add.
17993 *
17994 * This routine is invoked to manually add a single FCF record. The caller
17995 * must pass a completely initialized FCF_Record. This routine takes
17996 * care of the nonembedded mailbox operations.
17997 **/
17998int
17999lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
18000{
18001 int rc = 0;
18002 LPFC_MBOXQ_t *mboxq;
18003 uint8_t *bytep;
18004 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040018005 struct lpfc_mbx_sge sge;
18006 uint32_t alloc_len, req_len;
18007 uint32_t fcfindex;
18008
18009 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18010 if (!mboxq) {
18011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18012 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18013 return -ENOMEM;
18014 }
18015
18016 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18017 sizeof(uint32_t);
18018
18019 /* Allocate DMA memory and set up the non-embedded mailbox command */
18020 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18021 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18022 req_len, LPFC_SLI4_MBX_NEMBED);
18023 if (alloc_len < req_len) {
18024 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18025 "2523 Allocated DMA memory size (x%x) is "
18026 "less than the requested DMA memory "
18027 "size (x%x)\n", alloc_len, req_len);
18028 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18029 return -ENOMEM;
18030 }
18031
18032 /*
18033 * Get the first SGE entry from the non-embedded DMA memory. This
18034 * routine only uses a single SGE.
18035 */
18036 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040018037 virt_addr = mboxq->sge_array->addr[0];
18038 /*
18039 * Configure the FCF record for FCFI 0. This is the driver's
18040 * hardcoded default and gets used in nonFIP mode.
18041 */
18042 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18043 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18044 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18045
18046 /*
18047 * Copy the fcf_index and the FCF Record Data. The data starts after
18048 * the FCoE header plus word10. The data copy needs to be endian
18049 * correct.
18050 */
18051 bytep += sizeof(uint32_t);
18052 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18053 mboxq->vport = phba->pport;
18054 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18055 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18056 if (rc == MBX_NOT_FINISHED) {
18057 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18058 "2515 ADD_FCF_RECORD mailbox failed with "
18059 "status 0x%x\n", rc);
18060 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18061 rc = -EIO;
18062 } else
18063 rc = 0;
18064
18065 return rc;
18066}
18067
18068/**
18069 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18070 * @phba: pointer to lpfc hba data structure.
18071 * @fcf_record: pointer to the fcf record to write the default data.
18072 * @fcf_index: FCF table entry index.
18073 *
18074 * This routine is invoked to build the driver's default FCF record. The
18075 * values used are hardcoded. This routine handles memory initialization.
18076 *
18077 **/
18078void
18079lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18080 struct fcf_record *fcf_record,
18081 uint16_t fcf_index)
18082{
18083 memset(fcf_record, 0, sizeof(struct fcf_record));
18084 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18085 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18086 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18087 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18088 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18089 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18090 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18091 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18092 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18093 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18094 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18095 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18096 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040018097 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040018098 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18099 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18100 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18101 /* Set the VLAN bit map */
18102 if (phba->valid_vlan) {
18103 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18104 = 1 << (phba->vlan_id % 8);
18105 }
18106}
18107
18108/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018109 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040018110 * @phba: pointer to lpfc hba data structure.
18111 * @fcf_index: FCF table entry offset.
18112 *
James Smart0c9ab6f2010-02-26 14:15:57 -050018113 * This routine is invoked to scan the entire FCF table by reading FCF
18114 * record and processing it one at a time starting from the @fcf_index
18115 * for initial FCF discovery or fast FCF failover rediscovery.
18116 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018117 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018118 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040018119 **/
18120int
James Smart0c9ab6f2010-02-26 14:15:57 -050018121lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040018122{
18123 int rc = 0, error;
18124 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040018125
James Smart32b97932009-07-19 10:01:21 -040018126 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050018127 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040018128 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18129 if (!mboxq) {
18130 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18131 "2000 Failed to allocate mbox for "
18132 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040018133 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050018134 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018135 }
James Smartecfd03c2010-02-12 14:41:27 -050018136 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050018137 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050018138 if (rc) {
18139 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050018140 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018141 }
James Smartecfd03c2010-02-12 14:41:27 -050018142 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040018143 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050018144 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040018145
18146 spin_lock_irq(&phba->hbalock);
18147 phba->hba_flag |= FCF_TS_INPROG;
18148 spin_unlock_irq(&phba->hbalock);
18149
James Smart6fb120a2009-05-22 14:52:59 -040018150 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050018151 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040018152 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050018153 else {
James Smart38b92ef2010-08-04 16:11:39 -040018154 /* Reset eligible FCF count for new scan */
18155 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040018156 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018157 error = 0;
James Smart32b97932009-07-19 10:01:21 -040018158 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018159fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040018160 if (error) {
18161 if (mboxq)
18162 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040018163 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040018164 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040018165 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040018166 spin_unlock_irq(&phba->hbalock);
18167 }
James Smart6fb120a2009-05-22 14:52:59 -040018168 return error;
18169}
James Smarta0c87cb2009-07-19 10:01:10 -040018170
18171/**
James Smarta93ff372010-10-22 11:06:08 -040018172 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050018173 * @phba: pointer to lpfc hba data structure.
18174 * @fcf_index: FCF table entry offset.
18175 *
18176 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040018177 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050018178 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018179 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018180 * otherwise.
18181 **/
18182int
18183lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18184{
18185 int rc = 0, error;
18186 LPFC_MBOXQ_t *mboxq;
18187
18188 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18189 if (!mboxq) {
18190 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18191 "2763 Failed to allocate mbox for "
18192 "READ_FCF cmd\n");
18193 error = -ENOMEM;
18194 goto fail_fcf_read;
18195 }
18196 /* Construct the read FCF record mailbox command */
18197 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18198 if (rc) {
18199 error = -EINVAL;
18200 goto fail_fcf_read;
18201 }
18202 /* Issue the mailbox command asynchronously */
18203 mboxq->vport = phba->pport;
18204 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18205 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18206 if (rc == MBX_NOT_FINISHED)
18207 error = -EIO;
18208 else
18209 error = 0;
18210
18211fail_fcf_read:
18212 if (error && mboxq)
18213 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18214 return error;
18215}
18216
18217/**
18218 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18219 * @phba: pointer to lpfc hba data structure.
18220 * @fcf_index: FCF table entry offset.
18221 *
18222 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040018223 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050018224 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018225 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018226 * otherwise.
18227 **/
18228int
18229lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18230{
18231 int rc = 0, error;
18232 LPFC_MBOXQ_t *mboxq;
18233
18234 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18235 if (!mboxq) {
18236 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18237 "2758 Failed to allocate mbox for "
18238 "READ_FCF cmd\n");
18239 error = -ENOMEM;
18240 goto fail_fcf_read;
18241 }
18242 /* Construct the read FCF record mailbox command */
18243 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18244 if (rc) {
18245 error = -EINVAL;
18246 goto fail_fcf_read;
18247 }
18248 /* Issue the mailbox command asynchronously */
18249 mboxq->vport = phba->pport;
18250 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18251 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18252 if (rc == MBX_NOT_FINISHED)
18253 error = -EIO;
18254 else
18255 error = 0;
18256
18257fail_fcf_read:
18258 if (error && mboxq)
18259 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18260 return error;
18261}
18262
18263/**
James Smartf5cb5302015-12-16 18:11:52 -050018264 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040018265 * phba pointer to the lpfc_hba struct for this port.
18266 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18267 * routine when the rr_bmask is empty. The FCF indecies are put into the
18268 * rr_bmask based on their priority level. Starting from the highest priority
18269 * to the lowest. The most likely FCF candidate will be in the highest
18270 * priority group. When this routine is called it searches the fcf_pri list for
18271 * next lowest priority group and repopulates the rr_bmask with only those
18272 * fcf_indexes.
18273 * returns:
18274 * 1=success 0=failure
18275 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018276static int
James Smart7d791df2011-07-22 18:37:52 -040018277lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18278{
18279 uint16_t next_fcf_pri;
18280 uint16_t last_index;
18281 struct lpfc_fcf_pri *fcf_pri;
18282 int rc;
18283 int ret = 0;
18284
18285 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18286 LPFC_SLI4_FCF_TBL_INDX_MAX);
18287 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18288 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050018289
18290 /* Verify the priority list has 2 or more entries */
18291 spin_lock_irq(&phba->hbalock);
18292 if (list_empty(&phba->fcf.fcf_pri_list) ||
18293 list_is_singular(&phba->fcf.fcf_pri_list)) {
18294 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040018295 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18296 "3061 Last IDX %d\n", last_index);
18297 return 0; /* Empty rr list */
18298 }
James Smart25626692013-03-01 16:36:54 -050018299 spin_unlock_irq(&phba->hbalock);
18300
James Smart7d791df2011-07-22 18:37:52 -040018301 next_fcf_pri = 0;
18302 /*
18303 * Clear the rr_bmask and set all of the bits that are at this
18304 * priority.
18305 */
18306 memset(phba->fcf.fcf_rr_bmask, 0,
18307 sizeof(*phba->fcf.fcf_rr_bmask));
18308 spin_lock_irq(&phba->hbalock);
18309 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18310 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18311 continue;
18312 /*
18313 * the 1st priority that has not FLOGI failed
18314 * will be the highest.
18315 */
18316 if (!next_fcf_pri)
18317 next_fcf_pri = fcf_pri->fcf_rec.priority;
18318 spin_unlock_irq(&phba->hbalock);
18319 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18320 rc = lpfc_sli4_fcf_rr_index_set(phba,
18321 fcf_pri->fcf_rec.fcf_index);
18322 if (rc)
18323 return 0;
18324 }
18325 spin_lock_irq(&phba->hbalock);
18326 }
18327 /*
18328 * if next_fcf_pri was not set above and the list is not empty then
18329 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070018330 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040018331 */
18332 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18333 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18334 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18335 /*
18336 * the 1st priority that has not FLOGI failed
18337 * will be the highest.
18338 */
18339 if (!next_fcf_pri)
18340 next_fcf_pri = fcf_pri->fcf_rec.priority;
18341 spin_unlock_irq(&phba->hbalock);
18342 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18343 rc = lpfc_sli4_fcf_rr_index_set(phba,
18344 fcf_pri->fcf_rec.fcf_index);
18345 if (rc)
18346 return 0;
18347 }
18348 spin_lock_irq(&phba->hbalock);
18349 }
18350 } else
18351 ret = 1;
18352 spin_unlock_irq(&phba->hbalock);
18353
18354 return ret;
18355}
18356/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018357 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18358 * @phba: pointer to lpfc hba data structure.
18359 *
18360 * This routine is to get the next eligible FCF record index in a round
18361 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040018362 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050018363 * shall be returned, otherwise, the next eligible FCF record's index
18364 * shall be returned.
18365 **/
18366uint16_t
18367lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18368{
18369 uint16_t next_fcf_index;
18370
James Smart421c6622013-01-03 15:44:16 -050018371initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040018372 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050018373 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18374
James Smart7d791df2011-07-22 18:37:52 -040018375next_priority:
James Smart421c6622013-01-03 15:44:16 -050018376 /* Determine the next fcf index to check */
18377 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050018378 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18379 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040018380 next_fcf_index);
18381
James Smart0c9ab6f2010-02-26 14:15:57 -050018382 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018383 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18384 /*
18385 * If we have wrapped then we need to clear the bits that
18386 * have been tested so that we can detect when we should
18387 * change the priority level.
18388 */
James Smart0c9ab6f2010-02-26 14:15:57 -050018389 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18390 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040018391 }
18392
James Smart0c9ab6f2010-02-26 14:15:57 -050018393
James Smart3804dc82010-07-14 15:31:37 -040018394 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018395 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18396 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18397 /*
18398 * If next fcf index is not found check if there are lower
18399 * Priority level fcf's in the fcf_priority list.
18400 * Set up the rr_bmask with all of the avaiable fcf bits
18401 * at that level and continue the selection process.
18402 */
18403 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018404 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018405 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18406 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040018407 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18408 return LPFC_FCOE_FCF_NEXT_NONE;
18409 else {
18410 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18411 "3063 Only FCF available idx %d, flag %x\n",
18412 next_fcf_index,
18413 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18414 return next_fcf_index;
18415 }
James Smart3804dc82010-07-14 15:31:37 -040018416 }
18417
James Smart7d791df2011-07-22 18:37:52 -040018418 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18419 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018420 LPFC_FCF_FLOGI_FAILED) {
18421 if (list_is_singular(&phba->fcf.fcf_pri_list))
18422 return LPFC_FCOE_FCF_NEXT_NONE;
18423
James Smart7d791df2011-07-22 18:37:52 -040018424 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018425 }
James Smart7d791df2011-07-22 18:37:52 -040018426
James Smart3804dc82010-07-14 15:31:37 -040018427 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018428 "2845 Get next roundrobin failover FCF (x%x)\n",
18429 next_fcf_index);
18430
James Smart0c9ab6f2010-02-26 14:15:57 -050018431 return next_fcf_index;
18432}
18433
18434/**
18435 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18436 * @phba: pointer to lpfc hba data structure.
18437 *
18438 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018439 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018440 * does not go beyond the range of the driver allocated bmask dimension
18441 * before setting the bit.
18442 *
18443 * Returns 0 if the index bit successfully set, otherwise, it returns
18444 * -EINVAL.
18445 **/
18446int
18447lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18448{
18449 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18450 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018451 "2610 FCF (x%x) reached driver's book "
18452 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018453 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18454 return -EINVAL;
18455 }
18456 /* Set the eligible FCF record index bmask */
18457 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18458
James Smart3804dc82010-07-14 15:31:37 -040018459 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018460 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018461 "bmask\n", fcf_index);
18462
James Smart0c9ab6f2010-02-26 14:15:57 -050018463 return 0;
18464}
18465
18466/**
James Smart3804dc82010-07-14 15:31:37 -040018467 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018468 * @phba: pointer to lpfc hba data structure.
18469 *
18470 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018471 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018472 * does not go beyond the range of the driver allocated bmask dimension
18473 * before clearing the bit.
18474 **/
18475void
18476lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18477{
James Smart9a803a72013-09-06 12:17:56 -040018478 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018479 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18480 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018481 "2762 FCF (x%x) reached driver's book "
18482 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018483 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18484 return;
18485 }
18486 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018487 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018488 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18489 list) {
James Smart7d791df2011-07-22 18:37:52 -040018490 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18491 list_del_init(&fcf_pri->list);
18492 break;
18493 }
18494 }
18495 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018496 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018497
18498 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018499 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018500 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018501}
18502
18503/**
James Smartecfd03c2010-02-12 14:41:27 -050018504 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18505 * @phba: pointer to lpfc hba data structure.
18506 *
18507 * This routine is the completion routine for the rediscover FCF table mailbox
18508 * command. If the mailbox command returned failure, it will try to stop the
18509 * FCF rediscover wait timer.
18510 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018511static void
James Smartecfd03c2010-02-12 14:41:27 -050018512lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18513{
18514 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18515 uint32_t shdr_status, shdr_add_status;
18516
18517 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18518
18519 shdr_status = bf_get(lpfc_mbox_hdr_status,
18520 &redisc_fcf->header.cfg_shdr.response);
18521 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18522 &redisc_fcf->header.cfg_shdr.response);
18523 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018524 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018525 "2746 Requesting for FCF rediscovery failed "
18526 "status x%x add_status x%x\n",
18527 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018528 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018529 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018530 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018531 spin_unlock_irq(&phba->hbalock);
18532 /*
18533 * CVL event triggered FCF rediscover request failed,
18534 * last resort to re-try current registered FCF entry.
18535 */
18536 lpfc_retry_pport_discovery(phba);
18537 } else {
18538 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018539 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018540 spin_unlock_irq(&phba->hbalock);
18541 /*
18542 * DEAD FCF event triggered FCF rediscover request
18543 * failed, last resort to fail over as a link down
18544 * to FCF registration.
18545 */
18546 lpfc_sli4_fcf_dead_failthrough(phba);
18547 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018548 } else {
18549 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018550 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018551 /*
18552 * Start FCF rediscovery wait timer for pending FCF
18553 * before rescan FCF record table.
18554 */
18555 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018556 }
James Smartecfd03c2010-02-12 14:41:27 -050018557
18558 mempool_free(mbox, phba->mbox_mem_pool);
18559}
18560
18561/**
James Smart3804dc82010-07-14 15:31:37 -040018562 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018563 * @phba: pointer to lpfc hba data structure.
18564 *
18565 * This routine is invoked to request for rediscovery of the entire FCF table
18566 * by the port.
18567 **/
18568int
18569lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18570{
18571 LPFC_MBOXQ_t *mbox;
18572 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18573 int rc, length;
18574
James Smart0c9ab6f2010-02-26 14:15:57 -050018575 /* Cancel retry delay timers to all vports before FCF rediscover */
18576 lpfc_cancel_all_vport_retry_delay_timer(phba);
18577
James Smartecfd03c2010-02-12 14:41:27 -050018578 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18579 if (!mbox) {
18580 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18581 "2745 Failed to allocate mbox for "
18582 "requesting FCF rediscover.\n");
18583 return -ENOMEM;
18584 }
18585
18586 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18587 sizeof(struct lpfc_sli4_cfg_mhdr));
18588 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18589 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18590 length, LPFC_SLI4_MBX_EMBED);
18591
18592 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18593 /* Set count to 0 for invalidating the entire FCF database */
18594 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18595
18596 /* Issue the mailbox command asynchronously */
18597 mbox->vport = phba->pport;
18598 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18599 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18600
18601 if (rc == MBX_NOT_FINISHED) {
18602 mempool_free(mbox, phba->mbox_mem_pool);
18603 return -EIO;
18604 }
18605 return 0;
18606}
18607
18608/**
James Smartfc2b9892010-02-26 14:15:29 -050018609 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18610 * @phba: pointer to lpfc hba data structure.
18611 *
18612 * This function is the failover routine as a last resort to the FCF DEAD
18613 * event when driver failed to perform fast FCF failover.
18614 **/
18615void
18616lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18617{
18618 uint32_t link_state;
18619
18620 /*
18621 * Last resort as FCF DEAD event failover will treat this as
18622 * a link down, but save the link state because we don't want
18623 * it to be changed to Link Down unless it is already down.
18624 */
18625 link_state = phba->link_state;
18626 lpfc_linkdown(phba);
18627 phba->link_state = link_state;
18628
18629 /* Unregister FCF if no devices connected to it */
18630 lpfc_unregister_unused_fcf(phba);
18631}
18632
18633/**
James Smart026abb82011-12-13 13:20:45 -050018634 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018635 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018636 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018637 *
James Smart026abb82011-12-13 13:20:45 -050018638 * This function gets SLI3 port configure region 23 data through memory dump
18639 * mailbox command. When it successfully retrieves data, the size of the data
18640 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018641 **/
James Smart026abb82011-12-13 13:20:45 -050018642static uint32_t
18643lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018644{
18645 LPFC_MBOXQ_t *pmb = NULL;
18646 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018647 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018648 int rc;
18649
James Smart026abb82011-12-13 13:20:45 -050018650 if (!rgn23_data)
18651 return 0;
18652
James Smarta0c87cb2009-07-19 10:01:10 -040018653 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18654 if (!pmb) {
18655 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018656 "2600 failed to allocate mailbox memory\n");
18657 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018658 }
18659 mb = &pmb->u.mb;
18660
James Smarta0c87cb2009-07-19 10:01:10 -040018661 do {
18662 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18663 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18664
18665 if (rc != MBX_SUCCESS) {
18666 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018667 "2601 failed to read config "
18668 "region 23, rc 0x%x Status 0x%x\n",
18669 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018670 mb->un.varDmp.word_cnt = 0;
18671 }
18672 /*
18673 * dump mem may return a zero when finished or we got a
18674 * mailbox error, either way we are done.
18675 */
18676 if (mb->un.varDmp.word_cnt == 0)
18677 break;
18678 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18679 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18680
18681 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018682 rgn23_data + offset,
18683 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018684 offset += mb->un.varDmp.word_cnt;
18685 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18686
James Smart026abb82011-12-13 13:20:45 -050018687 mempool_free(pmb, phba->mbox_mem_pool);
18688 return offset;
18689}
18690
18691/**
18692 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18693 * @phba: pointer to lpfc hba data structure.
18694 * @rgn23_data: pointer to configure region 23 data.
18695 *
18696 * This function gets SLI4 port configure region 23 data through memory dump
18697 * mailbox command. When it successfully retrieves data, the size of the data
18698 * will be returned, otherwise, 0 will be returned.
18699 **/
18700static uint32_t
18701lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18702{
18703 LPFC_MBOXQ_t *mboxq = NULL;
18704 struct lpfc_dmabuf *mp = NULL;
18705 struct lpfc_mqe *mqe;
18706 uint32_t data_length = 0;
18707 int rc;
18708
18709 if (!rgn23_data)
18710 return 0;
18711
18712 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18713 if (!mboxq) {
18714 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18715 "3105 failed to allocate mailbox memory\n");
18716 return 0;
18717 }
18718
18719 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18720 goto out;
18721 mqe = &mboxq->u.mqe;
18722 mp = (struct lpfc_dmabuf *) mboxq->context1;
18723 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18724 if (rc)
18725 goto out;
18726 data_length = mqe->un.mb_words[5];
18727 if (data_length == 0)
18728 goto out;
18729 if (data_length > DMP_RGN23_SIZE) {
18730 data_length = 0;
18731 goto out;
18732 }
18733 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18734out:
18735 mempool_free(mboxq, phba->mbox_mem_pool);
18736 if (mp) {
18737 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18738 kfree(mp);
18739 }
18740 return data_length;
18741}
18742
18743/**
18744 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18745 * @phba: pointer to lpfc hba data structure.
18746 *
18747 * This function read region 23 and parse TLV for port status to
18748 * decide if the user disaled the port. If the TLV indicates the
18749 * port is disabled, the hba_flag is set accordingly.
18750 **/
18751void
18752lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18753{
18754 uint8_t *rgn23_data = NULL;
18755 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18756 uint32_t offset = 0;
18757
18758 /* Get adapter Region 23 data */
18759 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18760 if (!rgn23_data)
18761 goto out;
18762
18763 if (phba->sli_rev < LPFC_SLI_REV4)
18764 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18765 else {
18766 if_type = bf_get(lpfc_sli_intf_if_type,
18767 &phba->sli4_hba.sli_intf);
18768 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18769 goto out;
18770 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18771 }
James Smarta0c87cb2009-07-19 10:01:10 -040018772
18773 if (!data_size)
18774 goto out;
18775
18776 /* Check the region signature first */
18777 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18778 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18779 "2619 Config region 23 has bad signature\n");
18780 goto out;
18781 }
18782 offset += 4;
18783
18784 /* Check the data structure version */
18785 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18786 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18787 "2620 Config region 23 has bad version\n");
18788 goto out;
18789 }
18790 offset += 4;
18791
18792 /* Parse TLV entries in the region */
18793 while (offset < data_size) {
18794 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18795 break;
18796 /*
18797 * If the TLV is not driver specific TLV or driver id is
18798 * not linux driver id, skip the record.
18799 */
18800 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18801 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18802 (rgn23_data[offset + 3] != 0)) {
18803 offset += rgn23_data[offset + 1] * 4 + 4;
18804 continue;
18805 }
18806
18807 /* Driver found a driver specific TLV in the config region */
18808 sub_tlv_len = rgn23_data[offset + 1] * 4;
18809 offset += 4;
18810 tlv_offset = 0;
18811
18812 /*
18813 * Search for configured port state sub-TLV.
18814 */
18815 while ((offset < data_size) &&
18816 (tlv_offset < sub_tlv_len)) {
18817 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18818 offset += 4;
18819 tlv_offset += 4;
18820 break;
18821 }
18822 if (rgn23_data[offset] != PORT_STE_TYPE) {
18823 offset += rgn23_data[offset + 1] * 4 + 4;
18824 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18825 continue;
18826 }
18827
18828 /* This HBA contains PORT_STE configured */
18829 if (!rgn23_data[offset + 2])
18830 phba->hba_flag |= LINK_DISABLED;
18831
18832 goto out;
18833 }
18834 }
James Smart026abb82011-12-13 13:20:45 -050018835
James Smarta0c87cb2009-07-19 10:01:10 -040018836out:
James Smarta0c87cb2009-07-19 10:01:10 -040018837 kfree(rgn23_data);
18838 return;
18839}
James Smart695a8142010-01-26 23:08:03 -050018840
18841/**
James Smart52d52442011-05-24 11:42:45 -040018842 * lpfc_wr_object - write an object to the firmware
18843 * @phba: HBA structure that indicates port to create a queue on.
18844 * @dmabuf_list: list of dmabufs to write to the port.
18845 * @size: the total byte value of the objects to write to the port.
18846 * @offset: the current offset to be used to start the transfer.
18847 *
18848 * This routine will create a wr_object mailbox command to send to the port.
18849 * the mailbox command will be constructed using the dma buffers described in
18850 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18851 * BDEs that the imbedded mailbox can support. The @offset variable will be
18852 * used to indicate the starting offset of the transfer and will also return
18853 * the offset after the write object mailbox has completed. @size is used to
18854 * determine the end of the object and whether the eof bit should be set.
18855 *
18856 * Return 0 is successful and offset will contain the the new offset to use
18857 * for the next write.
18858 * Return negative value for error cases.
18859 **/
18860int
18861lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18862 uint32_t size, uint32_t *offset)
18863{
18864 struct lpfc_mbx_wr_object *wr_object;
18865 LPFC_MBOXQ_t *mbox;
18866 int rc = 0, i = 0;
18867 uint32_t shdr_status, shdr_add_status;
18868 uint32_t mbox_tmo;
18869 union lpfc_sli4_cfg_shdr *shdr;
18870 struct lpfc_dmabuf *dmabuf;
18871 uint32_t written = 0;
18872
18873 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18874 if (!mbox)
18875 return -ENOMEM;
18876
18877 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18878 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18879 sizeof(struct lpfc_mbx_wr_object) -
18880 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18881
18882 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18883 wr_object->u.request.write_offset = *offset;
18884 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18885 wr_object->u.request.object_name[0] =
18886 cpu_to_le32(wr_object->u.request.object_name[0]);
18887 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18888 list_for_each_entry(dmabuf, dmabuf_list, list) {
18889 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18890 break;
18891 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18892 wr_object->u.request.bde[i].addrHigh =
18893 putPaddrHigh(dmabuf->phys);
18894 if (written + SLI4_PAGE_SIZE >= size) {
18895 wr_object->u.request.bde[i].tus.f.bdeSize =
18896 (size - written);
18897 written += (size - written);
18898 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18899 } else {
18900 wr_object->u.request.bde[i].tus.f.bdeSize =
18901 SLI4_PAGE_SIZE;
18902 written += SLI4_PAGE_SIZE;
18903 }
18904 i++;
18905 }
18906 wr_object->u.request.bde_count = i;
18907 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18908 if (!phba->sli4_hba.intr_enable)
18909 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18910 else {
James Smarta183a152011-10-10 21:32:43 -040018911 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040018912 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18913 }
18914 /* The IOCTL status is embedded in the mailbox subheader. */
18915 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18916 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18917 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18918 if (rc != MBX_TIMEOUT)
18919 mempool_free(mbox, phba->mbox_mem_pool);
18920 if (shdr_status || shdr_add_status || rc) {
18921 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18922 "3025 Write Object mailbox failed with "
18923 "status x%x add_status x%x, mbx status x%x\n",
18924 shdr_status, shdr_add_status, rc);
18925 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080018926 *offset = shdr_add_status;
James Smart52d52442011-05-24 11:42:45 -040018927 } else
18928 *offset += wr_object->u.response.actual_write_length;
18929 return rc;
18930}
18931
18932/**
James Smart695a8142010-01-26 23:08:03 -050018933 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18934 * @vport: pointer to vport data structure.
18935 *
18936 * This function iterate through the mailboxq and clean up all REG_LOGIN
18937 * and REG_VPI mailbox commands associated with the vport. This function
18938 * is called when driver want to restart discovery of the vport due to
18939 * a Clear Virtual Link event.
18940 **/
18941void
18942lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18943{
18944 struct lpfc_hba *phba = vport->phba;
18945 LPFC_MBOXQ_t *mb, *nextmb;
18946 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040018947 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040018948 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040018949 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040018950 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050018951 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050018952
James Smartd439d282010-09-29 11:18:45 -040018953 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050018954 spin_lock_irq(&phba->hbalock);
18955 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18956 if (mb->vport != vport)
18957 continue;
18958
18959 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18960 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18961 continue;
18962
James Smartd439d282010-09-29 11:18:45 -040018963 list_del(&mb->list);
18964 list_add_tail(&mb->list, &mbox_cmd_list);
18965 }
18966 /* Clean up active mailbox command with the vport */
18967 mb = phba->sli.mbox_active;
18968 if (mb && (mb->vport == vport)) {
18969 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18970 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18971 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18972 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18973 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18974 /* Put reference count for delayed processing */
18975 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18976 /* Unregister the RPI when mailbox complete */
18977 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18978 }
18979 }
James Smart63e801c2010-11-20 23:14:19 -050018980 /* Cleanup any mailbox completions which are not yet processed */
18981 do {
18982 restart_loop = 0;
18983 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18984 /*
18985 * If this mailox is already processed or it is
18986 * for another vport ignore it.
18987 */
18988 if ((mb->vport != vport) ||
18989 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18990 continue;
18991
18992 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18993 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18994 continue;
18995
18996 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18997 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18998 ndlp = (struct lpfc_nodelist *)mb->context2;
18999 /* Unregister the RPI when mailbox complete */
19000 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19001 restart_loop = 1;
19002 spin_unlock_irq(&phba->hbalock);
19003 spin_lock(shost->host_lock);
19004 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19005 spin_unlock(shost->host_lock);
19006 spin_lock_irq(&phba->hbalock);
19007 break;
19008 }
19009 }
19010 } while (restart_loop);
19011
James Smartd439d282010-09-29 11:18:45 -040019012 spin_unlock_irq(&phba->hbalock);
19013
19014 /* Release the cleaned-up mailbox commands */
19015 while (!list_empty(&mbox_cmd_list)) {
19016 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050019017 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19018 mp = (struct lpfc_dmabuf *) (mb->context1);
19019 if (mp) {
19020 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19021 kfree(mp);
19022 }
James Smart78730cf2010-04-06 15:06:30 -040019023 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040019024 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040019025 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019026 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040019027 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019028 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040019029 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040019030 }
James Smart695a8142010-01-26 23:08:03 -050019031 }
James Smart695a8142010-01-26 23:08:03 -050019032 mempool_free(mb, phba->mbox_mem_pool);
19033 }
James Smartd439d282010-09-29 11:18:45 -040019034
19035 /* Release the ndlp with the cleaned-up active mailbox command */
19036 if (act_mbx_ndlp) {
19037 spin_lock(shost->host_lock);
19038 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19039 spin_unlock(shost->host_lock);
19040 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050019041 }
James Smart695a8142010-01-26 23:08:03 -050019042}
19043
James Smart2a9bf3d2010-06-07 15:24:45 -040019044/**
19045 * lpfc_drain_txq - Drain the txq
19046 * @phba: Pointer to HBA context object.
19047 *
19048 * This function attempt to submit IOCBs on the txq
19049 * to the adapter. For SLI4 adapters, the txq contains
19050 * ELS IOCBs that have been deferred because the there
19051 * are no SGLs. This congestion can occur with large
19052 * vport counts during node discovery.
19053 **/
19054
19055uint32_t
19056lpfc_drain_txq(struct lpfc_hba *phba)
19057{
19058 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080019059 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090019060 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040019061 unsigned long iflags = 0;
19062 char *fail_msg = NULL;
19063 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080019064 union lpfc_wqe128 wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040019065 uint32_t txq_cnt = 0;
James Smartdc19e3b2018-05-24 21:08:57 -070019066 struct lpfc_queue *wq;
James Smart2a9bf3d2010-06-07 15:24:45 -040019067
James Smartdc19e3b2018-05-24 21:08:57 -070019068 if (phba->link_flag & LS_MDS_LOOPBACK) {
19069 /* MDS WQE are posted only to first WQ*/
19070 wq = phba->sli4_hba.fcp_wq[0];
19071 if (unlikely(!wq))
19072 return 0;
19073 pring = wq->pring;
19074 } else {
19075 wq = phba->sli4_hba.els_wq;
19076 if (unlikely(!wq))
19077 return 0;
19078 pring = lpfc_phba_elsring(phba);
19079 }
19080
19081 if (unlikely(!pring) || list_empty(&pring->txq))
Dick Kennedy1234a6d2017-09-29 17:34:29 -070019082 return 0;
James Smart895427b2017-02-12 13:52:30 -080019083
James Smart398d81c2013-05-31 17:04:19 -040019084 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050019085 list_for_each_entry(piocbq, &pring->txq, list) {
19086 txq_cnt++;
19087 }
19088
19089 if (txq_cnt > pring->txq_max)
19090 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019091
James Smart398d81c2013-05-31 17:04:19 -040019092 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019093
James Smart0e9bb8d2013-03-01 16:35:12 -050019094 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040019095 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019096
James Smart19ca7602010-11-20 23:11:55 -050019097 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040019098 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040019099 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040019100 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19101 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050019102 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040019103 break;
19104 }
James Smart895427b2017-02-12 13:52:30 -080019105 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040019106 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050019107 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040019108 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019109 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040019110 }
James Smart0e9bb8d2013-03-01 16:35:12 -050019111 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040019112
19113 /* The xri and iocb resources secured,
19114 * attempt to issue request
19115 */
James Smart6d368e52011-05-24 11:44:12 -040019116 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040019117 piocbq->sli4_xritag = sglq->sli4_xritag;
19118 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19119 fail_msg = "to convert bpl to sgl";
James Smart205e8242018-03-05 12:04:03 -080019120 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019121 fail_msg = "to convert iocb to wqe";
James Smartdc19e3b2018-05-24 21:08:57 -070019122 else if (lpfc_sli4_wq_put(wq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019123 fail_msg = " - Wq is full";
19124 else
19125 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19126
19127 if (fail_msg) {
19128 /* Failed means we can't issue and need to cancel */
19129 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19130 "2822 IOCB failed %s iotag 0x%x "
19131 "xri 0x%x\n",
19132 fail_msg,
19133 piocbq->iotag, piocbq->sli4_xritag);
19134 list_add_tail(&piocbq->list, &completions);
19135 }
James Smart398d81c2013-05-31 17:04:19 -040019136 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019137 }
19138
James Smart2a9bf3d2010-06-07 15:24:45 -040019139 /* Cancel all the IOCBs that cannot be issued */
19140 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19141 IOERR_SLI_ABORTED);
19142
James Smart0e9bb8d2013-03-01 16:35:12 -050019143 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019144}
James Smart895427b2017-02-12 13:52:30 -080019145
19146/**
19147 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19148 * @phba: Pointer to HBA context object.
19149 * @pwqe: Pointer to command WQE.
19150 * @sglq: Pointer to the scatter gather queue object.
19151 *
19152 * This routine converts the bpl or bde that is in the WQE
19153 * to a sgl list for the sli4 hardware. The physical address
19154 * of the bpl/bde is converted back to a virtual address.
19155 * If the WQE contains a BPL then the list of BDE's is
19156 * converted to sli4_sge's. If the WQE contains a single
19157 * BDE then it is converted to a single sli_sge.
19158 * The WQE is still in cpu endianness so the contents of
19159 * the bpl can be used without byte swapping.
19160 *
19161 * Returns valid XRI = Success, NO_XRI = Failure.
19162 */
19163static uint16_t
19164lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19165 struct lpfc_sglq *sglq)
19166{
19167 uint16_t xritag = NO_XRI;
19168 struct ulp_bde64 *bpl = NULL;
19169 struct ulp_bde64 bde;
19170 struct sli4_sge *sgl = NULL;
19171 struct lpfc_dmabuf *dmabuf;
James Smart205e8242018-03-05 12:04:03 -080019172 union lpfc_wqe128 *wqe;
James Smart895427b2017-02-12 13:52:30 -080019173 int numBdes = 0;
19174 int i = 0;
19175 uint32_t offset = 0; /* accumulated offset in the sg request list */
19176 int inbound = 0; /* number of sg reply entries inbound from firmware */
19177 uint32_t cmd;
19178
19179 if (!pwqeq || !sglq)
19180 return xritag;
19181
19182 sgl = (struct sli4_sge *)sglq->sgl;
19183 wqe = &pwqeq->wqe;
19184 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19185
19186 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19187 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19188 return sglq->sli4_xritag;
19189 numBdes = pwqeq->rsvd2;
19190 if (numBdes) {
19191 /* The addrHigh and addrLow fields within the WQE
19192 * have not been byteswapped yet so there is no
19193 * need to swap them back.
19194 */
19195 if (pwqeq->context3)
19196 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19197 else
19198 return xritag;
19199
19200 bpl = (struct ulp_bde64 *)dmabuf->virt;
19201 if (!bpl)
19202 return xritag;
19203
19204 for (i = 0; i < numBdes; i++) {
19205 /* Should already be byte swapped. */
19206 sgl->addr_hi = bpl->addrHigh;
19207 sgl->addr_lo = bpl->addrLow;
19208
19209 sgl->word2 = le32_to_cpu(sgl->word2);
19210 if ((i+1) == numBdes)
19211 bf_set(lpfc_sli4_sge_last, sgl, 1);
19212 else
19213 bf_set(lpfc_sli4_sge_last, sgl, 0);
19214 /* swap the size field back to the cpu so we
19215 * can assign it to the sgl.
19216 */
19217 bde.tus.w = le32_to_cpu(bpl->tus.w);
19218 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19219 /* The offsets in the sgl need to be accumulated
19220 * separately for the request and reply lists.
19221 * The request is always first, the reply follows.
19222 */
19223 switch (cmd) {
19224 case CMD_GEN_REQUEST64_WQE:
19225 /* add up the reply sg entries */
19226 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19227 inbound++;
19228 /* first inbound? reset the offset */
19229 if (inbound == 1)
19230 offset = 0;
19231 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19232 bf_set(lpfc_sli4_sge_type, sgl,
19233 LPFC_SGE_TYPE_DATA);
19234 offset += bde.tus.f.bdeSize;
19235 break;
19236 case CMD_FCP_TRSP64_WQE:
19237 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19238 bf_set(lpfc_sli4_sge_type, sgl,
19239 LPFC_SGE_TYPE_DATA);
19240 break;
19241 case CMD_FCP_TSEND64_WQE:
19242 case CMD_FCP_TRECEIVE64_WQE:
19243 bf_set(lpfc_sli4_sge_type, sgl,
19244 bpl->tus.f.bdeFlags);
19245 if (i < 3)
19246 offset = 0;
19247 else
19248 offset += bde.tus.f.bdeSize;
19249 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19250 break;
19251 }
19252 sgl->word2 = cpu_to_le32(sgl->word2);
19253 bpl++;
19254 sgl++;
19255 }
19256 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19257 /* The addrHigh and addrLow fields of the BDE have not
19258 * been byteswapped yet so they need to be swapped
19259 * before putting them in the sgl.
19260 */
19261 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19262 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19263 sgl->word2 = le32_to_cpu(sgl->word2);
19264 bf_set(lpfc_sli4_sge_last, sgl, 1);
19265 sgl->word2 = cpu_to_le32(sgl->word2);
19266 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19267 }
19268 return sglq->sli4_xritag;
19269}
19270
19271/**
19272 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19273 * @phba: Pointer to HBA context object.
19274 * @ring_number: Base sli ring number
19275 * @pwqe: Pointer to command WQE.
19276 **/
19277int
19278lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
19279 struct lpfc_iocbq *pwqe)
19280{
James Smart205e8242018-03-05 12:04:03 -080019281 union lpfc_wqe128 *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080019282 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080019283 struct lpfc_queue *wq;
19284 struct lpfc_sglq *sglq;
19285 struct lpfc_sli_ring *pring;
19286 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070019287 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080019288
19289 /* NVME_LS and NVME_LS ABTS requests. */
19290 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19291 pring = phba->sli4_hba.nvmels_wq->pring;
19292 spin_lock_irqsave(&pring->ring_lock, iflags);
19293 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19294 if (!sglq) {
19295 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19296 return WQE_BUSY;
19297 }
19298 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19299 pwqe->sli4_xritag = sglq->sli4_xritag;
19300 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19301 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19302 return WQE_ERROR;
19303 }
19304 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19305 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070019306 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19307 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019308 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019309 return ret;
James Smart895427b2017-02-12 13:52:30 -080019310 }
Dick Kennedycd22d602017-08-23 16:55:35 -070019311
James Smart895427b2017-02-12 13:52:30 -080019312 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19313 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19314 return 0;
19315 }
19316
19317 /* NVME_FCREQ and NVME_ABTS requests */
19318 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19319 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19320 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19321
19322 spin_lock_irqsave(&pring->ring_lock, iflags);
19323 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19324 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19325 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019326 ret = lpfc_sli4_wq_put(wq, wqe);
19327 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019328 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019329 return ret;
James Smart895427b2017-02-12 13:52:30 -080019330 }
19331 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19332 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19333 return 0;
19334 }
19335
James Smartf358dd02017-02-12 13:52:34 -080019336 /* NVMET requests */
19337 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19338 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19339 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19340
19341 spin_lock_irqsave(&pring->ring_lock, iflags);
19342 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070019343 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080019344 if (pwqe->sli4_xritag == NO_XRI) {
19345 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19346 pwqe->sli4_xritag = sglq->sli4_xritag;
19347 }
19348 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19349 pwqe->sli4_xritag);
19350 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19351 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19352 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019353 ret = lpfc_sli4_wq_put(wq, wqe);
19354 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080019355 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019356 return ret;
James Smartf358dd02017-02-12 13:52:34 -080019357 }
19358 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19359 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19360 return 0;
19361 }
James Smart895427b2017-02-12 13:52:30 -080019362 return WQE_ERROR;
19363}