blob: c4c319245a66da01c4199d1a597d0df808f6e3ca [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 Smart0d041212019-01-28 11:14:41 -08004 * Copyright (C) 2017-2019 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 Smart32517fc2019-01-28 11:14:33 -080081static bool lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba,
82 struct lpfc_queue *cq, struct lpfc_cqe *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,
James Smart32517fc2019-01-28 11:14:33 -080086 struct lpfc_queue *eq,
87 struct lpfc_eqe *eqe);
James Smarte8d3c3b2013-10-10 12:21:30 -040088static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
89static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart895427b2017-02-12 13:52:30 -080090static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
91 struct lpfc_sli_ring *pring,
92 struct lpfc_iocbq *cmdiocb);
James Smart05580562011-05-24 11:40:48 -040093
James Smart4f774512009-05-22 14:52:35 -040094static IOCB_t *
95lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
96{
97 return &iocbq->iocb;
98}
99
James Smart48f8fdb2018-05-04 20:37:51 -0700100#if defined(CONFIG_64BIT) && defined(__LITTLE_ENDIAN)
101/**
102 * lpfc_sli4_pcimem_bcopy - SLI4 memory copy function
103 * @srcp: Source memory pointer.
104 * @destp: Destination memory pointer.
105 * @cnt: Number of words required to be copied.
106 * Must be a multiple of sizeof(uint64_t)
107 *
108 * This function is used for copying data between driver memory
109 * and the SLI WQ. This function also changes the endianness
110 * of each word if native endianness is different from SLI
111 * endianness. This function can be called with or without
112 * lock.
113 **/
114void
115lpfc_sli4_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
116{
117 uint64_t *src = srcp;
118 uint64_t *dest = destp;
119 int i;
120
121 for (i = 0; i < (int)cnt; i += sizeof(uint64_t))
122 *dest++ = *src++;
123}
124#else
125#define lpfc_sli4_pcimem_bcopy(a, b, c) lpfc_sli_pcimem_bcopy(a, b, c)
126#endif
127
James Smart4f774512009-05-22 14:52:35 -0400128/**
129 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
130 * @q: The Work Queue to operate on.
131 * @wqe: The work Queue Entry to put on the Work queue.
132 *
133 * This routine will copy the contents of @wqe to the next available entry on
134 * the @q. This function will then ring the Work Queue Doorbell to signal the
135 * HBA to start processing the Work Queue Entry. This function returns 0 if
136 * successful. If no entries are available on @q then this function will return
137 * -ENOMEM.
138 * The caller is expected to hold the hbalock when calling this routine.
139 **/
Dick Kennedycd22d602017-08-23 16:55:35 -0700140static int
James Smart205e8242018-03-05 12:04:03 -0800141lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -0400142{
James Smart2e90f4b2011-12-13 13:22:37 -0500143 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400144 struct lpfc_register doorbell;
145 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400146 uint32_t idx;
James Smart1351e692018-02-22 08:18:43 -0800147 uint32_t i = 0;
148 uint8_t *tmp;
James Smart5cc167d2018-06-26 08:24:23 -0700149 u32 if_type;
James Smart4f774512009-05-22 14:52:35 -0400150
James Smart2e90f4b2011-12-13 13:22:37 -0500151 /* sanity check on queue memory */
152 if (unlikely(!q))
153 return -ENOMEM;
154 temp_wqe = q->qe[q->host_index].wqe;
155
James Smart4f774512009-05-22 14:52:35 -0400156 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400157 idx = ((q->host_index + 1) % q->entry_count);
158 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400159 q->WQ_overflow++;
Dick Kennedycd22d602017-08-23 16:55:35 -0700160 return -EBUSY;
James Smartb84daac2012-08-03 12:35:13 -0400161 }
162 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400163 /* set consumption flag every once in a while */
James Smart32517fc2019-01-28 11:14:33 -0800164 if (!((q->host_index + 1) % q->notify_interval))
James Smartf0d9bcc2010-10-22 11:07:09 -0400165 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smart04673e32018-01-30 15:58:45 -0800166 else
167 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
James Smartfedd3b72011-02-16 12:39:24 -0500168 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
169 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart48f8fdb2018-05-04 20:37:51 -0700170 lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
James Smart1351e692018-02-22 08:18:43 -0800171 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
172 /* write to DPP aperture taking advatage of Combined Writes */
James Smart4c066192018-03-05 10:29:03 -0800173 tmp = (uint8_t *)temp_wqe;
174#ifdef __raw_writeq
James Smart1351e692018-02-22 08:18:43 -0800175 for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
James Smart4c066192018-03-05 10:29:03 -0800176 __raw_writeq(*((uint64_t *)(tmp + i)),
177 q->dpp_regaddr + i);
178#else
179 for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
180 __raw_writel(*((uint32_t *)(tmp + i)),
181 q->dpp_regaddr + i);
182#endif
James Smart1351e692018-02-22 08:18:43 -0800183 }
184 /* ensure WQE bcopy and DPP flushed before doorbell write */
James Smart6b3b3bd2016-12-19 15:07:30 -0800185 wmb();
James Smart4f774512009-05-22 14:52:35 -0400186
187 /* Update the host index before invoking device */
188 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400189
190 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400191
192 /* Ring Doorbell */
193 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500194 if (q->db_format == LPFC_DB_LIST_FORMAT) {
James Smart1351e692018-02-22 08:18:43 -0800195 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
196 bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
197 bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
198 bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
199 q->dpp_id);
200 bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
201 q->queue_id);
202 } else {
203 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
James Smart1351e692018-02-22 08:18:43 -0800204 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
James Smart5cc167d2018-06-26 08:24:23 -0700205
206 /* Leave bits <23:16> clear for if_type 6 dpp */
207 if_type = bf_get(lpfc_sli_intf_if_type,
208 &q->phba->sli4_hba.sli_intf);
209 if (if_type != LPFC_SLI_INTF_IF_TYPE_6)
210 bf_set(lpfc_wq_db_list_fm_index, &doorbell,
211 host_index);
James Smart1351e692018-02-22 08:18:43 -0800212 }
James Smart962bc512013-01-03 15:44:00 -0500213 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
214 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
215 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
216 } else {
217 return -EINVAL;
218 }
219 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400220
221 return 0;
222}
223
224/**
225 * lpfc_sli4_wq_release - Updates internal hba index for WQ
226 * @q: The Work Queue to operate on.
227 * @index: The index to advance the hba index to.
228 *
229 * This routine will update the HBA index of a queue to reflect consumption of
230 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
231 * an entry the host calls this function to update the queue's internal
232 * pointers. This routine returns the number of entries that were consumed by
233 * the HBA.
234 **/
235static uint32_t
236lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
237{
238 uint32_t released = 0;
239
James Smart2e90f4b2011-12-13 13:22:37 -0500240 /* sanity check on queue memory */
241 if (unlikely(!q))
242 return 0;
243
James Smart4f774512009-05-22 14:52:35 -0400244 if (q->hba_index == index)
245 return 0;
246 do {
247 q->hba_index = ((q->hba_index + 1) % q->entry_count);
248 released++;
249 } while (q->hba_index != index);
250 return released;
251}
252
253/**
254 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
255 * @q: The Mailbox Queue to operate on.
256 * @wqe: The Mailbox Queue Entry to put on the Work queue.
257 *
258 * This routine will copy the contents of @mqe to the next available entry on
259 * the @q. This function will then ring the Work Queue Doorbell to signal the
260 * HBA to start processing the Work Queue Entry. This function returns 0 if
261 * successful. If no entries are available on @q then this function will return
262 * -ENOMEM.
263 * The caller is expected to hold the hbalock when calling this routine.
264 **/
265static uint32_t
266lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
267{
James Smart2e90f4b2011-12-13 13:22:37 -0500268 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400269 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400270
James Smart2e90f4b2011-12-13 13:22:37 -0500271 /* sanity check on queue memory */
272 if (unlikely(!q))
273 return -ENOMEM;
274 temp_mqe = q->qe[q->host_index].mqe;
275
James Smart4f774512009-05-22 14:52:35 -0400276 /* If the host has not yet processed the next entry then we are done */
277 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
278 return -ENOMEM;
James Smart48f8fdb2018-05-04 20:37:51 -0700279 lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
James Smart4f774512009-05-22 14:52:35 -0400280 /* Save off the mailbox pointer for completion */
281 q->phba->mbox = (MAILBOX_t *)temp_mqe;
282
283 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400284 q->host_index = ((q->host_index + 1) % q->entry_count);
285
286 /* Ring Doorbell */
287 doorbell.word0 = 0;
288 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
289 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
290 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400291 return 0;
292}
293
294/**
295 * lpfc_sli4_mq_release - Updates internal hba index for MQ
296 * @q: The Mailbox Queue to operate on.
297 *
298 * This routine will update the HBA index of a queue to reflect consumption of
299 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
300 * an entry the host calls this function to update the queue's internal
301 * pointers. This routine returns the number of entries that were consumed by
302 * the HBA.
303 **/
304static uint32_t
305lpfc_sli4_mq_release(struct lpfc_queue *q)
306{
James Smart2e90f4b2011-12-13 13:22:37 -0500307 /* sanity check on queue memory */
308 if (unlikely(!q))
309 return 0;
310
James Smart4f774512009-05-22 14:52:35 -0400311 /* Clear the mailbox pointer for completion */
312 q->phba->mbox = NULL;
313 q->hba_index = ((q->hba_index + 1) % q->entry_count);
314 return 1;
315}
316
317/**
318 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
319 * @q: The Event Queue to get the first valid EQE from
320 *
321 * This routine will get the first valid Event Queue Entry from @q, update
322 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
323 * the Queue (no more work to do), or the Queue is full of EQEs that have been
324 * processed, but not popped back to the HBA then this routine will return NULL.
325 **/
326static struct lpfc_eqe *
327lpfc_sli4_eq_get(struct lpfc_queue *q)
328{
James Smart2e90f4b2011-12-13 13:22:37 -0500329 struct lpfc_eqe *eqe;
330
331 /* sanity check on queue memory */
332 if (unlikely(!q))
333 return NULL;
James Smart32517fc2019-01-28 11:14:33 -0800334 eqe = q->qe[q->host_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400335
336 /* If the next EQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800337 if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400338 return NULL;
James Smart27f344e2014-05-07 17:16:46 -0400339
340 /*
341 * insert barrier for instruction interlock : data from the hardware
342 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800343 * upon. Speculative instructions were allowing a bcopy at the start
344 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
345 * after our return, to copy data before the valid bit check above
346 * was done. As such, some of the copied data was stale. The barrier
347 * ensures the check is before any data is copied.
James Smart27f344e2014-05-07 17:16:46 -0400348 */
349 mb();
James Smart4f774512009-05-22 14:52:35 -0400350 return eqe;
351}
352
353/**
James Smartba20c852012-08-03 12:36:52 -0400354 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
355 * @q: The Event Queue to disable interrupts
356 *
357 **/
James Smartb71413d2018-02-22 08:18:40 -0800358inline void
James Smartba20c852012-08-03 12:36:52 -0400359lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
360{
361 struct lpfc_register doorbell;
362
363 doorbell.word0 = 0;
364 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
365 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
366 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
367 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
368 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800369 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smartba20c852012-08-03 12:36:52 -0400370}
371
372/**
James Smart27d6ac02018-02-22 08:18:42 -0800373 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
374 * @q: The Event Queue to disable interrupts
375 *
376 **/
377inline void
378lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
379{
380 struct lpfc_register doorbell;
381
382 doorbell.word0 = 0;
James Smartaad59d52018-09-10 10:30:47 -0700383 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
James Smart27d6ac02018-02-22 08:18:42 -0800384 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
385}
386
387/**
James Smart32517fc2019-01-28 11:14:33 -0800388 * lpfc_sli4_write_eq_db - write EQ DB for eqe's consumed or arm state
389 * @phba: adapter with EQ
James Smart4f774512009-05-22 14:52:35 -0400390 * @q: The Event Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800391 * @count: Number of elements that have been consumed
James Smart4f774512009-05-22 14:52:35 -0400392 * @arm: Indicates whether the host wants to arms this CQ.
393 *
James Smart32517fc2019-01-28 11:14:33 -0800394 * This routine will notify the HBA, by ringing the doorbell, that count
395 * number of EQEs have been processed. The @arm parameter indicates whether
396 * the queue should be rearmed when ringing the doorbell.
James Smart4f774512009-05-22 14:52:35 -0400397 **/
James Smart32517fc2019-01-28 11:14:33 -0800398void
399lpfc_sli4_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
400 uint32_t count, bool arm)
James Smart4f774512009-05-22 14:52:35 -0400401{
James Smart4f774512009-05-22 14:52:35 -0400402 struct lpfc_register doorbell;
403
James Smart2e90f4b2011-12-13 13:22:37 -0500404 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800405 if (unlikely(!q || (count == 0 && !arm)))
406 return;
James Smart4f774512009-05-22 14:52:35 -0400407
408 /* ring doorbell for number popped */
409 doorbell.word0 = 0;
410 if (arm) {
411 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
412 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
413 }
James Smart32517fc2019-01-28 11:14:33 -0800414 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
James Smart4f774512009-05-22 14:52:35 -0400415 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500416 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
417 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
418 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800419 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500420 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
421 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
James Smart9dd35422018-02-22 08:18:41 -0800422 readl(q->phba->sli4_hba.EQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400423}
424
425/**
James Smart32517fc2019-01-28 11:14:33 -0800426 * lpfc_sli4_if6_write_eq_db - write EQ DB for eqe's consumed or arm state
427 * @phba: adapter with EQ
James Smart27d6ac02018-02-22 08:18:42 -0800428 * @q: The Event Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800429 * @count: Number of elements that have been consumed
James Smart27d6ac02018-02-22 08:18:42 -0800430 * @arm: Indicates whether the host wants to arms this CQ.
431 *
James Smart32517fc2019-01-28 11:14:33 -0800432 * This routine will notify the HBA, by ringing the doorbell, that count
433 * number of EQEs have been processed. The @arm parameter indicates whether
434 * the queue should be rearmed when ringing the doorbell.
James Smart27d6ac02018-02-22 08:18:42 -0800435 **/
James Smart32517fc2019-01-28 11:14:33 -0800436void
437lpfc_sli4_if6_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
438 uint32_t count, bool arm)
James Smart27d6ac02018-02-22 08:18:42 -0800439{
James Smart27d6ac02018-02-22 08:18:42 -0800440 struct lpfc_register doorbell;
441
442 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800443 if (unlikely(!q || (count == 0 && !arm)))
444 return;
James Smart27d6ac02018-02-22 08:18:42 -0800445
446 /* ring doorbell for number popped */
447 doorbell.word0 = 0;
448 if (arm)
449 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
James Smart32517fc2019-01-28 11:14:33 -0800450 bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, count);
James Smart27d6ac02018-02-22 08:18:42 -0800451 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
452 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
453 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
454 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
455 readl(q->phba->sli4_hba.EQDBregaddr);
James Smart32517fc2019-01-28 11:14:33 -0800456}
457
458static void
459__lpfc_sli4_consume_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
460 struct lpfc_eqe *eqe)
461{
462 if (!phba->sli4_hba.pc_sli4_params.eqav)
463 bf_set_le32(lpfc_eqe_valid, eqe, 0);
464
465 eq->host_index = ((eq->host_index + 1) % eq->entry_count);
466
467 /* if the index wrapped around, toggle the valid bit */
468 if (phba->sli4_hba.pc_sli4_params.eqav && !eq->host_index)
469 eq->qe_valid = (eq->qe_valid) ? 0 : 1;
470}
471
472static void
473lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
474{
475 struct lpfc_eqe *eqe;
476 uint32_t count = 0;
477
478 /* walk all the EQ entries and drop on the floor */
479 eqe = lpfc_sli4_eq_get(eq);
480 while (eqe) {
481 __lpfc_sli4_consume_eqe(phba, eq, eqe);
482 count++;
483 eqe = lpfc_sli4_eq_get(eq);
484 }
485
486 /* Clear and re-arm the EQ */
487 phba->sli4_hba.sli4_write_eq_db(phba, eq, count, LPFC_QUEUE_REARM);
488}
489
490static int
491lpfc_sli4_process_eq(struct lpfc_hba *phba, struct lpfc_queue *eq)
492{
493 struct lpfc_eqe *eqe;
494 int count = 0, consumed = 0;
495
496 if (cmpxchg(&eq->queue_claimed, 0, 1) != 0)
497 goto rearm_and_exit;
498
499 eqe = lpfc_sli4_eq_get(eq);
500 while (eqe) {
501 lpfc_sli4_hba_handle_eqe(phba, eq, eqe);
502 __lpfc_sli4_consume_eqe(phba, eq, eqe);
503
504 consumed++;
505 if (!(++count % eq->max_proc_limit))
506 break;
507
508 if (!(count % eq->notify_interval)) {
509 phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed,
510 LPFC_QUEUE_NOARM);
511 consumed = 0;
512 }
513
514 eqe = lpfc_sli4_eq_get(eq);
515 }
516 eq->EQ_processed += count;
517
518 /* Track the max number of EQEs processed in 1 intr */
519 if (count > eq->EQ_max_eqe)
520 eq->EQ_max_eqe = count;
521
522 eq->queue_claimed = 0;
523
524rearm_and_exit:
525 /* Always clear and re-arm the EQ */
526 phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed, LPFC_QUEUE_REARM);
527
528 return count;
James Smart27d6ac02018-02-22 08:18:42 -0800529}
530
531/**
James Smart4f774512009-05-22 14:52:35 -0400532 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
533 * @q: The Completion Queue to get the first valid CQE from
534 *
535 * This routine will get the first valid Completion Queue Entry from @q, update
536 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
537 * the Queue (no more work to do), or the Queue is full of CQEs that have been
538 * processed, but not popped back to the HBA then this routine will return NULL.
539 **/
540static struct lpfc_cqe *
541lpfc_sli4_cq_get(struct lpfc_queue *q)
542{
543 struct lpfc_cqe *cqe;
544
James Smart2e90f4b2011-12-13 13:22:37 -0500545 /* sanity check on queue memory */
546 if (unlikely(!q))
547 return NULL;
James Smart32517fc2019-01-28 11:14:33 -0800548 cqe = q->qe[q->host_index].cqe;
James Smart2e90f4b2011-12-13 13:22:37 -0500549
James Smart4f774512009-05-22 14:52:35 -0400550 /* If the next CQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800551 if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400552 return NULL;
James Smart27f344e2014-05-07 17:16:46 -0400553
554 /*
555 * insert barrier for instruction interlock : data from the hardware
556 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800557 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
558 * instructions allowing action on content before valid bit checked,
559 * add barrier here as well. May not be needed as "content" is a
560 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400561 */
562 mb();
James Smart4f774512009-05-22 14:52:35 -0400563 return cqe;
564}
565
James Smart32517fc2019-01-28 11:14:33 -0800566static void
567__lpfc_sli4_consume_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
568 struct lpfc_cqe *cqe)
569{
570 if (!phba->sli4_hba.pc_sli4_params.cqav)
571 bf_set_le32(lpfc_cqe_valid, cqe, 0);
572
573 cq->host_index = ((cq->host_index + 1) % cq->entry_count);
574
575 /* if the index wrapped around, toggle the valid bit */
576 if (phba->sli4_hba.pc_sli4_params.cqav && !cq->host_index)
577 cq->qe_valid = (cq->qe_valid) ? 0 : 1;
578}
579
James Smart4f774512009-05-22 14:52:35 -0400580/**
James Smart32517fc2019-01-28 11:14:33 -0800581 * lpfc_sli4_write_cq_db - write cq DB for entries consumed or arm state.
582 * @phba: the adapter with the CQ
James Smart4f774512009-05-22 14:52:35 -0400583 * @q: The Completion Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800584 * @count: the number of elements that were consumed
James Smart4f774512009-05-22 14:52:35 -0400585 * @arm: Indicates whether the host wants to arms this CQ.
586 *
James Smart32517fc2019-01-28 11:14:33 -0800587 * This routine will notify the HBA, by ringing the doorbell, that the
588 * CQEs have been processed. The @arm parameter specifies whether the
589 * queue should be rearmed when ringing the doorbell.
James Smart4f774512009-05-22 14:52:35 -0400590 **/
James Smart32517fc2019-01-28 11:14:33 -0800591void
592lpfc_sli4_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
593 uint32_t count, bool arm)
James Smart4f774512009-05-22 14:52:35 -0400594{
James Smart4f774512009-05-22 14:52:35 -0400595 struct lpfc_register doorbell;
596
James Smart2e90f4b2011-12-13 13:22:37 -0500597 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800598 if (unlikely(!q || (count == 0 && !arm)))
599 return;
James Smart4f774512009-05-22 14:52:35 -0400600
601 /* ring doorbell for number popped */
602 doorbell.word0 = 0;
603 if (arm)
604 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
James Smart32517fc2019-01-28 11:14:33 -0800605 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
James Smart4f774512009-05-22 14:52:35 -0400606 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500607 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
608 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
609 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800610 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400611}
612
613/**
James Smart32517fc2019-01-28 11:14:33 -0800614 * lpfc_sli4_if6_write_cq_db - write cq DB for entries consumed or arm state.
615 * @phba: the adapter with the CQ
James Smart27d6ac02018-02-22 08:18:42 -0800616 * @q: The Completion Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800617 * @count: the number of elements that were consumed
James Smart27d6ac02018-02-22 08:18:42 -0800618 * @arm: Indicates whether the host wants to arms this CQ.
619 *
James Smart32517fc2019-01-28 11:14:33 -0800620 * This routine will notify the HBA, by ringing the doorbell, that the
621 * CQEs have been processed. The @arm parameter specifies whether the
622 * queue should be rearmed when ringing the doorbell.
James Smart27d6ac02018-02-22 08:18:42 -0800623 **/
James Smart32517fc2019-01-28 11:14:33 -0800624void
625lpfc_sli4_if6_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
626 uint32_t count, bool arm)
James Smart27d6ac02018-02-22 08:18:42 -0800627{
James Smart27d6ac02018-02-22 08:18:42 -0800628 struct lpfc_register doorbell;
629
630 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800631 if (unlikely(!q || (count == 0 && !arm)))
632 return;
James Smart27d6ac02018-02-22 08:18:42 -0800633
634 /* ring doorbell for number popped */
635 doorbell.word0 = 0;
636 if (arm)
637 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
James Smart32517fc2019-01-28 11:14:33 -0800638 bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, count);
James Smart27d6ac02018-02-22 08:18:42 -0800639 bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
640 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
James Smart27d6ac02018-02-22 08:18:42 -0800641}
642
643/**
James Smart4f774512009-05-22 14:52:35 -0400644 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
645 * @q: The Header Receive Queue to operate on.
646 * @wqe: The Receive Queue Entry to put on the Receive queue.
647 *
648 * This routine will copy the contents of @wqe to the next available entry on
649 * the @q. This function will then ring the Receive Queue Doorbell to signal the
650 * HBA to start processing the Receive Queue Entry. This function returns the
651 * index that the rqe was copied to if successful. If no entries are available
652 * on @q then this function will return -ENOMEM.
653 * The caller is expected to hold the hbalock when calling this routine.
654 **/
James Smart895427b2017-02-12 13:52:30 -0800655int
James Smart4f774512009-05-22 14:52:35 -0400656lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
657 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
658{
James Smart2e90f4b2011-12-13 13:22:37 -0500659 struct lpfc_rqe *temp_hrqe;
660 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400661 struct lpfc_register doorbell;
James Smartcbc5de12017-12-08 17:18:04 -0800662 int hq_put_index;
663 int dq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400664
James Smart2e90f4b2011-12-13 13:22:37 -0500665 /* sanity check on queue memory */
666 if (unlikely(!hq) || unlikely(!dq))
667 return -ENOMEM;
James Smartcbc5de12017-12-08 17:18:04 -0800668 hq_put_index = hq->host_index;
669 dq_put_index = dq->host_index;
670 temp_hrqe = hq->qe[hq_put_index].rqe;
671 temp_drqe = dq->qe[dq_put_index].rqe;
James Smart2e90f4b2011-12-13 13:22:37 -0500672
James Smart4f774512009-05-22 14:52:35 -0400673 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
674 return -EINVAL;
James Smartcbc5de12017-12-08 17:18:04 -0800675 if (hq_put_index != dq_put_index)
James Smart4f774512009-05-22 14:52:35 -0400676 return -EINVAL;
677 /* If the host has not yet processed the next entry then we are done */
James Smartcbc5de12017-12-08 17:18:04 -0800678 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
James Smart4f774512009-05-22 14:52:35 -0400679 return -EBUSY;
James Smart48f8fdb2018-05-04 20:37:51 -0700680 lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
681 lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
James Smart4f774512009-05-22 14:52:35 -0400682
683 /* Update the host index to point to the next slot */
James Smartcbc5de12017-12-08 17:18:04 -0800684 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
685 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
James Smart61f3d4b2017-05-15 15:20:41 -0700686 hq->RQ_buf_posted++;
James Smart4f774512009-05-22 14:52:35 -0400687
688 /* Ring The Header Receive Queue Doorbell */
James Smart32517fc2019-01-28 11:14:33 -0800689 if (!(hq->host_index % hq->notify_interval)) {
James Smart4f774512009-05-22 14:52:35 -0400690 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500691 if (hq->db_format == LPFC_DB_RING_FORMAT) {
692 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
James Smart32517fc2019-01-28 11:14:33 -0800693 hq->notify_interval);
James Smart962bc512013-01-03 15:44:00 -0500694 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
695 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
696 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
James Smart32517fc2019-01-28 11:14:33 -0800697 hq->notify_interval);
James Smart962bc512013-01-03 15:44:00 -0500698 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
699 hq->host_index);
700 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
701 } else {
702 return -EINVAL;
703 }
704 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400705 }
James Smartcbc5de12017-12-08 17:18:04 -0800706 return hq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400707}
708
709/**
710 * lpfc_sli4_rq_release - Updates internal hba index for RQ
711 * @q: The Header Receive Queue to operate on.
712 *
713 * This routine will update the HBA index of a queue to reflect consumption of
714 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
715 * consumed an entry the host calls this function to update the queue's
716 * internal pointers. This routine returns the number of entries that were
717 * consumed by the HBA.
718 **/
719static uint32_t
720lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
721{
James Smart2e90f4b2011-12-13 13:22:37 -0500722 /* sanity check on queue memory */
723 if (unlikely(!hq) || unlikely(!dq))
724 return 0;
725
James Smart4f774512009-05-22 14:52:35 -0400726 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
727 return 0;
728 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
729 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
730 return 1;
731}
732
James Smarte59058c2008-08-24 21:49:00 -0400733/**
James Smart3621a712009-04-06 18:47:14 -0400734 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400735 * @phba: Pointer to HBA context object.
736 * @pring: Pointer to driver SLI ring object.
737 *
738 * This function returns pointer to next command iocb entry
739 * in the command ring. The caller must hold hbalock to prevent
740 * other threads consume the next command iocb.
741 * SLI-2/SLI-3 provide different sized iocbs.
742 **/
James Smarted957682007-06-17 19:56:37 -0500743static inline IOCB_t *
744lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
745{
James Smart7e56aa22012-08-03 12:35:34 -0400746 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
747 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500748}
749
James Smarte59058c2008-08-24 21:49:00 -0400750/**
James Smart3621a712009-04-06 18:47:14 -0400751 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400752 * @phba: Pointer to HBA context object.
753 * @pring: Pointer to driver SLI ring object.
754 *
755 * This function returns pointer to next response iocb entry
756 * in the response ring. The caller must hold hbalock to make sure
757 * that no other thread consume the next response iocb.
758 * SLI-2/SLI-3 provide different sized iocbs.
759 **/
James Smarted957682007-06-17 19:56:37 -0500760static inline IOCB_t *
761lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
762{
James Smart7e56aa22012-08-03 12:35:34 -0400763 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
764 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500765}
766
James Smarte59058c2008-08-24 21:49:00 -0400767/**
James Smart3621a712009-04-06 18:47:14 -0400768 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400769 * @phba: Pointer to HBA context object.
770 *
771 * This function is called with hbalock held. This function
772 * allocates a new driver iocb object from the iocb pool. If the
773 * allocation is successful, it returns pointer to the newly
774 * allocated iocb object else it returns NULL.
775 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400776struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500777__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400778{
779 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
780 struct lpfc_iocbq * iocbq = NULL;
781
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100782 lockdep_assert_held(&phba->hbalock);
783
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400784 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400785 if (iocbq)
786 phba->iocb_cnt++;
787 if (phba->iocb_cnt > phba->iocb_max)
788 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400789 return iocbq;
790}
791
James Smarte59058c2008-08-24 21:49:00 -0400792/**
James Smartda0436e2009-05-22 14:51:39 -0400793 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
794 * @phba: Pointer to HBA context object.
795 * @xritag: XRI value.
796 *
797 * This function clears the sglq pointer from the array of acive
798 * sglq's. The xritag that is passed in is used to index into the
799 * array. Before the xritag can be used it needs to be adjusted
800 * by subtracting the xribase.
801 *
802 * Returns sglq ponter = success, NULL = Failure.
803 **/
James Smart895427b2017-02-12 13:52:30 -0800804struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400805__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
806{
James Smartda0436e2009-05-22 14:51:39 -0400807 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400808
809 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
810 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400811 return sglq;
812}
813
814/**
815 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
816 * @phba: Pointer to HBA context object.
817 * @xritag: XRI value.
818 *
819 * This function returns the sglq pointer from the array of acive
820 * sglq's. The xritag that is passed in is used to index into the
821 * array. Before the xritag can be used it needs to be adjusted
822 * by subtracting the xribase.
823 *
824 * Returns sglq ponter = success, NULL = Failure.
825 **/
James Smart0f65ff62010-02-26 14:14:23 -0500826struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400827__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
828{
James Smartda0436e2009-05-22 14:51:39 -0400829 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400830
831 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400832 return sglq;
833}
834
835/**
James Smart1151e3e2011-02-16 12:39:35 -0500836 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500837 * @phba: Pointer to HBA context object.
838 * @xritag: xri used in this exchange.
839 * @rrq: The RRQ to be cleared.
840 *
James Smart19ca7602010-11-20 23:11:55 -0500841 **/
James Smart1151e3e2011-02-16 12:39:35 -0500842void
843lpfc_clr_rrq_active(struct lpfc_hba *phba,
844 uint16_t xritag,
845 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500846{
James Smart1151e3e2011-02-16 12:39:35 -0500847 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500848
James Smart1151e3e2011-02-16 12:39:35 -0500849 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
850 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500851
852 /* The target DID could have been swapped (cable swap)
853 * we should use the ndlp from the findnode if it is
854 * available.
855 */
James Smart1151e3e2011-02-16 12:39:35 -0500856 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500857 ndlp = rrq->ndlp;
858
James Smart1151e3e2011-02-16 12:39:35 -0500859 if (!ndlp)
860 goto out;
861
James Smartcff261f2013-12-17 20:29:47 -0500862 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500863 rrq->send_rrq = 0;
864 rrq->xritag = 0;
865 rrq->rrq_stop_time = 0;
866 }
James Smart1151e3e2011-02-16 12:39:35 -0500867out:
James Smart19ca7602010-11-20 23:11:55 -0500868 mempool_free(rrq, phba->rrq_pool);
869}
870
871/**
872 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
873 * @phba: Pointer to HBA context object.
874 *
875 * This function is called with hbalock held. This function
876 * Checks if stop_time (ratov from setting rrq active) has
877 * been reached, if it has and the send_rrq flag is set then
878 * it will call lpfc_send_rrq. If the send_rrq flag is not set
879 * then it will just call the routine to clear the rrq and
880 * free the rrq resource.
881 * The timer is set to the next rrq that is going to expire before
882 * leaving the routine.
883 *
884 **/
885void
886lpfc_handle_rrq_active(struct lpfc_hba *phba)
887{
888 struct lpfc_node_rrq *rrq;
889 struct lpfc_node_rrq *nextrrq;
890 unsigned long next_time;
891 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500892 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500893
894 spin_lock_irqsave(&phba->hbalock, iflags);
895 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400896 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500897 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500898 &phba->active_rrq_list, list) {
899 if (time_after(jiffies, rrq->rrq_stop_time))
900 list_move(&rrq->list, &send_rrq);
901 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500902 next_time = rrq->rrq_stop_time;
903 }
904 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500905 if ((!list_empty(&phba->active_rrq_list)) &&
906 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500907 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500908 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
909 list_del(&rrq->list);
910 if (!rrq->send_rrq)
911 /* this call will free the rrq */
912 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
913 else if (lpfc_send_rrq(phba, rrq)) {
914 /* if we send the rrq then the completion handler
915 * will clear the bit in the xribitmap.
916 */
917 lpfc_clr_rrq_active(phba, rrq->xritag,
918 rrq);
919 }
920 }
James Smart19ca7602010-11-20 23:11:55 -0500921}
922
923/**
924 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
925 * @vport: Pointer to vport context object.
926 * @xri: The xri used in the exchange.
927 * @did: The targets DID for this exchange.
928 *
929 * returns NULL = rrq not found in the phba->active_rrq_list.
930 * rrq = rrq for this xri and target.
931 **/
932struct lpfc_node_rrq *
933lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
934{
935 struct lpfc_hba *phba = vport->phba;
936 struct lpfc_node_rrq *rrq;
937 struct lpfc_node_rrq *nextrrq;
938 unsigned long iflags;
939
940 if (phba->sli_rev != LPFC_SLI_REV4)
941 return NULL;
942 spin_lock_irqsave(&phba->hbalock, iflags);
943 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
944 if (rrq->vport == vport && rrq->xritag == xri &&
945 rrq->nlp_DID == did){
946 list_del(&rrq->list);
947 spin_unlock_irqrestore(&phba->hbalock, iflags);
948 return rrq;
949 }
950 }
951 spin_unlock_irqrestore(&phba->hbalock, iflags);
952 return NULL;
953}
954
955/**
956 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
957 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500958 * @ndlp: Pointer to the lpfc_node_list structure.
959 * If ndlp is NULL Remove all active RRQs for this vport from the
960 * phba->active_rrq_list and clear the rrq.
961 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500962 **/
963void
James Smart1151e3e2011-02-16 12:39:35 -0500964lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500965
966{
967 struct lpfc_hba *phba = vport->phba;
968 struct lpfc_node_rrq *rrq;
969 struct lpfc_node_rrq *nextrrq;
970 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500971 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500972
973 if (phba->sli_rev != LPFC_SLI_REV4)
974 return;
James Smart1151e3e2011-02-16 12:39:35 -0500975 if (!ndlp) {
976 lpfc_sli4_vport_delete_els_xri_aborted(vport);
977 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500978 }
James Smart1151e3e2011-02-16 12:39:35 -0500979 spin_lock_irqsave(&phba->hbalock, iflags);
980 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
981 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
982 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500983 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500984
985 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
986 list_del(&rrq->list);
987 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
988 }
James Smart19ca7602010-11-20 23:11:55 -0500989}
990
991/**
James Smart1151e3e2011-02-16 12:39:35 -0500992 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500993 * @phba: Pointer to HBA context object.
994 * @ndlp: Targets nodelist pointer for this exchange.
995 * @xritag the xri in the bitmap to test.
996 *
997 * This function is called with hbalock held. This function
998 * returns 0 = rrq not active for this xri
999 * 1 = rrq is valid for this xri.
1000 **/
James Smart1151e3e2011-02-16 12:39:35 -05001001int
1002lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -05001003 uint16_t xritag)
1004{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001005 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -05001006 if (!ndlp)
1007 return 0;
James Smartcff261f2013-12-17 20:29:47 -05001008 if (!ndlp->active_rrqs_xri_bitmap)
1009 return 0;
1010 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -05001011 return 1;
1012 else
1013 return 0;
1014}
1015
1016/**
1017 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1018 * @phba: Pointer to HBA context object.
1019 * @ndlp: nodelist pointer for this target.
1020 * @xritag: xri used in this exchange.
1021 * @rxid: Remote Exchange ID.
1022 * @send_rrq: Flag used to determine if we should send rrq els cmd.
1023 *
1024 * This function takes the hbalock.
1025 * The active bit is always set in the active rrq xri_bitmap even
1026 * if there is no slot avaiable for the other rrq information.
1027 *
1028 * returns 0 rrq actived for this xri
1029 * < 0 No memory or invalid ndlp.
1030 **/
1031int
1032lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -05001033 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -05001034{
James Smart19ca7602010-11-20 23:11:55 -05001035 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -05001036 struct lpfc_node_rrq *rrq;
1037 int empty;
1038
1039 if (!ndlp)
1040 return -EINVAL;
1041
1042 if (!phba->cfg_enable_rrq)
1043 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001044
1045 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001046 if (phba->pport->load_flag & FC_UNLOADING) {
1047 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1048 goto out;
1049 }
1050
1051 /*
1052 * set the active bit even if there is no mem available.
1053 */
1054 if (NLP_CHK_FREE_REQ(ndlp))
1055 goto out;
1056
1057 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1058 goto out;
1059
James Smartcff261f2013-12-17 20:29:47 -05001060 if (!ndlp->active_rrqs_xri_bitmap)
1061 goto out;
1062
1063 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -05001064 goto out;
1065
James Smart19ca7602010-11-20 23:11:55 -05001066 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001067 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
1068 if (!rrq) {
1069 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1070 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1071 " DID:0x%x Send:%d\n",
1072 xritag, rxid, ndlp->nlp_DID, send_rrq);
1073 return -EINVAL;
1074 }
James Smarte5771b42013-03-01 16:37:14 -05001075 if (phba->cfg_enable_rrq == 1)
1076 rrq->send_rrq = send_rrq;
1077 else
1078 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -05001079 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -04001080 rrq->rrq_stop_time = jiffies +
1081 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -05001082 rrq->ndlp = ndlp;
1083 rrq->nlp_DID = ndlp->nlp_DID;
1084 rrq->vport = ndlp->vport;
1085 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -05001086 spin_lock_irqsave(&phba->hbalock, iflags);
1087 empty = list_empty(&phba->active_rrq_list);
1088 list_add_tail(&rrq->list, &phba->active_rrq_list);
1089 phba->hba_flag |= HBA_RRQ_ACTIVE;
1090 if (empty)
1091 lpfc_worker_wake_up(phba);
1092 spin_unlock_irqrestore(&phba->hbalock, iflags);
1093 return 0;
1094out:
1095 spin_unlock_irqrestore(&phba->hbalock, iflags);
1096 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1097 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1098 " DID:0x%x Send:%d\n",
1099 xritag, rxid, ndlp->nlp_DID, send_rrq);
1100 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001101}
1102
1103/**
James Smart895427b2017-02-12 13:52:30 -08001104 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -04001105 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -05001106 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -04001107 *
James Smartdafe8ce2014-09-03 12:56:40 -04001108 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -04001109 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -04001110 * list is not empty then it is successful, it returns pointer to the newly
1111 * allocated sglq object else it returns NULL.
1112 **/
1113static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -08001114__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -04001115{
James Smart895427b2017-02-12 13:52:30 -08001116 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -04001117 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001118 struct lpfc_sglq *start_sglq = NULL;
James Smartc4908502019-01-28 11:14:28 -08001119 struct lpfc_io_buf *lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001120 struct lpfc_nodelist *ndlp;
1121 int found = 0;
1122
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001123 lockdep_assert_held(&phba->hbalock);
1124
James Smart19ca7602010-11-20 23:11:55 -05001125 if (piocbq->iocb_flag & LPFC_IO_FCP) {
James Smartc4908502019-01-28 11:14:28 -08001126 lpfc_cmd = (struct lpfc_io_buf *) piocbq->context1;
James Smart19ca7602010-11-20 23:11:55 -05001127 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -05001128 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -04001129 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -05001130 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -04001131 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1132 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1133 ndlp = NULL;
1134 else
1135 ndlp = piocbq->context_un.ndlp;
1136 } else {
James Smart19ca7602010-11-20 23:11:55 -05001137 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -04001138 }
James Smart19ca7602010-11-20 23:11:55 -05001139
James Smart895427b2017-02-12 13:52:30 -08001140 spin_lock(&phba->sli4_hba.sgl_list_lock);
1141 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -05001142 start_sglq = sglq;
1143 while (!found) {
1144 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -08001145 break;
James Smart895427b2017-02-12 13:52:30 -08001146 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1147 test_bit(sglq->sli4_lxritag,
1148 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -05001149 /* This xri has an rrq outstanding for this DID.
1150 * put it back in the list and get another xri.
1151 */
James Smart895427b2017-02-12 13:52:30 -08001152 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001153 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -08001154 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -05001155 struct lpfc_sglq, list);
1156 if (sglq == start_sglq) {
James Smart14041bd2017-06-01 21:07:01 -07001157 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001158 sglq = NULL;
1159 break;
1160 } else
1161 continue;
1162 }
1163 sglq->ndlp = ndlp;
1164 found = 1;
James Smart6d368e52011-05-24 11:44:12 -04001165 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -05001166 sglq->state = SGL_ALLOCATED;
1167 }
James Smart895427b2017-02-12 13:52:30 -08001168 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001169 return sglq;
1170}
1171
1172/**
James Smartf358dd02017-02-12 13:52:34 -08001173 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1174 * @phba: Pointer to HBA context object.
1175 * @piocb: Pointer to the iocbq.
1176 *
1177 * This function is called with the sgl_list lock held. This function
1178 * gets a new driver sglq object from the sglq list. If the
1179 * list is not empty then it is successful, it returns pointer to the newly
1180 * allocated sglq object else it returns NULL.
1181 **/
1182struct lpfc_sglq *
1183__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1184{
1185 struct list_head *lpfc_nvmet_sgl_list;
1186 struct lpfc_sglq *sglq = NULL;
1187
1188 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1189
1190 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1191
1192 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1193 if (!sglq)
1194 return NULL;
1195 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1196 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001197 return sglq;
1198}
1199
James Smarte59058c2008-08-24 21:49:00 -04001200/**
James Smart3621a712009-04-06 18:47:14 -04001201 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001202 * @phba: Pointer to HBA context object.
1203 *
1204 * This function is called with no lock held. This function
1205 * allocates a new driver iocb object from the iocb pool. If the
1206 * allocation is successful, it returns pointer to the newly
1207 * allocated iocb object else it returns NULL.
1208 **/
James Smart2e0fef82007-06-17 19:56:36 -05001209struct lpfc_iocbq *
1210lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001211{
James Smart2e0fef82007-06-17 19:56:36 -05001212 struct lpfc_iocbq * iocbq = NULL;
1213 unsigned long iflags;
1214
1215 spin_lock_irqsave(&phba->hbalock, iflags);
1216 iocbq = __lpfc_sli_get_iocbq(phba);
1217 spin_unlock_irqrestore(&phba->hbalock, iflags);
1218 return iocbq;
1219}
1220
James Smarte59058c2008-08-24 21:49:00 -04001221/**
James Smart4f774512009-05-22 14:52:35 -04001222 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1223 * @phba: Pointer to HBA context object.
1224 * @iocbq: Pointer to driver iocb object.
1225 *
1226 * This function is called with hbalock held to release driver
1227 * iocb object to the iocb pool. The iotag in the iocb object
1228 * does not change for each use of the iocb object. This function
1229 * clears all other fields of the iocb object when it is freed.
1230 * The sqlq structure that holds the xritag and phys and virtual
1231 * mappings for the scatter gather list is retrieved from the
1232 * active array of sglq. The get of the sglq pointer also clears
1233 * the entry in the array. If the status of the IO indiactes that
1234 * this IO was aborted then the sglq entry it put on the
1235 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1236 * IO has good status or fails for any other reason then the sglq
James Smart895427b2017-02-12 13:52:30 -08001237 * entry is added to the free list (lpfc_els_sgl_list).
James Smart4f774512009-05-22 14:52:35 -04001238 **/
1239static void
1240__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1241{
1242 struct lpfc_sglq *sglq;
1243 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001244 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001245 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001246
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001247 lockdep_assert_held(&phba->hbalock);
1248
James Smart4f774512009-05-22 14:52:35 -04001249 if (iocbq->sli4_xritag == NO_XRI)
1250 sglq = NULL;
1251 else
James Smart6d368e52011-05-24 11:44:12 -04001252 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1253
James Smart0e9bb8d2013-03-01 16:35:12 -05001254
James Smart4f774512009-05-22 14:52:35 -04001255 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001256 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1257 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1258 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001259 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001260 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001261 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001262 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1263 spin_unlock_irqrestore(
1264 &phba->sli4_hba.sgl_list_lock, iflag);
1265 goto out;
1266 }
1267
James Smart895427b2017-02-12 13:52:30 -08001268 pring = phba->sli4_hba.els_wq->pring;
James Smart4f774512009-05-22 14:52:35 -04001269 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1270 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001271 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1272 iflag);
James Smart341af102010-01-26 23:07:37 -05001273 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001274 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001275 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001276 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001277 } else {
James Smart895427b2017-02-12 13:52:30 -08001278 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1279 iflag);
James Smart4f774512009-05-22 14:52:35 -04001280 sglq->state = SGL_FREED;
1281 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001282 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001283 &phba->sli4_hba.lpfc_els_sgl_list);
1284 spin_unlock_irqrestore(
1285 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001286
1287 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001288 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001289 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001290 }
James Smart4f774512009-05-22 14:52:35 -04001291 }
1292
James Smartf358dd02017-02-12 13:52:34 -08001293out:
James Smart4f774512009-05-22 14:52:35 -04001294 /*
1295 * Clean all volatile data fields, preserve iotag and node struct.
1296 */
1297 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001298 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001299 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001300 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1301 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001302 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1303}
1304
James Smart2a9bf3d2010-06-07 15:24:45 -04001305
James Smart4f774512009-05-22 14:52:35 -04001306/**
James Smart3772a992009-05-22 14:50:54 -04001307 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1308 * @phba: Pointer to HBA context object.
1309 * @iocbq: Pointer to driver iocb object.
1310 *
1311 * This function is called with hbalock held to release driver
1312 * iocb object to the iocb pool. The iotag in the iocb object
1313 * does not change for each use of the iocb object. This function
1314 * clears all other fields of the iocb object when it is freed.
1315 **/
1316static void
1317__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1318{
1319 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1320
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001321 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001322
1323 /*
James Smart3772a992009-05-22 14:50:54 -04001324 * Clean all volatile data fields, preserve iotag and node struct.
1325 */
1326 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1327 iocbq->sli4_xritag = NO_XRI;
1328 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1329}
1330
1331/**
James Smart3621a712009-04-06 18:47:14 -04001332 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001333 * @phba: Pointer to HBA context object.
1334 * @iocbq: Pointer to driver iocb object.
1335 *
1336 * This function is called with hbalock held to release driver
1337 * iocb object to the iocb pool. The iotag in the iocb object
1338 * does not change for each use of the iocb object. This function
1339 * clears all other fields of the iocb object when it is freed.
1340 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001341static void
James Smart2e0fef82007-06-17 19:56:36 -05001342__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1343{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001344 lockdep_assert_held(&phba->hbalock);
1345
James Smart3772a992009-05-22 14:50:54 -04001346 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001347 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001348}
1349
James Smarte59058c2008-08-24 21:49:00 -04001350/**
James Smart3621a712009-04-06 18:47:14 -04001351 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001352 * @phba: Pointer to HBA context object.
1353 * @iocbq: Pointer to driver iocb object.
1354 *
1355 * This function is called with no lock held to release the iocb to
1356 * iocb pool.
1357 **/
James Smart2e0fef82007-06-17 19:56:36 -05001358void
1359lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1360{
1361 unsigned long iflags;
1362
1363 /*
1364 * Clean all volatile data fields, preserve iotag and node struct.
1365 */
1366 spin_lock_irqsave(&phba->hbalock, iflags);
1367 __lpfc_sli_release_iocbq(phba, iocbq);
1368 spin_unlock_irqrestore(&phba->hbalock, iflags);
1369}
1370
James Smarte59058c2008-08-24 21:49:00 -04001371/**
James Smarta257bf92009-04-06 18:48:10 -04001372 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1373 * @phba: Pointer to HBA context object.
1374 * @iocblist: List of IOCBs.
1375 * @ulpstatus: ULP status in IOCB command field.
1376 * @ulpWord4: ULP word-4 in IOCB command field.
1377 *
1378 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1379 * on the list by invoking the complete callback function associated with the
1380 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1381 * fields.
1382 **/
1383void
1384lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1385 uint32_t ulpstatus, uint32_t ulpWord4)
1386{
1387 struct lpfc_iocbq *piocb;
1388
1389 while (!list_empty(iocblist)) {
1390 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001391 if (!piocb->iocb_cmpl)
1392 lpfc_sli_release_iocbq(phba, piocb);
1393 else {
1394 piocb->iocb.ulpStatus = ulpstatus;
1395 piocb->iocb.un.ulpWord[4] = ulpWord4;
1396 (piocb->iocb_cmpl) (phba, piocb, piocb);
1397 }
1398 }
1399 return;
1400}
1401
1402/**
James Smart3621a712009-04-06 18:47:14 -04001403 * lpfc_sli_iocb_cmd_type - Get the iocb type
1404 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001405 *
1406 * This function is called by ring event handler function to get the iocb type.
1407 * This function translates the iocb command to an iocb command type used to
1408 * decide the final disposition of each completed IOCB.
1409 * The function returns
1410 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1411 * LPFC_SOL_IOCB if it is a solicited iocb completion
1412 * LPFC_ABORT_IOCB if it is an abort iocb
1413 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1414 *
1415 * The caller is not required to hold any lock.
1416 **/
dea31012005-04-17 16:05:31 -05001417static lpfc_iocb_type
1418lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1419{
1420 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1421
1422 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1423 return 0;
1424
1425 switch (iocb_cmnd) {
1426 case CMD_XMIT_SEQUENCE_CR:
1427 case CMD_XMIT_SEQUENCE_CX:
1428 case CMD_XMIT_BCAST_CN:
1429 case CMD_XMIT_BCAST_CX:
1430 case CMD_ELS_REQUEST_CR:
1431 case CMD_ELS_REQUEST_CX:
1432 case CMD_CREATE_XRI_CR:
1433 case CMD_CREATE_XRI_CX:
1434 case CMD_GET_RPI_CN:
1435 case CMD_XMIT_ELS_RSP_CX:
1436 case CMD_GET_RPI_CR:
1437 case CMD_FCP_IWRITE_CR:
1438 case CMD_FCP_IWRITE_CX:
1439 case CMD_FCP_IREAD_CR:
1440 case CMD_FCP_IREAD_CX:
1441 case CMD_FCP_ICMND_CR:
1442 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001443 case CMD_FCP_TSEND_CX:
1444 case CMD_FCP_TRSP_CX:
1445 case CMD_FCP_TRECEIVE_CX:
1446 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001447 case CMD_ADAPTER_MSG:
1448 case CMD_ADAPTER_DUMP:
1449 case CMD_XMIT_SEQUENCE64_CR:
1450 case CMD_XMIT_SEQUENCE64_CX:
1451 case CMD_XMIT_BCAST64_CN:
1452 case CMD_XMIT_BCAST64_CX:
1453 case CMD_ELS_REQUEST64_CR:
1454 case CMD_ELS_REQUEST64_CX:
1455 case CMD_FCP_IWRITE64_CR:
1456 case CMD_FCP_IWRITE64_CX:
1457 case CMD_FCP_IREAD64_CR:
1458 case CMD_FCP_IREAD64_CX:
1459 case CMD_FCP_ICMND64_CR:
1460 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001461 case CMD_FCP_TSEND64_CX:
1462 case CMD_FCP_TRSP64_CX:
1463 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001464 case CMD_GEN_REQUEST64_CR:
1465 case CMD_GEN_REQUEST64_CX:
1466 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001467 case DSSCMD_IWRITE64_CR:
1468 case DSSCMD_IWRITE64_CX:
1469 case DSSCMD_IREAD64_CR:
1470 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001471 type = LPFC_SOL_IOCB;
1472 break;
1473 case CMD_ABORT_XRI_CN:
1474 case CMD_ABORT_XRI_CX:
1475 case CMD_CLOSE_XRI_CN:
1476 case CMD_CLOSE_XRI_CX:
1477 case CMD_XRI_ABORTED_CX:
1478 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001479 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001480 type = LPFC_ABORT_IOCB;
1481 break;
1482 case CMD_RCV_SEQUENCE_CX:
1483 case CMD_RCV_ELS_REQ_CX:
1484 case CMD_RCV_SEQUENCE64_CX:
1485 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001486 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001487 case CMD_IOCB_RCV_SEQ64_CX:
1488 case CMD_IOCB_RCV_ELS64_CX:
1489 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001490 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001491 type = LPFC_UNSOL_IOCB;
1492 break;
James Smart3163f722008-02-08 18:50:25 -05001493 case CMD_IOCB_XMIT_MSEQ64_CR:
1494 case CMD_IOCB_XMIT_MSEQ64_CX:
1495 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1496 case CMD_IOCB_RCV_ELS_LIST64_CX:
1497 case CMD_IOCB_CLOSE_EXTENDED_CN:
1498 case CMD_IOCB_ABORT_EXTENDED_CN:
1499 case CMD_IOCB_RET_HBQE64_CN:
1500 case CMD_IOCB_FCP_IBIDIR64_CR:
1501 case CMD_IOCB_FCP_IBIDIR64_CX:
1502 case CMD_IOCB_FCP_ITASKMGT64_CX:
1503 case CMD_IOCB_LOGENTRY_CN:
1504 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1505 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001506 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001507 type = LPFC_UNKNOWN_IOCB;
1508 break;
dea31012005-04-17 16:05:31 -05001509 default:
1510 type = LPFC_UNKNOWN_IOCB;
1511 break;
1512 }
1513
1514 return type;
1515}
1516
James Smarte59058c2008-08-24 21:49:00 -04001517/**
James Smart3621a712009-04-06 18:47:14 -04001518 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001519 * @phba: Pointer to HBA context object.
1520 *
1521 * This function is called from SLI initialization code
1522 * to configure every ring of the HBA's SLI interface. The
1523 * caller is not required to hold any lock. This function issues
1524 * a config_ring mailbox command for each ring.
1525 * This function returns zero if successful else returns a negative
1526 * error code.
1527 **/
dea31012005-04-17 16:05:31 -05001528static int
James Smarted957682007-06-17 19:56:37 -05001529lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001530{
1531 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001532 LPFC_MBOXQ_t *pmb;
1533 MAILBOX_t *pmbox;
1534 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001535
James Smarted957682007-06-17 19:56:37 -05001536 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1537 if (!pmb)
1538 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001539 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001540 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001541 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001542 lpfc_config_ring(phba, i, pmb);
1543 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1544 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001545 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001546 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001547 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1548 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001549 rc, pmbox->mbxCommand,
1550 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001551 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001552 ret = -ENXIO;
1553 break;
dea31012005-04-17 16:05:31 -05001554 }
1555 }
James Smarted957682007-06-17 19:56:37 -05001556 mempool_free(pmb, phba->mbox_mem_pool);
1557 return ret;
dea31012005-04-17 16:05:31 -05001558}
1559
James Smarte59058c2008-08-24 21:49:00 -04001560/**
James Smart3621a712009-04-06 18:47:14 -04001561 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001562 * @phba: Pointer to HBA context object.
1563 * @pring: Pointer to driver SLI ring object.
1564 * @piocb: Pointer to the driver iocb object.
1565 *
1566 * This function is called with hbalock held. The function adds the
1567 * new iocb to txcmplq of the given ring. This function always returns
1568 * 0. If this function is called for ELS ring, this function checks if
1569 * there is a vport associated with the ELS command. This function also
1570 * starts els_tmofunc timer if this is an ELS command.
1571 **/
dea31012005-04-17 16:05:31 -05001572static int
James Smart2e0fef82007-06-17 19:56:36 -05001573lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1574 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001575{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001576 lockdep_assert_held(&phba->hbalock);
1577
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001578 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001579
dea31012005-04-17 16:05:31 -05001580 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001581 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08001582 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001583
James Smart92d7f7b2007-06-17 19:56:38 -05001584 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1585 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001586 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1587 BUG_ON(!piocb->vport);
1588 if (!(piocb->vport->load_flag & FC_UNLOADING))
1589 mod_timer(&piocb->vport->els_tmofunc,
1590 jiffies +
1591 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1592 }
dea31012005-04-17 16:05:31 -05001593
James Smart2e0fef82007-06-17 19:56:36 -05001594 return 0;
dea31012005-04-17 16:05:31 -05001595}
1596
James Smarte59058c2008-08-24 21:49:00 -04001597/**
James Smart3621a712009-04-06 18:47:14 -04001598 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001599 * @phba: Pointer to HBA context object.
1600 * @pring: Pointer to driver SLI ring object.
1601 *
1602 * This function is called with hbalock held to get next
1603 * iocb in txq of the given ring. If there is any iocb in
1604 * the txq, the function returns first iocb in the list after
1605 * removing the iocb from the list, else it returns NULL.
1606 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001607struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001608lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001609{
dea31012005-04-17 16:05:31 -05001610 struct lpfc_iocbq *cmd_iocb;
1611
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001612 lockdep_assert_held(&phba->hbalock);
1613
James Smart858c9f62007-06-17 19:56:39 -05001614 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001615 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001616}
1617
James Smarte59058c2008-08-24 21:49:00 -04001618/**
James Smart3621a712009-04-06 18:47:14 -04001619 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001620 * @phba: Pointer to HBA context object.
1621 * @pring: Pointer to driver SLI ring object.
1622 *
1623 * This function is called with hbalock held and the caller must post the
1624 * iocb without releasing the lock. If the caller releases the lock,
1625 * iocb slot returned by the function is not guaranteed to be available.
1626 * The function returns pointer to the next available iocb slot if there
1627 * is available slot in the ring, else it returns NULL.
1628 * If the get index of the ring is ahead of the put index, the function
1629 * will post an error attention event to the worker thread to take the
1630 * HBA to offline state.
1631 **/
dea31012005-04-17 16:05:31 -05001632static IOCB_t *
1633lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1634{
James Smart34b02dc2008-08-24 21:49:55 -04001635 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001636 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001637
1638 lockdep_assert_held(&phba->hbalock);
1639
James Smart7e56aa22012-08-03 12:35:34 -04001640 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1641 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1642 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001643
James Smart7e56aa22012-08-03 12:35:34 -04001644 if (unlikely(pring->sli.sli3.local_getidx ==
1645 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001646
James Smart7e56aa22012-08-03 12:35:34 -04001647 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001648
James Smart7e56aa22012-08-03 12:35:34 -04001649 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001650 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001651 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001652 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001653 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001654 pring->sli.sli3.local_getidx,
1655 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001656
James Smart2e0fef82007-06-17 19:56:36 -05001657 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001658 /*
1659 * All error attention handlers are posted to
1660 * worker thread
1661 */
1662 phba->work_ha |= HA_ERATT;
1663 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001664
James Smart5e9d9b82008-06-14 22:52:53 -04001665 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001666
1667 return NULL;
1668 }
1669
James Smart7e56aa22012-08-03 12:35:34 -04001670 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001671 return NULL;
1672 }
1673
James Smarted957682007-06-17 19:56:37 -05001674 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001675}
1676
James Smarte59058c2008-08-24 21:49:00 -04001677/**
James Smart3621a712009-04-06 18:47:14 -04001678 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001679 * @phba: Pointer to HBA context object.
1680 * @iocbq: Pointer to driver iocb object.
1681 *
1682 * This function gets an iotag for the iocb. If there is no unused iotag and
1683 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1684 * array and assigns a new iotag.
1685 * The function returns the allocated iotag if successful, else returns zero.
1686 * Zero is not a valid iotag.
1687 * The caller is not required to hold any lock.
1688 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001689uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001690lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001691{
James Smart2e0fef82007-06-17 19:56:36 -05001692 struct lpfc_iocbq **new_arr;
1693 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001694 size_t new_len;
1695 struct lpfc_sli *psli = &phba->sli;
1696 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001697
James Smart2e0fef82007-06-17 19:56:36 -05001698 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001699 iotag = psli->last_iotag;
1700 if(++iotag < psli->iocbq_lookup_len) {
1701 psli->last_iotag = iotag;
1702 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001703 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001704 iocbq->iotag = iotag;
1705 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001706 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001707 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1708 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001709 spin_unlock_irq(&phba->hbalock);
Kees Cook6396bb22018-06-12 14:03:40 -07001710 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001711 GFP_KERNEL);
1712 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001713 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001714 old_arr = psli->iocbq_lookup;
1715 if (new_len <= psli->iocbq_lookup_len) {
1716 /* highly unprobable case */
1717 kfree(new_arr);
1718 iotag = psli->last_iotag;
1719 if(++iotag < psli->iocbq_lookup_len) {
1720 psli->last_iotag = iotag;
1721 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001722 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001723 iocbq->iotag = iotag;
1724 return iotag;
1725 }
James Smart2e0fef82007-06-17 19:56:36 -05001726 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001727 return 0;
1728 }
1729 if (psli->iocbq_lookup)
1730 memcpy(new_arr, old_arr,
1731 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001732 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001733 psli->iocbq_lookup = new_arr;
1734 psli->iocbq_lookup_len = new_len;
1735 psli->last_iotag = iotag;
1736 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001737 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001738 iocbq->iotag = iotag;
1739 kfree(old_arr);
1740 return iotag;
1741 }
James Smart8f6d98d2006-08-01 07:34:00 -04001742 } else
James Smart2e0fef82007-06-17 19:56:36 -05001743 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001744
James Smartbc739052010-08-04 16:11:18 -04001745 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001746 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1747 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001748
James Bottomley604a3e32005-10-29 10:28:33 -05001749 return 0;
dea31012005-04-17 16:05:31 -05001750}
1751
James Smarte59058c2008-08-24 21:49:00 -04001752/**
James Smart3621a712009-04-06 18:47:14 -04001753 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001754 * @phba: Pointer to HBA context object.
1755 * @pring: Pointer to driver SLI ring object.
1756 * @iocb: Pointer to iocb slot in the ring.
1757 * @nextiocb: Pointer to driver iocb object which need to be
1758 * posted to firmware.
1759 *
1760 * This function is called with hbalock held to post a new iocb to
1761 * the firmware. This function copies the new iocb to ring iocb slot and
1762 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1763 * a completion call back for this iocb else the function will free the
1764 * iocb object.
1765 **/
dea31012005-04-17 16:05:31 -05001766static void
1767lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1768 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1769{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001770 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001771 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001772 * Set up an iotag
dea31012005-04-17 16:05:31 -05001773 */
James Bottomley604a3e32005-10-29 10:28:33 -05001774 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001775
James Smarte2a0a9d2008-12-04 22:40:02 -05001776
James Smarta58cbd52007-08-02 11:09:43 -04001777 if (pring->ringno == LPFC_ELS_RING) {
1778 lpfc_debugfs_slow_ring_trc(phba,
1779 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1780 *(((uint32_t *) &nextiocb->iocb) + 4),
1781 *(((uint32_t *) &nextiocb->iocb) + 6),
1782 *(((uint32_t *) &nextiocb->iocb) + 7));
1783 }
1784
dea31012005-04-17 16:05:31 -05001785 /*
1786 * Issue iocb command to adapter
1787 */
James Smart92d7f7b2007-06-17 19:56:38 -05001788 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001789 wmb();
1790 pring->stats.iocb_cmd++;
1791
1792 /*
1793 * If there is no completion routine to call, we can release the
1794 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1795 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1796 */
1797 if (nextiocb->iocb_cmpl)
1798 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001799 else
James Smart2e0fef82007-06-17 19:56:36 -05001800 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001801
1802 /*
1803 * Let the HBA know what IOCB slot will be the next one the
1804 * driver will put a command into.
1805 */
James Smart7e56aa22012-08-03 12:35:34 -04001806 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1807 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001808}
1809
James Smarte59058c2008-08-24 21:49:00 -04001810/**
James Smart3621a712009-04-06 18:47:14 -04001811 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001812 * @phba: Pointer to HBA context object.
1813 * @pring: Pointer to driver SLI ring object.
1814 *
1815 * The caller is not required to hold any lock for calling this function.
1816 * This function updates the chip attention bits for the ring to inform firmware
1817 * that there are pending work to be done for this ring and requests an
1818 * interrupt when there is space available in the ring. This function is
1819 * called when the driver is unable to post more iocbs to the ring due
1820 * to unavailability of space in the ring.
1821 **/
dea31012005-04-17 16:05:31 -05001822static void
James Smart2e0fef82007-06-17 19:56:36 -05001823lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001824{
1825 int ringno = pring->ringno;
1826
1827 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1828
1829 wmb();
1830
1831 /*
1832 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1833 * The HBA will tell us when an IOCB entry is available.
1834 */
1835 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1836 readl(phba->CAregaddr); /* flush */
1837
1838 pring->stats.iocb_cmd_full++;
1839}
1840
James Smarte59058c2008-08-24 21:49:00 -04001841/**
James Smart3621a712009-04-06 18:47:14 -04001842 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001843 * @phba: Pointer to HBA context object.
1844 * @pring: Pointer to driver SLI ring object.
1845 *
1846 * This function updates the chip attention register bit for the
1847 * given ring to inform HBA that there is more work to be done
1848 * in this ring. The caller is not required to hold any lock.
1849 **/
dea31012005-04-17 16:05:31 -05001850static void
James Smart2e0fef82007-06-17 19:56:36 -05001851lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001852{
1853 int ringno = pring->ringno;
1854
1855 /*
1856 * Tell the HBA that there is work to do in this ring.
1857 */
James Smart34b02dc2008-08-24 21:49:55 -04001858 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1859 wmb();
1860 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1861 readl(phba->CAregaddr); /* flush */
1862 }
dea31012005-04-17 16:05:31 -05001863}
1864
James Smarte59058c2008-08-24 21:49:00 -04001865/**
James Smart3621a712009-04-06 18:47:14 -04001866 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001867 * @phba: Pointer to HBA context object.
1868 * @pring: Pointer to driver SLI ring object.
1869 *
1870 * This function is called with hbalock held to post pending iocbs
1871 * in the txq to the firmware. This function is called when driver
1872 * detects space available in the ring.
1873 **/
dea31012005-04-17 16:05:31 -05001874static void
James Smart2e0fef82007-06-17 19:56:36 -05001875lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001876{
1877 IOCB_t *iocb;
1878 struct lpfc_iocbq *nextiocb;
1879
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001880 lockdep_assert_held(&phba->hbalock);
1881
dea31012005-04-17 16:05:31 -05001882 /*
1883 * Check to see if:
1884 * (a) there is anything on the txq to send
1885 * (b) link is up
1886 * (c) link attention events can be processed (fcp ring only)
1887 * (d) IOCB processing is not blocked by the outstanding mbox command.
1888 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001889
1890 if (lpfc_is_link_up(phba) &&
1891 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001892 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001893 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001894
1895 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1896 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1897 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1898
1899 if (iocb)
1900 lpfc_sli_update_ring(phba, pring);
1901 else
1902 lpfc_sli_update_full_ring(phba, pring);
1903 }
1904
1905 return;
1906}
1907
James Smarte59058c2008-08-24 21:49:00 -04001908/**
James Smart3621a712009-04-06 18:47:14 -04001909 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001910 * @phba: Pointer to HBA context object.
1911 * @hbqno: HBQ number.
1912 *
1913 * This function is called with hbalock held to get the next
1914 * available slot for the given HBQ. If there is free slot
1915 * available for the HBQ it will return pointer to the next available
1916 * HBQ entry else it will return NULL.
1917 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001918static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001919lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1920{
1921 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1922
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001923 lockdep_assert_held(&phba->hbalock);
1924
James Smarted957682007-06-17 19:56:37 -05001925 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1926 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1927 hbqp->next_hbqPutIdx = 0;
1928
1929 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001930 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001931 uint32_t getidx = le32_to_cpu(raw_index);
1932
1933 hbqp->local_hbqGetIdx = getidx;
1934
1935 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1936 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001937 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001938 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001939 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001940 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001941 hbqp->entry_count);
1942
1943 phba->link_state = LPFC_HBA_ERROR;
1944 return NULL;
1945 }
1946
1947 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1948 return NULL;
1949 }
1950
James Smart51ef4c22007-08-02 11:10:31 -04001951 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1952 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001953}
1954
James Smarte59058c2008-08-24 21:49:00 -04001955/**
James Smart3621a712009-04-06 18:47:14 -04001956 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001957 * @phba: Pointer to HBA context object.
1958 *
1959 * This function is called with no lock held to free all the
1960 * hbq buffers while uninitializing the SLI interface. It also
1961 * frees the HBQ buffers returned by the firmware but not yet
1962 * processed by the upper layers.
1963 **/
James Smarted957682007-06-17 19:56:37 -05001964void
1965lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1966{
James Smart92d7f7b2007-06-17 19:56:38 -05001967 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1968 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001969 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001970 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001971
James Smart51ef4c22007-08-02 11:10:31 -04001972 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001973 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001974 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001975 for (i = 0; i < hbq_count; ++i) {
1976 list_for_each_entry_safe(dmabuf, next_dmabuf,
1977 &phba->hbqs[i].hbq_buffer_list, list) {
1978 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1979 list_del(&hbq_buf->dbuf.list);
1980 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1981 }
James Smarta8adb832007-10-27 13:37:53 -04001982 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001983 }
James Smart3163f722008-02-08 18:50:25 -05001984
1985 /* Mark the HBQs not in use */
1986 phba->hbq_in_use = 0;
1987 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001988}
1989
James Smarte59058c2008-08-24 21:49:00 -04001990/**
James Smart3621a712009-04-06 18:47:14 -04001991 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001992 * @phba: Pointer to HBA context object.
1993 * @hbqno: HBQ number.
1994 * @hbq_buf: Pointer to HBQ buffer.
1995 *
1996 * This function is called with the hbalock held to post a
1997 * hbq buffer to the firmware. If the function finds an empty
1998 * slot in the HBQ, it will post the buffer. The function will return
1999 * pointer to the hbq entry if it successfully post the buffer
2000 * else it will return NULL.
2001 **/
James Smart3772a992009-05-22 14:50:54 -04002002static int
James Smarted957682007-06-17 19:56:37 -05002003lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05002004 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05002005{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002006 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002007 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2008}
2009
2010/**
2011 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2012 * @phba: Pointer to HBA context object.
2013 * @hbqno: HBQ number.
2014 * @hbq_buf: Pointer to HBQ buffer.
2015 *
2016 * This function is called with the hbalock held to post a hbq buffer to the
2017 * firmware. If the function finds an empty slot in the HBQ, it will post the
2018 * buffer and place it on the hbq_buffer_list. The function will return zero if
2019 * it successfully post the buffer else it will return an error.
2020 **/
2021static int
2022lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2023 struct hbq_dmabuf *hbq_buf)
2024{
James Smarted957682007-06-17 19:56:37 -05002025 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05002026 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05002027
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002028 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05002029 /* Get next HBQ entry slot to use */
2030 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2031 if (hbqe) {
2032 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2033
James Smart92d7f7b2007-06-17 19:56:38 -05002034 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2035 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08002036 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05002037 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002038 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2039 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2040 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05002041 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2042 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05002043 /* flush */
James Smarted957682007-06-17 19:56:37 -05002044 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04002045 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04002046 return 0;
2047 } else
2048 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05002049}
2050
James Smart4f774512009-05-22 14:52:35 -04002051/**
2052 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2053 * @phba: Pointer to HBA context object.
2054 * @hbqno: HBQ number.
2055 * @hbq_buf: Pointer to HBQ buffer.
2056 *
2057 * This function is called with the hbalock held to post an RQE to the SLI4
2058 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2059 * the hbq_buffer_list and return zero, otherwise it will return an error.
2060 **/
2061static int
2062lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2063 struct hbq_dmabuf *hbq_buf)
2064{
2065 int rc;
2066 struct lpfc_rqe hrqe;
2067 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08002068 struct lpfc_queue *hrq;
2069 struct lpfc_queue *drq;
2070
2071 if (hbqno != LPFC_ELS_HBQ)
2072 return 1;
2073 hrq = phba->sli4_hba.hdr_rq;
2074 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04002075
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002076 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04002077 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2078 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2079 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2080 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08002081 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04002082 if (rc < 0)
2083 return rc;
James Smart895427b2017-02-12 13:52:30 -08002084 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04002085 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2086 return 0;
2087}
2088
James Smarte59058c2008-08-24 21:49:00 -04002089/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05002090static struct lpfc_hbq_init lpfc_els_hbq = {
2091 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05002092 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05002093 .mask_count = 0,
2094 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04002095 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05002096 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04002097 .init_count = 40,
2098 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05002099};
James Smarted957682007-06-17 19:56:37 -05002100
James Smarte59058c2008-08-24 21:49:00 -04002101/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04002102struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05002103 &lpfc_els_hbq,
2104};
2105
James Smarte59058c2008-08-24 21:49:00 -04002106/**
James Smart3621a712009-04-06 18:47:14 -04002107 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04002108 * @phba: Pointer to HBA context object.
2109 * @hbqno: HBQ number.
2110 * @count: Number of HBQ buffers to be posted.
2111 *
James Smartd7c255b2008-08-24 21:50:00 -04002112 * This function is called with no lock held to post more hbq buffers to the
2113 * given HBQ. The function returns the number of HBQ buffers successfully
2114 * posted.
James Smarte59058c2008-08-24 21:49:00 -04002115 **/
James Smart311464e2007-08-02 11:10:37 -04002116static int
James Smart92d7f7b2007-06-17 19:56:38 -05002117lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2118{
James Smartd7c255b2008-08-24 21:50:00 -04002119 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05002120 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05002121 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04002122 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002123 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04002124 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04002125
James Smartd7c255b2008-08-24 21:50:00 -04002126 if ((phba->hbqs[hbqno].buffer_count + count) >
2127 lpfc_hbq_defs[hbqno]->entry_count)
2128 count = lpfc_hbq_defs[hbqno]->entry_count -
2129 phba->hbqs[hbqno].buffer_count;
2130 if (!count)
2131 return 0;
2132 /* Allocate HBQ entries */
2133 for (i = 0; i < count; i++) {
2134 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2135 if (!hbq_buffer)
2136 break;
2137 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2138 }
James Smart3163f722008-02-08 18:50:25 -05002139 /* Check whether HBQ is still in use */
2140 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002141 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04002142 goto err;
2143 while (!list_empty(&hbq_buf_list)) {
2144 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2145 dbuf.list);
2146 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2147 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04002148 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04002149 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04002150 posted++;
2151 } else
James Smart51ef4c22007-08-02 11:10:31 -04002152 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05002153 }
James Smart3163f722008-02-08 18:50:25 -05002154 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04002155 return posted;
2156err:
2157 spin_unlock_irqrestore(&phba->hbalock, flags);
2158 while (!list_empty(&hbq_buf_list)) {
2159 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2160 dbuf.list);
2161 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2162 }
James Smart92d7f7b2007-06-17 19:56:38 -05002163 return 0;
James Smarted957682007-06-17 19:56:37 -05002164}
2165
James Smarte59058c2008-08-24 21:49:00 -04002166/**
James Smart3621a712009-04-06 18:47:14 -04002167 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04002168 * @phba: Pointer to HBA context object.
2169 * @qno: HBQ number.
2170 *
2171 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04002172 * is called with no lock held. The function returns the number of HBQ entries
2173 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002174 **/
James Smarted957682007-06-17 19:56:37 -05002175int
James Smart92d7f7b2007-06-17 19:56:38 -05002176lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002177{
James Smartdef9c7a2009-12-21 17:02:28 -05002178 if (phba->sli_rev == LPFC_SLI_REV4)
2179 return 0;
2180 else
2181 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2182 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05002183}
2184
James Smarte59058c2008-08-24 21:49:00 -04002185/**
James Smart3621a712009-04-06 18:47:14 -04002186 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002187 * @phba: Pointer to HBA context object.
2188 * @qno: HBQ queue number.
2189 *
2190 * This function is called from SLI initialization code path with
2191 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002192 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002193 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002194static int
James Smart92d7f7b2007-06-17 19:56:38 -05002195lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002196{
James Smartdef9c7a2009-12-21 17:02:28 -05002197 if (phba->sli_rev == LPFC_SLI_REV4)
2198 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002199 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002200 else
2201 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2202 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002203}
2204
James Smarte59058c2008-08-24 21:49:00 -04002205/**
James Smart3772a992009-05-22 14:50:54 -04002206 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2207 * @phba: Pointer to HBA context object.
2208 * @hbqno: HBQ number.
2209 *
2210 * This function removes the first hbq buffer on an hbq list and returns a
2211 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2212 **/
2213static struct hbq_dmabuf *
2214lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2215{
2216 struct lpfc_dmabuf *d_buf;
2217
2218 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2219 if (!d_buf)
2220 return NULL;
2221 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2222}
2223
2224/**
James Smart2d7dbc42017-02-12 13:52:35 -08002225 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2226 * @phba: Pointer to HBA context object.
2227 * @hbqno: HBQ number.
2228 *
2229 * This function removes the first RQ buffer on an RQ buffer list and returns a
2230 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2231 **/
2232static struct rqb_dmabuf *
2233lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2234{
2235 struct lpfc_dmabuf *h_buf;
2236 struct lpfc_rqb *rqbp;
2237
2238 rqbp = hrq->rqbp;
2239 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2240 struct lpfc_dmabuf, list);
2241 if (!h_buf)
2242 return NULL;
2243 rqbp->buffer_count--;
2244 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2245}
2246
2247/**
James Smart3621a712009-04-06 18:47:14 -04002248 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002249 * @phba: Pointer to HBA context object.
2250 * @tag: Tag of the hbq buffer.
2251 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002252 * This function searches for the hbq buffer associated with the given tag in
2253 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2254 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002255 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002256static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002257lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2258{
James Smart92d7f7b2007-06-17 19:56:38 -05002259 struct lpfc_dmabuf *d_buf;
2260 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002261 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002262
James Smart51ef4c22007-08-02 11:10:31 -04002263 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002264 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002265 return NULL;
2266
James Smart3772a992009-05-22 14:50:54 -04002267 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002268 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002269 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002270 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002271 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002272 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002273 }
2274 }
James Smart3772a992009-05-22 14:50:54 -04002275 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002276 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002277 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002278 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002279 return NULL;
James Smarted957682007-06-17 19:56:37 -05002280}
2281
James Smarte59058c2008-08-24 21:49:00 -04002282/**
James Smart3621a712009-04-06 18:47:14 -04002283 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002284 * @phba: Pointer to HBA context object.
2285 * @hbq_buffer: Pointer to HBQ buffer.
2286 *
2287 * This function is called with hbalock. This function gives back
2288 * the hbq buffer to firmware. If the HBQ does not have space to
2289 * post the buffer, it will free the buffer.
2290 **/
James Smarted957682007-06-17 19:56:37 -05002291void
James Smart51ef4c22007-08-02 11:10:31 -04002292lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002293{
2294 uint32_t hbqno;
2295
James Smart51ef4c22007-08-02 11:10:31 -04002296 if (hbq_buffer) {
2297 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002298 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002299 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002300 }
2301}
2302
James Smarte59058c2008-08-24 21:49:00 -04002303/**
James Smart3621a712009-04-06 18:47:14 -04002304 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002305 * @mbxCommand: mailbox command code.
2306 *
2307 * This function is called by the mailbox event handler function to verify
2308 * that the completed mailbox command is a legitimate mailbox command. If the
2309 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2310 * and the mailbox event handler will take the HBA offline.
2311 **/
dea31012005-04-17 16:05:31 -05002312static int
2313lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2314{
2315 uint8_t ret;
2316
2317 switch (mbxCommand) {
2318 case MBX_LOAD_SM:
2319 case MBX_READ_NV:
2320 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002321 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002322 case MBX_RUN_BIU_DIAG:
2323 case MBX_INIT_LINK:
2324 case MBX_DOWN_LINK:
2325 case MBX_CONFIG_LINK:
2326 case MBX_CONFIG_RING:
2327 case MBX_RESET_RING:
2328 case MBX_READ_CONFIG:
2329 case MBX_READ_RCONFIG:
2330 case MBX_READ_SPARM:
2331 case MBX_READ_STATUS:
2332 case MBX_READ_RPI:
2333 case MBX_READ_XRI:
2334 case MBX_READ_REV:
2335 case MBX_READ_LNK_STAT:
2336 case MBX_REG_LOGIN:
2337 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002338 case MBX_CLEAR_LA:
2339 case MBX_DUMP_MEMORY:
2340 case MBX_DUMP_CONTEXT:
2341 case MBX_RUN_DIAGS:
2342 case MBX_RESTART:
2343 case MBX_UPDATE_CFG:
2344 case MBX_DOWN_LOAD:
2345 case MBX_DEL_LD_ENTRY:
2346 case MBX_RUN_PROGRAM:
2347 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002348 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002349 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002350 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002351 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002352 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002353 case MBX_LOAD_AREA:
2354 case MBX_RUN_BIU_DIAG64:
2355 case MBX_CONFIG_PORT:
2356 case MBX_READ_SPARM64:
2357 case MBX_READ_RPI64:
2358 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002359 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002360 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002361 case MBX_SET_DEBUG:
2362 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002363 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002364 case MBX_REG_VPI:
2365 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002366 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002367 case MBX_PORT_CAPABILITIES:
2368 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002369 case MBX_SLI4_CONFIG:
2370 case MBX_SLI4_REQ_FTRS:
2371 case MBX_REG_FCFI:
2372 case MBX_UNREG_FCFI:
2373 case MBX_REG_VFI:
2374 case MBX_UNREG_VFI:
2375 case MBX_INIT_VPI:
2376 case MBX_INIT_VFI:
2377 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002378 case MBX_READ_EVENT_LOG_STATUS:
2379 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002380 case MBX_SECURITY_MGMT:
2381 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002382 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002383 ret = mbxCommand;
2384 break;
2385 default:
2386 ret = MBX_SHUTDOWN;
2387 break;
2388 }
James Smart2e0fef82007-06-17 19:56:36 -05002389 return ret;
dea31012005-04-17 16:05:31 -05002390}
James Smarte59058c2008-08-24 21:49:00 -04002391
2392/**
James Smart3621a712009-04-06 18:47:14 -04002393 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002394 * @phba: Pointer to HBA context object.
2395 * @pmboxq: Pointer to mailbox command.
2396 *
2397 * This is completion handler function for mailbox commands issued from
2398 * lpfc_sli_issue_mbox_wait function. This function is called by the
2399 * mailbox event handler function with no lock held. This function
2400 * will wake up thread waiting on the wait queue pointed by context1
2401 * of the mailbox.
2402 **/
James Smart04c68492009-05-22 14:52:52 -04002403void
James Smart2e0fef82007-06-17 19:56:36 -05002404lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002405{
James Smart858c9f62007-06-17 19:56:39 -05002406 unsigned long drvr_flag;
James Smarte29d74f2018-03-05 12:04:07 -08002407 struct completion *pmbox_done;
dea31012005-04-17 16:05:31 -05002408
2409 /*
James Smarte29d74f2018-03-05 12:04:07 -08002410 * If pmbox_done is empty, the driver thread gave up waiting and
dea31012005-04-17 16:05:31 -05002411 * continued running.
2412 */
James Smart7054a602007-04-25 09:52:34 -04002413 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002414 spin_lock_irqsave(&phba->hbalock, drvr_flag);
James Smarte29d74f2018-03-05 12:04:07 -08002415 pmbox_done = (struct completion *)pmboxq->context3;
2416 if (pmbox_done)
2417 complete(pmbox_done);
James Smart858c9f62007-06-17 19:56:39 -05002418 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002419 return;
2420}
2421
James Smarte59058c2008-08-24 21:49:00 -04002422
2423/**
James Smart3621a712009-04-06 18:47:14 -04002424 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002425 * @phba: Pointer to HBA context object.
2426 * @pmb: Pointer to mailbox object.
2427 *
2428 * This function is the default mailbox completion handler. It
2429 * frees the memory resources associated with the completed mailbox
2430 * command. If the completed command is a REG_LOGIN mailbox command,
2431 * this function will issue a UREG_LOGIN to re-claim the RPI.
2432 **/
dea31012005-04-17 16:05:31 -05002433void
James Smart2e0fef82007-06-17 19:56:36 -05002434lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002435{
James Smartd439d282010-09-29 11:18:45 -04002436 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002437 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002438 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002439 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002440 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002441 int rc;
2442
James Smart3e1f0712018-11-29 16:09:29 -08002443 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
James Smart7054a602007-04-25 09:52:34 -04002444
dea31012005-04-17 16:05:31 -05002445 if (mp) {
2446 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2447 kfree(mp);
2448 }
James Smart7054a602007-04-25 09:52:34 -04002449
2450 /*
2451 * If a REG_LOGIN succeeded after node is destroyed or node
2452 * is in re-discovery driver need to cleanup the RPI.
2453 */
James Smart2e0fef82007-06-17 19:56:36 -05002454 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002455 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2456 !pmb->u.mb.mbxStatus) {
2457 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002458 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002459 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002460 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002461 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002462 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2463 if (rc != MBX_NOT_FINISHED)
2464 return;
2465 }
2466
James Smart695a8142010-01-26 23:08:03 -05002467 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2468 !(phba->pport->load_flag & FC_UNLOADING) &&
2469 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002470 shost = lpfc_shost_from_vport(vport);
2471 spin_lock_irq(shost->host_lock);
2472 vport->vpi_state |= LPFC_VPI_REGISTERED;
2473 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2474 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002475 }
2476
James Smartd439d282010-09-29 11:18:45 -04002477 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -08002478 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -04002479 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002480 pmb->ctx_buf = NULL;
2481 pmb->ctx_ndlp = NULL;
2482 }
2483
2484 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2485 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2486
2487 /* Check to see if there are any deferred events to process */
2488 if (ndlp) {
2489 lpfc_printf_vlog(
2490 vport,
2491 KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2492 "1438 UNREG cmpl deferred mbox x%x "
2493 "on NPort x%x Data: x%x x%x %p\n",
2494 ndlp->nlp_rpi, ndlp->nlp_DID,
2495 ndlp->nlp_flag, ndlp->nlp_defer_did, ndlp);
2496
2497 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2498 (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
James Smart00292e02018-12-13 15:17:55 -08002499 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002500 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2501 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002502 } else {
2503 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002504 }
James Smartdea16bd2018-11-29 16:09:30 -08002505 }
James Smart3e1f0712018-11-29 16:09:29 -08002506 pmb->ctx_ndlp = NULL;
James Smartd439d282010-09-29 11:18:45 -04002507 }
2508
James Smartdcf2a4e2010-09-29 11:18:53 -04002509 /* Check security permission status on INIT_LINK mailbox command */
2510 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2511 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2512 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2513 "2860 SLI authentication is required "
2514 "for INIT_LINK but has not done yet\n");
2515
James Smart04c68492009-05-22 14:52:52 -04002516 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2517 lpfc_sli4_mbox_cmd_free(phba, pmb);
2518 else
2519 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002520}
James Smartbe6bb942015-04-07 15:07:22 -04002521 /**
2522 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2523 * @phba: Pointer to HBA context object.
2524 * @pmb: Pointer to mailbox object.
2525 *
2526 * This function is the unreg rpi mailbox completion handler. It
2527 * frees the memory resources associated with the completed mailbox
2528 * command. An additional refrenece is put on the ndlp to prevent
2529 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2530 * the unreg mailbox command completes, this routine puts the
2531 * reference back.
2532 *
2533 **/
2534void
2535lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2536{
2537 struct lpfc_vport *vport = pmb->vport;
2538 struct lpfc_nodelist *ndlp;
2539
James Smart3e1f0712018-11-29 16:09:29 -08002540 ndlp = pmb->ctx_ndlp;
James Smartbe6bb942015-04-07 15:07:22 -04002541 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2542 if (phba->sli_rev == LPFC_SLI_REV4 &&
2543 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002544 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002545 LPFC_SLI_INTF_IF_TYPE_2)) {
2546 if (ndlp) {
James Smartdea16bd2018-11-29 16:09:30 -08002547 lpfc_printf_vlog(
2548 vport, KERN_INFO, LOG_MBOX | LOG_SLI,
2549 "0010 UNREG_LOGIN vpi:%x "
2550 "rpi:%x DID:%x defer x%x flg x%x "
2551 "map:%x %p\n",
2552 vport->vpi, ndlp->nlp_rpi,
2553 ndlp->nlp_DID, ndlp->nlp_defer_did,
2554 ndlp->nlp_flag,
2555 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002556 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002557 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002558
2559 /* Check to see if there are any deferred
2560 * events to process
2561 */
2562 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2563 (ndlp->nlp_defer_did !=
2564 NLP_EVT_NOTHING_PENDING)) {
2565 lpfc_printf_vlog(
2566 vport, KERN_INFO, LOG_DISCOVERY,
2567 "4111 UNREG cmpl deferred "
2568 "clr x%x on "
2569 "NPort x%x Data: x%x %p\n",
2570 ndlp->nlp_rpi, ndlp->nlp_DID,
2571 ndlp->nlp_defer_did, ndlp);
James Smart00292e02018-12-13 15:17:55 -08002572 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002573 ndlp->nlp_defer_did =
2574 NLP_EVT_NOTHING_PENDING;
2575 lpfc_issue_els_plogi(
2576 vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002577 } else {
2578 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002579 }
James Smartbe6bb942015-04-07 15:07:22 -04002580 }
2581 }
2582 }
2583
2584 mempool_free(pmb, phba->mbox_mem_pool);
2585}
dea31012005-04-17 16:05:31 -05002586
James Smarte59058c2008-08-24 21:49:00 -04002587/**
James Smart3621a712009-04-06 18:47:14 -04002588 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002589 * @phba: Pointer to HBA context object.
2590 *
2591 * This function is called with no lock held. This function processes all
2592 * the completed mailbox commands and gives it to upper layers. The interrupt
2593 * service routine processes mailbox completion interrupt and adds completed
2594 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2595 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2596 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2597 * function returns the mailbox commands to the upper layer by calling the
2598 * completion handler function of each mailbox.
2599 **/
dea31012005-04-17 16:05:31 -05002600int
James Smart2e0fef82007-06-17 19:56:36 -05002601lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002602{
James Smart92d7f7b2007-06-17 19:56:38 -05002603 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002604 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002605 int rc;
2606 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002607
2608 phba->sli.slistat.mbox_event++;
2609
James Smart92d7f7b2007-06-17 19:56:38 -05002610 /* Get all completed mailboxe buffers into the cmplq */
2611 spin_lock_irq(&phba->hbalock);
2612 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2613 spin_unlock_irq(&phba->hbalock);
2614
dea31012005-04-17 16:05:31 -05002615 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002616 do {
2617 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2618 if (pmb == NULL)
2619 break;
2620
James Smart04c68492009-05-22 14:52:52 -04002621 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002622
James Smart858c9f62007-06-17 19:56:39 -05002623 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2624 if (pmb->vport) {
2625 lpfc_debugfs_disc_trc(pmb->vport,
2626 LPFC_DISC_TRC_MBOX_VPORT,
2627 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2628 (uint32_t)pmbox->mbxCommand,
2629 pmbox->un.varWords[0],
2630 pmbox->un.varWords[1]);
2631 }
2632 else {
2633 lpfc_debugfs_disc_trc(phba->pport,
2634 LPFC_DISC_TRC_MBOX,
2635 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2636 (uint32_t)pmbox->mbxCommand,
2637 pmbox->un.varWords[0],
2638 pmbox->un.varWords[1]);
2639 }
2640 }
2641
dea31012005-04-17 16:05:31 -05002642 /*
2643 * It is a fatal error if unknown mbox command completion.
2644 */
2645 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2646 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002647 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002648 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002649 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002650 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002651 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002652 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002653 lpfc_sli_config_mbox_subsys_get(phba,
2654 pmb),
2655 lpfc_sli_config_mbox_opcode_get(phba,
2656 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002657 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002658 phba->work_hs = HS_FFER3;
2659 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002660 continue;
dea31012005-04-17 16:05:31 -05002661 }
2662
dea31012005-04-17 16:05:31 -05002663 if (pmbox->mbxStatus) {
2664 phba->sli.slistat.mbox_stat_err++;
2665 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2666 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002667 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002668 LOG_MBOX | LOG_SLI,
2669 "(%d):0305 Mbox cmd cmpl "
2670 "error - RETRYing Data: x%x "
2671 "(x%x/x%x) x%x x%x x%x\n",
2672 pmb->vport ? pmb->vport->vpi : 0,
2673 pmbox->mbxCommand,
2674 lpfc_sli_config_mbox_subsys_get(phba,
2675 pmb),
2676 lpfc_sli_config_mbox_opcode_get(phba,
2677 pmb),
2678 pmbox->mbxStatus,
2679 pmbox->un.varWords[0],
2680 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002681 pmbox->mbxStatus = 0;
2682 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002683 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002684 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002685 continue;
dea31012005-04-17 16:05:31 -05002686 }
2687 }
2688
2689 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002690 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002691 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002692 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2693 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002694 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002695 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002696 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2697 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002698 pmb->mbox_cmpl,
2699 *((uint32_t *) pmbox),
2700 pmbox->un.varWords[0],
2701 pmbox->un.varWords[1],
2702 pmbox->un.varWords[2],
2703 pmbox->un.varWords[3],
2704 pmbox->un.varWords[4],
2705 pmbox->un.varWords[5],
2706 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002707 pmbox->un.varWords[7],
2708 pmbox->un.varWords[8],
2709 pmbox->un.varWords[9],
2710 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002711
James Smart92d7f7b2007-06-17 19:56:38 -05002712 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002713 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002714 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002715 return 0;
dea31012005-04-17 16:05:31 -05002716}
James Smart92d7f7b2007-06-17 19:56:38 -05002717
James Smarte59058c2008-08-24 21:49:00 -04002718/**
James Smart3621a712009-04-06 18:47:14 -04002719 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002720 * @phba: Pointer to HBA context object.
2721 * @pring: Pointer to driver SLI ring object.
2722 * @tag: buffer tag.
2723 *
2724 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2725 * is set in the tag the buffer is posted for a particular exchange,
2726 * the function will return the buffer without replacing the buffer.
2727 * If the buffer is for unsolicited ELS or CT traffic, this function
2728 * returns the buffer and also posts another buffer to the firmware.
2729 **/
James Smart76bb24e2007-10-27 13:38:00 -04002730static struct lpfc_dmabuf *
2731lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002732 struct lpfc_sli_ring *pring,
2733 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002734{
James Smart9f1e1b52008-12-04 22:39:40 -05002735 struct hbq_dmabuf *hbq_entry;
2736
James Smart76bb24e2007-10-27 13:38:00 -04002737 if (tag & QUE_BUFTAG_BIT)
2738 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002739 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2740 if (!hbq_entry)
2741 return NULL;
2742 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002743}
James Smart57127f12007-10-27 13:37:05 -04002744
James Smart3772a992009-05-22 14:50:54 -04002745/**
2746 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2747 * @phba: Pointer to HBA context object.
2748 * @pring: Pointer to driver SLI ring object.
2749 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2750 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2751 * @fch_type: the type for the first frame of the sequence.
2752 *
2753 * This function is called with no lock held. This function uses the r_ctl and
2754 * type of the received sequence to find the correct callback function to call
2755 * to process the sequence.
2756 **/
2757static int
2758lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2759 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2760 uint32_t fch_type)
2761{
2762 int i;
2763
James Smartf358dd02017-02-12 13:52:34 -08002764 switch (fch_type) {
2765 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002766 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002767 return 1;
2768 default:
2769 break;
2770 }
2771
James Smart3772a992009-05-22 14:50:54 -04002772 /* unSolicited Responses */
2773 if (pring->prt[0].profile) {
2774 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2775 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2776 saveq);
2777 return 1;
2778 }
2779 /* We must search, based on rctl / type
2780 for the right routine */
2781 for (i = 0; i < pring->num_mask; i++) {
2782 if ((pring->prt[i].rctl == fch_r_ctl) &&
2783 (pring->prt[i].type == fch_type)) {
2784 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2785 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2786 (phba, pring, saveq);
2787 return 1;
2788 }
2789 }
2790 return 0;
2791}
James Smarte59058c2008-08-24 21:49:00 -04002792
2793/**
James Smart3621a712009-04-06 18:47:14 -04002794 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002795 * @phba: Pointer to HBA context object.
2796 * @pring: Pointer to driver SLI ring object.
2797 * @saveq: Pointer to the unsolicited iocb.
2798 *
2799 * This function is called with no lock held by the ring event handler
2800 * when there is an unsolicited iocb posted to the response ring by the
2801 * firmware. This function gets the buffer associated with the iocbs
2802 * and calls the event handler for the ring. This function handles both
2803 * qring buffers and hbq buffers.
2804 * When the function returns 1 the caller can free the iocb object otherwise
2805 * upper layer functions will free the iocb objects.
2806 **/
dea31012005-04-17 16:05:31 -05002807static int
2808lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2809 struct lpfc_iocbq *saveq)
2810{
2811 IOCB_t * irsp;
2812 WORD5 * w5p;
2813 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002814 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002815 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002816
dea31012005-04-17 16:05:31 -05002817 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002818
2819 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2820 if (pring->lpfc_sli_rcv_async_status)
2821 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2822 else
2823 lpfc_printf_log(phba,
2824 KERN_WARNING,
2825 LOG_SLI,
2826 "0316 Ring %d handler: unexpected "
2827 "ASYNC_STATUS iocb received evt_code "
2828 "0x%x\n",
2829 pring->ringno,
2830 irsp->un.asyncstat.evt_code);
2831 return 1;
2832 }
2833
James Smart3163f722008-02-08 18:50:25 -05002834 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2835 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2836 if (irsp->ulpBdeCount > 0) {
2837 dmzbuf = lpfc_sli_get_buff(phba, pring,
2838 irsp->un.ulpWord[3]);
2839 lpfc_in_buf_free(phba, dmzbuf);
2840 }
2841
2842 if (irsp->ulpBdeCount > 1) {
2843 dmzbuf = lpfc_sli_get_buff(phba, pring,
2844 irsp->unsli3.sli3Words[3]);
2845 lpfc_in_buf_free(phba, dmzbuf);
2846 }
2847
2848 if (irsp->ulpBdeCount > 2) {
2849 dmzbuf = lpfc_sli_get_buff(phba, pring,
2850 irsp->unsli3.sli3Words[7]);
2851 lpfc_in_buf_free(phba, dmzbuf);
2852 }
2853
2854 return 1;
2855 }
2856
James Smart92d7f7b2007-06-17 19:56:38 -05002857 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002858 if (irsp->ulpBdeCount != 0) {
2859 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002860 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002861 if (!saveq->context2)
2862 lpfc_printf_log(phba,
2863 KERN_ERR,
2864 LOG_SLI,
2865 "0341 Ring %d Cannot find buffer for "
2866 "an unsolicited iocb. tag 0x%x\n",
2867 pring->ringno,
2868 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002869 }
2870 if (irsp->ulpBdeCount == 2) {
2871 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002872 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002873 if (!saveq->context3)
2874 lpfc_printf_log(phba,
2875 KERN_ERR,
2876 LOG_SLI,
2877 "0342 Ring %d Cannot find buffer for an"
2878 " unsolicited iocb. tag 0x%x\n",
2879 pring->ringno,
2880 irsp->unsli3.sli3Words[7]);
2881 }
2882 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002883 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002884 if (irsp->ulpBdeCount != 0) {
2885 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2886 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002887 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002888 lpfc_printf_log(phba,
2889 KERN_ERR,
2890 LOG_SLI,
2891 "0343 Ring %d Cannot find "
2892 "buffer for an unsolicited iocb"
2893 ". tag 0x%x\n", pring->ringno,
2894 irsp->un.ulpWord[3]);
2895 }
2896 if (irsp->ulpBdeCount == 2) {
2897 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2898 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002899 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002900 lpfc_printf_log(phba,
2901 KERN_ERR,
2902 LOG_SLI,
2903 "0344 Ring %d Cannot find "
2904 "buffer for an unsolicited "
2905 "iocb. tag 0x%x\n",
2906 pring->ringno,
2907 irsp->unsli3.sli3Words[7]);
2908 }
2909 }
James Smart92d7f7b2007-06-17 19:56:38 -05002910 }
James Smart9c2face2008-01-11 01:53:18 -05002911 if (irsp->ulpBdeCount != 0 &&
2912 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2913 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2914 int found = 0;
2915
2916 /* search continue save q for same XRI */
2917 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002918 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2919 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002920 list_add_tail(&saveq->list, &iocbq->list);
2921 found = 1;
2922 break;
2923 }
2924 }
2925 if (!found)
2926 list_add_tail(&saveq->clist,
2927 &pring->iocb_continue_saveq);
2928 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2929 list_del_init(&iocbq->clist);
2930 saveq = iocbq;
2931 irsp = &(saveq->iocb);
2932 } else
2933 return 0;
2934 }
2935 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2936 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2937 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002938 Rctl = FC_RCTL_ELS_REQ;
2939 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002940 } else {
2941 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2942 Rctl = w5p->hcsw.Rctl;
2943 Type = w5p->hcsw.Type;
2944
2945 /* Firmware Workaround */
2946 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2947 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2948 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002949 Rctl = FC_RCTL_ELS_REQ;
2950 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002951 w5p->hcsw.Rctl = Rctl;
2952 w5p->hcsw.Type = Type;
2953 }
2954 }
James Smart92d7f7b2007-06-17 19:56:38 -05002955
James Smart3772a992009-05-22 14:50:54 -04002956 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002957 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002958 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002959 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002960 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002961
James Smart92d7f7b2007-06-17 19:56:38 -05002962 return 1;
dea31012005-04-17 16:05:31 -05002963}
2964
James Smarte59058c2008-08-24 21:49:00 -04002965/**
James Smart3621a712009-04-06 18:47:14 -04002966 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002967 * @phba: Pointer to HBA context object.
2968 * @pring: Pointer to driver SLI ring object.
2969 * @prspiocb: Pointer to response iocb object.
2970 *
2971 * This function looks up the iocb_lookup table to get the command iocb
2972 * corresponding to the given response iocb using the iotag of the
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002973 * response iocb. This function is called with the hbalock held
2974 * for sli3 devices or the ring_lock for sli4 devices.
James Smarte59058c2008-08-24 21:49:00 -04002975 * This function returns the command iocb object if it finds the command
2976 * iocb else returns NULL.
2977 **/
dea31012005-04-17 16:05:31 -05002978static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002979lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2980 struct lpfc_sli_ring *pring,
2981 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002982{
dea31012005-04-17 16:05:31 -05002983 struct lpfc_iocbq *cmd_iocb = NULL;
2984 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002985 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002986
James Bottomley604a3e32005-10-29 10:28:33 -05002987 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002988
James Bottomley604a3e32005-10-29 10:28:33 -05002989 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2990 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002991 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002992 /* remove from txcmpl queue list */
2993 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002994 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08002995 pring->txcmplq_cnt--;
James Smart89533e92016-10-13 15:06:15 -07002996 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002997 }
dea31012005-04-17 16:05:31 -05002998 }
2999
dea31012005-04-17 16:05:31 -05003000 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07003001 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05003002 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003003 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05003004 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05003005 return NULL;
3006}
3007
James Smarte59058c2008-08-24 21:49:00 -04003008/**
James Smart3772a992009-05-22 14:50:54 -04003009 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3010 * @phba: Pointer to HBA context object.
3011 * @pring: Pointer to driver SLI ring object.
3012 * @iotag: IOCB tag.
3013 *
3014 * This function looks up the iocb_lookup table to get the command iocb
3015 * corresponding to the given iotag. This function is called with the
3016 * hbalock held.
3017 * This function returns the command iocb object if it finds the command
3018 * iocb else returns NULL.
3019 **/
3020static struct lpfc_iocbq *
3021lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3022 struct lpfc_sli_ring *pring, uint16_t iotag)
3023{
James Smart895427b2017-02-12 13:52:30 -08003024 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04003025
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01003026 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04003027 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3028 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003029 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3030 /* remove from txcmpl queue list */
3031 list_del_init(&cmd_iocb->list);
3032 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003033 pring->txcmplq_cnt--;
James Smart4f2e66c2012-05-09 21:17:07 -04003034 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003035 }
James Smart3772a992009-05-22 14:50:54 -04003036 }
James Smart89533e92016-10-13 15:06:15 -07003037
James Smart3772a992009-05-22 14:50:54 -04003038 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08003039 "0372 iotag x%x lookup error: max iotag (x%x) "
3040 "iocb_flag x%x\n",
3041 iotag, phba->sli.last_iotag,
3042 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04003043 return NULL;
3044}
3045
3046/**
James Smart3621a712009-04-06 18:47:14 -04003047 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04003048 * @phba: Pointer to HBA context object.
3049 * @pring: Pointer to driver SLI ring object.
3050 * @saveq: Pointer to the response iocb to be processed.
3051 *
3052 * This function is called by the ring event handler for non-fcp
3053 * rings when there is a new response iocb in the response ring.
3054 * The caller is not required to hold any locks. This function
3055 * gets the command iocb associated with the response iocb and
3056 * calls the completion handler for the command iocb. If there
3057 * is no completion handler, the function will free the resources
3058 * associated with command iocb. If the response iocb is for
3059 * an already aborted command iocb, the status of the completion
3060 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3061 * This function always returns 1.
3062 **/
dea31012005-04-17 16:05:31 -05003063static int
James Smart2e0fef82007-06-17 19:56:36 -05003064lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003065 struct lpfc_iocbq *saveq)
3066{
James Smart2e0fef82007-06-17 19:56:36 -05003067 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05003068 int rc = 1;
3069 unsigned long iflag;
3070
3071 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003072 if (phba->sli_rev == LPFC_SLI_REV4)
3073 spin_lock_irqsave(&pring->ring_lock, iflag);
3074 else
3075 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003076 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003077 if (phba->sli_rev == LPFC_SLI_REV4)
3078 spin_unlock_irqrestore(&pring->ring_lock, iflag);
3079 else
3080 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart2e0fef82007-06-17 19:56:36 -05003081
dea31012005-04-17 16:05:31 -05003082 if (cmdiocbp) {
3083 if (cmdiocbp->iocb_cmpl) {
3084 /*
James Smartea2151b2008-09-07 11:52:10 -04003085 * If an ELS command failed send an event to mgmt
3086 * application.
3087 */
3088 if (saveq->iocb.ulpStatus &&
3089 (pring->ringno == LPFC_ELS_RING) &&
3090 (cmdiocbp->iocb.ulpCommand ==
3091 CMD_ELS_REQUEST64_CR))
3092 lpfc_send_els_failure_event(phba,
3093 cmdiocbp, saveq);
3094
3095 /*
dea31012005-04-17 16:05:31 -05003096 * Post all ELS completions to the worker thread.
3097 * All other are passed to the completion callback.
3098 */
3099 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05003100 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3101 (cmdiocbp->iocb_flag &
3102 LPFC_DRIVER_ABORTED)) {
3103 spin_lock_irqsave(&phba->hbalock,
3104 iflag);
James Smart07951072007-04-25 09:51:38 -04003105 cmdiocbp->iocb_flag &=
3106 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05003107 spin_unlock_irqrestore(&phba->hbalock,
3108 iflag);
James Smart07951072007-04-25 09:51:38 -04003109 saveq->iocb.ulpStatus =
3110 IOSTAT_LOCAL_REJECT;
3111 saveq->iocb.un.ulpWord[4] =
3112 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05003113
3114 /* Firmware could still be in progress
3115 * of DMAing payload, so don't free data
3116 * buffer till after a hbeat.
3117 */
James Smart341af102010-01-26 23:07:37 -05003118 spin_lock_irqsave(&phba->hbalock,
3119 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05003120 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05003121 spin_unlock_irqrestore(&phba->hbalock,
3122 iflag);
3123 }
James Smart0f65ff62010-02-26 14:14:23 -05003124 if (phba->sli_rev == LPFC_SLI_REV4) {
3125 if (saveq->iocb_flag &
3126 LPFC_EXCHANGE_BUSY) {
3127 /* Set cmdiocb flag for the
3128 * exchange busy so sgl (xri)
3129 * will not be released until
3130 * the abort xri is received
3131 * from hba.
3132 */
3133 spin_lock_irqsave(
3134 &phba->hbalock, iflag);
3135 cmdiocbp->iocb_flag |=
3136 LPFC_EXCHANGE_BUSY;
3137 spin_unlock_irqrestore(
3138 &phba->hbalock, iflag);
3139 }
3140 if (cmdiocbp->iocb_flag &
3141 LPFC_DRIVER_ABORTED) {
3142 /*
3143 * Clear LPFC_DRIVER_ABORTED
3144 * bit in case it was driver
3145 * initiated abort.
3146 */
3147 spin_lock_irqsave(
3148 &phba->hbalock, iflag);
3149 cmdiocbp->iocb_flag &=
3150 ~LPFC_DRIVER_ABORTED;
3151 spin_unlock_irqrestore(
3152 &phba->hbalock, iflag);
3153 cmdiocbp->iocb.ulpStatus =
3154 IOSTAT_LOCAL_REJECT;
3155 cmdiocbp->iocb.un.ulpWord[4] =
3156 IOERR_ABORT_REQUESTED;
3157 /*
3158 * For SLI4, irsiocb contains
3159 * NO_XRI in sli_xritag, it
3160 * shall not affect releasing
3161 * sgl (xri) process.
3162 */
3163 saveq->iocb.ulpStatus =
3164 IOSTAT_LOCAL_REJECT;
3165 saveq->iocb.un.ulpWord[4] =
3166 IOERR_SLI_ABORTED;
3167 spin_lock_irqsave(
3168 &phba->hbalock, iflag);
3169 saveq->iocb_flag |=
3170 LPFC_DELAY_MEM_FREE;
3171 spin_unlock_irqrestore(
3172 &phba->hbalock, iflag);
3173 }
James Smart07951072007-04-25 09:51:38 -04003174 }
dea31012005-04-17 16:05:31 -05003175 }
James Smart2e0fef82007-06-17 19:56:36 -05003176 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003177 } else
3178 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003179 } else {
3180 /*
3181 * Unknown initiating command based on the response iotag.
3182 * This could be the case on the ELS ring because of
3183 * lpfc_els_abort().
3184 */
3185 if (pring->ringno != LPFC_ELS_RING) {
3186 /*
3187 * Ring <ringno> handler: unexpected completion IoTag
3188 * <IoTag>
3189 */
James Smarta257bf92009-04-06 18:48:10 -04003190 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003191 "0322 Ring %d handler: "
3192 "unexpected completion IoTag x%x "
3193 "Data: x%x x%x x%x x%x\n",
3194 pring->ringno,
3195 saveq->iocb.ulpIoTag,
3196 saveq->iocb.ulpStatus,
3197 saveq->iocb.un.ulpWord[4],
3198 saveq->iocb.ulpCommand,
3199 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003200 }
3201 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003202
dea31012005-04-17 16:05:31 -05003203 return rc;
3204}
3205
James Smarte59058c2008-08-24 21:49:00 -04003206/**
James Smart3621a712009-04-06 18:47:14 -04003207 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003208 * @phba: Pointer to HBA context object.
3209 * @pring: Pointer to driver SLI ring object.
3210 *
3211 * This function is called from the iocb ring event handlers when
3212 * put pointer is ahead of the get pointer for a ring. This function signal
3213 * an error attention condition to the worker thread and the worker
3214 * thread will transition the HBA to offline state.
3215 **/
James Smart2e0fef82007-06-17 19:56:36 -05003216static void
3217lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003218{
James Smart34b02dc2008-08-24 21:49:55 -04003219 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003220 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003221 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003222 * rsp ring <portRspMax>
3223 */
3224 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003225 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003226 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003227 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003228 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003229
James Smart2e0fef82007-06-17 19:56:36 -05003230 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003231
3232 /*
3233 * All error attention handlers are posted to
3234 * worker thread
3235 */
3236 phba->work_ha |= HA_ERATT;
3237 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003238
James Smart5e9d9b82008-06-14 22:52:53 -04003239 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003240
3241 return;
3242}
3243
James Smarte59058c2008-08-24 21:49:00 -04003244/**
James Smart3621a712009-04-06 18:47:14 -04003245 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04003246 * @ptr: Pointer to address of HBA context object.
3247 *
3248 * This function is invoked by the Error Attention polling timer when the
3249 * timer times out. It will check the SLI Error Attention register for
3250 * possible attention events. If so, it will post an Error Attention event
3251 * and wake up worker thread to process it. Otherwise, it will set up the
3252 * Error Attention polling timer for the next poll.
3253 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003254void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003255{
3256 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003257 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003258 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003259
Kees Cookf22eb4d2017-09-06 20:24:26 -07003260 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003261
James Smartaa6fbb72012-08-03 12:36:03 -04003262 /* Here we will also keep track of interrupts per sec of the hba */
3263 sli_intr = phba->sli.slistat.sli_intr;
3264
3265 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3266 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3267 sli_intr);
3268 else
3269 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3270
James Smart65791f12016-07-06 12:35:56 -07003271 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3272 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003273 phba->sli.slistat.sli_ips = cnt;
3274
3275 phba->sli.slistat.sli_prev_intr = sli_intr;
3276
James Smart93996272008-08-24 21:50:30 -04003277 /* Check chip HA register for error event */
3278 eratt = lpfc_sli_check_eratt(phba);
3279
3280 if (eratt)
3281 /* Tell the worker thread there is work to do */
3282 lpfc_worker_wake_up(phba);
3283 else
3284 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003285 mod_timer(&phba->eratt_poll,
3286 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003287 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003288 return;
3289}
3290
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003291
James Smarte59058c2008-08-24 21:49:00 -04003292/**
James Smart3621a712009-04-06 18:47:14 -04003293 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003294 * @phba: Pointer to HBA context object.
3295 * @pring: Pointer to driver SLI ring object.
3296 * @mask: Host attention register mask for this ring.
3297 *
3298 * This function is called from the interrupt context when there is a ring
3299 * event for the fcp ring. The caller does not hold any lock.
3300 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003301 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003302 * LE bit set. The function will call the completion handler of the command iocb
3303 * if the response iocb indicates a completion for a command iocb or it is
3304 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3305 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003306 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003307 * to check it explicitly.
3308 */
3309int
James Smart2e0fef82007-06-17 19:56:36 -05003310lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3311 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003312{
James Smart34b02dc2008-08-24 21:49:55 -04003313 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003314 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003315 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003316 struct lpfc_iocbq *cmdiocbq = NULL;
3317 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003318 uint32_t status;
3319 uint32_t portRspPut, portRspMax;
3320 int rc = 1;
3321 lpfc_iocb_type type;
3322 unsigned long iflag;
3323 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003324
James Smart2e0fef82007-06-17 19:56:36 -05003325 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003326 pring->stats.iocb_event++;
3327
dea31012005-04-17 16:05:31 -05003328 /*
3329 * The next available response entry should never exceed the maximum
3330 * entries. If it does, treat it as an adapter hardware error.
3331 */
James Smart7e56aa22012-08-03 12:35:34 -04003332 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003333 portRspPut = le32_to_cpu(pgp->rspPutInx);
3334 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003335 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003336 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003337 return 1;
3338 }
James Smart45ed1192009-10-02 15:17:02 -04003339 if (phba->fcp_ring_in_use) {
3340 spin_unlock_irqrestore(&phba->hbalock, iflag);
3341 return 1;
3342 } else
3343 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003344
3345 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003346 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003347 /*
3348 * Fetch an entry off the ring and copy it into a local data
3349 * structure. The copy involves a byte-swap since the
3350 * network byte order and pci byte orders are different.
3351 */
James Smarted957682007-06-17 19:56:37 -05003352 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003353 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003354
James Smart7e56aa22012-08-03 12:35:34 -04003355 if (++pring->sli.sli3.rspidx >= portRspMax)
3356 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003357
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003358 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3359 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003360 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003361 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003362 irsp = &rspiocbq.iocb;
3363
dea31012005-04-17 16:05:31 -05003364 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3365 pring->stats.iocb_rsp++;
3366 rsp_cmpl++;
3367
3368 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003369 /*
3370 * If resource errors reported from HBA, reduce
3371 * queuedepths of the SCSI device.
3372 */
3373 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003374 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3375 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003376 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003377 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003378 spin_lock_irqsave(&phba->hbalock, iflag);
3379 }
3380
dea31012005-04-17 16:05:31 -05003381 /* Rsp ring <ringno> error: IOCB */
3382 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003383 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003384 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003385 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003386 irsp->un.ulpWord[0],
3387 irsp->un.ulpWord[1],
3388 irsp->un.ulpWord[2],
3389 irsp->un.ulpWord[3],
3390 irsp->un.ulpWord[4],
3391 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003392 *(uint32_t *)&irsp->un1,
3393 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003394 }
3395
3396 switch (type) {
3397 case LPFC_ABORT_IOCB:
3398 case LPFC_SOL_IOCB:
3399 /*
3400 * Idle exchange closed via ABTS from port. No iocb
3401 * resources need to be recovered.
3402 */
3403 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003404 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003405 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003406 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003407 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003408 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003409 break;
3410 }
3411
James Bottomley604a3e32005-10-29 10:28:33 -05003412 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3413 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003414 if (unlikely(!cmdiocbq))
3415 break;
3416 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3417 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3418 if (cmdiocbq->iocb_cmpl) {
3419 spin_unlock_irqrestore(&phba->hbalock, iflag);
3420 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3421 &rspiocbq);
3422 spin_lock_irqsave(&phba->hbalock, iflag);
3423 }
dea31012005-04-17 16:05:31 -05003424 break;
James Smarta4bc3372006-12-02 13:34:16 -05003425 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003426 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003427 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003428 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003429 break;
dea31012005-04-17 16:05:31 -05003430 default:
3431 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3432 char adaptermsg[LPFC_MAX_ADPTMSG];
3433 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3434 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3435 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003436 dev_warn(&((phba->pcidev)->dev),
3437 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003438 phba->brd_no, adaptermsg);
3439 } else {
3440 /* Unknown IOCB command */
3441 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003442 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003443 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003444 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003445 irsp->ulpStatus,
3446 irsp->ulpIoTag,
3447 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003448 }
3449 break;
3450 }
3451
3452 /*
3453 * The response IOCB has been processed. Update the ring
3454 * pointer in SLIM. If the port response put pointer has not
3455 * been updated, sync the pgp->rspPutInx and fetch the new port
3456 * response put pointer.
3457 */
James Smart7e56aa22012-08-03 12:35:34 -04003458 writel(pring->sli.sli3.rspidx,
3459 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003460
James Smart7e56aa22012-08-03 12:35:34 -04003461 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003462 portRspPut = le32_to_cpu(pgp->rspPutInx);
3463 }
3464
3465 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3466 pring->stats.iocb_rsp_full++;
3467 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3468 writel(status, phba->CAregaddr);
3469 readl(phba->CAregaddr);
3470 }
3471 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3472 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3473 pring->stats.iocb_cmd_empty++;
3474
3475 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003476 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003477 lpfc_sli_resume_iocb(phba, pring);
3478
3479 if ((pring->lpfc_sli_cmd_available))
3480 (pring->lpfc_sli_cmd_available) (phba, pring);
3481
3482 }
3483
James Smart45ed1192009-10-02 15:17:02 -04003484 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003485 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003486 return rc;
3487}
3488
James Smarte59058c2008-08-24 21:49:00 -04003489/**
James Smart3772a992009-05-22 14:50:54 -04003490 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3491 * @phba: Pointer to HBA context object.
3492 * @pring: Pointer to driver SLI ring object.
3493 * @rspiocbp: Pointer to driver response IOCB object.
3494 *
3495 * This function is called from the worker thread when there is a slow-path
3496 * response IOCB to process. This function chains all the response iocbs until
3497 * seeing the iocb with the LE bit set. The function will call
3498 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3499 * completion of a command iocb. The function will call the
3500 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3501 * The function frees the resources or calls the completion handler if this
3502 * iocb is an abort completion. The function returns NULL when the response
3503 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3504 * this function shall chain the iocb on to the iocb_continueq and return the
3505 * response iocb passed in.
3506 **/
3507static struct lpfc_iocbq *
3508lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3509 struct lpfc_iocbq *rspiocbp)
3510{
3511 struct lpfc_iocbq *saveq;
3512 struct lpfc_iocbq *cmdiocbp;
3513 struct lpfc_iocbq *next_iocb;
3514 IOCB_t *irsp = NULL;
3515 uint32_t free_saveq;
3516 uint8_t iocb_cmd_type;
3517 lpfc_iocb_type type;
3518 unsigned long iflag;
3519 int rc;
3520
3521 spin_lock_irqsave(&phba->hbalock, iflag);
3522 /* First add the response iocb to the countinueq list */
3523 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3524 pring->iocb_continueq_cnt++;
3525
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003526 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003527 irsp = &rspiocbp->iocb;
3528 if (irsp->ulpLe) {
3529 /*
3530 * By default, the driver expects to free all resources
3531 * associated with this iocb completion.
3532 */
3533 free_saveq = 1;
3534 saveq = list_get_first(&pring->iocb_continueq,
3535 struct lpfc_iocbq, list);
3536 irsp = &(saveq->iocb);
3537 list_del_init(&pring->iocb_continueq);
3538 pring->iocb_continueq_cnt = 0;
3539
3540 pring->stats.iocb_rsp++;
3541
3542 /*
3543 * If resource errors reported from HBA, reduce
3544 * queuedepths of the SCSI device.
3545 */
3546 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003547 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3548 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003549 spin_unlock_irqrestore(&phba->hbalock, iflag);
3550 phba->lpfc_rampdown_queue_depth(phba);
3551 spin_lock_irqsave(&phba->hbalock, iflag);
3552 }
3553
3554 if (irsp->ulpStatus) {
3555 /* Rsp ring <ringno> error: IOCB */
3556 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3557 "0328 Rsp Ring %d error: "
3558 "IOCB Data: "
3559 "x%x x%x x%x x%x "
3560 "x%x x%x x%x x%x "
3561 "x%x x%x x%x x%x "
3562 "x%x x%x x%x x%x\n",
3563 pring->ringno,
3564 irsp->un.ulpWord[0],
3565 irsp->un.ulpWord[1],
3566 irsp->un.ulpWord[2],
3567 irsp->un.ulpWord[3],
3568 irsp->un.ulpWord[4],
3569 irsp->un.ulpWord[5],
3570 *(((uint32_t *) irsp) + 6),
3571 *(((uint32_t *) irsp) + 7),
3572 *(((uint32_t *) irsp) + 8),
3573 *(((uint32_t *) irsp) + 9),
3574 *(((uint32_t *) irsp) + 10),
3575 *(((uint32_t *) irsp) + 11),
3576 *(((uint32_t *) irsp) + 12),
3577 *(((uint32_t *) irsp) + 13),
3578 *(((uint32_t *) irsp) + 14),
3579 *(((uint32_t *) irsp) + 15));
3580 }
3581
3582 /*
3583 * Fetch the IOCB command type and call the correct completion
3584 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3585 * get freed back to the lpfc_iocb_list by the discovery
3586 * kernel thread.
3587 */
3588 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3589 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3590 switch (type) {
3591 case LPFC_SOL_IOCB:
3592 spin_unlock_irqrestore(&phba->hbalock, iflag);
3593 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3594 spin_lock_irqsave(&phba->hbalock, iflag);
3595 break;
3596
3597 case LPFC_UNSOL_IOCB:
3598 spin_unlock_irqrestore(&phba->hbalock, iflag);
3599 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3600 spin_lock_irqsave(&phba->hbalock, iflag);
3601 if (!rc)
3602 free_saveq = 0;
3603 break;
3604
3605 case LPFC_ABORT_IOCB:
3606 cmdiocbp = NULL;
3607 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3608 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3609 saveq);
3610 if (cmdiocbp) {
3611 /* Call the specified completion routine */
3612 if (cmdiocbp->iocb_cmpl) {
3613 spin_unlock_irqrestore(&phba->hbalock,
3614 iflag);
3615 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3616 saveq);
3617 spin_lock_irqsave(&phba->hbalock,
3618 iflag);
3619 } else
3620 __lpfc_sli_release_iocbq(phba,
3621 cmdiocbp);
3622 }
3623 break;
3624
3625 case LPFC_UNKNOWN_IOCB:
3626 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3627 char adaptermsg[LPFC_MAX_ADPTMSG];
3628 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3629 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3630 MAX_MSG_DATA);
3631 dev_warn(&((phba->pcidev)->dev),
3632 "lpfc%d: %s\n",
3633 phba->brd_no, adaptermsg);
3634 } else {
3635 /* Unknown IOCB command */
3636 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3637 "0335 Unknown IOCB "
3638 "command Data: x%x "
3639 "x%x x%x x%x\n",
3640 irsp->ulpCommand,
3641 irsp->ulpStatus,
3642 irsp->ulpIoTag,
3643 irsp->ulpContext);
3644 }
3645 break;
3646 }
3647
3648 if (free_saveq) {
3649 list_for_each_entry_safe(rspiocbp, next_iocb,
3650 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003651 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003652 __lpfc_sli_release_iocbq(phba, rspiocbp);
3653 }
3654 __lpfc_sli_release_iocbq(phba, saveq);
3655 }
3656 rspiocbp = NULL;
3657 }
3658 spin_unlock_irqrestore(&phba->hbalock, iflag);
3659 return rspiocbp;
3660}
3661
3662/**
3663 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003664 * @phba: Pointer to HBA context object.
3665 * @pring: Pointer to driver SLI ring object.
3666 * @mask: Host attention register mask for this ring.
3667 *
James Smart3772a992009-05-22 14:50:54 -04003668 * This routine wraps the actual slow_ring event process routine from the
3669 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003670 **/
James Smart3772a992009-05-22 14:50:54 -04003671void
James Smart2e0fef82007-06-17 19:56:36 -05003672lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3673 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003674{
James Smart3772a992009-05-22 14:50:54 -04003675 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3676}
3677
3678/**
3679 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3680 * @phba: Pointer to HBA context object.
3681 * @pring: Pointer to driver SLI ring object.
3682 * @mask: Host attention register mask for this ring.
3683 *
3684 * This function is called from the worker thread when there is a ring event
3685 * for non-fcp rings. The caller does not hold any lock. The function will
3686 * remove each response iocb in the response ring and calls the handle
3687 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3688 **/
3689static void
3690lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3691 struct lpfc_sli_ring *pring, uint32_t mask)
3692{
James Smart34b02dc2008-08-24 21:49:55 -04003693 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003694 IOCB_t *entry;
3695 IOCB_t *irsp = NULL;
3696 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003697 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003698 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003699 uint32_t status;
dea31012005-04-17 16:05:31 -05003700
James Smart34b02dc2008-08-24 21:49:55 -04003701 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003702 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003703 pring->stats.iocb_event++;
3704
dea31012005-04-17 16:05:31 -05003705 /*
3706 * The next available response entry should never exceed the maximum
3707 * entries. If it does, treat it as an adapter hardware error.
3708 */
James Smart7e56aa22012-08-03 12:35:34 -04003709 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003710 portRspPut = le32_to_cpu(pgp->rspPutInx);
3711 if (portRspPut >= portRspMax) {
3712 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003713 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003714 * rsp ring <portRspMax>
3715 */
James Smarted957682007-06-17 19:56:37 -05003716 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003717 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003718 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003719 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003720
James Smart2e0fef82007-06-17 19:56:36 -05003721 phba->link_state = LPFC_HBA_ERROR;
3722 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003723
3724 phba->work_hs = HS_FFER3;
3725 lpfc_handle_eratt(phba);
3726
James Smart3772a992009-05-22 14:50:54 -04003727 return;
dea31012005-04-17 16:05:31 -05003728 }
3729
3730 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003731 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003732 /*
3733 * Build a completion list and call the appropriate handler.
3734 * The process is to get the next available response iocb, get
3735 * a free iocb from the list, copy the response data into the
3736 * free iocb, insert to the continuation list, and update the
3737 * next response index to slim. This process makes response
3738 * iocb's in the ring available to DMA as fast as possible but
3739 * pays a penalty for a copy operation. Since the iocb is
3740 * only 32 bytes, this penalty is considered small relative to
3741 * the PCI reads for register values and a slim write. When
3742 * the ulpLe field is set, the entire Command has been
3743 * received.
3744 */
James Smarted957682007-06-17 19:56:37 -05003745 entry = lpfc_resp_iocb(phba, pring);
3746
James Smart858c9f62007-06-17 19:56:39 -05003747 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003748 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003749 if (rspiocbp == NULL) {
3750 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003751 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003752 break;
3753 }
3754
James Smarted957682007-06-17 19:56:37 -05003755 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3756 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003757 irsp = &rspiocbp->iocb;
3758
James Smart7e56aa22012-08-03 12:35:34 -04003759 if (++pring->sli.sli3.rspidx >= portRspMax)
3760 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003761
James Smarta58cbd52007-08-02 11:09:43 -04003762 if (pring->ringno == LPFC_ELS_RING) {
3763 lpfc_debugfs_slow_ring_trc(phba,
3764 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3765 *(((uint32_t *) irsp) + 4),
3766 *(((uint32_t *) irsp) + 6),
3767 *(((uint32_t *) irsp) + 7));
3768 }
3769
James Smart7e56aa22012-08-03 12:35:34 -04003770 writel(pring->sli.sli3.rspidx,
3771 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003772
James Smart3772a992009-05-22 14:50:54 -04003773 spin_unlock_irqrestore(&phba->hbalock, iflag);
3774 /* Handle the response IOCB */
3775 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3776 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003777
3778 /*
3779 * If the port response put pointer has not been updated, sync
3780 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3781 * response put pointer.
3782 */
James Smart7e56aa22012-08-03 12:35:34 -04003783 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003784 portRspPut = le32_to_cpu(pgp->rspPutInx);
3785 }
James Smart7e56aa22012-08-03 12:35:34 -04003786 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003787
James Smart92d7f7b2007-06-17 19:56:38 -05003788 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003789 /* At least one response entry has been freed */
3790 pring->stats.iocb_rsp_full++;
3791 /* SET RxRE_RSP in Chip Att register */
3792 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3793 writel(status, phba->CAregaddr);
3794 readl(phba->CAregaddr); /* flush */
3795 }
3796 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3797 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3798 pring->stats.iocb_cmd_empty++;
3799
3800 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003801 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003802 lpfc_sli_resume_iocb(phba, pring);
3803
3804 if ((pring->lpfc_sli_cmd_available))
3805 (pring->lpfc_sli_cmd_available) (phba, pring);
3806
3807 }
3808
James Smart2e0fef82007-06-17 19:56:36 -05003809 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003810 return;
dea31012005-04-17 16:05:31 -05003811}
3812
James Smarte59058c2008-08-24 21:49:00 -04003813/**
James Smart4f774512009-05-22 14:52:35 -04003814 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3815 * @phba: Pointer to HBA context object.
3816 * @pring: Pointer to driver SLI ring object.
3817 * @mask: Host attention register mask for this ring.
3818 *
3819 * This function is called from the worker thread when there is a pending
3820 * ELS response iocb on the driver internal slow-path response iocb worker
3821 * queue. The caller does not hold any lock. The function will remove each
3822 * response iocb from the response worker queue and calls the handle
3823 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3824 **/
3825static void
3826lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3827 struct lpfc_sli_ring *pring, uint32_t mask)
3828{
3829 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003830 struct hbq_dmabuf *dmabuf;
3831 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003832 unsigned long iflag;
James Smart0ef01a22018-09-10 10:30:45 -07003833 int count = 0;
James Smart4f774512009-05-22 14:52:35 -04003834
James Smart45ed1192009-10-02 15:17:02 -04003835 spin_lock_irqsave(&phba->hbalock, iflag);
3836 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3837 spin_unlock_irqrestore(&phba->hbalock, iflag);
3838 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003839 /* Get the response iocb from the head of work queue */
3840 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003841 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003842 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003843 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003844
3845 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3846 case CQE_CODE_COMPL_WQE:
3847 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3848 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003849 /* Translate ELS WCQE to response IOCBQ */
3850 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3851 irspiocbq);
3852 if (irspiocbq)
3853 lpfc_sli_sp_handle_rspiocb(phba, pring,
3854 irspiocbq);
James Smart0ef01a22018-09-10 10:30:45 -07003855 count++;
James Smart4d9ab992009-10-02 15:16:39 -04003856 break;
3857 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003858 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003859 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3860 cq_event);
3861 lpfc_sli4_handle_received_buffer(phba, dmabuf);
James Smart0ef01a22018-09-10 10:30:45 -07003862 count++;
James Smart4d9ab992009-10-02 15:16:39 -04003863 break;
3864 default:
3865 break;
3866 }
James Smart0ef01a22018-09-10 10:30:45 -07003867
3868 /* Limit the number of events to 64 to avoid soft lockups */
3869 if (count == 64)
3870 break;
James Smart4f774512009-05-22 14:52:35 -04003871 }
3872}
3873
3874/**
James Smart3621a712009-04-06 18:47:14 -04003875 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003876 * @phba: Pointer to HBA context object.
3877 * @pring: Pointer to driver SLI ring object.
3878 *
3879 * This function aborts all iocbs in the given ring and frees all the iocb
3880 * objects in txq. This function issues an abort iocb for all the iocb commands
3881 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3882 * the return of this function. The caller is not required to hold any locks.
3883 **/
James Smart2e0fef82007-06-17 19:56:36 -05003884void
dea31012005-04-17 16:05:31 -05003885lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3886{
James Smart2534ba72007-04-25 09:52:20 -04003887 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003888 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003889
James Smart92d7f7b2007-06-17 19:56:38 -05003890 if (pring->ringno == LPFC_ELS_RING) {
3891 lpfc_fabric_abort_hba(phba);
3892 }
3893
dea31012005-04-17 16:05:31 -05003894 /* Error everything on txq and txcmplq
3895 * First do the txq.
3896 */
James Smartdb55fba2014-04-04 13:52:02 -04003897 if (phba->sli_rev >= LPFC_SLI_REV4) {
3898 spin_lock_irq(&pring->ring_lock);
3899 list_splice_init(&pring->txq, &completions);
3900 pring->txq_cnt = 0;
3901 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003902
James Smartdb55fba2014-04-04 13:52:02 -04003903 spin_lock_irq(&phba->hbalock);
3904 /* Next issue ABTS for everything on the txcmplq */
3905 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3906 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3907 spin_unlock_irq(&phba->hbalock);
3908 } else {
3909 spin_lock_irq(&phba->hbalock);
3910 list_splice_init(&pring->txq, &completions);
3911 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003912
James Smartdb55fba2014-04-04 13:52:02 -04003913 /* Next issue ABTS for everything on the txcmplq */
3914 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3915 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3916 spin_unlock_irq(&phba->hbalock);
3917 }
James Smart2534ba72007-04-25 09:52:20 -04003918
James Smarta257bf92009-04-06 18:48:10 -04003919 /* Cancel all the IOCBs from the completions list */
3920 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3921 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003922}
3923
James Smarte59058c2008-08-24 21:49:00 -04003924/**
James Smart895427b2017-02-12 13:52:30 -08003925 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3926 * @phba: Pointer to HBA context object.
3927 * @pring: Pointer to driver SLI ring object.
3928 *
3929 * This function aborts all iocbs in the given ring and frees all the iocb
3930 * objects in txq. This function issues an abort iocb for all the iocb commands
3931 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3932 * the return of this function. The caller is not required to hold any locks.
3933 **/
3934void
3935lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3936{
3937 LIST_HEAD(completions);
3938 struct lpfc_iocbq *iocb, *next_iocb;
3939
3940 if (pring->ringno == LPFC_ELS_RING)
3941 lpfc_fabric_abort_hba(phba);
3942
3943 spin_lock_irq(&phba->hbalock);
3944 /* Next issue ABTS for everything on the txcmplq */
3945 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3946 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3947 spin_unlock_irq(&phba->hbalock);
3948}
3949
3950
3951/**
James Smartdb55fba2014-04-04 13:52:02 -04003952 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3953 * @phba: Pointer to HBA context object.
3954 * @pring: Pointer to driver SLI ring object.
3955 *
3956 * This function aborts all iocbs in FCP rings and frees all the iocb
3957 * objects in txq. This function issues an abort iocb for all the iocb commands
3958 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3959 * the return of this function. The caller is not required to hold any locks.
3960 **/
3961void
3962lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3963{
3964 struct lpfc_sli *psli = &phba->sli;
3965 struct lpfc_sli_ring *pring;
3966 uint32_t i;
3967
3968 /* Look on all the FCP Rings for the iotag */
3969 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08003970 for (i = 0; i < phba->cfg_hdw_queue; i++) {
3971 pring = phba->sli4_hba.hdwq[i].fcp_wq->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003972 lpfc_sli_abort_iocb_ring(phba, pring);
3973 }
3974 } else {
James Smart895427b2017-02-12 13:52:30 -08003975 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003976 lpfc_sli_abort_iocb_ring(phba, pring);
3977 }
3978}
3979
James Smart895427b2017-02-12 13:52:30 -08003980/**
3981 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3982 * @phba: Pointer to HBA context object.
3983 *
3984 * This function aborts all wqes in NVME rings. This function issues an
3985 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3986 * the txcmplq is not guaranteed to complete before the return of this
3987 * function. The caller is not required to hold any locks.
3988 **/
3989void
3990lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3991{
3992 struct lpfc_sli_ring *pring;
3993 uint32_t i;
3994
James Smartcdb42be2019-01-28 11:14:21 -08003995 if ((phba->sli_rev < LPFC_SLI_REV4) ||
3996 !(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
James Smart895427b2017-02-12 13:52:30 -08003997 return;
3998
3999 /* Abort all IO on each NVME ring. */
James Smartcdb42be2019-01-28 11:14:21 -08004000 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4001 pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
James Smart895427b2017-02-12 13:52:30 -08004002 lpfc_sli_abort_wqe_ring(phba, pring);
4003 }
4004}
4005
James Smartdb55fba2014-04-04 13:52:02 -04004006
4007/**
James Smart3621a712009-04-06 18:47:14 -04004008 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04004009 * @phba: Pointer to HBA context object.
4010 *
4011 * This function flushes all iocbs in the fcp ring and frees all the iocb
4012 * objects in txq and txcmplq. This function will not issue abort iocbs
4013 * for all the iocb commands in txcmplq, they will just be returned with
4014 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4015 * slot has been permanently disabled.
4016 **/
4017void
4018lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
4019{
4020 LIST_HEAD(txq);
4021 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04004022 struct lpfc_sli *psli = &phba->sli;
4023 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04004024 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004025 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04004026
4027 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04004028 /* Indicate the I/O queues are flushed */
4029 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04004030 spin_unlock_irq(&phba->hbalock);
4031
James Smartdb55fba2014-04-04 13:52:02 -04004032 /* Look on all the FCP Rings for the iotag */
4033 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004034 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4035 pring = phba->sli4_hba.hdwq[i].fcp_wq->pring;
James Smarta8e497d2008-08-24 21:50:11 -04004036
James Smartdb55fba2014-04-04 13:52:02 -04004037 spin_lock_irq(&pring->ring_lock);
4038 /* Retrieve everything on txq */
4039 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004040 list_for_each_entry_safe(piocb, next_iocb,
4041 &pring->txcmplq, list)
4042 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004043 /* Retrieve everything on the txcmplq */
4044 list_splice_init(&pring->txcmplq, &txcmplq);
4045 pring->txq_cnt = 0;
4046 pring->txcmplq_cnt = 0;
4047 spin_unlock_irq(&pring->ring_lock);
4048
4049 /* Flush the txq */
4050 lpfc_sli_cancel_iocbs(phba, &txq,
4051 IOSTAT_LOCAL_REJECT,
4052 IOERR_SLI_DOWN);
4053 /* Flush the txcmpq */
4054 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4055 IOSTAT_LOCAL_REJECT,
4056 IOERR_SLI_DOWN);
4057 }
4058 } else {
James Smart895427b2017-02-12 13:52:30 -08004059 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004060
4061 spin_lock_irq(&phba->hbalock);
4062 /* Retrieve everything on txq */
4063 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004064 list_for_each_entry_safe(piocb, next_iocb,
4065 &pring->txcmplq, list)
4066 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004067 /* Retrieve everything on the txcmplq */
4068 list_splice_init(&pring->txcmplq, &txcmplq);
4069 pring->txq_cnt = 0;
4070 pring->txcmplq_cnt = 0;
4071 spin_unlock_irq(&phba->hbalock);
4072
4073 /* Flush the txq */
4074 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4075 IOERR_SLI_DOWN);
4076 /* Flush the txcmpq */
4077 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4078 IOERR_SLI_DOWN);
4079 }
James Smarta8e497d2008-08-24 21:50:11 -04004080}
4081
4082/**
James Smart895427b2017-02-12 13:52:30 -08004083 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
4084 * @phba: Pointer to HBA context object.
4085 *
4086 * This function flushes all wqes in the nvme rings and frees all resources
4087 * in the txcmplq. This function does not issue abort wqes for the IO
4088 * commands in txcmplq, they will just be returned with
4089 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4090 * slot has been permanently disabled.
4091 **/
4092void
4093lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
4094{
4095 LIST_HEAD(txcmplq);
4096 struct lpfc_sli_ring *pring;
4097 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004098 struct lpfc_iocbq *piocb, *next_iocb;
James Smart895427b2017-02-12 13:52:30 -08004099
James Smartcdb42be2019-01-28 11:14:21 -08004100 if ((phba->sli_rev < LPFC_SLI_REV4) ||
4101 !(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
James Smart895427b2017-02-12 13:52:30 -08004102 return;
4103
4104 /* Hint to other driver operations that a flush is in progress. */
4105 spin_lock_irq(&phba->hbalock);
4106 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
4107 spin_unlock_irq(&phba->hbalock);
4108
4109 /* Cycle through all NVME rings and complete each IO with
4110 * a local driver reason code. This is a flush so no
4111 * abort exchange to FW.
4112 */
James Smartcdb42be2019-01-28 11:14:21 -08004113 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4114 pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
James Smart895427b2017-02-12 13:52:30 -08004115
James Smart895427b2017-02-12 13:52:30 -08004116 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -08004117 list_for_each_entry_safe(piocb, next_iocb,
4118 &pring->txcmplq, list)
4119 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4120 /* Retrieve everything on the txcmplq */
James Smart895427b2017-02-12 13:52:30 -08004121 list_splice_init(&pring->txcmplq, &txcmplq);
4122 pring->txcmplq_cnt = 0;
4123 spin_unlock_irq(&pring->ring_lock);
4124
4125 /* Flush the txcmpq &&&PAE */
4126 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4127 IOSTAT_LOCAL_REJECT,
4128 IOERR_SLI_DOWN);
4129 }
4130}
4131
4132/**
James Smart3772a992009-05-22 14:50:54 -04004133 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004134 * @phba: Pointer to HBA context object.
4135 * @mask: Bit mask to be checked.
4136 *
4137 * This function reads the host status register and compares
4138 * with the provided bit mask to check if HBA completed
4139 * the restart. This function will wait in a loop for the
4140 * HBA to complete restart. If the HBA does not restart within
4141 * 15 iterations, the function will reset the HBA again. The
4142 * function returns 1 when HBA fail to restart otherwise returns
4143 * zero.
4144 **/
James Smart3772a992009-05-22 14:50:54 -04004145static int
4146lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004147{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004148 uint32_t status;
4149 int i = 0;
4150 int retval = 0;
dea31012005-04-17 16:05:31 -05004151
Jamie Wellnitz41415862006-02-28 19:25:27 -05004152 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004153 if (lpfc_readl(phba->HSregaddr, &status))
4154 return 1;
dea31012005-04-17 16:05:31 -05004155
Jamie Wellnitz41415862006-02-28 19:25:27 -05004156 /*
4157 * Check status register every 100ms for 5 retries, then every
4158 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4159 * every 2.5 sec for 4.
4160 * Break our of the loop if errors occurred during init.
4161 */
4162 while (((status & mask) != mask) &&
4163 !(status & HS_FFERM) &&
4164 i++ < 20) {
dea31012005-04-17 16:05:31 -05004165
Jamie Wellnitz41415862006-02-28 19:25:27 -05004166 if (i <= 5)
4167 msleep(10);
4168 else if (i <= 10)
4169 msleep(500);
4170 else
4171 msleep(2500);
dea31012005-04-17 16:05:31 -05004172
Jamie Wellnitz41415862006-02-28 19:25:27 -05004173 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004174 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004175 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004176 lpfc_sli_brdrestart(phba);
4177 }
4178 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004179 if (lpfc_readl(phba->HSregaddr, &status)) {
4180 retval = 1;
4181 break;
4182 }
dea31012005-04-17 16:05:31 -05004183 }
dea31012005-04-17 16:05:31 -05004184
Jamie Wellnitz41415862006-02-28 19:25:27 -05004185 /* Check to see if any errors occurred during init */
4186 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05004187 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4188 "2751 Adapter failed to restart, "
4189 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4190 status,
4191 readl(phba->MBslimaddr + 0xa8),
4192 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004193 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004194 retval = 1;
4195 }
dea31012005-04-17 16:05:31 -05004196
Jamie Wellnitz41415862006-02-28 19:25:27 -05004197 return retval;
dea31012005-04-17 16:05:31 -05004198}
4199
James Smartda0436e2009-05-22 14:51:39 -04004200/**
4201 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4202 * @phba: Pointer to HBA context object.
4203 * @mask: Bit mask to be checked.
4204 *
4205 * This function checks the host status register to check if HBA is
4206 * ready. This function will wait in a loop for the HBA to be ready
4207 * If the HBA is not ready , the function will will reset the HBA PCI
4208 * function again. The function returns 1 when HBA fail to be ready
4209 * otherwise returns zero.
4210 **/
4211static int
4212lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4213{
4214 uint32_t status;
4215 int retval = 0;
4216
4217 /* Read the HBA Host Status Register */
4218 status = lpfc_sli4_post_status_check(phba);
4219
4220 if (status) {
4221 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4222 lpfc_sli_brdrestart(phba);
4223 status = lpfc_sli4_post_status_check(phba);
4224 }
4225
4226 /* Check to see if any errors occurred during init */
4227 if (status) {
4228 phba->link_state = LPFC_HBA_ERROR;
4229 retval = 1;
4230 } else
4231 phba->sli4_hba.intr_enable = 0;
4232
4233 return retval;
4234}
4235
4236/**
4237 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4238 * @phba: Pointer to HBA context object.
4239 * @mask: Bit mask to be checked.
4240 *
4241 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4242 * from the API jump table function pointer from the lpfc_hba struct.
4243 **/
4244int
4245lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4246{
4247 return phba->lpfc_sli_brdready(phba, mask);
4248}
4249
James Smart92908312006-03-07 15:04:13 -05004250#define BARRIER_TEST_PATTERN (0xdeadbeef)
4251
James Smarte59058c2008-08-24 21:49:00 -04004252/**
James Smart3621a712009-04-06 18:47:14 -04004253 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004254 * @phba: Pointer to HBA context object.
4255 *
James Smart1b511972011-12-13 13:23:09 -05004256 * This function is called before resetting an HBA. This function is called
4257 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004258 **/
James Smart2e0fef82007-06-17 19:56:36 -05004259void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004260{
James Smart65a29c12006-07-06 15:50:50 -04004261 uint32_t __iomem *resp_buf;
4262 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004263 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004264 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004265 int i;
4266 uint8_t hdrtype;
4267
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004268 lockdep_assert_held(&phba->hbalock);
4269
James Smart92908312006-03-07 15:04:13 -05004270 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4271 if (hdrtype != 0x80 ||
4272 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4273 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4274 return;
4275
4276 /*
4277 * Tell the other part of the chip to suspend temporarily all
4278 * its DMA activity.
4279 */
James Smart65a29c12006-07-06 15:50:50 -04004280 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004281
4282 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004283 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4284 return;
James Smart92908312006-03-07 15:04:13 -05004285 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4286 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004287 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004288
James Smart9940b972011-03-11 16:06:12 -05004289 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4290 return;
4291 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004292 /* Clear Chip error bit */
4293 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004294 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004295 }
4296
4297 mbox = 0;
4298 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4299 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4300
4301 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004302 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004303 writel(mbox, mbox_buf);
4304
James Smart9940b972011-03-11 16:06:12 -05004305 for (i = 0; i < 50; i++) {
4306 if (lpfc_readl((resp_buf + 1), &resp_data))
4307 return;
4308 if (resp_data != ~(BARRIER_TEST_PATTERN))
4309 mdelay(1);
4310 else
4311 break;
4312 }
4313 resp_data = 0;
4314 if (lpfc_readl((resp_buf + 1), &resp_data))
4315 return;
4316 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004317 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004318 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004319 goto restore_hc;
4320 else
4321 goto clear_errat;
4322 }
4323
4324 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004325 resp_data = 0;
4326 for (i = 0; i < 500; i++) {
4327 if (lpfc_readl(resp_buf, &resp_data))
4328 return;
4329 if (resp_data != mbox)
4330 mdelay(1);
4331 else
4332 break;
4333 }
James Smart92908312006-03-07 15:04:13 -05004334
4335clear_errat:
4336
James Smart9940b972011-03-11 16:06:12 -05004337 while (++i < 500) {
4338 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4339 return;
4340 if (!(ha_copy & HA_ERATT))
4341 mdelay(1);
4342 else
4343 break;
4344 }
James Smart92908312006-03-07 15:04:13 -05004345
4346 if (readl(phba->HAregaddr) & HA_ERATT) {
4347 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004348 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004349 }
4350
4351restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004352 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004353 writel(hc_copy, phba->HCregaddr);
4354 readl(phba->HCregaddr); /* flush */
4355}
4356
James Smarte59058c2008-08-24 21:49:00 -04004357/**
James Smart3621a712009-04-06 18:47:14 -04004358 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004359 * @phba: Pointer to HBA context object.
4360 *
4361 * This function issues a kill_board mailbox command and waits for
4362 * the error attention interrupt. This function is called for stopping
4363 * the firmware processing. The caller is not required to hold any
4364 * locks. This function calls lpfc_hba_down_post function to free
4365 * any pending commands after the kill. The function will return 1 when it
4366 * fails to kill the board else will return 0.
4367 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004368int
James Smart2e0fef82007-06-17 19:56:36 -05004369lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004370{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004371 struct lpfc_sli *psli;
4372 LPFC_MBOXQ_t *pmb;
4373 uint32_t status;
4374 uint32_t ha_copy;
4375 int retval;
4376 int i = 0;
4377
4378 psli = &phba->sli;
4379
4380 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004381 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004382 "0329 Kill HBA Data: x%x x%x\n",
4383 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004384
James Smart98c9ea52007-10-27 13:37:33 -04004385 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4386 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004387 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004388
4389 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004390 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004391 if (lpfc_readl(phba->HCregaddr, &status)) {
4392 spin_unlock_irq(&phba->hbalock);
4393 mempool_free(pmb, phba->mbox_mem_pool);
4394 return 1;
4395 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004396 status &= ~HC_ERINT_ENA;
4397 writel(status, phba->HCregaddr);
4398 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004399 phba->link_flag |= LS_IGNORE_ERATT;
4400 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004401
4402 lpfc_kill_board(phba, pmb);
4403 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4404 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4405
4406 if (retval != MBX_SUCCESS) {
4407 if (retval != MBX_BUSY)
4408 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004409 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4410 "2752 KILL_BOARD command failed retval %d\n",
4411 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004412 spin_lock_irq(&phba->hbalock);
4413 phba->link_flag &= ~LS_IGNORE_ERATT;
4414 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004415 return 1;
4416 }
4417
James Smartf4b4c682009-05-22 14:53:12 -04004418 spin_lock_irq(&phba->hbalock);
4419 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4420 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004421
Jamie Wellnitz41415862006-02-28 19:25:27 -05004422 mempool_free(pmb, phba->mbox_mem_pool);
4423
4424 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4425 * attention every 100ms for 3 seconds. If we don't get ERATT after
4426 * 3 seconds we still set HBA_ERROR state because the status of the
4427 * board is now undefined.
4428 */
James Smart9940b972011-03-11 16:06:12 -05004429 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4430 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004431 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4432 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004433 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4434 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004435 }
4436
4437 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004438 if (ha_copy & HA_ERATT) {
4439 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004440 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004441 }
James Smart2e0fef82007-06-17 19:56:36 -05004442 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004443 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004444 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004445 phba->link_flag &= ~LS_IGNORE_ERATT;
4446 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004447
Jamie Wellnitz41415862006-02-28 19:25:27 -05004448 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004449 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004450
James Smart2e0fef82007-06-17 19:56:36 -05004451 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004452}
4453
James Smarte59058c2008-08-24 21:49:00 -04004454/**
James Smart3772a992009-05-22 14:50:54 -04004455 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004456 * @phba: Pointer to HBA context object.
4457 *
4458 * This function resets the HBA by writing HC_INITFF to the control
4459 * register. After the HBA resets, this function resets all the iocb ring
4460 * indices. This function disables PCI layer parity checking during
4461 * the reset.
4462 * This function returns 0 always.
4463 * The caller is not required to hold any locks.
4464 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004465int
James Smart2e0fef82007-06-17 19:56:36 -05004466lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004467{
4468 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004469 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004470 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004471 int i;
dea31012005-04-17 16:05:31 -05004472
Jamie Wellnitz41415862006-02-28 19:25:27 -05004473 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004474
Jamie Wellnitz41415862006-02-28 19:25:27 -05004475 /* Reset HBA */
4476 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004477 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004478 (phba->pport) ? phba->pport->port_state : 0,
4479 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004480
4481 /* perform board reset */
4482 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004483 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004484 if (phba->pport) {
4485 phba->pport->fc_myDID = 0;
4486 phba->pport->fc_prevDID = 0;
4487 }
dea31012005-04-17 16:05:31 -05004488
Jamie Wellnitz41415862006-02-28 19:25:27 -05004489 /* Turn off parity checking and serr during the physical reset */
4490 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4491 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4492 (cfg_value &
4493 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4494
James Smart3772a992009-05-22 14:50:54 -04004495 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4496
Jamie Wellnitz41415862006-02-28 19:25:27 -05004497 /* Now toggle INITFF bit in the Host Control Register */
4498 writel(HC_INITFF, phba->HCregaddr);
4499 mdelay(1);
4500 readl(phba->HCregaddr); /* flush */
4501 writel(0, phba->HCregaddr);
4502 readl(phba->HCregaddr); /* flush */
4503
4504 /* Restore PCI cmd register */
4505 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004506
4507 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004508 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004509 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004510 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004511 pring->sli.sli3.rspidx = 0;
4512 pring->sli.sli3.next_cmdidx = 0;
4513 pring->sli.sli3.local_getidx = 0;
4514 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004515 pring->missbufcnt = 0;
4516 }
dea31012005-04-17 16:05:31 -05004517
James Smart2e0fef82007-06-17 19:56:36 -05004518 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004519 return 0;
4520}
4521
James Smarte59058c2008-08-24 21:49:00 -04004522/**
James Smartda0436e2009-05-22 14:51:39 -04004523 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4524 * @phba: Pointer to HBA context object.
4525 *
4526 * This function resets a SLI4 HBA. This function disables PCI layer parity
4527 * checking during resets the device. The caller is not required to hold
4528 * any locks.
4529 *
4530 * This function returns 0 always.
4531 **/
4532int
4533lpfc_sli4_brdreset(struct lpfc_hba *phba)
4534{
4535 struct lpfc_sli *psli = &phba->sli;
4536 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004537 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004538
4539 /* Reset HBA */
4540 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004541 "0295 Reset HBA Data: x%x x%x x%x\n",
4542 phba->pport->port_state, psli->sli_flag,
4543 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004544
4545 /* perform board reset */
4546 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004547 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004548 phba->pport->fc_myDID = 0;
4549 phba->pport->fc_prevDID = 0;
4550
James Smartda0436e2009-05-22 14:51:39 -04004551 spin_lock_irq(&phba->hbalock);
4552 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4553 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004554 spin_unlock_irq(&phba->hbalock);
4555
James Smart02936352014-04-04 13:52:12 -04004556 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4557 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4558 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4559 return rc;
4560 }
4561
James Smartda0436e2009-05-22 14:51:39 -04004562 /* Now physically reset the device */
4563 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4564 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004565
4566 /* Turn off parity checking and serr during the physical reset */
4567 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4568 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4569 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4570
James Smart88318812012-09-29 11:29:29 -04004571 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004572 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004573
James Smartbe858b62010-12-15 17:57:20 -05004574 /* Restore PCI cmd register */
4575 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4576
James Smart27b01b82012-05-09 21:19:44 -04004577 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004578}
4579
4580/**
4581 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004582 * @phba: Pointer to HBA context object.
4583 *
4584 * This function is called in the SLI initialization code path to
4585 * restart the HBA. The caller is not required to hold any lock.
4586 * This function writes MBX_RESTART mailbox command to the SLIM and
4587 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4588 * function to free any pending commands. The function enables
4589 * POST only during the first initialization. The function returns zero.
4590 * The function does not guarantee completion of MBX_RESTART mailbox
4591 * command before the return of this function.
4592 **/
James Smartda0436e2009-05-22 14:51:39 -04004593static int
4594lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004595{
4596 MAILBOX_t *mb;
4597 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004598 volatile uint32_t word0;
4599 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004600 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004601
James Smart2e0fef82007-06-17 19:56:36 -05004602 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004603
James Smart0d878412009-10-02 15:16:56 -04004604 /* Take PCIe device Advanced Error Reporting (AER) state */
4605 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4606
Jamie Wellnitz41415862006-02-28 19:25:27 -05004607 psli = &phba->sli;
4608
4609 /* Restart HBA */
4610 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004611 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004612 (phba->pport) ? phba->pport->port_state : 0,
4613 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004614
4615 word0 = 0;
4616 mb = (MAILBOX_t *) &word0;
4617 mb->mbxCommand = MBX_RESTART;
4618 mb->mbxHc = 1;
4619
James Smart92908312006-03-07 15:04:13 -05004620 lpfc_reset_barrier(phba);
4621
Jamie Wellnitz41415862006-02-28 19:25:27 -05004622 to_slim = phba->MBslimaddr;
4623 writel(*(uint32_t *) mb, to_slim);
4624 readl(to_slim); /* flush */
4625
4626 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004627 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004628 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004629 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004630 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004631 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004632 writel(*(uint32_t *) mb, to_slim);
4633 readl(to_slim); /* flush */
4634
4635 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004636 if (phba->pport)
4637 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004638 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004639 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004640 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004641
James Smart64ba8812006-08-02 15:24:34 -04004642 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004643 psli->stats_start = ktime_get_seconds();
James Smart64ba8812006-08-02 15:24:34 -04004644
James Smarteaf15d52008-12-04 22:39:29 -05004645 /* Give the INITFF and Post time to settle. */
4646 mdelay(100);
dea31012005-04-17 16:05:31 -05004647
James Smart0d878412009-10-02 15:16:56 -04004648 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4649 if (hba_aer_enabled)
4650 pci_disable_pcie_error_reporting(phba->pcidev);
4651
Jamie Wellnitz41415862006-02-28 19:25:27 -05004652 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004653
4654 return 0;
4655}
4656
James Smarte59058c2008-08-24 21:49:00 -04004657/**
James Smartda0436e2009-05-22 14:51:39 -04004658 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4659 * @phba: Pointer to HBA context object.
4660 *
4661 * This function is called in the SLI initialization code path to restart
4662 * a SLI4 HBA. The caller is not required to hold any lock.
4663 * At the end of the function, it calls lpfc_hba_down_post function to
4664 * free any pending commands.
4665 **/
4666static int
4667lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4668{
4669 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004670 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004671 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004672
4673 /* Restart HBA */
4674 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4675 "0296 Restart HBA Data: x%x x%x\n",
4676 phba->pport->port_state, psli->sli_flag);
4677
James Smart75baf692010-06-08 18:31:21 -04004678 /* Take PCIe device Advanced Error Reporting (AER) state */
4679 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4680
James Smart27b01b82012-05-09 21:19:44 -04004681 rc = lpfc_sli4_brdreset(phba);
James Smart5a9eeff2018-11-29 16:09:32 -08004682 if (rc)
4683 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004684
4685 spin_lock_irq(&phba->hbalock);
4686 phba->pport->stopped = 0;
4687 phba->link_state = LPFC_INIT_START;
4688 phba->hba_flag = 0;
4689 spin_unlock_irq(&phba->hbalock);
4690
4691 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004692 psli->stats_start = ktime_get_seconds();
James Smartda0436e2009-05-22 14:51:39 -04004693
James Smart75baf692010-06-08 18:31:21 -04004694 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4695 if (hba_aer_enabled)
4696 pci_disable_pcie_error_reporting(phba->pcidev);
4697
James Smartda0436e2009-05-22 14:51:39 -04004698 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004699 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004700
James Smart27b01b82012-05-09 21:19:44 -04004701 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004702}
4703
4704/**
4705 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4706 * @phba: Pointer to HBA context object.
4707 *
4708 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4709 * API jump table function pointer from the lpfc_hba struct.
4710**/
4711int
4712lpfc_sli_brdrestart(struct lpfc_hba *phba)
4713{
4714 return phba->lpfc_sli_brdrestart(phba);
4715}
4716
4717/**
James Smart3621a712009-04-06 18:47:14 -04004718 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004719 * @phba: Pointer to HBA context object.
4720 *
4721 * This function is called after a HBA restart to wait for successful
4722 * restart of the HBA. Successful restart of the HBA is indicated by
4723 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4724 * iteration, the function will restart the HBA again. The function returns
4725 * zero if HBA successfully restarted else returns negative error code.
4726 **/
James Smart4492b732017-04-27 15:08:26 -07004727int
dea31012005-04-17 16:05:31 -05004728lpfc_sli_chipset_init(struct lpfc_hba *phba)
4729{
4730 uint32_t status, i = 0;
4731
4732 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004733 if (lpfc_readl(phba->HSregaddr, &status))
4734 return -EIO;
dea31012005-04-17 16:05:31 -05004735
4736 /* Check status register to see what current state is */
4737 i = 0;
4738 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4739
James Smartdcf2a4e2010-09-29 11:18:53 -04004740 /* Check every 10ms for 10 retries, then every 100ms for 90
4741 * retries, then every 1 sec for 50 retires for a total of
4742 * ~60 seconds before reset the board again and check every
4743 * 1 sec for 50 retries. The up to 60 seconds before the
4744 * board ready is required by the Falcon FIPS zeroization
4745 * complete, and any reset the board in between shall cause
4746 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004747 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004748 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004749 /* Adapter failed to init, timeout, status reg
4750 <status> */
James Smarted957682007-06-17 19:56:37 -05004751 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004752 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004753 "timeout, status reg x%x, "
4754 "FW Data: A8 x%x AC x%x\n", status,
4755 readl(phba->MBslimaddr + 0xa8),
4756 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004757 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004758 return -ETIMEDOUT;
4759 }
4760
4761 /* Check to see if any errors occurred during init */
4762 if (status & HS_FFERM) {
4763 /* ERROR: During chipset initialization */
4764 /* Adapter failed to init, chipset, status reg
4765 <status> */
James Smarted957682007-06-17 19:56:37 -05004766 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004767 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004768 "chipset, status reg x%x, "
4769 "FW Data: A8 x%x AC x%x\n", status,
4770 readl(phba->MBslimaddr + 0xa8),
4771 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004772 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004773 return -EIO;
4774 }
4775
James Smartdcf2a4e2010-09-29 11:18:53 -04004776 if (i <= 10)
dea31012005-04-17 16:05:31 -05004777 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004778 else if (i <= 100)
4779 msleep(100);
4780 else
4781 msleep(1000);
dea31012005-04-17 16:05:31 -05004782
James Smartdcf2a4e2010-09-29 11:18:53 -04004783 if (i == 150) {
4784 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004785 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004786 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004787 }
4788 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004789 if (lpfc_readl(phba->HSregaddr, &status))
4790 return -EIO;
dea31012005-04-17 16:05:31 -05004791 }
4792
4793 /* Check to see if any errors occurred during init */
4794 if (status & HS_FFERM) {
4795 /* ERROR: During chipset initialization */
4796 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004797 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004798 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004799 "status reg x%x, "
4800 "FW Data: A8 x%x AC x%x\n", status,
4801 readl(phba->MBslimaddr + 0xa8),
4802 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004803 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004804 return -EIO;
4805 }
4806
4807 /* Clear all interrupt enable conditions */
4808 writel(0, phba->HCregaddr);
4809 readl(phba->HCregaddr); /* flush */
4810
4811 /* setup host attn register */
4812 writel(0xffffffff, phba->HAregaddr);
4813 readl(phba->HAregaddr); /* flush */
4814 return 0;
4815}
4816
James Smarte59058c2008-08-24 21:49:00 -04004817/**
James Smart3621a712009-04-06 18:47:14 -04004818 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004819 *
4820 * This function calculates and returns the number of HBQs required to be
4821 * configured.
4822 **/
James Smart78b2d852007-08-02 11:10:21 -04004823int
James Smarted957682007-06-17 19:56:37 -05004824lpfc_sli_hbq_count(void)
4825{
James Smart92d7f7b2007-06-17 19:56:38 -05004826 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004827}
4828
James Smarte59058c2008-08-24 21:49:00 -04004829/**
James Smart3621a712009-04-06 18:47:14 -04004830 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004831 *
4832 * This function adds the number of hbq entries in every HBQ to get
4833 * the total number of hbq entries required for the HBA and returns
4834 * the total count.
4835 **/
James Smarted957682007-06-17 19:56:37 -05004836static int
4837lpfc_sli_hbq_entry_count(void)
4838{
4839 int hbq_count = lpfc_sli_hbq_count();
4840 int count = 0;
4841 int i;
4842
4843 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004844 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004845 return count;
4846}
4847
James Smarte59058c2008-08-24 21:49:00 -04004848/**
James Smart3621a712009-04-06 18:47:14 -04004849 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004850 *
4851 * This function calculates amount of memory required for all hbq entries
4852 * to be configured and returns the total memory required.
4853 **/
dea31012005-04-17 16:05:31 -05004854int
James Smarted957682007-06-17 19:56:37 -05004855lpfc_sli_hbq_size(void)
4856{
4857 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4858}
4859
James Smarte59058c2008-08-24 21:49:00 -04004860/**
James Smart3621a712009-04-06 18:47:14 -04004861 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004862 * @phba: Pointer to HBA context object.
4863 *
4864 * This function is called during the SLI initialization to configure
4865 * all the HBQs and post buffers to the HBQ. The caller is not
4866 * required to hold any locks. This function will return zero if successful
4867 * else it will return negative error code.
4868 **/
James Smarted957682007-06-17 19:56:37 -05004869static int
4870lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4871{
4872 int hbq_count = lpfc_sli_hbq_count();
4873 LPFC_MBOXQ_t *pmb;
4874 MAILBOX_t *pmbox;
4875 uint32_t hbqno;
4876 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004877
James Smart92d7f7b2007-06-17 19:56:38 -05004878 /* Get a Mailbox buffer to setup mailbox
4879 * commands for HBA initialization
4880 */
James Smarted957682007-06-17 19:56:37 -05004881 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4882
4883 if (!pmb)
4884 return -ENOMEM;
4885
James Smart04c68492009-05-22 14:52:52 -04004886 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004887
4888 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4889 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004890 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004891
4892 hbq_entry_index = 0;
4893 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4894 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4895 phba->hbqs[hbqno].hbqPutIdx = 0;
4896 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4897 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004898 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004899 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4900 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004901 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4902
4903 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4904 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4905 mbxStatus <status>, ring <num> */
4906
4907 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004908 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004909 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004910 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004911 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004912 pmbox->mbxStatus, hbqno);
4913
4914 phba->link_state = LPFC_HBA_ERROR;
4915 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004916 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004917 }
4918 }
4919 phba->hbq_count = hbq_count;
4920
James Smarted957682007-06-17 19:56:37 -05004921 mempool_free(pmb, phba->mbox_mem_pool);
4922
James Smart92d7f7b2007-06-17 19:56:38 -05004923 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004924 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4925 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004926 return 0;
4927}
4928
James Smarte59058c2008-08-24 21:49:00 -04004929/**
James Smart4f774512009-05-22 14:52:35 -04004930 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4931 * @phba: Pointer to HBA context object.
4932 *
4933 * This function is called during the SLI initialization to configure
4934 * all the HBQs and post buffers to the HBQ. The caller is not
4935 * required to hold any locks. This function will return zero if successful
4936 * else it will return negative error code.
4937 **/
4938static int
4939lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4940{
4941 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004942 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4943 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004944 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004945 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004946 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004947 return 0;
4948}
4949
4950/**
James Smart3621a712009-04-06 18:47:14 -04004951 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004952 * @phba: Pointer to HBA context object.
4953 * @sli_mode: sli mode - 2/3
4954 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004955 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004956 * to issue config_port mailbox command. This function restarts the
4957 * HBA firmware and issues a config_port mailbox command to configure
4958 * the SLI interface in the sli mode specified by sli_mode
4959 * variable. The caller is not required to hold any locks.
4960 * The function returns 0 if successful, else returns negative error
4961 * code.
4962 **/
James Smart93996272008-08-24 21:50:30 -04004963int
4964lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004965{
4966 LPFC_MBOXQ_t *pmb;
4967 uint32_t resetcount = 0, rc = 0, done = 0;
4968
4969 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4970 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004971 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004972 return -ENOMEM;
4973 }
4974
James Smarted957682007-06-17 19:56:37 -05004975 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004976 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004977 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004978 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004979 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004980 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004981 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004982 rc = lpfc_sli_chipset_init(phba);
4983 if (rc)
4984 break;
4985
James Smart2e0fef82007-06-17 19:56:36 -05004986 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004987 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004988 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004989 resetcount++;
4990
James Smarted957682007-06-17 19:56:37 -05004991 /* Call pre CONFIG_PORT mailbox command initialization. A
4992 * value of 0 means the call was successful. Any other
4993 * nonzero value is a failure, but if ERESTART is returned,
4994 * the driver may reset the HBA and try again.
4995 */
dea31012005-04-17 16:05:31 -05004996 rc = lpfc_config_port_prep(phba);
4997 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004998 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004999 continue;
James Smart34b02dc2008-08-24 21:49:55 -04005000 } else if (rc)
dea31012005-04-17 16:05:31 -05005001 break;
James Smart6d368e52011-05-24 11:44:12 -04005002
James Smart2e0fef82007-06-17 19:56:36 -05005003 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05005004 lpfc_config_port(phba, pmb);
5005 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04005006 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5007 LPFC_SLI3_HBQ_ENABLED |
5008 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04005009 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05005010 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05005011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005012 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05005013 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04005014 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005015 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04005016 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005017 spin_unlock_irq(&phba->hbalock);
5018 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04005019 } else {
5020 /* Allow asynchronous mailbox command to go through */
5021 spin_lock_irq(&phba->hbalock);
5022 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5023 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05005024 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05005025
5026 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5027 (pmb->u.mb.un.varCfgPort.gasabt == 0))
5028 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5029 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04005030 }
dea31012005-04-17 16:05:31 -05005031 }
James Smarted957682007-06-17 19:56:37 -05005032 if (!done) {
5033 rc = -EINVAL;
5034 goto do_prep_failed;
5035 }
James Smart04c68492009-05-22 14:52:52 -04005036 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5037 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04005038 rc = -ENXIO;
5039 goto do_prep_failed;
5040 }
James Smart04c68492009-05-22 14:52:52 -04005041 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04005042 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005043 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5044 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5045 phba->max_vpi : phba->max_vports;
5046
James Smart34b02dc2008-08-24 21:49:55 -04005047 } else
5048 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04005049 phba->fips_level = 0;
5050 phba->fips_spec_rev = 0;
5051 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04005052 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04005053 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
5054 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
5055 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5056 "2850 Security Crypto Active. FIPS x%d "
5057 "(Spec Rev: x%d)",
5058 phba->fips_level, phba->fips_spec_rev);
5059 }
5060 if (pmb->u.mb.un.varCfgPort.sec_err) {
5061 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5062 "2856 Config Port Security Crypto "
5063 "Error: x%x ",
5064 pmb->u.mb.un.varCfgPort.sec_err);
5065 }
James Smart04c68492009-05-22 14:52:52 -04005066 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04005067 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005068 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04005069 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04005070
5071 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5072 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05005073
James Smartf44ac122018-03-05 12:04:08 -08005074 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5075 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5076 phba->cfg_enable_bg = 0;
5077 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smarte2a0a9d2008-12-04 22:40:02 -05005078 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5079 "0443 Adapter did not grant "
5080 "BlockGuard\n");
James Smartf44ac122018-03-05 12:04:08 -08005081 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005082 }
James Smart34b02dc2008-08-24 21:49:55 -04005083 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05005084 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04005085 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04005086 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05005087 }
James Smart92d7f7b2007-06-17 19:56:38 -05005088do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05005089 mempool_free(pmb, phba->mbox_mem_pool);
5090 return rc;
5091}
5092
James Smarte59058c2008-08-24 21:49:00 -04005093
5094/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08005095 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04005096 * @phba: Pointer to HBA context object.
5097 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005098 * This function is the main SLI initialization function. This function
5099 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04005100 * error attention handler code. Caller is not required to hold any
5101 * locks. This function issues config_port mailbox command to configure
5102 * the SLI, setup iocb rings and HBQ rings. In the end the function
5103 * calls the config_port_post function to issue init_link mailbox
5104 * command and to start the discovery. The function will return zero
5105 * if successful, else it will return negative error code.
5106 **/
James Smarted957682007-06-17 19:56:37 -05005107int
5108lpfc_sli_hba_setup(struct lpfc_hba *phba)
5109{
5110 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04005111 int mode = 3, i;
5112 int longs;
James Smarted957682007-06-17 19:56:37 -05005113
James Smart12247e82016-07-06 12:36:09 -07005114 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05005115 case 2:
James Smart78b2d852007-08-02 11:10:21 -04005116 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05005117 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005118 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05005119 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07005120 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05005121 break;
5122 }
James Smarted957682007-06-17 19:56:37 -05005123 mode = 2;
5124 break;
5125 case 0:
5126 case 3:
5127 break;
5128 default:
James Smart92d7f7b2007-06-17 19:56:38 -05005129 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005130 "1819 Unrecognized sli_mode parameter: %d.\n",
5131 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05005132
5133 break;
5134 }
James Smartb5c53952016-03-31 14:12:30 -07005135 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005136
James Smart93996272008-08-24 21:50:30 -04005137 rc = lpfc_sli_config_port(phba, mode);
5138
James Smart12247e82016-07-06 12:36:09 -07005139 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05005140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005141 "1820 Unable to select SLI-3. "
5142 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05005143 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04005144 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07005145 else if (rc && mode == 2)
5146 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05005147 if (rc)
dea31012005-04-17 16:05:31 -05005148 goto lpfc_sli_hba_setup_error;
5149
James Smart0d878412009-10-02 15:16:56 -04005150 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5151 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5152 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5153 if (!rc) {
5154 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5155 "2709 This device supports "
5156 "Advanced Error Reporting (AER)\n");
5157 spin_lock_irq(&phba->hbalock);
5158 phba->hba_flag |= HBA_AER_ENABLED;
5159 spin_unlock_irq(&phba->hbalock);
5160 } else {
5161 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5162 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005163 "Advanced Error Reporting (AER): %d\n",
5164 rc);
James Smart0d878412009-10-02 15:16:56 -04005165 phba->cfg_aer_support = 0;
5166 }
5167 }
5168
James Smarted957682007-06-17 19:56:37 -05005169 if (phba->sli_rev == 3) {
5170 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5171 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005172 } else {
5173 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5174 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005175 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005176 }
5177
5178 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005179 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5180 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005181 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005182
5183 if (rc)
5184 goto lpfc_sli_hba_setup_error;
5185
James Smart6d368e52011-05-24 11:44:12 -04005186 /* Initialize VPIs. */
5187 if (phba->sli_rev == LPFC_SLI_REV3) {
5188 /*
5189 * The VPI bitmask and physical ID array are allocated
5190 * and initialized once only - at driver load. A port
5191 * reset doesn't need to reinitialize this memory.
5192 */
5193 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5194 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07005195 phba->vpi_bmask = kcalloc(longs,
5196 sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005197 GFP_KERNEL);
5198 if (!phba->vpi_bmask) {
5199 rc = -ENOMEM;
5200 goto lpfc_sli_hba_setup_error;
5201 }
5202
Kees Cook6396bb22018-06-12 14:03:40 -07005203 phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5204 sizeof(uint16_t),
5205 GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -04005206 if (!phba->vpi_ids) {
5207 kfree(phba->vpi_bmask);
5208 rc = -ENOMEM;
5209 goto lpfc_sli_hba_setup_error;
5210 }
5211 for (i = 0; i < phba->max_vpi; i++)
5212 phba->vpi_ids[i] = i;
5213 }
5214 }
5215
James Smart93996272008-08-24 21:50:30 -04005216 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005217 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5218 rc = lpfc_sli_hbq_setup(phba);
5219 if (rc)
5220 goto lpfc_sli_hba_setup_error;
5221 }
James Smart04c68492009-05-22 14:52:52 -04005222 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005223 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005224 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005225
5226 rc = lpfc_config_port_post(phba);
5227 if (rc)
5228 goto lpfc_sli_hba_setup_error;
5229
James Smarted957682007-06-17 19:56:37 -05005230 return rc;
5231
James Smart92d7f7b2007-06-17 19:56:38 -05005232lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005233 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05005234 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005235 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005236 return rc;
5237}
5238
James Smartda0436e2009-05-22 14:51:39 -04005239/**
5240 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5241 * @phba: Pointer to HBA context object.
5242 * @mboxq: mailbox pointer.
5243 * This function issue a dump mailbox command to read config region
5244 * 23 and parse the records in the region and populate driver
5245 * data structure.
5246 **/
5247static int
James Smartff78d8f2011-12-13 13:21:35 -05005248lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005249{
James Smartff78d8f2011-12-13 13:21:35 -05005250 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005251 struct lpfc_dmabuf *mp;
5252 struct lpfc_mqe *mqe;
5253 uint32_t data_length;
5254 int rc;
5255
5256 /* Program the default value of vlan_id and fc_map */
5257 phba->valid_vlan = 0;
5258 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5259 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5260 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5261
James Smartff78d8f2011-12-13 13:21:35 -05005262 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5263 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005264 return -ENOMEM;
5265
James Smartff78d8f2011-12-13 13:21:35 -05005266 mqe = &mboxq->u.mqe;
5267 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5268 rc = -ENOMEM;
5269 goto out_free_mboxq;
5270 }
5271
James Smart3e1f0712018-11-29 16:09:29 -08005272 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04005273 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5274
5275 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5276 "(%d):2571 Mailbox cmd x%x Status x%x "
5277 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5278 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5279 "CQ: x%x x%x x%x x%x\n",
5280 mboxq->vport ? mboxq->vport->vpi : 0,
5281 bf_get(lpfc_mqe_command, mqe),
5282 bf_get(lpfc_mqe_status, mqe),
5283 mqe->un.mb_words[0], mqe->un.mb_words[1],
5284 mqe->un.mb_words[2], mqe->un.mb_words[3],
5285 mqe->un.mb_words[4], mqe->un.mb_words[5],
5286 mqe->un.mb_words[6], mqe->un.mb_words[7],
5287 mqe->un.mb_words[8], mqe->un.mb_words[9],
5288 mqe->un.mb_words[10], mqe->un.mb_words[11],
5289 mqe->un.mb_words[12], mqe->un.mb_words[13],
5290 mqe->un.mb_words[14], mqe->un.mb_words[15],
5291 mqe->un.mb_words[16], mqe->un.mb_words[50],
5292 mboxq->mcqe.word0,
5293 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5294 mboxq->mcqe.trailer);
5295
5296 if (rc) {
5297 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5298 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005299 rc = -EIO;
5300 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005301 }
5302 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005303 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005304 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5305 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005306 rc = -EIO;
5307 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005308 }
James Smartda0436e2009-05-22 14:51:39 -04005309
5310 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5311 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5312 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005313 rc = 0;
5314
5315out_free_mboxq:
5316 mempool_free(mboxq, phba->mbox_mem_pool);
5317 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005318}
5319
5320/**
5321 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5322 * @phba: pointer to lpfc hba data structure.
5323 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5324 * @vpd: pointer to the memory to hold resulting port vpd data.
5325 * @vpd_size: On input, the number of bytes allocated to @vpd.
5326 * On output, the number of data bytes in @vpd.
5327 *
5328 * This routine executes a READ_REV SLI4 mailbox command. In
5329 * addition, this routine gets the port vpd data.
5330 *
5331 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005332 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005333 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005334 **/
5335static int
5336lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5337 uint8_t *vpd, uint32_t *vpd_size)
5338{
5339 int rc = 0;
5340 uint32_t dma_size;
5341 struct lpfc_dmabuf *dmabuf;
5342 struct lpfc_mqe *mqe;
5343
5344 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5345 if (!dmabuf)
5346 return -ENOMEM;
5347
5348 /*
5349 * Get a DMA buffer for the vpd data resulting from the READ_REV
5350 * mailbox command.
5351 */
5352 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04005353 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5354 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005355 if (!dmabuf->virt) {
5356 kfree(dmabuf);
5357 return -ENOMEM;
5358 }
James Smartda0436e2009-05-22 14:51:39 -04005359
5360 /*
5361 * The SLI4 implementation of READ_REV conflicts at word1,
5362 * bits 31:16 and SLI4 adds vpd functionality not present
5363 * in SLI3. This code corrects the conflicts.
5364 */
5365 lpfc_read_rev(phba, mboxq);
5366 mqe = &mboxq->u.mqe;
5367 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5368 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5369 mqe->un.read_rev.word1 &= 0x0000FFFF;
5370 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5371 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5372
5373 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5374 if (rc) {
5375 dma_free_coherent(&phba->pcidev->dev, dma_size,
5376 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005377 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005378 return -EIO;
5379 }
5380
James Smartda0436e2009-05-22 14:51:39 -04005381 /*
5382 * The available vpd length cannot be bigger than the
5383 * DMA buffer passed to the port. Catch the less than
5384 * case and update the caller's size.
5385 */
5386 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5387 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5388
James Smartd7c47992010-06-08 18:31:54 -04005389 memcpy(vpd, dmabuf->virt, *vpd_size);
5390
James Smartda0436e2009-05-22 14:51:39 -04005391 dma_free_coherent(&phba->pcidev->dev, dma_size,
5392 dmabuf->virt, dmabuf->phys);
5393 kfree(dmabuf);
5394 return 0;
5395}
5396
5397/**
James Smartcd1c8302011-10-10 21:33:25 -04005398 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5399 * @phba: pointer to lpfc hba data structure.
5400 *
5401 * This routine retrieves SLI4 device physical port name this PCI function
5402 * is attached to.
5403 *
5404 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005405 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005406 * otherwise - failed to retrieve physical port name
5407 **/
5408static int
5409lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5410{
5411 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005412 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5413 struct lpfc_controller_attribute *cntl_attr;
5414 struct lpfc_mbx_get_port_name *get_port_name;
5415 void *virtaddr = NULL;
5416 uint32_t alloclen, reqlen;
5417 uint32_t shdr_status, shdr_add_status;
5418 union lpfc_sli4_cfg_shdr *shdr;
5419 char cport_name = 0;
5420 int rc;
5421
5422 /* We assume nothing at this point */
5423 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5424 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5425
5426 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5427 if (!mboxq)
5428 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005429 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005430 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5431 lpfc_sli4_read_config(phba);
5432 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5433 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005434
5435 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5436 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5437 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5438 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5439 LPFC_SLI4_MBX_NEMBED);
5440 if (alloclen < reqlen) {
5441 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5442 "3084 Allocated DMA memory size (%d) is "
5443 "less than the requested DMA memory size "
5444 "(%d)\n", alloclen, reqlen);
5445 rc = -ENOMEM;
5446 goto out_free_mboxq;
5447 }
5448 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5449 virtaddr = mboxq->sge_array->addr[0];
5450 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5451 shdr = &mbx_cntl_attr->cfg_shdr;
5452 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5453 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5454 if (shdr_status || shdr_add_status || rc) {
5455 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5456 "3085 Mailbox x%x (x%x/x%x) failed, "
5457 "rc:x%x, status:x%x, add_status:x%x\n",
5458 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5459 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5460 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5461 rc, shdr_status, shdr_add_status);
5462 rc = -ENXIO;
5463 goto out_free_mboxq;
5464 }
5465 cntl_attr = &mbx_cntl_attr->cntl_attr;
5466 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5467 phba->sli4_hba.lnk_info.lnk_tp =
5468 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5469 phba->sli4_hba.lnk_info.lnk_no =
5470 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5471 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5472 "3086 lnk_type:%d, lnk_numb:%d\n",
5473 phba->sli4_hba.lnk_info.lnk_tp,
5474 phba->sli4_hba.lnk_info.lnk_no);
5475
5476retrieve_ppname:
5477 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5478 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5479 sizeof(struct lpfc_mbx_get_port_name) -
5480 sizeof(struct lpfc_sli4_cfg_mhdr),
5481 LPFC_SLI4_MBX_EMBED);
5482 get_port_name = &mboxq->u.mqe.un.get_port_name;
5483 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5484 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5485 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5486 phba->sli4_hba.lnk_info.lnk_tp);
5487 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5488 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5489 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5490 if (shdr_status || shdr_add_status || rc) {
5491 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5492 "3087 Mailbox x%x (x%x/x%x) failed: "
5493 "rc:x%x, status:x%x, add_status:x%x\n",
5494 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5495 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5496 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5497 rc, shdr_status, shdr_add_status);
5498 rc = -ENXIO;
5499 goto out_free_mboxq;
5500 }
5501 switch (phba->sli4_hba.lnk_info.lnk_no) {
5502 case LPFC_LINK_NUMBER_0:
5503 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5504 &get_port_name->u.response);
5505 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5506 break;
5507 case LPFC_LINK_NUMBER_1:
5508 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5509 &get_port_name->u.response);
5510 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5511 break;
5512 case LPFC_LINK_NUMBER_2:
5513 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5514 &get_port_name->u.response);
5515 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5516 break;
5517 case LPFC_LINK_NUMBER_3:
5518 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5519 &get_port_name->u.response);
5520 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5521 break;
5522 default:
5523 break;
5524 }
5525
5526 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5527 phba->Port[0] = cport_name;
5528 phba->Port[1] = '\0';
5529 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5530 "3091 SLI get port name: %s\n", phba->Port);
5531 }
5532
5533out_free_mboxq:
5534 if (rc != MBX_TIMEOUT) {
5535 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5536 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5537 else
5538 mempool_free(mboxq, phba->mbox_mem_pool);
5539 }
5540 return rc;
5541}
5542
5543/**
James Smartda0436e2009-05-22 14:51:39 -04005544 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5545 * @phba: pointer to lpfc hba data structure.
5546 *
5547 * This routine is called to explicitly arm the SLI4 device's completion and
5548 * event queues
5549 **/
5550static void
5551lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5552{
James Smart895427b2017-02-12 13:52:30 -08005553 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08005554 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartcdb42be2019-01-28 11:14:21 -08005555 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -04005556
James Smart32517fc2019-01-28 11:14:33 -08005557 sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
5558 sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
James Smartb71413d2018-02-22 08:18:40 -08005559 if (sli4_hba->nvmels_cq)
James Smart32517fc2019-01-28 11:14:33 -08005560 sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
5561 LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08005562
James Smartcdb42be2019-01-28 11:14:21 -08005563 qp = sli4_hba->hdwq;
5564 if (sli4_hba->hdwq) {
5565 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart32517fc2019-01-28 11:14:33 -08005566 sli4_hba->sli4_write_cq_db(phba, qp[qidx].fcp_cq, 0,
5567 LPFC_QUEUE_REARM);
5568 sli4_hba->sli4_write_cq_db(phba, qp[qidx].nvme_cq, 0,
5569 LPFC_QUEUE_REARM);
James Smartcdb42be2019-01-28 11:14:21 -08005570 }
James Smart1ba981f2014-02-20 09:56:45 -05005571
James Smart6a828b02019-01-28 11:14:31 -08005572 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++)
James Smart32517fc2019-01-28 11:14:33 -08005573 sli4_hba->sli4_write_eq_db(phba, qp[qidx].hba_eq,
5574 0, LPFC_QUEUE_REARM);
James Smartcdb42be2019-01-28 11:14:21 -08005575 }
James Smart1ba981f2014-02-20 09:56:45 -05005576
James Smart2d7dbc42017-02-12 13:52:35 -08005577 if (phba->nvmet_support) {
5578 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smart32517fc2019-01-28 11:14:33 -08005579 sli4_hba->sli4_write_cq_db(phba,
5580 sli4_hba->nvmet_cqset[qidx], 0,
James Smart2d7dbc42017-02-12 13:52:35 -08005581 LPFC_QUEUE_REARM);
5582 }
James Smart2e90f4b2011-12-13 13:22:37 -05005583 }
James Smartda0436e2009-05-22 14:51:39 -04005584}
5585
5586/**
James Smart6d368e52011-05-24 11:44:12 -04005587 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5588 * @phba: Pointer to HBA context object.
5589 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005590 * @extnt_count: buffer to hold port available extent count.
5591 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005592 *
James Smartb76f2dc2011-07-22 18:37:42 -04005593 * This function calls the port and retrievs the number of available
5594 * extents and their size for a particular extent type.
5595 *
5596 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005597 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005598int
James Smart6d368e52011-05-24 11:44:12 -04005599lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5600 uint16_t *extnt_count, uint16_t *extnt_size)
5601{
5602 int rc = 0;
5603 uint32_t length;
5604 uint32_t mbox_tmo;
5605 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5606 LPFC_MBOXQ_t *mbox;
5607
5608 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5609 if (!mbox)
5610 return -ENOMEM;
5611
5612 /* Find out how many extents are available for this resource type */
5613 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5614 sizeof(struct lpfc_sli4_cfg_mhdr));
5615 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5616 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5617 length, LPFC_SLI4_MBX_EMBED);
5618
5619 /* Send an extents count of 0 - the GET doesn't use it. */
5620 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5621 LPFC_SLI4_MBX_EMBED);
5622 if (unlikely(rc)) {
5623 rc = -EIO;
5624 goto err_exit;
5625 }
5626
5627 if (!phba->sli4_hba.intr_enable)
5628 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5629 else {
James Smarta183a152011-10-10 21:32:43 -04005630 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005631 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5632 }
5633 if (unlikely(rc)) {
5634 rc = -EIO;
5635 goto err_exit;
5636 }
5637
5638 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5639 if (bf_get(lpfc_mbox_hdr_status,
5640 &rsrc_info->header.cfg_shdr.response)) {
5641 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5642 "2930 Failed to get resource extents "
5643 "Status 0x%x Add'l Status 0x%x\n",
5644 bf_get(lpfc_mbox_hdr_status,
5645 &rsrc_info->header.cfg_shdr.response),
5646 bf_get(lpfc_mbox_hdr_add_status,
5647 &rsrc_info->header.cfg_shdr.response));
5648 rc = -EIO;
5649 goto err_exit;
5650 }
5651
5652 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5653 &rsrc_info->u.rsp);
5654 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5655 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005656
5657 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5658 "3162 Retrieved extents type-%d from port: count:%d, "
5659 "size:%d\n", type, *extnt_count, *extnt_size);
5660
5661err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005662 mempool_free(mbox, phba->mbox_mem_pool);
5663 return rc;
5664}
5665
5666/**
5667 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5668 * @phba: Pointer to HBA context object.
5669 * @type: The extent type to check.
5670 *
5671 * This function reads the current available extents from the port and checks
5672 * if the extent count or extent size has changed since the last access.
5673 * Callers use this routine post port reset to understand if there is a
5674 * extent reprovisioning requirement.
5675 *
5676 * Returns:
5677 * -Error: error indicates problem.
5678 * 1: Extent count or size has changed.
5679 * 0: No changes.
5680 **/
5681static int
5682lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5683{
5684 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5685 uint16_t size_diff, rsrc_ext_size;
5686 int rc = 0;
5687 struct lpfc_rsrc_blks *rsrc_entry;
5688 struct list_head *rsrc_blk_list = NULL;
5689
5690 size_diff = 0;
5691 curr_ext_cnt = 0;
5692 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5693 &rsrc_ext_cnt,
5694 &rsrc_ext_size);
5695 if (unlikely(rc))
5696 return -EIO;
5697
5698 switch (type) {
5699 case LPFC_RSC_TYPE_FCOE_RPI:
5700 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5701 break;
5702 case LPFC_RSC_TYPE_FCOE_VPI:
5703 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5704 break;
5705 case LPFC_RSC_TYPE_FCOE_XRI:
5706 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5707 break;
5708 case LPFC_RSC_TYPE_FCOE_VFI:
5709 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5710 break;
5711 default:
5712 break;
5713 }
5714
5715 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5716 curr_ext_cnt++;
5717 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5718 size_diff++;
5719 }
5720
5721 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5722 rc = 1;
5723
5724 return rc;
5725}
5726
5727/**
5728 * lpfc_sli4_cfg_post_extnts -
5729 * @phba: Pointer to HBA context object.
5730 * @extnt_cnt - number of available extents.
5731 * @type - the extent type (rpi, xri, vfi, vpi).
5732 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5733 * @mbox - pointer to the caller's allocated mailbox structure.
5734 *
5735 * This function executes the extents allocation request. It also
5736 * takes care of the amount of memory needed to allocate or get the
5737 * allocated extents. It is the caller's responsibility to evaluate
5738 * the response.
5739 *
5740 * Returns:
5741 * -Error: Error value describes the condition found.
5742 * 0: if successful
5743 **/
5744static int
James Smart8a9d2e82012-05-09 21:16:12 -04005745lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005746 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5747{
5748 int rc = 0;
5749 uint32_t req_len;
5750 uint32_t emb_len;
5751 uint32_t alloc_len, mbox_tmo;
5752
5753 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005754 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005755
5756 /*
5757 * Calculate the size of an embedded mailbox. The uint32_t
5758 * accounts for extents-specific word.
5759 */
5760 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5761 sizeof(uint32_t);
5762
5763 /*
5764 * Presume the allocation and response will fit into an embedded
5765 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5766 */
5767 *emb = LPFC_SLI4_MBX_EMBED;
5768 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005769 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005770 sizeof(union lpfc_sli4_cfg_shdr) +
5771 sizeof(uint32_t);
5772 *emb = LPFC_SLI4_MBX_NEMBED;
5773 }
5774
5775 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5776 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5777 req_len, *emb);
5778 if (alloc_len < req_len) {
5779 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005780 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005781 "less than the requested DMA memory "
5782 "size (x%x)\n", alloc_len, req_len);
5783 return -ENOMEM;
5784 }
James Smart8a9d2e82012-05-09 21:16:12 -04005785 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005786 if (unlikely(rc))
5787 return -EIO;
5788
5789 if (!phba->sli4_hba.intr_enable)
5790 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5791 else {
James Smarta183a152011-10-10 21:32:43 -04005792 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005793 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5794 }
5795
5796 if (unlikely(rc))
5797 rc = -EIO;
5798 return rc;
5799}
5800
5801/**
5802 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5803 * @phba: Pointer to HBA context object.
5804 * @type: The resource extent type to allocate.
5805 *
5806 * This function allocates the number of elements for the specified
5807 * resource type.
5808 **/
5809static int
5810lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5811{
5812 bool emb = false;
5813 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5814 uint16_t rsrc_id, rsrc_start, j, k;
5815 uint16_t *ids;
5816 int i, rc;
5817 unsigned long longs;
5818 unsigned long *bmask;
5819 struct lpfc_rsrc_blks *rsrc_blks;
5820 LPFC_MBOXQ_t *mbox;
5821 uint32_t length;
5822 struct lpfc_id_range *id_array = NULL;
5823 void *virtaddr = NULL;
5824 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5825 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5826 struct list_head *ext_blk_list;
5827
5828 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5829 &rsrc_cnt,
5830 &rsrc_size);
5831 if (unlikely(rc))
5832 return -EIO;
5833
5834 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5835 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5836 "3009 No available Resource Extents "
5837 "for resource type 0x%x: Count: 0x%x, "
5838 "Size 0x%x\n", type, rsrc_cnt,
5839 rsrc_size);
5840 return -ENOMEM;
5841 }
5842
James Smart8a9d2e82012-05-09 21:16:12 -04005843 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5844 "2903 Post resource extents type-0x%x: "
5845 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005846
5847 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5848 if (!mbox)
5849 return -ENOMEM;
5850
James Smart8a9d2e82012-05-09 21:16:12 -04005851 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005852 if (unlikely(rc)) {
5853 rc = -EIO;
5854 goto err_exit;
5855 }
5856
5857 /*
5858 * Figure out where the response is located. Then get local pointers
5859 * to the response data. The port does not guarantee to respond to
5860 * all extents counts request so update the local variable with the
5861 * allocated count from the port.
5862 */
5863 if (emb == LPFC_SLI4_MBX_EMBED) {
5864 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5865 id_array = &rsrc_ext->u.rsp.id[0];
5866 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5867 } else {
5868 virtaddr = mbox->sge_array->addr[0];
5869 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5870 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5871 id_array = &n_rsrc->id;
5872 }
5873
5874 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5875 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5876
5877 /*
5878 * Based on the resource size and count, correct the base and max
5879 * resource values.
5880 */
5881 length = sizeof(struct lpfc_rsrc_blks);
5882 switch (type) {
5883 case LPFC_RSC_TYPE_FCOE_RPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005884 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005885 sizeof(unsigned long),
5886 GFP_KERNEL);
5887 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5888 rc = -ENOMEM;
5889 goto err_exit;
5890 }
Kees Cook6396bb22018-06-12 14:03:40 -07005891 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005892 sizeof(uint16_t),
5893 GFP_KERNEL);
5894 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5895 kfree(phba->sli4_hba.rpi_bmask);
5896 rc = -ENOMEM;
5897 goto err_exit;
5898 }
5899
5900 /*
5901 * The next_rpi was initialized with the maximum available
5902 * count but the port may allocate a smaller number. Catch
5903 * that case and update the next_rpi.
5904 */
5905 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5906
5907 /* Initialize local ptrs for common extent processing later. */
5908 bmask = phba->sli4_hba.rpi_bmask;
5909 ids = phba->sli4_hba.rpi_ids;
5910 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5911 break;
5912 case LPFC_RSC_TYPE_FCOE_VPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005913 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005914 GFP_KERNEL);
5915 if (unlikely(!phba->vpi_bmask)) {
5916 rc = -ENOMEM;
5917 goto err_exit;
5918 }
Kees Cook6396bb22018-06-12 14:03:40 -07005919 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04005920 GFP_KERNEL);
5921 if (unlikely(!phba->vpi_ids)) {
5922 kfree(phba->vpi_bmask);
5923 rc = -ENOMEM;
5924 goto err_exit;
5925 }
5926
5927 /* Initialize local ptrs for common extent processing later. */
5928 bmask = phba->vpi_bmask;
5929 ids = phba->vpi_ids;
5930 ext_blk_list = &phba->lpfc_vpi_blk_list;
5931 break;
5932 case LPFC_RSC_TYPE_FCOE_XRI:
Kees Cook6396bb22018-06-12 14:03:40 -07005933 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005934 sizeof(unsigned long),
5935 GFP_KERNEL);
5936 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5937 rc = -ENOMEM;
5938 goto err_exit;
5939 }
James Smart8a9d2e82012-05-09 21:16:12 -04005940 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07005941 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005942 sizeof(uint16_t),
5943 GFP_KERNEL);
5944 if (unlikely(!phba->sli4_hba.xri_ids)) {
5945 kfree(phba->sli4_hba.xri_bmask);
5946 rc = -ENOMEM;
5947 goto err_exit;
5948 }
5949
5950 /* Initialize local ptrs for common extent processing later. */
5951 bmask = phba->sli4_hba.xri_bmask;
5952 ids = phba->sli4_hba.xri_ids;
5953 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5954 break;
5955 case LPFC_RSC_TYPE_FCOE_VFI:
Kees Cook6396bb22018-06-12 14:03:40 -07005956 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005957 sizeof(unsigned long),
5958 GFP_KERNEL);
5959 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5960 rc = -ENOMEM;
5961 goto err_exit;
5962 }
Kees Cook6396bb22018-06-12 14:03:40 -07005963 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005964 sizeof(uint16_t),
5965 GFP_KERNEL);
5966 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5967 kfree(phba->sli4_hba.vfi_bmask);
5968 rc = -ENOMEM;
5969 goto err_exit;
5970 }
5971
5972 /* Initialize local ptrs for common extent processing later. */
5973 bmask = phba->sli4_hba.vfi_bmask;
5974 ids = phba->sli4_hba.vfi_ids;
5975 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5976 break;
5977 default:
5978 /* Unsupported Opcode. Fail call. */
5979 id_array = NULL;
5980 bmask = NULL;
5981 ids = NULL;
5982 ext_blk_list = NULL;
5983 goto err_exit;
5984 }
5985
5986 /*
5987 * Complete initializing the extent configuration with the
5988 * allocated ids assigned to this function. The bitmask serves
5989 * as an index into the array and manages the available ids. The
5990 * array just stores the ids communicated to the port via the wqes.
5991 */
5992 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5993 if ((i % 2) == 0)
5994 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5995 &id_array[k]);
5996 else
5997 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5998 &id_array[k]);
5999
6000 rsrc_blks = kzalloc(length, GFP_KERNEL);
6001 if (unlikely(!rsrc_blks)) {
6002 rc = -ENOMEM;
6003 kfree(bmask);
6004 kfree(ids);
6005 goto err_exit;
6006 }
6007 rsrc_blks->rsrc_start = rsrc_id;
6008 rsrc_blks->rsrc_size = rsrc_size;
6009 list_add_tail(&rsrc_blks->list, ext_blk_list);
6010 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08006011 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart5e5b5112019-01-28 11:14:22 -08006012 phba->sli4_hba.io_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08006013 lpfc_sli4_get_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08006014 }
James Smart6d368e52011-05-24 11:44:12 -04006015
6016 while (rsrc_id < (rsrc_start + rsrc_size)) {
6017 ids[j] = rsrc_id;
6018 rsrc_id++;
6019 j++;
6020 }
6021 /* Entire word processed. Get next word.*/
6022 if ((i % 2) == 1)
6023 k++;
6024 }
6025 err_exit:
6026 lpfc_sli4_mbox_cmd_free(phba, mbox);
6027 return rc;
6028}
6029
James Smart895427b2017-02-12 13:52:30 -08006030
6031
James Smart6d368e52011-05-24 11:44:12 -04006032/**
6033 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6034 * @phba: Pointer to HBA context object.
6035 * @type: the extent's type.
6036 *
6037 * This function deallocates all extents of a particular resource type.
6038 * SLI4 does not allow for deallocating a particular extent range. It
6039 * is the caller's responsibility to release all kernel memory resources.
6040 **/
6041static int
6042lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6043{
6044 int rc;
6045 uint32_t length, mbox_tmo = 0;
6046 LPFC_MBOXQ_t *mbox;
6047 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6048 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6049
6050 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6051 if (!mbox)
6052 return -ENOMEM;
6053
6054 /*
6055 * This function sends an embedded mailbox because it only sends the
6056 * the resource type. All extents of this type are released by the
6057 * port.
6058 */
6059 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6060 sizeof(struct lpfc_sli4_cfg_mhdr));
6061 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6062 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6063 length, LPFC_SLI4_MBX_EMBED);
6064
6065 /* Send an extents count of 0 - the dealloc doesn't use it. */
6066 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6067 LPFC_SLI4_MBX_EMBED);
6068 if (unlikely(rc)) {
6069 rc = -EIO;
6070 goto out_free_mbox;
6071 }
6072 if (!phba->sli4_hba.intr_enable)
6073 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6074 else {
James Smarta183a152011-10-10 21:32:43 -04006075 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006076 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6077 }
6078 if (unlikely(rc)) {
6079 rc = -EIO;
6080 goto out_free_mbox;
6081 }
6082
6083 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6084 if (bf_get(lpfc_mbox_hdr_status,
6085 &dealloc_rsrc->header.cfg_shdr.response)) {
6086 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6087 "2919 Failed to release resource extents "
6088 "for type %d - Status 0x%x Add'l Status 0x%x. "
6089 "Resource memory not released.\n",
6090 type,
6091 bf_get(lpfc_mbox_hdr_status,
6092 &dealloc_rsrc->header.cfg_shdr.response),
6093 bf_get(lpfc_mbox_hdr_add_status,
6094 &dealloc_rsrc->header.cfg_shdr.response));
6095 rc = -EIO;
6096 goto out_free_mbox;
6097 }
6098
6099 /* Release kernel memory resources for the specific type. */
6100 switch (type) {
6101 case LPFC_RSC_TYPE_FCOE_VPI:
6102 kfree(phba->vpi_bmask);
6103 kfree(phba->vpi_ids);
6104 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6105 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6106 &phba->lpfc_vpi_blk_list, list) {
6107 list_del_init(&rsrc_blk->list);
6108 kfree(rsrc_blk);
6109 }
James Smart16a3a202013-04-17 20:14:38 -04006110 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006111 break;
6112 case LPFC_RSC_TYPE_FCOE_XRI:
6113 kfree(phba->sli4_hba.xri_bmask);
6114 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006115 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6116 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6117 list_del_init(&rsrc_blk->list);
6118 kfree(rsrc_blk);
6119 }
6120 break;
6121 case LPFC_RSC_TYPE_FCOE_VFI:
6122 kfree(phba->sli4_hba.vfi_bmask);
6123 kfree(phba->sli4_hba.vfi_ids);
6124 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6125 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6126 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6127 list_del_init(&rsrc_blk->list);
6128 kfree(rsrc_blk);
6129 }
6130 break;
6131 case LPFC_RSC_TYPE_FCOE_RPI:
6132 /* RPI bitmask and physical id array are cleaned up earlier. */
6133 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6134 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6135 list_del_init(&rsrc_blk->list);
6136 kfree(rsrc_blk);
6137 }
6138 break;
6139 default:
6140 break;
6141 }
6142
6143 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6144
6145 out_free_mbox:
6146 mempool_free(mbox, phba->mbox_mem_pool);
6147 return rc;
6148}
6149
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006150static void
James Smart7bdedb32016-07-06 12:36:00 -07006151lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6152 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006153{
James Smart65791f12016-07-06 12:35:56 -07006154 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07006155
James Smart65791f12016-07-06 12:35:56 -07006156 len = sizeof(struct lpfc_mbx_set_feature) -
6157 sizeof(struct lpfc_sli4_cfg_mhdr);
6158 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6159 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6160 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006161
James Smart7bdedb32016-07-06 12:36:00 -07006162 switch (feature) {
6163 case LPFC_SET_UE_RECOVERY:
6164 bf_set(lpfc_mbx_set_feature_UER,
6165 &mbox->u.mqe.un.set_feature, 1);
6166 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6167 mbox->u.mqe.un.set_feature.param_len = 8;
6168 break;
6169 case LPFC_SET_MDS_DIAGS:
6170 bf_set(lpfc_mbx_set_feature_mds,
6171 &mbox->u.mqe.un.set_feature, 1);
6172 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006173 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006174 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6175 mbox->u.mqe.un.set_feature.param_len = 8;
6176 break;
James Smart65791f12016-07-06 12:35:56 -07006177 }
James Smart7bdedb32016-07-06 12:36:00 -07006178
6179 return;
James Smart65791f12016-07-06 12:35:56 -07006180}
6181
James Smart6d368e52011-05-24 11:44:12 -04006182/**
James Smart1165a5c2018-11-29 16:09:39 -08006183 * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6184 * @phba: Pointer to HBA context object.
6185 *
6186 * Disable FW logging into host memory on the adapter. To
6187 * be done before reading logs from the host memory.
6188 **/
6189void
6190lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6191{
6192 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6193
6194 ras_fwlog->ras_active = false;
6195
6196 /* Disable FW logging to host memory */
6197 writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6198 phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
6199}
6200
6201/**
James Smartd2cc9bc2018-09-10 10:30:50 -07006202 * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6203 * @phba: Pointer to HBA context object.
6204 *
6205 * This function is called to free memory allocated for RAS FW logging
6206 * support in the driver.
6207 **/
6208void
6209lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6210{
6211 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6212 struct lpfc_dmabuf *dmabuf, *next;
6213
6214 if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6215 list_for_each_entry_safe(dmabuf, next,
6216 &ras_fwlog->fwlog_buff_list,
6217 list) {
6218 list_del(&dmabuf->list);
6219 dma_free_coherent(&phba->pcidev->dev,
6220 LPFC_RAS_MAX_ENTRY_SIZE,
6221 dmabuf->virt, dmabuf->phys);
6222 kfree(dmabuf);
6223 }
6224 }
6225
6226 if (ras_fwlog->lwpd.virt) {
6227 dma_free_coherent(&phba->pcidev->dev,
6228 sizeof(uint32_t) * 2,
6229 ras_fwlog->lwpd.virt,
6230 ras_fwlog->lwpd.phys);
6231 ras_fwlog->lwpd.virt = NULL;
6232 }
6233
6234 ras_fwlog->ras_active = false;
6235}
6236
6237/**
6238 * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6239 * @phba: Pointer to HBA context object.
6240 * @fwlog_buff_count: Count of buffers to be created.
6241 *
6242 * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6243 * to update FW log is posted to the adapter.
6244 * Buffer count is calculated based on module param ras_fwlog_buffsize
6245 * Size of each buffer posted to FW is 64K.
6246 **/
6247
6248static int
6249lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6250 uint32_t fwlog_buff_count)
6251{
6252 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6253 struct lpfc_dmabuf *dmabuf;
6254 int rc = 0, i = 0;
6255
6256 /* Initialize List */
6257 INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6258
6259 /* Allocate memory for the LWPD */
6260 ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6261 sizeof(uint32_t) * 2,
6262 &ras_fwlog->lwpd.phys,
6263 GFP_KERNEL);
6264 if (!ras_fwlog->lwpd.virt) {
James Smartcb349902018-11-29 16:09:27 -08006265 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006266 "6185 LWPD Memory Alloc Failed\n");
6267
6268 return -ENOMEM;
6269 }
6270
6271 ras_fwlog->fw_buffcount = fwlog_buff_count;
6272 for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6273 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6274 GFP_KERNEL);
6275 if (!dmabuf) {
6276 rc = -ENOMEM;
6277 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6278 "6186 Memory Alloc failed FW logging");
6279 goto free_mem;
6280 }
6281
Sabyasachi Gupta359d0ac2018-11-18 20:08:48 +05306282 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
James Smartd2cc9bc2018-09-10 10:30:50 -07006283 LPFC_RAS_MAX_ENTRY_SIZE,
6284 &dmabuf->phys,
6285 GFP_KERNEL);
6286 if (!dmabuf->virt) {
6287 kfree(dmabuf);
6288 rc = -ENOMEM;
6289 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6290 "6187 DMA Alloc Failed FW logging");
6291 goto free_mem;
6292 }
James Smartd2cc9bc2018-09-10 10:30:50 -07006293 dmabuf->buffer_tag = i;
6294 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6295 }
6296
6297free_mem:
6298 if (rc)
6299 lpfc_sli4_ras_dma_free(phba);
6300
6301 return rc;
6302}
6303
6304/**
6305 * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6306 * @phba: pointer to lpfc hba data structure.
6307 * @pmboxq: pointer to the driver internal queue element for mailbox command.
6308 *
6309 * Completion handler for driver's RAS MBX command to the device.
6310 **/
6311static void
6312lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6313{
6314 MAILBOX_t *mb;
6315 union lpfc_sli4_cfg_shdr *shdr;
6316 uint32_t shdr_status, shdr_add_status;
6317 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6318
6319 mb = &pmb->u.mb;
6320
6321 shdr = (union lpfc_sli4_cfg_shdr *)
6322 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6323 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6324 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6325
6326 if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
James Smartcb349902018-11-29 16:09:27 -08006327 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smartd2cc9bc2018-09-10 10:30:50 -07006328 "6188 FW LOG mailbox "
6329 "completed with status x%x add_status x%x,"
6330 " mbx status x%x\n",
6331 shdr_status, shdr_add_status, mb->mbxStatus);
James Smartcb349902018-11-29 16:09:27 -08006332
6333 ras_fwlog->ras_hwsupport = false;
James Smartd2cc9bc2018-09-10 10:30:50 -07006334 goto disable_ras;
6335 }
6336
6337 ras_fwlog->ras_active = true;
6338 mempool_free(pmb, phba->mbox_mem_pool);
6339
6340 return;
6341
6342disable_ras:
6343 /* Free RAS DMA memory */
6344 lpfc_sli4_ras_dma_free(phba);
6345 mempool_free(pmb, phba->mbox_mem_pool);
6346}
6347
6348/**
6349 * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6350 * @phba: pointer to lpfc hba data structure.
6351 * @fwlog_level: Logging verbosity level.
6352 * @fwlog_enable: Enable/Disable logging.
6353 *
6354 * Initialize memory and post mailbox command to enable FW logging in host
6355 * memory.
6356 **/
6357int
6358lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6359 uint32_t fwlog_level,
6360 uint32_t fwlog_enable)
6361{
6362 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6363 struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6364 struct lpfc_dmabuf *dmabuf;
6365 LPFC_MBOXQ_t *mbox;
6366 uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6367 int rc = 0;
6368
6369 fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6370 phba->cfg_ras_fwlog_buffsize);
6371 fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6372
6373 /*
6374 * If re-enabling FW logging support use earlier allocated
6375 * DMA buffers while posting MBX command.
6376 **/
6377 if (!ras_fwlog->lwpd.virt) {
6378 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6379 if (rc) {
6380 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smartcb349902018-11-29 16:09:27 -08006381 "6189 FW Log Memory Allocation Failed");
James Smartd2cc9bc2018-09-10 10:30:50 -07006382 return rc;
6383 }
6384 }
6385
6386 /* Setup Mailbox command */
6387 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6388 if (!mbox) {
James Smartcb349902018-11-29 16:09:27 -08006389 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006390 "6190 RAS MBX Alloc Failed");
6391 rc = -ENOMEM;
6392 goto mem_free;
6393 }
6394
6395 ras_fwlog->fw_loglevel = fwlog_level;
6396 len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6397 sizeof(struct lpfc_sli4_cfg_mhdr));
6398
6399 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6400 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6401 len, LPFC_SLI4_MBX_EMBED);
6402
6403 mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6404 bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6405 fwlog_enable);
6406 bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6407 ras_fwlog->fw_loglevel);
6408 bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6409 ras_fwlog->fw_buffcount);
6410 bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6411 LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6412
6413 /* Update DMA buffer address */
6414 list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6415 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6416
6417 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6418 putPaddrLow(dmabuf->phys);
6419
6420 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
6421 putPaddrHigh(dmabuf->phys);
6422 }
6423
6424 /* Update LPWD address */
6425 mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
6426 mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
6427
6428 mbox->vport = phba->pport;
6429 mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
6430
6431 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6432
6433 if (rc == MBX_NOT_FINISHED) {
James Smartcb349902018-11-29 16:09:27 -08006434 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6435 "6191 FW-Log Mailbox failed. "
James Smartd2cc9bc2018-09-10 10:30:50 -07006436 "status %d mbxStatus : x%x", rc,
6437 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6438 mempool_free(mbox, phba->mbox_mem_pool);
6439 rc = -EIO;
6440 goto mem_free;
6441 } else
6442 rc = 0;
6443mem_free:
6444 if (rc)
6445 lpfc_sli4_ras_dma_free(phba);
6446
6447 return rc;
6448}
6449
6450/**
6451 * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
6452 * @phba: Pointer to HBA context object.
6453 *
6454 * Check if RAS is supported on the adapter and initialize it.
6455 **/
6456void
6457lpfc_sli4_ras_setup(struct lpfc_hba *phba)
6458{
6459 /* Check RAS FW Log needs to be enabled or not */
6460 if (lpfc_check_fwlog_support(phba))
6461 return;
6462
6463 lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
6464 LPFC_RAS_ENABLE_LOGGING);
6465}
6466
6467/**
James Smart6d368e52011-05-24 11:44:12 -04006468 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6469 * @phba: Pointer to HBA context object.
6470 *
6471 * This function allocates all SLI4 resource identifiers.
6472 **/
6473int
6474lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6475{
6476 int i, rc, error = 0;
6477 uint16_t count, base;
6478 unsigned long longs;
6479
James Smartff78d8f2011-12-13 13:21:35 -05006480 if (!phba->sli4_hba.rpi_hdrs_in_use)
6481 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04006482 if (phba->sli4_hba.extents_in_use) {
6483 /*
6484 * The port supports resource extents. The XRI, VPI, VFI, RPI
6485 * resource extent count must be read and allocated before
6486 * provisioning the resource id arrays.
6487 */
6488 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6489 LPFC_IDX_RSRC_RDY) {
6490 /*
6491 * Extent-based resources are set - the driver could
6492 * be in a port reset. Figure out if any corrective
6493 * actions need to be taken.
6494 */
6495 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6496 LPFC_RSC_TYPE_FCOE_VFI);
6497 if (rc != 0)
6498 error++;
6499 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6500 LPFC_RSC_TYPE_FCOE_VPI);
6501 if (rc != 0)
6502 error++;
6503 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6504 LPFC_RSC_TYPE_FCOE_XRI);
6505 if (rc != 0)
6506 error++;
6507 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6508 LPFC_RSC_TYPE_FCOE_RPI);
6509 if (rc != 0)
6510 error++;
6511
6512 /*
6513 * It's possible that the number of resources
6514 * provided to this port instance changed between
6515 * resets. Detect this condition and reallocate
6516 * resources. Otherwise, there is no action.
6517 */
6518 if (error) {
6519 lpfc_printf_log(phba, KERN_INFO,
6520 LOG_MBOX | LOG_INIT,
6521 "2931 Detected extent resource "
6522 "change. Reallocating all "
6523 "extents.\n");
6524 rc = lpfc_sli4_dealloc_extent(phba,
6525 LPFC_RSC_TYPE_FCOE_VFI);
6526 rc = lpfc_sli4_dealloc_extent(phba,
6527 LPFC_RSC_TYPE_FCOE_VPI);
6528 rc = lpfc_sli4_dealloc_extent(phba,
6529 LPFC_RSC_TYPE_FCOE_XRI);
6530 rc = lpfc_sli4_dealloc_extent(phba,
6531 LPFC_RSC_TYPE_FCOE_RPI);
6532 } else
6533 return 0;
6534 }
6535
6536 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6537 if (unlikely(rc))
6538 goto err_exit;
6539
6540 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6541 if (unlikely(rc))
6542 goto err_exit;
6543
6544 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6545 if (unlikely(rc))
6546 goto err_exit;
6547
6548 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6549 if (unlikely(rc))
6550 goto err_exit;
6551 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6552 LPFC_IDX_RSRC_RDY);
6553 return rc;
6554 } else {
6555 /*
6556 * The port does not support resource extents. The XRI, VPI,
6557 * VFI, RPI resource ids were determined from READ_CONFIG.
6558 * Just allocate the bitmasks and provision the resource id
6559 * arrays. If a port reset is active, the resources don't
6560 * need any action - just exit.
6561 */
6562 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006563 LPFC_IDX_RSRC_RDY) {
6564 lpfc_sli4_dealloc_resource_identifiers(phba);
6565 lpfc_sli4_remove_rpis(phba);
6566 }
James Smart6d368e52011-05-24 11:44:12 -04006567 /* RPIs. */
6568 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006569 if (count <= 0) {
6570 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6571 "3279 Invalid provisioning of "
6572 "rpi:%d\n", count);
6573 rc = -EINVAL;
6574 goto err_exit;
6575 }
James Smart6d368e52011-05-24 11:44:12 -04006576 base = phba->sli4_hba.max_cfg_param.rpi_base;
6577 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006578 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006579 sizeof(unsigned long),
6580 GFP_KERNEL);
6581 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6582 rc = -ENOMEM;
6583 goto err_exit;
6584 }
Kees Cook6396bb22018-06-12 14:03:40 -07006585 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006586 GFP_KERNEL);
6587 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6588 rc = -ENOMEM;
6589 goto free_rpi_bmask;
6590 }
6591
6592 for (i = 0; i < count; i++)
6593 phba->sli4_hba.rpi_ids[i] = base + i;
6594
6595 /* VPIs. */
6596 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006597 if (count <= 0) {
6598 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6599 "3280 Invalid provisioning of "
6600 "vpi:%d\n", count);
6601 rc = -EINVAL;
6602 goto free_rpi_ids;
6603 }
James Smart6d368e52011-05-24 11:44:12 -04006604 base = phba->sli4_hba.max_cfg_param.vpi_base;
6605 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006606 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006607 GFP_KERNEL);
6608 if (unlikely(!phba->vpi_bmask)) {
6609 rc = -ENOMEM;
6610 goto free_rpi_ids;
6611 }
Kees Cook6396bb22018-06-12 14:03:40 -07006612 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006613 GFP_KERNEL);
6614 if (unlikely(!phba->vpi_ids)) {
6615 rc = -ENOMEM;
6616 goto free_vpi_bmask;
6617 }
6618
6619 for (i = 0; i < count; i++)
6620 phba->vpi_ids[i] = base + i;
6621
6622 /* XRIs. */
6623 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006624 if (count <= 0) {
6625 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6626 "3281 Invalid provisioning of "
6627 "xri:%d\n", count);
6628 rc = -EINVAL;
6629 goto free_vpi_ids;
6630 }
James Smart6d368e52011-05-24 11:44:12 -04006631 base = phba->sli4_hba.max_cfg_param.xri_base;
6632 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006633 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006634 sizeof(unsigned long),
6635 GFP_KERNEL);
6636 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6637 rc = -ENOMEM;
6638 goto free_vpi_ids;
6639 }
James Smart41899be2012-03-01 22:34:19 -05006640 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006641 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006642 GFP_KERNEL);
6643 if (unlikely(!phba->sli4_hba.xri_ids)) {
6644 rc = -ENOMEM;
6645 goto free_xri_bmask;
6646 }
6647
6648 for (i = 0; i < count; i++)
6649 phba->sli4_hba.xri_ids[i] = base + i;
6650
6651 /* VFIs. */
6652 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006653 if (count <= 0) {
6654 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6655 "3282 Invalid provisioning of "
6656 "vfi:%d\n", count);
6657 rc = -EINVAL;
6658 goto free_xri_ids;
6659 }
James Smart6d368e52011-05-24 11:44:12 -04006660 base = phba->sli4_hba.max_cfg_param.vfi_base;
6661 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006662 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006663 sizeof(unsigned long),
6664 GFP_KERNEL);
6665 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6666 rc = -ENOMEM;
6667 goto free_xri_ids;
6668 }
Kees Cook6396bb22018-06-12 14:03:40 -07006669 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006670 GFP_KERNEL);
6671 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6672 rc = -ENOMEM;
6673 goto free_vfi_bmask;
6674 }
6675
6676 for (i = 0; i < count; i++)
6677 phba->sli4_hba.vfi_ids[i] = base + i;
6678
6679 /*
6680 * Mark all resources ready. An HBA reset doesn't need
6681 * to reset the initialization.
6682 */
6683 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6684 LPFC_IDX_RSRC_RDY);
6685 return 0;
6686 }
6687
6688 free_vfi_bmask:
6689 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006690 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006691 free_xri_ids:
6692 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006693 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006694 free_xri_bmask:
6695 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006696 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006697 free_vpi_ids:
6698 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006699 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006700 free_vpi_bmask:
6701 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006702 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006703 free_rpi_ids:
6704 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006705 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006706 free_rpi_bmask:
6707 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006708 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006709 err_exit:
6710 return rc;
6711}
6712
6713/**
6714 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6715 * @phba: Pointer to HBA context object.
6716 *
6717 * This function allocates the number of elements for the specified
6718 * resource type.
6719 **/
6720int
6721lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6722{
6723 if (phba->sli4_hba.extents_in_use) {
6724 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6725 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6726 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6727 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6728 } else {
6729 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006730 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006731 kfree(phba->vpi_ids);
6732 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6733 kfree(phba->sli4_hba.xri_bmask);
6734 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006735 kfree(phba->sli4_hba.vfi_bmask);
6736 kfree(phba->sli4_hba.vfi_ids);
6737 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6738 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6739 }
6740
6741 return 0;
6742}
6743
6744/**
James Smartb76f2dc2011-07-22 18:37:42 -04006745 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6746 * @phba: Pointer to HBA context object.
6747 * @type: The resource extent type.
6748 * @extnt_count: buffer to hold port extent count response
6749 * @extnt_size: buffer to hold port extent size response.
6750 *
6751 * This function calls the port to read the host allocated extents
6752 * for a particular type.
6753 **/
6754int
6755lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6756 uint16_t *extnt_cnt, uint16_t *extnt_size)
6757{
6758 bool emb;
6759 int rc = 0;
6760 uint16_t curr_blks = 0;
6761 uint32_t req_len, emb_len;
6762 uint32_t alloc_len, mbox_tmo;
6763 struct list_head *blk_list_head;
6764 struct lpfc_rsrc_blks *rsrc_blk;
6765 LPFC_MBOXQ_t *mbox;
6766 void *virtaddr = NULL;
6767 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6768 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6769 union lpfc_sli4_cfg_shdr *shdr;
6770
6771 switch (type) {
6772 case LPFC_RSC_TYPE_FCOE_VPI:
6773 blk_list_head = &phba->lpfc_vpi_blk_list;
6774 break;
6775 case LPFC_RSC_TYPE_FCOE_XRI:
6776 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6777 break;
6778 case LPFC_RSC_TYPE_FCOE_VFI:
6779 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6780 break;
6781 case LPFC_RSC_TYPE_FCOE_RPI:
6782 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6783 break;
6784 default:
6785 return -EIO;
6786 }
6787
6788 /* Count the number of extents currently allocatd for this type. */
6789 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6790 if (curr_blks == 0) {
6791 /*
6792 * The GET_ALLOCATED mailbox does not return the size,
6793 * just the count. The size should be just the size
6794 * stored in the current allocated block and all sizes
6795 * for an extent type are the same so set the return
6796 * value now.
6797 */
6798 *extnt_size = rsrc_blk->rsrc_size;
6799 }
6800 curr_blks++;
6801 }
6802
James Smartb76f2dc2011-07-22 18:37:42 -04006803 /*
6804 * Calculate the size of an embedded mailbox. The uint32_t
6805 * accounts for extents-specific word.
6806 */
6807 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6808 sizeof(uint32_t);
6809
6810 /*
6811 * Presume the allocation and response will fit into an embedded
6812 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6813 */
6814 emb = LPFC_SLI4_MBX_EMBED;
6815 req_len = emb_len;
6816 if (req_len > emb_len) {
6817 req_len = curr_blks * sizeof(uint16_t) +
6818 sizeof(union lpfc_sli4_cfg_shdr) +
6819 sizeof(uint32_t);
6820 emb = LPFC_SLI4_MBX_NEMBED;
6821 }
6822
6823 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6824 if (!mbox)
6825 return -ENOMEM;
6826 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6827
6828 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6829 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6830 req_len, emb);
6831 if (alloc_len < req_len) {
6832 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6833 "2983 Allocated DMA memory size (x%x) is "
6834 "less than the requested DMA memory "
6835 "size (x%x)\n", alloc_len, req_len);
6836 rc = -ENOMEM;
6837 goto err_exit;
6838 }
6839 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6840 if (unlikely(rc)) {
6841 rc = -EIO;
6842 goto err_exit;
6843 }
6844
6845 if (!phba->sli4_hba.intr_enable)
6846 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6847 else {
James Smarta183a152011-10-10 21:32:43 -04006848 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006849 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6850 }
6851
6852 if (unlikely(rc)) {
6853 rc = -EIO;
6854 goto err_exit;
6855 }
6856
6857 /*
6858 * Figure out where the response is located. Then get local pointers
6859 * to the response data. The port does not guarantee to respond to
6860 * all extents counts request so update the local variable with the
6861 * allocated count from the port.
6862 */
6863 if (emb == LPFC_SLI4_MBX_EMBED) {
6864 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6865 shdr = &rsrc_ext->header.cfg_shdr;
6866 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6867 } else {
6868 virtaddr = mbox->sge_array->addr[0];
6869 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6870 shdr = &n_rsrc->cfg_shdr;
6871 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6872 }
6873
6874 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6875 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6876 "2984 Failed to read allocated resources "
6877 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6878 type,
6879 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6880 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6881 rc = -EIO;
6882 goto err_exit;
6883 }
6884 err_exit:
6885 lpfc_sli4_mbox_cmd_free(phba, mbox);
6886 return rc;
6887}
6888
6889/**
James Smart0ef69962017-04-21 16:04:50 -07006890 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006891 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006892 * @pring: Pointer to driver SLI ring object.
6893 * @sgl_list: linked link of sgl buffers to post
6894 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006895 *
James Smart895427b2017-02-12 13:52:30 -08006896 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006897 * repost them to the port by using SGL block post. This is needed after a
6898 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006899 * of buffer sgls which contains contiguous xris and uses the non-embedded
6900 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006901 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6902 * mailbox command for posting.
6903 *
6904 * Returns: 0 = success, non-zero failure.
6905 **/
6906static int
James Smart895427b2017-02-12 13:52:30 -08006907lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6908 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006909{
6910 struct lpfc_sglq *sglq_entry = NULL;
6911 struct lpfc_sglq *sglq_entry_next = NULL;
6912 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006913 int status, total_cnt;
6914 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006915 int last_xritag = NO_XRI;
6916 LIST_HEAD(prep_sgl_list);
6917 LIST_HEAD(blck_sgl_list);
6918 LIST_HEAD(allc_sgl_list);
6919 LIST_HEAD(post_sgl_list);
6920 LIST_HEAD(free_sgl_list);
6921
James Smart38c20672013-03-01 16:37:44 -05006922 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006923 spin_lock(&phba->sli4_hba.sgl_list_lock);
6924 list_splice_init(sgl_list, &allc_sgl_list);
6925 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006926 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006927
James Smart895427b2017-02-12 13:52:30 -08006928 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006929 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6930 &allc_sgl_list, list) {
6931 list_del_init(&sglq_entry->list);
6932 block_cnt++;
6933 if ((last_xritag != NO_XRI) &&
6934 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6935 /* a hole in xri block, form a sgl posting block */
6936 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6937 post_cnt = block_cnt - 1;
6938 /* prepare list for next posting block */
6939 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6940 block_cnt = 1;
6941 } else {
6942 /* prepare list for next posting block */
6943 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6944 /* enough sgls for non-embed sgl mbox command */
6945 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6946 list_splice_init(&prep_sgl_list,
6947 &blck_sgl_list);
6948 post_cnt = block_cnt;
6949 block_cnt = 0;
6950 }
6951 }
6952 num_posted++;
6953
6954 /* keep track of last sgl's xritag */
6955 last_xritag = sglq_entry->sli4_xritag;
6956
James Smart895427b2017-02-12 13:52:30 -08006957 /* end of repost sgl list condition for buffers */
6958 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006959 if (post_cnt == 0) {
6960 list_splice_init(&prep_sgl_list,
6961 &blck_sgl_list);
6962 post_cnt = block_cnt;
6963 } else if (block_cnt == 1) {
6964 status = lpfc_sli4_post_sgl(phba,
6965 sglq_entry->phys, 0,
6966 sglq_entry->sli4_xritag);
6967 if (!status) {
6968 /* successful, put sgl to posted list */
6969 list_add_tail(&sglq_entry->list,
6970 &post_sgl_list);
6971 } else {
6972 /* Failure, put sgl to free list */
6973 lpfc_printf_log(phba, KERN_WARNING,
6974 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006975 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006976 "sgl, xritag:x%x\n",
6977 sglq_entry->sli4_xritag);
6978 list_add_tail(&sglq_entry->list,
6979 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006980 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006981 }
6982 }
6983 }
6984
6985 /* continue until a nembed page worth of sgls */
6986 if (post_cnt == 0)
6987 continue;
6988
James Smart895427b2017-02-12 13:52:30 -08006989 /* post the buffer list sgls as a block */
6990 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6991 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006992
6993 if (!status) {
6994 /* success, put sgl list to posted sgl list */
6995 list_splice_init(&blck_sgl_list, &post_sgl_list);
6996 } else {
6997 /* Failure, put sgl list to free sgl list */
6998 sglq_entry_first = list_first_entry(&blck_sgl_list,
6999 struct lpfc_sglq,
7000 list);
7001 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007002 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04007003 "xritag:x%x-x%x\n",
7004 sglq_entry_first->sli4_xritag,
7005 (sglq_entry_first->sli4_xritag +
7006 post_cnt - 1));
7007 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04007008 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007009 }
7010
7011 /* don't reset xirtag due to hole in xri block */
7012 if (block_cnt == 0)
7013 last_xritag = NO_XRI;
7014
James Smart895427b2017-02-12 13:52:30 -08007015 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04007016 post_cnt = 0;
7017 }
7018
James Smart895427b2017-02-12 13:52:30 -08007019 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04007020 lpfc_free_sgl_list(phba, &free_sgl_list);
7021
James Smart895427b2017-02-12 13:52:30 -08007022 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04007023 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05007024 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007025 spin_lock(&phba->sli4_hba.sgl_list_lock);
7026 list_splice_init(&post_sgl_list, sgl_list);
7027 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05007028 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04007029 } else {
7030 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007031 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04007032 return -EIO;
7033 }
James Smart895427b2017-02-12 13:52:30 -08007034
7035 /* return the number of XRIs actually posted */
7036 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007037}
7038
James Smart0794d602019-01-28 11:14:19 -08007039/**
James Smart5e5b5112019-01-28 11:14:22 -08007040 * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
James Smart0794d602019-01-28 11:14:19 -08007041 * @phba: pointer to lpfc hba data structure.
7042 *
7043 * This routine walks the list of nvme buffers that have been allocated and
7044 * repost them to the port by using SGL block post. This is needed after a
7045 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7046 * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
James Smart5e5b5112019-01-28 11:14:22 -08007047 * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
James Smart0794d602019-01-28 11:14:19 -08007048 *
7049 * Returns: 0 = success, non-zero failure.
7050 **/
7051int
James Smart5e5b5112019-01-28 11:14:22 -08007052lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
James Smart0794d602019-01-28 11:14:19 -08007053{
7054 LIST_HEAD(post_nblist);
7055 int num_posted, rc = 0;
7056
7057 /* get all NVME buffers need to repost to a local list */
James Smart5e5b5112019-01-28 11:14:22 -08007058 lpfc_io_buf_flush(phba, &post_nblist);
James Smart0794d602019-01-28 11:14:19 -08007059
7060 /* post the list of nvme buffer sgls to port if available */
7061 if (!list_empty(&post_nblist)) {
James Smart5e5b5112019-01-28 11:14:22 -08007062 num_posted = lpfc_sli4_post_io_sgl_list(
7063 phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
James Smart0794d602019-01-28 11:14:19 -08007064 /* failed to post any nvme buffer, return error */
7065 if (num_posted == 0)
7066 rc = -EIO;
7067 }
7068 return rc;
7069}
7070
James Smart61bda8f2016-10-13 15:06:05 -07007071void
7072lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7073{
7074 uint32_t len;
7075
7076 len = sizeof(struct lpfc_mbx_set_host_data) -
7077 sizeof(struct lpfc_sli4_cfg_mhdr);
7078 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7079 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7080 LPFC_SLI4_MBX_EMBED);
7081
7082 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08007083 mbox->u.mqe.un.set_host_data.param_len =
7084 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07007085 snprintf(mbox->u.mqe.un.set_host_data.data,
7086 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7087 "Linux %s v"LPFC_DRIVER_VERSION,
7088 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7089}
7090
James Smarta8cf5df2017-05-15 15:20:46 -07007091int
James Smart6c621a22017-05-15 15:20:45 -07007092lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07007093 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07007094{
7095 int rc, i;
7096 struct lpfc_rqe hrqe;
7097 struct lpfc_rqe drqe;
7098 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08007099 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07007100 struct rqb_dmabuf *rqb_buffer;
7101 LIST_HEAD(rqb_buf_list);
7102
James Smart411de512018-01-30 15:58:52 -08007103 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007104 rqbp = hrq->rqbp;
7105 for (i = 0; i < count; i++) {
7106 /* IF RQ is already full, don't bother */
7107 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
7108 break;
7109 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7110 if (!rqb_buffer)
7111 break;
7112 rqb_buffer->hrq = hrq;
7113 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07007114 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07007115 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7116 }
7117 while (!list_empty(&rqb_buf_list)) {
7118 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7119 hbuf.list);
7120
7121 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7122 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7123 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7124 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7125 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7126 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08007127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7128 "6421 Cannot post to HRQ %d: %x %x %x "
7129 "DRQ %x %x\n",
7130 hrq->queue_id,
7131 hrq->host_index,
7132 hrq->hba_index,
7133 hrq->entry_count,
7134 drq->host_index,
7135 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07007136 rqbp->rqb_free_buffer(phba, rqb_buffer);
7137 } else {
7138 list_add_tail(&rqb_buffer->hbuf.list,
7139 &rqbp->rqb_buffer_list);
7140 rqbp->buffer_count++;
7141 }
7142 }
James Smart411de512018-01-30 15:58:52 -08007143 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007144 return 1;
7145}
7146
James Smart8a9d2e82012-05-09 21:16:12 -04007147/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08007148 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04007149 * @phba: Pointer to HBA context object.
7150 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08007151 * This function is the main SLI4 device initialization PCI function. This
7152 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04007153 * HBA error attention handler code. Caller is not required to hold any
7154 * locks.
7155 **/
7156int
7157lpfc_sli4_hba_setup(struct lpfc_hba *phba)
7158{
James Smartc4908502019-01-28 11:14:28 -08007159 int rc, i, cnt, len;
James Smartda0436e2009-05-22 14:51:39 -04007160 LPFC_MBOXQ_t *mboxq;
7161 struct lpfc_mqe *mqe;
7162 uint8_t *vpd;
7163 uint32_t vpd_size;
7164 uint32_t ftr_rsp = 0;
7165 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
7166 struct lpfc_vport *vport = phba->pport;
7167 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08007168 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04007169
7170 /* Perform a PCI function reset to start from clean */
7171 rc = lpfc_pci_function_reset(phba);
7172 if (unlikely(rc))
7173 return -ENODEV;
7174
7175 /* Check the HBA Host Status Register for readyness */
7176 rc = lpfc_sli4_post_status_check(phba);
7177 if (unlikely(rc))
7178 return -ENODEV;
7179 else {
7180 spin_lock_irq(&phba->hbalock);
7181 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
7182 spin_unlock_irq(&phba->hbalock);
7183 }
7184
7185 /*
7186 * Allocate a single mailbox container for initializing the
7187 * port.
7188 */
7189 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7190 if (!mboxq)
7191 return -ENOMEM;
7192
James Smartda0436e2009-05-22 14:51:39 -04007193 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04007194 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04007195 vpd = kzalloc(vpd_size, GFP_KERNEL);
7196 if (!vpd) {
7197 rc = -ENOMEM;
7198 goto out_free_mbox;
7199 }
7200
7201 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05007202 if (unlikely(rc)) {
7203 kfree(vpd);
7204 goto out_free_mbox;
7205 }
James Smart572709e2013-07-15 18:32:43 -04007206
James Smartda0436e2009-05-22 14:51:39 -04007207 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04007208 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07007209 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05007210 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007211 phba->fcp_embed_io = 0; /* SLI4 FC support only */
7212 } else {
James Smart76a95d72010-11-20 23:11:48 -05007213 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007214 }
James Smart45ed1192009-10-02 15:17:02 -04007215
7216 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
7217 LPFC_DCBX_CEE_MODE)
7218 phba->hba_flag |= HBA_FIP_SUPPORT;
7219 else
7220 phba->hba_flag &= ~HBA_FIP_SUPPORT;
7221
James Smart4f2e66c2012-05-09 21:17:07 -04007222 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
7223
James Smartc31098c2011-04-16 11:03:33 -04007224 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007225 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7226 "0376 READ_REV Error. SLI Level %d "
7227 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05007228 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04007229 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007230 kfree(vpd);
7231 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007232 }
James Smartcd1c8302011-10-10 21:33:25 -04007233
7234 /*
James Smartff78d8f2011-12-13 13:21:35 -05007235 * Continue initialization with default values even if driver failed
7236 * to read FCoE param config regions, only read parameters if the
7237 * board is FCoE
7238 */
7239 if (phba->hba_flag & HBA_FCOE_MODE &&
7240 lpfc_sli4_read_fcoe_params(phba))
7241 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
7242 "2570 Failed to read FCoE parameters\n");
7243
7244 /*
James Smartcd1c8302011-10-10 21:33:25 -04007245 * Retrieve sli4 device physical port name, failure of doing it
7246 * is considered as non-fatal.
7247 */
7248 rc = lpfc_sli4_retrieve_pport_name(phba);
7249 if (!rc)
7250 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7251 "3080 Successful retrieving SLI4 device "
7252 "physical port name: %s.\n", phba->Port);
7253
James Smartda0436e2009-05-22 14:51:39 -04007254 /*
7255 * Evaluate the read rev and vpd data. Populate the driver
7256 * state with the results. If this routine fails, the failure
7257 * is not fatal as the driver will use generic values.
7258 */
7259 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
7260 if (unlikely(!rc)) {
7261 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7262 "0377 Error %d parsing vpd. "
7263 "Using defaults.\n", rc);
7264 rc = 0;
7265 }
James Smart76a95d72010-11-20 23:11:48 -05007266 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04007267
James Smartf1126682009-06-10 17:22:44 -04007268 /* Save information as VPD data */
7269 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
7270 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
James Smart4e565cf2018-02-22 08:18:50 -08007271
7272 /*
7273 * This is because first G7 ASIC doesn't support the standard
7274 * 0x5a NVME cmd descriptor type/subtype
7275 */
7276 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7277 LPFC_SLI_INTF_IF_TYPE_6) &&
7278 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
7279 (phba->vpd.rev.smRev == 0) &&
7280 (phba->cfg_nvme_embed_cmd == 1))
7281 phba->cfg_nvme_embed_cmd = 0;
7282
James Smartf1126682009-06-10 17:22:44 -04007283 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
7284 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
7285 &mqe->un.read_rev);
7286 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
7287 &mqe->un.read_rev);
7288 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
7289 &mqe->un.read_rev);
7290 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
7291 &mqe->un.read_rev);
7292 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
7293 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
7294 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
7295 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
7296 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
7297 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
7298 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7299 "(%d):0380 READ_REV Status x%x "
7300 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
7301 mboxq->vport ? mboxq->vport->vpi : 0,
7302 bf_get(lpfc_mqe_status, mqe),
7303 phba->vpd.rev.opFwName,
7304 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
7305 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04007306
James Smart572709e2013-07-15 18:32:43 -04007307 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
7308 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
7309 if (phba->pport->cfg_lun_queue_depth > rc) {
7310 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7311 "3362 LUN queue depth changed from %d to %d\n",
7312 phba->pport->cfg_lun_queue_depth, rc);
7313 phba->pport->cfg_lun_queue_depth = rc;
7314 }
7315
James Smart65791f12016-07-06 12:35:56 -07007316 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07007317 LPFC_SLI_INTF_IF_TYPE_0) {
7318 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
7319 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7320 if (rc == MBX_SUCCESS) {
7321 phba->hba_flag |= HBA_RECOVERABLE_UE;
7322 /* Set 1Sec interval to detect UE */
7323 phba->eratt_poll_interval = 1;
7324 phba->sli4_hba.ue_to_sr = bf_get(
7325 lpfc_mbx_set_feature_UESR,
7326 &mboxq->u.mqe.un.set_feature);
7327 phba->sli4_hba.ue_to_rp = bf_get(
7328 lpfc_mbx_set_feature_UERP,
7329 &mboxq->u.mqe.un.set_feature);
7330 }
7331 }
7332
7333 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
7334 /* Enable MDS Diagnostics only if the SLI Port supports it */
7335 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
7336 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7337 if (rc != MBX_SUCCESS)
7338 phba->mds_diags_support = 0;
7339 }
James Smart572709e2013-07-15 18:32:43 -04007340
James Smartda0436e2009-05-22 14:51:39 -04007341 /*
7342 * Discover the port's supported feature set and match it against the
7343 * hosts requests.
7344 */
7345 lpfc_request_features(phba, mboxq);
7346 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7347 if (unlikely(rc)) {
7348 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007349 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007350 }
7351
7352 /*
7353 * The port must support FCP initiator mode as this is the
7354 * only mode running in the host.
7355 */
7356 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7357 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7358 "0378 No support for fcpi mode.\n");
7359 ftr_rsp++;
7360 }
James Smart0bc2b7c2018-02-22 08:18:48 -08007361
7362 /* Performance Hints are ONLY for FCoE */
7363 if (phba->hba_flag & HBA_FCOE_MODE) {
7364 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7365 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7366 else
7367 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7368 }
7369
James Smartda0436e2009-05-22 14:51:39 -04007370 /*
7371 * If the port cannot support the host's requested features
7372 * then turn off the global config parameters to disable the
7373 * feature in the driver. This is not a fatal error.
7374 */
James Smartf44ac122018-03-05 12:04:08 -08007375 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7376 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7377 phba->cfg_enable_bg = 0;
7378 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smartbf086112011-08-21 21:48:13 -04007379 ftr_rsp++;
James Smartf44ac122018-03-05 12:04:08 -08007380 }
James Smartbf086112011-08-21 21:48:13 -04007381 }
James Smartda0436e2009-05-22 14:51:39 -04007382
7383 if (phba->max_vpi && phba->cfg_enable_npiv &&
7384 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7385 ftr_rsp++;
7386
7387 if (ftr_rsp) {
7388 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7389 "0379 Feature Mismatch Data: x%08x %08x "
7390 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7391 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7392 phba->cfg_enable_npiv, phba->max_vpi);
7393 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7394 phba->cfg_enable_bg = 0;
7395 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7396 phba->cfg_enable_npiv = 0;
7397 }
7398
7399 /* These SLI3 features are assumed in SLI4 */
7400 spin_lock_irq(&phba->hbalock);
7401 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7402 spin_unlock_irq(&phba->hbalock);
7403
James Smart6d368e52011-05-24 11:44:12 -04007404 /*
7405 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
7406 * calls depends on these resources to complete port setup.
7407 */
7408 rc = lpfc_sli4_alloc_resource_identifiers(phba);
7409 if (rc) {
7410 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7411 "2920 Failed to alloc Resource IDs "
7412 "rc = x%x\n", rc);
7413 goto out_free_mbox;
7414 }
7415
James Smart61bda8f2016-10-13 15:06:05 -07007416 lpfc_set_host_data(phba, mboxq);
7417
7418 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7419 if (rc) {
7420 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7421 "2134 Failed to set host os driver version %x",
7422 rc);
7423 }
7424
James Smartda0436e2009-05-22 14:51:39 -04007425 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05007426 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7427 if (rc) {
7428 phba->link_state = LPFC_HBA_ERROR;
7429 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05007430 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05007431 }
7432
James Smartda0436e2009-05-22 14:51:39 -04007433 mboxq->vport = vport;
7434 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart3e1f0712018-11-29 16:09:29 -08007435 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04007436 if (rc == MBX_SUCCESS) {
7437 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7438 rc = 0;
7439 }
7440
7441 /*
7442 * This memory was allocated by the lpfc_read_sparam routine. Release
7443 * it to the mbuf pool.
7444 */
7445 lpfc_mbuf_free(phba, mp->virt, mp->phys);
7446 kfree(mp);
James Smart3e1f0712018-11-29 16:09:29 -08007447 mboxq->ctx_buf = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007448 if (unlikely(rc)) {
7449 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7450 "0382 READ_SPARAM command failed "
7451 "status %d, mbxStatus x%x\n",
7452 rc, bf_get(lpfc_mqe_status, mqe));
7453 phba->link_state = LPFC_HBA_ERROR;
7454 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007455 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007456 }
7457
James Smart05580562011-05-24 11:40:48 -04007458 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04007459
7460 /* Update the fc_host data structures with new wwn. */
7461 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7462 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7463
James Smart895427b2017-02-12 13:52:30 -08007464 /* Create all the SLI4 queues */
7465 rc = lpfc_sli4_queue_create(phba);
7466 if (rc) {
7467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7468 "3089 Failed to allocate queues\n");
7469 rc = -ENODEV;
7470 goto out_free_mbox;
7471 }
7472 /* Set up all the queues to the device */
7473 rc = lpfc_sli4_queue_setup(phba);
7474 if (unlikely(rc)) {
7475 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7476 "0381 Error %d during queue setup.\n ", rc);
7477 goto out_stop_timers;
7478 }
7479 /* Initialize the driver internal SLI layer lists. */
7480 lpfc_sli4_setup(phba);
7481 lpfc_sli4_queue_init(phba);
7482
7483 /* update host els xri-sgl sizes and mappings */
7484 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007485 if (unlikely(rc)) {
7486 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7487 "1400 Failed to update xri-sgl size and "
7488 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08007489 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007490 }
7491
James Smart8a9d2e82012-05-09 21:16:12 -04007492 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08007493 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7494 phba->sli4_hba.els_xri_cnt);
7495 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04007496 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7497 "0582 Error %d during els sgl post "
7498 "operation\n", rc);
7499 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007500 goto out_destroy_queue;
7501 }
7502 phba->sli4_hba.els_xri_cnt = rc;
7503
James Smartf358dd02017-02-12 13:52:34 -08007504 if (phba->nvmet_support) {
7505 /* update host nvmet xri-sgl sizes and mappings */
7506 rc = lpfc_sli4_nvmet_sgl_update(phba);
7507 if (unlikely(rc)) {
7508 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7509 "6308 Failed to update nvmet-sgl size "
7510 "and mapping: %d\n", rc);
7511 goto out_destroy_queue;
7512 }
7513
7514 /* register the nvmet sgl pool to the port */
7515 rc = lpfc_sli4_repost_sgl_list(
7516 phba,
7517 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7518 phba->sli4_hba.nvmet_xri_cnt);
7519 if (unlikely(rc < 0)) {
7520 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7521 "3117 Error %d during nvmet "
7522 "sgl post\n", rc);
7523 rc = -ENODEV;
7524 goto out_destroy_queue;
7525 }
7526 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07007527
7528 cnt = phba->cfg_iocb_cnt * 1024;
7529 /* We need 1 iocbq for every SGL, for IO processing */
7530 cnt += phba->sli4_hba.nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08007531 } else {
James Smart0794d602019-01-28 11:14:19 -08007532 /* update host common xri-sgl sizes and mappings */
James Smart5e5b5112019-01-28 11:14:22 -08007533 rc = lpfc_sli4_io_sgl_update(phba);
James Smart895427b2017-02-12 13:52:30 -08007534 if (unlikely(rc)) {
7535 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7536 "6082 Failed to update nvme-sgl size "
7537 "and mapping: %d\n", rc);
7538 goto out_destroy_queue;
7539 }
James Smart6c621a22017-05-15 15:20:45 -07007540
James Smart0794d602019-01-28 11:14:19 -08007541 /* register the allocated common sgl pool to the port */
James Smart5e5b5112019-01-28 11:14:22 -08007542 rc = lpfc_sli4_repost_io_sgl_list(phba);
James Smart0794d602019-01-28 11:14:19 -08007543 if (unlikely(rc)) {
7544 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7545 "6116 Error %d during nvme sgl post "
7546 "operation\n", rc);
7547 /* Some NVME buffers were moved to abort nvme list */
7548 /* A pci function reset will repost them */
7549 rc = -ENODEV;
7550 goto out_destroy_queue;
7551 }
James Smart6c621a22017-05-15 15:20:45 -07007552 cnt = phba->cfg_iocb_cnt * 1024;
James Smart11e644e2017-06-15 22:56:48 -07007553 }
7554
7555 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07007556 /* Initialize and populate the iocb list per host */
7557 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007558 "2821 initialize iocb list %d total %d\n",
James Smart6c621a22017-05-15 15:20:45 -07007559 phba->cfg_iocb_cnt, cnt);
7560 rc = lpfc_init_iocb_list(phba, cnt);
7561 if (rc) {
7562 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007563 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07007564 goto out_destroy_queue;
7565 }
James Smart8a9d2e82012-05-09 21:16:12 -04007566 }
7567
James Smart11e644e2017-06-15 22:56:48 -07007568 if (phba->nvmet_support)
7569 lpfc_nvmet_create_targetport(phba);
7570
James Smart2d7dbc42017-02-12 13:52:35 -08007571 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007572 /* Post initial buffers to all RQs created */
7573 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7574 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7575 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7576 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7577 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007578 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007579 rqbp->buffer_count = 0;
7580
James Smart2d7dbc42017-02-12 13:52:35 -08007581 lpfc_post_rq_buffer(
7582 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7583 phba->sli4_hba.nvmet_mrq_data[i],
James Smart2448e482018-04-09 14:24:24 -07007584 phba->cfg_nvmet_mrq_post, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007585 }
7586 }
7587
James Smartda0436e2009-05-22 14:51:39 -04007588 /* Post the rpi header region to the device. */
7589 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7590 if (unlikely(rc)) {
7591 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7592 "0393 Error %d during rpi post operation\n",
7593 rc);
7594 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007595 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007596 }
James Smart97f2ecf2012-03-01 22:35:23 -05007597 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007598
James Smart895427b2017-02-12 13:52:30 -08007599 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007600 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007601 /*
7602 * The FC Port needs to register FCFI (index 0)
7603 */
7604 lpfc_reg_fcfi(phba, mboxq);
7605 mboxq->vport = phba->pport;
7606 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7607 if (rc != MBX_SUCCESS)
7608 goto out_unset_queue;
7609 rc = 0;
7610 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7611 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007612 } else {
7613 /* We are a NVME Target mode with MRQ > 1 */
7614
7615 /* First register the FCFI */
7616 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7617 mboxq->vport = phba->pport;
7618 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7619 if (rc != MBX_SUCCESS)
7620 goto out_unset_queue;
7621 rc = 0;
7622 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7623 &mboxq->u.mqe.un.reg_fcfi_mrq);
7624
7625 /* Next register the MRQs */
7626 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7627 mboxq->vport = phba->pport;
7628 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7629 if (rc != MBX_SUCCESS)
7630 goto out_unset_queue;
7631 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007632 }
7633 /* Check if the port is configured to be disabled */
7634 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007635 }
7636
James Smartc4908502019-01-28 11:14:28 -08007637 /* Don't post more new bufs if repost already recovered
7638 * the nvme sgls.
7639 */
7640 if (phba->nvmet_support == 0) {
7641 if (phba->sli4_hba.io_xri_cnt == 0) {
7642 len = lpfc_new_io_buf(
7643 phba, phba->sli4_hba.io_xri_max);
7644 if (len == 0) {
7645 rc = -ENOMEM;
7646 goto out_unset_queue;
7647 }
7648
7649 if (phba->cfg_xri_rebalancing)
7650 lpfc_create_multixri_pools(phba);
7651 }
7652 } else {
7653 phba->cfg_xri_rebalancing = 0;
7654 }
7655
James Smartda0436e2009-05-22 14:51:39 -04007656 /* Arm the CQs and then EQs on device */
7657 lpfc_sli4_arm_cqeq_intr(phba);
7658
7659 /* Indicate device interrupt mode */
7660 phba->sli4_hba.intr_enable = 1;
7661
7662 /* Allow asynchronous mailbox command to go through */
7663 spin_lock_irq(&phba->hbalock);
7664 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7665 spin_unlock_irq(&phba->hbalock);
7666
7667 /* Post receive buffers to the device */
7668 lpfc_sli4_rb_setup(phba);
7669
James Smartfc2b9892010-02-26 14:15:29 -05007670 /* Reset HBA FCF states after HBA reset */
7671 phba->fcf.fcf_flag = 0;
7672 phba->fcf.current_rec.flag = 0;
7673
James Smartda0436e2009-05-22 14:51:39 -04007674 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007675 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007676 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007677
7678 /* Start heart beat timer */
7679 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007680 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007681 phba->hb_outstanding = 0;
7682 phba->last_completion_time = jiffies;
7683
James Smart32517fc2019-01-28 11:14:33 -08007684 /* start eq_delay heartbeat */
7685 if (phba->cfg_auto_imax)
7686 queue_delayed_work(phba->wq, &phba->eq_delay_work,
7687 msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
7688
James Smartda0436e2009-05-22 14:51:39 -04007689 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007690 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007691 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007692
James Smart75baf692010-06-08 18:31:21 -04007693 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7694 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7695 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7696 if (!rc) {
7697 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7698 "2829 This device supports "
7699 "Advanced Error Reporting (AER)\n");
7700 spin_lock_irq(&phba->hbalock);
7701 phba->hba_flag |= HBA_AER_ENABLED;
7702 spin_unlock_irq(&phba->hbalock);
7703 } else {
7704 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7705 "2830 This device does not support "
7706 "Advanced Error Reporting (AER)\n");
7707 phba->cfg_aer_support = 0;
7708 }
James Smart0a96e972011-07-22 18:37:28 -04007709 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007710 }
7711
James Smartda0436e2009-05-22 14:51:39 -04007712 /*
7713 * The port is ready, set the host's link state to LINK_DOWN
7714 * in preparation for link interrupts.
7715 */
James Smartda0436e2009-05-22 14:51:39 -04007716 spin_lock_irq(&phba->hbalock);
7717 phba->link_state = LPFC_LINK_DOWN;
James Smart1dc5ec22018-10-23 13:41:11 -07007718
7719 /* Check if physical ports are trunked */
7720 if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
7721 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
7722 if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
7723 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
7724 if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
7725 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
7726 if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
7727 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04007728 spin_unlock_irq(&phba->hbalock);
James Smart1dc5ec22018-10-23 13:41:11 -07007729
James Smart026abb82011-12-13 13:20:45 -05007730 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7731 (phba->hba_flag & LINK_DISABLED)) {
7732 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7733 "3103 Adapter Link is disabled.\n");
7734 lpfc_down_link(phba, mboxq);
7735 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7736 if (rc != MBX_SUCCESS) {
7737 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7738 "3104 Adapter failed to issue "
7739 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
James Smartc4908502019-01-28 11:14:28 -08007740 goto out_io_buff_free;
James Smart026abb82011-12-13 13:20:45 -05007741 }
7742 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007743 /* don't perform init_link on SLI4 FC port loopback test */
7744 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7745 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7746 if (rc)
James Smartc4908502019-01-28 11:14:28 -08007747 goto out_io_buff_free;
James Smart1b511972011-12-13 13:23:09 -05007748 }
James Smart5350d872011-10-10 21:33:49 -04007749 }
7750 mempool_free(mboxq, phba->mbox_mem_pool);
7751 return rc;
James Smartc4908502019-01-28 11:14:28 -08007752out_io_buff_free:
7753 /* Free allocated IO Buffers */
7754 lpfc_io_free(phba);
James Smart76a95d72010-11-20 23:11:48 -05007755out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007756 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007757 lpfc_sli4_queue_unset(phba);
7758out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007759 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007760 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007761out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007762 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007763out_free_mbox:
7764 mempool_free(mboxq, phba->mbox_mem_pool);
7765 return rc;
7766}
James Smarte59058c2008-08-24 21:49:00 -04007767
7768/**
James Smart3621a712009-04-06 18:47:14 -04007769 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007770 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007771 *
James Smarte59058c2008-08-24 21:49:00 -04007772 * This is the callback function for mailbox timer. The mailbox
7773 * timer is armed when a new mailbox command is issued and the timer
7774 * is deleted when the mailbox complete. The function is called by
7775 * the kernel timer code when a mailbox does not complete within
7776 * expected time. This function wakes up the worker thread to
7777 * process the mailbox timeout and returns. All the processing is
7778 * done by the worker thread function lpfc_mbox_timeout_handler.
7779 **/
dea31012005-04-17 16:05:31 -05007780void
Kees Cookf22eb4d2017-09-06 20:24:26 -07007781lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05007782{
Kees Cookf22eb4d2017-09-06 20:24:26 -07007783 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05007784 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007785 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007786
James Smart2e0fef82007-06-17 19:56:36 -05007787 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007788 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007789 if (!tmo_posted)
7790 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7791 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7792
James Smart5e9d9b82008-06-14 22:52:53 -04007793 if (!tmo_posted)
7794 lpfc_worker_wake_up(phba);
7795 return;
dea31012005-04-17 16:05:31 -05007796}
7797
James Smarte8d3c3b2013-10-10 12:21:30 -04007798/**
7799 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7800 * are pending
7801 * @phba: Pointer to HBA context object.
7802 *
7803 * This function checks if any mailbox completions are present on the mailbox
7804 * completion queue.
7805 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007806static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007807lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7808{
7809
7810 uint32_t idx;
7811 struct lpfc_queue *mcq;
7812 struct lpfc_mcqe *mcqe;
7813 bool pending_completions = false;
James Smart7365f6f2018-02-22 08:18:46 -08007814 uint8_t qe_valid;
James Smarte8d3c3b2013-10-10 12:21:30 -04007815
7816 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7817 return false;
7818
7819 /* Check for completions on mailbox completion queue */
7820
7821 mcq = phba->sli4_hba.mbx_cq;
7822 idx = mcq->hba_index;
James Smart7365f6f2018-02-22 08:18:46 -08007823 qe_valid = mcq->qe_valid;
7824 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe) == qe_valid) {
James Smarte8d3c3b2013-10-10 12:21:30 -04007825 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7826 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7827 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7828 pending_completions = true;
7829 break;
7830 }
7831 idx = (idx + 1) % mcq->entry_count;
7832 if (mcq->hba_index == idx)
7833 break;
James Smart7365f6f2018-02-22 08:18:46 -08007834
7835 /* if the index wrapped around, toggle the valid bit */
7836 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7837 qe_valid = (qe_valid) ? 0 : 1;
James Smarte8d3c3b2013-10-10 12:21:30 -04007838 }
7839 return pending_completions;
7840
7841}
7842
7843/**
7844 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7845 * that were missed.
7846 * @phba: Pointer to HBA context object.
7847 *
7848 * For sli4, it is possible to miss an interrupt. As such mbox completions
7849 * maybe missed causing erroneous mailbox timeouts to occur. This function
7850 * checks to see if mbox completions are on the mailbox completion queue
7851 * and will process all the completions associated with the eq for the
7852 * mailbox completion queue.
7853 **/
7854bool
7855lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7856{
James Smartb71413d2018-02-22 08:18:40 -08007857 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04007858 uint32_t eqidx;
7859 struct lpfc_queue *fpeq = NULL;
James Smarte8d3c3b2013-10-10 12:21:30 -04007860 bool mbox_pending;
7861
7862 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7863 return false;
7864
7865 /* Find the eq associated with the mcq */
7866
James Smartcdb42be2019-01-28 11:14:21 -08007867 if (sli4_hba->hdwq)
James Smart6a828b02019-01-28 11:14:31 -08007868 for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++)
James Smartcdb42be2019-01-28 11:14:21 -08007869 if (sli4_hba->hdwq[eqidx].hba_eq->queue_id ==
James Smartb71413d2018-02-22 08:18:40 -08007870 sli4_hba->mbx_cq->assoc_qid) {
James Smartcdb42be2019-01-28 11:14:21 -08007871 fpeq = sli4_hba->hdwq[eqidx].hba_eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04007872 break;
7873 }
7874 if (!fpeq)
7875 return false;
7876
7877 /* Turn off interrupts from this EQ */
7878
James Smartb71413d2018-02-22 08:18:40 -08007879 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04007880
7881 /* Check to see if a mbox completion is pending */
7882
7883 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7884
7885 /*
7886 * If a mbox completion is pending, process all the events on EQ
7887 * associated with the mbox completion queue (this could include
7888 * mailbox commands, async events, els commands, receive queue data
7889 * and fcp commands)
7890 */
7891
7892 if (mbox_pending)
James Smart32517fc2019-01-28 11:14:33 -08007893 /* process and rearm the EQ */
7894 lpfc_sli4_process_eq(phba, fpeq);
7895 else
7896 /* Always clear and re-arm the EQ */
7897 sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04007898
7899 return mbox_pending;
7900
7901}
James Smarte59058c2008-08-24 21:49:00 -04007902
7903/**
James Smart3621a712009-04-06 18:47:14 -04007904 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007905 * @phba: Pointer to HBA context object.
7906 *
7907 * This function is called from worker thread when a mailbox command times out.
7908 * The caller is not required to hold any locks. This function will reset the
7909 * HBA and recover all the pending commands.
7910 **/
dea31012005-04-17 16:05:31 -05007911void
7912lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7913{
James Smart2e0fef82007-06-17 19:56:36 -05007914 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007915 MAILBOX_t *mb = NULL;
7916
James Smart1dcb58e2007-04-25 09:51:30 -04007917 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007918
James Smarte8d3c3b2013-10-10 12:21:30 -04007919 /* If the mailbox completed, process the completion and return */
7920 if (lpfc_sli4_process_missed_mbox_completions(phba))
7921 return;
7922
James Smarteb016562014-09-03 12:58:06 -04007923 if (pmbox != NULL)
7924 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007925 /* Check the pmbox pointer first. There is a race condition
7926 * between the mbox timeout handler getting executed in the
7927 * worklist and the mailbox actually completing. When this
7928 * race condition occurs, the mbox_active will be NULL.
7929 */
7930 spin_lock_irq(&phba->hbalock);
7931 if (pmbox == NULL) {
7932 lpfc_printf_log(phba, KERN_WARNING,
7933 LOG_MBOX | LOG_SLI,
7934 "0353 Active Mailbox cleared - mailbox timeout "
7935 "exiting\n");
7936 spin_unlock_irq(&phba->hbalock);
7937 return;
7938 }
7939
dea31012005-04-17 16:05:31 -05007940 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007941 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007942 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007943 mb->mbxCommand,
7944 phba->pport->port_state,
7945 phba->sli.sli_flag,
7946 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007947 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007948
James Smart1dcb58e2007-04-25 09:51:30 -04007949 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7950 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007951 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007952 */
James Smart2e0fef82007-06-17 19:56:36 -05007953 spin_lock_irq(&phba->pport->work_port_lock);
7954 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7955 spin_unlock_irq(&phba->pport->work_port_lock);
7956 spin_lock_irq(&phba->hbalock);
7957 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007958 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007959 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007960
James Smartdb55fba2014-04-04 13:52:02 -04007961 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007962
7963 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007964 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007965
7966 /* Reset the HBA device */
7967 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007968}
7969
James Smarte59058c2008-08-24 21:49:00 -04007970/**
James Smart3772a992009-05-22 14:50:54 -04007971 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007972 * @phba: Pointer to HBA context object.
7973 * @pmbox: Pointer to mailbox object.
7974 * @flag: Flag indicating how the mailbox need to be processed.
7975 *
7976 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007977 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7978 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007979 * The mailbox command can be submitted in polling mode, in which case
7980 * this function will wait in a polling loop for the completion of the
7981 * mailbox.
7982 * If the mailbox is submitted in no_wait mode (not polling) the
7983 * function will submit the command and returns immediately without waiting
7984 * for the mailbox completion. The no_wait is supported only when HBA
7985 * is in SLI2/SLI3 mode - interrupts are enabled.
7986 * The SLI interface allows only one mailbox pending at a time. If the
7987 * mailbox is issued in polling mode and there is already a mailbox
7988 * pending, then the function will return an error. If the mailbox is issued
7989 * in NO_WAIT mode and there is a mailbox pending already, the function
7990 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7991 * The sli layer owns the mailbox object until the completion of mailbox
7992 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7993 * return codes the caller owns the mailbox command after the return of
7994 * the function.
7995 **/
James Smart3772a992009-05-22 14:50:54 -04007996static int
7997lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7998 uint32_t flag)
dea31012005-04-17 16:05:31 -05007999{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008000 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05008001 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008002 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05008003 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008004 int i;
James Smart09372822008-01-11 01:52:54 -05008005 unsigned long timeout;
dea31012005-04-17 16:05:31 -05008006 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04008007 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05008008 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04008009 int processing_queue = 0;
8010
8011 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8012 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04008013 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04008014 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04008015 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8016 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8017 return MBX_SUCCESS;
8018 }
James Smart58da1ff2008-04-07 10:15:56 -04008019 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04008020 pmbox = lpfc_mbox_get(phba);
8021 if (!pmbox) {
8022 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8023 return MBX_SUCCESS;
8024 }
8025 }
dea31012005-04-17 16:05:31 -05008026
James Smarted957682007-06-17 19:56:37 -05008027 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05008028 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05008029 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04008030 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05008031 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05008032 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008033 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04008034 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05008035 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04008036 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05008037 }
8038 }
8039
Linas Vepstas8d63f372007-02-14 14:28:36 -06008040 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04008041 if (unlikely(pci_channel_offline(phba->pcidev))) {
8042 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8043 goto out_not_finished;
8044 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06008045
James Smarta257bf92009-04-06 18:48:10 -04008046 /* If HBA has a deferred error attention, fail the iocb. */
8047 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8048 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8049 goto out_not_finished;
8050 }
8051
dea31012005-04-17 16:05:31 -05008052 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05008053
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008054 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05008055 status = MBX_SUCCESS;
8056
James Smart2e0fef82007-06-17 19:56:36 -05008057 if (phba->link_state == LPFC_HBA_ERROR) {
8058 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05008059
8060 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008061 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8062 "(%d):0311 Mailbox command x%x cannot "
8063 "issue Data: x%x x%x\n",
8064 pmbox->vport ? pmbox->vport->vpi : 0,
8065 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008066 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05008067 }
8068
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008069 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05008070 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
8071 !(hc_copy & HC_MBINT_ENA)) {
8072 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8073 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04008074 "(%d):2528 Mailbox command x%x cannot "
8075 "issue Data: x%x x%x\n",
8076 pmbox->vport ? pmbox->vport->vpi : 0,
8077 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05008078 goto out_not_finished;
8079 }
James Smart92908312006-03-07 15:04:13 -05008080 }
8081
dea31012005-04-17 16:05:31 -05008082 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8083 /* Polling for a mbox command when another one is already active
8084 * is not allowed in SLI. Also, the driver must have established
8085 * SLI2 mode to queue and process multiple mbox commands.
8086 */
8087
8088 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05008089 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008090
8091 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008092 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8093 "(%d):2529 Mailbox command x%x "
8094 "cannot issue Data: x%x x%x\n",
8095 pmbox->vport ? pmbox->vport->vpi : 0,
8096 pmbox->u.mb.mbxCommand,
8097 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008098 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008099 }
8100
James Smart3772a992009-05-22 14:50:54 -04008101 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05008102 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008103 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008104 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8105 "(%d):2530 Mailbox command x%x "
8106 "cannot issue Data: x%x x%x\n",
8107 pmbox->vport ? pmbox->vport->vpi : 0,
8108 pmbox->u.mb.mbxCommand,
8109 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008110 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008111 }
8112
dea31012005-04-17 16:05:31 -05008113 /* Another mailbox command is still being processed, queue this
8114 * command to be processed later.
8115 */
8116 lpfc_mbox_put(phba, pmbox);
8117
8118 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05008119 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008120 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05008121 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008122 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07008123 mbx->mbxCommand,
8124 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008125 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008126
8127 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05008128 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008129
James Smart858c9f62007-06-17 19:56:39 -05008130 if (pmbox->vport) {
8131 lpfc_debugfs_disc_trc(pmbox->vport,
8132 LPFC_DISC_TRC_MBOX_VPORT,
8133 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008134 (uint32_t)mbx->mbxCommand,
8135 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008136 }
8137 else {
8138 lpfc_debugfs_disc_trc(phba->pport,
8139 LPFC_DISC_TRC_MBOX,
8140 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008141 (uint32_t)mbx->mbxCommand,
8142 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008143 }
8144
James Smart2e0fef82007-06-17 19:56:36 -05008145 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05008146 }
8147
dea31012005-04-17 16:05:31 -05008148 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8149
8150 /* If we are not polling, we MUST be in SLI2 mode */
8151 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04008152 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008153 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05008154 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008155 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008156 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008157 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8158 "(%d):2531 Mailbox command x%x "
8159 "cannot issue Data: x%x x%x\n",
8160 pmbox->vport ? pmbox->vport->vpi : 0,
8161 pmbox->u.mb.mbxCommand,
8162 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008163 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008164 }
8165 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04008166 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8167 1000);
8168 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05008169 }
8170
8171 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05008172 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008173 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05008174 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008175 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07008176 mbx->mbxCommand,
8177 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008178 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008179
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008180 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05008181 if (pmbox->vport) {
8182 lpfc_debugfs_disc_trc(pmbox->vport,
8183 LPFC_DISC_TRC_MBOX_VPORT,
8184 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008185 (uint32_t)mbx->mbxCommand,
8186 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008187 }
8188 else {
8189 lpfc_debugfs_disc_trc(phba->pport,
8190 LPFC_DISC_TRC_MBOX,
8191 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008192 (uint32_t)mbx->mbxCommand,
8193 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008194 }
8195 }
8196
dea31012005-04-17 16:05:31 -05008197 psli->slistat.mbox_cmd++;
8198 evtctr = psli->slistat.mbox_event;
8199
8200 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008201 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05008202
James Smart3772a992009-05-22 14:50:54 -04008203 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04008204 /* Populate mbox extension offset word. */
8205 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008206 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008207 = (uint8_t *)phba->mbox_ext
8208 - (uint8_t *)phba->mbox;
8209 }
8210
8211 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008212 if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
8213 lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
8214 (uint8_t *)phba->mbox_ext,
8215 pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008216 }
8217 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008218 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008219 } else {
James Smart7a470272010-03-15 11:25:20 -04008220 /* Populate mbox extension offset word. */
8221 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008222 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008223 = MAILBOX_HBA_EXT_OFFSET;
8224
8225 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008226 if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -04008227 lpfc_memcpy_to_slim(phba->MBslimaddr +
8228 MAILBOX_HBA_EXT_OFFSET,
James Smart3e1f0712018-11-29 16:09:29 -08008229 pmbox->ctx_buf, pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008230
James Smart895427b2017-02-12 13:52:30 -08008231 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008232 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08008233 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
8234 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008235
8236 /* First copy mbox command data to HBA SLIM, skip past first
8237 word */
8238 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008239 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05008240 MAILBOX_CMD_SIZE - sizeof (uint32_t));
8241
8242 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008243 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05008244 to_slim = phba->MBslimaddr;
8245 writel(ldata, to_slim);
8246 readl(to_slim); /* flush */
8247
James Smart895427b2017-02-12 13:52:30 -08008248 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008249 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04008250 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008251 }
8252
8253 wmb();
dea31012005-04-17 16:05:31 -05008254
8255 switch (flag) {
8256 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05008257 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05008258 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05008259 /* Interrupt board to do it */
8260 writel(CA_MBATT, phba->CAregaddr);
8261 readl(phba->CAregaddr); /* flush */
8262 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05008263 break;
8264
8265 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05008266 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05008267 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05008268 /* Interrupt board to do it */
8269 writel(CA_MBATT, phba->CAregaddr);
8270 readl(phba->CAregaddr); /* flush */
8271
James Smart3772a992009-05-22 14:50:54 -04008272 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008273 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04008274 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008275 word0 = le32_to_cpu(word0);
8276 } else {
8277 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05008278 if (lpfc_readl(phba->MBslimaddr, &word0)) {
8279 spin_unlock_irqrestore(&phba->hbalock,
8280 drvr_flag);
8281 goto out_not_finished;
8282 }
dea31012005-04-17 16:05:31 -05008283 }
8284
8285 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008286 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8287 spin_unlock_irqrestore(&phba->hbalock,
8288 drvr_flag);
8289 goto out_not_finished;
8290 }
James Smarta183a152011-10-10 21:32:43 -04008291 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8292 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05008293 i = 0;
dea31012005-04-17 16:05:31 -05008294 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05008295 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
8296 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05008297 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05008298 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05008299 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008300 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05008301 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04008302 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008303 }
8304
8305 /* Check if we took a mbox interrupt while we were
8306 polling */
8307 if (((word0 & OWN_CHIP) != OWN_CHIP)
8308 && (evtctr != psli->slistat.mbox_event))
8309 break;
8310
James Smart09372822008-01-11 01:52:54 -05008311 if (i++ > 10) {
8312 spin_unlock_irqrestore(&phba->hbalock,
8313 drvr_flag);
8314 msleep(1);
8315 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8316 }
dea31012005-04-17 16:05:31 -05008317
James Smart3772a992009-05-22 14:50:54 -04008318 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008319 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04008320 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008321 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008322 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05008323 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04008324 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05008325 /* Check real SLIM for any errors */
8326 slimword0 = readl(phba->MBslimaddr);
8327 slimmb = (MAILBOX_t *) & slimword0;
8328 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
8329 && slimmb->mbxStatus) {
8330 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04008331 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008332 word0 = slimword0;
8333 }
8334 }
8335 } else {
8336 /* First copy command data */
8337 word0 = readl(phba->MBslimaddr);
8338 }
8339 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008340 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8341 spin_unlock_irqrestore(&phba->hbalock,
8342 drvr_flag);
8343 goto out_not_finished;
8344 }
dea31012005-04-17 16:05:31 -05008345 }
8346
James Smart3772a992009-05-22 14:50:54 -04008347 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008348 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08008349 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
8350 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008351 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008352 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
James Smart7a470272010-03-15 11:25:20 -04008353 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -08008354 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008355 pmbox->out_ext_byte_len);
8356 }
dea31012005-04-17 16:05:31 -05008357 } else {
8358 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008359 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08008360 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008361 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008362 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
8363 lpfc_memcpy_from_slim(
8364 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008365 phba->MBslimaddr +
8366 MAILBOX_HBA_EXT_OFFSET,
8367 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05008368 }
8369 }
8370
8371 writel(HA_MBATT, phba->HAregaddr);
8372 readl(phba->HAregaddr); /* flush */
8373
8374 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008375 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05008376 }
8377
James Smart2e0fef82007-06-17 19:56:36 -05008378 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8379 return status;
James Smart58da1ff2008-04-07 10:15:56 -04008380
8381out_not_finished:
8382 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04008383 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04008384 lpfc_mbox_cmpl_put(phba, pmbox);
8385 }
8386 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008387}
8388
James Smarte59058c2008-08-24 21:49:00 -04008389/**
James Smartf1126682009-06-10 17:22:44 -04008390 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8391 * @phba: Pointer to HBA context object.
8392 *
8393 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8394 * the driver internal pending mailbox queue. It will then try to wait out the
8395 * possible outstanding mailbox command before return.
8396 *
8397 * Returns:
8398 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8399 * the outstanding mailbox command timed out.
8400 **/
8401static int
8402lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8403{
8404 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04008405 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04008406 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04008407
8408 /* Mark the asynchronous mailbox command posting as blocked */
8409 spin_lock_irq(&phba->hbalock);
8410 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04008411 /* Determine how long we might wait for the active mailbox
8412 * command to be gracefully completed by firmware.
8413 */
James Smarta183a152011-10-10 21:32:43 -04008414 if (phba->sli.mbox_active)
8415 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8416 phba->sli.mbox_active) *
8417 1000) + jiffies;
8418 spin_unlock_irq(&phba->hbalock);
8419
James Smarte8d3c3b2013-10-10 12:21:30 -04008420 /* Make sure the mailbox is really active */
8421 if (timeout)
8422 lpfc_sli4_process_missed_mbox_completions(phba);
8423
James Smartf1126682009-06-10 17:22:44 -04008424 /* Wait for the outstnading mailbox command to complete */
8425 while (phba->sli.mbox_active) {
8426 /* Check active mailbox complete status every 2ms */
8427 msleep(2);
8428 if (time_after(jiffies, timeout)) {
8429 /* Timeout, marked the outstanding cmd not complete */
8430 rc = 1;
8431 break;
8432 }
8433 }
8434
8435 /* Can not cleanly block async mailbox command, fails it */
8436 if (rc) {
8437 spin_lock_irq(&phba->hbalock);
8438 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8439 spin_unlock_irq(&phba->hbalock);
8440 }
8441 return rc;
8442}
8443
8444/**
8445 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8446 * @phba: Pointer to HBA context object.
8447 *
8448 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8449 * commands from the driver internal pending mailbox queue. It makes sure
8450 * that there is no outstanding mailbox command before resuming posting
8451 * asynchronous mailbox commands. If, for any reason, there is outstanding
8452 * mailbox command, it will try to wait it out before resuming asynchronous
8453 * mailbox command posting.
8454 **/
8455static void
8456lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8457{
8458 struct lpfc_sli *psli = &phba->sli;
8459
8460 spin_lock_irq(&phba->hbalock);
8461 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8462 /* Asynchronous mailbox posting is not blocked, do nothing */
8463 spin_unlock_irq(&phba->hbalock);
8464 return;
8465 }
8466
8467 /* Outstanding synchronous mailbox command is guaranteed to be done,
8468 * successful or timeout, after timing-out the outstanding mailbox
8469 * command shall always be removed, so just unblock posting async
8470 * mailbox command and resume
8471 */
8472 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8473 spin_unlock_irq(&phba->hbalock);
8474
8475 /* wake up worker thread to post asynchronlous mailbox command */
8476 lpfc_worker_wake_up(phba);
8477}
8478
8479/**
James Smart2d843ed2012-09-29 11:29:06 -04008480 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8481 * @phba: Pointer to HBA context object.
8482 * @mboxq: Pointer to mailbox object.
8483 *
8484 * The function waits for the bootstrap mailbox register ready bit from
8485 * port for twice the regular mailbox command timeout value.
8486 *
8487 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8488 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8489 **/
8490static int
8491lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8492{
8493 uint32_t db_ready;
8494 unsigned long timeout;
8495 struct lpfc_register bmbx_reg;
8496
8497 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8498 * 1000) + jiffies;
8499
8500 do {
8501 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8502 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8503 if (!db_ready)
8504 msleep(2);
8505
8506 if (time_after(jiffies, timeout))
8507 return MBXERR_ERROR;
8508 } while (!db_ready);
8509
8510 return 0;
8511}
8512
8513/**
James Smartda0436e2009-05-22 14:51:39 -04008514 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8515 * @phba: Pointer to HBA context object.
8516 * @mboxq: Pointer to mailbox object.
8517 *
8518 * The function posts a mailbox to the port. The mailbox is expected
8519 * to be comletely filled in and ready for the port to operate on it.
8520 * This routine executes a synchronous completion operation on the
8521 * mailbox by polling for its completion.
8522 *
8523 * The caller must not be holding any locks when calling this routine.
8524 *
8525 * Returns:
8526 * MBX_SUCCESS - mailbox posted successfully
8527 * Any of the MBX error values.
8528 **/
8529static int
8530lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8531{
8532 int rc = MBX_SUCCESS;
8533 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04008534 uint32_t mcqe_status;
8535 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04008536 struct lpfc_sli *psli = &phba->sli;
8537 struct lpfc_mqe *mb = &mboxq->u.mqe;
8538 struct lpfc_bmbx_create *mbox_rgn;
8539 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04008540
8541 /*
8542 * Only one mailbox can be active to the bootstrap mailbox region
8543 * at a time and there is no queueing provided.
8544 */
8545 spin_lock_irqsave(&phba->hbalock, iflag);
8546 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8547 spin_unlock_irqrestore(&phba->hbalock, iflag);
8548 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008549 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008550 "cannot issue Data: x%x x%x\n",
8551 mboxq->vport ? mboxq->vport->vpi : 0,
8552 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008553 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8554 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008555 psli->sli_flag, MBX_POLL);
8556 return MBXERR_ERROR;
8557 }
8558 /* The server grabs the token and owns it until release */
8559 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8560 phba->sli.mbox_active = mboxq;
8561 spin_unlock_irqrestore(&phba->hbalock, iflag);
8562
James Smart2d843ed2012-09-29 11:29:06 -04008563 /* wait for bootstrap mbox register for readyness */
8564 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8565 if (rc)
8566 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008567 /*
8568 * Initialize the bootstrap memory region to avoid stale data areas
8569 * in the mailbox post. Then copy the caller's mailbox contents to
8570 * the bmbx mailbox region.
8571 */
8572 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8573 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
James Smart48f8fdb2018-05-04 20:37:51 -07008574 lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8575 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008576
8577 /* Post the high mailbox dma address to the port and wait for ready. */
8578 dma_address = &phba->sli4_hba.bmbx.dma_address;
8579 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8580
James Smart2d843ed2012-09-29 11:29:06 -04008581 /* wait for bootstrap mbox register for hi-address write done */
8582 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8583 if (rc)
8584 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008585
8586 /* Post the low mailbox dma address to the port. */
8587 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008588
James Smart2d843ed2012-09-29 11:29:06 -04008589 /* wait for bootstrap mbox register for low address write done */
8590 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8591 if (rc)
8592 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008593
8594 /*
8595 * Read the CQ to ensure the mailbox has completed.
8596 * If so, update the mailbox status so that the upper layers
8597 * can complete the request normally.
8598 */
James Smart48f8fdb2018-05-04 20:37:51 -07008599 lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8600 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008601 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
James Smart48f8fdb2018-05-04 20:37:51 -07008602 lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8603 sizeof(struct lpfc_mcqe));
James Smartda0436e2009-05-22 14:51:39 -04008604 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008605 /*
8606 * When the CQE status indicates a failure and the mailbox status
8607 * indicates success then copy the CQE status into the mailbox status
8608 * (and prefix it with x4000).
8609 */
James Smartda0436e2009-05-22 14:51:39 -04008610 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008611 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8612 bf_set(lpfc_mqe_status, mb,
8613 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008614 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008615 } else
8616 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008617
8618 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008619 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008620 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8621 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008622 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8623 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8624 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008625 bf_get(lpfc_mqe_status, mb),
8626 mb->un.mb_words[0], mb->un.mb_words[1],
8627 mb->un.mb_words[2], mb->un.mb_words[3],
8628 mb->un.mb_words[4], mb->un.mb_words[5],
8629 mb->un.mb_words[6], mb->un.mb_words[7],
8630 mb->un.mb_words[8], mb->un.mb_words[9],
8631 mb->un.mb_words[10], mb->un.mb_words[11],
8632 mb->un.mb_words[12], mboxq->mcqe.word0,
8633 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8634 mboxq->mcqe.trailer);
8635exit:
8636 /* We are holding the token, no needed for lock when release */
8637 spin_lock_irqsave(&phba->hbalock, iflag);
8638 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8639 phba->sli.mbox_active = NULL;
8640 spin_unlock_irqrestore(&phba->hbalock, iflag);
8641 return rc;
8642}
8643
8644/**
8645 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8646 * @phba: Pointer to HBA context object.
8647 * @pmbox: Pointer to mailbox object.
8648 * @flag: Flag indicating how the mailbox need to be processed.
8649 *
8650 * This function is called by discovery code and HBA management code to submit
8651 * a mailbox command to firmware with SLI-4 interface spec.
8652 *
8653 * Return codes the caller owns the mailbox command after the return of the
8654 * function.
8655 **/
8656static int
8657lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8658 uint32_t flag)
8659{
8660 struct lpfc_sli *psli = &phba->sli;
8661 unsigned long iflags;
8662 int rc;
8663
James Smartb76f2dc2011-07-22 18:37:42 -04008664 /* dump from issue mailbox command if setup */
8665 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8666
James Smart8fa38512009-07-19 10:01:03 -04008667 rc = lpfc_mbox_dev_check(phba);
8668 if (unlikely(rc)) {
8669 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008670 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008671 "cannot issue Data: x%x x%x\n",
8672 mboxq->vport ? mboxq->vport->vpi : 0,
8673 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008674 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8675 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008676 psli->sli_flag, flag);
8677 goto out_not_finished;
8678 }
8679
James Smartda0436e2009-05-22 14:51:39 -04008680 /* Detect polling mode and jump to a handler */
8681 if (!phba->sli4_hba.intr_enable) {
8682 if (flag == MBX_POLL)
8683 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8684 else
8685 rc = -EIO;
8686 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008687 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008688 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008689 "(x%x/x%x) failure: "
8690 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8691 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008692 mboxq->vport ? mboxq->vport->vpi : 0,
8693 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008694 lpfc_sli_config_mbox_subsys_get(phba,
8695 mboxq),
8696 lpfc_sli_config_mbox_opcode_get(phba,
8697 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008698 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8699 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8700 bf_get(lpfc_mcqe_ext_status,
8701 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008702 psli->sli_flag, flag);
8703 return rc;
8704 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008705 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8706 "(%d):2542 Try to issue mailbox command "
James Smart7365f6f2018-02-22 08:18:46 -08008707 "x%x (x%x/x%x) synchronously ahead of async "
James Smartf1126682009-06-10 17:22:44 -04008708 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008709 mboxq->vport ? mboxq->vport->vpi : 0,
8710 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008711 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8712 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008713 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008714 /* Try to block the asynchronous mailbox posting */
8715 rc = lpfc_sli4_async_mbox_block(phba);
8716 if (!rc) {
8717 /* Successfully blocked, now issue sync mbox cmd */
8718 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8719 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008720 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008721 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008722 "(%d):2597 Sync Mailbox command "
8723 "x%x (x%x/x%x) failure: "
8724 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8725 "Data: x%x x%x\n,",
8726 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008727 mboxq->u.mb.mbxCommand,
8728 lpfc_sli_config_mbox_subsys_get(phba,
8729 mboxq),
8730 lpfc_sli_config_mbox_opcode_get(phba,
8731 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008732 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8733 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8734 bf_get(lpfc_mcqe_ext_status,
8735 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008736 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008737 /* Unblock the async mailbox posting afterward */
8738 lpfc_sli4_async_mbox_unblock(phba);
8739 }
8740 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008741 }
8742
8743 /* Now, interrupt mode asynchrous mailbox command */
8744 rc = lpfc_mbox_cmd_check(phba, mboxq);
8745 if (rc) {
8746 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008747 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008748 "cannot issue Data: x%x x%x\n",
8749 mboxq->vport ? mboxq->vport->vpi : 0,
8750 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008751 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8752 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008753 psli->sli_flag, flag);
8754 goto out_not_finished;
8755 }
James Smartda0436e2009-05-22 14:51:39 -04008756
8757 /* Put the mailbox command to the driver internal FIFO */
8758 psli->slistat.mbox_busy++;
8759 spin_lock_irqsave(&phba->hbalock, iflags);
8760 lpfc_mbox_put(phba, mboxq);
8761 spin_unlock_irqrestore(&phba->hbalock, iflags);
8762 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8763 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008764 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008765 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8766 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008767 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8768 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008769 phba->pport->port_state,
8770 psli->sli_flag, MBX_NOWAIT);
8771 /* Wake up worker thread to transport mailbox command from head */
8772 lpfc_worker_wake_up(phba);
8773
8774 return MBX_BUSY;
8775
8776out_not_finished:
8777 return MBX_NOT_FINISHED;
8778}
8779
8780/**
8781 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8782 * @phba: Pointer to HBA context object.
8783 *
8784 * This function is called by worker thread to send a mailbox command to
8785 * SLI4 HBA firmware.
8786 *
8787 **/
8788int
8789lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8790{
8791 struct lpfc_sli *psli = &phba->sli;
8792 LPFC_MBOXQ_t *mboxq;
8793 int rc = MBX_SUCCESS;
8794 unsigned long iflags;
8795 struct lpfc_mqe *mqe;
8796 uint32_t mbx_cmnd;
8797
8798 /* Check interrupt mode before post async mailbox command */
8799 if (unlikely(!phba->sli4_hba.intr_enable))
8800 return MBX_NOT_FINISHED;
8801
8802 /* Check for mailbox command service token */
8803 spin_lock_irqsave(&phba->hbalock, iflags);
8804 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8805 spin_unlock_irqrestore(&phba->hbalock, iflags);
8806 return MBX_NOT_FINISHED;
8807 }
8808 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8809 spin_unlock_irqrestore(&phba->hbalock, iflags);
8810 return MBX_NOT_FINISHED;
8811 }
8812 if (unlikely(phba->sli.mbox_active)) {
8813 spin_unlock_irqrestore(&phba->hbalock, iflags);
8814 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8815 "0384 There is pending active mailbox cmd\n");
8816 return MBX_NOT_FINISHED;
8817 }
8818 /* Take the mailbox command service token */
8819 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8820
8821 /* Get the next mailbox command from head of queue */
8822 mboxq = lpfc_mbox_get(phba);
8823
8824 /* If no more mailbox command waiting for post, we're done */
8825 if (!mboxq) {
8826 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8827 spin_unlock_irqrestore(&phba->hbalock, iflags);
8828 return MBX_SUCCESS;
8829 }
8830 phba->sli.mbox_active = mboxq;
8831 spin_unlock_irqrestore(&phba->hbalock, iflags);
8832
8833 /* Check device readiness for posting mailbox command */
8834 rc = lpfc_mbox_dev_check(phba);
8835 if (unlikely(rc))
8836 /* Driver clean routine will clean up pending mailbox */
8837 goto out_not_finished;
8838
8839 /* Prepare the mbox command to be posted */
8840 mqe = &mboxq->u.mqe;
8841 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8842
8843 /* Start timer for the mbox_tmo and log some mailbox post messages */
8844 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008845 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008846
8847 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008848 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008849 "x%x x%x\n",
8850 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008851 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8852 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008853 phba->pport->port_state, psli->sli_flag);
8854
8855 if (mbx_cmnd != MBX_HEARTBEAT) {
8856 if (mboxq->vport) {
8857 lpfc_debugfs_disc_trc(mboxq->vport,
8858 LPFC_DISC_TRC_MBOX_VPORT,
8859 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8860 mbx_cmnd, mqe->un.mb_words[0],
8861 mqe->un.mb_words[1]);
8862 } else {
8863 lpfc_debugfs_disc_trc(phba->pport,
8864 LPFC_DISC_TRC_MBOX,
8865 "MBOX Send: cmd:x%x mb:x%x x%x",
8866 mbx_cmnd, mqe->un.mb_words[0],
8867 mqe->un.mb_words[1]);
8868 }
8869 }
8870 psli->slistat.mbox_cmd++;
8871
8872 /* Post the mailbox command to the port */
8873 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8874 if (rc != MBX_SUCCESS) {
8875 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008876 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008877 "cannot issue Data: x%x x%x\n",
8878 mboxq->vport ? mboxq->vport->vpi : 0,
8879 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008880 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8881 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008882 psli->sli_flag, MBX_NOWAIT);
8883 goto out_not_finished;
8884 }
8885
8886 return rc;
8887
8888out_not_finished:
8889 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008890 if (phba->sli.mbox_active) {
8891 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8892 __lpfc_mbox_cmpl_put(phba, mboxq);
8893 /* Release the token */
8894 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8895 phba->sli.mbox_active = NULL;
8896 }
James Smartda0436e2009-05-22 14:51:39 -04008897 spin_unlock_irqrestore(&phba->hbalock, iflags);
8898
8899 return MBX_NOT_FINISHED;
8900}
8901
8902/**
8903 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8904 * @phba: Pointer to HBA context object.
8905 * @pmbox: Pointer to mailbox object.
8906 * @flag: Flag indicating how the mailbox need to be processed.
8907 *
8908 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8909 * the API jump table function pointer from the lpfc_hba struct.
8910 *
8911 * Return codes the caller owns the mailbox command after the return of the
8912 * function.
8913 **/
8914int
8915lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8916{
8917 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8918}
8919
8920/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008921 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008922 * @phba: The hba struct for which this call is being executed.
8923 * @dev_grp: The HBA PCI-Device group number.
8924 *
8925 * This routine sets up the mbox interface API function jump table in @phba
8926 * struct.
8927 * Returns: 0 - success, -ENODEV - failure.
8928 **/
8929int
8930lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8931{
8932
8933 switch (dev_grp) {
8934 case LPFC_PCI_DEV_LP:
8935 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8936 phba->lpfc_sli_handle_slow_ring_event =
8937 lpfc_sli_handle_slow_ring_event_s3;
8938 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8939 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8940 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8941 break;
8942 case LPFC_PCI_DEV_OC:
8943 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8944 phba->lpfc_sli_handle_slow_ring_event =
8945 lpfc_sli_handle_slow_ring_event_s4;
8946 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8947 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8948 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8949 break;
8950 default:
8951 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8952 "1420 Invalid HBA PCI-device group: 0x%x\n",
8953 dev_grp);
8954 return -ENODEV;
8955 break;
8956 }
8957 return 0;
8958}
8959
8960/**
James Smart3621a712009-04-06 18:47:14 -04008961 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008962 * @phba: Pointer to HBA context object.
8963 * @pring: Pointer to driver SLI ring object.
8964 * @piocb: Pointer to address of newly added command iocb.
8965 *
8966 * This function is called with hbalock held to add a command
8967 * iocb to the txq when SLI layer cannot submit the command iocb
8968 * to the ring.
8969 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008970void
James Smart92d7f7b2007-06-17 19:56:38 -05008971__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008972 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008973{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008974 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008975 /* Insert the caller's iocb in the txq tail for later processing. */
8976 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008977}
8978
James Smarte59058c2008-08-24 21:49:00 -04008979/**
James Smart3621a712009-04-06 18:47:14 -04008980 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008981 * @phba: Pointer to HBA context object.
8982 * @pring: Pointer to driver SLI ring object.
8983 * @piocb: Pointer to address of newly added command iocb.
8984 *
8985 * This function is called with hbalock held before a new
8986 * iocb is submitted to the firmware. This function checks
8987 * txq to flush the iocbs in txq to Firmware before
8988 * submitting new iocbs to the Firmware.
8989 * If there are iocbs in the txq which need to be submitted
8990 * to firmware, lpfc_sli_next_iocb returns the first element
8991 * of the txq after dequeuing it from txq.
8992 * If there is no iocb in the txq then the function will return
8993 * *piocb and *piocb is set to NULL. Caller needs to check
8994 * *piocb to find if there are more commands in the txq.
8995 **/
dea31012005-04-17 16:05:31 -05008996static struct lpfc_iocbq *
8997lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008998 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008999{
9000 struct lpfc_iocbq * nextiocb;
9001
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009002 lockdep_assert_held(&phba->hbalock);
9003
dea31012005-04-17 16:05:31 -05009004 nextiocb = lpfc_sli_ringtx_get(phba, pring);
9005 if (!nextiocb) {
9006 nextiocb = *piocb;
9007 *piocb = NULL;
9008 }
9009
9010 return nextiocb;
9011}
9012
James Smarte59058c2008-08-24 21:49:00 -04009013/**
James Smart3772a992009-05-22 14:50:54 -04009014 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009015 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04009016 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04009017 * @piocb: Pointer to command iocb.
9018 * @flag: Flag indicating if this command can be put into txq.
9019 *
James Smart3772a992009-05-22 14:50:54 -04009020 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
9021 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
9022 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
9023 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
9024 * this function allows only iocbs for posting buffers. This function finds
9025 * next available slot in the command ring and posts the command to the
9026 * available slot and writes the port attention register to request HBA start
9027 * processing new iocb. If there is no slot available in the ring and
9028 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
9029 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04009030 *
James Smart3772a992009-05-22 14:50:54 -04009031 * This function is called with hbalock held. The function will return success
9032 * after it successfully submit the iocb to firmware or after adding to the
9033 * txq.
James Smarte59058c2008-08-24 21:49:00 -04009034 **/
James Smart98c9ea52007-10-27 13:37:33 -04009035static int
James Smart3772a992009-05-22 14:50:54 -04009036__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05009037 struct lpfc_iocbq *piocb, uint32_t flag)
9038{
9039 struct lpfc_iocbq *nextiocb;
9040 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08009041 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05009042
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009043 lockdep_assert_held(&phba->hbalock);
9044
James Smart92d7f7b2007-06-17 19:56:38 -05009045 if (piocb->iocb_cmpl && (!piocb->vport) &&
9046 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
9047 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
9048 lpfc_printf_log(phba, KERN_ERR,
9049 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04009050 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05009051 piocb->iocb.ulpCommand);
9052 dump_stack();
9053 return IOCB_ERROR;
9054 }
9055
9056
Linas Vepstas8d63f372007-02-14 14:28:36 -06009057 /* If the PCI channel is in offline state, do not post iocbs. */
9058 if (unlikely(pci_channel_offline(phba->pcidev)))
9059 return IOCB_ERROR;
9060
James Smarta257bf92009-04-06 18:48:10 -04009061 /* If HBA has a deferred error attention, fail the iocb. */
9062 if (unlikely(phba->hba_flag & DEFER_ERATT))
9063 return IOCB_ERROR;
9064
dea31012005-04-17 16:05:31 -05009065 /*
9066 * We should never get an IOCB if we are in a < LINK_DOWN state
9067 */
James Smart2e0fef82007-06-17 19:56:36 -05009068 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05009069 return IOCB_ERROR;
9070
9071 /*
9072 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04009073 * outstanding event.
dea31012005-04-17 16:05:31 -05009074 */
James Smart0b727fe2007-10-27 13:37:25 -04009075 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05009076 goto iocb_busy;
9077
James Smart2e0fef82007-06-17 19:56:36 -05009078 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05009079 /*
James Smart2680eea2007-04-25 09:52:55 -04009080 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05009081 * can be issued if the link is not up.
9082 */
9083 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04009084 case CMD_GEN_REQUEST64_CR:
9085 case CMD_GEN_REQUEST64_CX:
9086 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
9087 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04009088 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04009089 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
9090 MENLO_TRANSPORT_TYPE))
9091
9092 goto iocb_busy;
9093 break;
dea31012005-04-17 16:05:31 -05009094 case CMD_QUE_RING_BUF_CN:
9095 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05009096 /*
9097 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
9098 * completion, iocb_cmpl MUST be 0.
9099 */
9100 if (piocb->iocb_cmpl)
9101 piocb->iocb_cmpl = NULL;
9102 /*FALLTHROUGH*/
9103 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04009104 case CMD_CLOSE_XRI_CN:
9105 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05009106 break;
9107 default:
9108 goto iocb_busy;
9109 }
9110
9111 /*
9112 * For FCP commands, we must be in a state where we can process link
9113 * attention events.
9114 */
James Smart895427b2017-02-12 13:52:30 -08009115 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05009116 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05009117 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05009118 }
dea31012005-04-17 16:05:31 -05009119
dea31012005-04-17 16:05:31 -05009120 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
9121 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
9122 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
9123
9124 if (iocb)
9125 lpfc_sli_update_ring(phba, pring);
9126 else
9127 lpfc_sli_update_full_ring(phba, pring);
9128
9129 if (!piocb)
9130 return IOCB_SUCCESS;
9131
9132 goto out_busy;
9133
9134 iocb_busy:
9135 pring->stats.iocb_cmd_delay++;
9136
9137 out_busy:
9138
9139 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05009140 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05009141 return IOCB_SUCCESS;
9142 }
9143
9144 return IOCB_BUSY;
9145}
9146
James Smart3772a992009-05-22 14:50:54 -04009147/**
James Smart4f774512009-05-22 14:52:35 -04009148 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
9149 * @phba: Pointer to HBA context object.
9150 * @piocb: Pointer to command iocb.
9151 * @sglq: Pointer to the scatter gather queue object.
9152 *
9153 * This routine converts the bpl or bde that is in the IOCB
9154 * to a sgl list for the sli4 hardware. The physical address
9155 * of the bpl/bde is converted back to a virtual address.
9156 * If the IOCB contains a BPL then the list of BDE's is
9157 * converted to sli4_sge's. If the IOCB contains a single
9158 * BDE then it is converted to a single sli_sge.
9159 * The IOCB is still in cpu endianess so the contents of
9160 * the bpl can be used without byte swapping.
9161 *
9162 * Returns valid XRI = Success, NO_XRI = Failure.
9163**/
9164static uint16_t
9165lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
9166 struct lpfc_sglq *sglq)
9167{
9168 uint16_t xritag = NO_XRI;
9169 struct ulp_bde64 *bpl = NULL;
9170 struct ulp_bde64 bde;
9171 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05009172 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04009173 IOCB_t *icmd;
9174 int numBdes = 0;
9175 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05009176 uint32_t offset = 0; /* accumulated offset in the sg request list */
9177 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04009178
9179 if (!piocbq || !sglq)
9180 return xritag;
9181
9182 sgl = (struct sli4_sge *)sglq->sgl;
9183 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05009184 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
9185 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009186 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9187 numBdes = icmd->un.genreq64.bdl.bdeSize /
9188 sizeof(struct ulp_bde64);
9189 /* The addrHigh and addrLow fields within the IOCB
9190 * have not been byteswapped yet so there is no
9191 * need to swap them back.
9192 */
James Smart1b511972011-12-13 13:23:09 -05009193 if (piocbq->context3)
9194 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
9195 else
9196 return xritag;
James Smart4f774512009-05-22 14:52:35 -04009197
James Smart1b511972011-12-13 13:23:09 -05009198 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04009199 if (!bpl)
9200 return xritag;
9201
9202 for (i = 0; i < numBdes; i++) {
9203 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05009204 sgl->addr_hi = bpl->addrHigh;
9205 sgl->addr_lo = bpl->addrLow;
9206
James Smart05580562011-05-24 11:40:48 -04009207 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009208 if ((i+1) == numBdes)
9209 bf_set(lpfc_sli4_sge_last, sgl, 1);
9210 else
9211 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05009212 /* swap the size field back to the cpu so we
9213 * can assign it to the sgl.
9214 */
9215 bde.tus.w = le32_to_cpu(bpl->tus.w);
9216 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05009217 /* The offsets in the sgl need to be accumulated
9218 * separately for the request and reply lists.
9219 * The request is always first, the reply follows.
9220 */
9221 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
9222 /* add up the reply sg entries */
9223 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
9224 inbound++;
9225 /* first inbound? reset the offset */
9226 if (inbound == 1)
9227 offset = 0;
9228 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04009229 bf_set(lpfc_sli4_sge_type, sgl,
9230 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05009231 offset += bde.tus.f.bdeSize;
9232 }
James Smart546fc852011-03-11 16:06:29 -05009233 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009234 bpl++;
9235 sgl++;
9236 }
9237 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
9238 /* The addrHigh and addrLow fields of the BDE have not
9239 * been byteswapped yet so they need to be swapped
9240 * before putting them in the sgl.
9241 */
9242 sgl->addr_hi =
9243 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
9244 sgl->addr_lo =
9245 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04009246 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009247 bf_set(lpfc_sli4_sge_last, sgl, 1);
9248 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05009249 sgl->sge_len =
9250 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04009251 }
9252 return sglq->sli4_xritag;
9253}
9254
9255/**
James Smart4f774512009-05-22 14:52:35 -04009256 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
9257 * @phba: Pointer to HBA context object.
9258 * @piocb: Pointer to command iocb.
9259 * @wqe: Pointer to the work queue entry.
9260 *
9261 * This routine converts the iocb command to its Work Queue Entry
9262 * equivalent. The wqe pointer should not have any fields set when
9263 * this routine is called because it will memcpy over them.
9264 * This routine does not set the CQ_ID or the WQEC bits in the
9265 * wqe.
9266 *
9267 * Returns: 0 = Success, IOCB_ERROR = Failure.
9268 **/
9269static int
9270lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
James Smart205e8242018-03-05 12:04:03 -08009271 union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -04009272{
James Smart5ffc2662009-11-18 15:39:44 -05009273 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04009274 uint8_t ct = 0;
9275 uint32_t fip;
9276 uint32_t abort_tag;
9277 uint8_t command_type = ELS_COMMAND_NON_FIP;
9278 uint8_t cmnd;
9279 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04009280 uint16_t abrt_iotag;
9281 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04009282 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04009283 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05009284 int numBdes, i;
9285 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04009286 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05009287 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05009288 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04009289
James Smart45ed1192009-10-02 15:17:02 -04009290 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04009291 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04009292 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04009293 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05009294 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04009295 command_type = ELS_COMMAND_FIP;
9296 else
9297 command_type = ELS_COMMAND_NON_FIP;
9298
James Smartb5c53952016-03-31 14:12:30 -07009299 if (phba->fcp_embed_io)
9300 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04009301 /* Some of the fields are in the right position already */
9302 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartae9e28f2017-05-15 15:20:51 -07009303 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
9304 /* The ct field has moved so reset */
9305 wqe->generic.wqe_com.word7 = 0;
9306 wqe->generic.wqe_com.word10 = 0;
9307 }
James Smartb5c53952016-03-31 14:12:30 -07009308
9309 abort_tag = (uint32_t) iocbq->iotag;
9310 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009311 /* words0-2 bpl convert bde */
9312 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05009313 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9314 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04009315 bpl = (struct ulp_bde64 *)
9316 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
9317 if (!bpl)
9318 return IOCB_ERROR;
9319
9320 /* Should already be byte swapped. */
9321 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
9322 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
9323 /* swap the size field back to the cpu so we
9324 * can assign it to the sgl.
9325 */
9326 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05009327 xmit_len = wqe->generic.bde.tus.f.bdeSize;
9328 total_len = 0;
9329 for (i = 0; i < numBdes; i++) {
9330 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
9331 total_len += bde.tus.f.bdeSize;
9332 }
James Smart4f774512009-05-22 14:52:35 -04009333 } else
James Smart5ffc2662009-11-18 15:39:44 -05009334 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04009335
9336 iocbq->iocb.ulpIoTag = iocbq->iotag;
9337 cmnd = iocbq->iocb.ulpCommand;
9338
9339 switch (iocbq->iocb.ulpCommand) {
9340 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04009341 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
9342 ndlp = iocbq->context_un.ndlp;
9343 else
9344 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009345 if (!iocbq->iocb.ulpLe) {
9346 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9347 "2007 Only Limited Edition cmd Format"
9348 " supported 0x%x\n",
9349 iocbq->iocb.ulpCommand);
9350 return IOCB_ERROR;
9351 }
James Smartff78d8f2011-12-13 13:21:35 -05009352
James Smart5ffc2662009-11-18 15:39:44 -05009353 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009354 /* Els_reguest64 has a TMO */
9355 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
9356 iocbq->iocb.ulpTimeout);
9357 /* Need a VF for word 4 set the vf bit*/
9358 bf_set(els_req64_vf, &wqe->els_req, 0);
9359 /* And a VFID for word 12 */
9360 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04009361 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04009362 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9363 iocbq->iocb.ulpContext);
9364 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9365 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04009366 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05009367 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05009368 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9369 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05009370 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9371 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05009372 if_type = bf_get(lpfc_sli_intf_if_type,
9373 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009374 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05009375 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05009376 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05009377 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05009378 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05009379 *pcmd == ELS_CMD_PLOGI)) {
9380 bf_set(els_req64_sp, &wqe->els_req, 1);
9381 bf_set(els_req64_sid, &wqe->els_req,
9382 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009383 if ((*pcmd == ELS_CMD_FLOGI) &&
9384 !(phba->fc_topology ==
9385 LPFC_TOPOLOGY_LOOP))
9386 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05009387 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9388 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04009389 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05009390 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05009391 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9392 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9393 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9394 }
James Smartc8685952009-11-18 15:39:16 -05009395 }
James Smart6d368e52011-05-24 11:44:12 -04009396 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9397 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009398 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9399 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9400 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9401 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9402 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9403 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009404 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04009405 break;
James Smart5ffc2662009-11-18 15:39:44 -05009406 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04009407 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9408 iocbq->iocb.un.ulpWord[3]);
9409 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009410 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05009411 /* The entire sequence is transmitted for this IOCB */
9412 xmit_len = total_len;
9413 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05009414 if (phba->link_flag & LS_LOOPBACK_MODE)
9415 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04009416 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04009417 /* word3 iocb=io_tag32 wqe=reserved */
9418 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04009419 /* word4 relative_offset memcpy */
9420 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009421 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9422 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9423 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9424 LPFC_WQE_IOD_WRITE);
9425 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9426 LPFC_WQE_LENLOC_WORD12);
9427 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05009428 wqe->xmit_sequence.xmit_len = xmit_len;
9429 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009430 break;
James Smart4f774512009-05-22 14:52:35 -04009431 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04009432 /* word3 iocb=iotag32 wqe=seq_payload_len */
9433 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009434 /* word4 iocb=rsvd wqe=rsvd */
9435 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9436 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04009437 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009438 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04009439 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9440 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9441 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9442 LPFC_WQE_LENLOC_WORD3);
9443 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04009444 break;
James Smart4f774512009-05-22 14:52:35 -04009445 case CMD_FCP_IWRITE64_CR:
9446 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04009447 /* word3 iocb=iotag wqe=payload_offset_len */
9448 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009449 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9450 xmit_len + sizeof(struct fcp_rsp));
9451 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9452 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009453 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9454 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9455 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9456 iocbq->iocb.ulpFCP2Rcvy);
9457 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9458 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009459 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9460 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9461 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009462 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009463 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009464 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9465 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009466 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9467 if (iocbq->priority) {
9468 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9469 (iocbq->priority << 1));
9470 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009471 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9472 (phba->cfg_XLanePriority << 1));
9473 }
9474 }
James Smartb5c53952016-03-31 14:12:30 -07009475 /* Note, word 10 is already initialized to 0 */
9476
James Smart414abe02018-06-26 08:24:26 -07009477 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9478 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009479 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9480 else
9481 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9482
James Smartb5c53952016-03-31 14:12:30 -07009483 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009484 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009485 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009486 struct fcp_cmnd *fcp_cmnd;
9487 uint32_t *ptr;
9488
9489 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009490
9491 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009492 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009493 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9494
9495 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009496 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009497 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009498 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9499 wqe->generic.bde.addrHigh = 0;
9500 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009501
James Smart205e8242018-03-05 12:04:03 -08009502 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9503 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009504
9505 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009506 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009507 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9508 }
James Smart7851fe22011-07-22 18:36:52 -04009509 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04009510 case CMD_FCP_IREAD64_CR:
9511 /* word3 iocb=iotag wqe=payload_offset_len */
9512 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009513 bf_set(payload_offset_len, &wqe->fcp_iread,
9514 xmit_len + sizeof(struct fcp_rsp));
9515 bf_set(cmd_buff_len, &wqe->fcp_iread,
9516 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009517 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9518 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9519 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9520 iocbq->iocb.ulpFCP2Rcvy);
9521 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04009522 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009523 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9524 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9525 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009526 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009527 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009528 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9529 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009530 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9531 if (iocbq->priority) {
9532 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9533 (iocbq->priority << 1));
9534 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009535 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9536 (phba->cfg_XLanePriority << 1));
9537 }
9538 }
James Smartb5c53952016-03-31 14:12:30 -07009539 /* Note, word 10 is already initialized to 0 */
9540
James Smart414abe02018-06-26 08:24:26 -07009541 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9542 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009543 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9544 else
9545 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9546
James Smartb5c53952016-03-31 14:12:30 -07009547 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009548 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009549 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009550 struct fcp_cmnd *fcp_cmnd;
9551 uint32_t *ptr;
9552
9553 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009554
9555 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009556 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009557 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9558
9559 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009560 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009561 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009562 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9563 wqe->generic.bde.addrHigh = 0;
9564 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009565
James Smart205e8242018-03-05 12:04:03 -08009566 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9567 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009568
9569 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009570 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009571 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9572 }
James Smart7851fe22011-07-22 18:36:52 -04009573 break;
James Smartf1126682009-06-10 17:22:44 -04009574 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04009575 /* word3 iocb=iotag wqe=payload_offset_len */
9576 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9577 bf_set(payload_offset_len, &wqe->fcp_icmd,
9578 xmit_len + sizeof(struct fcp_rsp));
9579 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9580 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009581 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04009582 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04009583 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009584 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9585 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9586 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9587 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9588 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04009589 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9590 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009591 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9592 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009593 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9594 if (iocbq->priority) {
9595 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9596 (iocbq->priority << 1));
9597 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009598 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9599 (phba->cfg_XLanePriority << 1));
9600 }
9601 }
James Smartb5c53952016-03-31 14:12:30 -07009602 /* Note, word 10 is already initialized to 0 */
9603
9604 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009605 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009606 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009607 struct fcp_cmnd *fcp_cmnd;
9608 uint32_t *ptr;
9609
9610 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009611
9612 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009613 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009614 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9615
9616 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009617 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009618 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009619 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9620 wqe->generic.bde.addrHigh = 0;
9621 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009622
James Smart205e8242018-03-05 12:04:03 -08009623 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9624 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009625
9626 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009627 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009628 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9629 }
James Smart7851fe22011-07-22 18:36:52 -04009630 break;
James Smart4f774512009-05-22 14:52:35 -04009631 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009632 /* For this command calculate the xmit length of the
9633 * request bde.
9634 */
9635 xmit_len = 0;
9636 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9637 sizeof(struct ulp_bde64);
9638 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009639 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009640 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9641 break;
James Smart63e801c2010-11-20 23:14:19 -05009642 xmit_len += bde.tus.f.bdeSize;
9643 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009644 /* word3 iocb=IO_TAG wqe=request_payload_len */
9645 wqe->gen_req.request_payload_len = xmit_len;
9646 /* word4 iocb=parameter wqe=relative_offset memcpy */
9647 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009648 /* word6 context tag copied in memcpy */
9649 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9650 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9651 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9652 "2015 Invalid CT %x command 0x%x\n",
9653 ct, iocbq->iocb.ulpCommand);
9654 return IOCB_ERROR;
9655 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009656 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9657 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9658 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9659 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9660 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9661 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9662 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9663 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009664 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009665 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009666 break;
James Smart4f774512009-05-22 14:52:35 -04009667 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009668 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009669 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009670 /* word3 iocb=iotag32 wqe=response_payload_len */
9671 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009672 /* word4 */
9673 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009674 /* word5 iocb=rsvd wge=did */
9675 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009676 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9677
9678 if_type = bf_get(lpfc_sli_intf_if_type,
9679 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009680 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -04009681 if (iocbq->vport->fc_flag & FC_PT2PT) {
9682 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9683 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9684 iocbq->vport->fc_myDID);
9685 if (iocbq->vport->fc_myDID == Fabric_DID) {
9686 bf_set(wqe_els_did,
9687 &wqe->xmit_els_rsp.wqe_dest, 0);
9688 }
9689 }
9690 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009691 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9692 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9693 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9694 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009695 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009696 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009697 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009698 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009699 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9700 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9701 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9702 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9703 LPFC_WQE_LENLOC_WORD3);
9704 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009705 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9706 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009707 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9708 iocbq->context2)->virt);
9709 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009710 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9711 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009712 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009713 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9714 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009715 phba->vpi_ids[phba->pport->vpi]);
9716 }
James Smart4f774512009-05-22 14:52:35 -04009717 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009718 break;
James Smart4f774512009-05-22 14:52:35 -04009719 case CMD_CLOSE_XRI_CN:
9720 case CMD_ABORT_XRI_CN:
9721 case CMD_ABORT_XRI_CX:
9722 /* words 0-2 memcpy should be 0 rserved */
9723 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009724 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9725 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9726 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9727 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9728 } else
9729 fip = 0;
9730
9731 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009732 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009733 * The link is down, or the command was ELS_FIP
9734 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009735 * on the wire.
9736 */
9737 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9738 else
9739 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9740 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009741 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9742 wqe->abort_cmd.rsrvd5 = 0;
9743 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009744 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9745 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009746 /*
9747 * The abort handler will send us CMD_ABORT_XRI_CN or
9748 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9749 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009750 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9751 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9752 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9753 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009754 cmnd = CMD_ABORT_XRI_CX;
9755 command_type = OTHER_COMMAND;
9756 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009757 break;
James Smart6669f9b2009-10-02 15:16:45 -04009758 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009759 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009760 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009761 * we re-construct this WQE here based on information in
9762 * iocbq from scratch.
9763 */
9764 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009765 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009766 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009767 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9768 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009769 LPFC_ABTS_UNSOL_INT) {
9770 /* ABTS sent by initiator to CT exchange, the
9771 * RX_ID field will be filled with the newly
9772 * allocated responder XRI.
9773 */
9774 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9775 iocbq->sli4_xritag);
9776 } else {
9777 /* ABTS sent by responder to CT exchange, the
9778 * RX_ID field will be filled with the responder
9779 * RX_ID from ABTS.
9780 */
9781 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009782 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009783 }
James Smart6669f9b2009-10-02 15:16:45 -04009784 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9785 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009786
9787 /* Use CT=VPI */
9788 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9789 ndlp->nlp_DID);
9790 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9791 iocbq->iocb.ulpContext);
9792 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009793 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009794 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009795 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9796 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9797 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009798 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9799 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009800 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9801 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9802 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9803 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9804 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9805 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9806 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9807 }
9808
James Smart7851fe22011-07-22 18:36:52 -04009809 break;
James Smartae9e28f2017-05-15 15:20:51 -07009810 case CMD_SEND_FRAME:
9811 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9812 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9813 return 0;
James Smart4f774512009-05-22 14:52:35 -04009814 case CMD_XRI_ABORTED_CX:
9815 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009816 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9817 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9818 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9819 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9820 default:
9821 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9822 "2014 Invalid command 0x%x\n",
9823 iocbq->iocb.ulpCommand);
9824 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009825 break;
James Smart4f774512009-05-22 14:52:35 -04009826 }
James Smart6d368e52011-05-24 11:44:12 -04009827
James Smart8012cc32012-10-31 14:44:49 -04009828 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9829 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9830 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9831 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9832 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9833 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9834 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9835 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009836 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9837 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9838 wqe->generic.wqe_com.abort_tag = abort_tag;
9839 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9840 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9841 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9842 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009843 return 0;
9844}
9845
9846/**
9847 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9848 * @phba: Pointer to HBA context object.
9849 * @ring_number: SLI ring number to issue iocb on.
9850 * @piocb: Pointer to command iocb.
9851 * @flag: Flag indicating if this command can be put into txq.
9852 *
9853 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9854 * an iocb command to an HBA with SLI-4 interface spec.
9855 *
9856 * This function is called with hbalock held. The function will return success
9857 * after it successfully submit the iocb to firmware or after adding to the
9858 * txq.
9859 **/
9860static int
9861__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9862 struct lpfc_iocbq *piocb, uint32_t flag)
9863{
9864 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -08009865 union lpfc_wqe128 wqe;
James Smart1ba981f2014-02-20 09:56:45 -05009866 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009867 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009868
James Smart895427b2017-02-12 13:52:30 -08009869 /* Get the WQ */
9870 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9871 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartcdb42be2019-01-28 11:14:21 -08009872 wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].fcp_wq;
James Smart895427b2017-02-12 13:52:30 -08009873 } else {
9874 wq = phba->sli4_hba.els_wq;
9875 }
9876
9877 /* Get corresponding ring */
9878 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009879
James Smartb5c53952016-03-31 14:12:30 -07009880 /*
9881 * The WQE can be either 64 or 128 bytes,
James Smartb5c53952016-03-31 14:12:30 -07009882 */
James Smartb5c53952016-03-31 14:12:30 -07009883
James Smart895427b2017-02-12 13:52:30 -08009884 lockdep_assert_held(&phba->hbalock);
9885
James Smart4f774512009-05-22 14:52:35 -04009886 if (piocb->sli4_xritag == NO_XRI) {
9887 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009888 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009889 sglq = NULL;
9890 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009891 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009892 if (!(flag & SLI_IOCB_RET_IOCB)) {
9893 __lpfc_sli_ringtx_put(phba,
9894 pring, piocb);
9895 return IOCB_SUCCESS;
9896 } else {
9897 return IOCB_BUSY;
9898 }
9899 } else {
James Smart895427b2017-02-12 13:52:30 -08009900 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009901 if (!sglq) {
9902 if (!(flag & SLI_IOCB_RET_IOCB)) {
9903 __lpfc_sli_ringtx_put(phba,
9904 pring,
9905 piocb);
9906 return IOCB_SUCCESS;
9907 } else
9908 return IOCB_BUSY;
9909 }
9910 }
James Smart4f774512009-05-22 14:52:35 -04009911 }
James Smart2ea259e2017-02-12 13:52:27 -08009912 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009913 /* These IO's already have an XRI and a mapped sgl. */
9914 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009915 else {
James Smart6d368e52011-05-24 11:44:12 -04009916 /*
9917 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009918 * sglq is on the active list
9919 */
James Smartedccdc12013-01-03 15:43:45 -05009920 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009921 if (!sglq)
9922 return IOCB_ERROR;
9923 }
9924
9925 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009926 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009927 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009928 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009929 return IOCB_ERROR;
9930 }
9931
James Smart205e8242018-03-05 12:04:03 -08009932 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
James Smart4f774512009-05-22 14:52:35 -04009933 return IOCB_ERROR;
9934
James Smart205e8242018-03-05 12:04:03 -08009935 if (lpfc_sli4_wq_put(wq, &wqe))
James Smart895427b2017-02-12 13:52:30 -08009936 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009937 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9938
9939 return 0;
9940}
9941
9942/**
James Smart3772a992009-05-22 14:50:54 -04009943 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9944 *
9945 * This routine wraps the actual lockless version for issusing IOCB function
9946 * pointer from the lpfc_hba struct.
9947 *
9948 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009949 * IOCB_ERROR - Error
9950 * IOCB_SUCCESS - Success
9951 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009952 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009953int
James Smart3772a992009-05-22 14:50:54 -04009954__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9955 struct lpfc_iocbq *piocb, uint32_t flag)
9956{
9957 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9958}
9959
9960/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009961 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009962 * @phba: The hba struct for which this call is being executed.
9963 * @dev_grp: The HBA PCI-Device group number.
9964 *
9965 * This routine sets up the SLI interface API function jump table in @phba
9966 * struct.
9967 * Returns: 0 - success, -ENODEV - failure.
9968 **/
9969int
9970lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9971{
9972
9973 switch (dev_grp) {
9974 case LPFC_PCI_DEV_LP:
9975 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9976 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9977 break;
James Smart4f774512009-05-22 14:52:35 -04009978 case LPFC_PCI_DEV_OC:
9979 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9980 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9981 break;
James Smart3772a992009-05-22 14:50:54 -04009982 default:
9983 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9984 "1419 Invalid HBA PCI-device group: 0x%x\n",
9985 dev_grp);
9986 return -ENODEV;
9987 break;
9988 }
9989 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9990 return 0;
9991}
James Smart92d7f7b2007-06-17 19:56:38 -05009992
James Smarta1efe162015-05-21 13:55:20 -04009993/**
James Smart895427b2017-02-12 13:52:30 -08009994 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009995 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009996 * @piocb: Pointer to command iocb.
9997 *
James Smart895427b2017-02-12 13:52:30 -08009998 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9999 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -040010000 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -080010001 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -040010002 */
James Smart895427b2017-02-12 13:52:30 -080010003struct lpfc_sli_ring *
10004lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -040010005{
James Smartc4908502019-01-28 11:14:28 -080010006 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -080010007
James Smart895427b2017-02-12 13:52:30 -080010008 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smartcdb42be2019-01-28 11:14:21 -080010009 if (unlikely(!phba->sli4_hba.hdwq))
James Smart7370d102019-01-28 11:14:20 -080010010 return NULL;
10011 /*
10012 * for abort iocb hba_wqidx should already
10013 * be setup based on what work queue we used.
10014 */
10015 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc4908502019-01-28 11:14:28 -080010016 lpfc_cmd = (struct lpfc_io_buf *)piocb->context1;
James Smart1fbf9742019-01-28 11:14:26 -080010017 piocb->hba_wqidx = lpfc_cmd->hdwq_no;
James Smart9bd2bff52014-09-03 12:57:30 -040010018 }
James Smartcdb42be2019-01-28 11:14:21 -080010019 return phba->sli4_hba.hdwq[piocb->hba_wqidx].fcp_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010020 } else {
10021 if (unlikely(!phba->sli4_hba.els_wq))
10022 return NULL;
10023 piocb->hba_wqidx = 0;
10024 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -040010025 }
James Smart9bd2bff52014-09-03 12:57:30 -040010026}
10027
James Smarte59058c2008-08-24 21:49:00 -040010028/**
James Smart3621a712009-04-06 18:47:14 -040010029 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -040010030 * @phba: Pointer to HBA context object.
10031 * @pring: Pointer to driver SLI ring object.
10032 * @piocb: Pointer to command iocb.
10033 * @flag: Flag indicating if this command can be put into txq.
10034 *
10035 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
10036 * function. This function gets the hbalock and calls
10037 * __lpfc_sli_issue_iocb function and will return the error returned
10038 * by __lpfc_sli_issue_iocb function. This wrapper is used by
10039 * functions which do not hold hbalock.
10040 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010041int
James Smart3772a992009-05-22 14:50:54 -040010042lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -050010043 struct lpfc_iocbq *piocb, uint32_t flag)
10044{
James Smart2a76a282012-08-03 12:35:54 -040010045 struct lpfc_sli_ring *pring;
James Smart92d7f7b2007-06-17 19:56:38 -050010046 unsigned long iflags;
James Smart6a828b02019-01-28 11:14:31 -080010047 int rc;
James Smart92d7f7b2007-06-17 19:56:38 -050010048
James Smart7e56aa22012-08-03 12:35:34 -040010049 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010050 pring = lpfc_sli4_calc_ring(phba, piocb);
10051 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010052 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -040010053
James Smart9bd2bff52014-09-03 12:57:30 -040010054 spin_lock_irqsave(&pring->ring_lock, iflags);
10055 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10056 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart7e56aa22012-08-03 12:35:34 -040010057 } else {
10058 /* For now, SLI2/3 will still use hbalock */
10059 spin_lock_irqsave(&phba->hbalock, iflags);
10060 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10061 spin_unlock_irqrestore(&phba->hbalock, iflags);
10062 }
James Smart92d7f7b2007-06-17 19:56:38 -050010063 return rc;
10064}
10065
James Smarte59058c2008-08-24 21:49:00 -040010066/**
James Smart3621a712009-04-06 18:47:14 -040010067 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010068 * @phba: Pointer to HBA context object.
10069 *
10070 * This function is called while driver attaches with the
10071 * HBA to setup the extra ring. The extra ring is used
10072 * only when driver needs to support target mode functionality
10073 * or IP over FC functionalities.
10074 *
James Smart895427b2017-02-12 13:52:30 -080010075 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -040010076 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010077static int
10078lpfc_extra_ring_setup( struct lpfc_hba *phba)
10079{
10080 struct lpfc_sli *psli;
10081 struct lpfc_sli_ring *pring;
10082
10083 psli = &phba->sli;
10084
10085 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -050010086
10087 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -080010088 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -040010089 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10090 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10091 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10092 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010093
James Smarta4bc3372006-12-02 13:34:16 -050010094 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -080010095 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -050010096
James Smart7e56aa22012-08-03 12:35:34 -040010097 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10098 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10099 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10100 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010101
10102 /* Setup default profile for this ring */
10103 pring->iotag_max = 4096;
10104 pring->num_mask = 1;
10105 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -050010106 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
10107 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010108 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
10109 return 0;
10110}
10111
James Smartcb69f7d2011-12-13 13:21:57 -050010112/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
10113 * @phba: Pointer to HBA context object.
10114 * @iocbq: Pointer to iocb object.
10115 *
10116 * The async_event handler calls this routine when it receives
10117 * an ASYNC_STATUS_CN event from the port. The port generates
10118 * this event when an Abort Sequence request to an rport fails
10119 * twice in succession. The abort could be originated by the
10120 * driver or by the port. The ABTS could have been for an ELS
10121 * or FCP IO. The port only generates this event when an ABTS
10122 * fails to complete after one retry.
10123 */
10124static void
10125lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
10126 struct lpfc_iocbq *iocbq)
10127{
10128 struct lpfc_nodelist *ndlp = NULL;
10129 uint16_t rpi = 0, vpi = 0;
10130 struct lpfc_vport *vport = NULL;
10131
10132 /* The rpi in the ulpContext is vport-sensitive. */
10133 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
10134 rpi = iocbq->iocb.ulpContext;
10135
10136 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10137 "3092 Port generated ABTS async event "
10138 "on vpi %d rpi %d status 0x%x\n",
10139 vpi, rpi, iocbq->iocb.ulpStatus);
10140
10141 vport = lpfc_find_vport_by_vpid(phba, vpi);
10142 if (!vport)
10143 goto err_exit;
10144 ndlp = lpfc_findnode_rpi(vport, rpi);
10145 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
10146 goto err_exit;
10147
10148 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
10149 lpfc_sli_abts_recover_port(vport, ndlp);
10150 return;
10151
10152 err_exit:
10153 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10154 "3095 Event Context not found, no "
10155 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
10156 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
10157 vpi, rpi);
10158}
10159
10160/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
10161 * @phba: pointer to HBA context object.
10162 * @ndlp: nodelist pointer for the impacted rport.
10163 * @axri: pointer to the wcqe containing the failed exchange.
10164 *
10165 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
10166 * port. The port generates this event when an abort exchange request to an
10167 * rport fails twice in succession with no reply. The abort could be originated
10168 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
10169 */
10170void
10171lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
10172 struct lpfc_nodelist *ndlp,
10173 struct sli4_wcqe_xri_aborted *axri)
10174{
10175 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -050010176 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -050010177
James Smart6b5151f2012-01-18 16:24:06 -050010178 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -050010179 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10180 "3115 Node Context not found, driver "
10181 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -050010182 return;
10183 }
10184
James Smartcb69f7d2011-12-13 13:21:57 -050010185 vport = ndlp->vport;
10186 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10187 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -050010188 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -040010189 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -050010190 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -050010191 bf_get(lpfc_wcqe_xa_status, axri),
10192 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -050010193
James Smart5c1db2a2012-03-01 22:34:36 -050010194 /*
10195 * Catch the ABTS protocol failure case. Older OCe FW releases returned
10196 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
10197 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
10198 */
James Smarte3d2b802012-08-14 14:25:43 -040010199 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -050010200 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
10201 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -050010202 lpfc_sli_abts_recover_port(vport, ndlp);
10203}
10204
James Smarte59058c2008-08-24 21:49:00 -040010205/**
James Smart3621a712009-04-06 18:47:14 -040010206 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -040010207 * @phba: Pointer to HBA context object.
10208 * @pring: Pointer to driver SLI ring object.
10209 * @iocbq: Pointer to iocb object.
10210 *
10211 * This function is called by the slow ring event handler
10212 * function when there is an ASYNC event iocb in the ring.
10213 * This function is called with no lock held.
10214 * Currently this function handles only temperature related
10215 * ASYNC events. The function decodes the temperature sensor
10216 * event message and posts events for the management applications.
10217 **/
James Smart98c9ea52007-10-27 13:37:33 -040010218static void
James Smart57127f12007-10-27 13:37:05 -040010219lpfc_sli_async_event_handler(struct lpfc_hba * phba,
10220 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
10221{
10222 IOCB_t *icmd;
10223 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -040010224 struct temp_event temp_event_data;
10225 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -040010226 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -040010227
10228 icmd = &iocbq->iocb;
10229 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -040010230
James Smartcb69f7d2011-12-13 13:21:57 -050010231 switch (evt_code) {
10232 case ASYNC_TEMP_WARN:
10233 case ASYNC_TEMP_SAFE:
10234 temp_event_data.data = (uint32_t) icmd->ulpContext;
10235 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
10236 if (evt_code == ASYNC_TEMP_WARN) {
10237 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
10238 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10239 "0347 Adapter is very hot, please take "
10240 "corrective action. temperature : %d Celsius\n",
10241 (uint32_t) icmd->ulpContext);
10242 } else {
10243 temp_event_data.event_code = LPFC_NORMAL_TEMP;
10244 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10245 "0340 Adapter temperature is OK now. "
10246 "temperature : %d Celsius\n",
10247 (uint32_t) icmd->ulpContext);
10248 }
10249
10250 /* Send temperature change event to applications */
10251 shost = lpfc_shost_from_vport(phba->pport);
10252 fc_host_post_vendor_event(shost, fc_get_event_number(),
10253 sizeof(temp_event_data), (char *) &temp_event_data,
10254 LPFC_NL_VENDOR_ID);
10255 break;
10256 case ASYNC_STATUS_CN:
10257 lpfc_sli_abts_err_handler(phba, iocbq);
10258 break;
10259 default:
James Smarta257bf92009-04-06 18:48:10 -040010260 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -050010261 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -040010262 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -040010263 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -040010264 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
10265 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
10266 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
10267 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -050010268 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -040010269 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
10270 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
10271 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
10272 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
10273
James Smartcb69f7d2011-12-13 13:21:57 -050010274 break;
James Smart57127f12007-10-27 13:37:05 -040010275 }
James Smart57127f12007-10-27 13:37:05 -040010276}
10277
10278
James Smarte59058c2008-08-24 21:49:00 -040010279/**
James Smart895427b2017-02-12 13:52:30 -080010280 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010281 * @phba: Pointer to HBA context object.
10282 *
10283 * lpfc_sli_setup sets up rings of the SLI interface with
10284 * number of iocbs per ring and iotags. This function is
10285 * called while driver attach to the HBA and before the
10286 * interrupts are enabled. So there is no need for locking.
10287 *
10288 * This function always returns 0.
10289 **/
dea31012005-04-17 16:05:31 -050010290int
James Smart895427b2017-02-12 13:52:30 -080010291lpfc_sli4_setup(struct lpfc_hba *phba)
10292{
10293 struct lpfc_sli_ring *pring;
10294
10295 pring = phba->sli4_hba.els_wq->pring;
10296 pring->num_mask = LPFC_MAX_RING_MASK;
10297 pring->prt[0].profile = 0; /* Mask 0 */
10298 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10299 pring->prt[0].type = FC_TYPE_ELS;
10300 pring->prt[0].lpfc_sli_rcv_unsol_event =
10301 lpfc_els_unsol_event;
10302 pring->prt[1].profile = 0; /* Mask 1 */
10303 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10304 pring->prt[1].type = FC_TYPE_ELS;
10305 pring->prt[1].lpfc_sli_rcv_unsol_event =
10306 lpfc_els_unsol_event;
10307 pring->prt[2].profile = 0; /* Mask 2 */
10308 /* NameServer Inquiry */
10309 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10310 /* NameServer */
10311 pring->prt[2].type = FC_TYPE_CT;
10312 pring->prt[2].lpfc_sli_rcv_unsol_event =
10313 lpfc_ct_unsol_event;
10314 pring->prt[3].profile = 0; /* Mask 3 */
10315 /* NameServer response */
10316 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10317 /* NameServer */
10318 pring->prt[3].type = FC_TYPE_CT;
10319 pring->prt[3].lpfc_sli_rcv_unsol_event =
10320 lpfc_ct_unsol_event;
10321 return 0;
10322}
10323
10324/**
10325 * lpfc_sli_setup - SLI ring setup function
10326 * @phba: Pointer to HBA context object.
10327 *
10328 * lpfc_sli_setup sets up rings of the SLI interface with
10329 * number of iocbs per ring and iotags. This function is
10330 * called while driver attach to the HBA and before the
10331 * interrupts are enabled. So there is no need for locking.
10332 *
10333 * This function always returns 0. SLI3 only.
10334 **/
10335int
dea31012005-04-17 16:05:31 -050010336lpfc_sli_setup(struct lpfc_hba *phba)
10337{
James Smarted957682007-06-17 19:56:37 -050010338 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -050010339 struct lpfc_sli *psli = &phba->sli;
10340 struct lpfc_sli_ring *pring;
10341
James Smart2a76a282012-08-03 12:35:54 -040010342 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -050010343 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -050010344
James Bottomley604a3e32005-10-29 10:28:33 -050010345 psli->iocbq_lookup = NULL;
10346 psli->iocbq_lookup_len = 0;
10347 psli->last_iotag = 0;
10348
dea31012005-04-17 16:05:31 -050010349 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010350 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010351 switch (i) {
10352 case LPFC_FCP_RING: /* ring 0 - FCP */
10353 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010354 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10355 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10356 pring->sli.sli3.numCiocb +=
10357 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10358 pring->sli.sli3.numRiocb +=
10359 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10360 pring->sli.sli3.numCiocb +=
10361 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10362 pring->sli.sli3.numRiocb +=
10363 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10364 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010365 SLI3_IOCB_CMD_SIZE :
10366 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010367 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010368 SLI3_IOCB_RSP_SIZE :
10369 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010370 pring->iotag_ctr = 0;
10371 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -050010372 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -050010373 pring->fast_iotag = pring->iotag_max;
10374 pring->num_mask = 0;
10375 break;
James Smarta4bc3372006-12-02 13:34:16 -050010376 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -050010377 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010378 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10379 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10380 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010381 SLI3_IOCB_CMD_SIZE :
10382 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010383 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010384 SLI3_IOCB_RSP_SIZE :
10385 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -050010386 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -050010387 pring->num_mask = 0;
10388 break;
10389 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10390 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010391 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10392 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10393 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010394 SLI3_IOCB_CMD_SIZE :
10395 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010396 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010397 SLI3_IOCB_RSP_SIZE :
10398 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010399 pring->fast_iotag = 0;
10400 pring->iotag_ctr = 0;
10401 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -040010402 pring->lpfc_sli_rcv_async_status =
10403 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -040010404 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -050010405 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -040010406 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10407 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010408 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010409 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010410 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -040010411 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10412 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010413 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010414 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010415 pring->prt[2].profile = 0; /* Mask 2 */
10416 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -040010417 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -050010418 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010419 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010420 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010421 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010422 pring->prt[3].profile = 0; /* Mask 3 */
10423 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -040010424 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -050010425 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010426 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010427 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010428 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010429 break;
10430 }
James Smart7e56aa22012-08-03 12:35:34 -040010431 totiocbsize += (pring->sli.sli3.numCiocb *
10432 pring->sli.sli3.sizeCiocb) +
10433 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050010434 }
James Smarted957682007-06-17 19:56:37 -050010435 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050010436 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040010437 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10438 "SLI2 SLIM Data: x%x x%lx\n",
10439 phba->brd_no, totiocbsize,
10440 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050010441 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010442 if (phba->cfg_multi_ring_support == 2)
10443 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050010444
10445 return 0;
10446}
10447
James Smarte59058c2008-08-24 21:49:00 -040010448/**
James Smart895427b2017-02-12 13:52:30 -080010449 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040010450 * @phba: Pointer to HBA context object.
10451 *
James Smart895427b2017-02-12 13:52:30 -080010452 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040010453 * ring. This function also initializes ring indices of each ring.
10454 * This function is called during the initialization of the SLI
10455 * interface of an HBA.
10456 * This function is called with no lock held and always returns
10457 * 1.
10458 **/
James Smart895427b2017-02-12 13:52:30 -080010459void
10460lpfc_sli4_queue_init(struct lpfc_hba *phba)
10461{
10462 struct lpfc_sli *psli;
10463 struct lpfc_sli_ring *pring;
10464 int i;
10465
10466 psli = &phba->sli;
10467 spin_lock_irq(&phba->hbalock);
10468 INIT_LIST_HEAD(&psli->mboxq);
10469 INIT_LIST_HEAD(&psli->mboxq_cmpl);
10470 /* Initialize list headers for txq and txcmplq as double linked lists */
James Smartcdb42be2019-01-28 11:14:21 -080010471 for (i = 0; i < phba->cfg_hdw_queue; i++) {
10472 pring = phba->sli4_hba.hdwq[i].fcp_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010473 pring->flag = 0;
10474 pring->ringno = LPFC_FCP_RING;
James Smartc4908502019-01-28 11:14:28 -080010475 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010476 INIT_LIST_HEAD(&pring->txq);
10477 INIT_LIST_HEAD(&pring->txcmplq);
10478 INIT_LIST_HEAD(&pring->iocb_continueq);
10479 spin_lock_init(&pring->ring_lock);
10480 }
10481 pring = phba->sli4_hba.els_wq->pring;
10482 pring->flag = 0;
10483 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080010484 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010485 INIT_LIST_HEAD(&pring->txq);
10486 INIT_LIST_HEAD(&pring->txcmplq);
10487 INIT_LIST_HEAD(&pring->iocb_continueq);
10488 spin_lock_init(&pring->ring_lock);
10489
James Smartcdb42be2019-01-28 11:14:21 -080010490 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10491 for (i = 0; i < phba->cfg_hdw_queue; i++) {
10492 pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
James Smartc4908502019-01-28 11:14:28 -080010493 pring->flag = 0;
James Smartcdb42be2019-01-28 11:14:21 -080010494 pring->ringno = LPFC_FCP_RING;
James Smartc4908502019-01-28 11:14:28 -080010495 pring->txcmplq_cnt = 0;
James Smartcdb42be2019-01-28 11:14:21 -080010496 INIT_LIST_HEAD(&pring->txq);
10497 INIT_LIST_HEAD(&pring->txcmplq);
10498 INIT_LIST_HEAD(&pring->iocb_continueq);
10499 spin_lock_init(&pring->ring_lock);
10500 }
James Smart895427b2017-02-12 13:52:30 -080010501 pring = phba->sli4_hba.nvmels_wq->pring;
10502 pring->flag = 0;
10503 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080010504 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010505 INIT_LIST_HEAD(&pring->txq);
10506 INIT_LIST_HEAD(&pring->txcmplq);
10507 INIT_LIST_HEAD(&pring->iocb_continueq);
10508 spin_lock_init(&pring->ring_lock);
10509 }
10510
James Smart895427b2017-02-12 13:52:30 -080010511 spin_unlock_irq(&phba->hbalock);
10512}
10513
10514/**
10515 * lpfc_sli_queue_init - Queue initialization function
10516 * @phba: Pointer to HBA context object.
10517 *
10518 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10519 * ring. This function also initializes ring indices of each ring.
10520 * This function is called during the initialization of the SLI
10521 * interface of an HBA.
10522 * This function is called with no lock held and always returns
10523 * 1.
10524 **/
10525void
10526lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010527{
10528 struct lpfc_sli *psli;
10529 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050010530 int i;
dea31012005-04-17 16:05:31 -050010531
10532 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050010533 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010534 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050010535 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050010536 /* Initialize list headers for txq and txcmplq as double linked lists */
10537 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010538 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010539 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040010540 pring->sli.sli3.next_cmdidx = 0;
10541 pring->sli.sli3.local_getidx = 0;
10542 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050010543 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050010544 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050010545 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080010546 pring->flag = 0;
10547 INIT_LIST_HEAD(&pring->txq);
10548 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010549 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010550 }
James Smart2e0fef82007-06-17 19:56:36 -050010551 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010552}
10553
James Smarte59058c2008-08-24 21:49:00 -040010554/**
James Smart04c68492009-05-22 14:52:52 -040010555 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10556 * @phba: Pointer to HBA context object.
10557 *
10558 * This routine flushes the mailbox command subsystem. It will unconditionally
10559 * flush all the mailbox commands in the three possible stages in the mailbox
10560 * command sub-system: pending mailbox command queue; the outstanding mailbox
10561 * command; and completed mailbox command queue. It is caller's responsibility
10562 * to make sure that the driver is in the proper state to flush the mailbox
10563 * command sub-system. Namely, the posting of mailbox commands into the
10564 * pending mailbox command queue from the various clients must be stopped;
10565 * either the HBA is in a state that it will never works on the outstanding
10566 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10567 * mailbox command has been completed.
10568 **/
10569static void
10570lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10571{
10572 LIST_HEAD(completions);
10573 struct lpfc_sli *psli = &phba->sli;
10574 LPFC_MBOXQ_t *pmb;
10575 unsigned long iflag;
10576
James Smart523128e2018-09-10 10:30:46 -070010577 /* Disable softirqs, including timers from obtaining phba->hbalock */
10578 local_bh_disable();
10579
James Smart04c68492009-05-22 14:52:52 -040010580 /* Flush all the mailbox commands in the mbox system */
10581 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart523128e2018-09-10 10:30:46 -070010582
James Smart04c68492009-05-22 14:52:52 -040010583 /* The pending mailbox command queue */
10584 list_splice_init(&phba->sli.mboxq, &completions);
10585 /* The outstanding active mailbox command */
10586 if (psli->mbox_active) {
10587 list_add_tail(&psli->mbox_active->list, &completions);
10588 psli->mbox_active = NULL;
10589 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10590 }
10591 /* The completed mailbox command queue */
10592 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10593 spin_unlock_irqrestore(&phba->hbalock, iflag);
10594
James Smart523128e2018-09-10 10:30:46 -070010595 /* Enable softirqs again, done with phba->hbalock */
10596 local_bh_enable();
10597
James Smart04c68492009-05-22 14:52:52 -040010598 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10599 while (!list_empty(&completions)) {
10600 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10601 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10602 if (pmb->mbox_cmpl)
10603 pmb->mbox_cmpl(phba, pmb);
10604 }
10605}
10606
10607/**
James Smart3621a712009-04-06 18:47:14 -040010608 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010609 * @vport: Pointer to virtual port object.
10610 *
10611 * lpfc_sli_host_down is called to clean up the resources
10612 * associated with a vport before destroying virtual
10613 * port data structures.
10614 * This function does following operations:
10615 * - Free discovery resources associated with this virtual
10616 * port.
10617 * - Free iocbs associated with this virtual port in
10618 * the txq.
10619 * - Send abort for all iocb commands associated with this
10620 * vport in txcmplq.
10621 *
10622 * This function is called with no lock held and always returns 1.
10623 **/
dea31012005-04-17 16:05:31 -050010624int
James Smart92d7f7b2007-06-17 19:56:38 -050010625lpfc_sli_host_down(struct lpfc_vport *vport)
10626{
James Smart858c9f62007-06-17 19:56:39 -050010627 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010628 struct lpfc_hba *phba = vport->phba;
10629 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010630 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010631 struct lpfc_sli_ring *pring;
10632 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010633 int i;
10634 unsigned long flags = 0;
10635 uint16_t prev_pring_flag;
10636
10637 lpfc_cleanup_discovery_resources(vport);
10638
10639 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010640
James Smart895427b2017-02-12 13:52:30 -080010641 /*
10642 * Error everything on the txq since these iocbs
10643 * have not been given to the FW yet.
10644 * Also issue ABTS for everything on the txcmplq
10645 */
10646 if (phba->sli_rev != LPFC_SLI_REV4) {
10647 for (i = 0; i < psli->num_rings; i++) {
10648 pring = &psli->sli3_ring[i];
10649 prev_pring_flag = pring->flag;
10650 /* Only slow rings */
10651 if (pring->ringno == LPFC_ELS_RING) {
10652 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10653 /* Set the lpfc data pending flag */
10654 set_bit(LPFC_DATA_READY, &phba->data_flags);
10655 }
10656 list_for_each_entry_safe(iocb, next_iocb,
10657 &pring->txq, list) {
10658 if (iocb->vport != vport)
10659 continue;
10660 list_move_tail(&iocb->list, &completions);
10661 }
10662 list_for_each_entry_safe(iocb, next_iocb,
10663 &pring->txcmplq, list) {
10664 if (iocb->vport != vport)
10665 continue;
10666 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10667 }
10668 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010669 }
James Smart895427b2017-02-12 13:52:30 -080010670 } else {
10671 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10672 pring = qp->pring;
10673 if (!pring)
10674 continue;
10675 if (pring == phba->sli4_hba.els_wq->pring) {
10676 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10677 /* Set the lpfc data pending flag */
10678 set_bit(LPFC_DATA_READY, &phba->data_flags);
10679 }
10680 prev_pring_flag = pring->flag;
10681 spin_lock_irq(&pring->ring_lock);
10682 list_for_each_entry_safe(iocb, next_iocb,
10683 &pring->txq, list) {
10684 if (iocb->vport != vport)
10685 continue;
10686 list_move_tail(&iocb->list, &completions);
10687 }
10688 spin_unlock_irq(&pring->ring_lock);
10689 list_for_each_entry_safe(iocb, next_iocb,
10690 &pring->txcmplq, list) {
10691 if (iocb->vport != vport)
10692 continue;
10693 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10694 }
10695 pring->flag = prev_pring_flag;
10696 }
James Smart92d7f7b2007-06-17 19:56:38 -050010697 }
James Smart92d7f7b2007-06-17 19:56:38 -050010698 spin_unlock_irqrestore(&phba->hbalock, flags);
10699
James Smarta257bf92009-04-06 18:48:10 -040010700 /* Cancel all the IOCBs from the completions list */
10701 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10702 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010703 return 1;
10704}
10705
James Smarte59058c2008-08-24 21:49:00 -040010706/**
James Smart3621a712009-04-06 18:47:14 -040010707 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010708 * @phba: Pointer to HBA context object.
10709 *
10710 * This function cleans up all iocb, buffers, mailbox commands
10711 * while shutting down the HBA. This function is called with no
10712 * lock held and always returns 1.
10713 * This function does the following to cleanup driver resources:
10714 * - Free discovery resources for each virtual port
10715 * - Cleanup any pending fabric iocbs
10716 * - Iterate through the iocb txq and free each entry
10717 * in the list.
10718 * - Free up any buffer posted to the HBA
10719 * - Free mailbox commands in the mailbox queue.
10720 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010721int
James Smart2e0fef82007-06-17 19:56:36 -050010722lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010723{
James Smart2534ba72007-04-25 09:52:20 -040010724 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010725 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010726 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010727 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010728 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010729 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010730 int i;
10731
10732 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010733 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010734
dea31012005-04-17 16:05:31 -050010735 lpfc_hba_down_prep(phba);
10736
James Smart523128e2018-09-10 10:30:46 -070010737 /* Disable softirqs, including timers from obtaining phba->hbalock */
10738 local_bh_disable();
10739
James Smart92d7f7b2007-06-17 19:56:38 -050010740 lpfc_fabric_abort_hba(phba);
10741
James Smart2e0fef82007-06-17 19:56:36 -050010742 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010743
James Smart895427b2017-02-12 13:52:30 -080010744 /*
10745 * Error everything on the txq since these iocbs
10746 * have not been given to the FW yet.
10747 */
10748 if (phba->sli_rev != LPFC_SLI_REV4) {
10749 for (i = 0; i < psli->num_rings; i++) {
10750 pring = &psli->sli3_ring[i];
10751 /* Only slow rings */
10752 if (pring->ringno == LPFC_ELS_RING) {
10753 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10754 /* Set the lpfc data pending flag */
10755 set_bit(LPFC_DATA_READY, &phba->data_flags);
10756 }
10757 list_splice_init(&pring->txq, &completions);
10758 }
10759 } else {
10760 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10761 pring = qp->pring;
10762 if (!pring)
10763 continue;
10764 spin_lock_irq(&pring->ring_lock);
10765 list_splice_init(&pring->txq, &completions);
10766 spin_unlock_irq(&pring->ring_lock);
10767 if (pring == phba->sli4_hba.els_wq->pring) {
10768 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10769 /* Set the lpfc data pending flag */
10770 set_bit(LPFC_DATA_READY, &phba->data_flags);
10771 }
10772 }
dea31012005-04-17 16:05:31 -050010773 }
James Smart2e0fef82007-06-17 19:56:36 -050010774 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010775
James Smarta257bf92009-04-06 18:48:10 -040010776 /* Cancel all the IOCBs from the completions list */
10777 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10778 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010779
James Smart0ff10d42008-01-11 01:52:36 -050010780 spin_lock_irqsave(&phba->hbalock, flags);
10781 list_splice_init(&phba->elsbuf, &completions);
10782 phba->elsbuf_cnt = 0;
10783 phba->elsbuf_prev_cnt = 0;
10784 spin_unlock_irqrestore(&phba->hbalock, flags);
10785
10786 while (!list_empty(&completions)) {
10787 list_remove_head(&completions, buf_ptr,
10788 struct lpfc_dmabuf, list);
10789 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10790 kfree(buf_ptr);
10791 }
10792
James Smart523128e2018-09-10 10:30:46 -070010793 /* Enable softirqs again, done with phba->hbalock */
10794 local_bh_enable();
10795
dea31012005-04-17 16:05:31 -050010796 /* Return any active mbox cmds */
10797 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010798
James Smartda0436e2009-05-22 14:51:39 -040010799 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010800 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010801 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010802
James Smartda0436e2009-05-22 14:51:39 -040010803 return 1;
10804}
James Smart92d7f7b2007-06-17 19:56:38 -050010805
James Smartda0436e2009-05-22 14:51:39 -040010806/**
James Smart3621a712009-04-06 18:47:14 -040010807 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010808 * @srcp: Source memory pointer.
10809 * @destp: Destination memory pointer.
10810 * @cnt: Number of words required to be copied.
10811 *
10812 * This function is used for copying data between driver memory
10813 * and the SLI memory. This function also changes the endianness
10814 * of each word if native endianness is different from SLI
10815 * endianness. This function can be called with or without
10816 * lock.
10817 **/
dea31012005-04-17 16:05:31 -050010818void
10819lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10820{
10821 uint32_t *src = srcp;
10822 uint32_t *dest = destp;
10823 uint32_t ldata;
10824 int i;
10825
10826 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10827 ldata = *src;
10828 ldata = le32_to_cpu(ldata);
10829 *dest = ldata;
10830 src++;
10831 dest++;
10832 }
10833}
10834
James Smarte59058c2008-08-24 21:49:00 -040010835
10836/**
James Smarta0c87cb2009-07-19 10:01:10 -040010837 * lpfc_sli_bemem_bcopy - SLI memory copy function
10838 * @srcp: Source memory pointer.
10839 * @destp: Destination memory pointer.
10840 * @cnt: Number of words required to be copied.
10841 *
10842 * This function is used for copying data between a data structure
10843 * with big endian representation to local endianness.
10844 * This function can be called with or without lock.
10845 **/
10846void
10847lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10848{
10849 uint32_t *src = srcp;
10850 uint32_t *dest = destp;
10851 uint32_t ldata;
10852 int i;
10853
10854 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10855 ldata = *src;
10856 ldata = be32_to_cpu(ldata);
10857 *dest = ldata;
10858 src++;
10859 dest++;
10860 }
10861}
10862
10863/**
James Smart3621a712009-04-06 18:47:14 -040010864 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010865 * @phba: Pointer to HBA context object.
10866 * @pring: Pointer to driver SLI ring object.
10867 * @mp: Pointer to driver buffer object.
10868 *
10869 * This function is called with no lock held.
10870 * It always return zero after adding the buffer to the postbufq
10871 * buffer list.
10872 **/
dea31012005-04-17 16:05:31 -050010873int
James Smart2e0fef82007-06-17 19:56:36 -050010874lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10875 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010876{
10877 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10878 later */
James Smart2e0fef82007-06-17 19:56:36 -050010879 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010880 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010881 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010882 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010883 return 0;
10884}
10885
James Smarte59058c2008-08-24 21:49:00 -040010886/**
James Smart3621a712009-04-06 18:47:14 -040010887 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010888 * @phba: Pointer to HBA context object.
10889 *
10890 * When HBQ is enabled, buffers are searched based on tags. This function
10891 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10892 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10893 * does not conflict with tags of buffer posted for unsolicited events.
10894 * The function returns the allocated tag. The function is called with
10895 * no locks held.
10896 **/
James Smart76bb24e2007-10-27 13:38:00 -040010897uint32_t
10898lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10899{
10900 spin_lock_irq(&phba->hbalock);
10901 phba->buffer_tag_count++;
10902 /*
10903 * Always set the QUE_BUFTAG_BIT to distiguish between
10904 * a tag assigned by HBQ.
10905 */
10906 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10907 spin_unlock_irq(&phba->hbalock);
10908 return phba->buffer_tag_count;
10909}
10910
James Smarte59058c2008-08-24 21:49:00 -040010911/**
James Smart3621a712009-04-06 18:47:14 -040010912 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010913 * @phba: Pointer to HBA context object.
10914 * @pring: Pointer to driver SLI ring object.
10915 * @tag: Buffer tag.
10916 *
10917 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10918 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10919 * iocb is posted to the response ring with the tag of the buffer.
10920 * This function searches the pring->postbufq list using the tag
10921 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10922 * iocb. If the buffer is found then lpfc_dmabuf object of the
10923 * buffer is returned to the caller else NULL is returned.
10924 * This function is called with no lock held.
10925 **/
James Smart76bb24e2007-10-27 13:38:00 -040010926struct lpfc_dmabuf *
10927lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10928 uint32_t tag)
10929{
10930 struct lpfc_dmabuf *mp, *next_mp;
10931 struct list_head *slp = &pring->postbufq;
10932
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010933 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010934 spin_lock_irq(&phba->hbalock);
10935 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10936 if (mp->buffer_tag == tag) {
10937 list_del_init(&mp->list);
10938 pring->postbufq_cnt--;
10939 spin_unlock_irq(&phba->hbalock);
10940 return mp;
10941 }
10942 }
10943
10944 spin_unlock_irq(&phba->hbalock);
10945 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010946 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010947 "ring %d Data x%lx x%p x%p x%x\n",
10948 pring->ringno, (unsigned long) tag,
10949 slp->next, slp->prev, pring->postbufq_cnt);
10950
10951 return NULL;
10952}
dea31012005-04-17 16:05:31 -050010953
James Smarte59058c2008-08-24 21:49:00 -040010954/**
James Smart3621a712009-04-06 18:47:14 -040010955 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010956 * @phba: Pointer to HBA context object.
10957 * @pring: Pointer to driver SLI ring object.
10958 * @phys: DMA address of the buffer.
10959 *
10960 * This function searches the buffer list using the dma_address
10961 * of unsolicited event to find the driver's lpfc_dmabuf object
10962 * corresponding to the dma_address. The function returns the
10963 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10964 * This function is called by the ct and els unsolicited event
10965 * handlers to get the buffer associated with the unsolicited
10966 * event.
10967 *
10968 * This function is called with no lock held.
10969 **/
dea31012005-04-17 16:05:31 -050010970struct lpfc_dmabuf *
10971lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10972 dma_addr_t phys)
10973{
10974 struct lpfc_dmabuf *mp, *next_mp;
10975 struct list_head *slp = &pring->postbufq;
10976
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010977 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010978 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010979 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10980 if (mp->phys == phys) {
10981 list_del_init(&mp->list);
10982 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010983 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010984 return mp;
10985 }
10986 }
10987
James Smart2e0fef82007-06-17 19:56:36 -050010988 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010989 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010990 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010991 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010992 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010993 slp->next, slp->prev, pring->postbufq_cnt);
10994 return NULL;
10995}
10996
James Smarte59058c2008-08-24 21:49:00 -040010997/**
James Smart3621a712009-04-06 18:47:14 -040010998 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010999 * @phba: Pointer to HBA context object.
11000 * @cmdiocb: Pointer to driver command iocb object.
11001 * @rspiocb: Pointer to driver response iocb object.
11002 *
11003 * This function is the completion handler for the abort iocbs for
11004 * ELS commands. This function is called from the ELS ring event
11005 * handler with no lock held. This function frees memory resources
11006 * associated with the abort iocb.
11007 **/
dea31012005-04-17 16:05:31 -050011008static void
James Smart2e0fef82007-06-17 19:56:36 -050011009lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11010 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050011011{
James Smart2e0fef82007-06-17 19:56:36 -050011012 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040011013 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050011014 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040011015
11016 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050011017
11018 /*
11019 * Assume that the port already completed and returned, or
11020 * will return the iocb. Just Log the message.
11021 */
James Smart2680eea2007-04-25 09:52:55 -040011022 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
11023 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
11024
James Smart2e0fef82007-06-17 19:56:36 -050011025 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040011026 if (phba->sli_rev < LPFC_SLI_REV4) {
James Smartfaa832e2018-07-31 17:23:18 -070011027 if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
11028 irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
11029 irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
11030 spin_unlock_irq(&phba->hbalock);
11031 goto release_iocb;
11032 }
James Smart45ed1192009-10-02 15:17:02 -040011033 if (abort_iotag != 0 &&
11034 abort_iotag <= phba->sli.last_iotag)
11035 abort_iocb =
11036 phba->sli.iocbq_lookup[abort_iotag];
11037 } else
11038 /* For sli4 the abort_tag is the XRI,
11039 * so the abort routine puts the iotag of the iocb
11040 * being aborted in the context field of the abort
11041 * IOCB.
11042 */
11043 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040011044
James Smart2a9bf3d2010-06-07 15:24:45 -040011045 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
11046 "0327 Cannot abort els iocb %p "
11047 "with tag %x context %x, abort status %x, "
11048 "abort code %x\n",
11049 abort_iocb, abort_iotag, abort_context,
11050 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040011051
James Smartff78d8f2011-12-13 13:21:35 -050011052 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040011053 }
James Smartfaa832e2018-07-31 17:23:18 -070011054release_iocb:
James Bottomley604a3e32005-10-29 10:28:33 -050011055 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050011056 return;
11057}
11058
James Smarte59058c2008-08-24 21:49:00 -040011059/**
James Smart3621a712009-04-06 18:47:14 -040011060 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040011061 * @phba: Pointer to HBA context object.
11062 * @cmdiocb: Pointer to driver command iocb object.
11063 * @rspiocb: Pointer to driver response iocb object.
11064 *
11065 * The function is called from SLI ring event handler with no
11066 * lock held. This function is the completion handler for ELS commands
11067 * which are aborted. The function frees memory resources used for
11068 * the aborted ELS commands.
11069 **/
James Smart92d7f7b2007-06-17 19:56:38 -050011070static void
11071lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11072 struct lpfc_iocbq *rspiocb)
11073{
11074 IOCB_t *irsp = &rspiocb->iocb;
11075
11076 /* ELS cmd tag <ulpIoTag> completes */
11077 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040011078 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050011079 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011080 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050011081 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050011082 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
11083 lpfc_ct_free_iocb(phba, cmdiocb);
11084 else
11085 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050011086 return;
11087}
11088
James Smarte59058c2008-08-24 21:49:00 -040011089/**
James Smart5af5eee2010-10-22 11:06:38 -040011090 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040011091 * @phba: Pointer to HBA context object.
11092 * @pring: Pointer to driver SLI ring object.
11093 * @cmdiocb: Pointer to driver command iocb object.
11094 *
James Smart5af5eee2010-10-22 11:06:38 -040011095 * This function issues an abort iocb for the provided command iocb down to
11096 * the port. Other than the case the outstanding command iocb is an abort
11097 * request, this function issues abort out unconditionally. This function is
11098 * called with hbalock held. The function returns 0 when it fails due to
11099 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040011100 **/
James Smart5af5eee2010-10-22 11:06:38 -040011101static int
11102lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050011103 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050011104{
James Smart2e0fef82007-06-17 19:56:36 -050011105 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011106 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050011107 IOCB_t *icmd = NULL;
11108 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011109 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040011110 unsigned long iflags;
James Smartfaa832e2018-07-31 17:23:18 -070011111 struct lpfc_nodelist *ndlp;
James Smart07951072007-04-25 09:51:38 -040011112
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011113 lockdep_assert_held(&phba->hbalock);
11114
James Smart92d7f7b2007-06-17 19:56:38 -050011115 /*
11116 * There are certain command types we don't want to abort. And we
11117 * don't want to abort commands that are already in the process of
11118 * being aborted.
James Smart07951072007-04-25 09:51:38 -040011119 */
11120 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050011121 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050011122 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11123 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040011124 return 0;
11125
dea31012005-04-17 16:05:31 -050011126 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050011127 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011128 if (abtsiocbp == NULL)
11129 return 0;
dea31012005-04-17 16:05:31 -050011130
James Smart07951072007-04-25 09:51:38 -040011131 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050011132 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040011133 */
11134 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11135
dea31012005-04-17 16:05:31 -050011136 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040011137 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
11138 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040011139 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040011140 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040011141 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
James Smartfaa832e2018-07-31 17:23:18 -070011142 } else {
James Smartda0436e2009-05-22 14:51:39 -040011143 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
James Smartfaa832e2018-07-31 17:23:18 -070011144 if (pring->ringno == LPFC_ELS_RING) {
11145 ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
11146 iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
11147 }
11148 }
dea31012005-04-17 16:05:31 -050011149 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040011150 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050011151
James Smart5ffc2662009-11-18 15:39:44 -050011152 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011153 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011154 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
11155 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011156 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
11157 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011158
James Smart2e0fef82007-06-17 19:56:36 -050011159 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040011160 iabt->ulpCommand = CMD_ABORT_XRI_CN;
11161 else
11162 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
11163
11164 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080011165 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040011166
James Smarte8b62012007-08-02 11:10:09 -040011167 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
11168 "0339 Abort xri x%x, original iotag x%x, "
11169 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040011170 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040011171 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040011172 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040011173
11174 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011175 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
11176 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040011177 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040011178 /* Note: both hbalock and ring_lock need to be set here */
11179 spin_lock_irqsave(&pring->ring_lock, iflags);
11180 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11181 abtsiocbp, 0);
11182 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11183 } else {
11184 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11185 abtsiocbp, 0);
11186 }
James Smart07951072007-04-25 09:51:38 -040011187
James Smartd7c255b2008-08-24 21:50:00 -040011188 if (retval)
11189 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040011190
11191 /*
11192 * Caller to this routine should check for IOCB_ERROR
11193 * and handle it properly. This routine no longer removes
11194 * iocb off txcmplq and call compl in case of IOCB_ERROR.
11195 */
11196 return retval;
11197}
11198
11199/**
11200 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
11201 * @phba: Pointer to HBA context object.
11202 * @pring: Pointer to driver SLI ring object.
11203 * @cmdiocb: Pointer to driver command iocb object.
11204 *
11205 * This function issues an abort iocb for the provided command iocb. In case
11206 * of unloading, the abort iocb will not be issued to commands on the ELS
11207 * ring. Instead, the callback function shall be changed to those commands
11208 * so that nothing happens when them finishes. This function is called with
11209 * hbalock held. The function returns 0 when the command iocb is an abort
11210 * request.
11211 **/
11212int
11213lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11214 struct lpfc_iocbq *cmdiocb)
11215{
11216 struct lpfc_vport *vport = cmdiocb->vport;
11217 int retval = IOCB_ERROR;
11218 IOCB_t *icmd = NULL;
11219
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011220 lockdep_assert_held(&phba->hbalock);
11221
James Smart5af5eee2010-10-22 11:06:38 -040011222 /*
11223 * There are certain command types we don't want to abort. And we
11224 * don't want to abort commands that are already in the process of
11225 * being aborted.
11226 */
11227 icmd = &cmdiocb->iocb;
11228 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
11229 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11230 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11231 return 0;
11232
Dick Kennedy1234a6d2017-09-29 17:34:29 -070011233 if (!pring) {
11234 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11235 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11236 else
11237 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11238 goto abort_iotag_exit;
11239 }
11240
James Smart5af5eee2010-10-22 11:06:38 -040011241 /*
11242 * If we're unloading, don't abort iocb on the ELS ring, but change
11243 * the callback so that nothing happens when it finishes.
11244 */
11245 if ((vport->load_flag & FC_UNLOADING) &&
11246 (pring->ringno == LPFC_ELS_RING)) {
11247 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11248 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11249 else
11250 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11251 goto abort_iotag_exit;
11252 }
11253
11254 /* Now, we try to issue the abort to the cmdiocb out */
11255 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
11256
James Smart07951072007-04-25 09:51:38 -040011257abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050011258 /*
11259 * Caller to this routine should check for IOCB_ERROR
11260 * and handle it properly. This routine no longer removes
11261 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040011262 */
James Smart2e0fef82007-06-17 19:56:36 -050011263 return retval;
dea31012005-04-17 16:05:31 -050011264}
11265
James Smarte59058c2008-08-24 21:49:00 -040011266/**
James Smart895427b2017-02-12 13:52:30 -080011267 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
11268 * @phba: Pointer to HBA context object.
11269 * @pring: Pointer to driver SLI ring object.
11270 * @cmdiocb: Pointer to driver command iocb object.
11271 *
11272 * This function issues an abort iocb for the provided command iocb down to
11273 * the port. Other than the case the outstanding command iocb is an abort
11274 * request, this function issues abort out unconditionally. This function is
11275 * called with hbalock held. The function returns 0 when it fails due to
11276 * memory allocation failure or when the command iocb is an abort request.
11277 **/
11278static int
11279lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11280 struct lpfc_iocbq *cmdiocb)
11281{
11282 struct lpfc_vport *vport = cmdiocb->vport;
11283 struct lpfc_iocbq *abtsiocbp;
James Smart205e8242018-03-05 12:04:03 -080011284 union lpfc_wqe128 *abts_wqe;
James Smart895427b2017-02-12 13:52:30 -080011285 int retval;
James Smart1fbf9742019-01-28 11:14:26 -080011286 int idx = cmdiocb->hba_wqidx;
James Smart895427b2017-02-12 13:52:30 -080011287
11288 /*
11289 * There are certain command types we don't want to abort. And we
11290 * don't want to abort commands that are already in the process of
11291 * being aborted.
11292 */
11293 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
11294 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
11295 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11296 return 0;
11297
11298 /* issue ABTS for this io based on iotag */
11299 abtsiocbp = __lpfc_sli_get_iocbq(phba);
11300 if (abtsiocbp == NULL)
11301 return 0;
11302
11303 /* This signals the response to set the correct status
11304 * before calling the completion handler
11305 */
11306 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11307
11308 /* Complete prepping the abort wqe and issue to the FW. */
11309 abts_wqe = &abtsiocbp->wqe;
James Smart1c368332018-11-29 16:09:37 -080011310
11311 /* Clear any stale WQE contents */
11312 memset(abts_wqe, 0, sizeof(union lpfc_wqe));
James Smart895427b2017-02-12 13:52:30 -080011313 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
11314
James Smart895427b2017-02-12 13:52:30 -080011315 /* word 7 */
James Smart895427b2017-02-12 13:52:30 -080011316 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
11317 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
11318 cmdiocb->iocb.ulpClass);
11319
11320 /* word 8 - tell the FW to abort the IO associated with this
11321 * outstanding exchange ID.
11322 */
11323 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
11324
11325 /* word 9 - this is the iotag for the abts_wqe completion. */
11326 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
11327 abtsiocbp->iotag);
11328
11329 /* word 10 */
James Smart895427b2017-02-12 13:52:30 -080011330 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
11331 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
11332
11333 /* word 11 */
11334 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11335 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
11336 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
11337
11338 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11339 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
11340 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080011341 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart1fbf9742019-01-28 11:14:26 -080011342 retval = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[idx],
11343 abtsiocbp);
Dick Kennedycd22d602017-08-23 16:55:35 -070011344 if (retval) {
James Smart895427b2017-02-12 13:52:30 -080011345 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11346 "6147 Failed abts issue_wqe with status x%x "
11347 "for oxid x%x\n",
11348 retval, cmdiocb->sli4_xritag);
11349 lpfc_sli_release_iocbq(phba, abtsiocbp);
11350 return retval;
11351 }
11352
11353 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11354 "6148 Drv Abort NVME Request Issued for "
11355 "ox_id x%x on reqtag x%x\n",
11356 cmdiocb->sli4_xritag,
11357 abtsiocbp->iotag);
11358
11359 return retval;
11360}
11361
11362/**
James Smart5af5eee2010-10-22 11:06:38 -040011363 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11364 * @phba: pointer to lpfc HBA data structure.
11365 *
11366 * This routine will abort all pending and outstanding iocbs to an HBA.
11367 **/
11368void
11369lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11370{
11371 struct lpfc_sli *psli = &phba->sli;
11372 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080011373 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011374 int i;
11375
James Smart895427b2017-02-12 13:52:30 -080011376 if (phba->sli_rev != LPFC_SLI_REV4) {
11377 for (i = 0; i < psli->num_rings; i++) {
11378 pring = &psli->sli3_ring[i];
11379 lpfc_sli_abort_iocb_ring(phba, pring);
11380 }
11381 return;
11382 }
11383 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11384 pring = qp->pring;
11385 if (!pring)
11386 continue;
James Smartdb55fba2014-04-04 13:52:02 -040011387 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040011388 }
11389}
11390
11391/**
James Smart3621a712009-04-06 18:47:14 -040011392 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040011393 * @iocbq: Pointer to driver iocb object.
11394 * @vport: Pointer to driver virtual port object.
11395 * @tgt_id: SCSI ID of the target.
11396 * @lun_id: LUN ID of the scsi device.
11397 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11398 *
James Smart3621a712009-04-06 18:47:14 -040011399 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040011400 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11401 * 0 if the filtering criteria is met for the given iocb and will return
11402 * 1 if the filtering criteria is not met.
11403 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11404 * given iocb is for the SCSI device specified by vport, tgt_id and
11405 * lun_id parameter.
11406 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11407 * given iocb is for the SCSI target specified by vport and tgt_id
11408 * parameters.
11409 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11410 * given iocb is for the SCSI host associated with the given vport.
11411 * This function is called with no locks held.
11412 **/
dea31012005-04-17 16:05:31 -050011413static int
James Smart51ef4c22007-08-02 11:10:31 -040011414lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11415 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011416 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011417{
James Smartc4908502019-01-28 11:14:28 -080011418 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011419 int rc = 1;
11420
James Smartb0e83012018-06-26 08:24:29 -070011421 if (iocbq->vport != vport)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011422 return rc;
11423
James Smartb0e83012018-06-26 08:24:29 -070011424 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
11425 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
James Smart51ef4c22007-08-02 11:10:31 -040011426 return rc;
11427
James Smartc4908502019-01-28 11:14:28 -080011428 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011429
James Smart495a7142008-06-14 22:52:59 -040011430 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050011431 return rc;
11432
11433 switch (ctx_cmd) {
11434 case LPFC_CTX_LUN:
James Smartb0e83012018-06-26 08:24:29 -070011435 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011436 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11437 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050011438 rc = 0;
11439 break;
11440 case LPFC_CTX_TGT:
James Smartb0e83012018-06-26 08:24:29 -070011441 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011442 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050011443 rc = 0;
11444 break;
dea31012005-04-17 16:05:31 -050011445 case LPFC_CTX_HOST:
11446 rc = 0;
11447 break;
11448 default:
11449 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070011450 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050011451 break;
11452 }
11453
11454 return rc;
11455}
11456
James Smarte59058c2008-08-24 21:49:00 -040011457/**
James Smart3621a712009-04-06 18:47:14 -040011458 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040011459 * @vport: Pointer to virtual port.
11460 * @tgt_id: SCSI ID of the target.
11461 * @lun_id: LUN ID of the scsi device.
11462 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11463 *
11464 * This function returns number of FCP commands pending for the vport.
11465 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11466 * commands pending on the vport associated with SCSI device specified
11467 * by tgt_id and lun_id parameters.
11468 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11469 * commands pending on the vport associated with SCSI target specified
11470 * by tgt_id parameter.
11471 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11472 * commands pending on the vport.
11473 * This function returns the number of iocbs which satisfy the filter.
11474 * This function is called without any lock held.
11475 **/
dea31012005-04-17 16:05:31 -050011476int
James Smart51ef4c22007-08-02 11:10:31 -040011477lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11478 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011479{
James Smart51ef4c22007-08-02 11:10:31 -040011480 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011481 struct lpfc_iocbq *iocbq;
11482 int sum, i;
dea31012005-04-17 16:05:31 -050011483
Johannes Thumshirn31979002016-07-18 16:06:03 +020011484 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011485 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11486 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011487
James Smart51ef4c22007-08-02 11:10:31 -040011488 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11489 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011490 sum++;
dea31012005-04-17 16:05:31 -050011491 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020011492 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011493
dea31012005-04-17 16:05:31 -050011494 return sum;
11495}
11496
James Smarte59058c2008-08-24 21:49:00 -040011497/**
James Smart3621a712009-04-06 18:47:14 -040011498 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040011499 * @phba: Pointer to HBA context object
11500 * @cmdiocb: Pointer to command iocb object.
11501 * @rspiocb: Pointer to response iocb object.
11502 *
11503 * This function is called when an aborted FCP iocb completes. This
11504 * function is called by the ring event handler with no lock held.
11505 * This function frees the iocb.
11506 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011507void
James Smart2e0fef82007-06-17 19:56:36 -050011508lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11509 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011510{
James Smartcb69f7d2011-12-13 13:21:57 -050011511 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040011512 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050011513 "original iotag x%x, abort cmd iotag x%x "
11514 "status 0x%x, reason 0x%x\n",
11515 cmdiocb->iocb.un.acxri.abortContextTag,
11516 cmdiocb->iocb.un.acxri.abortIoTag,
11517 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11518 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050011519 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011520 return;
11521}
11522
James Smarte59058c2008-08-24 21:49:00 -040011523/**
James Smart3621a712009-04-06 18:47:14 -040011524 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040011525 * @vport: Pointer to virtual port.
11526 * @pring: Pointer to driver SLI ring object.
11527 * @tgt_id: SCSI ID of the target.
11528 * @lun_id: LUN ID of the scsi device.
11529 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11530 *
11531 * This function sends an abort command for every SCSI command
11532 * associated with the given virtual port pending on the ring
11533 * filtered by lpfc_sli_validate_fcp_iocb function.
11534 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11535 * FCP iocbs associated with lun specified by tgt_id and lun_id
11536 * parameters
11537 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11538 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11539 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11540 * FCP iocbs associated with virtual port.
11541 * This function returns number of iocbs it failed to abort.
11542 * This function is called with no locks held.
11543 **/
dea31012005-04-17 16:05:31 -050011544int
James Smart51ef4c22007-08-02 11:10:31 -040011545lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11546 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050011547{
James Smart51ef4c22007-08-02 11:10:31 -040011548 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011549 struct lpfc_iocbq *iocbq;
11550 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070011551 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050011552 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050011553 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011554 int i;
dea31012005-04-17 16:05:31 -050011555
James Smartb0e83012018-06-26 08:24:29 -070011556 /* all I/Os are in process of being flushed */
11557 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH)
11558 return errcnt;
11559
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011560 for (i = 1; i <= phba->sli.last_iotag; i++) {
11561 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011562
James Smart51ef4c22007-08-02 11:10:31 -040011563 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011564 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011565 continue;
11566
James Smartafbd8d82013-09-06 12:22:13 -040011567 /*
11568 * If the iocbq is already being aborted, don't take a second
11569 * action, but do count it.
11570 */
11571 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11572 continue;
11573
dea31012005-04-17 16:05:31 -050011574 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011575 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011576 if (abtsiocb == NULL) {
11577 errcnt++;
11578 continue;
11579 }
dea31012005-04-17 16:05:31 -050011580
James Smartafbd8d82013-09-06 12:22:13 -040011581 /* indicate the IO is being aborted by the driver. */
11582 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11583
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011584 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011585 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11586 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011587 if (phba->sli_rev == LPFC_SLI_REV4)
11588 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11589 else
11590 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011591 abtsiocb->iocb.ulpLe = 1;
11592 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011593 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011594
James Smart5ffc2662009-11-18 15:39:44 -050011595 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011596 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011597 if (iocbq->iocb_flag & LPFC_IO_FCP)
11598 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011599 if (iocbq->iocb_flag & LPFC_IO_FOF)
11600 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011601
James Smart2e0fef82007-06-17 19:56:36 -050011602 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011603 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11604 else
11605 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11606
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011607 /* Setup callback routine and issue the command. */
11608 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011609 if (phba->sli_rev == LPFC_SLI_REV4) {
11610 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11611 if (!pring_s4)
11612 continue;
11613 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11614 abtsiocb, 0);
11615 } else
11616 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11617 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011618 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011619 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011620 errcnt++;
11621 continue;
11622 }
11623 }
11624
11625 return errcnt;
11626}
11627
James Smarte59058c2008-08-24 21:49:00 -040011628/**
James Smart98912dda2014-04-04 13:52:31 -040011629 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11630 * @vport: Pointer to virtual port.
11631 * @pring: Pointer to driver SLI ring object.
11632 * @tgt_id: SCSI ID of the target.
11633 * @lun_id: LUN ID of the scsi device.
11634 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11635 *
11636 * This function sends an abort command for every SCSI command
11637 * associated with the given virtual port pending on the ring
11638 * filtered by lpfc_sli_validate_fcp_iocb function.
11639 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11640 * FCP iocbs associated with lun specified by tgt_id and lun_id
11641 * parameters
11642 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11643 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11644 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11645 * FCP iocbs associated with virtual port.
11646 * This function returns number of iocbs it aborted .
11647 * This function is called with no locks held right after a taskmgmt
11648 * command is sent.
11649 **/
11650int
11651lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11652 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11653{
11654 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -080011655 struct lpfc_io_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011656 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011657 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011658 struct lpfc_iocbq *iocbq;
11659 IOCB_t *icmd;
11660 int sum, i, ret_val;
11661 unsigned long iflags;
James Smartc2017262019-01-28 11:14:37 -080011662 struct lpfc_sli_ring *pring_s4 = NULL;
James Smart98912dda2014-04-04 13:52:31 -040011663
James Smart59c68ea2018-04-09 14:24:25 -070011664 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011665
11666 /* all I/Os are in process of being flushed */
11667 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart59c68ea2018-04-09 14:24:25 -070011668 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011669 return 0;
11670 }
11671 sum = 0;
11672
11673 for (i = 1; i <= phba->sli.last_iotag; i++) {
11674 iocbq = phba->sli.iocbq_lookup[i];
11675
11676 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11677 cmd) != 0)
11678 continue;
11679
James Smartc2017262019-01-28 11:14:37 -080011680 /* Guard against IO completion being called at same time */
11681 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
11682 spin_lock(&lpfc_cmd->buf_lock);
11683
11684 if (!lpfc_cmd->pCmd) {
11685 spin_unlock(&lpfc_cmd->buf_lock);
11686 continue;
11687 }
11688
11689 if (phba->sli_rev == LPFC_SLI_REV4) {
11690 pring_s4 =
11691 phba->sli4_hba.hdwq[iocbq->hba_wqidx].fcp_wq->pring;
11692 if (!pring_s4) {
11693 spin_unlock(&lpfc_cmd->buf_lock);
11694 continue;
11695 }
11696 /* Note: both hbalock and ring_lock must be set here */
11697 spin_lock(&pring_s4->ring_lock);
11698 }
11699
James Smart98912dda2014-04-04 13:52:31 -040011700 /*
11701 * If the iocbq is already being aborted, don't take a second
11702 * action, but do count it.
11703 */
James Smartc2017262019-01-28 11:14:37 -080011704 if ((iocbq->iocb_flag & LPFC_DRIVER_ABORTED) ||
11705 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
11706 if (phba->sli_rev == LPFC_SLI_REV4)
11707 spin_unlock(&pring_s4->ring_lock);
11708 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011709 continue;
James Smartc2017262019-01-28 11:14:37 -080011710 }
James Smart98912dda2014-04-04 13:52:31 -040011711
11712 /* issue ABTS for this IOCB based on iotag */
11713 abtsiocbq = __lpfc_sli_get_iocbq(phba);
James Smartc2017262019-01-28 11:14:37 -080011714 if (!abtsiocbq) {
11715 if (phba->sli_rev == LPFC_SLI_REV4)
11716 spin_unlock(&pring_s4->ring_lock);
11717 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011718 continue;
James Smartc2017262019-01-28 11:14:37 -080011719 }
James Smart98912dda2014-04-04 13:52:31 -040011720
11721 icmd = &iocbq->iocb;
11722 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11723 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11724 if (phba->sli_rev == LPFC_SLI_REV4)
11725 abtsiocbq->iocb.un.acxri.abortIoTag =
11726 iocbq->sli4_xritag;
11727 else
11728 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11729 abtsiocbq->iocb.ulpLe = 1;
11730 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11731 abtsiocbq->vport = vport;
11732
11733 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011734 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011735 if (iocbq->iocb_flag & LPFC_IO_FCP)
11736 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011737 if (iocbq->iocb_flag & LPFC_IO_FOF)
11738 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011739
James Smart8c50d252014-09-03 12:58:16 -040011740 ndlp = lpfc_cmd->rdata->pnode;
11741
11742 if (lpfc_is_link_up(phba) &&
11743 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011744 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11745 else
11746 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11747
11748 /* Setup callback routine and issue the command. */
11749 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11750
11751 /*
11752 * Indicate the IO is being aborted by the driver and set
11753 * the caller's flag into the aborted IO.
11754 */
11755 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11756
11757 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart98912dda2014-04-04 13:52:31 -040011758 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11759 abtsiocbq, 0);
James Smart59c68ea2018-04-09 14:24:25 -070011760 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011761 } else {
11762 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11763 abtsiocbq, 0);
11764 }
11765
James Smartc2017262019-01-28 11:14:37 -080011766 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011767
11768 if (ret_val == IOCB_ERROR)
11769 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11770 else
11771 sum++;
11772 }
James Smart59c68ea2018-04-09 14:24:25 -070011773 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011774 return sum;
11775}
11776
11777/**
James Smart3621a712009-04-06 18:47:14 -040011778 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011779 * @phba: Pointer to HBA context object.
11780 * @cmdiocbq: Pointer to command iocb.
11781 * @rspiocbq: Pointer to response iocb.
11782 *
11783 * This function is the completion handler for iocbs issued using
11784 * lpfc_sli_issue_iocb_wait function. This function is called by the
11785 * ring event handler function without any lock held. This function
11786 * can be called from both worker thread context and interrupt
11787 * context. This function also can be called from other thread which
11788 * cleans up the SLI layer objects.
11789 * This function copy the contents of the response iocb to the
11790 * response iocb memory object provided by the caller of
11791 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11792 * sleeps for the iocb completion.
11793 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011794static void
11795lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11796 struct lpfc_iocbq *cmdiocbq,
11797 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011798{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011799 wait_queue_head_t *pdone_q;
11800 unsigned long iflags;
James Smartc4908502019-01-28 11:14:28 -080011801 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011802
James Smart2e0fef82007-06-17 19:56:36 -050011803 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011804 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11805
11806 /*
11807 * A time out has occurred for the iocb. If a time out
11808 * completion handler has been supplied, call it. Otherwise,
11809 * just free the iocbq.
11810 */
11811
11812 spin_unlock_irqrestore(&phba->hbalock, iflags);
11813 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11814 cmdiocbq->wait_iocb_cmpl = NULL;
11815 if (cmdiocbq->iocb_cmpl)
11816 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11817 else
11818 lpfc_sli_release_iocbq(phba, cmdiocbq);
11819 return;
11820 }
11821
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011822 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11823 if (cmdiocbq->context2 && rspiocbq)
11824 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11825 &rspiocbq->iocb, sizeof(IOCB_t));
11826
James Smart0f65ff62010-02-26 14:14:23 -050011827 /* Set the exchange busy flag for task management commands */
11828 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11829 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smartc4908502019-01-28 11:14:28 -080011830 lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
James Smart0f65ff62010-02-26 14:14:23 -050011831 cur_iocbq);
11832 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11833 }
11834
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011835 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011836 if (pdone_q)
11837 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011838 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011839 return;
11840}
11841
James Smarte59058c2008-08-24 21:49:00 -040011842/**
James Smartd11e31d2009-06-10 17:23:06 -040011843 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11844 * @phba: Pointer to HBA context object..
11845 * @piocbq: Pointer to command iocb.
11846 * @flag: Flag to test.
11847 *
11848 * This routine grabs the hbalock and then test the iocb_flag to
11849 * see if the passed in flag is set.
11850 * Returns:
11851 * 1 if flag is set.
11852 * 0 if flag is not set.
11853 **/
11854static int
11855lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11856 struct lpfc_iocbq *piocbq, uint32_t flag)
11857{
11858 unsigned long iflags;
11859 int ret;
11860
11861 spin_lock_irqsave(&phba->hbalock, iflags);
11862 ret = piocbq->iocb_flag & flag;
11863 spin_unlock_irqrestore(&phba->hbalock, iflags);
11864 return ret;
11865
11866}
11867
11868/**
James Smart3621a712009-04-06 18:47:14 -040011869 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011870 * @phba: Pointer to HBA context object..
11871 * @pring: Pointer to sli ring.
11872 * @piocb: Pointer to command iocb.
11873 * @prspiocbq: Pointer to response iocb.
11874 * @timeout: Timeout in number of seconds.
11875 *
11876 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011877 * iocb to complete. The iocb_cmpl field of the shall be used
11878 * to handle iocbs which time out. If the field is NULL, the
11879 * function shall free the iocbq structure. If more clean up is
11880 * needed, the caller is expected to provide a completion function
11881 * that will provide the needed clean up. If the iocb command is
11882 * not completed within timeout seconds, the function will either
11883 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11884 * completion function set in the iocb_cmpl field and then return
11885 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11886 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011887 * The function waits for the iocb completion using an
11888 * non-interruptible wait.
11889 * This function will sleep while waiting for iocb completion.
11890 * So, this function should not be called from any context which
11891 * does not allow sleeping. Due to the same reason, this function
11892 * cannot be called with interrupt disabled.
11893 * This function assumes that the iocb completions occur while
11894 * this function sleep. So, this function cannot be called from
11895 * the thread which process iocb completion for this ring.
11896 * This function clears the iocb_flag of the iocb object before
11897 * issuing the iocb and the iocb completion handler sets this
11898 * flag and wakes this thread when the iocb completes.
11899 * The contents of the response iocb will be copied to prspiocbq
11900 * by the completion handler when the command completes.
11901 * This function returns IOCB_SUCCESS when success.
11902 * This function is called with no lock held.
11903 **/
dea31012005-04-17 16:05:31 -050011904int
James Smart2e0fef82007-06-17 19:56:36 -050011905lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011906 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011907 struct lpfc_iocbq *piocb,
11908 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011909 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011910{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011911 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011912 long timeleft, timeout_req = 0;
11913 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011914 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011915 struct lpfc_iocbq *iocb;
11916 int txq_cnt = 0;
11917 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011918 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011919 unsigned long iflags;
11920 bool iocb_completed = true;
11921
James Smart895427b2017-02-12 13:52:30 -080011922 if (phba->sli_rev >= LPFC_SLI_REV4)
11923 pring = lpfc_sli4_calc_ring(phba, piocb);
11924 else
11925 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011926 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011927 * If the caller has provided a response iocbq buffer, then context2
11928 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011929 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011930 if (prspiocbq) {
11931 if (piocb->context2)
11932 return IOCB_ERROR;
11933 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011934 }
11935
James Smart5a0916b2013-07-15 18:31:42 -040011936 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011937 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11938 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011939 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011940
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011941 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011942 if (lpfc_readl(phba->HCregaddr, &creg_val))
11943 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011944 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11945 writel(creg_val, phba->HCregaddr);
11946 readl(phba->HCregaddr); /* flush */
11947 }
11948
James Smart2a9bf3d2010-06-07 15:24:45 -040011949 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11950 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011951 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011952 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011953 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011954 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011955 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011956 spin_lock_irqsave(&phba->hbalock, iflags);
11957 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011958
James Smart5a0916b2013-07-15 18:31:42 -040011959 /*
11960 * IOCB timed out. Inform the wake iocb wait
11961 * completion function and set local status
11962 */
11963
11964 iocb_completed = false;
11965 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11966 }
11967 spin_unlock_irqrestore(&phba->hbalock, iflags);
11968 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011969 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011970 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011971 /* Note: we are not indicating if the IOCB has a success
11972 * status or not - that's for the caller to check.
11973 * IOCB_SUCCESS means just that the command was sent and
11974 * completed. Not that it completed successfully.
11975 * */
James Smart7054a602007-04-25 09:52:34 -040011976 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011977 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011978 "0338 IOCB wait timeout error - no "
11979 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011980 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011981 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011982 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011983 "0330 IOCB wake NOT set, "
11984 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011985 timeout, (timeleft / jiffies));
11986 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011987 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011988 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011989 if (phba->cfg_log_verbose & LOG_SLI) {
11990 list_for_each_entry(iocb, &pring->txq, list) {
11991 txq_cnt++;
11992 }
11993 list_for_each_entry(iocb, &pring->txcmplq, list) {
11994 txcmplq_cnt++;
11995 }
11996 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11997 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11998 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11999 }
James Smart2a9bf3d2010-06-07 15:24:45 -040012000 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012001 } else {
12002 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012003 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012004 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012005 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050012006 }
12007
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012008 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050012009 if (lpfc_readl(phba->HCregaddr, &creg_val))
12010 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012011 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
12012 writel(creg_val, phba->HCregaddr);
12013 readl(phba->HCregaddr); /* flush */
12014 }
12015
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012016 if (prspiocbq)
12017 piocb->context2 = NULL;
12018
12019 piocb->context_un.wait_queue = NULL;
12020 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050012021 return retval;
12022}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012023
James Smarte59058c2008-08-24 21:49:00 -040012024/**
James Smart3621a712009-04-06 18:47:14 -040012025 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040012026 * @phba: Pointer to HBA context object.
12027 * @pmboxq: Pointer to driver mailbox object.
12028 * @timeout: Timeout in number of seconds.
12029 *
12030 * This function issues the mailbox to firmware and waits for the
12031 * mailbox command to complete. If the mailbox command is not
12032 * completed within timeout seconds, it returns MBX_TIMEOUT.
12033 * The function waits for the mailbox completion using an
12034 * interruptible wait. If the thread is woken up due to a
12035 * signal, MBX_TIMEOUT error is returned to the caller. Caller
12036 * should not free the mailbox resources, if this function returns
12037 * MBX_TIMEOUT.
12038 * This function will sleep while waiting for mailbox completion.
12039 * So, this function should not be called from any context which
12040 * does not allow sleeping. Due to the same reason, this function
12041 * cannot be called with interrupt disabled.
12042 * This function assumes that the mailbox completion occurs while
12043 * this function sleep. So, this function cannot be called from
12044 * the worker thread which processes mailbox completion.
12045 * This function is called in the context of HBA management
12046 * applications.
12047 * This function returns MBX_SUCCESS when successful.
12048 * This function is called with no lock held.
12049 **/
dea31012005-04-17 16:05:31 -050012050int
James Smart2e0fef82007-06-17 19:56:36 -050012051lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050012052 uint32_t timeout)
12053{
James Smarte29d74f2018-03-05 12:04:07 -080012054 struct completion mbox_done;
dea31012005-04-17 16:05:31 -050012055 int retval;
James Smart858c9f62007-06-17 19:56:39 -050012056 unsigned long flag;
dea31012005-04-17 16:05:31 -050012057
James Smart495a7142008-06-14 22:52:59 -040012058 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050012059 /* setup wake call as IOCB callback */
12060 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea31012005-04-17 16:05:31 -050012061
James Smarte29d74f2018-03-05 12:04:07 -080012062 /* setup context3 field to pass wait_queue pointer to wake function */
12063 init_completion(&mbox_done);
12064 pmboxq->context3 = &mbox_done;
dea31012005-04-17 16:05:31 -050012065 /* now issue the command */
12066 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050012067 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smarte29d74f2018-03-05 12:04:07 -080012068 wait_for_completion_timeout(&mbox_done,
12069 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040012070
James Smart858c9f62007-06-17 19:56:39 -050012071 spin_lock_irqsave(&phba->hbalock, flag);
James Smarte29d74f2018-03-05 12:04:07 -080012072 pmboxq->context3 = NULL;
James Smart7054a602007-04-25 09:52:34 -040012073 /*
12074 * if LPFC_MBX_WAKE flag is set the mailbox is completed
12075 * else do not free the resources.
12076 */
James Smartd7c47992010-06-08 18:31:54 -040012077 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050012078 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040012079 } else {
James Smart7054a602007-04-25 09:52:34 -040012080 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050012081 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12082 }
12083 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050012084 }
dea31012005-04-17 16:05:31 -050012085 return retval;
12086}
12087
James Smarte59058c2008-08-24 21:49:00 -040012088/**
James Smart3772a992009-05-22 14:50:54 -040012089 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040012090 * @phba: Pointer to HBA context.
12091 *
James Smart3772a992009-05-22 14:50:54 -040012092 * This function is called to shutdown the driver's mailbox sub-system.
12093 * It first marks the mailbox sub-system is in a block state to prevent
12094 * the asynchronous mailbox command from issued off the pending mailbox
12095 * command queue. If the mailbox command sub-system shutdown is due to
12096 * HBA error conditions such as EEH or ERATT, this routine shall invoke
12097 * the mailbox sub-system flush routine to forcefully bring down the
12098 * mailbox sub-system. Otherwise, if it is due to normal condition (such
12099 * as with offline or HBA function reset), this routine will wait for the
12100 * outstanding mailbox command to complete before invoking the mailbox
12101 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040012102 **/
James Smart3772a992009-05-22 14:50:54 -040012103void
James Smart618a5232012-06-12 13:54:36 -040012104lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040012105{
James Smart3772a992009-05-22 14:50:54 -040012106 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040012107 unsigned long timeout;
12108
James Smart618a5232012-06-12 13:54:36 -040012109 if (mbx_action == LPFC_MBX_NO_WAIT) {
12110 /* delay 100ms for port state */
12111 msleep(100);
12112 lpfc_sli_mbox_sys_flush(phba);
12113 return;
12114 }
James Smarta183a152011-10-10 21:32:43 -040012115 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050012116
James Smart523128e2018-09-10 10:30:46 -070012117 /* Disable softirqs, including timers from obtaining phba->hbalock */
12118 local_bh_disable();
12119
James Smart3772a992009-05-22 14:50:54 -040012120 spin_lock_irq(&phba->hbalock);
12121 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040012122
12123 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040012124 /* Determine how long we might wait for the active mailbox
12125 * command to be gracefully completed by firmware.
12126 */
James Smarta183a152011-10-10 21:32:43 -040012127 if (phba->sli.mbox_active)
12128 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
12129 phba->sli.mbox_active) *
12130 1000) + jiffies;
12131 spin_unlock_irq(&phba->hbalock);
12132
James Smart523128e2018-09-10 10:30:46 -070012133 /* Enable softirqs again, done with phba->hbalock */
12134 local_bh_enable();
12135
James Smart3772a992009-05-22 14:50:54 -040012136 while (phba->sli.mbox_active) {
12137 /* Check active mailbox complete status every 2ms */
12138 msleep(2);
12139 if (time_after(jiffies, timeout))
12140 /* Timeout, let the mailbox flush routine to
12141 * forcefully release active mailbox command
12142 */
12143 break;
12144 }
James Smart523128e2018-09-10 10:30:46 -070012145 } else {
James Smartd7069f02012-03-01 22:36:29 -050012146 spin_unlock_irq(&phba->hbalock);
12147
James Smart523128e2018-09-10 10:30:46 -070012148 /* Enable softirqs again, done with phba->hbalock */
12149 local_bh_enable();
12150 }
12151
James Smart3772a992009-05-22 14:50:54 -040012152 lpfc_sli_mbox_sys_flush(phba);
12153}
12154
12155/**
12156 * lpfc_sli_eratt_read - read sli-3 error attention events
12157 * @phba: Pointer to HBA context.
12158 *
12159 * This function is called to read the SLI3 device error attention registers
12160 * for possible error attention events. The caller must hold the hostlock
12161 * with spin_lock_irq().
12162 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012163 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040012164 * Register and returns 0 otherwise.
12165 **/
12166static int
12167lpfc_sli_eratt_read(struct lpfc_hba *phba)
12168{
James Smarted957682007-06-17 19:56:37 -050012169 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040012170
James Smart3772a992009-05-22 14:50:54 -040012171 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050012172 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12173 goto unplug_err;
12174
James Smart3772a992009-05-22 14:50:54 -040012175 if (ha_copy & HA_ERATT) {
12176 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050012177 if (lpfc_sli_read_hs(phba))
12178 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040012179
James Smart3772a992009-05-22 14:50:54 -040012180 /* Check if there is a deferred error condition is active */
12181 if ((HS_FFER1 & phba->work_hs) &&
12182 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012183 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040012184 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040012185 /* Clear all interrupt enable conditions */
12186 writel(0, phba->HCregaddr);
12187 readl(phba->HCregaddr);
12188 }
12189
12190 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040012191 phba->work_ha |= HA_ERATT;
12192 /* Indicate polling handles this ERATT */
12193 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012194 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012195 }
James Smart3772a992009-05-22 14:50:54 -040012196 return 0;
James Smart9940b972011-03-11 16:06:12 -050012197
12198unplug_err:
12199 /* Set the driver HS work bitmap */
12200 phba->work_hs |= UNPLUG_ERR;
12201 /* Set the driver HA work bitmap */
12202 phba->work_ha |= HA_ERATT;
12203 /* Indicate polling handles this ERATT */
12204 phba->hba_flag |= HBA_ERATT_HANDLED;
12205 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012206}
12207
James Smarte59058c2008-08-24 21:49:00 -040012208/**
James Smartda0436e2009-05-22 14:51:39 -040012209 * lpfc_sli4_eratt_read - read sli-4 error attention events
12210 * @phba: Pointer to HBA context.
12211 *
12212 * This function is called to read the SLI4 device error attention registers
12213 * for possible error attention events. The caller must hold the hostlock
12214 * with spin_lock_irq().
12215 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012216 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040012217 * Register and returns 0 otherwise.
12218 **/
12219static int
12220lpfc_sli4_eratt_read(struct lpfc_hba *phba)
12221{
12222 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050012223 uint32_t if_type, portsmphr;
12224 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040012225
James Smart2fcee4b2010-12-15 17:57:46 -050012226 /*
12227 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040012228 * registers for error attention. This can be changed later.
12229 */
James Smart2fcee4b2010-12-15 17:57:46 -050012230 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
12231 switch (if_type) {
12232 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050012233 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
12234 &uerr_sta_lo) ||
12235 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
12236 &uerr_sta_hi)) {
12237 phba->work_hs |= UNPLUG_ERR;
12238 phba->work_ha |= HA_ERATT;
12239 phba->hba_flag |= HBA_ERATT_HANDLED;
12240 return 1;
12241 }
James Smart2fcee4b2010-12-15 17:57:46 -050012242 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
12243 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
12244 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12245 "1423 HBA Unrecoverable error: "
12246 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
12247 "ue_mask_lo_reg=0x%x, "
12248 "ue_mask_hi_reg=0x%x\n",
12249 uerr_sta_lo, uerr_sta_hi,
12250 phba->sli4_hba.ue_mask_lo,
12251 phba->sli4_hba.ue_mask_hi);
12252 phba->work_status[0] = uerr_sta_lo;
12253 phba->work_status[1] = uerr_sta_hi;
12254 phba->work_ha |= HA_ERATT;
12255 phba->hba_flag |= HBA_ERATT_HANDLED;
12256 return 1;
12257 }
12258 break;
12259 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080012260 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050012261 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
12262 &portstat_reg.word0) ||
12263 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
12264 &portsmphr)){
12265 phba->work_hs |= UNPLUG_ERR;
12266 phba->work_ha |= HA_ERATT;
12267 phba->hba_flag |= HBA_ERATT_HANDLED;
12268 return 1;
12269 }
James Smart2fcee4b2010-12-15 17:57:46 -050012270 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
12271 phba->work_status[0] =
12272 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
12273 phba->work_status[1] =
12274 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
12275 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050012276 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050012277 "port status reg 0x%x, "
12278 "port smphr reg 0x%x, "
12279 "error 1=0x%x, error 2=0x%x\n",
12280 portstat_reg.word0,
12281 portsmphr,
12282 phba->work_status[0],
12283 phba->work_status[1]);
12284 phba->work_ha |= HA_ERATT;
12285 phba->hba_flag |= HBA_ERATT_HANDLED;
12286 return 1;
12287 }
12288 break;
12289 case LPFC_SLI_INTF_IF_TYPE_1:
12290 default:
James Smarta747c9c2009-11-18 15:41:10 -050012291 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050012292 "2886 HBA Error Attention on unsupported "
12293 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050012294 return 1;
James Smartda0436e2009-05-22 14:51:39 -040012295 }
James Smart2fcee4b2010-12-15 17:57:46 -050012296
James Smartda0436e2009-05-22 14:51:39 -040012297 return 0;
12298}
12299
12300/**
James Smart3621a712009-04-06 18:47:14 -040012301 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040012302 * @phba: Pointer to HBA context.
12303 *
James Smart3772a992009-05-22 14:50:54 -040012304 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040012305 * error attention register bit for error attention events.
12306 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012307 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040012308 * Register and returns 0 otherwise.
12309 **/
12310int
12311lpfc_sli_check_eratt(struct lpfc_hba *phba)
12312{
12313 uint32_t ha_copy;
12314
12315 /* If somebody is waiting to handle an eratt, don't process it
12316 * here. The brdkill function will do this.
12317 */
12318 if (phba->link_flag & LS_IGNORE_ERATT)
12319 return 0;
12320
12321 /* Check if interrupt handler handles this ERATT */
12322 spin_lock_irq(&phba->hbalock);
12323 if (phba->hba_flag & HBA_ERATT_HANDLED) {
12324 /* Interrupt handler has handled ERATT */
12325 spin_unlock_irq(&phba->hbalock);
12326 return 0;
12327 }
12328
James Smarta257bf92009-04-06 18:48:10 -040012329 /*
12330 * If there is deferred error attention, do not check for error
12331 * attention
12332 */
12333 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12334 spin_unlock_irq(&phba->hbalock);
12335 return 0;
12336 }
12337
James Smart3772a992009-05-22 14:50:54 -040012338 /* If PCI channel is offline, don't process it */
12339 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040012340 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012341 return 0;
12342 }
12343
12344 switch (phba->sli_rev) {
12345 case LPFC_SLI_REV2:
12346 case LPFC_SLI_REV3:
12347 /* Read chip Host Attention (HA) register */
12348 ha_copy = lpfc_sli_eratt_read(phba);
12349 break;
James Smartda0436e2009-05-22 14:51:39 -040012350 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050012351 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040012352 ha_copy = lpfc_sli4_eratt_read(phba);
12353 break;
James Smart3772a992009-05-22 14:50:54 -040012354 default:
12355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12356 "0299 Invalid SLI revision (%d)\n",
12357 phba->sli_rev);
12358 ha_copy = 0;
12359 break;
James Smart93996272008-08-24 21:50:30 -040012360 }
12361 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012362
12363 return ha_copy;
12364}
12365
12366/**
12367 * lpfc_intr_state_check - Check device state for interrupt handling
12368 * @phba: Pointer to HBA context.
12369 *
12370 * This inline routine checks whether a device or its PCI slot is in a state
12371 * that the interrupt should be handled.
12372 *
12373 * This function returns 0 if the device or the PCI slot is in a state that
12374 * interrupt should be handled, otherwise -EIO.
12375 */
12376static inline int
12377lpfc_intr_state_check(struct lpfc_hba *phba)
12378{
12379 /* If the pci channel is offline, ignore all the interrupts */
12380 if (unlikely(pci_channel_offline(phba->pcidev)))
12381 return -EIO;
12382
12383 /* Update device level interrupt statistics */
12384 phba->sli.slistat.sli_intr++;
12385
12386 /* Ignore all interrupts during initialization. */
12387 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12388 return -EIO;
12389
James Smart93996272008-08-24 21:50:30 -040012390 return 0;
12391}
12392
12393/**
James Smart3772a992009-05-22 14:50:54 -040012394 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040012395 * @irq: Interrupt number.
12396 * @dev_id: The device context pointer.
12397 *
James Smart93996272008-08-24 21:50:30 -040012398 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012399 * service routine when device with SLI-3 interface spec is enabled with
12400 * MSI-X multi-message interrupt mode and there are slow-path events in
12401 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12402 * interrupt mode, this function is called as part of the device-level
12403 * interrupt handler. When the PCI slot is in error recovery or the HBA
12404 * is undergoing initialization, the interrupt handler will not process
12405 * the interrupt. The link attention and ELS ring attention events are
12406 * handled by the worker thread. The interrupt handler signals the worker
12407 * thread and returns for these events. This function is called without
12408 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040012409 * structures.
12410 *
12411 * This function returns IRQ_HANDLED when interrupt is handled else it
12412 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040012413 **/
dea31012005-04-17 16:05:31 -050012414irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012415lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050012416{
James Smart2e0fef82007-06-17 19:56:36 -050012417 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050012418 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050012419 uint32_t work_ha_copy;
12420 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012421 unsigned long iflag;
dea31012005-04-17 16:05:31 -050012422 uint32_t control;
12423
James Smart92d7f7b2007-06-17 19:56:38 -050012424 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050012425 struct lpfc_vport *vport;
12426 struct lpfc_nodelist *ndlp;
12427 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050012428 LPFC_MBOXQ_t *pmb;
12429 int rc;
12430
dea31012005-04-17 16:05:31 -050012431 /*
12432 * Get the driver's phba structure from the dev_id and
12433 * assume the HBA is not interrupting.
12434 */
James Smart93996272008-08-24 21:50:30 -040012435 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050012436
12437 if (unlikely(!phba))
12438 return IRQ_NONE;
12439
dea31012005-04-17 16:05:31 -050012440 /*
James Smart93996272008-08-24 21:50:30 -040012441 * Stuff needs to be attented to when this function is invoked as an
12442 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012443 */
James Smart93996272008-08-24 21:50:30 -040012444 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012445 /* Check device state for handling interrupt */
12446 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012447 return IRQ_NONE;
12448 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050012449 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012450 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12451 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040012452 /* If somebody is waiting to handle an eratt don't process it
12453 * here. The brdkill function will do this.
12454 */
12455 if (phba->link_flag & LS_IGNORE_ERATT)
12456 ha_copy &= ~HA_ERATT;
12457 /* Check the need for handling ERATT in interrupt handler */
12458 if (ha_copy & HA_ERATT) {
12459 if (phba->hba_flag & HBA_ERATT_HANDLED)
12460 /* ERATT polling has handled ERATT */
12461 ha_copy &= ~HA_ERATT;
12462 else
12463 /* Indicate interrupt handler handles ERATT */
12464 phba->hba_flag |= HBA_ERATT_HANDLED;
12465 }
James Smarta257bf92009-04-06 18:48:10 -040012466
12467 /*
12468 * If there is deferred error attention, do not check for any
12469 * interrupt.
12470 */
12471 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012472 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012473 return IRQ_NONE;
12474 }
12475
James Smart93996272008-08-24 21:50:30 -040012476 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050012477 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12478 goto unplug_error;
12479
James Smarta747c9c2009-11-18 15:41:10 -050012480 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12481 HC_LAINT_ENA | HC_ERINT_ENA),
12482 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012483 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12484 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012485 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012486 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012487 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012488 } else
12489 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050012490
dea31012005-04-17 16:05:31 -050012491 work_ha_copy = ha_copy & phba->work_ha_mask;
12492
James Smart93996272008-08-24 21:50:30 -040012493 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050012494 if (work_ha_copy & HA_LATT) {
12495 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12496 /*
12497 * Turn off Link Attention interrupts
12498 * until CLEAR_LA done
12499 */
James Smart5b75da22008-12-04 22:39:35 -050012500 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012501 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050012502 if (lpfc_readl(phba->HCregaddr, &control))
12503 goto unplug_error;
dea31012005-04-17 16:05:31 -050012504 control &= ~HC_LAINT_ENA;
12505 writel(control, phba->HCregaddr);
12506 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012507 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012508 }
12509 else
12510 work_ha_copy &= ~HA_LATT;
12511 }
12512
James Smart93996272008-08-24 21:50:30 -040012513 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050012514 /*
12515 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12516 * the only slow ring.
12517 */
12518 status = (work_ha_copy &
12519 (HA_RXMASK << (4*LPFC_ELS_RING)));
12520 status >>= (4*LPFC_ELS_RING);
12521 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050012522 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012523 if (lpfc_readl(phba->HCregaddr, &control))
12524 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040012525
12526 lpfc_debugfs_slow_ring_trc(phba,
12527 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12528 control, status,
12529 (uint32_t)phba->sli.slistat.sli_intr);
12530
James Smart858c9f62007-06-17 19:56:39 -050012531 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040012532 lpfc_debugfs_slow_ring_trc(phba,
12533 "ISR Disable ring:"
12534 "pwork:x%x hawork:x%x wait:x%x",
12535 phba->work_ha, work_ha_copy,
12536 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012537 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012538
James Smart858c9f62007-06-17 19:56:39 -050012539 control &=
12540 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050012541 writel(control, phba->HCregaddr);
12542 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050012543 }
James Smarta58cbd52007-08-02 11:09:43 -040012544 else {
12545 lpfc_debugfs_slow_ring_trc(phba,
12546 "ISR slow ring: pwork:"
12547 "x%x hawork:x%x wait:x%x",
12548 phba->work_ha, work_ha_copy,
12549 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012550 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012551 }
James Smart5b75da22008-12-04 22:39:35 -050012552 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012553 }
12554 }
James Smart5b75da22008-12-04 22:39:35 -050012555 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012556 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050012557 if (lpfc_sli_read_hs(phba))
12558 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040012559 /*
12560 * Check if there is a deferred error condition
12561 * is active
12562 */
12563 if ((HS_FFER1 & phba->work_hs) &&
12564 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012565 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12566 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040012567 phba->hba_flag |= DEFER_ERATT;
12568 /* Clear all interrupt enable conditions */
12569 writel(0, phba->HCregaddr);
12570 readl(phba->HCregaddr);
12571 }
12572 }
12573
James Smart93996272008-08-24 21:50:30 -040012574 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050012575 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040012576 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040012577 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050012578 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050012579
12580 /* First check out the status word */
12581 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12582 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050012583 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050012584 /*
12585 * Stray Mailbox Interrupt, mbxCommand <cmd>
12586 * mbxStatus <status>
12587 */
James Smart09372822008-01-11 01:52:54 -050012588 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012589 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012590 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012591 "Interrupt mbxCommand x%x "
12592 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012593 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012594 pmbox->mbxCommand,
12595 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012596 /* clear mailbox attention bit */
12597 work_ha_copy &= ~HA_MBATT;
12598 } else {
James Smart97eab632008-04-07 10:16:05 -040012599 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012600 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012601 phba->last_completion_time = jiffies;
12602 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012603 if (pmb->mbox_cmpl) {
12604 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12605 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012606 if (pmb->out_ext_byte_len &&
James Smart3e1f0712018-11-29 16:09:29 -080012607 pmb->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -040012608 lpfc_sli_pcimem_bcopy(
12609 phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -080012610 pmb->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -040012611 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012612 }
James Smart09372822008-01-11 01:52:54 -050012613 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12614 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12615
12616 lpfc_debugfs_disc_trc(vport,
12617 LPFC_DISC_TRC_MBOX_VPORT,
12618 "MBOX dflt rpi: : "
12619 "status:x%x rpi:x%x",
12620 (uint32_t)pmbox->mbxStatus,
12621 pmbox->un.varWords[0], 0);
12622
12623 if (!pmbox->mbxStatus) {
12624 mp = (struct lpfc_dmabuf *)
James Smart3e1f0712018-11-29 16:09:29 -080012625 (pmb->ctx_buf);
James Smart09372822008-01-11 01:52:54 -050012626 ndlp = (struct lpfc_nodelist *)
James Smart3e1f0712018-11-29 16:09:29 -080012627 pmb->ctx_ndlp;
James Smart09372822008-01-11 01:52:54 -050012628
12629 /* Reg_LOGIN of dflt RPI was
12630 * successful. new lets get
12631 * rid of the RPI using the
12632 * same mbox buffer.
12633 */
12634 lpfc_unreg_login(phba,
12635 vport->vpi,
12636 pmbox->un.varWords[0],
12637 pmb);
12638 pmb->mbox_cmpl =
12639 lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080012640 pmb->ctx_buf = mp;
12641 pmb->ctx_ndlp = ndlp;
James Smart09372822008-01-11 01:52:54 -050012642 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012643 rc = lpfc_sli_issue_mbox(phba,
12644 pmb,
12645 MBX_NOWAIT);
12646 if (rc != MBX_BUSY)
12647 lpfc_printf_log(phba,
12648 KERN_ERR,
12649 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012650 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012651 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012652 if (rc != MBX_NOT_FINISHED)
12653 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012654 }
12655 }
James Smart5b75da22008-12-04 22:39:35 -050012656 spin_lock_irqsave(
12657 &phba->pport->work_port_lock,
12658 iflag);
James Smart09372822008-01-11 01:52:54 -050012659 phba->pport->work_port_events &=
12660 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012661 spin_unlock_irqrestore(
12662 &phba->pport->work_port_lock,
12663 iflag);
James Smart09372822008-01-11 01:52:54 -050012664 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012665 }
James Smart97eab632008-04-07 10:16:05 -040012666 } else
James Smart5b75da22008-12-04 22:39:35 -050012667 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012668
James Smart92d7f7b2007-06-17 19:56:38 -050012669 if ((work_ha_copy & HA_MBATT) &&
12670 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012671send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012672 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012673 do {
12674 rc = lpfc_sli_issue_mbox(phba, NULL,
12675 MBX_NOWAIT);
12676 } while (rc == MBX_NOT_FINISHED);
12677 if (rc != MBX_SUCCESS)
12678 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12679 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012680 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012681 }
12682
James Smart5b75da22008-12-04 22:39:35 -050012683 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012684 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012685 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012686 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012687 }
James Smart93996272008-08-24 21:50:30 -040012688 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012689unplug_error:
12690 spin_unlock_irqrestore(&phba->hbalock, iflag);
12691 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012692
James Smart3772a992009-05-22 14:50:54 -040012693} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012694
12695/**
James Smart3772a992009-05-22 14:50:54 -040012696 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012697 * @irq: Interrupt number.
12698 * @dev_id: The device context pointer.
12699 *
12700 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012701 * service routine when device with SLI-3 interface spec is enabled with
12702 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12703 * ring event in the HBA. However, when the device is enabled with either
12704 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12705 * device-level interrupt handler. When the PCI slot is in error recovery
12706 * or the HBA is undergoing initialization, the interrupt handler will not
12707 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12708 * the intrrupt context. This function is called without any lock held.
12709 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012710 *
12711 * This function returns IRQ_HANDLED when interrupt is handled else it
12712 * returns IRQ_NONE.
12713 **/
12714irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012715lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012716{
12717 struct lpfc_hba *phba;
12718 uint32_t ha_copy;
12719 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012720 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012721 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012722
12723 /* Get the driver's phba structure from the dev_id and
12724 * assume the HBA is not interrupting.
12725 */
12726 phba = (struct lpfc_hba *) dev_id;
12727
12728 if (unlikely(!phba))
12729 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012730
12731 /*
James Smart93996272008-08-24 21:50:30 -040012732 * Stuff needs to be attented to when this function is invoked as an
12733 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012734 */
James Smart93996272008-08-24 21:50:30 -040012735 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012736 /* Check device state for handling interrupt */
12737 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012738 return IRQ_NONE;
12739 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012740 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12741 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012742 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012743 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012744 /*
12745 * If there is deferred error attention, do not check for
12746 * any interrupt.
12747 */
12748 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012749 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012750 return IRQ_NONE;
12751 }
James Smart93996272008-08-24 21:50:30 -040012752 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12753 phba->HAregaddr);
12754 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012755 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012756 } else
12757 ha_copy = phba->ha_copy;
12758
12759 /*
12760 * Process all events on FCP ring. Take the optimized path for FCP IO.
12761 */
12762 ha_copy &= ~(phba->work_ha_mask);
12763
12764 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012765 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012766 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012767 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012768 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012769
12770 if (phba->cfg_multi_ring_support == 2) {
12771 /*
James Smart93996272008-08-24 21:50:30 -040012772 * Process all events on extra ring. Take the optimized path
12773 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012774 */
James Smart93996272008-08-24 21:50:30 -040012775 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012776 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012777 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012778 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012779 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012780 status);
12781 }
12782 }
dea31012005-04-17 16:05:31 -050012783 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012784} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012785
James Smart93996272008-08-24 21:50:30 -040012786/**
James Smart3772a992009-05-22 14:50:54 -040012787 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012788 * @irq: Interrupt number.
12789 * @dev_id: The device context pointer.
12790 *
James Smart3772a992009-05-22 14:50:54 -040012791 * This function is the HBA device-level interrupt handler to device with
12792 * SLI-3 interface spec, called from the PCI layer when either MSI or
12793 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12794 * requires driver attention. This function invokes the slow-path interrupt
12795 * attention handling function and fast-path interrupt attention handling
12796 * function in turn to process the relevant HBA attention events. This
12797 * function is called without any lock held. It gets the hbalock to access
12798 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012799 *
12800 * This function returns IRQ_HANDLED when interrupt is handled, else it
12801 * returns IRQ_NONE.
12802 **/
12803irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012804lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012805{
12806 struct lpfc_hba *phba;
12807 irqreturn_t sp_irq_rc, fp_irq_rc;
12808 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012809 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012810
12811 /*
12812 * Get the driver's phba structure from the dev_id and
12813 * assume the HBA is not interrupting.
12814 */
12815 phba = (struct lpfc_hba *) dev_id;
12816
12817 if (unlikely(!phba))
12818 return IRQ_NONE;
12819
James Smart3772a992009-05-22 14:50:54 -040012820 /* Check device state for handling interrupt */
12821 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012822 return IRQ_NONE;
12823
12824 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012825 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12826 spin_unlock(&phba->hbalock);
12827 return IRQ_HANDLED;
12828 }
12829
James Smart93996272008-08-24 21:50:30 -040012830 if (unlikely(!phba->ha_copy)) {
12831 spin_unlock(&phba->hbalock);
12832 return IRQ_NONE;
12833 } else if (phba->ha_copy & HA_ERATT) {
12834 if (phba->hba_flag & HBA_ERATT_HANDLED)
12835 /* ERATT polling has handled ERATT */
12836 phba->ha_copy &= ~HA_ERATT;
12837 else
12838 /* Indicate interrupt handler handles ERATT */
12839 phba->hba_flag |= HBA_ERATT_HANDLED;
12840 }
12841
James Smarta257bf92009-04-06 18:48:10 -040012842 /*
12843 * If there is deferred error attention, do not check for any interrupt.
12844 */
12845 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012846 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012847 return IRQ_NONE;
12848 }
12849
James Smart93996272008-08-24 21:50:30 -040012850 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012851 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12852 spin_unlock(&phba->hbalock);
12853 return IRQ_HANDLED;
12854 }
James Smarta747c9c2009-11-18 15:41:10 -050012855 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12856 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12857 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012858 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012859 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012860 readl(phba->HAregaddr); /* flush */
12861 spin_unlock(&phba->hbalock);
12862
12863 /*
12864 * Invokes slow-path host attention interrupt handling as appropriate.
12865 */
12866
12867 /* status of events with mailbox and link attention */
12868 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12869
12870 /* status of events with ELS ring */
12871 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12872 status2 >>= (4*LPFC_ELS_RING);
12873
12874 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012875 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012876 else
12877 sp_irq_rc = IRQ_NONE;
12878
12879 /*
12880 * Invoke fast-path host attention interrupt handling as appropriate.
12881 */
12882
12883 /* status of events with FCP ring */
12884 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12885 status1 >>= (4*LPFC_FCP_RING);
12886
12887 /* status of events with extra ring */
12888 if (phba->cfg_multi_ring_support == 2) {
12889 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12890 status2 >>= (4*LPFC_EXTRA_RING);
12891 } else
12892 status2 = 0;
12893
12894 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012895 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012896 else
12897 fp_irq_rc = IRQ_NONE;
12898
12899 /* Return device-level interrupt handling status */
12900 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012901} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012902
12903/**
James Smart4f774512009-05-22 14:52:35 -040012904 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12905 * @phba: pointer to lpfc hba data structure.
12906 *
12907 * This routine is invoked by the worker thread to process all the pending
12908 * SLI4 els abort xri events.
12909 **/
12910void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12911{
12912 struct lpfc_cq_event *cq_event;
12913
12914 /* First, declare the els xri abort event has been handled */
12915 spin_lock_irq(&phba->hbalock);
12916 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12917 spin_unlock_irq(&phba->hbalock);
12918 /* Now, handle all the els xri abort events */
12919 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12920 /* Get the first event from the head of the event queue */
12921 spin_lock_irq(&phba->hbalock);
12922 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12923 cq_event, struct lpfc_cq_event, list);
12924 spin_unlock_irq(&phba->hbalock);
12925 /* Notify aborted XRI for ELS work queue */
12926 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12927 /* Free the event processed back to the free pool */
12928 lpfc_sli4_cq_event_release(phba, cq_event);
12929 }
12930}
12931
James Smart341af102010-01-26 23:07:37 -050012932/**
12933 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12934 * @phba: pointer to lpfc hba data structure
12935 * @pIocbIn: pointer to the rspiocbq
12936 * @pIocbOut: pointer to the cmdiocbq
12937 * @wcqe: pointer to the complete wcqe
12938 *
12939 * This routine transfers the fields of a command iocbq to a response iocbq
12940 * by copying all the IOCB fields from command iocbq and transferring the
12941 * completion status information from the complete wcqe.
12942 **/
James Smart4f774512009-05-22 14:52:35 -040012943static void
James Smart341af102010-01-26 23:07:37 -050012944lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12945 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012946 struct lpfc_iocbq *pIocbOut,
12947 struct lpfc_wcqe_complete *wcqe)
12948{
James Smartaf227412013-10-10 12:23:10 -040012949 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012950 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012951 uint32_t status, max_response;
12952 struct lpfc_dmabuf *dmabuf;
12953 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012954 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12955
12956 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12957 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012958 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012959 status = bf_get(lpfc_wcqe_c_status, wcqe);
12960 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012961 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12962 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12963 pIocbIn->iocb.un.fcpi.fcpi_parm =
12964 pIocbOut->iocb.un.fcpi.fcpi_parm -
12965 wcqe->total_data_placed;
12966 else
12967 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012968 else {
James Smart4f774512009-05-22 14:52:35 -040012969 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012970 switch (pIocbOut->iocb.ulpCommand) {
12971 case CMD_ELS_REQUEST64_CR:
12972 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12973 bpl = (struct ulp_bde64 *)dmabuf->virt;
12974 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12975 max_response = bde.tus.f.bdeSize;
12976 break;
12977 case CMD_GEN_REQUEST64_CR:
12978 max_response = 0;
12979 if (!pIocbOut->context3)
12980 break;
12981 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12982 sizeof(struct ulp_bde64);
12983 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12984 bpl = (struct ulp_bde64 *)dmabuf->virt;
12985 for (i = 0; i < numBdes; i++) {
12986 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12987 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12988 max_response += bde.tus.f.bdeSize;
12989 }
12990 break;
12991 default:
12992 max_response = wcqe->total_data_placed;
12993 break;
12994 }
12995 if (max_response < wcqe->total_data_placed)
12996 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12997 else
12998 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12999 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050013000 }
James Smart341af102010-01-26 23:07:37 -050013001
James Smartacd68592012-01-18 16:25:09 -050013002 /* Convert BG errors for completion status */
13003 if (status == CQE_STATUS_DI_ERROR) {
13004 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
13005
13006 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
13007 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
13008 else
13009 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
13010
13011 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
13012 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
13013 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13014 BGS_GUARD_ERR_MASK;
13015 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
13016 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13017 BGS_APPTAG_ERR_MASK;
13018 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
13019 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13020 BGS_REFTAG_ERR_MASK;
13021
13022 /* Check to see if there was any good data before the error */
13023 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
13024 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13025 BGS_HI_WATER_MARK_PRESENT_MASK;
13026 pIocbIn->iocb.unsli3.sli3_bg.bghm =
13027 wcqe->total_data_placed;
13028 }
13029
13030 /*
13031 * Set ALL the error bits to indicate we don't know what
13032 * type of error it is.
13033 */
13034 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
13035 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13036 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
13037 BGS_GUARD_ERR_MASK);
13038 }
13039
James Smart341af102010-01-26 23:07:37 -050013040 /* Pick up HBA exchange busy condition */
13041 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
13042 spin_lock_irqsave(&phba->hbalock, iflags);
13043 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
13044 spin_unlock_irqrestore(&phba->hbalock, iflags);
13045 }
James Smart4f774512009-05-22 14:52:35 -040013046}
13047
13048/**
James Smart45ed1192009-10-02 15:17:02 -040013049 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
13050 * @phba: Pointer to HBA context object.
13051 * @wcqe: Pointer to work-queue completion queue entry.
13052 *
13053 * This routine handles an ELS work-queue completion event and construct
13054 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
13055 * discovery engine to handle.
13056 *
13057 * Return: Pointer to the receive IOCBQ, NULL otherwise.
13058 **/
13059static struct lpfc_iocbq *
13060lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
13061 struct lpfc_iocbq *irspiocbq)
13062{
James Smart895427b2017-02-12 13:52:30 -080013063 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040013064 struct lpfc_iocbq *cmdiocbq;
13065 struct lpfc_wcqe_complete *wcqe;
13066 unsigned long iflags;
13067
James Smart895427b2017-02-12 13:52:30 -080013068 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070013069 if (unlikely(!pring))
13070 return NULL;
James Smart895427b2017-02-12 13:52:30 -080013071
James Smart45ed1192009-10-02 15:17:02 -040013072 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040013073 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040013074 pring->stats.iocb_event++;
13075 /* Look up the ELS command IOCB and create pseudo response IOCB */
13076 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13077 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040013078 if (unlikely(!cmdiocbq)) {
Dick Kennedy401bb412017-09-29 17:34:28 -070013079 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040013080 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13081 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070013082 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
13083 wcqe->word0, wcqe->total_data_placed,
13084 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040013085 lpfc_sli_release_iocbq(phba, irspiocbq);
13086 return NULL;
13087 }
13088
Dick Kennedy401bb412017-09-29 17:34:28 -070013089 /* Put the iocb back on the txcmplq */
13090 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
13091 spin_unlock_irqrestore(&pring->ring_lock, iflags);
13092
James Smart45ed1192009-10-02 15:17:02 -040013093 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013094 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040013095
13096 return irspiocbq;
13097}
13098
James Smart8a5ca102017-11-20 16:00:30 -080013099inline struct lpfc_cq_event *
13100lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
13101{
13102 struct lpfc_cq_event *cq_event;
13103
13104 /* Allocate a new internal CQ_EVENT entry */
13105 cq_event = lpfc_sli4_cq_event_alloc(phba);
13106 if (!cq_event) {
13107 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13108 "0602 Failed to alloc CQ_EVENT entry\n");
13109 return NULL;
13110 }
13111
13112 /* Move the CQE into the event */
13113 memcpy(&cq_event->cqe, entry, size);
13114 return cq_event;
13115}
13116
James Smart45ed1192009-10-02 15:17:02 -040013117/**
James Smart04c68492009-05-22 14:52:52 -040013118 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
13119 * @phba: Pointer to HBA context object.
13120 * @cqe: Pointer to mailbox completion queue entry.
13121 *
13122 * This routine process a mailbox completion queue entry with asynchrous
13123 * event.
13124 *
13125 * Return: true if work posted to worker thread, otherwise false.
13126 **/
13127static bool
13128lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13129{
13130 struct lpfc_cq_event *cq_event;
13131 unsigned long iflags;
13132
13133 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13134 "0392 Async Event: word0:x%x, word1:x%x, "
13135 "word2:x%x, word3:x%x\n", mcqe->word0,
13136 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
13137
James Smart8a5ca102017-11-20 16:00:30 -080013138 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
13139 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040013140 return false;
James Smart04c68492009-05-22 14:52:52 -040013141 spin_lock_irqsave(&phba->hbalock, iflags);
13142 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
13143 /* Set the async event flag */
13144 phba->hba_flag |= ASYNC_EVENT;
13145 spin_unlock_irqrestore(&phba->hbalock, iflags);
13146
13147 return true;
13148}
13149
13150/**
13151 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
13152 * @phba: Pointer to HBA context object.
13153 * @cqe: Pointer to mailbox completion queue entry.
13154 *
13155 * This routine process a mailbox completion queue entry with mailbox
13156 * completion event.
13157 *
13158 * Return: true if work posted to worker thread, otherwise false.
13159 **/
13160static bool
13161lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13162{
13163 uint32_t mcqe_status;
13164 MAILBOX_t *mbox, *pmbox;
13165 struct lpfc_mqe *mqe;
13166 struct lpfc_vport *vport;
13167 struct lpfc_nodelist *ndlp;
13168 struct lpfc_dmabuf *mp;
13169 unsigned long iflags;
13170 LPFC_MBOXQ_t *pmb;
13171 bool workposted = false;
13172 int rc;
13173
13174 /* If not a mailbox complete MCQE, out by checking mailbox consume */
13175 if (!bf_get(lpfc_trailer_completed, mcqe))
13176 goto out_no_mqe_complete;
13177
13178 /* Get the reference to the active mbox command */
13179 spin_lock_irqsave(&phba->hbalock, iflags);
13180 pmb = phba->sli.mbox_active;
13181 if (unlikely(!pmb)) {
13182 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
13183 "1832 No pending MBOX command to handle\n");
13184 spin_unlock_irqrestore(&phba->hbalock, iflags);
13185 goto out_no_mqe_complete;
13186 }
13187 spin_unlock_irqrestore(&phba->hbalock, iflags);
13188 mqe = &pmb->u.mqe;
13189 pmbox = (MAILBOX_t *)&pmb->u.mqe;
13190 mbox = phba->mbox;
13191 vport = pmb->vport;
13192
13193 /* Reset heartbeat timer */
13194 phba->last_completion_time = jiffies;
13195 del_timer(&phba->sli.mbox_tmo);
13196
13197 /* Move mbox data to caller's mailbox region, do endian swapping */
13198 if (pmb->mbox_cmpl && mbox)
James Smart48f8fdb2018-05-04 20:37:51 -070013199 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040013200
James Smart73d91e52011-10-10 21:32:10 -040013201 /*
13202 * For mcqe errors, conditionally move a modified error code to
13203 * the mbox so that the error will not be missed.
13204 */
13205 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
13206 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
13207 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
13208 bf_set(lpfc_mqe_status, mqe,
13209 (LPFC_MBX_ERROR_RANGE | mcqe_status));
13210 }
James Smart04c68492009-05-22 14:52:52 -040013211 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13212 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13213 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
13214 "MBOX dflt rpi: status:x%x rpi:x%x",
13215 mcqe_status,
13216 pmbox->un.varWords[0], 0);
13217 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
James Smart3e1f0712018-11-29 16:09:29 -080013218 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
13219 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smart04c68492009-05-22 14:52:52 -040013220 /* Reg_LOGIN of dflt RPI was successful. Now lets get
13221 * RID of the PPI using the same mbox buffer.
13222 */
13223 lpfc_unreg_login(phba, vport->vpi,
13224 pmbox->un.varWords[0], pmb);
13225 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080013226 pmb->ctx_buf = mp;
13227 pmb->ctx_ndlp = ndlp;
James Smart04c68492009-05-22 14:52:52 -040013228 pmb->vport = vport;
13229 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
13230 if (rc != MBX_BUSY)
13231 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
13232 LOG_SLI, "0385 rc should "
13233 "have been MBX_BUSY\n");
13234 if (rc != MBX_NOT_FINISHED)
13235 goto send_current_mbox;
13236 }
13237 }
13238 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
13239 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
13240 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
13241
13242 /* There is mailbox completion work to do */
13243 spin_lock_irqsave(&phba->hbalock, iflags);
13244 __lpfc_mbox_cmpl_put(phba, pmb);
13245 phba->work_ha |= HA_MBATT;
13246 spin_unlock_irqrestore(&phba->hbalock, iflags);
13247 workposted = true;
13248
13249send_current_mbox:
13250 spin_lock_irqsave(&phba->hbalock, iflags);
13251 /* Release the mailbox command posting token */
13252 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13253 /* Setting active mailbox pointer need to be in sync to flag clear */
13254 phba->sli.mbox_active = NULL;
13255 spin_unlock_irqrestore(&phba->hbalock, iflags);
13256 /* Wake up worker thread to post the next pending mailbox command */
13257 lpfc_worker_wake_up(phba);
13258out_no_mqe_complete:
13259 if (bf_get(lpfc_trailer_consumed, mcqe))
13260 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
13261 return workposted;
13262}
13263
13264/**
13265 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
13266 * @phba: Pointer to HBA context object.
13267 * @cqe: Pointer to mailbox completion queue entry.
13268 *
13269 * This routine process a mailbox completion queue entry, it invokes the
13270 * proper mailbox complete handling or asynchrous event handling routine
13271 * according to the MCQE's async bit.
13272 *
13273 * Return: true if work posted to worker thread, otherwise false.
13274 **/
13275static bool
James Smart32517fc2019-01-28 11:14:33 -080013276lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13277 struct lpfc_cqe *cqe)
James Smart04c68492009-05-22 14:52:52 -040013278{
13279 struct lpfc_mcqe mcqe;
13280 bool workposted;
13281
James Smart32517fc2019-01-28 11:14:33 -080013282 cq->CQ_mbox++;
13283
James Smart04c68492009-05-22 14:52:52 -040013284 /* Copy the mailbox MCQE and convert endian order as needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013285 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
James Smart04c68492009-05-22 14:52:52 -040013286
13287 /* Invoke the proper event handling routine */
13288 if (!bf_get(lpfc_trailer_async, &mcqe))
13289 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
13290 else
13291 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
13292 return workposted;
13293}
13294
13295/**
James Smart4f774512009-05-22 14:52:35 -040013296 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
13297 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040013298 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040013299 * @wcqe: Pointer to work-queue completion queue entry.
13300 *
13301 * This routine handles an ELS work-queue completion event.
13302 *
13303 * Return: true if work posted to worker thread, otherwise false.
13304 **/
13305static bool
James Smart2a76a282012-08-03 12:35:54 -040013306lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013307 struct lpfc_wcqe_complete *wcqe)
13308{
James Smart4f774512009-05-22 14:52:35 -040013309 struct lpfc_iocbq *irspiocbq;
13310 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040013311 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050013312 int txq_cnt = 0;
13313 int txcmplq_cnt = 0;
13314 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040013315
James Smart11f0e342018-05-04 20:37:57 -070013316 /* Check for response status */
13317 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13318 /* Log the error status */
13319 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13320 "0357 ELS CQE error: status=x%x: "
13321 "CQE: %08x %08x %08x %08x\n",
13322 bf_get(lpfc_wcqe_c_status, wcqe),
13323 wcqe->word0, wcqe->total_data_placed,
13324 wcqe->parameter, wcqe->word3);
13325 }
13326
James Smart45ed1192009-10-02 15:17:02 -040013327 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040013328 irspiocbq = lpfc_sli_get_iocbq(phba);
13329 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050013330 if (!list_empty(&pring->txq))
13331 txq_cnt++;
13332 if (!list_empty(&pring->txcmplq))
13333 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040013334 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040013335 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
13336 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050013337 txq_cnt, phba->iocb_cnt,
13338 fcp_txcmplq_cnt,
13339 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040013340 return false;
James Smart4f774512009-05-22 14:52:35 -040013341 }
James Smart4f774512009-05-22 14:52:35 -040013342
James Smart45ed1192009-10-02 15:17:02 -040013343 /* Save off the slow-path queue event for work thread to process */
13344 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040013345 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040013346 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013347 &phba->sli4_hba.sp_queue_event);
13348 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040013349 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013350
James Smart45ed1192009-10-02 15:17:02 -040013351 return true;
James Smart4f774512009-05-22 14:52:35 -040013352}
13353
13354/**
13355 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13356 * @phba: Pointer to HBA context object.
13357 * @wcqe: Pointer to work-queue completion queue entry.
13358 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013359 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013360 * proper WQ release routine to the slow-path WQ.
13361 **/
13362static void
13363lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13364 struct lpfc_wcqe_release *wcqe)
13365{
James Smart2e90f4b2011-12-13 13:22:37 -050013366 /* sanity check on queue memory */
13367 if (unlikely(!phba->sli4_hba.els_wq))
13368 return;
James Smart4f774512009-05-22 14:52:35 -040013369 /* Check for the slow-path ELS work queue */
13370 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13371 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13372 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13373 else
13374 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13375 "2579 Slow-path wqe consume event carries "
13376 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13377 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13378 phba->sli4_hba.els_wq->queue_id);
13379}
13380
13381/**
13382 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13383 * @phba: Pointer to HBA context object.
13384 * @cq: Pointer to a WQ completion queue.
13385 * @wcqe: Pointer to work-queue completion queue entry.
13386 *
13387 * This routine handles an XRI abort event.
13388 *
13389 * Return: true if work posted to worker thread, otherwise false.
13390 **/
13391static bool
13392lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13393 struct lpfc_queue *cq,
13394 struct sli4_wcqe_xri_aborted *wcqe)
13395{
13396 bool workposted = false;
13397 struct lpfc_cq_event *cq_event;
13398 unsigned long iflags;
13399
James Smart4f774512009-05-22 14:52:35 -040013400 switch (cq->subtype) {
13401 case LPFC_FCP:
James Smart5e5b5112019-01-28 11:14:22 -080013402 lpfc_sli4_fcp_xri_aborted(phba, wcqe, cq->hdwq);
13403 workposted = false;
James Smart4f774512009-05-22 14:52:35 -040013404 break;
James Smart422c4cb2017-11-20 16:00:32 -080013405 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040013406 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080013407 cq_event = lpfc_cq_event_setup(
13408 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13409 if (!cq_event)
13410 return false;
James Smart5e5b5112019-01-28 11:14:22 -080013411 cq_event->hdwq = cq->hdwq;
James Smart4f774512009-05-22 14:52:35 -040013412 spin_lock_irqsave(&phba->hbalock, iflags);
13413 list_add_tail(&cq_event->list,
13414 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13415 /* Set the els xri abort event flag */
13416 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13417 spin_unlock_irqrestore(&phba->hbalock, iflags);
13418 workposted = true;
13419 break;
James Smart318083a2017-03-04 09:30:30 -080013420 case LPFC_NVME:
James Smart8a5ca102017-11-20 16:00:30 -080013421 /* Notify aborted XRI for NVME work queue */
13422 if (phba->nvmet_support)
13423 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13424 else
James Smart5e5b5112019-01-28 11:14:22 -080013425 lpfc_sli4_nvme_xri_aborted(phba, wcqe, cq->hdwq);
James Smart8a5ca102017-11-20 16:00:30 -080013426
13427 workposted = false;
James Smart318083a2017-03-04 09:30:30 -080013428 break;
James Smart4f774512009-05-22 14:52:35 -040013429 default:
13430 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080013431 "0603 Invalid CQ subtype %d: "
13432 "%08x %08x %08x %08x\n",
13433 cq->subtype, wcqe->word0, wcqe->parameter,
13434 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013435 workposted = false;
13436 break;
13437 }
13438 return workposted;
13439}
13440
James Smarte817e5d2018-12-13 15:17:53 -080013441#define FC_RCTL_MDS_DIAGS 0xF4
13442
James Smart4f774512009-05-22 14:52:35 -040013443/**
James Smart4d9ab992009-10-02 15:16:39 -040013444 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040013445 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040013446 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013447 *
James Smart4d9ab992009-10-02 15:16:39 -040013448 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013449 *
13450 * Return: true if work posted to worker thread, otherwise false.
13451 **/
13452static bool
James Smart4d9ab992009-10-02 15:16:39 -040013453lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13454{
13455 bool workposted = false;
James Smarte817e5d2018-12-13 15:17:53 -080013456 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040013457 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13458 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070013459 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040013460 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040013461 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040013462 unsigned long iflags;
13463
James Smart2e90f4b2011-12-13 13:22:37 -050013464 /* sanity check on queue memory */
13465 if (unlikely(!hrq) || unlikely(!drq))
13466 return workposted;
13467
James Smart7851fe22011-07-22 18:36:52 -040013468 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13469 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13470 else
13471 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13472 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040013473 goto out;
13474
13475 status = bf_get(lpfc_rcqe_status, rcqe);
13476 switch (status) {
13477 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13478 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13479 "2537 Receive Frame Truncated!!\n");
13480 case FC_STATUS_RQ_SUCCESS:
13481 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013482 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040013483 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13484 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040013485 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040013486 spin_unlock_irqrestore(&phba->hbalock, iflags);
13487 goto out;
13488 }
James Smartb84daac2012-08-03 12:35:13 -040013489 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013490 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040013491 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080013492
James Smarte817e5d2018-12-13 15:17:53 -080013493 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13494
13495 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
13496 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
13497 spin_unlock_irqrestore(&phba->hbalock, iflags);
13498 /* Handle MDS Loopback frames */
13499 lpfc_sli4_handle_mds_loopback(phba->pport, dma_buf);
13500 break;
13501 }
13502
13503 /* save off the frame for the work thread to process */
James Smart4d9ab992009-10-02 15:16:39 -040013504 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013505 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040013506 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040013507 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040013508 spin_unlock_irqrestore(&phba->hbalock, iflags);
13509 workposted = true;
13510 break;
James Smart4d9ab992009-10-02 15:16:39 -040013511 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013512 if (phba->nvmet_support) {
13513 tgtp = phba->targetport->private;
13514 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13515 "6402 RQE Error x%x, posted %d err_cnt "
13516 "%d: %x %x %x\n",
13517 status, hrq->RQ_buf_posted,
13518 hrq->RQ_no_posted_buf,
13519 atomic_read(&tgtp->rcv_fcp_cmd_in),
13520 atomic_read(&tgtp->rcv_fcp_cmd_out),
13521 atomic_read(&tgtp->xmt_fcp_release));
13522 }
13523 /* fallthrough */
13524
13525 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040013526 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040013527 /* Post more buffers if possible */
13528 spin_lock_irqsave(&phba->hbalock, iflags);
13529 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13530 spin_unlock_irqrestore(&phba->hbalock, iflags);
13531 workposted = true;
13532 break;
13533 }
13534out:
13535 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040013536}
13537
13538/**
13539 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13540 * @phba: Pointer to HBA context object.
13541 * @cq: Pointer to the completion queue.
James Smart32517fc2019-01-28 11:14:33 -080013542 * @cqe: Pointer to a completion queue entry.
James Smart4d9ab992009-10-02 15:16:39 -040013543 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013544 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040013545 * entry.
13546 *
13547 * Return: true if work posted to worker thread, otherwise false.
13548 **/
13549static bool
13550lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013551 struct lpfc_cqe *cqe)
13552{
James Smart45ed1192009-10-02 15:17:02 -040013553 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040013554 bool workposted = false;
13555
13556 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013557 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013558
13559 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040013560 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040013561 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040013562 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040013563 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040013564 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013565 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013566 break;
13567 case CQE_CODE_RELEASE_WQE:
13568 /* Process the WQ release event */
13569 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013570 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013571 break;
13572 case CQE_CODE_XRI_ABORTED:
13573 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013574 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013575 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013576 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013577 break;
James Smart4d9ab992009-10-02 15:16:39 -040013578 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013579 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013580 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013581 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013582 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013583 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013584 break;
James Smart4f774512009-05-22 14:52:35 -040013585 default:
13586 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13587 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013588 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013589 break;
13590 }
13591 return workposted;
13592}
13593
13594/**
James Smart4f774512009-05-22 14:52:35 -040013595 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13596 * @phba: Pointer to HBA context object.
13597 * @eqe: Pointer to fast-path event queue entry.
13598 *
13599 * This routine process a event queue entry from the slow-path event queue.
13600 * It will check the MajorCode and MinorCode to determine this is for a
13601 * completion event on a completion queue, if not, an error shall be logged
13602 * and just return. Otherwise, it will get to the corresponding completion
13603 * queue and process all the entries on that completion queue, rearm the
13604 * completion queue, and then return.
13605 *
13606 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013607static void
James Smart67d12732012-08-03 12:36:13 -040013608lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13609 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013610{
James Smart67d12732012-08-03 12:36:13 -040013611 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013612 uint16_t cqid;
13613
James Smart4f774512009-05-22 14:52:35 -040013614 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013615 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013616
James Smart4f774512009-05-22 14:52:35 -040013617 list_for_each_entry(childq, &speq->child_list, list) {
13618 if (childq->queue_id == cqid) {
13619 cq = childq;
13620 break;
13621 }
13622 }
13623 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013624 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13625 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13626 "0365 Slow-path CQ identifier "
13627 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013628 return;
James Smart4f774512009-05-22 14:52:35 -040013629 }
13630
James Smart895427b2017-02-12 13:52:30 -080013631 /* Save EQ associated with this CQ */
13632 cq->assoc_qp = speq;
13633
James Smart6a828b02019-01-28 11:14:31 -080013634 if (!queue_work_on(cq->chann, phba->wq, &cq->spwork))
Dick Kennedyf485c182017-09-29 17:34:34 -070013635 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13636 "0390 Cannot schedule soft IRQ "
13637 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13638 cqid, cq->queue_id, smp_processor_id());
13639}
13640
13641/**
James Smart32517fc2019-01-28 11:14:33 -080013642 * __lpfc_sli4_process_cq - Process elements of a CQ
Dick Kennedyf485c182017-09-29 17:34:34 -070013643 * @phba: Pointer to HBA context object.
James Smart32517fc2019-01-28 11:14:33 -080013644 * @cq: Pointer to CQ to be processed
13645 * @handler: Routine to process each cqe
13646 * @delay: Pointer to usdelay to set in case of rescheduling of the handler
Dick Kennedyf485c182017-09-29 17:34:34 -070013647 *
James Smart32517fc2019-01-28 11:14:33 -080013648 * This routine processes completion queue entries in a CQ. While a valid
13649 * queue element is found, the handler is called. During processing checks
13650 * are made for periodic doorbell writes to let the hardware know of
13651 * element consumption.
Dick Kennedyf485c182017-09-29 17:34:34 -070013652 *
James Smart32517fc2019-01-28 11:14:33 -080013653 * If the max limit on cqes to process is hit, or there are no more valid
13654 * entries, the loop stops. If we processed a sufficient number of elements,
13655 * meaning there is sufficient load, rather than rearming and generating
13656 * another interrupt, a cq rescheduling delay will be set. A delay of 0
13657 * indicates no rescheduling.
13658 *
13659 * Returns True if work scheduled, False otherwise.
Dick Kennedyf485c182017-09-29 17:34:34 -070013660 **/
James Smart32517fc2019-01-28 11:14:33 -080013661static bool
13662__lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
13663 bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
13664 struct lpfc_cqe *), unsigned long *delay)
Dick Kennedyf485c182017-09-29 17:34:34 -070013665{
Dick Kennedyf485c182017-09-29 17:34:34 -070013666 struct lpfc_cqe *cqe;
13667 bool workposted = false;
James Smart32517fc2019-01-28 11:14:33 -080013668 int count = 0, consumed = 0;
13669 bool arm = true;
13670
13671 /* default - no reschedule */
13672 *delay = 0;
13673
13674 if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
13675 goto rearm_and_exit;
Dick Kennedyf485c182017-09-29 17:34:34 -070013676
James Smart4f774512009-05-22 14:52:35 -040013677 /* Process all the entries to the CQ */
James Smart32517fc2019-01-28 11:14:33 -080013678 cqe = lpfc_sli4_cq_get(cq);
13679 while (cqe) {
13680#if defined(CONFIG_SCSI_LPFC_DEBUG_FS) && defined(BUILD_NVME)
13681 if (phba->ktime_on)
13682 cq->isr_timestamp = ktime_get_ns();
13683 else
13684 cq->isr_timestamp = 0;
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013685#endif
James Smart32517fc2019-01-28 11:14:33 -080013686 workposted |= handler(phba, cq, cqe);
13687 __lpfc_sli4_consume_cqe(phba, cq, cqe);
13688
13689 consumed++;
13690 if (!(++count % cq->max_proc_limit))
13691 break;
13692
13693 if (!(count % cq->notify_interval)) {
13694 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
13695 LPFC_QUEUE_NOARM);
13696 consumed = 0;
James Smart4f774512009-05-22 14:52:35 -040013697 }
James Smartb84daac2012-08-03 12:35:13 -040013698
James Smart32517fc2019-01-28 11:14:33 -080013699 cqe = lpfc_sli4_cq_get(cq);
13700 }
13701 if (count >= phba->cfg_cq_poll_threshold) {
13702 *delay = 1;
13703 arm = false;
13704 }
13705
13706 /* Track the max number of CQEs processed in 1 EQ */
13707 if (count > cq->CQ_max_cqe)
13708 cq->CQ_max_cqe = count;
13709
13710 cq->assoc_qp->EQ_cqe_cnt += count;
13711
13712 /* Catch the no cq entry condition */
13713 if (unlikely(count == 0))
13714 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13715 "0369 No entry from completion queue "
13716 "qid=%d\n", cq->queue_id);
13717
13718 cq->queue_claimed = 0;
13719
13720rearm_and_exit:
13721 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
13722 arm ? LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
13723
13724 return workposted;
13725}
13726
13727/**
13728 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13729 * @cq: pointer to CQ to process
13730 *
13731 * This routine calls the cq processing routine with a handler specific
13732 * to the type of queue bound to it.
13733 *
13734 * The CQ routine returns two values: the first is the calling status,
13735 * which indicates whether work was queued to the background discovery
13736 * thread. If true, the routine should wakeup the discovery thread;
13737 * the second is the delay parameter. If non-zero, rather than rearming
13738 * the CQ and yet another interrupt, the CQ handler should be queued so
13739 * that it is processed in a subsequent polling action. The value of
13740 * the delay indicates when to reschedule it.
13741 **/
13742static void
13743__lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
13744{
13745 struct lpfc_hba *phba = cq->phba;
13746 unsigned long delay;
13747 bool workposted = false;
13748
13749 /* Process and rearm the CQ */
13750 switch (cq->type) {
13751 case LPFC_MCQ:
13752 workposted |= __lpfc_sli4_process_cq(phba, cq,
13753 lpfc_sli4_sp_handle_mcqe,
13754 &delay);
13755 break;
13756 case LPFC_WCQ:
13757 if (cq->subtype == LPFC_FCP || cq->subtype == LPFC_NVME)
13758 workposted |= __lpfc_sli4_process_cq(phba, cq,
13759 lpfc_sli4_fp_handle_cqe,
13760 &delay);
13761 else
13762 workposted |= __lpfc_sli4_process_cq(phba, cq,
13763 lpfc_sli4_sp_handle_cqe,
13764 &delay);
James Smart4f774512009-05-22 14:52:35 -040013765 break;
13766 default:
13767 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13768 "0370 Invalid completion queue type (%d)\n",
13769 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013770 return;
James Smart4f774512009-05-22 14:52:35 -040013771 }
13772
James Smart32517fc2019-01-28 11:14:33 -080013773 if (delay) {
13774 if (!queue_delayed_work_on(cq->chann, phba->wq,
13775 &cq->sched_spwork, delay))
13776 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13777 "0394 Cannot schedule soft IRQ "
13778 "for cqid=%d on CPU %d\n",
13779 cq->queue_id, cq->chann);
13780 }
James Smart4f774512009-05-22 14:52:35 -040013781
13782 /* wake up worker thread if there are works to be done */
13783 if (workposted)
13784 lpfc_worker_wake_up(phba);
13785}
13786
13787/**
James Smart32517fc2019-01-28 11:14:33 -080013788 * lpfc_sli4_sp_process_cq - slow-path work handler when started by
13789 * interrupt
13790 * @work: pointer to work element
13791 *
13792 * translates from the work handler and calls the slow-path handler.
13793 **/
13794static void
13795lpfc_sli4_sp_process_cq(struct work_struct *work)
13796{
13797 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
13798
13799 __lpfc_sli4_sp_process_cq(cq);
13800}
13801
13802/**
13803 * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
13804 * @work: pointer to work element
13805 *
13806 * translates from the work handler and calls the slow-path handler.
13807 **/
13808static void
13809lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
13810{
13811 struct lpfc_queue *cq = container_of(to_delayed_work(work),
13812 struct lpfc_queue, sched_spwork);
13813
13814 __lpfc_sli4_sp_process_cq(cq);
13815}
13816
13817/**
James Smart4f774512009-05-22 14:52:35 -040013818 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013819 * @phba: Pointer to HBA context object.
13820 * @cq: Pointer to associated CQ
13821 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013822 *
13823 * This routine process a fast-path work queue completion entry from fast-path
13824 * event queue for FCP command response completion.
13825 **/
13826static void
James Smart2a76a282012-08-03 12:35:54 -040013827lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013828 struct lpfc_wcqe_complete *wcqe)
13829{
James Smart2a76a282012-08-03 12:35:54 -040013830 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013831 struct lpfc_iocbq *cmdiocbq;
13832 struct lpfc_iocbq irspiocbq;
13833 unsigned long iflags;
13834
James Smart4f774512009-05-22 14:52:35 -040013835 /* Check for response status */
13836 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13837 /* If resource errors reported from HBA, reduce queue
13838 * depth of the SCSI device.
13839 */
James Smarte3d2b802012-08-14 14:25:43 -040013840 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13841 IOSTAT_LOCAL_REJECT)) &&
13842 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13843 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013844 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013845
James Smart4f774512009-05-22 14:52:35 -040013846 /* Log the error status */
James Smart11f0e342018-05-04 20:37:57 -070013847 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13848 "0373 FCP CQE error: status=x%x: "
13849 "CQE: %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013850 bf_get(lpfc_wcqe_c_status, wcqe),
James Smart11f0e342018-05-04 20:37:57 -070013851 wcqe->word0, wcqe->total_data_placed,
13852 wcqe->parameter, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013853 }
13854
13855 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013856 spin_lock_irqsave(&pring->ring_lock, iflags);
13857 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013858 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13859 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013860 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013861 if (unlikely(!cmdiocbq)) {
13862 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13863 "0374 FCP complete with no corresponding "
13864 "cmdiocb: iotag (%d)\n",
13865 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13866 return;
13867 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013868#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13869 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13870#endif
James Smart895427b2017-02-12 13:52:30 -080013871 if (cmdiocbq->iocb_cmpl == NULL) {
13872 if (cmdiocbq->wqe_cmpl) {
13873 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13874 spin_lock_irqsave(&phba->hbalock, iflags);
13875 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13876 spin_unlock_irqrestore(&phba->hbalock, iflags);
13877 }
13878
13879 /* Pass the cmd_iocb and the wcqe to the upper layer */
13880 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13881 return;
13882 }
James Smart4f774512009-05-22 14:52:35 -040013883 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13884 "0375 FCP cmdiocb not callback function "
13885 "iotag: (%d)\n",
13886 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13887 return;
13888 }
13889
13890 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013891 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013892
James Smart0f65ff62010-02-26 14:14:23 -050013893 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13894 spin_lock_irqsave(&phba->hbalock, iflags);
13895 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13896 spin_unlock_irqrestore(&phba->hbalock, iflags);
13897 }
13898
James Smart4f774512009-05-22 14:52:35 -040013899 /* Pass the cmd_iocb and the rsp state to the upper layer */
13900 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13901}
13902
13903/**
13904 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13905 * @phba: Pointer to HBA context object.
13906 * @cq: Pointer to completion queue.
13907 * @wcqe: Pointer to work-queue completion queue entry.
13908 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013909 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013910 * proper WQ release routine to the slow-path WQ.
13911 **/
13912static void
13913lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13914 struct lpfc_wcqe_release *wcqe)
13915{
13916 struct lpfc_queue *childwq;
13917 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013918 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013919
13920 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013921 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013922 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013923 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013924 lpfc_sli4_wq_release(childwq,
13925 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080013926 if (childwq->q_flag & HBA_NVMET_WQFULL)
13927 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040013928 wqid_matched = true;
13929 break;
13930 }
13931 }
13932 /* Report warning log message if no match found */
13933 if (wqid_matched != true)
13934 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13935 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013936 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013937}
13938
13939/**
James Smart2d7dbc42017-02-12 13:52:35 -080013940 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13941 * @phba: Pointer to HBA context object.
13942 * @rcqe: Pointer to receive-queue completion queue entry.
13943 *
13944 * This routine process a receive-queue completion queue entry.
13945 *
13946 * Return: true if work posted to worker thread, otherwise false.
13947 **/
13948static bool
13949lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13950 struct lpfc_rcqe *rcqe)
13951{
13952 bool workposted = false;
13953 struct lpfc_queue *hrq;
13954 struct lpfc_queue *drq;
13955 struct rqb_dmabuf *dma_buf;
13956 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013957 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013958 uint32_t status, rq_id;
13959 unsigned long iflags;
13960 uint32_t fctl, idx;
13961
13962 if ((phba->nvmet_support == 0) ||
13963 (phba->sli4_hba.nvmet_cqset == NULL))
13964 return workposted;
13965
13966 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13967 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13968 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13969
13970 /* sanity check on queue memory */
13971 if (unlikely(!hrq) || unlikely(!drq))
13972 return workposted;
13973
13974 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13975 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13976 else
13977 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13978
13979 if ((phba->nvmet_support == 0) ||
13980 (rq_id != hrq->queue_id))
13981 return workposted;
13982
13983 status = bf_get(lpfc_rcqe_status, rcqe);
13984 switch (status) {
13985 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13986 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13987 "6126 Receive Frame Truncated!!\n");
James Smart78e1d202017-06-01 21:07:09 -070013988 /* Drop thru */
James Smart2d7dbc42017-02-12 13:52:35 -080013989 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080013990 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013991 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080013992 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13993 if (!dma_buf) {
13994 hrq->RQ_no_buf_found++;
13995 spin_unlock_irqrestore(&phba->hbalock, iflags);
13996 goto out;
13997 }
13998 spin_unlock_irqrestore(&phba->hbalock, iflags);
13999 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070014000 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080014001 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
14002
14003 /* Just some basic sanity checks on FCP Command frame */
14004 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
14005 fc_hdr->fh_f_ctl[1] << 8 |
14006 fc_hdr->fh_f_ctl[2]);
14007 if (((fctl &
14008 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
14009 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
14010 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
14011 goto drop;
14012
14013 if (fc_hdr->fh_type == FC_TYPE_FCP) {
14014 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080014015 lpfc_nvmet_unsol_fcp_event(
Dick Kennedy66d7ce92017-08-23 16:55:42 -070014016 phba, idx, dma_buf,
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070014017 cq->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080014018 return false;
14019 }
14020drop:
14021 lpfc_in_buf_free(phba, &dma_buf->dbuf);
14022 break;
James Smart2d7dbc42017-02-12 13:52:35 -080014023 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070014024 if (phba->nvmet_support) {
14025 tgtp = phba->targetport->private;
14026 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
14027 "6401 RQE Error x%x, posted %d err_cnt "
14028 "%d: %x %x %x\n",
14029 status, hrq->RQ_buf_posted,
14030 hrq->RQ_no_posted_buf,
14031 atomic_read(&tgtp->rcv_fcp_cmd_in),
14032 atomic_read(&tgtp->rcv_fcp_cmd_out),
14033 atomic_read(&tgtp->xmt_fcp_release));
14034 }
14035 /* fallthrough */
14036
14037 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080014038 hrq->RQ_no_posted_buf++;
14039 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080014040 break;
14041 }
14042out:
14043 return workposted;
14044}
14045
14046/**
James Smart895427b2017-02-12 13:52:30 -080014047 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart32517fc2019-01-28 11:14:33 -080014048 * @phba: adapter with cq
James Smart4f774512009-05-22 14:52:35 -040014049 * @cq: Pointer to the completion queue.
14050 * @eqe: Pointer to fast-path completion queue entry.
14051 *
14052 * This routine process a fast-path work queue completion entry from fast-path
14053 * event queue for FCP command response completion.
James Smart32517fc2019-01-28 11:14:33 -080014054 *
14055 * Return: true if work posted to worker thread, otherwise false.
James Smart4f774512009-05-22 14:52:35 -040014056 **/
James Smart32517fc2019-01-28 11:14:33 -080014057static bool
James Smart895427b2017-02-12 13:52:30 -080014058lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040014059 struct lpfc_cqe *cqe)
14060{
14061 struct lpfc_wcqe_release wcqe;
14062 bool workposted = false;
14063
14064 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070014065 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040014066
14067 /* Check and process for different type of WCQE and dispatch */
14068 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
14069 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080014070 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040014071 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040014072 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040014073 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080014074 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
14075 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
14076 (struct lpfc_wcqe_complete *)&wcqe);
14077 if (cq->subtype == LPFC_NVME_LS)
14078 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040014079 (struct lpfc_wcqe_complete *)&wcqe);
14080 break;
14081 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040014082 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040014083 /* Process the WQ release event */
14084 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
14085 (struct lpfc_wcqe_release *)&wcqe);
14086 break;
14087 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040014088 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040014089 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040014090 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040014091 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14092 (struct sli4_wcqe_xri_aborted *)&wcqe);
14093 break;
James Smart895427b2017-02-12 13:52:30 -080014094 case CQE_CODE_RECEIVE_V1:
14095 case CQE_CODE_RECEIVE:
14096 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080014097 if (cq->subtype == LPFC_NVMET) {
14098 workposted = lpfc_sli4_nvmet_handle_rcqe(
14099 phba, cq, (struct lpfc_rcqe *)&wcqe);
14100 }
James Smart895427b2017-02-12 13:52:30 -080014101 break;
James Smart4f774512009-05-22 14:52:35 -040014102 default:
14103 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080014104 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040014105 bf_get(lpfc_wcqe_c_code, &wcqe));
14106 break;
14107 }
14108 return workposted;
14109}
14110
14111/**
James Smart67d12732012-08-03 12:36:13 -040014112 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040014113 * @phba: Pointer to HBA context object.
14114 * @eqe: Pointer to fast-path event queue entry.
14115 *
14116 * This routine process a event queue entry from the fast-path event queue.
14117 * It will check the MajorCode and MinorCode to determine this is for a
14118 * completion event on a completion queue, if not, an error shall be logged
14119 * and just return. Otherwise, it will get to the corresponding completion
14120 * queue and process all the entries on the completion queue, rearm the
14121 * completion queue, and then return.
14122 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070014123static void
James Smart32517fc2019-01-28 11:14:33 -080014124lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
14125 struct lpfc_eqe *eqe)
James Smart4f774512009-05-22 14:52:35 -040014126{
James Smart895427b2017-02-12 13:52:30 -080014127 struct lpfc_queue *cq = NULL;
James Smart32517fc2019-01-28 11:14:33 -080014128 uint32_t qidx = eq->hdwq;
James Smart2d7dbc42017-02-12 13:52:35 -080014129 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040014130
James Smartcb5172e2010-03-15 11:25:07 -040014131 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040014132 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040014133 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040014134 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040014135 bf_get_le32(lpfc_eqe_major_code, eqe),
14136 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070014137 return;
James Smart4f774512009-05-22 14:52:35 -040014138 }
14139
James Smart67d12732012-08-03 12:36:13 -040014140 /* Get the reference to the corresponding CQ */
14141 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14142
James Smart6a828b02019-01-28 11:14:31 -080014143 /* Use the fast lookup method first */
14144 if (cqid <= phba->sli4_hba.cq_max) {
14145 cq = phba->sli4_hba.cq_lookup[cqid];
14146 if (cq)
14147 goto work_cq;
James Smartcdb42be2019-01-28 11:14:21 -080014148 }
14149
14150 /* Next check for NVMET completion */
James Smart2d7dbc42017-02-12 13:52:35 -080014151 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
14152 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
14153 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
14154 /* Process NVMET unsol rcv */
14155 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
14156 goto process_cq;
14157 }
14158 }
14159
James Smart895427b2017-02-12 13:52:30 -080014160 if (phba->sli4_hba.nvmels_cq &&
14161 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
14162 /* Process NVME unsol rcv */
14163 cq = phba->sli4_hba.nvmels_cq;
14164 }
14165
14166 /* Otherwise this is a Slow path event */
14167 if (cq == NULL) {
James Smartcdb42be2019-01-28 11:14:21 -080014168 lpfc_sli4_sp_handle_eqe(phba, eqe,
14169 phba->sli4_hba.hdwq[qidx].hba_eq);
Dick Kennedyf485c182017-09-29 17:34:34 -070014170 return;
James Smart67d12732012-08-03 12:36:13 -040014171 }
14172
James Smart895427b2017-02-12 13:52:30 -080014173process_cq:
James Smart4f774512009-05-22 14:52:35 -040014174 if (unlikely(cqid != cq->queue_id)) {
14175 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14176 "0368 Miss-matched fast-path completion "
14177 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
14178 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070014179 return;
James Smart4f774512009-05-22 14:52:35 -040014180 }
14181
James Smart6a828b02019-01-28 11:14:31 -080014182work_cq:
James Smart45aa3122019-01-28 11:14:29 -080014183 if (!queue_work_on(cq->chann, phba->wq, &cq->irqwork))
Dick Kennedyf485c182017-09-29 17:34:34 -070014184 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14185 "0363 Cannot schedule soft IRQ "
14186 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14187 cqid, cq->queue_id, smp_processor_id());
14188}
14189
14190/**
James Smart32517fc2019-01-28 11:14:33 -080014191 * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
14192 * @cq: Pointer to CQ to be processed
Dick Kennedyf485c182017-09-29 17:34:34 -070014193 *
James Smart32517fc2019-01-28 11:14:33 -080014194 * This routine calls the cq processing routine with the handler for
14195 * fast path CQEs.
14196 *
14197 * The CQ routine returns two values: the first is the calling status,
14198 * which indicates whether work was queued to the background discovery
14199 * thread. If true, the routine should wakeup the discovery thread;
14200 * the second is the delay parameter. If non-zero, rather than rearming
14201 * the CQ and yet another interrupt, the CQ handler should be queued so
14202 * that it is processed in a subsequent polling action. The value of
14203 * the delay indicates when to reschedule it.
Dick Kennedyf485c182017-09-29 17:34:34 -070014204 **/
14205static void
James Smart32517fc2019-01-28 11:14:33 -080014206__lpfc_sli4_hba_process_cq(struct lpfc_queue *cq)
Dick Kennedyf485c182017-09-29 17:34:34 -070014207{
Dick Kennedyf485c182017-09-29 17:34:34 -070014208 struct lpfc_hba *phba = cq->phba;
James Smart32517fc2019-01-28 11:14:33 -080014209 unsigned long delay;
Dick Kennedyf485c182017-09-29 17:34:34 -070014210 bool workposted = false;
Dick Kennedyf485c182017-09-29 17:34:34 -070014211
James Smart32517fc2019-01-28 11:14:33 -080014212 /* process and rearm the CQ */
14213 workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
14214 &delay);
14215
14216 if (delay) {
14217 if (!queue_delayed_work_on(cq->chann, phba->wq,
14218 &cq->sched_irqwork, delay))
14219 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14220 "0367 Cannot schedule soft IRQ "
14221 "for cqid=%d on CPU %d\n",
14222 cq->queue_id, cq->chann);
James Smart4f774512009-05-22 14:52:35 -040014223 }
14224
James Smart4f774512009-05-22 14:52:35 -040014225 /* wake up worker thread if there are works to be done */
14226 if (workposted)
14227 lpfc_worker_wake_up(phba);
14228}
14229
James Smart32517fc2019-01-28 11:14:33 -080014230/**
14231 * lpfc_sli4_hba_process_cq - fast-path work handler when started by
14232 * interrupt
14233 * @work: pointer to work element
14234 *
14235 * translates from the work handler and calls the fast-path handler.
14236 **/
James Smart4f774512009-05-22 14:52:35 -040014237static void
James Smart32517fc2019-01-28 11:14:33 -080014238lpfc_sli4_hba_process_cq(struct work_struct *work)
James Smart4f774512009-05-22 14:52:35 -040014239{
James Smart32517fc2019-01-28 11:14:33 -080014240 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
James Smart4f774512009-05-22 14:52:35 -040014241
James Smart32517fc2019-01-28 11:14:33 -080014242 __lpfc_sli4_hba_process_cq(cq);
James Smart4f774512009-05-22 14:52:35 -040014243}
14244
James Smart32517fc2019-01-28 11:14:33 -080014245/**
14246 * lpfc_sli4_hba_process_cq - fast-path work handler when started by timer
14247 * @work: pointer to work element
14248 *
14249 * translates from the work handler and calls the fast-path handler.
14250 **/
14251static void
14252lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
14253{
14254 struct lpfc_queue *cq = container_of(to_delayed_work(work),
14255 struct lpfc_queue, sched_irqwork);
14256
14257 __lpfc_sli4_hba_process_cq(cq);
14258}
James Smart1ba981f2014-02-20 09:56:45 -050014259
14260/**
James Smart67d12732012-08-03 12:36:13 -040014261 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040014262 * @irq: Interrupt number.
14263 * @dev_id: The device context pointer.
14264 *
14265 * This function is directly called from the PCI layer as an interrupt
14266 * service routine when device with SLI-4 interface spec is enabled with
14267 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
14268 * ring event in the HBA. However, when the device is enabled with either
14269 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14270 * device-level interrupt handler. When the PCI slot is in error recovery
14271 * or the HBA is undergoing initialization, the interrupt handler will not
14272 * process the interrupt. The SCSI FCP fast-path ring event are handled in
14273 * the intrrupt context. This function is called without any lock held.
14274 * It gets the hbalock to access and update SLI data structures. Note that,
14275 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14276 * equal to that of FCP CQ index.
14277 *
James Smart67d12732012-08-03 12:36:13 -040014278 * The link attention and ELS ring attention events are handled
14279 * by the worker thread. The interrupt handler signals the worker thread
14280 * and returns for these events. This function is called without any lock
14281 * held. It gets the hbalock to access and update SLI data structures.
14282 *
James Smart4f774512009-05-22 14:52:35 -040014283 * This function returns IRQ_HANDLED when interrupt is handled else it
14284 * returns IRQ_NONE.
14285 **/
14286irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040014287lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040014288{
14289 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080014290 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040014291 struct lpfc_queue *fpeq;
James Smart4f774512009-05-22 14:52:35 -040014292 unsigned long iflag;
14293 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080014294 int hba_eqidx;
James Smart32517fc2019-01-28 11:14:33 -080014295 struct lpfc_eq_intr_info *eqi;
14296 uint32_t icnt;
James Smart4f774512009-05-22 14:52:35 -040014297
14298 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080014299 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14300 phba = hba_eq_hdl->phba;
14301 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040014302
14303 if (unlikely(!phba))
14304 return IRQ_NONE;
James Smartcdb42be2019-01-28 11:14:21 -080014305 if (unlikely(!phba->sli4_hba.hdwq))
James Smart5350d872011-10-10 21:33:49 -040014306 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014307
14308 /* Get to the EQ struct associated with this vector */
James Smartcdb42be2019-01-28 11:14:21 -080014309 fpeq = phba->sli4_hba.hdwq[hba_eqidx].hba_eq;
James Smart2e90f4b2011-12-13 13:22:37 -050014310 if (unlikely(!fpeq))
14311 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014312
14313 /* Check device state for handling interrupt */
14314 if (unlikely(lpfc_intr_state_check(phba))) {
14315 /* Check again for link_state with lock held */
14316 spin_lock_irqsave(&phba->hbalock, iflag);
14317 if (phba->link_state < LPFC_LINK_DOWN)
14318 /* Flush, clear interrupt, and rearm the EQ */
14319 lpfc_sli4_eq_flush(phba, fpeq);
14320 spin_unlock_irqrestore(&phba->hbalock, iflag);
14321 return IRQ_NONE;
14322 }
14323
James Smart32517fc2019-01-28 11:14:33 -080014324 eqi = phba->sli4_hba.eq_info;
14325 icnt = this_cpu_inc_return(eqi->icnt);
14326 fpeq->last_cpu = smp_processor_id();
James Smart4f774512009-05-22 14:52:35 -040014327
James Smart32517fc2019-01-28 11:14:33 -080014328 if (icnt > LPFC_EQD_ISR_TRIGGER &&
14329 phba->cfg_irq_chann == 1 &&
14330 phba->cfg_auto_imax &&
14331 fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
14332 phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
14333 lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
James Smartb84daac2012-08-03 12:35:13 -040014334
James Smart32517fc2019-01-28 11:14:33 -080014335 /* process and rearm the EQ */
14336 ecount = lpfc_sli4_process_eq(phba, fpeq);
James Smart4f774512009-05-22 14:52:35 -040014337
14338 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040014339 fpeq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040014340 if (phba->intr_type == MSIX)
14341 /* MSI-X treated interrupt served as no EQ share INT */
14342 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14343 "0358 MSI-X interrupt with no EQE\n");
14344 else
14345 /* Non MSI-X treated on interrupt as EQ share INT */
14346 return IRQ_NONE;
14347 }
14348
14349 return IRQ_HANDLED;
14350} /* lpfc_sli4_fp_intr_handler */
14351
14352/**
14353 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14354 * @irq: Interrupt number.
14355 * @dev_id: The device context pointer.
14356 *
14357 * This function is the device-level interrupt handler to device with SLI-4
14358 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14359 * interrupt mode is enabled and there is an event in the HBA which requires
14360 * driver attention. This function invokes the slow-path interrupt attention
14361 * handling function and fast-path interrupt attention handling function in
14362 * turn to process the relevant HBA attention events. This function is called
14363 * without any lock held. It gets the hbalock to access and update SLI data
14364 * structures.
14365 *
14366 * This function returns IRQ_HANDLED when interrupt is handled, else it
14367 * returns IRQ_NONE.
14368 **/
14369irqreturn_t
14370lpfc_sli4_intr_handler(int irq, void *dev_id)
14371{
14372 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040014373 irqreturn_t hba_irq_rc;
14374 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080014375 int qidx;
James Smart4f774512009-05-22 14:52:35 -040014376
14377 /* Get the driver's phba structure from the dev_id */
14378 phba = (struct lpfc_hba *)dev_id;
14379
14380 if (unlikely(!phba))
14381 return IRQ_NONE;
14382
14383 /*
James Smart4f774512009-05-22 14:52:35 -040014384 * Invoke fast-path host attention interrupt handling as appropriate.
14385 */
James Smart6a828b02019-01-28 11:14:31 -080014386 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040014387 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014388 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040014389 if (hba_irq_rc == IRQ_HANDLED)
14390 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040014391 }
14392
James Smart67d12732012-08-03 12:36:13 -040014393 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014394} /* lpfc_sli4_intr_handler */
14395
14396/**
14397 * lpfc_sli4_queue_free - free a queue structure and associated memory
14398 * @queue: The queue structure to free.
14399 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040014400 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040014401 * the host resident queue. This function must be called after destroying the
14402 * queue on the HBA.
14403 **/
14404void
14405lpfc_sli4_queue_free(struct lpfc_queue *queue)
14406{
14407 struct lpfc_dmabuf *dmabuf;
14408
14409 if (!queue)
14410 return;
14411
14412 while (!list_empty(&queue->page_list)) {
14413 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14414 list);
James Smart81b96ed2017-11-20 16:00:29 -080014415 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040014416 dmabuf->virt, dmabuf->phys);
14417 kfree(dmabuf);
14418 }
James Smart895427b2017-02-12 13:52:30 -080014419 if (queue->rqbp) {
14420 lpfc_free_rq_buffer(queue->phba, queue);
14421 kfree(queue->rqbp);
14422 }
James Smartd1f525a2017-04-21 16:04:55 -070014423
James Smart32517fc2019-01-28 11:14:33 -080014424 if (!list_empty(&queue->cpu_list))
14425 list_del(&queue->cpu_list);
14426
James Smartd1f525a2017-04-21 16:04:55 -070014427 if (!list_empty(&queue->wq_list))
14428 list_del(&queue->wq_list);
14429
James Smart4f774512009-05-22 14:52:35 -040014430 kfree(queue);
14431 return;
14432}
14433
14434/**
14435 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14436 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080014437 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040014438 * @entry_size: The size of each queue entry for this queue.
14439 * @entry count: The number of entries that this queue will handle.
14440 *
14441 * This function allocates a queue structure and the DMAable memory used for
14442 * the host resident queue. This function must be called before creating the
14443 * queue on the HBA.
14444 **/
14445struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080014446lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14447 uint32_t entry_size, uint32_t entry_count)
James Smart4f774512009-05-22 14:52:35 -040014448{
14449 struct lpfc_queue *queue;
14450 struct lpfc_dmabuf *dmabuf;
14451 int x, total_qe_count;
14452 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040014453 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040014454
James Smartcb5172e2010-03-15 11:25:07 -040014455 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014456 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040014457
James Smart4f774512009-05-22 14:52:35 -040014458 queue = kzalloc(sizeof(struct lpfc_queue) +
14459 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14460 if (!queue)
14461 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040014462 queue->page_count = (ALIGN(entry_size * entry_count,
14463 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080014464
14465 /* If needed, Adjust page count to match the max the adapter supports */
Ewan D. Milne4e87eb22018-12-13 15:25:16 -050014466 if (phba->sli4_hba.pc_sli4_params.wqpcnt &&
14467 (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt))
James Smart895427b2017-02-12 13:52:30 -080014468 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14469
James Smart4f774512009-05-22 14:52:35 -040014470 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080014471 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080014472 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040014473 INIT_LIST_HEAD(&queue->page_list);
14474 INIT_LIST_HEAD(&queue->child_list);
James Smart32517fc2019-01-28 11:14:33 -080014475 INIT_LIST_HEAD(&queue->cpu_list);
James Smart81b96ed2017-11-20 16:00:29 -080014476
14477 /* Set queue parameters now. If the system cannot provide memory
14478 * resources, the free routine needs to know what was allocated.
14479 */
14480 queue->entry_size = entry_size;
14481 queue->entry_count = entry_count;
14482 queue->page_size = hw_page_size;
14483 queue->phba = phba;
14484
James Smart4f774512009-05-22 14:52:35 -040014485 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14486 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14487 if (!dmabuf)
14488 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040014489 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14490 hw_page_size, &dmabuf->phys,
14491 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040014492 if (!dmabuf->virt) {
14493 kfree(dmabuf);
14494 goto out_fail;
14495 }
14496 dmabuf->buffer_tag = x;
14497 list_add_tail(&dmabuf->list, &queue->page_list);
14498 /* initialize queue's entry array */
14499 dma_pointer = dmabuf->virt;
14500 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040014501 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040014502 total_qe_count++, dma_pointer += entry_size) {
14503 queue->qe[total_qe_count].address = dma_pointer;
14504 }
14505 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014506 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14507 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart32517fc2019-01-28 11:14:33 -080014508 INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
14509 INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014510
James Smart32517fc2019-01-28 11:14:33 -080014511 /* notify_interval will be set during q creation */
James Smart64eb4dc2017-05-15 15:20:49 -070014512
James Smart4f774512009-05-22 14:52:35 -040014513 return queue;
14514out_fail:
14515 lpfc_sli4_queue_free(queue);
14516 return NULL;
14517}
14518
14519/**
James Smart962bc512013-01-03 15:44:00 -050014520 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14521 * @phba: HBA structure that indicates port to create a queue on.
14522 * @pci_barset: PCI BAR set flag.
14523 *
14524 * This function shall perform iomap of the specified PCI BAR address to host
14525 * memory address if not already done so and return it. The returned host
14526 * memory address can be NULL.
14527 */
14528static void __iomem *
14529lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14530{
James Smart962bc512013-01-03 15:44:00 -050014531 if (!phba->pcidev)
14532 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014533
14534 switch (pci_barset) {
14535 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014536 return phba->pci_bar0_memmap_p;
14537 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014538 return phba->pci_bar2_memmap_p;
14539 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014540 return phba->pci_bar4_memmap_p;
14541 default:
14542 break;
14543 }
14544 return NULL;
14545}
14546
14547/**
James Smartcb733e32019-01-28 11:14:32 -080014548 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
14549 * @phba: HBA structure that EQs are on.
14550 * @startq: The starting EQ index to modify
14551 * @numq: The number of EQs (consecutive indexes) to modify
14552 * @usdelay: amount of delay
James Smart173edbb2012-06-12 13:54:50 -040014553 *
James Smartcb733e32019-01-28 11:14:32 -080014554 * This function revises the EQ delay on 1 or more EQs. The EQ delay
14555 * is set either by writing to a register (if supported by the SLI Port)
14556 * or by mailbox command. The mailbox command allows several EQs to be
14557 * updated at once.
James Smart173edbb2012-06-12 13:54:50 -040014558 *
James Smartcb733e32019-01-28 11:14:32 -080014559 * The @phba struct is used to send a mailbox command to HBA. The @startq
14560 * is used to get the starting EQ index to change. The @numq value is
14561 * used to specify how many consecutive EQ indexes, starting at EQ index,
14562 * are to be changed. This function is asynchronous and will wait for any
14563 * mailbox commands to finish before returning.
James Smart173edbb2012-06-12 13:54:50 -040014564 *
James Smartcb733e32019-01-28 11:14:32 -080014565 * On success this function will return a zero. If unable to allocate
14566 * enough memory this function will return -ENOMEM. If a mailbox command
14567 * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
14568 * have had their delay multipler changed.
James Smart173edbb2012-06-12 13:54:50 -040014569 **/
James Smartcb733e32019-01-28 11:14:32 -080014570void
James Smart0cf07f842017-06-01 21:07:10 -070014571lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
James Smartcb733e32019-01-28 11:14:32 -080014572 uint32_t numq, uint32_t usdelay)
James Smart173edbb2012-06-12 13:54:50 -040014573{
14574 struct lpfc_mbx_modify_eq_delay *eq_delay;
14575 LPFC_MBOXQ_t *mbox;
14576 struct lpfc_queue *eq;
James Smartcb733e32019-01-28 11:14:32 -080014577 int cnt = 0, rc, length;
James Smart173edbb2012-06-12 13:54:50 -040014578 uint32_t shdr_status, shdr_add_status;
James Smartcb733e32019-01-28 11:14:32 -080014579 uint32_t dmult;
James Smart895427b2017-02-12 13:52:30 -080014580 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014581 union lpfc_sli4_cfg_shdr *shdr;
James Smart173edbb2012-06-12 13:54:50 -040014582
James Smart6a828b02019-01-28 11:14:31 -080014583 if (startq >= phba->cfg_irq_chann)
James Smartcb733e32019-01-28 11:14:32 -080014584 return;
14585
14586 if (usdelay > 0xFFFF) {
14587 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
14588 "6429 usdelay %d too large. Scaled down to "
14589 "0xFFFF.\n", usdelay);
14590 usdelay = 0xFFFF;
14591 }
14592
14593 /* set values by EQ_DELAY register if supported */
14594 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14595 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
14596 eq = phba->sli4_hba.hdwq[qidx].hba_eq;
14597 if (!eq)
14598 continue;
14599
James Smart32517fc2019-01-28 11:14:33 -080014600 lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
James Smartcb733e32019-01-28 11:14:32 -080014601
14602 if (++cnt >= numq)
14603 break;
14604 }
14605
14606 return;
14607 }
14608
14609 /* Otherwise, set values by mailbox cmd */
James Smart173edbb2012-06-12 13:54:50 -040014610
14611 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smartcb733e32019-01-28 11:14:32 -080014612 if (!mbox) {
14613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_FCP | LOG_NVME,
14614 "6428 Failed allocating mailbox cmd buffer."
14615 " EQ delay was not set.\n");
14616 return;
14617 }
James Smart173edbb2012-06-12 13:54:50 -040014618 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14619 sizeof(struct lpfc_sli4_cfg_mhdr));
14620 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14621 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14622 length, LPFC_SLI4_MBX_EMBED);
14623 eq_delay = &mbox->u.mqe.un.eq_delay;
14624
14625 /* Calculate delay multiper from maximum interrupt per second */
James Smartcb733e32019-01-28 11:14:32 -080014626 dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
14627 if (dmult)
14628 dmult--;
James Smart0cf07f842017-06-01 21:07:10 -070014629 if (dmult > LPFC_DMULT_MAX)
14630 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014631
James Smart6a828b02019-01-28 11:14:31 -080014632 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smartcdb42be2019-01-28 11:14:21 -080014633 eq = phba->sli4_hba.hdwq[qidx].hba_eq;
James Smart173edbb2012-06-12 13:54:50 -040014634 if (!eq)
14635 continue;
James Smartcb733e32019-01-28 11:14:32 -080014636 eq->q_mode = usdelay;
James Smart173edbb2012-06-12 13:54:50 -040014637 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14638 eq_delay->u.request.eq[cnt].phase = 0;
14639 eq_delay->u.request.eq[cnt].delay_multi = dmult;
James Smart0cf07f842017-06-01 21:07:10 -070014640
James Smartcb733e32019-01-28 11:14:32 -080014641 if (++cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014642 break;
14643 }
14644 eq_delay->u.request.num_eq = cnt;
14645
14646 mbox->vport = phba->pport;
14647 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080014648 mbox->ctx_buf = NULL;
14649 mbox->ctx_ndlp = NULL;
James Smart173edbb2012-06-12 13:54:50 -040014650 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14651 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14652 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14653 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14654 if (shdr_status || shdr_add_status || rc) {
14655 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14656 "2512 MODIFY_EQ_DELAY mailbox failed with "
14657 "status x%x add_status x%x, mbx status x%x\n",
14658 shdr_status, shdr_add_status, rc);
James Smart173edbb2012-06-12 13:54:50 -040014659 }
14660 mempool_free(mbox, phba->mbox_mem_pool);
James Smartcb733e32019-01-28 11:14:32 -080014661 return;
James Smart173edbb2012-06-12 13:54:50 -040014662}
14663
14664/**
James Smart4f774512009-05-22 14:52:35 -040014665 * lpfc_eq_create - Create an Event Queue on the HBA
14666 * @phba: HBA structure that indicates port to create a queue on.
14667 * @eq: The queue structure to use to create the event queue.
14668 * @imax: The maximum interrupt per second limit.
14669 *
14670 * This function creates an event queue, as detailed in @eq, on a port,
14671 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14672 *
14673 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14674 * is used to get the entry count and entry size that are necessary to
14675 * determine the number of pages to allocate and use for this queue. This
14676 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14677 * event queue. This function is asynchronous and will wait for the mailbox
14678 * command to finish before continuing.
14679 *
14680 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014681 * memory this function will return -ENOMEM. If the queue create mailbox command
14682 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014683 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014684int
James Smartee020062012-09-29 11:28:52 -040014685lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014686{
14687 struct lpfc_mbx_eq_create *eq_create;
14688 LPFC_MBOXQ_t *mbox;
14689 int rc, length, status = 0;
14690 struct lpfc_dmabuf *dmabuf;
14691 uint32_t shdr_status, shdr_add_status;
14692 union lpfc_sli4_cfg_shdr *shdr;
14693 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014694 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14695
James Smart2e90f4b2011-12-13 13:22:37 -050014696 /* sanity check on queue memory */
14697 if (!eq)
14698 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014699 if (!phba->sli4_hba.pc_sli4_params.supported)
14700 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014701
14702 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14703 if (!mbox)
14704 return -ENOMEM;
14705 length = (sizeof(struct lpfc_mbx_eq_create) -
14706 sizeof(struct lpfc_sli4_cfg_mhdr));
14707 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14708 LPFC_MBOX_OPCODE_EQ_CREATE,
14709 length, LPFC_SLI4_MBX_EMBED);
14710 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080014711 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014712 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14713 eq->page_count);
14714 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14715 LPFC_EQE_SIZE);
14716 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080014717
14718 /* Use version 2 of CREATE_EQ if eqav is set */
14719 if (phba->sli4_hba.pc_sli4_params.eqav) {
14720 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14721 LPFC_Q_CREATE_VERSION_2);
14722 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14723 phba->sli4_hba.pc_sli4_params.eqav);
14724 }
14725
James Smart2c9c5a02015-04-07 15:07:15 -040014726 /* don't setup delay multiplier using EQ_CREATE */
14727 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014728 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14729 dmult);
14730 switch (eq->entry_count) {
14731 default:
14732 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14733 "0360 Unsupported EQ count. (%d)\n",
14734 eq->entry_count);
14735 if (eq->entry_count < 256)
14736 return -EINVAL;
14737 /* otherwise default to smallest count (drop through) */
14738 case 256:
14739 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14740 LPFC_EQ_CNT_256);
14741 break;
14742 case 512:
14743 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14744 LPFC_EQ_CNT_512);
14745 break;
14746 case 1024:
14747 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14748 LPFC_EQ_CNT_1024);
14749 break;
14750 case 2048:
14751 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14752 LPFC_EQ_CNT_2048);
14753 break;
14754 case 4096:
14755 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14756 LPFC_EQ_CNT_4096);
14757 break;
14758 }
14759 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014760 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014761 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14762 putPaddrLow(dmabuf->phys);
14763 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14764 putPaddrHigh(dmabuf->phys);
14765 }
14766 mbox->vport = phba->pport;
14767 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080014768 mbox->ctx_buf = NULL;
14769 mbox->ctx_ndlp = NULL;
James Smart4f774512009-05-22 14:52:35 -040014770 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040014771 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14772 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14773 if (shdr_status || shdr_add_status || rc) {
14774 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14775 "2500 EQ_CREATE mailbox failed with "
14776 "status x%x add_status x%x, mbx status x%x\n",
14777 shdr_status, shdr_add_status, rc);
14778 status = -ENXIO;
14779 }
14780 eq->type = LPFC_EQ;
14781 eq->subtype = LPFC_NONE;
14782 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14783 if (eq->queue_id == 0xFFFF)
14784 status = -ENXIO;
14785 eq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080014786 eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
14787 eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
James Smart4f774512009-05-22 14:52:35 -040014788
James Smart8fa38512009-07-19 10:01:03 -040014789 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014790 return status;
14791}
14792
14793/**
14794 * lpfc_cq_create - Create a Completion Queue on the HBA
14795 * @phba: HBA structure that indicates port to create a queue on.
14796 * @cq: The queue structure to use to create the completion queue.
14797 * @eq: The event queue to bind this completion queue to.
14798 *
14799 * This function creates a completion queue, as detailed in @wq, on a port,
14800 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14801 *
14802 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14803 * is used to get the entry count and entry size that are necessary to
14804 * determine the number of pages to allocate and use for this queue. The @eq
14805 * is used to indicate which event queue to bind this completion queue to. This
14806 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14807 * completion queue. This function is asynchronous and will wait for the mailbox
14808 * command to finish before continuing.
14809 *
14810 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014811 * memory this function will return -ENOMEM. If the queue create mailbox command
14812 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014813 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014814int
James Smart4f774512009-05-22 14:52:35 -040014815lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14816 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14817{
14818 struct lpfc_mbx_cq_create *cq_create;
14819 struct lpfc_dmabuf *dmabuf;
14820 LPFC_MBOXQ_t *mbox;
14821 int rc, length, status = 0;
14822 uint32_t shdr_status, shdr_add_status;
14823 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014824
James Smart2e90f4b2011-12-13 13:22:37 -050014825 /* sanity check on queue memory */
14826 if (!cq || !eq)
14827 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014828
James Smart4f774512009-05-22 14:52:35 -040014829 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14830 if (!mbox)
14831 return -ENOMEM;
14832 length = (sizeof(struct lpfc_mbx_cq_create) -
14833 sizeof(struct lpfc_sli4_cfg_mhdr));
14834 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14835 LPFC_MBOX_OPCODE_CQ_CREATE,
14836 length, LPFC_SLI4_MBX_EMBED);
14837 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014838 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014839 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14840 cq->page_count);
14841 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14842 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014843 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14844 phba->sli4_hba.pc_sli4_params.cqv);
14845 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080014846 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14847 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014848 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14849 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080014850 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14851 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050014852 } else {
14853 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14854 eq->queue_id);
14855 }
James Smart4f774512009-05-22 14:52:35 -040014856 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014857 case 2048:
14858 case 4096:
14859 if (phba->sli4_hba.pc_sli4_params.cqv ==
14860 LPFC_Q_CREATE_VERSION_2) {
14861 cq_create->u.request.context.lpfc_cq_context_count =
14862 cq->entry_count;
14863 bf_set(lpfc_cq_context_count,
14864 &cq_create->u.request.context,
14865 LPFC_CQ_CNT_WORD7);
14866 break;
14867 }
14868 /* Fall Thru */
James Smart4f774512009-05-22 14:52:35 -040014869 default:
14870 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014871 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014872 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014873 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014874 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014875 if (cq->entry_count < 256) {
14876 status = -EINVAL;
14877 goto out;
14878 }
James Smart4f774512009-05-22 14:52:35 -040014879 /* otherwise default to smallest count (drop through) */
14880 case 256:
14881 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14882 LPFC_CQ_CNT_256);
14883 break;
14884 case 512:
14885 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14886 LPFC_CQ_CNT_512);
14887 break;
14888 case 1024:
14889 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14890 LPFC_CQ_CNT_1024);
14891 break;
14892 }
14893 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080014894 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040014895 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14896 putPaddrLow(dmabuf->phys);
14897 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14898 putPaddrHigh(dmabuf->phys);
14899 }
14900 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14901
14902 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014903 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14904 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14905 if (shdr_status || shdr_add_status || rc) {
14906 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14907 "2501 CQ_CREATE mailbox failed with "
14908 "status x%x add_status x%x, mbx status x%x\n",
14909 shdr_status, shdr_add_status, rc);
14910 status = -ENXIO;
14911 goto out;
14912 }
14913 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14914 if (cq->queue_id == 0xFFFF) {
14915 status = -ENXIO;
14916 goto out;
14917 }
14918 /* link the cq onto the parent eq child list */
14919 list_add_tail(&cq->list, &eq->child_list);
14920 /* Set up completion queue's type and subtype */
14921 cq->type = type;
14922 cq->subtype = subtype;
14923 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014924 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080014925 cq->assoc_qp = eq;
James Smart4f774512009-05-22 14:52:35 -040014926 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080014927 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
14928 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
James Smart4f774512009-05-22 14:52:35 -040014929
James Smart6a828b02019-01-28 11:14:31 -080014930 if (cq->queue_id > phba->sli4_hba.cq_max)
14931 phba->sli4_hba.cq_max = cq->queue_id;
James Smart8fa38512009-07-19 10:01:03 -040014932out:
14933 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014934 return status;
14935}
14936
14937/**
James Smart2d7dbc42017-02-12 13:52:35 -080014938 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14939 * @phba: HBA structure that indicates port to create a queue on.
14940 * @cqp: The queue structure array to use to create the completion queues.
James Smartcdb42be2019-01-28 11:14:21 -080014941 * @hdwq: The hardware queue array with the EQ to bind completion queues to.
James Smart2d7dbc42017-02-12 13:52:35 -080014942 *
14943 * This function creates a set of completion queue, s to support MRQ
14944 * as detailed in @cqp, on a port,
14945 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14946 *
14947 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14948 * is used to get the entry count and entry size that are necessary to
14949 * determine the number of pages to allocate and use for this queue. The @eq
14950 * is used to indicate which event queue to bind this completion queue to. This
14951 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14952 * completion queue. This function is asynchronous and will wait for the mailbox
14953 * command to finish before continuing.
14954 *
14955 * On success this function will return a zero. If unable to allocate enough
14956 * memory this function will return -ENOMEM. If the queue create mailbox command
14957 * fails this function will return -ENXIO.
14958 **/
14959int
14960lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
James Smartcdb42be2019-01-28 11:14:21 -080014961 struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
14962 uint32_t subtype)
James Smart2d7dbc42017-02-12 13:52:35 -080014963{
14964 struct lpfc_queue *cq;
14965 struct lpfc_queue *eq;
14966 struct lpfc_mbx_cq_create_set *cq_set;
14967 struct lpfc_dmabuf *dmabuf;
14968 LPFC_MBOXQ_t *mbox;
14969 int rc, length, alloclen, status = 0;
14970 int cnt, idx, numcq, page_idx = 0;
14971 uint32_t shdr_status, shdr_add_status;
14972 union lpfc_sli4_cfg_shdr *shdr;
14973 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14974
14975 /* sanity check on queue memory */
14976 numcq = phba->cfg_nvmet_mrq;
James Smartcdb42be2019-01-28 11:14:21 -080014977 if (!cqp || !hdwq || !numcq)
James Smart2d7dbc42017-02-12 13:52:35 -080014978 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080014979
14980 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14981 if (!mbox)
14982 return -ENOMEM;
14983
14984 length = sizeof(struct lpfc_mbx_cq_create_set);
14985 length += ((numcq * cqp[0]->page_count) *
14986 sizeof(struct dma_address));
14987 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14988 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14989 LPFC_SLI4_MBX_NEMBED);
14990 if (alloclen < length) {
14991 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14992 "3098 Allocated DMA memory size (%d) is "
14993 "less than the requested DMA memory size "
14994 "(%d)\n", alloclen, length);
14995 status = -ENOMEM;
14996 goto out;
14997 }
14998 cq_set = mbox->sge_array->addr[0];
14999 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
15000 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
15001
15002 for (idx = 0; idx < numcq; idx++) {
15003 cq = cqp[idx];
James Smartcdb42be2019-01-28 11:14:21 -080015004 eq = hdwq[idx].hba_eq;
James Smart2d7dbc42017-02-12 13:52:35 -080015005 if (!cq || !eq) {
15006 status = -ENOMEM;
15007 goto out;
15008 }
James Smart81b96ed2017-11-20 16:00:29 -080015009 if (!phba->sli4_hba.pc_sli4_params.supported)
15010 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080015011
15012 switch (idx) {
15013 case 0:
15014 bf_set(lpfc_mbx_cq_create_set_page_size,
15015 &cq_set->u.request,
15016 (hw_page_size / SLI4_PAGE_SIZE));
15017 bf_set(lpfc_mbx_cq_create_set_num_pages,
15018 &cq_set->u.request, cq->page_count);
15019 bf_set(lpfc_mbx_cq_create_set_evt,
15020 &cq_set->u.request, 1);
15021 bf_set(lpfc_mbx_cq_create_set_valid,
15022 &cq_set->u.request, 1);
15023 bf_set(lpfc_mbx_cq_create_set_cqe_size,
15024 &cq_set->u.request, 0);
15025 bf_set(lpfc_mbx_cq_create_set_num_cq,
15026 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080015027 bf_set(lpfc_mbx_cq_create_set_autovalid,
15028 &cq_set->u.request,
15029 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080015030 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080015031 case 2048:
15032 case 4096:
15033 if (phba->sli4_hba.pc_sli4_params.cqv ==
15034 LPFC_Q_CREATE_VERSION_2) {
15035 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15036 &cq_set->u.request,
15037 cq->entry_count);
15038 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15039 &cq_set->u.request,
15040 LPFC_CQ_CNT_WORD7);
15041 break;
15042 }
15043 /* Fall Thru */
James Smart2d7dbc42017-02-12 13:52:35 -080015044 default:
15045 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15046 "3118 Bad CQ count. (%d)\n",
15047 cq->entry_count);
15048 if (cq->entry_count < 256) {
15049 status = -EINVAL;
15050 goto out;
15051 }
15052 /* otherwise default to smallest (drop thru) */
15053 case 256:
15054 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15055 &cq_set->u.request, LPFC_CQ_CNT_256);
15056 break;
15057 case 512:
15058 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15059 &cq_set->u.request, LPFC_CQ_CNT_512);
15060 break;
15061 case 1024:
15062 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15063 &cq_set->u.request, LPFC_CQ_CNT_1024);
15064 break;
15065 }
15066 bf_set(lpfc_mbx_cq_create_set_eq_id0,
15067 &cq_set->u.request, eq->queue_id);
15068 break;
15069 case 1:
15070 bf_set(lpfc_mbx_cq_create_set_eq_id1,
15071 &cq_set->u.request, eq->queue_id);
15072 break;
15073 case 2:
15074 bf_set(lpfc_mbx_cq_create_set_eq_id2,
15075 &cq_set->u.request, eq->queue_id);
15076 break;
15077 case 3:
15078 bf_set(lpfc_mbx_cq_create_set_eq_id3,
15079 &cq_set->u.request, eq->queue_id);
15080 break;
15081 case 4:
15082 bf_set(lpfc_mbx_cq_create_set_eq_id4,
15083 &cq_set->u.request, eq->queue_id);
15084 break;
15085 case 5:
15086 bf_set(lpfc_mbx_cq_create_set_eq_id5,
15087 &cq_set->u.request, eq->queue_id);
15088 break;
15089 case 6:
15090 bf_set(lpfc_mbx_cq_create_set_eq_id6,
15091 &cq_set->u.request, eq->queue_id);
15092 break;
15093 case 7:
15094 bf_set(lpfc_mbx_cq_create_set_eq_id7,
15095 &cq_set->u.request, eq->queue_id);
15096 break;
15097 case 8:
15098 bf_set(lpfc_mbx_cq_create_set_eq_id8,
15099 &cq_set->u.request, eq->queue_id);
15100 break;
15101 case 9:
15102 bf_set(lpfc_mbx_cq_create_set_eq_id9,
15103 &cq_set->u.request, eq->queue_id);
15104 break;
15105 case 10:
15106 bf_set(lpfc_mbx_cq_create_set_eq_id10,
15107 &cq_set->u.request, eq->queue_id);
15108 break;
15109 case 11:
15110 bf_set(lpfc_mbx_cq_create_set_eq_id11,
15111 &cq_set->u.request, eq->queue_id);
15112 break;
15113 case 12:
15114 bf_set(lpfc_mbx_cq_create_set_eq_id12,
15115 &cq_set->u.request, eq->queue_id);
15116 break;
15117 case 13:
15118 bf_set(lpfc_mbx_cq_create_set_eq_id13,
15119 &cq_set->u.request, eq->queue_id);
15120 break;
15121 case 14:
15122 bf_set(lpfc_mbx_cq_create_set_eq_id14,
15123 &cq_set->u.request, eq->queue_id);
15124 break;
15125 case 15:
15126 bf_set(lpfc_mbx_cq_create_set_eq_id15,
15127 &cq_set->u.request, eq->queue_id);
15128 break;
15129 }
15130
15131 /* link the cq onto the parent eq child list */
15132 list_add_tail(&cq->list, &eq->child_list);
15133 /* Set up completion queue's type and subtype */
15134 cq->type = type;
15135 cq->subtype = subtype;
15136 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080015137 cq->assoc_qp = eq;
James Smart2d7dbc42017-02-12 13:52:35 -080015138 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015139 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
15140 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
15141 cq->entry_count);
James Smart81b96ed2017-11-20 16:00:29 -080015142 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080015143
15144 rc = 0;
15145 list_for_each_entry(dmabuf, &cq->page_list, list) {
15146 memset(dmabuf->virt, 0, hw_page_size);
15147 cnt = page_idx + dmabuf->buffer_tag;
15148 cq_set->u.request.page[cnt].addr_lo =
15149 putPaddrLow(dmabuf->phys);
15150 cq_set->u.request.page[cnt].addr_hi =
15151 putPaddrHigh(dmabuf->phys);
15152 rc++;
15153 }
15154 page_idx += rc;
15155 }
15156
15157 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15158
15159 /* The IOCTL status is embedded in the mailbox subheader. */
15160 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15161 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15162 if (shdr_status || shdr_add_status || rc) {
15163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15164 "3119 CQ_CREATE_SET mailbox failed with "
15165 "status x%x add_status x%x, mbx status x%x\n",
15166 shdr_status, shdr_add_status, rc);
15167 status = -ENXIO;
15168 goto out;
15169 }
15170 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
15171 if (rc == 0xFFFF) {
15172 status = -ENXIO;
15173 goto out;
15174 }
15175
15176 for (idx = 0; idx < numcq; idx++) {
15177 cq = cqp[idx];
15178 cq->queue_id = rc + idx;
James Smart6a828b02019-01-28 11:14:31 -080015179 if (cq->queue_id > phba->sli4_hba.cq_max)
15180 phba->sli4_hba.cq_max = cq->queue_id;
James Smart2d7dbc42017-02-12 13:52:35 -080015181 }
15182
15183out:
15184 lpfc_sli4_mbox_cmd_free(phba, mbox);
15185 return status;
15186}
15187
15188/**
James Smartb19a0612010-04-06 14:48:51 -040015189 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040015190 * @phba: HBA structure that indicates port to create a queue on.
15191 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040015192 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
15193 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040015194 *
James Smartb19a0612010-04-06 14:48:51 -040015195 * This function provides failback (fb) functionality when the
15196 * mq_create_ext fails on older FW generations. It's purpose is identical
15197 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040015198 *
James Smartb19a0612010-04-06 14:48:51 -040015199 * This routine cannot fail as all attributes were previously accessed and
15200 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040015201 **/
James Smartb19a0612010-04-06 14:48:51 -040015202static void
15203lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
15204 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040015205{
15206 struct lpfc_mbx_mq_create *mq_create;
15207 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040015208 int length;
James Smart04c68492009-05-22 14:52:52 -040015209
James Smart04c68492009-05-22 14:52:52 -040015210 length = (sizeof(struct lpfc_mbx_mq_create) -
15211 sizeof(struct lpfc_sli4_cfg_mhdr));
15212 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15213 LPFC_MBOX_OPCODE_MQ_CREATE,
15214 length, LPFC_SLI4_MBX_EMBED);
15215 mq_create = &mbox->u.mqe.un.mq_create;
15216 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040015217 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040015218 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040015219 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040015220 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
15221 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040015222 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015223 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15224 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040015225 break;
15226 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015227 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15228 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040015229 break;
15230 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015231 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15232 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040015233 break;
15234 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015235 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15236 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040015237 break;
15238 }
15239 list_for_each_entry(dmabuf, &mq->page_list, list) {
15240 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040015241 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040015242 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040015243 putPaddrHigh(dmabuf->phys);
15244 }
15245}
15246
15247/**
15248 * lpfc_mq_create - Create a mailbox Queue on the HBA
15249 * @phba: HBA structure that indicates port to create a queue on.
15250 * @mq: The queue structure to use to create the mailbox queue.
15251 * @cq: The completion queue to associate with this cq.
15252 * @subtype: The queue's subtype.
15253 *
15254 * This function creates a mailbox queue, as detailed in @mq, on a port,
15255 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
15256 *
15257 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15258 * is used to get the entry count and entry size that are necessary to
15259 * determine the number of pages to allocate and use for this queue. This
15260 * function will send the MQ_CREATE mailbox command to the HBA to setup the
15261 * mailbox queue. This function is asynchronous and will wait for the mailbox
15262 * command to finish before continuing.
15263 *
15264 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015265 * memory this function will return -ENOMEM. If the queue create mailbox command
15266 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040015267 **/
15268int32_t
15269lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15270 struct lpfc_queue *cq, uint32_t subtype)
15271{
15272 struct lpfc_mbx_mq_create *mq_create;
15273 struct lpfc_mbx_mq_create_ext *mq_create_ext;
15274 struct lpfc_dmabuf *dmabuf;
15275 LPFC_MBOXQ_t *mbox;
15276 int rc, length, status = 0;
15277 uint32_t shdr_status, shdr_add_status;
15278 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015279 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040015280
James Smart2e90f4b2011-12-13 13:22:37 -050015281 /* sanity check on queue memory */
15282 if (!mq || !cq)
15283 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015284 if (!phba->sli4_hba.pc_sli4_params.supported)
15285 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040015286
15287 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15288 if (!mbox)
15289 return -ENOMEM;
15290 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15291 sizeof(struct lpfc_sli4_cfg_mhdr));
15292 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15293 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15294 length, LPFC_SLI4_MBX_EMBED);
15295
15296 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050015297 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050015298 bf_set(lpfc_mbx_mq_create_ext_num_pages,
15299 &mq_create_ext->u.request, mq->page_count);
15300 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15301 &mq_create_ext->u.request, 1);
15302 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040015303 &mq_create_ext->u.request, 1);
15304 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15305 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050015306 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15307 &mq_create_ext->u.request, 1);
15308 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15309 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040015310 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050015311 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15312 phba->sli4_hba.pc_sli4_params.mqv);
15313 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15314 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15315 cq->queue_id);
15316 else
15317 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15318 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040015319 switch (mq->entry_count) {
15320 default:
15321 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15322 "0362 Unsupported MQ count. (%d)\n",
15323 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015324 if (mq->entry_count < 16) {
15325 status = -EINVAL;
15326 goto out;
15327 }
James Smartb19a0612010-04-06 14:48:51 -040015328 /* otherwise default to smallest count (drop through) */
15329 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015330 bf_set(lpfc_mq_context_ring_size,
15331 &mq_create_ext->u.request.context,
15332 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040015333 break;
15334 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015335 bf_set(lpfc_mq_context_ring_size,
15336 &mq_create_ext->u.request.context,
15337 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040015338 break;
15339 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015340 bf_set(lpfc_mq_context_ring_size,
15341 &mq_create_ext->u.request.context,
15342 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040015343 break;
15344 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015345 bf_set(lpfc_mq_context_ring_size,
15346 &mq_create_ext->u.request.context,
15347 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040015348 break;
15349 }
15350 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015351 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040015352 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15353 putPaddrLow(dmabuf->phys);
15354 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040015355 putPaddrHigh(dmabuf->phys);
15356 }
15357 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040015358 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15359 &mq_create_ext->u.response);
15360 if (rc != MBX_SUCCESS) {
15361 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15362 "2795 MQ_CREATE_EXT failed with "
15363 "status x%x. Failback to MQ_CREATE.\n",
15364 rc);
15365 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15366 mq_create = &mbox->u.mqe.un.mq_create;
15367 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15368 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15369 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15370 &mq_create->u.response);
15371 }
15372
James Smart04c68492009-05-22 14:52:52 -040015373 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040015374 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15375 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15376 if (shdr_status || shdr_add_status || rc) {
15377 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15378 "2502 MQ_CREATE mailbox failed with "
15379 "status x%x add_status x%x, mbx status x%x\n",
15380 shdr_status, shdr_add_status, rc);
15381 status = -ENXIO;
15382 goto out;
15383 }
James Smart04c68492009-05-22 14:52:52 -040015384 if (mq->queue_id == 0xFFFF) {
15385 status = -ENXIO;
15386 goto out;
15387 }
15388 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050015389 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040015390 mq->subtype = subtype;
15391 mq->host_index = 0;
15392 mq->hba_index = 0;
15393
15394 /* link the mq onto the parent cq child list */
15395 list_add_tail(&mq->list, &cq->child_list);
15396out:
James Smart8fa38512009-07-19 10:01:03 -040015397 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015398 return status;
15399}
15400
15401/**
James Smart4f774512009-05-22 14:52:35 -040015402 * lpfc_wq_create - Create a Work Queue on the HBA
15403 * @phba: HBA structure that indicates port to create a queue on.
15404 * @wq: The queue structure to use to create the work queue.
15405 * @cq: The completion queue to bind this work queue to.
15406 * @subtype: The subtype of the work queue indicating its functionality.
15407 *
15408 * This function creates a work queue, as detailed in @wq, on a port, described
15409 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15410 *
15411 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15412 * is used to get the entry count and entry size that are necessary to
15413 * determine the number of pages to allocate and use for this queue. The @cq
15414 * is used to indicate which completion queue to bind this work queue to. This
15415 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15416 * work queue. This function is asynchronous and will wait for the mailbox
15417 * command to finish before continuing.
15418 *
15419 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015420 * memory this function will return -ENOMEM. If the queue create mailbox command
15421 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015422 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015423int
James Smart4f774512009-05-22 14:52:35 -040015424lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15425 struct lpfc_queue *cq, uint32_t subtype)
15426{
15427 struct lpfc_mbx_wq_create *wq_create;
15428 struct lpfc_dmabuf *dmabuf;
15429 LPFC_MBOXQ_t *mbox;
15430 int rc, length, status = 0;
15431 uint32_t shdr_status, shdr_add_status;
15432 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015433 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050015434 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050015435 void __iomem *bar_memmap_p;
15436 uint32_t db_offset;
15437 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080015438 uint8_t dpp_barset;
15439 uint32_t dpp_offset;
15440 unsigned long pg_addr;
James Smart81b96ed2017-11-20 16:00:29 -080015441 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040015442
James Smart2e90f4b2011-12-13 13:22:37 -050015443 /* sanity check on queue memory */
15444 if (!wq || !cq)
15445 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015446 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015447 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040015448
15449 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15450 if (!mbox)
15451 return -ENOMEM;
15452 length = (sizeof(struct lpfc_mbx_wq_create) -
15453 sizeof(struct lpfc_sli4_cfg_mhdr));
15454 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15455 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15456 length, LPFC_SLI4_MBX_EMBED);
15457 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015458 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015459 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15460 wq->page_count);
15461 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15462 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040015463
15464 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050015465 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15466 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050015467
James Smartc176ffa2018-01-30 15:58:46 -080015468 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15469 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080015470 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15471 else
15472 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15473
James Smart0c651872013-07-15 18:33:23 -040015474
James Smart1351e692018-02-22 08:18:43 -080015475 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15476 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15477 else
15478 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15479
15480 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040015481 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050015482 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15483 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070015484 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15485 LPFC_Q_CREATE_VERSION_1);
15486
James Smart5a6f1332011-03-11 16:05:35 -050015487 switch (wq->entry_size) {
15488 default:
15489 case 64:
15490 bf_set(lpfc_mbx_wq_create_wqe_size,
15491 &wq_create->u.request_1,
15492 LPFC_WQ_WQE_SIZE_64);
15493 break;
15494 case 128:
15495 bf_set(lpfc_mbx_wq_create_wqe_size,
15496 &wq_create->u.request_1,
15497 LPFC_WQ_WQE_SIZE_128);
15498 break;
15499 }
James Smart1351e692018-02-22 08:18:43 -080015500 /* Request DPP by default */
15501 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080015502 bf_set(lpfc_mbx_wq_create_page_size,
15503 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015504 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015505 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015506 break;
15507 default:
James Smart1351e692018-02-22 08:18:43 -080015508 page = wq_create->u.request.page;
15509 break;
James Smart5a6f1332011-03-11 16:05:35 -050015510 }
James Smart0c651872013-07-15 18:33:23 -040015511
James Smart4f774512009-05-22 14:52:35 -040015512 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015513 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015514 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15515 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015516 }
James Smart962bc512013-01-03 15:44:00 -050015517
15518 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15519 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15520
James Smart4f774512009-05-22 14:52:35 -040015521 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15522 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015523 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15524 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15525 if (shdr_status || shdr_add_status || rc) {
15526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15527 "2503 WQ_CREATE mailbox failed with "
15528 "status x%x add_status x%x, mbx status x%x\n",
15529 shdr_status, shdr_add_status, rc);
15530 status = -ENXIO;
15531 goto out;
15532 }
James Smart1351e692018-02-22 08:18:43 -080015533
15534 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15535 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15536 &wq_create->u.response);
15537 else
15538 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15539 &wq_create->u.response_1);
15540
James Smart4f774512009-05-22 14:52:35 -040015541 if (wq->queue_id == 0xFFFF) {
15542 status = -ENXIO;
15543 goto out;
15544 }
James Smart1351e692018-02-22 08:18:43 -080015545
15546 wq->db_format = LPFC_DB_LIST_FORMAT;
15547 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15548 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15549 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15550 &wq_create->u.response);
15551 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15552 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15553 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15554 "3265 WQ[%d] doorbell format "
15555 "not supported: x%x\n",
15556 wq->queue_id, wq->db_format);
15557 status = -EINVAL;
15558 goto out;
15559 }
15560 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15561 &wq_create->u.response);
15562 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15563 pci_barset);
15564 if (!bar_memmap_p) {
15565 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15566 "3263 WQ[%d] failed to memmap "
15567 "pci barset:x%x\n",
15568 wq->queue_id, pci_barset);
15569 status = -ENOMEM;
15570 goto out;
15571 }
15572 db_offset = wq_create->u.response.doorbell_offset;
15573 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15574 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15576 "3252 WQ[%d] doorbell offset "
15577 "not supported: x%x\n",
15578 wq->queue_id, db_offset);
15579 status = -EINVAL;
15580 goto out;
15581 }
15582 wq->db_regaddr = bar_memmap_p + db_offset;
15583 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15584 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15585 "format:x%x\n", wq->queue_id,
15586 pci_barset, db_offset, wq->db_format);
15587 } else
15588 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015589 } else {
James Smart1351e692018-02-22 08:18:43 -080015590 /* Check if DPP was honored by the firmware */
15591 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15592 &wq_create->u.response_1);
15593 if (wq->dpp_enable) {
15594 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15595 &wq_create->u.response_1);
15596 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15597 pci_barset);
15598 if (!bar_memmap_p) {
15599 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15600 "3267 WQ[%d] failed to memmap "
15601 "pci barset:x%x\n",
15602 wq->queue_id, pci_barset);
15603 status = -ENOMEM;
15604 goto out;
15605 }
15606 db_offset = wq_create->u.response_1.doorbell_offset;
15607 wq->db_regaddr = bar_memmap_p + db_offset;
15608 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15609 &wq_create->u.response_1);
15610 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15611 &wq_create->u.response_1);
15612 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15613 dpp_barset);
15614 if (!bar_memmap_p) {
15615 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15616 "3268 WQ[%d] failed to memmap "
15617 "pci barset:x%x\n",
15618 wq->queue_id, dpp_barset);
15619 status = -ENOMEM;
15620 goto out;
15621 }
15622 dpp_offset = wq_create->u.response_1.dpp_offset;
15623 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15624 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15625 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15626 "dpp_id:x%x dpp_barset:x%x "
15627 "dpp_offset:x%x\n",
15628 wq->queue_id, pci_barset, db_offset,
15629 wq->dpp_id, dpp_barset, dpp_offset);
15630
15631 /* Enable combined writes for DPP aperture */
15632 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15633#ifdef CONFIG_X86
15634 rc = set_memory_wc(pg_addr, 1);
15635 if (rc) {
15636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15637 "3272 Cannot setup Combined "
15638 "Write on WQ[%d] - disable DPP\n",
15639 wq->queue_id);
15640 phba->cfg_enable_dpp = 0;
15641 }
15642#else
15643 phba->cfg_enable_dpp = 0;
15644#endif
15645 } else
15646 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015647 }
James Smart895427b2017-02-12 13:52:30 -080015648 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15649 if (wq->pring == NULL) {
15650 status = -ENOMEM;
15651 goto out;
15652 }
James Smart4f774512009-05-22 14:52:35 -040015653 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015654 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015655 wq->subtype = subtype;
15656 wq->host_index = 0;
15657 wq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015658 wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040015659
15660 /* link the wq onto the parent cq child list */
15661 list_add_tail(&wq->list, &cq->child_list);
15662out:
James Smart8fa38512009-07-19 10:01:03 -040015663 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015664 return status;
15665}
15666
15667/**
15668 * lpfc_rq_create - Create a Receive Queue on the HBA
15669 * @phba: HBA structure that indicates port to create a queue on.
15670 * @hrq: The queue structure to use to create the header receive queue.
15671 * @drq: The queue structure to use to create the data receive queue.
15672 * @cq: The completion queue to bind this work queue to.
15673 *
15674 * This function creates a receive buffer queue pair , as detailed in @hrq and
15675 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15676 * to the HBA.
15677 *
15678 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15679 * struct is used to get the entry count that is necessary to determine the
15680 * number of pages to use for this queue. The @cq is used to indicate which
15681 * completion queue to bind received buffers that are posted to these queues to.
15682 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15683 * receive queue pair. This function is asynchronous and will wait for the
15684 * mailbox command to finish before continuing.
15685 *
15686 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015687 * memory this function will return -ENOMEM. If the queue create mailbox command
15688 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015689 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015690int
James Smart4f774512009-05-22 14:52:35 -040015691lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15692 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15693{
15694 struct lpfc_mbx_rq_create *rq_create;
15695 struct lpfc_dmabuf *dmabuf;
15696 LPFC_MBOXQ_t *mbox;
15697 int rc, length, status = 0;
15698 uint32_t shdr_status, shdr_add_status;
15699 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015700 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015701 void __iomem *bar_memmap_p;
15702 uint32_t db_offset;
15703 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015704
James Smart2e90f4b2011-12-13 13:22:37 -050015705 /* sanity check on queue memory */
15706 if (!hrq || !drq || !cq)
15707 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015708 if (!phba->sli4_hba.pc_sli4_params.supported)
15709 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015710
15711 if (hrq->entry_count != drq->entry_count)
15712 return -EINVAL;
15713 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15714 if (!mbox)
15715 return -ENOMEM;
15716 length = (sizeof(struct lpfc_mbx_rq_create) -
15717 sizeof(struct lpfc_sli4_cfg_mhdr));
15718 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15719 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15720 length, LPFC_SLI4_MBX_EMBED);
15721 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015722 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15723 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15724 phba->sli4_hba.pc_sli4_params.rqv);
15725 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15726 bf_set(lpfc_rq_context_rqe_count_1,
15727 &rq_create->u.request.context,
15728 hrq->entry_count);
15729 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015730 bf_set(lpfc_rq_context_rqe_size,
15731 &rq_create->u.request.context,
15732 LPFC_RQE_SIZE_8);
15733 bf_set(lpfc_rq_context_page_size,
15734 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015735 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015736 } else {
15737 switch (hrq->entry_count) {
15738 default:
15739 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15740 "2535 Unsupported RQ count. (%d)\n",
15741 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015742 if (hrq->entry_count < 512) {
15743 status = -EINVAL;
15744 goto out;
15745 }
James Smart5a6f1332011-03-11 16:05:35 -050015746 /* otherwise default to smallest count (drop through) */
15747 case 512:
15748 bf_set(lpfc_rq_context_rqe_count,
15749 &rq_create->u.request.context,
15750 LPFC_RQ_RING_SIZE_512);
15751 break;
15752 case 1024:
15753 bf_set(lpfc_rq_context_rqe_count,
15754 &rq_create->u.request.context,
15755 LPFC_RQ_RING_SIZE_1024);
15756 break;
15757 case 2048:
15758 bf_set(lpfc_rq_context_rqe_count,
15759 &rq_create->u.request.context,
15760 LPFC_RQ_RING_SIZE_2048);
15761 break;
15762 case 4096:
15763 bf_set(lpfc_rq_context_rqe_count,
15764 &rq_create->u.request.context,
15765 LPFC_RQ_RING_SIZE_4096);
15766 break;
15767 }
15768 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15769 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015770 }
15771 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15772 cq->queue_id);
15773 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15774 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015775 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015776 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015777 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15778 putPaddrLow(dmabuf->phys);
15779 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15780 putPaddrHigh(dmabuf->phys);
15781 }
James Smart962bc512013-01-03 15:44:00 -050015782 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15783 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15784
James Smart4f774512009-05-22 14:52:35 -040015785 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15786 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015787 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15788 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15789 if (shdr_status || shdr_add_status || rc) {
15790 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15791 "2504 RQ_CREATE mailbox failed with "
15792 "status x%x add_status x%x, mbx status x%x\n",
15793 shdr_status, shdr_add_status, rc);
15794 status = -ENXIO;
15795 goto out;
15796 }
15797 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15798 if (hrq->queue_id == 0xFFFF) {
15799 status = -ENXIO;
15800 goto out;
15801 }
James Smart962bc512013-01-03 15:44:00 -050015802
15803 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15804 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15805 &rq_create->u.response);
15806 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15807 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15809 "3262 RQ [%d] doorbell format not "
15810 "supported: x%x\n", hrq->queue_id,
15811 hrq->db_format);
15812 status = -EINVAL;
15813 goto out;
15814 }
15815
15816 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15817 &rq_create->u.response);
15818 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15819 if (!bar_memmap_p) {
15820 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15821 "3269 RQ[%d] failed to memmap pci "
15822 "barset:x%x\n", hrq->queue_id,
15823 pci_barset);
15824 status = -ENOMEM;
15825 goto out;
15826 }
15827
15828 db_offset = rq_create->u.response.doorbell_offset;
15829 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15830 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15831 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15832 "3270 RQ[%d] doorbell offset not "
15833 "supported: x%x\n", hrq->queue_id,
15834 db_offset);
15835 status = -EINVAL;
15836 goto out;
15837 }
15838 hrq->db_regaddr = bar_memmap_p + db_offset;
15839 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015840 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15841 "format:x%x\n", hrq->queue_id, pci_barset,
15842 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015843 } else {
15844 hrq->db_format = LPFC_DB_RING_FORMAT;
15845 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15846 }
James Smart4f774512009-05-22 14:52:35 -040015847 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015848 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015849 hrq->subtype = subtype;
15850 hrq->host_index = 0;
15851 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015852 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040015853
15854 /* now create the data queue */
15855 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15856 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15857 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015858 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15859 phba->sli4_hba.pc_sli4_params.rqv);
15860 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15861 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015862 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015863 if (subtype == LPFC_NVMET)
15864 rq_create->u.request.context.buffer_size =
15865 LPFC_NVMET_DATA_BUF_SIZE;
15866 else
15867 rq_create->u.request.context.buffer_size =
15868 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015869 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15870 LPFC_RQE_SIZE_8);
15871 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15872 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015873 } else {
15874 switch (drq->entry_count) {
15875 default:
15876 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15877 "2536 Unsupported RQ count. (%d)\n",
15878 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015879 if (drq->entry_count < 512) {
15880 status = -EINVAL;
15881 goto out;
15882 }
James Smart5a6f1332011-03-11 16:05:35 -050015883 /* otherwise default to smallest count (drop through) */
15884 case 512:
15885 bf_set(lpfc_rq_context_rqe_count,
15886 &rq_create->u.request.context,
15887 LPFC_RQ_RING_SIZE_512);
15888 break;
15889 case 1024:
15890 bf_set(lpfc_rq_context_rqe_count,
15891 &rq_create->u.request.context,
15892 LPFC_RQ_RING_SIZE_1024);
15893 break;
15894 case 2048:
15895 bf_set(lpfc_rq_context_rqe_count,
15896 &rq_create->u.request.context,
15897 LPFC_RQ_RING_SIZE_2048);
15898 break;
15899 case 4096:
15900 bf_set(lpfc_rq_context_rqe_count,
15901 &rq_create->u.request.context,
15902 LPFC_RQ_RING_SIZE_4096);
15903 break;
15904 }
James Smart3c603be2017-05-15 15:20:44 -070015905 if (subtype == LPFC_NVMET)
15906 bf_set(lpfc_rq_context_buf_size,
15907 &rq_create->u.request.context,
15908 LPFC_NVMET_DATA_BUF_SIZE);
15909 else
15910 bf_set(lpfc_rq_context_buf_size,
15911 &rq_create->u.request.context,
15912 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015913 }
15914 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15915 cq->queue_id);
15916 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15917 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015918 list_for_each_entry(dmabuf, &drq->page_list, list) {
15919 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15920 putPaddrLow(dmabuf->phys);
15921 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15922 putPaddrHigh(dmabuf->phys);
15923 }
James Smart962bc512013-01-03 15:44:00 -050015924 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15925 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015926 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15927 /* The IOCTL status is embedded in the mailbox subheader. */
15928 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15929 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15930 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15931 if (shdr_status || shdr_add_status || rc) {
15932 status = -ENXIO;
15933 goto out;
15934 }
15935 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15936 if (drq->queue_id == 0xFFFF) {
15937 status = -ENXIO;
15938 goto out;
15939 }
15940 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015941 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015942 drq->subtype = subtype;
15943 drq->host_index = 0;
15944 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015945 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040015946
15947 /* link the header and data RQs onto the parent cq child list */
15948 list_add_tail(&hrq->list, &cq->child_list);
15949 list_add_tail(&drq->list, &cq->child_list);
15950
15951out:
James Smart8fa38512009-07-19 10:01:03 -040015952 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015953 return status;
15954}
15955
15956/**
James Smart2d7dbc42017-02-12 13:52:35 -080015957 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15958 * @phba: HBA structure that indicates port to create a queue on.
15959 * @hrqp: The queue structure array to use to create the header receive queues.
15960 * @drqp: The queue structure array to use to create the data receive queues.
15961 * @cqp: The completion queue array to bind these receive queues to.
15962 *
15963 * This function creates a receive buffer queue pair , as detailed in @hrq and
15964 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15965 * to the HBA.
15966 *
15967 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15968 * struct is used to get the entry count that is necessary to determine the
15969 * number of pages to use for this queue. The @cq is used to indicate which
15970 * completion queue to bind received buffers that are posted to these queues to.
15971 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15972 * receive queue pair. This function is asynchronous and will wait for the
15973 * mailbox command to finish before continuing.
15974 *
15975 * On success this function will return a zero. If unable to allocate enough
15976 * memory this function will return -ENOMEM. If the queue create mailbox command
15977 * fails this function will return -ENXIO.
15978 **/
15979int
15980lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15981 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15982 uint32_t subtype)
15983{
15984 struct lpfc_queue *hrq, *drq, *cq;
15985 struct lpfc_mbx_rq_create_v2 *rq_create;
15986 struct lpfc_dmabuf *dmabuf;
15987 LPFC_MBOXQ_t *mbox;
15988 int rc, length, alloclen, status = 0;
15989 int cnt, idx, numrq, page_idx = 0;
15990 uint32_t shdr_status, shdr_add_status;
15991 union lpfc_sli4_cfg_shdr *shdr;
15992 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15993
15994 numrq = phba->cfg_nvmet_mrq;
15995 /* sanity check on array memory */
15996 if (!hrqp || !drqp || !cqp || !numrq)
15997 return -ENODEV;
15998 if (!phba->sli4_hba.pc_sli4_params.supported)
15999 hw_page_size = SLI4_PAGE_SIZE;
16000
16001 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16002 if (!mbox)
16003 return -ENOMEM;
16004
16005 length = sizeof(struct lpfc_mbx_rq_create_v2);
16006 length += ((2 * numrq * hrqp[0]->page_count) *
16007 sizeof(struct dma_address));
16008
16009 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16010 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
16011 LPFC_SLI4_MBX_NEMBED);
16012 if (alloclen < length) {
16013 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16014 "3099 Allocated DMA memory size (%d) is "
16015 "less than the requested DMA memory size "
16016 "(%d)\n", alloclen, length);
16017 status = -ENOMEM;
16018 goto out;
16019 }
16020
16021
16022
16023 rq_create = mbox->sge_array->addr[0];
16024 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
16025
16026 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
16027 cnt = 0;
16028
16029 for (idx = 0; idx < numrq; idx++) {
16030 hrq = hrqp[idx];
16031 drq = drqp[idx];
16032 cq = cqp[idx];
16033
James Smart2d7dbc42017-02-12 13:52:35 -080016034 /* sanity check on queue memory */
16035 if (!hrq || !drq || !cq) {
16036 status = -ENODEV;
16037 goto out;
16038 }
16039
James Smart7aabe842017-03-04 09:30:22 -080016040 if (hrq->entry_count != drq->entry_count) {
16041 status = -EINVAL;
16042 goto out;
16043 }
16044
James Smart2d7dbc42017-02-12 13:52:35 -080016045 if (idx == 0) {
16046 bf_set(lpfc_mbx_rq_create_num_pages,
16047 &rq_create->u.request,
16048 hrq->page_count);
16049 bf_set(lpfc_mbx_rq_create_rq_cnt,
16050 &rq_create->u.request, (numrq * 2));
16051 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
16052 1);
16053 bf_set(lpfc_rq_context_base_cq,
16054 &rq_create->u.request.context,
16055 cq->queue_id);
16056 bf_set(lpfc_rq_context_data_size,
16057 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070016058 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080016059 bf_set(lpfc_rq_context_hdr_size,
16060 &rq_create->u.request.context,
16061 LPFC_HDR_BUF_SIZE);
16062 bf_set(lpfc_rq_context_rqe_count_1,
16063 &rq_create->u.request.context,
16064 hrq->entry_count);
16065 bf_set(lpfc_rq_context_rqe_size,
16066 &rq_create->u.request.context,
16067 LPFC_RQE_SIZE_8);
16068 bf_set(lpfc_rq_context_page_size,
16069 &rq_create->u.request.context,
16070 (PAGE_SIZE/SLI4_PAGE_SIZE));
16071 }
16072 rc = 0;
16073 list_for_each_entry(dmabuf, &hrq->page_list, list) {
16074 memset(dmabuf->virt, 0, hw_page_size);
16075 cnt = page_idx + dmabuf->buffer_tag;
16076 rq_create->u.request.page[cnt].addr_lo =
16077 putPaddrLow(dmabuf->phys);
16078 rq_create->u.request.page[cnt].addr_hi =
16079 putPaddrHigh(dmabuf->phys);
16080 rc++;
16081 }
16082 page_idx += rc;
16083
16084 rc = 0;
16085 list_for_each_entry(dmabuf, &drq->page_list, list) {
16086 memset(dmabuf->virt, 0, hw_page_size);
16087 cnt = page_idx + dmabuf->buffer_tag;
16088 rq_create->u.request.page[cnt].addr_lo =
16089 putPaddrLow(dmabuf->phys);
16090 rq_create->u.request.page[cnt].addr_hi =
16091 putPaddrHigh(dmabuf->phys);
16092 rc++;
16093 }
16094 page_idx += rc;
16095
16096 hrq->db_format = LPFC_DB_RING_FORMAT;
16097 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16098 hrq->type = LPFC_HRQ;
16099 hrq->assoc_qid = cq->queue_id;
16100 hrq->subtype = subtype;
16101 hrq->host_index = 0;
16102 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016103 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080016104
16105 drq->db_format = LPFC_DB_RING_FORMAT;
16106 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16107 drq->type = LPFC_DRQ;
16108 drq->assoc_qid = cq->queue_id;
16109 drq->subtype = subtype;
16110 drq->host_index = 0;
16111 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016112 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080016113
16114 list_add_tail(&hrq->list, &cq->child_list);
16115 list_add_tail(&drq->list, &cq->child_list);
16116 }
16117
16118 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16119 /* The IOCTL status is embedded in the mailbox subheader. */
16120 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16121 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16122 if (shdr_status || shdr_add_status || rc) {
16123 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16124 "3120 RQ_CREATE mailbox failed with "
16125 "status x%x add_status x%x, mbx status x%x\n",
16126 shdr_status, shdr_add_status, rc);
16127 status = -ENXIO;
16128 goto out;
16129 }
16130 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16131 if (rc == 0xFFFF) {
16132 status = -ENXIO;
16133 goto out;
16134 }
16135
16136 /* Initialize all RQs with associated queue id */
16137 for (idx = 0; idx < numrq; idx++) {
16138 hrq = hrqp[idx];
16139 hrq->queue_id = rc + (2 * idx);
16140 drq = drqp[idx];
16141 drq->queue_id = rc + (2 * idx) + 1;
16142 }
16143
16144out:
16145 lpfc_sli4_mbox_cmd_free(phba, mbox);
16146 return status;
16147}
16148
16149/**
James Smart4f774512009-05-22 14:52:35 -040016150 * lpfc_eq_destroy - Destroy an event Queue on the HBA
16151 * @eq: The queue structure associated with the queue to destroy.
16152 *
16153 * This function destroys a queue, as detailed in @eq by sending an mailbox
16154 * command, specific to the type of queue, to the HBA.
16155 *
16156 * The @eq struct is used to get the queue ID of the queue to destroy.
16157 *
16158 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016159 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016160 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016161int
James Smart4f774512009-05-22 14:52:35 -040016162lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
16163{
16164 LPFC_MBOXQ_t *mbox;
16165 int rc, length, status = 0;
16166 uint32_t shdr_status, shdr_add_status;
16167 union lpfc_sli4_cfg_shdr *shdr;
16168
James Smart2e90f4b2011-12-13 13:22:37 -050016169 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016170 if (!eq)
16171 return -ENODEV;
James Smart32517fc2019-01-28 11:14:33 -080016172
James Smart4f774512009-05-22 14:52:35 -040016173 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
16174 if (!mbox)
16175 return -ENOMEM;
16176 length = (sizeof(struct lpfc_mbx_eq_destroy) -
16177 sizeof(struct lpfc_sli4_cfg_mhdr));
16178 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16179 LPFC_MBOX_OPCODE_EQ_DESTROY,
16180 length, LPFC_SLI4_MBX_EMBED);
16181 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
16182 eq->queue_id);
16183 mbox->vport = eq->phba->pport;
16184 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16185
16186 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
16187 /* The IOCTL status is embedded in the mailbox subheader. */
16188 shdr = (union lpfc_sli4_cfg_shdr *)
16189 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
16190 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16191 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16192 if (shdr_status || shdr_add_status || rc) {
16193 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16194 "2505 EQ_DESTROY mailbox failed with "
16195 "status x%x add_status x%x, mbx status x%x\n",
16196 shdr_status, shdr_add_status, rc);
16197 status = -ENXIO;
16198 }
16199
16200 /* Remove eq from any list */
16201 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040016202 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016203 return status;
16204}
16205
16206/**
16207 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
16208 * @cq: The queue structure associated with the queue to destroy.
16209 *
16210 * This function destroys a queue, as detailed in @cq by sending an mailbox
16211 * command, specific to the type of queue, to the HBA.
16212 *
16213 * The @cq struct is used to get the queue ID of the queue to destroy.
16214 *
16215 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016216 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016217 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016218int
James Smart4f774512009-05-22 14:52:35 -040016219lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
16220{
16221 LPFC_MBOXQ_t *mbox;
16222 int rc, length, status = 0;
16223 uint32_t shdr_status, shdr_add_status;
16224 union lpfc_sli4_cfg_shdr *shdr;
16225
James Smart2e90f4b2011-12-13 13:22:37 -050016226 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016227 if (!cq)
16228 return -ENODEV;
16229 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
16230 if (!mbox)
16231 return -ENOMEM;
16232 length = (sizeof(struct lpfc_mbx_cq_destroy) -
16233 sizeof(struct lpfc_sli4_cfg_mhdr));
16234 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16235 LPFC_MBOX_OPCODE_CQ_DESTROY,
16236 length, LPFC_SLI4_MBX_EMBED);
16237 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
16238 cq->queue_id);
16239 mbox->vport = cq->phba->pport;
16240 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16241 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
16242 /* The IOCTL status is embedded in the mailbox subheader. */
16243 shdr = (union lpfc_sli4_cfg_shdr *)
16244 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
16245 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16246 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16247 if (shdr_status || shdr_add_status || rc) {
16248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16249 "2506 CQ_DESTROY mailbox failed with "
16250 "status x%x add_status x%x, mbx status x%x\n",
16251 shdr_status, shdr_add_status, rc);
16252 status = -ENXIO;
16253 }
16254 /* Remove cq from any list */
16255 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040016256 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016257 return status;
16258}
16259
16260/**
James Smart04c68492009-05-22 14:52:52 -040016261 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
16262 * @qm: The queue structure associated with the queue to destroy.
16263 *
16264 * This function destroys a queue, as detailed in @mq by sending an mailbox
16265 * command, specific to the type of queue, to the HBA.
16266 *
16267 * The @mq struct is used to get the queue ID of the queue to destroy.
16268 *
16269 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016270 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040016271 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016272int
James Smart04c68492009-05-22 14:52:52 -040016273lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16274{
16275 LPFC_MBOXQ_t *mbox;
16276 int rc, length, status = 0;
16277 uint32_t shdr_status, shdr_add_status;
16278 union lpfc_sli4_cfg_shdr *shdr;
16279
James Smart2e90f4b2011-12-13 13:22:37 -050016280 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040016281 if (!mq)
16282 return -ENODEV;
16283 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16284 if (!mbox)
16285 return -ENOMEM;
16286 length = (sizeof(struct lpfc_mbx_mq_destroy) -
16287 sizeof(struct lpfc_sli4_cfg_mhdr));
16288 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16289 LPFC_MBOX_OPCODE_MQ_DESTROY,
16290 length, LPFC_SLI4_MBX_EMBED);
16291 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16292 mq->queue_id);
16293 mbox->vport = mq->phba->pport;
16294 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16295 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16296 /* The IOCTL status is embedded in the mailbox subheader. */
16297 shdr = (union lpfc_sli4_cfg_shdr *)
16298 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16299 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16300 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16301 if (shdr_status || shdr_add_status || rc) {
16302 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16303 "2507 MQ_DESTROY mailbox failed with "
16304 "status x%x add_status x%x, mbx status x%x\n",
16305 shdr_status, shdr_add_status, rc);
16306 status = -ENXIO;
16307 }
16308 /* Remove mq from any list */
16309 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040016310 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040016311 return status;
16312}
16313
16314/**
James Smart4f774512009-05-22 14:52:35 -040016315 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16316 * @wq: The queue structure associated with the queue to destroy.
16317 *
16318 * This function destroys a queue, as detailed in @wq by sending an mailbox
16319 * command, specific to the type of queue, to the HBA.
16320 *
16321 * The @wq struct is used to get the queue ID of the queue to destroy.
16322 *
16323 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016324 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016325 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016326int
James Smart4f774512009-05-22 14:52:35 -040016327lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16328{
16329 LPFC_MBOXQ_t *mbox;
16330 int rc, length, status = 0;
16331 uint32_t shdr_status, shdr_add_status;
16332 union lpfc_sli4_cfg_shdr *shdr;
16333
James Smart2e90f4b2011-12-13 13:22:37 -050016334 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016335 if (!wq)
16336 return -ENODEV;
16337 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16338 if (!mbox)
16339 return -ENOMEM;
16340 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16341 sizeof(struct lpfc_sli4_cfg_mhdr));
16342 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16343 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16344 length, LPFC_SLI4_MBX_EMBED);
16345 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16346 wq->queue_id);
16347 mbox->vport = wq->phba->pport;
16348 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16349 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16350 shdr = (union lpfc_sli4_cfg_shdr *)
16351 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16352 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16353 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16354 if (shdr_status || shdr_add_status || rc) {
16355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16356 "2508 WQ_DESTROY mailbox failed with "
16357 "status x%x add_status x%x, mbx status x%x\n",
16358 shdr_status, shdr_add_status, rc);
16359 status = -ENXIO;
16360 }
16361 /* Remove wq from any list */
16362 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070016363 kfree(wq->pring);
16364 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040016365 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016366 return status;
16367}
16368
16369/**
16370 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16371 * @rq: The queue structure associated with the queue to destroy.
16372 *
16373 * This function destroys a queue, as detailed in @rq by sending an mailbox
16374 * command, specific to the type of queue, to the HBA.
16375 *
16376 * The @rq struct is used to get the queue ID of the queue to destroy.
16377 *
16378 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016379 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016380 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016381int
James Smart4f774512009-05-22 14:52:35 -040016382lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16383 struct lpfc_queue *drq)
16384{
16385 LPFC_MBOXQ_t *mbox;
16386 int rc, length, status = 0;
16387 uint32_t shdr_status, shdr_add_status;
16388 union lpfc_sli4_cfg_shdr *shdr;
16389
James Smart2e90f4b2011-12-13 13:22:37 -050016390 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016391 if (!hrq || !drq)
16392 return -ENODEV;
16393 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16394 if (!mbox)
16395 return -ENOMEM;
16396 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050016397 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040016398 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16399 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16400 length, LPFC_SLI4_MBX_EMBED);
16401 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16402 hrq->queue_id);
16403 mbox->vport = hrq->phba->pport;
16404 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16405 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16406 /* The IOCTL status is embedded in the mailbox subheader. */
16407 shdr = (union lpfc_sli4_cfg_shdr *)
16408 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16409 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16410 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16411 if (shdr_status || shdr_add_status || rc) {
16412 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16413 "2509 RQ_DESTROY mailbox failed with "
16414 "status x%x add_status x%x, mbx status x%x\n",
16415 shdr_status, shdr_add_status, rc);
16416 if (rc != MBX_TIMEOUT)
16417 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16418 return -ENXIO;
16419 }
16420 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16421 drq->queue_id);
16422 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16423 shdr = (union lpfc_sli4_cfg_shdr *)
16424 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16425 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16426 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16427 if (shdr_status || shdr_add_status || rc) {
16428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16429 "2510 RQ_DESTROY mailbox failed with "
16430 "status x%x add_status x%x, mbx status x%x\n",
16431 shdr_status, shdr_add_status, rc);
16432 status = -ENXIO;
16433 }
16434 list_del_init(&hrq->list);
16435 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040016436 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016437 return status;
16438}
16439
16440/**
16441 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16442 * @phba: The virtual port for which this call being executed.
16443 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16444 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16445 * @xritag: the xritag that ties this io to the SGL pages.
16446 *
16447 * This routine will post the sgl pages for the IO that has the xritag
16448 * that is in the iocbq structure. The xritag is assigned during iocbq
16449 * creation and persists for as long as the driver is loaded.
16450 * if the caller has fewer than 256 scatter gather segments to map then
16451 * pdma_phys_addr1 should be 0.
16452 * If the caller needs to map more than 256 scatter gather segment then
16453 * pdma_phys_addr1 should be a valid physical address.
16454 * physical address for SGLs must be 64 byte aligned.
16455 * If you are going to map 2 SGL's then the first one must have 256 entries
16456 * the second sgl can have between 1 and 256 entries.
16457 *
16458 * Return codes:
16459 * 0 - Success
16460 * -ENXIO, -ENOMEM - Failure
16461 **/
16462int
16463lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16464 dma_addr_t pdma_phys_addr0,
16465 dma_addr_t pdma_phys_addr1,
16466 uint16_t xritag)
16467{
16468 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16469 LPFC_MBOXQ_t *mbox;
16470 int rc;
16471 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040016472 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040016473 union lpfc_sli4_cfg_shdr *shdr;
16474
16475 if (xritag == NO_XRI) {
16476 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16477 "0364 Invalid param:\n");
16478 return -EINVAL;
16479 }
16480
16481 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16482 if (!mbox)
16483 return -ENOMEM;
16484
16485 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16486 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16487 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050016488 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040016489
16490 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16491 &mbox->u.mqe.un.post_sgl_pages;
16492 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16493 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16494
16495 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16496 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16497 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16498 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16499
16500 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16501 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16502 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16503 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16504 if (!phba->sli4_hba.intr_enable)
16505 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040016506 else {
James Smarta183a152011-10-10 21:32:43 -040016507 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040016508 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16509 }
James Smart4f774512009-05-22 14:52:35 -040016510 /* The IOCTL status is embedded in the mailbox subheader. */
16511 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16512 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16513 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16514 if (rc != MBX_TIMEOUT)
16515 mempool_free(mbox, phba->mbox_mem_pool);
16516 if (shdr_status || shdr_add_status || rc) {
16517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16518 "2511 POST_SGL mailbox failed with "
16519 "status x%x add_status x%x, mbx status x%x\n",
16520 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040016521 }
16522 return 0;
16523}
James Smart4f774512009-05-22 14:52:35 -040016524
16525/**
James Smart88a2cfb2011-07-22 18:36:33 -040016526 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040016527 * @phba: pointer to lpfc hba data structure.
16528 *
16529 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040016530 * HBA consistent with the SLI-4 interface spec. This routine
16531 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16532 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040016533 *
James Smart88a2cfb2011-07-22 18:36:33 -040016534 * Returns
16535 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16536 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16537 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016538static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016539lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16540{
16541 unsigned long xri;
16542
16543 /*
16544 * Fetch the next logical xri. Because this index is logical,
16545 * the driver starts at 0 each time.
16546 */
16547 spin_lock_irq(&phba->hbalock);
16548 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16549 phba->sli4_hba.max_cfg_param.max_xri, 0);
16550 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16551 spin_unlock_irq(&phba->hbalock);
16552 return NO_XRI;
16553 } else {
16554 set_bit(xri, phba->sli4_hba.xri_bmask);
16555 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040016556 }
James Smart6d368e52011-05-24 11:44:12 -040016557 spin_unlock_irq(&phba->hbalock);
16558 return xri;
16559}
16560
16561/**
16562 * lpfc_sli4_free_xri - Release an xri for reuse.
16563 * @phba: pointer to lpfc hba data structure.
16564 *
16565 * This routine is invoked to release an xri to the pool of
16566 * available rpis maintained by the driver.
16567 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016568static void
James Smart6d368e52011-05-24 11:44:12 -040016569__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16570{
16571 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016572 phba->sli4_hba.max_cfg_param.xri_used--;
16573 }
16574}
16575
16576/**
16577 * lpfc_sli4_free_xri - Release an xri for reuse.
16578 * @phba: pointer to lpfc hba data structure.
16579 *
16580 * This routine is invoked to release an xri to the pool of
16581 * available rpis maintained by the driver.
16582 **/
16583void
16584lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16585{
16586 spin_lock_irq(&phba->hbalock);
16587 __lpfc_sli4_free_xri(phba, xri);
16588 spin_unlock_irq(&phba->hbalock);
16589}
16590
16591/**
James Smart4f774512009-05-22 14:52:35 -040016592 * lpfc_sli4_next_xritag - Get an xritag for the io
16593 * @phba: Pointer to HBA context object.
16594 *
16595 * This function gets an xritag for the iocb. If there is no unused xritag
16596 * it will return 0xffff.
16597 * The function returns the allocated xritag if successful, else returns zero.
16598 * Zero is not a valid xritag.
16599 * The caller is not required to hold any lock.
16600 **/
16601uint16_t
16602lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16603{
James Smart6d368e52011-05-24 11:44:12 -040016604 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016605
James Smart6d368e52011-05-24 11:44:12 -040016606 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016607 if (xri_index == NO_XRI)
16608 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16609 "2004 Failed to allocate XRI.last XRITAG is %d"
16610 " Max XRI is %d, Used XRI is %d\n",
16611 xri_index,
16612 phba->sli4_hba.max_cfg_param.max_xri,
16613 phba->sli4_hba.max_cfg_param.xri_used);
16614 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016615}
16616
16617/**
James Smart895427b2017-02-12 13:52:30 -080016618 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016619 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016620 * @post_sgl_list: pointer to els sgl entry list.
16621 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016622 *
16623 * This routine is invoked to post a block of driver's sgl pages to the
16624 * HBA using non-embedded mailbox command. No Lock is held. This routine
16625 * is only called when the driver is loading and after all IO has been
16626 * stopped.
16627 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016628static int
James Smart895427b2017-02-12 13:52:30 -080016629lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016630 struct list_head *post_sgl_list,
16631 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016632{
James Smart8a9d2e82012-05-09 21:16:12 -040016633 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016634 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16635 struct sgl_page_pairs *sgl_pg_pairs;
16636 void *viraddr;
16637 LPFC_MBOXQ_t *mbox;
16638 uint32_t reqlen, alloclen, pg_pairs;
16639 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016640 uint16_t xritag_start = 0;
16641 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016642 uint32_t shdr_status, shdr_add_status;
16643 union lpfc_sli4_cfg_shdr *shdr;
16644
James Smart895427b2017-02-12 13:52:30 -080016645 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016646 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016647 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016648 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016649 "2559 Block sgl registration required DMA "
16650 "size (%d) great than a page\n", reqlen);
16651 return -ENOMEM;
16652 }
James Smart895427b2017-02-12 13:52:30 -080016653
James Smart4f774512009-05-22 14:52:35 -040016654 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016655 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016656 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016657
16658 /* Allocate DMA memory and set up the non-embedded mailbox command */
16659 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16660 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16661 LPFC_SLI4_MBX_NEMBED);
16662
16663 if (alloclen < reqlen) {
16664 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16665 "0285 Allocated DMA memory size (%d) is "
16666 "less than the requested DMA memory "
16667 "size (%d)\n", alloclen, reqlen);
16668 lpfc_sli4_mbox_cmd_free(phba, mbox);
16669 return -ENOMEM;
16670 }
James Smart4f774512009-05-22 14:52:35 -040016671 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016672 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016673 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16674 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16675
James Smart8a9d2e82012-05-09 21:16:12 -040016676 pg_pairs = 0;
16677 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016678 /* Set up the sge entry */
16679 sgl_pg_pairs->sgl_pg0_addr_lo =
16680 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16681 sgl_pg_pairs->sgl_pg0_addr_hi =
16682 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16683 sgl_pg_pairs->sgl_pg1_addr_lo =
16684 cpu_to_le32(putPaddrLow(0));
16685 sgl_pg_pairs->sgl_pg1_addr_hi =
16686 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016687
James Smart4f774512009-05-22 14:52:35 -040016688 /* Keep the first xritag on the list */
16689 if (pg_pairs == 0)
16690 xritag_start = sglq_entry->sli4_xritag;
16691 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016692 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016693 }
James Smart6d368e52011-05-24 11:44:12 -040016694
16695 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016696 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016697 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016698 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016699
James Smart4f774512009-05-22 14:52:35 -040016700 if (!phba->sli4_hba.intr_enable)
16701 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16702 else {
James Smarta183a152011-10-10 21:32:43 -040016703 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016704 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16705 }
16706 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16707 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16708 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16709 if (rc != MBX_TIMEOUT)
16710 lpfc_sli4_mbox_cmd_free(phba, mbox);
16711 if (shdr_status || shdr_add_status || rc) {
16712 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16713 "2513 POST_SGL_BLOCK mailbox command failed "
16714 "status x%x add_status x%x mbx status x%x\n",
16715 shdr_status, shdr_add_status, rc);
16716 rc = -ENXIO;
16717 }
16718 return rc;
16719}
16720
16721/**
James Smart5e5b5112019-01-28 11:14:22 -080016722 * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
James Smart4f774512009-05-22 14:52:35 -040016723 * @phba: pointer to lpfc hba data structure.
James Smart0794d602019-01-28 11:14:19 -080016724 * @nblist: pointer to nvme buffer list.
James Smart4f774512009-05-22 14:52:35 -040016725 * @count: number of scsi buffers on the list.
16726 *
16727 * This routine is invoked to post a block of @count scsi sgl pages from a
James Smart0794d602019-01-28 11:14:19 -080016728 * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
James Smart4f774512009-05-22 14:52:35 -040016729 * No Lock is held.
16730 *
16731 **/
James Smart0794d602019-01-28 11:14:19 -080016732static int
James Smart5e5b5112019-01-28 11:14:22 -080016733lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
16734 int count)
James Smart4f774512009-05-22 14:52:35 -040016735{
James Smartc4908502019-01-28 11:14:28 -080016736 struct lpfc_io_buf *lpfc_ncmd;
James Smart4f774512009-05-22 14:52:35 -040016737 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16738 struct sgl_page_pairs *sgl_pg_pairs;
16739 void *viraddr;
16740 LPFC_MBOXQ_t *mbox;
16741 uint32_t reqlen, alloclen, pg_pairs;
16742 uint32_t mbox_tmo;
16743 uint16_t xritag_start = 0;
16744 int rc = 0;
16745 uint32_t shdr_status, shdr_add_status;
16746 dma_addr_t pdma_phys_bpl1;
16747 union lpfc_sli4_cfg_shdr *shdr;
16748
16749 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016750 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016751 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016752 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016753 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016754 "6118 Block sgl registration required DMA "
James Smart4f774512009-05-22 14:52:35 -040016755 "size (%d) great than a page\n", reqlen);
16756 return -ENOMEM;
16757 }
16758 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16759 if (!mbox) {
16760 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016761 "6119 Failed to allocate mbox cmd memory\n");
James Smart4f774512009-05-22 14:52:35 -040016762 return -ENOMEM;
16763 }
16764
16765 /* Allocate DMA memory and set up the non-embedded mailbox command */
16766 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
James Smart0794d602019-01-28 11:14:19 -080016767 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16768 reqlen, LPFC_SLI4_MBX_NEMBED);
James Smart4f774512009-05-22 14:52:35 -040016769
16770 if (alloclen < reqlen) {
16771 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016772 "6120 Allocated DMA memory size (%d) is "
James Smart4f774512009-05-22 14:52:35 -040016773 "less than the requested DMA memory "
16774 "size (%d)\n", alloclen, reqlen);
16775 lpfc_sli4_mbox_cmd_free(phba, mbox);
16776 return -ENOMEM;
16777 }
James Smart6d368e52011-05-24 11:44:12 -040016778
James Smart4f774512009-05-22 14:52:35 -040016779 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016780 viraddr = mbox->sge_array->addr[0];
16781
16782 /* Set up the SGL pages in the non-embedded DMA pages */
16783 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16784 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16785
16786 pg_pairs = 0;
James Smart0794d602019-01-28 11:14:19 -080016787 list_for_each_entry(lpfc_ncmd, nblist, list) {
James Smart4f774512009-05-22 14:52:35 -040016788 /* Set up the sge entry */
16789 sgl_pg_pairs->sgl_pg0_addr_lo =
James Smart0794d602019-01-28 11:14:19 -080016790 cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040016791 sgl_pg_pairs->sgl_pg0_addr_hi =
James Smart0794d602019-01-28 11:14:19 -080016792 cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040016793 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
James Smart0794d602019-01-28 11:14:19 -080016794 pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
16795 SGL_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040016796 else
16797 pdma_phys_bpl1 = 0;
16798 sgl_pg_pairs->sgl_pg1_addr_lo =
16799 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16800 sgl_pg_pairs->sgl_pg1_addr_hi =
16801 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16802 /* Keep the first xritag on the list */
16803 if (pg_pairs == 0)
James Smart0794d602019-01-28 11:14:19 -080016804 xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -040016805 sgl_pg_pairs++;
16806 pg_pairs++;
16807 }
16808 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16809 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16810 /* Perform endian conversion if necessary */
16811 sgl->word0 = cpu_to_le32(sgl->word0);
16812
James Smart0794d602019-01-28 11:14:19 -080016813 if (!phba->sli4_hba.intr_enable) {
James Smart4f774512009-05-22 14:52:35 -040016814 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart0794d602019-01-28 11:14:19 -080016815 } else {
James Smarta183a152011-10-10 21:32:43 -040016816 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016817 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16818 }
James Smart0794d602019-01-28 11:14:19 -080016819 shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040016820 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16821 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16822 if (rc != MBX_TIMEOUT)
16823 lpfc_sli4_mbox_cmd_free(phba, mbox);
16824 if (shdr_status || shdr_add_status || rc) {
16825 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -080016826 "6125 POST_SGL_BLOCK mailbox command failed "
James Smart4f774512009-05-22 14:52:35 -040016827 "status x%x add_status x%x mbx status x%x\n",
16828 shdr_status, shdr_add_status, rc);
16829 rc = -ENXIO;
16830 }
16831 return rc;
16832}
16833
16834/**
James Smart5e5b5112019-01-28 11:14:22 -080016835 * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
James Smart0794d602019-01-28 11:14:19 -080016836 * @phba: pointer to lpfc hba data structure.
16837 * @post_nblist: pointer to the nvme buffer list.
16838 *
16839 * This routine walks a list of nvme buffers that was passed in. It attempts
16840 * to construct blocks of nvme buffer sgls which contains contiguous xris and
16841 * uses the non-embedded SGL block post mailbox commands to post to the port.
16842 * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
16843 * embedded SGL post mailbox command for posting. The @post_nblist passed in
16844 * must be local list, thus no lock is needed when manipulate the list.
16845 *
16846 * Returns: 0 = failure, non-zero number of successfully posted buffers.
16847 **/
16848int
James Smart5e5b5112019-01-28 11:14:22 -080016849lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
16850 struct list_head *post_nblist, int sb_count)
James Smart0794d602019-01-28 11:14:19 -080016851{
James Smartc4908502019-01-28 11:14:28 -080016852 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart0794d602019-01-28 11:14:19 -080016853 int status, sgl_size;
16854 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
16855 dma_addr_t pdma_phys_sgl1;
16856 int last_xritag = NO_XRI;
16857 int cur_xritag;
James Smart0794d602019-01-28 11:14:19 -080016858 LIST_HEAD(prep_nblist);
16859 LIST_HEAD(blck_nblist);
16860 LIST_HEAD(nvme_nblist);
16861
16862 /* sanity check */
16863 if (sb_count <= 0)
16864 return -EINVAL;
16865
16866 sgl_size = phba->cfg_sg_dma_buf_size;
16867 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
16868 list_del_init(&lpfc_ncmd->list);
16869 block_cnt++;
16870 if ((last_xritag != NO_XRI) &&
16871 (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
16872 /* a hole in xri block, form a sgl posting block */
16873 list_splice_init(&prep_nblist, &blck_nblist);
16874 post_cnt = block_cnt - 1;
16875 /* prepare list for next posting block */
16876 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
16877 block_cnt = 1;
16878 } else {
16879 /* prepare list for next posting block */
16880 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
16881 /* enough sgls for non-embed sgl mbox command */
16882 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
16883 list_splice_init(&prep_nblist, &blck_nblist);
16884 post_cnt = block_cnt;
16885 block_cnt = 0;
16886 }
16887 }
16888 num_posting++;
16889 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
16890
16891 /* end of repost sgl list condition for NVME buffers */
16892 if (num_posting == sb_count) {
16893 if (post_cnt == 0) {
16894 /* last sgl posting block */
16895 list_splice_init(&prep_nblist, &blck_nblist);
16896 post_cnt = block_cnt;
16897 } else if (block_cnt == 1) {
16898 /* last single sgl with non-contiguous xri */
16899 if (sgl_size > SGL_PAGE_SIZE)
16900 pdma_phys_sgl1 =
16901 lpfc_ncmd->dma_phys_sgl +
16902 SGL_PAGE_SIZE;
16903 else
16904 pdma_phys_sgl1 = 0;
16905 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
16906 status = lpfc_sli4_post_sgl(
16907 phba, lpfc_ncmd->dma_phys_sgl,
16908 pdma_phys_sgl1, cur_xritag);
16909 if (status) {
James Smartc4908502019-01-28 11:14:28 -080016910 /* Post error. Buffer unavailable. */
16911 lpfc_ncmd->flags |=
16912 LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016913 } else {
James Smartc4908502019-01-28 11:14:28 -080016914 /* Post success. Bffer available. */
16915 lpfc_ncmd->flags &=
16916 ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016917 lpfc_ncmd->status = IOSTAT_SUCCESS;
16918 num_posted++;
16919 }
16920 /* success, put on NVME buffer sgl list */
16921 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
16922 }
16923 }
16924
16925 /* continue until a nembed page worth of sgls */
16926 if (post_cnt == 0)
16927 continue;
16928
16929 /* post block of NVME buffer list sgls */
James Smart5e5b5112019-01-28 11:14:22 -080016930 status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
16931 post_cnt);
James Smart0794d602019-01-28 11:14:19 -080016932
16933 /* don't reset xirtag due to hole in xri block */
16934 if (block_cnt == 0)
16935 last_xritag = NO_XRI;
16936
16937 /* reset NVME buffer post count for next round of posting */
16938 post_cnt = 0;
16939
16940 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
16941 while (!list_empty(&blck_nblist)) {
16942 list_remove_head(&blck_nblist, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -080016943 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -080016944 if (status) {
James Smartc4908502019-01-28 11:14:28 -080016945 /* Post error. Mark buffer unavailable. */
16946 lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016947 } else {
James Smartc4908502019-01-28 11:14:28 -080016948 /* Post success, Mark buffer available. */
16949 lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016950 lpfc_ncmd->status = IOSTAT_SUCCESS;
16951 num_posted++;
16952 }
16953 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
16954 }
16955 }
16956 /* Push NVME buffers with sgl posted to the available list */
James Smart5e5b5112019-01-28 11:14:22 -080016957 lpfc_io_buf_replenish(phba, &nvme_nblist);
16958
James Smart0794d602019-01-28 11:14:19 -080016959 return num_posted;
16960}
16961
16962/**
James Smart4f774512009-05-22 14:52:35 -040016963 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16964 * @phba: pointer to lpfc_hba struct that the frame was received on
16965 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16966 *
16967 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16968 * valid type of frame that the LPFC driver will handle. This function will
16969 * return a zero if the frame is a valid frame or a non zero value when the
16970 * frame does not pass the check.
16971 **/
16972static int
16973lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16974{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016975 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016976 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016977 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016978
16979 switch (fc_hdr->fh_r_ctl) {
16980 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16981 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16982 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16983 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16984 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16985 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16986 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16987 case FC_RCTL_DD_CMD_STATUS: /* command status */
16988 case FC_RCTL_ELS_REQ: /* extended link services request */
16989 case FC_RCTL_ELS_REP: /* extended link services reply */
16990 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16991 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16992 case FC_RCTL_BA_NOP: /* basic link service NOP */
16993 case FC_RCTL_BA_ABTS: /* basic link service abort */
16994 case FC_RCTL_BA_RMC: /* remove connection */
16995 case FC_RCTL_BA_ACC: /* basic accept */
16996 case FC_RCTL_BA_RJT: /* basic reject */
16997 case FC_RCTL_BA_PRMT:
16998 case FC_RCTL_ACK_1: /* acknowledge_1 */
16999 case FC_RCTL_ACK_0: /* acknowledge_0 */
17000 case FC_RCTL_P_RJT: /* port reject */
17001 case FC_RCTL_F_RJT: /* fabric reject */
17002 case FC_RCTL_P_BSY: /* port busy */
17003 case FC_RCTL_F_BSY: /* fabric busy to data frame */
17004 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
17005 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070017006 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040017007 case FC_RCTL_END: /* end */
17008 break;
17009 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
17010 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
17011 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
17012 return lpfc_fc_frame_check(phba, fc_hdr);
17013 default:
17014 goto drop;
17015 }
James Smartae9e28f2017-05-15 15:20:51 -070017016
James Smart4f774512009-05-22 14:52:35 -040017017 switch (fc_hdr->fh_type) {
17018 case FC_TYPE_BLS:
17019 case FC_TYPE_ELS:
17020 case FC_TYPE_FCP:
17021 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080017022 case FC_TYPE_NVME:
James Smart4f774512009-05-22 14:52:35 -040017023 break;
17024 case FC_TYPE_IP:
17025 case FC_TYPE_ILS:
17026 default:
17027 goto drop;
17028 }
James Smart546fc852011-03-11 16:06:29 -050017029
James Smart4f774512009-05-22 14:52:35 -040017030 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070017031 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040017032 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070017033 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
17034 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
17035 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
17036 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
17037 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040017038 return 0;
17039drop:
17040 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070017041 "2539 Dropped frame rctl:x%x type:x%x\n",
17042 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040017043 return 1;
17044}
17045
17046/**
17047 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
17048 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17049 *
17050 * This function processes the FC header to retrieve the VFI from the VF
17051 * header, if one exists. This function will return the VFI if one exists
17052 * or 0 if no VSAN Header exists.
17053 **/
17054static uint32_t
17055lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
17056{
17057 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
17058
17059 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
17060 return 0;
17061 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
17062}
17063
17064/**
17065 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
17066 * @phba: Pointer to the HBA structure to search for the vport on
17067 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17068 * @fcfi: The FC Fabric ID that the frame came from
17069 *
17070 * This function searches the @phba for a vport that matches the content of the
17071 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
17072 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
17073 * returns the matching vport pointer or NULL if unable to match frame to a
17074 * vport.
17075 **/
17076static struct lpfc_vport *
17077lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080017078 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040017079{
17080 struct lpfc_vport **vports;
17081 struct lpfc_vport *vport = NULL;
17082 int i;
James Smart939723a2012-05-09 21:19:03 -040017083
James Smartbf086112011-08-21 21:48:13 -040017084 if (did == Fabric_DID)
17085 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040017086 if ((phba->pport->fc_flag & FC_PT2PT) &&
17087 !(phba->link_state == LPFC_HBA_READY))
17088 return phba->pport;
17089
James Smart4f774512009-05-22 14:52:35 -040017090 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080017091 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040017092 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
17093 if (phba->fcf.fcfi == fcfi &&
17094 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
17095 vports[i]->fc_myDID == did) {
17096 vport = vports[i];
17097 break;
17098 }
17099 }
James Smart895427b2017-02-12 13:52:30 -080017100 }
James Smart4f774512009-05-22 14:52:35 -040017101 lpfc_destroy_vport_work_array(phba, vports);
17102 return vport;
17103}
17104
17105/**
James Smart45ed1192009-10-02 15:17:02 -040017106 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
17107 * @vport: The vport to work on.
17108 *
17109 * This function updates the receive sequence time stamp for this vport. The
17110 * receive sequence time stamp indicates the time that the last frame of the
17111 * the sequence that has been idle for the longest amount of time was received.
17112 * the driver uses this time stamp to indicate if any received sequences have
17113 * timed out.
17114 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017115static void
James Smart45ed1192009-10-02 15:17:02 -040017116lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
17117{
17118 struct lpfc_dmabuf *h_buf;
17119 struct hbq_dmabuf *dmabuf = NULL;
17120
17121 /* get the oldest sequence on the rcv list */
17122 h_buf = list_get_first(&vport->rcv_buffer_list,
17123 struct lpfc_dmabuf, list);
17124 if (!h_buf)
17125 return;
17126 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17127 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
17128}
17129
17130/**
17131 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
17132 * @vport: The vport that the received sequences were sent to.
17133 *
17134 * This function cleans up all outstanding received sequences. This is called
17135 * by the driver when a link event or user action invalidates all the received
17136 * sequences.
17137 **/
17138void
17139lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
17140{
17141 struct lpfc_dmabuf *h_buf, *hnext;
17142 struct lpfc_dmabuf *d_buf, *dnext;
17143 struct hbq_dmabuf *dmabuf = NULL;
17144
17145 /* start with the oldest sequence on the rcv list */
17146 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17147 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17148 list_del_init(&dmabuf->hbuf.list);
17149 list_for_each_entry_safe(d_buf, dnext,
17150 &dmabuf->dbuf.list, list) {
17151 list_del_init(&d_buf->list);
17152 lpfc_in_buf_free(vport->phba, d_buf);
17153 }
17154 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17155 }
17156}
17157
17158/**
17159 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
17160 * @vport: The vport that the received sequences were sent to.
17161 *
17162 * This function determines whether any received sequences have timed out by
17163 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
17164 * indicates that there is at least one timed out sequence this routine will
17165 * go through the received sequences one at a time from most inactive to most
17166 * active to determine which ones need to be cleaned up. Once it has determined
17167 * that a sequence needs to be cleaned up it will simply free up the resources
17168 * without sending an abort.
17169 **/
17170void
17171lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
17172{
17173 struct lpfc_dmabuf *h_buf, *hnext;
17174 struct lpfc_dmabuf *d_buf, *dnext;
17175 struct hbq_dmabuf *dmabuf = NULL;
17176 unsigned long timeout;
17177 int abort_count = 0;
17178
17179 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17180 vport->rcv_buffer_time_stamp);
17181 if (list_empty(&vport->rcv_buffer_list) ||
17182 time_before(jiffies, timeout))
17183 return;
17184 /* start with the oldest sequence on the rcv list */
17185 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17186 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17187 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17188 dmabuf->time_stamp);
17189 if (time_before(jiffies, timeout))
17190 break;
17191 abort_count++;
17192 list_del_init(&dmabuf->hbuf.list);
17193 list_for_each_entry_safe(d_buf, dnext,
17194 &dmabuf->dbuf.list, list) {
17195 list_del_init(&d_buf->list);
17196 lpfc_in_buf_free(vport->phba, d_buf);
17197 }
17198 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17199 }
17200 if (abort_count)
17201 lpfc_update_rcv_time_stamp(vport);
17202}
17203
17204/**
James Smart4f774512009-05-22 14:52:35 -040017205 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
17206 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
17207 *
17208 * This function searches through the existing incomplete sequences that have
17209 * been sent to this @vport. If the frame matches one of the incomplete
17210 * sequences then the dbuf in the @dmabuf is added to the list of frames that
17211 * make up that sequence. If no sequence is found that matches this frame then
17212 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
17213 * This function returns a pointer to the first dmabuf in the sequence list that
17214 * the frame was linked to.
17215 **/
17216static struct hbq_dmabuf *
17217lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17218{
17219 struct fc_frame_header *new_hdr;
17220 struct fc_frame_header *temp_hdr;
17221 struct lpfc_dmabuf *d_buf;
17222 struct lpfc_dmabuf *h_buf;
17223 struct hbq_dmabuf *seq_dmabuf = NULL;
17224 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050017225 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040017226
James Smart4d9ab992009-10-02 15:16:39 -040017227 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017228 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040017229 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050017230
James Smart4f774512009-05-22 14:52:35 -040017231 /* Use the hdr_buf to find the sequence that this frame belongs to */
17232 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17233 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17234 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17235 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17236 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17237 continue;
17238 /* found a pending sequence that matches this frame */
17239 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17240 break;
17241 }
17242 if (!seq_dmabuf) {
17243 /*
17244 * This indicates first frame received for this sequence.
17245 * Queue the buffer on the vport's rcv_buffer_list.
17246 */
17247 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040017248 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017249 return dmabuf;
17250 }
17251 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050017252 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
17253 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040017254 list_del_init(&seq_dmabuf->hbuf.list);
17255 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17256 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017257 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017258 return dmabuf;
17259 }
James Smart45ed1192009-10-02 15:17:02 -040017260 /* move this sequence to the tail to indicate a young sequence */
17261 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
17262 seq_dmabuf->time_stamp = jiffies;
17263 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050017264 if (list_empty(&seq_dmabuf->dbuf.list)) {
17265 temp_hdr = dmabuf->hbuf.virt;
17266 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17267 return seq_dmabuf;
17268 }
James Smart4f774512009-05-22 14:52:35 -040017269 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050017270 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
17271 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040017272 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17273 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
17274 /*
17275 * If the frame's sequence count is greater than the frame on
17276 * the list then insert the frame right after this frame
17277 */
James Smarteeead812009-12-21 17:01:23 -050017278 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
17279 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040017280 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050017281 found = 1;
17282 break;
James Smart4f774512009-05-22 14:52:35 -040017283 }
James Smart4360ca92015-12-16 18:12:04 -050017284
17285 if (&d_buf->list == &seq_dmabuf->dbuf.list)
17286 break;
17287 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040017288 }
James Smart4360ca92015-12-16 18:12:04 -050017289
17290 if (found)
17291 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017292 return NULL;
17293}
17294
17295/**
James Smart6669f9b2009-10-02 15:16:45 -040017296 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
17297 * @vport: pointer to a vitural port
17298 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17299 *
17300 * This function tries to abort from the partially assembed sequence, described
17301 * by the information from basic abbort @dmabuf. It checks to see whether such
17302 * partially assembled sequence held by the driver. If so, it shall free up all
17303 * the frames from the partially assembled sequence.
17304 *
17305 * Return
17306 * true -- if there is matching partially assembled sequence present and all
17307 * the frames freed with the sequence;
17308 * false -- if there is no matching partially assembled sequence present so
17309 * nothing got aborted in the lower layer driver
17310 **/
17311static bool
17312lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
17313 struct hbq_dmabuf *dmabuf)
17314{
17315 struct fc_frame_header *new_hdr;
17316 struct fc_frame_header *temp_hdr;
17317 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
17318 struct hbq_dmabuf *seq_dmabuf = NULL;
17319
17320 /* Use the hdr_buf to find the sequence that matches this frame */
17321 INIT_LIST_HEAD(&dmabuf->dbuf.list);
17322 INIT_LIST_HEAD(&dmabuf->hbuf.list);
17323 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17324 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17325 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17326 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17327 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17328 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17329 continue;
17330 /* found a pending sequence that matches this frame */
17331 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17332 break;
17333 }
17334
17335 /* Free up all the frames from the partially assembled sequence */
17336 if (seq_dmabuf) {
17337 list_for_each_entry_safe(d_buf, n_buf,
17338 &seq_dmabuf->dbuf.list, list) {
17339 list_del_init(&d_buf->list);
17340 lpfc_in_buf_free(vport->phba, d_buf);
17341 }
17342 return true;
17343 }
17344 return false;
17345}
17346
17347/**
James Smart6dd9e312013-01-03 15:43:37 -050017348 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
17349 * @vport: pointer to a vitural port
17350 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17351 *
17352 * This function tries to abort from the assembed sequence from upper level
17353 * protocol, described by the information from basic abbort @dmabuf. It
17354 * checks to see whether such pending context exists at upper level protocol.
17355 * If so, it shall clean up the pending context.
17356 *
17357 * Return
17358 * true -- if there is matching pending context of the sequence cleaned
17359 * at ulp;
17360 * false -- if there is no matching pending context of the sequence present
17361 * at ulp.
17362 **/
17363static bool
17364lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17365{
17366 struct lpfc_hba *phba = vport->phba;
17367 int handled;
17368
17369 /* Accepting abort at ulp with SLI4 only */
17370 if (phba->sli_rev < LPFC_SLI_REV4)
17371 return false;
17372
17373 /* Register all caring upper level protocols to attend abort */
17374 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
17375 if (handled)
17376 return true;
17377
17378 return false;
17379}
17380
17381/**
James Smart546fc852011-03-11 16:06:29 -050017382 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040017383 * @phba: Pointer to HBA context object.
17384 * @cmd_iocbq: pointer to the command iocbq structure.
17385 * @rsp_iocbq: pointer to the response iocbq structure.
17386 *
James Smart546fc852011-03-11 16:06:29 -050017387 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040017388 * event. It properly releases the memory allocated to the sequence abort
17389 * accept iocb.
17390 **/
17391static void
James Smart546fc852011-03-11 16:06:29 -050017392lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040017393 struct lpfc_iocbq *cmd_iocbq,
17394 struct lpfc_iocbq *rsp_iocbq)
17395{
James Smart6dd9e312013-01-03 15:43:37 -050017396 struct lpfc_nodelist *ndlp;
17397
17398 if (cmd_iocbq) {
17399 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
17400 lpfc_nlp_put(ndlp);
17401 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017402 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050017403 }
James Smart6b5151f2012-01-18 16:24:06 -050017404
17405 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
17406 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
17407 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17408 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
17409 rsp_iocbq->iocb.ulpStatus,
17410 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040017411}
17412
17413/**
James Smart6d368e52011-05-24 11:44:12 -040017414 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
17415 * @phba: Pointer to HBA context object.
17416 * @xri: xri id in transaction.
17417 *
17418 * This function validates the xri maps to the known range of XRIs allocated an
17419 * used by the driver.
17420 **/
James Smart7851fe22011-07-22 18:36:52 -040017421uint16_t
James Smart6d368e52011-05-24 11:44:12 -040017422lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
17423 uint16_t xri)
17424{
James Smarta2fc4aef2014-09-03 12:57:55 -040017425 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040017426
17427 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
17428 if (xri == phba->sli4_hba.xri_ids[i])
17429 return i;
17430 }
17431 return NO_XRI;
17432}
17433
James Smart6d368e52011-05-24 11:44:12 -040017434/**
James Smart546fc852011-03-11 16:06:29 -050017435 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017436 * @phba: Pointer to HBA context object.
17437 * @fc_hdr: pointer to a FC frame header.
17438 *
James Smart546fc852011-03-11 16:06:29 -050017439 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017440 * event after aborting the sequence handling.
17441 **/
James Smart86c67372017-04-21 16:05:04 -070017442void
James Smart6dd9e312013-01-03 15:43:37 -050017443lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17444 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040017445{
James Smart6dd9e312013-01-03 15:43:37 -050017446 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040017447 struct lpfc_iocbq *ctiocb = NULL;
17448 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040017449 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050017450 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040017451 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050017452 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040017453
17454 if (!lpfc_is_link_up(phba))
17455 return;
17456
17457 sid = sli4_sid_from_fc_hdr(fc_hdr);
17458 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050017459 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040017460
James Smart6dd9e312013-01-03 15:43:37 -050017461 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040017462 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070017463 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050017464 if (!ndlp) {
17465 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17466 "1268 Failed to allocate ndlp for "
17467 "oxid:x%x SID:x%x\n", oxid, sid);
17468 return;
17469 }
James Smart6dd9e312013-01-03 15:43:37 -050017470 /* Put ndlp onto pport node list */
17471 lpfc_enqueue_node(vport, ndlp);
17472 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17473 /* re-setup ndlp without removing from node list */
17474 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17475 if (!ndlp) {
17476 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17477 "3275 Failed to active ndlp found "
17478 "for oxid:x%x SID:x%x\n", oxid, sid);
17479 return;
17480 }
James Smart6669f9b2009-10-02 15:16:45 -040017481 }
17482
James Smart546fc852011-03-11 16:06:29 -050017483 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040017484 ctiocb = lpfc_sli_get_iocbq(phba);
17485 if (!ctiocb)
17486 return;
17487
James Smart5ffc2662009-11-18 15:39:44 -050017488 /* Extract the F_CTL field from FC_HDR */
17489 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17490
James Smart6669f9b2009-10-02 15:16:45 -040017491 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040017492 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050017493 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040017494 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17495 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17496 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17497
17498 /* Fill in the rest of iocb fields */
17499 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17500 icmd->ulpBdeCount = 0;
17501 icmd->ulpLe = 1;
17502 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040017503 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050017504 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017505
James Smart6669f9b2009-10-02 15:16:45 -040017506 ctiocb->iocb_cmpl = NULL;
17507 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050017508 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040017509 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050017510 ctiocb->sli4_xritag = NO_XRI;
17511
James Smartee0f4fe2012-05-09 21:19:14 -040017512 if (fctl & FC_FC_EX_CTX)
17513 /* Exchange responder sent the abort so we
17514 * own the oxid.
17515 */
17516 xri = oxid;
17517 else
17518 xri = rxid;
17519 lxri = lpfc_sli4_xri_inrange(phba, xri);
17520 if (lxri != NO_XRI)
17521 lpfc_set_rrq_active(phba, ndlp, lxri,
17522 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050017523 /* For BA_ABTS from exchange responder, if the logical xri with
17524 * the oxid maps to the FCP XRI range, the port no longer has
17525 * that exchange context, send a BLS_RJT. Override the IOCB for
17526 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050017527 */
James Smart6dd9e312013-01-03 15:43:37 -050017528 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080017529 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050017530 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17531 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17532 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17533 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17534 }
17535
17536 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17537 * the driver no longer has that exchange, send a BLS_RJT. Override
17538 * the IOCB for a BA_RJT.
17539 */
17540 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050017541 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17542 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17543 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17544 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17545 }
James Smart6669f9b2009-10-02 15:16:45 -040017546
James Smart5ffc2662009-11-18 15:39:44 -050017547 if (fctl & FC_FC_EX_CTX) {
17548 /* ABTS sent by responder to CT exchange, construction
17549 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17550 * field and RX_ID from ABTS for RX_ID field.
17551 */
James Smart546fc852011-03-11 16:06:29 -050017552 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050017553 } else {
17554 /* ABTS sent by initiator to CT exchange, construction
17555 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050017556 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050017557 */
James Smart546fc852011-03-11 16:06:29 -050017558 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050017559 }
James Smartf09c3ac2012-03-01 22:33:29 -050017560 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050017561 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050017562
James Smart546fc852011-03-11 16:06:29 -050017563 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050017564 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17565 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17566 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050017567
17568 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17569 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050017570 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17571 "2925 Failed to issue CT ABTS RSP x%x on "
17572 "xri x%x, Data x%x\n",
17573 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17574 phba->link_state);
17575 lpfc_nlp_put(ndlp);
17576 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050017577 lpfc_sli_release_iocbq(phba, ctiocb);
17578 }
James Smart6669f9b2009-10-02 15:16:45 -040017579}
17580
17581/**
17582 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17583 * @vport: Pointer to the vport on which this sequence was received
17584 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17585 *
17586 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17587 * receive sequence is only partially assembed by the driver, it shall abort
17588 * the partially assembled frames for the sequence. Otherwise, if the
17589 * unsolicited receive sequence has been completely assembled and passed to
17590 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17591 * unsolicited sequence has been aborted. After that, it will issue a basic
17592 * accept to accept the abort.
17593 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017594static void
James Smart6669f9b2009-10-02 15:16:45 -040017595lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17596 struct hbq_dmabuf *dmabuf)
17597{
17598 struct lpfc_hba *phba = vport->phba;
17599 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050017600 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050017601 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040017602
James Smart6669f9b2009-10-02 15:16:45 -040017603 /* Make a copy of fc_hdr before the dmabuf being released */
17604 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050017605 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040017606
James Smart5ffc2662009-11-18 15:39:44 -050017607 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050017608 /* ABTS by responder to exchange, no cleanup needed */
17609 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050017610 } else {
James Smart6dd9e312013-01-03 15:43:37 -050017611 /* ABTS by initiator to exchange, need to do cleanup */
17612 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17613 if (aborted == false)
17614 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050017615 }
James Smart6dd9e312013-01-03 15:43:37 -050017616 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17617
James Smart86c67372017-04-21 16:05:04 -070017618 if (phba->nvmet_support) {
17619 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17620 return;
17621 }
17622
James Smart6dd9e312013-01-03 15:43:37 -050017623 /* Respond with BA_ACC or BA_RJT accordingly */
17624 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040017625}
17626
17627/**
James Smart4f774512009-05-22 14:52:35 -040017628 * lpfc_seq_complete - Indicates if a sequence is complete
17629 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17630 *
17631 * This function checks the sequence, starting with the frame described by
17632 * @dmabuf, to see if all the frames associated with this sequence are present.
17633 * the frames associated with this sequence are linked to the @dmabuf using the
17634 * dbuf list. This function looks for two major things. 1) That the first frame
17635 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17636 * set. 3) That there are no holes in the sequence count. The function will
17637 * return 1 when the sequence is complete, otherwise it will return 0.
17638 **/
17639static int
17640lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17641{
17642 struct fc_frame_header *hdr;
17643 struct lpfc_dmabuf *d_buf;
17644 struct hbq_dmabuf *seq_dmabuf;
17645 uint32_t fctl;
17646 int seq_count = 0;
17647
17648 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17649 /* make sure first fame of sequence has a sequence count of zero */
17650 if (hdr->fh_seq_cnt != seq_count)
17651 return 0;
17652 fctl = (hdr->fh_f_ctl[0] << 16 |
17653 hdr->fh_f_ctl[1] << 8 |
17654 hdr->fh_f_ctl[2]);
17655 /* If last frame of sequence we can return success. */
17656 if (fctl & FC_FC_END_SEQ)
17657 return 1;
17658 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17659 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17660 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17661 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050017662 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040017663 return 0;
17664 fctl = (hdr->fh_f_ctl[0] << 16 |
17665 hdr->fh_f_ctl[1] << 8 |
17666 hdr->fh_f_ctl[2]);
17667 /* If last frame of sequence we can return success. */
17668 if (fctl & FC_FC_END_SEQ)
17669 return 1;
17670 }
17671 return 0;
17672}
17673
17674/**
17675 * lpfc_prep_seq - Prep sequence for ULP processing
17676 * @vport: Pointer to the vport on which this sequence was received
17677 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17678 *
17679 * This function takes a sequence, described by a list of frames, and creates
17680 * a list of iocbq structures to describe the sequence. This iocbq list will be
17681 * used to issue to the generic unsolicited sequence handler. This routine
17682 * returns a pointer to the first iocbq in the list. If the function is unable
17683 * to allocate an iocbq then it throw out the received frames that were not
17684 * able to be described and return a pointer to the first iocbq. If unable to
17685 * allocate any iocbqs (including the first) this function will return NULL.
17686 **/
17687static struct lpfc_iocbq *
17688lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17689{
James Smart7851fe22011-07-22 18:36:52 -040017690 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040017691 struct lpfc_dmabuf *d_buf, *n_buf;
17692 struct lpfc_iocbq *first_iocbq, *iocbq;
17693 struct fc_frame_header *fc_hdr;
17694 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040017695 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050017696 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040017697
17698 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17699 /* remove from receive buffer list */
17700 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017701 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017702 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040017703 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040017704 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017705 /* Get an iocbq struct to fill in. */
17706 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17707 if (first_iocbq) {
17708 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040017709 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017710 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080017711 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040017712
17713 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17714 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17715 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17716 first_iocbq->iocb.un.rcvels.parmRo =
17717 sli4_did_from_fc_hdr(fc_hdr);
17718 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17719 } else
17720 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040017721 first_iocbq->iocb.ulpContext = NO_XRI;
17722 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17723 be16_to_cpu(fc_hdr->fh_ox_id);
17724 /* iocbq is prepped for internal consumption. Physical vpi. */
17725 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17726 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040017727 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040017728 tot_len = bf_get(lpfc_rcqe_length,
17729 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17730
James Smart4f774512009-05-22 14:52:35 -040017731 first_iocbq->context2 = &seq_dmabuf->dbuf;
17732 first_iocbq->context3 = NULL;
17733 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040017734 if (tot_len > LPFC_DATA_BUF_SIZE)
17735 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040017736 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040017737 else
17738 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17739
James Smart4f774512009-05-22 14:52:35 -040017740 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040017741
James Smart7851fe22011-07-22 18:36:52 -040017742 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040017743 }
17744 iocbq = first_iocbq;
17745 /*
17746 * Each IOCBq can have two Buffers assigned, so go through the list
17747 * of buffers for this sequence and save two buffers in each IOCBq
17748 */
17749 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17750 if (!iocbq) {
17751 lpfc_in_buf_free(vport->phba, d_buf);
17752 continue;
17753 }
17754 if (!iocbq->context3) {
17755 iocbq->context3 = d_buf;
17756 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040017757 /* We need to get the size out of the right CQE */
17758 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17759 len = bf_get(lpfc_rcqe_length,
17760 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017761 pbde = (struct ulp_bde64 *)
17762 &iocbq->iocb.unsli3.sli3Words[4];
17763 if (len > LPFC_DATA_BUF_SIZE)
17764 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17765 else
17766 pbde->tus.f.bdeSize = len;
17767
James Smart7851fe22011-07-22 18:36:52 -040017768 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17769 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040017770 } else {
17771 iocbq = lpfc_sli_get_iocbq(vport->phba);
17772 if (!iocbq) {
17773 if (first_iocbq) {
17774 first_iocbq->iocb.ulpStatus =
17775 IOSTAT_FCP_RSP_ERROR;
17776 first_iocbq->iocb.un.ulpWord[4] =
17777 IOERR_NO_RESOURCES;
17778 }
17779 lpfc_in_buf_free(vport->phba, d_buf);
17780 continue;
17781 }
James Smart7851fe22011-07-22 18:36:52 -040017782 /* We need to get the size out of the right CQE */
17783 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17784 len = bf_get(lpfc_rcqe_length,
17785 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017786 iocbq->context2 = d_buf;
17787 iocbq->context3 = NULL;
17788 iocbq->iocb.ulpBdeCount = 1;
17789 if (len > LPFC_DATA_BUF_SIZE)
17790 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17791 LPFC_DATA_BUF_SIZE;
17792 else
17793 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17794
James Smart7851fe22011-07-22 18:36:52 -040017795 tot_len += len;
17796 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17797
James Smart4f774512009-05-22 14:52:35 -040017798 iocbq->iocb.un.rcvels.remoteID = sid;
17799 list_add_tail(&iocbq->list, &first_iocbq->list);
17800 }
17801 }
17802 return first_iocbq;
17803}
17804
James Smart6669f9b2009-10-02 15:16:45 -040017805static void
17806lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17807 struct hbq_dmabuf *seq_dmabuf)
17808{
17809 struct fc_frame_header *fc_hdr;
17810 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17811 struct lpfc_hba *phba = vport->phba;
17812
17813 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17814 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17815 if (!iocbq) {
17816 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17817 "2707 Ring %d handler: Failed to allocate "
17818 "iocb Rctl x%x Type x%x received\n",
17819 LPFC_ELS_RING,
17820 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17821 return;
17822 }
17823 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017824 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017825 iocbq, fc_hdr->fh_r_ctl,
17826 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017827 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017828 "2540 Ring %d handler: unexpected Rctl "
17829 "x%x Type x%x received\n",
17830 LPFC_ELS_RING,
17831 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17832
17833 /* Free iocb created in lpfc_prep_seq */
17834 list_for_each_entry_safe(curr_iocb, next_iocb,
17835 &iocbq->list, list) {
17836 list_del_init(&curr_iocb->list);
17837 lpfc_sli_release_iocbq(phba, curr_iocb);
17838 }
17839 lpfc_sli_release_iocbq(phba, iocbq);
17840}
17841
James Smartae9e28f2017-05-15 15:20:51 -070017842static void
17843lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17844 struct lpfc_iocbq *rspiocb)
17845{
17846 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17847
17848 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017849 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017850 kfree(pcmd);
17851 lpfc_sli_release_iocbq(phba, cmdiocb);
James Smarte817e5d2018-12-13 15:17:53 -080017852 lpfc_drain_txq(phba);
James Smartae9e28f2017-05-15 15:20:51 -070017853}
17854
17855static void
17856lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17857 struct hbq_dmabuf *dmabuf)
17858{
17859 struct fc_frame_header *fc_hdr;
17860 struct lpfc_hba *phba = vport->phba;
17861 struct lpfc_iocbq *iocbq = NULL;
17862 union lpfc_wqe *wqe;
17863 struct lpfc_dmabuf *pcmd = NULL;
17864 uint32_t frame_len;
17865 int rc;
James Smarte817e5d2018-12-13 15:17:53 -080017866 unsigned long iflags;
James Smartae9e28f2017-05-15 15:20:51 -070017867
17868 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17869 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17870
17871 /* Send the received frame back */
17872 iocbq = lpfc_sli_get_iocbq(phba);
James Smarte817e5d2018-12-13 15:17:53 -080017873 if (!iocbq) {
17874 /* Queue cq event and wakeup worker thread to process it */
17875 spin_lock_irqsave(&phba->hbalock, iflags);
17876 list_add_tail(&dmabuf->cq_event.list,
17877 &phba->sli4_hba.sp_queue_event);
17878 phba->hba_flag |= HBA_SP_QUEUE_EVT;
17879 spin_unlock_irqrestore(&phba->hbalock, iflags);
17880 lpfc_worker_wake_up(phba);
17881 return;
17882 }
James Smartae9e28f2017-05-15 15:20:51 -070017883
17884 /* Allocate buffer for command payload */
17885 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17886 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020017887 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070017888 &pcmd->phys);
17889 if (!pcmd || !pcmd->virt)
17890 goto exit;
17891
17892 INIT_LIST_HEAD(&pcmd->list);
17893
17894 /* copyin the payload */
17895 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17896
17897 /* fill in BDE's for command */
17898 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17899 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17900 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17901 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17902
17903 iocbq->context2 = pcmd;
17904 iocbq->vport = vport;
17905 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17906 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17907
17908 /*
17909 * Setup rest of the iocb as though it were a WQE
17910 * Build the SEND_FRAME WQE
17911 */
17912 wqe = (union lpfc_wqe *)&iocbq->iocb;
17913
17914 wqe->send_frame.frame_len = frame_len;
17915 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17916 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17917 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17918 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17919 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17920 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17921
17922 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17923 iocbq->iocb.ulpLe = 1;
17924 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17925 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17926 if (rc == IOCB_ERROR)
17927 goto exit;
17928
17929 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17930 return;
17931
17932exit:
17933 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17934 "2023 Unable to process MDS loopback frame\n");
17935 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017936 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017937 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070017938 if (iocbq)
17939 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070017940 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17941}
17942
James Smart4f774512009-05-22 14:52:35 -040017943/**
17944 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17945 * @phba: Pointer to HBA context object.
17946 *
17947 * This function is called with no lock held. This function processes all
17948 * the received buffers and gives it to upper layers when a received buffer
17949 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017950 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017951 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17952 * appropriate receive function when the final frame in a sequence is received.
17953 **/
James Smart4d9ab992009-10-02 15:16:39 -040017954void
17955lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17956 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017957{
James Smart4d9ab992009-10-02 15:16:39 -040017958 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017959 struct fc_frame_header *fc_hdr;
17960 struct lpfc_vport *vport;
17961 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017962 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017963
James Smart4f774512009-05-22 14:52:35 -040017964 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017965 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017966
James Smarte817e5d2018-12-13 15:17:53 -080017967 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
17968 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
17969 vport = phba->pport;
17970 /* Handle MDS Loopback frames */
17971 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17972 return;
17973 }
17974
James Smart4d9ab992009-10-02 15:16:39 -040017975 /* check to see if this a valid type of frame */
17976 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17977 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17978 return;
17979 }
James Smart2ea259e2017-02-12 13:52:27 -080017980
James Smart7851fe22011-07-22 18:36:52 -040017981 if ((bf_get(lpfc_cqe_code,
17982 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17983 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17984 &dmabuf->cq_event.cqe.rcqe_cmpl);
17985 else
17986 fcfi = bf_get(lpfc_rcqe_fcf_id,
17987 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017988
James Smart895427b2017-02-12 13:52:30 -080017989 /* d_id this frame is directed to */
17990 did = sli4_did_from_fc_hdr(fc_hdr);
17991
17992 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017993 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017994 /* throw out the frame */
17995 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17996 return;
17997 }
James Smart939723a2012-05-09 21:19:03 -040017998
James Smart939723a2012-05-09 21:19:03 -040017999 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
18000 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
18001 (did != Fabric_DID)) {
18002 /*
18003 * Throw out the frame if we are not pt2pt.
18004 * The pt2pt protocol allows for discovery frames
18005 * to be received without a registered VPI.
18006 */
18007 if (!(vport->fc_flag & FC_PT2PT) ||
18008 (phba->link_state == LPFC_HBA_READY)) {
18009 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18010 return;
18011 }
18012 }
18013
James Smart6669f9b2009-10-02 15:16:45 -040018014 /* Handle the basic abort sequence (BA_ABTS) event */
18015 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
18016 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
18017 return;
18018 }
18019
James Smart4d9ab992009-10-02 15:16:39 -040018020 /* Link this frame */
18021 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
18022 if (!seq_dmabuf) {
18023 /* unable to add frame to vport - throw it out */
18024 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18025 return;
18026 }
18027 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050018028 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040018029 return;
James Smartdef9c7a2009-12-21 17:02:28 -050018030
James Smart6669f9b2009-10-02 15:16:45 -040018031 /* Send the complete sequence to the upper layer protocol */
18032 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040018033}
James Smart6fb120a2009-05-22 14:52:59 -040018034
18035/**
18036 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
18037 * @phba: pointer to lpfc hba data structure.
18038 *
18039 * This routine is invoked to post rpi header templates to the
18040 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040018041 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18042 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040018043 *
18044 * This routine does not require any locks. It's usage is expected
18045 * to be driver load or reset recovery when the driver is
18046 * sequential.
18047 *
18048 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018049 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040018050 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040018051 * When this error occurs, the driver is not guaranteed
18052 * to have any rpi regions posted to the device and
18053 * must either attempt to repost the regions or take a
18054 * fatal error.
18055 **/
18056int
18057lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
18058{
18059 struct lpfc_rpi_hdr *rpi_page;
18060 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040018061 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018062
James Smart6d368e52011-05-24 11:44:12 -040018063 /* SLI4 ports that support extents do not require RPI headers. */
18064 if (!phba->sli4_hba.rpi_hdrs_in_use)
18065 goto exit;
18066 if (phba->sli4_hba.extents_in_use)
18067 return -EIO;
18068
James Smart6fb120a2009-05-22 14:52:59 -040018069 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040018070 /*
18071 * Assign the rpi headers a physical rpi only if the driver
18072 * has not initialized those resources. A port reset only
18073 * needs the headers posted.
18074 */
18075 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
18076 LPFC_RPI_RSRC_RDY)
18077 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
18078
James Smart6fb120a2009-05-22 14:52:59 -040018079 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
18080 if (rc != MBX_SUCCESS) {
18081 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18082 "2008 Error %d posting all rpi "
18083 "headers\n", rc);
18084 rc = -EIO;
18085 break;
18086 }
18087 }
18088
James Smart6d368e52011-05-24 11:44:12 -040018089 exit:
18090 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
18091 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040018092 return rc;
18093}
18094
18095/**
18096 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
18097 * @phba: pointer to lpfc hba data structure.
18098 * @rpi_page: pointer to the rpi memory region.
18099 *
18100 * This routine is invoked to post a single rpi header to the
18101 * HBA consistent with the SLI-4 interface spec. This memory region
18102 * maps up to 64 rpi context regions.
18103 *
18104 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018105 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040018106 * -ENOMEM - No available memory
18107 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040018108 **/
18109int
18110lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
18111{
18112 LPFC_MBOXQ_t *mboxq;
18113 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
18114 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018115 uint32_t shdr_status, shdr_add_status;
18116 union lpfc_sli4_cfg_shdr *shdr;
18117
James Smart6d368e52011-05-24 11:44:12 -040018118 /* SLI4 ports that support extents do not require RPI headers. */
18119 if (!phba->sli4_hba.rpi_hdrs_in_use)
18120 return rc;
18121 if (phba->sli4_hba.extents_in_use)
18122 return -EIO;
18123
James Smart6fb120a2009-05-22 14:52:59 -040018124 /* The port is notified of the header region via a mailbox command. */
18125 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18126 if (!mboxq) {
18127 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18128 "2001 Unable to allocate memory for issuing "
18129 "SLI_CONFIG_SPECIAL mailbox command\n");
18130 return -ENOMEM;
18131 }
18132
18133 /* Post all rpi memory regions to the port. */
18134 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040018135 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18136 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
18137 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050018138 sizeof(struct lpfc_sli4_cfg_mhdr),
18139 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040018140
18141
18142 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040018143 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
18144 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040018145 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
18146 hdr_tmpl, rpi_page->page_count);
18147
James Smart6fb120a2009-05-22 14:52:59 -040018148 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
18149 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040018150 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040018151 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
18152 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18153 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18154 if (rc != MBX_TIMEOUT)
18155 mempool_free(mboxq, phba->mbox_mem_pool);
18156 if (shdr_status || shdr_add_status || rc) {
18157 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18158 "2514 POST_RPI_HDR mailbox failed with "
18159 "status x%x add_status x%x, mbx status x%x\n",
18160 shdr_status, shdr_add_status, rc);
18161 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070018162 } else {
18163 /*
18164 * The next_rpi stores the next logical module-64 rpi value used
18165 * to post physical rpis in subsequent rpi postings.
18166 */
18167 spin_lock_irq(&phba->hbalock);
18168 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
18169 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040018170 }
18171 return rc;
18172}
18173
18174/**
18175 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
18176 * @phba: pointer to lpfc hba data structure.
18177 *
18178 * This routine is invoked to post rpi header templates to the
18179 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040018180 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18181 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040018182 *
18183 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018184 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040018185 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
18186 **/
18187int
18188lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
18189{
James Smart6d368e52011-05-24 11:44:12 -040018190 unsigned long rpi;
18191 uint16_t max_rpi, rpi_limit;
18192 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018193 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040018194 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040018195
James Smart6fb120a2009-05-22 14:52:59 -040018196 /*
James Smart6d368e52011-05-24 11:44:12 -040018197 * Fetch the next logical rpi. Because this index is logical,
18198 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040018199 */
James Smart4902b382013-10-10 12:20:35 -040018200 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040018201 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
18202 rpi_limit = phba->sli4_hba.next_rpi;
18203
James Smart6d368e52011-05-24 11:44:12 -040018204 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
18205 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040018206 rpi = LPFC_RPI_ALLOC_ERROR;
18207 else {
18208 set_bit(rpi, phba->sli4_hba.rpi_bmask);
18209 phba->sli4_hba.max_cfg_param.rpi_used++;
18210 phba->sli4_hba.rpi_count++;
18211 }
James Smartbe6bb942015-04-07 15:07:22 -040018212 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
18213 "0001 rpi:%x max:%x lim:%x\n",
18214 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040018215
18216 /*
18217 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040018218 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040018219 */
18220 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
18221 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040018222 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018223 return rpi;
18224 }
18225
18226 /*
James Smart6d368e52011-05-24 11:44:12 -040018227 * RPI header postings are not required for SLI4 ports capable of
18228 * extents.
18229 */
18230 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040018231 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040018232 return rpi;
18233 }
18234
18235 /*
James Smart6fb120a2009-05-22 14:52:59 -040018236 * If the driver is running low on rpi resources, allocate another
18237 * page now. Note that the next_rpi value is used because
18238 * it represents how many are actually in use whereas max_rpi notes
18239 * how many are supported max by the device.
18240 */
James Smart6d368e52011-05-24 11:44:12 -040018241 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040018242 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018243 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
18244 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
18245 if (!rpi_hdr) {
18246 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18247 "2002 Error Could not grow rpi "
18248 "count\n");
18249 } else {
James Smart6d368e52011-05-24 11:44:12 -040018250 lrpi = rpi_hdr->start_rpi;
18251 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040018252 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
18253 }
18254 }
18255
18256 return rpi;
18257}
18258
18259/**
18260 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18261 * @phba: pointer to lpfc hba data structure.
18262 *
18263 * This routine is invoked to release an rpi to the pool of
18264 * available rpis maintained by the driver.
18265 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018266static void
James Smartd7c47992010-06-08 18:31:54 -040018267__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18268{
18269 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
18270 phba->sli4_hba.rpi_count--;
18271 phba->sli4_hba.max_cfg_param.rpi_used--;
18272 }
18273}
18274
18275/**
18276 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18277 * @phba: pointer to lpfc hba data structure.
18278 *
18279 * This routine is invoked to release an rpi to the pool of
18280 * available rpis maintained by the driver.
18281 **/
18282void
James Smart6fb120a2009-05-22 14:52:59 -040018283lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18284{
18285 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040018286 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040018287 spin_unlock_irq(&phba->hbalock);
18288}
18289
18290/**
18291 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
18292 * @phba: pointer to lpfc hba data structure.
18293 *
18294 * This routine is invoked to remove the memory region that
18295 * provided rpi via a bitmask.
18296 **/
18297void
18298lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
18299{
18300 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040018301 kfree(phba->sli4_hba.rpi_ids);
18302 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040018303}
18304
18305/**
18306 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
18307 * @phba: pointer to lpfc hba data structure.
18308 *
18309 * This routine is invoked to remove the memory region that
18310 * provided rpi via a bitmask.
18311 **/
18312int
James Smart6b5151f2012-01-18 16:24:06 -050018313lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
18314 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040018315{
18316 LPFC_MBOXQ_t *mboxq;
18317 struct lpfc_hba *phba = ndlp->phba;
18318 int rc;
18319
18320 /* The port is notified of the header region via a mailbox command. */
18321 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18322 if (!mboxq)
18323 return -ENOMEM;
18324
18325 /* Post all rpi memory regions to the port. */
18326 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050018327 if (cmpl) {
18328 mboxq->mbox_cmpl = cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080018329 mboxq->ctx_buf = arg;
18330 mboxq->ctx_ndlp = ndlp;
James Smart72859902012-01-18 16:25:38 -050018331 } else
18332 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050018333 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040018334 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18335 if (rc == MBX_NOT_FINISHED) {
18336 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18337 "2010 Resume RPI Mailbox failed "
18338 "status %d, mbxStatus x%x\n", rc,
18339 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
18340 mempool_free(mboxq, phba->mbox_mem_pool);
18341 return -EIO;
18342 }
18343 return 0;
18344}
18345
18346/**
18347 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050018348 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040018349 *
James Smart76a95d72010-11-20 23:11:48 -050018350 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040018351 *
18352 * Returns:
18353 * 0 success
18354 * -Evalue otherwise
18355 **/
18356int
James Smart76a95d72010-11-20 23:11:48 -050018357lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040018358{
18359 LPFC_MBOXQ_t *mboxq;
18360 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040018361 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040018362 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050018363 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040018364 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18365 if (!mboxq)
18366 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050018367 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040018368 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040018369 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040018370 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050018371 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040018372 "2022 INIT VPI Mailbox failed "
18373 "status %d, mbxStatus x%x\n", rc,
18374 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040018375 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040018376 }
James Smart6a9c52c2009-10-02 15:16:51 -040018377 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050018378 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040018379
18380 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040018381}
18382
18383/**
18384 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
18385 * @phba: pointer to lpfc hba data structure.
18386 * @mboxq: Pointer to mailbox object.
18387 *
18388 * This routine is invoked to manually add a single FCF record. The caller
18389 * must pass a completely initialized FCF_Record. This routine takes
18390 * care of the nonembedded mailbox operations.
18391 **/
18392static void
18393lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
18394{
18395 void *virt_addr;
18396 union lpfc_sli4_cfg_shdr *shdr;
18397 uint32_t shdr_status, shdr_add_status;
18398
18399 virt_addr = mboxq->sge_array->addr[0];
18400 /* The IOCTL status is embedded in the mailbox subheader. */
18401 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
18402 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18403 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18404
18405 if ((shdr_status || shdr_add_status) &&
18406 (shdr_status != STATUS_FCF_IN_USE))
18407 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18408 "2558 ADD_FCF_RECORD mailbox failed with "
18409 "status x%x add_status x%x\n",
18410 shdr_status, shdr_add_status);
18411
18412 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18413}
18414
18415/**
18416 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
18417 * @phba: pointer to lpfc hba data structure.
18418 * @fcf_record: pointer to the initialized fcf record to add.
18419 *
18420 * This routine is invoked to manually add a single FCF record. The caller
18421 * must pass a completely initialized FCF_Record. This routine takes
18422 * care of the nonembedded mailbox operations.
18423 **/
18424int
18425lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
18426{
18427 int rc = 0;
18428 LPFC_MBOXQ_t *mboxq;
18429 uint8_t *bytep;
18430 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040018431 struct lpfc_mbx_sge sge;
18432 uint32_t alloc_len, req_len;
18433 uint32_t fcfindex;
18434
18435 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18436 if (!mboxq) {
18437 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18438 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18439 return -ENOMEM;
18440 }
18441
18442 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18443 sizeof(uint32_t);
18444
18445 /* Allocate DMA memory and set up the non-embedded mailbox command */
18446 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18447 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18448 req_len, LPFC_SLI4_MBX_NEMBED);
18449 if (alloc_len < req_len) {
18450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18451 "2523 Allocated DMA memory size (x%x) is "
18452 "less than the requested DMA memory "
18453 "size (x%x)\n", alloc_len, req_len);
18454 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18455 return -ENOMEM;
18456 }
18457
18458 /*
18459 * Get the first SGE entry from the non-embedded DMA memory. This
18460 * routine only uses a single SGE.
18461 */
18462 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040018463 virt_addr = mboxq->sge_array->addr[0];
18464 /*
18465 * Configure the FCF record for FCFI 0. This is the driver's
18466 * hardcoded default and gets used in nonFIP mode.
18467 */
18468 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18469 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18470 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18471
18472 /*
18473 * Copy the fcf_index and the FCF Record Data. The data starts after
18474 * the FCoE header plus word10. The data copy needs to be endian
18475 * correct.
18476 */
18477 bytep += sizeof(uint32_t);
18478 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18479 mboxq->vport = phba->pport;
18480 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18481 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18482 if (rc == MBX_NOT_FINISHED) {
18483 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18484 "2515 ADD_FCF_RECORD mailbox failed with "
18485 "status 0x%x\n", rc);
18486 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18487 rc = -EIO;
18488 } else
18489 rc = 0;
18490
18491 return rc;
18492}
18493
18494/**
18495 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18496 * @phba: pointer to lpfc hba data structure.
18497 * @fcf_record: pointer to the fcf record to write the default data.
18498 * @fcf_index: FCF table entry index.
18499 *
18500 * This routine is invoked to build the driver's default FCF record. The
18501 * values used are hardcoded. This routine handles memory initialization.
18502 *
18503 **/
18504void
18505lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18506 struct fcf_record *fcf_record,
18507 uint16_t fcf_index)
18508{
18509 memset(fcf_record, 0, sizeof(struct fcf_record));
18510 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18511 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18512 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18513 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18514 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18515 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18516 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18517 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18518 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18519 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18520 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18521 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18522 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040018523 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040018524 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18525 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18526 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18527 /* Set the VLAN bit map */
18528 if (phba->valid_vlan) {
18529 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18530 = 1 << (phba->vlan_id % 8);
18531 }
18532}
18533
18534/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018535 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040018536 * @phba: pointer to lpfc hba data structure.
18537 * @fcf_index: FCF table entry offset.
18538 *
James Smart0c9ab6f2010-02-26 14:15:57 -050018539 * This routine is invoked to scan the entire FCF table by reading FCF
18540 * record and processing it one at a time starting from the @fcf_index
18541 * for initial FCF discovery or fast FCF failover rediscovery.
18542 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018543 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018544 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040018545 **/
18546int
James Smart0c9ab6f2010-02-26 14:15:57 -050018547lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040018548{
18549 int rc = 0, error;
18550 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040018551
James Smart32b97932009-07-19 10:01:21 -040018552 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050018553 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040018554 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18555 if (!mboxq) {
18556 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18557 "2000 Failed to allocate mbox for "
18558 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040018559 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050018560 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018561 }
James Smartecfd03c2010-02-12 14:41:27 -050018562 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050018563 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050018564 if (rc) {
18565 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050018566 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018567 }
James Smartecfd03c2010-02-12 14:41:27 -050018568 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040018569 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050018570 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040018571
18572 spin_lock_irq(&phba->hbalock);
18573 phba->hba_flag |= FCF_TS_INPROG;
18574 spin_unlock_irq(&phba->hbalock);
18575
James Smart6fb120a2009-05-22 14:52:59 -040018576 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050018577 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040018578 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050018579 else {
James Smart38b92ef2010-08-04 16:11:39 -040018580 /* Reset eligible FCF count for new scan */
18581 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040018582 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018583 error = 0;
James Smart32b97932009-07-19 10:01:21 -040018584 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018585fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040018586 if (error) {
18587 if (mboxq)
18588 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040018589 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040018590 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040018591 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040018592 spin_unlock_irq(&phba->hbalock);
18593 }
James Smart6fb120a2009-05-22 14:52:59 -040018594 return error;
18595}
James Smarta0c87cb2009-07-19 10:01:10 -040018596
18597/**
James Smarta93ff372010-10-22 11:06:08 -040018598 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050018599 * @phba: pointer to lpfc hba data structure.
18600 * @fcf_index: FCF table entry offset.
18601 *
18602 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040018603 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050018604 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018605 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018606 * otherwise.
18607 **/
18608int
18609lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18610{
18611 int rc = 0, error;
18612 LPFC_MBOXQ_t *mboxq;
18613
18614 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18615 if (!mboxq) {
18616 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18617 "2763 Failed to allocate mbox for "
18618 "READ_FCF cmd\n");
18619 error = -ENOMEM;
18620 goto fail_fcf_read;
18621 }
18622 /* Construct the read FCF record mailbox command */
18623 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18624 if (rc) {
18625 error = -EINVAL;
18626 goto fail_fcf_read;
18627 }
18628 /* Issue the mailbox command asynchronously */
18629 mboxq->vport = phba->pport;
18630 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18631 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18632 if (rc == MBX_NOT_FINISHED)
18633 error = -EIO;
18634 else
18635 error = 0;
18636
18637fail_fcf_read:
18638 if (error && mboxq)
18639 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18640 return error;
18641}
18642
18643/**
18644 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18645 * @phba: pointer to lpfc hba data structure.
18646 * @fcf_index: FCF table entry offset.
18647 *
18648 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040018649 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050018650 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018651 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018652 * otherwise.
18653 **/
18654int
18655lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18656{
18657 int rc = 0, error;
18658 LPFC_MBOXQ_t *mboxq;
18659
18660 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18661 if (!mboxq) {
18662 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18663 "2758 Failed to allocate mbox for "
18664 "READ_FCF cmd\n");
18665 error = -ENOMEM;
18666 goto fail_fcf_read;
18667 }
18668 /* Construct the read FCF record mailbox command */
18669 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18670 if (rc) {
18671 error = -EINVAL;
18672 goto fail_fcf_read;
18673 }
18674 /* Issue the mailbox command asynchronously */
18675 mboxq->vport = phba->pport;
18676 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18677 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18678 if (rc == MBX_NOT_FINISHED)
18679 error = -EIO;
18680 else
18681 error = 0;
18682
18683fail_fcf_read:
18684 if (error && mboxq)
18685 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18686 return error;
18687}
18688
18689/**
James Smartf5cb5302015-12-16 18:11:52 -050018690 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040018691 * phba pointer to the lpfc_hba struct for this port.
18692 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18693 * routine when the rr_bmask is empty. The FCF indecies are put into the
18694 * rr_bmask based on their priority level. Starting from the highest priority
18695 * to the lowest. The most likely FCF candidate will be in the highest
18696 * priority group. When this routine is called it searches the fcf_pri list for
18697 * next lowest priority group and repopulates the rr_bmask with only those
18698 * fcf_indexes.
18699 * returns:
18700 * 1=success 0=failure
18701 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018702static int
James Smart7d791df2011-07-22 18:37:52 -040018703lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18704{
18705 uint16_t next_fcf_pri;
18706 uint16_t last_index;
18707 struct lpfc_fcf_pri *fcf_pri;
18708 int rc;
18709 int ret = 0;
18710
18711 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18712 LPFC_SLI4_FCF_TBL_INDX_MAX);
18713 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18714 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050018715
18716 /* Verify the priority list has 2 or more entries */
18717 spin_lock_irq(&phba->hbalock);
18718 if (list_empty(&phba->fcf.fcf_pri_list) ||
18719 list_is_singular(&phba->fcf.fcf_pri_list)) {
18720 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040018721 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18722 "3061 Last IDX %d\n", last_index);
18723 return 0; /* Empty rr list */
18724 }
James Smart25626692013-03-01 16:36:54 -050018725 spin_unlock_irq(&phba->hbalock);
18726
James Smart7d791df2011-07-22 18:37:52 -040018727 next_fcf_pri = 0;
18728 /*
18729 * Clear the rr_bmask and set all of the bits that are at this
18730 * priority.
18731 */
18732 memset(phba->fcf.fcf_rr_bmask, 0,
18733 sizeof(*phba->fcf.fcf_rr_bmask));
18734 spin_lock_irq(&phba->hbalock);
18735 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18736 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18737 continue;
18738 /*
18739 * the 1st priority that has not FLOGI failed
18740 * will be the highest.
18741 */
18742 if (!next_fcf_pri)
18743 next_fcf_pri = fcf_pri->fcf_rec.priority;
18744 spin_unlock_irq(&phba->hbalock);
18745 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18746 rc = lpfc_sli4_fcf_rr_index_set(phba,
18747 fcf_pri->fcf_rec.fcf_index);
18748 if (rc)
18749 return 0;
18750 }
18751 spin_lock_irq(&phba->hbalock);
18752 }
18753 /*
18754 * if next_fcf_pri was not set above and the list is not empty then
18755 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070018756 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040018757 */
18758 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18759 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18760 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18761 /*
18762 * the 1st priority that has not FLOGI failed
18763 * will be the highest.
18764 */
18765 if (!next_fcf_pri)
18766 next_fcf_pri = fcf_pri->fcf_rec.priority;
18767 spin_unlock_irq(&phba->hbalock);
18768 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18769 rc = lpfc_sli4_fcf_rr_index_set(phba,
18770 fcf_pri->fcf_rec.fcf_index);
18771 if (rc)
18772 return 0;
18773 }
18774 spin_lock_irq(&phba->hbalock);
18775 }
18776 } else
18777 ret = 1;
18778 spin_unlock_irq(&phba->hbalock);
18779
18780 return ret;
18781}
18782/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018783 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18784 * @phba: pointer to lpfc hba data structure.
18785 *
18786 * This routine is to get the next eligible FCF record index in a round
18787 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040018788 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050018789 * shall be returned, otherwise, the next eligible FCF record's index
18790 * shall be returned.
18791 **/
18792uint16_t
18793lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18794{
18795 uint16_t next_fcf_index;
18796
James Smart421c6622013-01-03 15:44:16 -050018797initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040018798 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050018799 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18800
James Smart7d791df2011-07-22 18:37:52 -040018801next_priority:
James Smart421c6622013-01-03 15:44:16 -050018802 /* Determine the next fcf index to check */
18803 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050018804 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18805 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040018806 next_fcf_index);
18807
James Smart0c9ab6f2010-02-26 14:15:57 -050018808 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018809 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18810 /*
18811 * If we have wrapped then we need to clear the bits that
18812 * have been tested so that we can detect when we should
18813 * change the priority level.
18814 */
James Smart0c9ab6f2010-02-26 14:15:57 -050018815 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18816 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040018817 }
18818
James Smart0c9ab6f2010-02-26 14:15:57 -050018819
James Smart3804dc82010-07-14 15:31:37 -040018820 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018821 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18822 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18823 /*
18824 * If next fcf index is not found check if there are lower
18825 * Priority level fcf's in the fcf_priority list.
18826 * Set up the rr_bmask with all of the avaiable fcf bits
18827 * at that level and continue the selection process.
18828 */
18829 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018830 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018831 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18832 "2844 No roundrobin failover FCF available\n");
James Smart036cad12018-10-23 13:41:06 -070018833
18834 return LPFC_FCOE_FCF_NEXT_NONE;
James Smart3804dc82010-07-14 15:31:37 -040018835 }
18836
James Smart7d791df2011-07-22 18:37:52 -040018837 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18838 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018839 LPFC_FCF_FLOGI_FAILED) {
18840 if (list_is_singular(&phba->fcf.fcf_pri_list))
18841 return LPFC_FCOE_FCF_NEXT_NONE;
18842
James Smart7d791df2011-07-22 18:37:52 -040018843 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018844 }
James Smart7d791df2011-07-22 18:37:52 -040018845
James Smart3804dc82010-07-14 15:31:37 -040018846 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018847 "2845 Get next roundrobin failover FCF (x%x)\n",
18848 next_fcf_index);
18849
James Smart0c9ab6f2010-02-26 14:15:57 -050018850 return next_fcf_index;
18851}
18852
18853/**
18854 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18855 * @phba: pointer to lpfc hba data structure.
18856 *
18857 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018858 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018859 * does not go beyond the range of the driver allocated bmask dimension
18860 * before setting the bit.
18861 *
18862 * Returns 0 if the index bit successfully set, otherwise, it returns
18863 * -EINVAL.
18864 **/
18865int
18866lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18867{
18868 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18869 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018870 "2610 FCF (x%x) reached driver's book "
18871 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018872 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18873 return -EINVAL;
18874 }
18875 /* Set the eligible FCF record index bmask */
18876 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18877
James Smart3804dc82010-07-14 15:31:37 -040018878 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018879 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018880 "bmask\n", fcf_index);
18881
James Smart0c9ab6f2010-02-26 14:15:57 -050018882 return 0;
18883}
18884
18885/**
James Smart3804dc82010-07-14 15:31:37 -040018886 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018887 * @phba: pointer to lpfc hba data structure.
18888 *
18889 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018890 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018891 * does not go beyond the range of the driver allocated bmask dimension
18892 * before clearing the bit.
18893 **/
18894void
18895lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18896{
James Smart9a803a72013-09-06 12:17:56 -040018897 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018898 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18899 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018900 "2762 FCF (x%x) reached driver's book "
18901 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018902 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18903 return;
18904 }
18905 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018906 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018907 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18908 list) {
James Smart7d791df2011-07-22 18:37:52 -040018909 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18910 list_del_init(&fcf_pri->list);
18911 break;
18912 }
18913 }
18914 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018915 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018916
18917 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018918 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018919 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018920}
18921
18922/**
James Smartecfd03c2010-02-12 14:41:27 -050018923 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18924 * @phba: pointer to lpfc hba data structure.
18925 *
18926 * This routine is the completion routine for the rediscover FCF table mailbox
18927 * command. If the mailbox command returned failure, it will try to stop the
18928 * FCF rediscover wait timer.
18929 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018930static void
James Smartecfd03c2010-02-12 14:41:27 -050018931lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18932{
18933 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18934 uint32_t shdr_status, shdr_add_status;
18935
18936 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18937
18938 shdr_status = bf_get(lpfc_mbox_hdr_status,
18939 &redisc_fcf->header.cfg_shdr.response);
18940 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18941 &redisc_fcf->header.cfg_shdr.response);
18942 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018943 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018944 "2746 Requesting for FCF rediscovery failed "
18945 "status x%x add_status x%x\n",
18946 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018947 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018948 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018949 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018950 spin_unlock_irq(&phba->hbalock);
18951 /*
18952 * CVL event triggered FCF rediscover request failed,
18953 * last resort to re-try current registered FCF entry.
18954 */
18955 lpfc_retry_pport_discovery(phba);
18956 } else {
18957 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018958 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018959 spin_unlock_irq(&phba->hbalock);
18960 /*
18961 * DEAD FCF event triggered FCF rediscover request
18962 * failed, last resort to fail over as a link down
18963 * to FCF registration.
18964 */
18965 lpfc_sli4_fcf_dead_failthrough(phba);
18966 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018967 } else {
18968 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018969 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018970 /*
18971 * Start FCF rediscovery wait timer for pending FCF
18972 * before rescan FCF record table.
18973 */
18974 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018975 }
James Smartecfd03c2010-02-12 14:41:27 -050018976
18977 mempool_free(mbox, phba->mbox_mem_pool);
18978}
18979
18980/**
James Smart3804dc82010-07-14 15:31:37 -040018981 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018982 * @phba: pointer to lpfc hba data structure.
18983 *
18984 * This routine is invoked to request for rediscovery of the entire FCF table
18985 * by the port.
18986 **/
18987int
18988lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18989{
18990 LPFC_MBOXQ_t *mbox;
18991 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18992 int rc, length;
18993
James Smart0c9ab6f2010-02-26 14:15:57 -050018994 /* Cancel retry delay timers to all vports before FCF rediscover */
18995 lpfc_cancel_all_vport_retry_delay_timer(phba);
18996
James Smartecfd03c2010-02-12 14:41:27 -050018997 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18998 if (!mbox) {
18999 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19000 "2745 Failed to allocate mbox for "
19001 "requesting FCF rediscover.\n");
19002 return -ENOMEM;
19003 }
19004
19005 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
19006 sizeof(struct lpfc_sli4_cfg_mhdr));
19007 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
19008 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
19009 length, LPFC_SLI4_MBX_EMBED);
19010
19011 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
19012 /* Set count to 0 for invalidating the entire FCF database */
19013 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
19014
19015 /* Issue the mailbox command asynchronously */
19016 mbox->vport = phba->pport;
19017 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
19018 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
19019
19020 if (rc == MBX_NOT_FINISHED) {
19021 mempool_free(mbox, phba->mbox_mem_pool);
19022 return -EIO;
19023 }
19024 return 0;
19025}
19026
19027/**
James Smartfc2b9892010-02-26 14:15:29 -050019028 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
19029 * @phba: pointer to lpfc hba data structure.
19030 *
19031 * This function is the failover routine as a last resort to the FCF DEAD
19032 * event when driver failed to perform fast FCF failover.
19033 **/
19034void
19035lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
19036{
19037 uint32_t link_state;
19038
19039 /*
19040 * Last resort as FCF DEAD event failover will treat this as
19041 * a link down, but save the link state because we don't want
19042 * it to be changed to Link Down unless it is already down.
19043 */
19044 link_state = phba->link_state;
19045 lpfc_linkdown(phba);
19046 phba->link_state = link_state;
19047
19048 /* Unregister FCF if no devices connected to it */
19049 lpfc_unregister_unused_fcf(phba);
19050}
19051
19052/**
James Smart026abb82011-12-13 13:20:45 -050019053 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040019054 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050019055 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040019056 *
James Smart026abb82011-12-13 13:20:45 -050019057 * This function gets SLI3 port configure region 23 data through memory dump
19058 * mailbox command. When it successfully retrieves data, the size of the data
19059 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040019060 **/
James Smart026abb82011-12-13 13:20:45 -050019061static uint32_t
19062lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040019063{
19064 LPFC_MBOXQ_t *pmb = NULL;
19065 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050019066 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040019067 int rc;
19068
James Smart026abb82011-12-13 13:20:45 -050019069 if (!rgn23_data)
19070 return 0;
19071
James Smarta0c87cb2009-07-19 10:01:10 -040019072 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19073 if (!pmb) {
19074 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050019075 "2600 failed to allocate mailbox memory\n");
19076 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040019077 }
19078 mb = &pmb->u.mb;
19079
James Smarta0c87cb2009-07-19 10:01:10 -040019080 do {
19081 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
19082 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
19083
19084 if (rc != MBX_SUCCESS) {
19085 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050019086 "2601 failed to read config "
19087 "region 23, rc 0x%x Status 0x%x\n",
19088 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040019089 mb->un.varDmp.word_cnt = 0;
19090 }
19091 /*
19092 * dump mem may return a zero when finished or we got a
19093 * mailbox error, either way we are done.
19094 */
19095 if (mb->un.varDmp.word_cnt == 0)
19096 break;
19097 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
19098 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
19099
19100 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050019101 rgn23_data + offset,
19102 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040019103 offset += mb->un.varDmp.word_cnt;
19104 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
19105
James Smart026abb82011-12-13 13:20:45 -050019106 mempool_free(pmb, phba->mbox_mem_pool);
19107 return offset;
19108}
19109
19110/**
19111 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
19112 * @phba: pointer to lpfc hba data structure.
19113 * @rgn23_data: pointer to configure region 23 data.
19114 *
19115 * This function gets SLI4 port configure region 23 data through memory dump
19116 * mailbox command. When it successfully retrieves data, the size of the data
19117 * will be returned, otherwise, 0 will be returned.
19118 **/
19119static uint32_t
19120lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
19121{
19122 LPFC_MBOXQ_t *mboxq = NULL;
19123 struct lpfc_dmabuf *mp = NULL;
19124 struct lpfc_mqe *mqe;
19125 uint32_t data_length = 0;
19126 int rc;
19127
19128 if (!rgn23_data)
19129 return 0;
19130
19131 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19132 if (!mboxq) {
19133 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19134 "3105 failed to allocate mailbox memory\n");
19135 return 0;
19136 }
19137
19138 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
19139 goto out;
19140 mqe = &mboxq->u.mqe;
James Smart3e1f0712018-11-29 16:09:29 -080019141 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smart026abb82011-12-13 13:20:45 -050019142 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19143 if (rc)
19144 goto out;
19145 data_length = mqe->un.mb_words[5];
19146 if (data_length == 0)
19147 goto out;
19148 if (data_length > DMP_RGN23_SIZE) {
19149 data_length = 0;
19150 goto out;
19151 }
19152 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
19153out:
19154 mempool_free(mboxq, phba->mbox_mem_pool);
19155 if (mp) {
19156 lpfc_mbuf_free(phba, mp->virt, mp->phys);
19157 kfree(mp);
19158 }
19159 return data_length;
19160}
19161
19162/**
19163 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
19164 * @phba: pointer to lpfc hba data structure.
19165 *
19166 * This function read region 23 and parse TLV for port status to
19167 * decide if the user disaled the port. If the TLV indicates the
19168 * port is disabled, the hba_flag is set accordingly.
19169 **/
19170void
19171lpfc_sli_read_link_ste(struct lpfc_hba *phba)
19172{
19173 uint8_t *rgn23_data = NULL;
19174 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
19175 uint32_t offset = 0;
19176
19177 /* Get adapter Region 23 data */
19178 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
19179 if (!rgn23_data)
19180 goto out;
19181
19182 if (phba->sli_rev < LPFC_SLI_REV4)
19183 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
19184 else {
19185 if_type = bf_get(lpfc_sli_intf_if_type,
19186 &phba->sli4_hba.sli_intf);
19187 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
19188 goto out;
19189 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
19190 }
James Smarta0c87cb2009-07-19 10:01:10 -040019191
19192 if (!data_size)
19193 goto out;
19194
19195 /* Check the region signature first */
19196 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
19197 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19198 "2619 Config region 23 has bad signature\n");
19199 goto out;
19200 }
19201 offset += 4;
19202
19203 /* Check the data structure version */
19204 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
19205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19206 "2620 Config region 23 has bad version\n");
19207 goto out;
19208 }
19209 offset += 4;
19210
19211 /* Parse TLV entries in the region */
19212 while (offset < data_size) {
19213 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
19214 break;
19215 /*
19216 * If the TLV is not driver specific TLV or driver id is
19217 * not linux driver id, skip the record.
19218 */
19219 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
19220 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
19221 (rgn23_data[offset + 3] != 0)) {
19222 offset += rgn23_data[offset + 1] * 4 + 4;
19223 continue;
19224 }
19225
19226 /* Driver found a driver specific TLV in the config region */
19227 sub_tlv_len = rgn23_data[offset + 1] * 4;
19228 offset += 4;
19229 tlv_offset = 0;
19230
19231 /*
19232 * Search for configured port state sub-TLV.
19233 */
19234 while ((offset < data_size) &&
19235 (tlv_offset < sub_tlv_len)) {
19236 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
19237 offset += 4;
19238 tlv_offset += 4;
19239 break;
19240 }
19241 if (rgn23_data[offset] != PORT_STE_TYPE) {
19242 offset += rgn23_data[offset + 1] * 4 + 4;
19243 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
19244 continue;
19245 }
19246
19247 /* This HBA contains PORT_STE configured */
19248 if (!rgn23_data[offset + 2])
19249 phba->hba_flag |= LINK_DISABLED;
19250
19251 goto out;
19252 }
19253 }
James Smart026abb82011-12-13 13:20:45 -050019254
James Smarta0c87cb2009-07-19 10:01:10 -040019255out:
James Smarta0c87cb2009-07-19 10:01:10 -040019256 kfree(rgn23_data);
19257 return;
19258}
James Smart695a8142010-01-26 23:08:03 -050019259
19260/**
James Smart52d52442011-05-24 11:42:45 -040019261 * lpfc_wr_object - write an object to the firmware
19262 * @phba: HBA structure that indicates port to create a queue on.
19263 * @dmabuf_list: list of dmabufs to write to the port.
19264 * @size: the total byte value of the objects to write to the port.
19265 * @offset: the current offset to be used to start the transfer.
19266 *
19267 * This routine will create a wr_object mailbox command to send to the port.
19268 * the mailbox command will be constructed using the dma buffers described in
19269 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
19270 * BDEs that the imbedded mailbox can support. The @offset variable will be
19271 * used to indicate the starting offset of the transfer and will also return
19272 * the offset after the write object mailbox has completed. @size is used to
19273 * determine the end of the object and whether the eof bit should be set.
19274 *
19275 * Return 0 is successful and offset will contain the the new offset to use
19276 * for the next write.
19277 * Return negative value for error cases.
19278 **/
19279int
19280lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
19281 uint32_t size, uint32_t *offset)
19282{
19283 struct lpfc_mbx_wr_object *wr_object;
19284 LPFC_MBOXQ_t *mbox;
19285 int rc = 0, i = 0;
James Smart50212672018-12-13 15:17:57 -080019286 uint32_t shdr_status, shdr_add_status, shdr_change_status;
James Smart52d52442011-05-24 11:42:45 -040019287 uint32_t mbox_tmo;
James Smart52d52442011-05-24 11:42:45 -040019288 struct lpfc_dmabuf *dmabuf;
19289 uint32_t written = 0;
James Smart50212672018-12-13 15:17:57 -080019290 bool check_change_status = false;
James Smart52d52442011-05-24 11:42:45 -040019291
19292 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19293 if (!mbox)
19294 return -ENOMEM;
19295
19296 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
19297 LPFC_MBOX_OPCODE_WRITE_OBJECT,
19298 sizeof(struct lpfc_mbx_wr_object) -
19299 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
19300
19301 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
19302 wr_object->u.request.write_offset = *offset;
19303 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
19304 wr_object->u.request.object_name[0] =
19305 cpu_to_le32(wr_object->u.request.object_name[0]);
19306 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
19307 list_for_each_entry(dmabuf, dmabuf_list, list) {
19308 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
19309 break;
19310 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
19311 wr_object->u.request.bde[i].addrHigh =
19312 putPaddrHigh(dmabuf->phys);
19313 if (written + SLI4_PAGE_SIZE >= size) {
19314 wr_object->u.request.bde[i].tus.f.bdeSize =
19315 (size - written);
19316 written += (size - written);
19317 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
James Smart50212672018-12-13 15:17:57 -080019318 bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
19319 check_change_status = true;
James Smart52d52442011-05-24 11:42:45 -040019320 } else {
19321 wr_object->u.request.bde[i].tus.f.bdeSize =
19322 SLI4_PAGE_SIZE;
19323 written += SLI4_PAGE_SIZE;
19324 }
19325 i++;
19326 }
19327 wr_object->u.request.bde_count = i;
19328 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
19329 if (!phba->sli4_hba.intr_enable)
19330 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
19331 else {
James Smarta183a152011-10-10 21:32:43 -040019332 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040019333 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
19334 }
19335 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart50212672018-12-13 15:17:57 -080019336 shdr_status = bf_get(lpfc_mbox_hdr_status,
19337 &wr_object->header.cfg_shdr.response);
19338 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
19339 &wr_object->header.cfg_shdr.response);
19340 if (check_change_status) {
19341 shdr_change_status = bf_get(lpfc_wr_object_change_status,
19342 &wr_object->u.response);
19343 switch (shdr_change_status) {
19344 case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
19345 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19346 "3198 Firmware write complete: System "
19347 "reboot required to instantiate\n");
19348 break;
19349 case (LPFC_CHANGE_STATUS_FW_RESET):
19350 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19351 "3199 Firmware write complete: Firmware"
19352 " reset required to instantiate\n");
19353 break;
19354 case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
19355 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19356 "3200 Firmware write complete: Port "
19357 "Migration or PCI Reset required to "
19358 "instantiate\n");
19359 break;
19360 case (LPFC_CHANGE_STATUS_PCI_RESET):
19361 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19362 "3201 Firmware write complete: PCI "
19363 "Reset required to instantiate\n");
19364 break;
19365 default:
19366 break;
19367 }
19368 }
James Smart52d52442011-05-24 11:42:45 -040019369 if (rc != MBX_TIMEOUT)
19370 mempool_free(mbox, phba->mbox_mem_pool);
19371 if (shdr_status || shdr_add_status || rc) {
19372 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19373 "3025 Write Object mailbox failed with "
19374 "status x%x add_status x%x, mbx status x%x\n",
19375 shdr_status, shdr_add_status, rc);
19376 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080019377 *offset = shdr_add_status;
James Smart52d52442011-05-24 11:42:45 -040019378 } else
19379 *offset += wr_object->u.response.actual_write_length;
19380 return rc;
19381}
19382
19383/**
James Smart695a8142010-01-26 23:08:03 -050019384 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
19385 * @vport: pointer to vport data structure.
19386 *
19387 * This function iterate through the mailboxq and clean up all REG_LOGIN
19388 * and REG_VPI mailbox commands associated with the vport. This function
19389 * is called when driver want to restart discovery of the vport due to
19390 * a Clear Virtual Link event.
19391 **/
19392void
19393lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
19394{
19395 struct lpfc_hba *phba = vport->phba;
19396 LPFC_MBOXQ_t *mb, *nextmb;
19397 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040019398 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040019399 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040019400 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040019401 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050019402 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050019403
James Smartd439d282010-09-29 11:18:45 -040019404 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050019405 spin_lock_irq(&phba->hbalock);
19406 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
19407 if (mb->vport != vport)
19408 continue;
19409
19410 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19411 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19412 continue;
19413
James Smartd439d282010-09-29 11:18:45 -040019414 list_del(&mb->list);
19415 list_add_tail(&mb->list, &mbox_cmd_list);
19416 }
19417 /* Clean up active mailbox command with the vport */
19418 mb = phba->sli.mbox_active;
19419 if (mb && (mb->vport == vport)) {
19420 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
19421 (mb->u.mb.mbxCommand == MBX_REG_VPI))
19422 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19423 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019424 act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -040019425 /* Put reference count for delayed processing */
19426 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
19427 /* Unregister the RPI when mailbox complete */
19428 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19429 }
19430 }
James Smart63e801c2010-11-20 23:14:19 -050019431 /* Cleanup any mailbox completions which are not yet processed */
19432 do {
19433 restart_loop = 0;
19434 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
19435 /*
19436 * If this mailox is already processed or it is
19437 * for another vport ignore it.
19438 */
19439 if ((mb->vport != vport) ||
19440 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
19441 continue;
19442
19443 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19444 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19445 continue;
19446
19447 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19448 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019449 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smart63e801c2010-11-20 23:14:19 -050019450 /* Unregister the RPI when mailbox complete */
19451 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19452 restart_loop = 1;
19453 spin_unlock_irq(&phba->hbalock);
19454 spin_lock(shost->host_lock);
19455 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19456 spin_unlock(shost->host_lock);
19457 spin_lock_irq(&phba->hbalock);
19458 break;
19459 }
19460 }
19461 } while (restart_loop);
19462
James Smartd439d282010-09-29 11:18:45 -040019463 spin_unlock_irq(&phba->hbalock);
19464
19465 /* Release the cleaned-up mailbox commands */
19466 while (!list_empty(&mbox_cmd_list)) {
19467 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050019468 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019469 mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
James Smart695a8142010-01-26 23:08:03 -050019470 if (mp) {
19471 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19472 kfree(mp);
19473 }
James Smart3e1f0712018-11-29 16:09:29 -080019474 mb->ctx_buf = NULL;
19475 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
19476 mb->ctx_ndlp = NULL;
James Smart78730cf2010-04-06 15:06:30 -040019477 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019478 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040019479 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019480 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040019481 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040019482 }
James Smart695a8142010-01-26 23:08:03 -050019483 }
James Smart695a8142010-01-26 23:08:03 -050019484 mempool_free(mb, phba->mbox_mem_pool);
19485 }
James Smartd439d282010-09-29 11:18:45 -040019486
19487 /* Release the ndlp with the cleaned-up active mailbox command */
19488 if (act_mbx_ndlp) {
19489 spin_lock(shost->host_lock);
19490 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19491 spin_unlock(shost->host_lock);
19492 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050019493 }
James Smart695a8142010-01-26 23:08:03 -050019494}
19495
James Smart2a9bf3d2010-06-07 15:24:45 -040019496/**
19497 * lpfc_drain_txq - Drain the txq
19498 * @phba: Pointer to HBA context object.
19499 *
19500 * This function attempt to submit IOCBs on the txq
19501 * to the adapter. For SLI4 adapters, the txq contains
19502 * ELS IOCBs that have been deferred because the there
19503 * are no SGLs. This congestion can occur with large
19504 * vport counts during node discovery.
19505 **/
19506
19507uint32_t
19508lpfc_drain_txq(struct lpfc_hba *phba)
19509{
19510 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080019511 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090019512 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040019513 unsigned long iflags = 0;
19514 char *fail_msg = NULL;
19515 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080019516 union lpfc_wqe128 wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040019517 uint32_t txq_cnt = 0;
James Smartdc19e3b2018-05-24 21:08:57 -070019518 struct lpfc_queue *wq;
James Smart2a9bf3d2010-06-07 15:24:45 -040019519
James Smartdc19e3b2018-05-24 21:08:57 -070019520 if (phba->link_flag & LS_MDS_LOOPBACK) {
19521 /* MDS WQE are posted only to first WQ*/
James Smartcdb42be2019-01-28 11:14:21 -080019522 wq = phba->sli4_hba.hdwq[0].fcp_wq;
James Smartdc19e3b2018-05-24 21:08:57 -070019523 if (unlikely(!wq))
19524 return 0;
19525 pring = wq->pring;
19526 } else {
19527 wq = phba->sli4_hba.els_wq;
19528 if (unlikely(!wq))
19529 return 0;
19530 pring = lpfc_phba_elsring(phba);
19531 }
19532
19533 if (unlikely(!pring) || list_empty(&pring->txq))
Dick Kennedy1234a6d2017-09-29 17:34:29 -070019534 return 0;
James Smart895427b2017-02-12 13:52:30 -080019535
James Smart398d81c2013-05-31 17:04:19 -040019536 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050019537 list_for_each_entry(piocbq, &pring->txq, list) {
19538 txq_cnt++;
19539 }
19540
19541 if (txq_cnt > pring->txq_max)
19542 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019543
James Smart398d81c2013-05-31 17:04:19 -040019544 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019545
James Smart0e9bb8d2013-03-01 16:35:12 -050019546 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040019547 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019548
James Smart19ca7602010-11-20 23:11:55 -050019549 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040019550 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040019551 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040019552 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19553 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050019554 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040019555 break;
19556 }
James Smart895427b2017-02-12 13:52:30 -080019557 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040019558 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050019559 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040019560 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019561 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040019562 }
James Smart0e9bb8d2013-03-01 16:35:12 -050019563 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040019564
19565 /* The xri and iocb resources secured,
19566 * attempt to issue request
19567 */
James Smart6d368e52011-05-24 11:44:12 -040019568 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040019569 piocbq->sli4_xritag = sglq->sli4_xritag;
19570 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19571 fail_msg = "to convert bpl to sgl";
James Smart205e8242018-03-05 12:04:03 -080019572 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019573 fail_msg = "to convert iocb to wqe";
James Smartdc19e3b2018-05-24 21:08:57 -070019574 else if (lpfc_sli4_wq_put(wq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019575 fail_msg = " - Wq is full";
19576 else
19577 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19578
19579 if (fail_msg) {
19580 /* Failed means we can't issue and need to cancel */
19581 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19582 "2822 IOCB failed %s iotag 0x%x "
19583 "xri 0x%x\n",
19584 fail_msg,
19585 piocbq->iotag, piocbq->sli4_xritag);
19586 list_add_tail(&piocbq->list, &completions);
19587 }
James Smart398d81c2013-05-31 17:04:19 -040019588 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019589 }
19590
James Smart2a9bf3d2010-06-07 15:24:45 -040019591 /* Cancel all the IOCBs that cannot be issued */
19592 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19593 IOERR_SLI_ABORTED);
19594
James Smart0e9bb8d2013-03-01 16:35:12 -050019595 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019596}
James Smart895427b2017-02-12 13:52:30 -080019597
19598/**
19599 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19600 * @phba: Pointer to HBA context object.
19601 * @pwqe: Pointer to command WQE.
19602 * @sglq: Pointer to the scatter gather queue object.
19603 *
19604 * This routine converts the bpl or bde that is in the WQE
19605 * to a sgl list for the sli4 hardware. The physical address
19606 * of the bpl/bde is converted back to a virtual address.
19607 * If the WQE contains a BPL then the list of BDE's is
19608 * converted to sli4_sge's. If the WQE contains a single
19609 * BDE then it is converted to a single sli_sge.
19610 * The WQE is still in cpu endianness so the contents of
19611 * the bpl can be used without byte swapping.
19612 *
19613 * Returns valid XRI = Success, NO_XRI = Failure.
19614 */
19615static uint16_t
19616lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19617 struct lpfc_sglq *sglq)
19618{
19619 uint16_t xritag = NO_XRI;
19620 struct ulp_bde64 *bpl = NULL;
19621 struct ulp_bde64 bde;
19622 struct sli4_sge *sgl = NULL;
19623 struct lpfc_dmabuf *dmabuf;
James Smart205e8242018-03-05 12:04:03 -080019624 union lpfc_wqe128 *wqe;
James Smart895427b2017-02-12 13:52:30 -080019625 int numBdes = 0;
19626 int i = 0;
19627 uint32_t offset = 0; /* accumulated offset in the sg request list */
19628 int inbound = 0; /* number of sg reply entries inbound from firmware */
19629 uint32_t cmd;
19630
19631 if (!pwqeq || !sglq)
19632 return xritag;
19633
19634 sgl = (struct sli4_sge *)sglq->sgl;
19635 wqe = &pwqeq->wqe;
19636 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19637
19638 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19639 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19640 return sglq->sli4_xritag;
19641 numBdes = pwqeq->rsvd2;
19642 if (numBdes) {
19643 /* The addrHigh and addrLow fields within the WQE
19644 * have not been byteswapped yet so there is no
19645 * need to swap them back.
19646 */
19647 if (pwqeq->context3)
19648 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19649 else
19650 return xritag;
19651
19652 bpl = (struct ulp_bde64 *)dmabuf->virt;
19653 if (!bpl)
19654 return xritag;
19655
19656 for (i = 0; i < numBdes; i++) {
19657 /* Should already be byte swapped. */
19658 sgl->addr_hi = bpl->addrHigh;
19659 sgl->addr_lo = bpl->addrLow;
19660
19661 sgl->word2 = le32_to_cpu(sgl->word2);
19662 if ((i+1) == numBdes)
19663 bf_set(lpfc_sli4_sge_last, sgl, 1);
19664 else
19665 bf_set(lpfc_sli4_sge_last, sgl, 0);
19666 /* swap the size field back to the cpu so we
19667 * can assign it to the sgl.
19668 */
19669 bde.tus.w = le32_to_cpu(bpl->tus.w);
19670 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19671 /* The offsets in the sgl need to be accumulated
19672 * separately for the request and reply lists.
19673 * The request is always first, the reply follows.
19674 */
19675 switch (cmd) {
19676 case CMD_GEN_REQUEST64_WQE:
19677 /* add up the reply sg entries */
19678 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19679 inbound++;
19680 /* first inbound? reset the offset */
19681 if (inbound == 1)
19682 offset = 0;
19683 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19684 bf_set(lpfc_sli4_sge_type, sgl,
19685 LPFC_SGE_TYPE_DATA);
19686 offset += bde.tus.f.bdeSize;
19687 break;
19688 case CMD_FCP_TRSP64_WQE:
19689 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19690 bf_set(lpfc_sli4_sge_type, sgl,
19691 LPFC_SGE_TYPE_DATA);
19692 break;
19693 case CMD_FCP_TSEND64_WQE:
19694 case CMD_FCP_TRECEIVE64_WQE:
19695 bf_set(lpfc_sli4_sge_type, sgl,
19696 bpl->tus.f.bdeFlags);
19697 if (i < 3)
19698 offset = 0;
19699 else
19700 offset += bde.tus.f.bdeSize;
19701 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19702 break;
19703 }
19704 sgl->word2 = cpu_to_le32(sgl->word2);
19705 bpl++;
19706 sgl++;
19707 }
19708 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19709 /* The addrHigh and addrLow fields of the BDE have not
19710 * been byteswapped yet so they need to be swapped
19711 * before putting them in the sgl.
19712 */
19713 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19714 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19715 sgl->word2 = le32_to_cpu(sgl->word2);
19716 bf_set(lpfc_sli4_sge_last, sgl, 1);
19717 sgl->word2 = cpu_to_le32(sgl->word2);
19718 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19719 }
19720 return sglq->sli4_xritag;
19721}
19722
19723/**
19724 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19725 * @phba: Pointer to HBA context object.
19726 * @ring_number: Base sli ring number
19727 * @pwqe: Pointer to command WQE.
19728 **/
19729int
James Smart1fbf9742019-01-28 11:14:26 -080019730lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
James Smart895427b2017-02-12 13:52:30 -080019731 struct lpfc_iocbq *pwqe)
19732{
James Smart205e8242018-03-05 12:04:03 -080019733 union lpfc_wqe128 *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080019734 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080019735 struct lpfc_queue *wq;
19736 struct lpfc_sglq *sglq;
19737 struct lpfc_sli_ring *pring;
19738 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070019739 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080019740
19741 /* NVME_LS and NVME_LS ABTS requests. */
19742 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19743 pring = phba->sli4_hba.nvmels_wq->pring;
James Smart6a828b02019-01-28 11:14:31 -080019744 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
19745 qp, wq_access);
James Smart895427b2017-02-12 13:52:30 -080019746 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19747 if (!sglq) {
19748 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19749 return WQE_BUSY;
19750 }
19751 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19752 pwqe->sli4_xritag = sglq->sli4_xritag;
19753 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19754 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19755 return WQE_ERROR;
19756 }
19757 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19758 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070019759 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19760 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019761 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019762 return ret;
James Smart895427b2017-02-12 13:52:30 -080019763 }
Dick Kennedycd22d602017-08-23 16:55:35 -070019764
James Smart895427b2017-02-12 13:52:30 -080019765 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19766 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19767 return 0;
19768 }
19769
19770 /* NVME_FCREQ and NVME_ABTS requests */
19771 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19772 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smart1fbf9742019-01-28 11:14:26 -080019773 wq = qp->nvme_wq;
19774 pring = wq->pring;
19775
19776 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->nvme_cq_map);
James Smart895427b2017-02-12 13:52:30 -080019777
James Smart6a828b02019-01-28 11:14:31 -080019778 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
19779 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070019780 ret = lpfc_sli4_wq_put(wq, wqe);
19781 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019782 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019783 return ret;
James Smart895427b2017-02-12 13:52:30 -080019784 }
19785 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19786 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19787 return 0;
19788 }
19789
James Smartf358dd02017-02-12 13:52:34 -080019790 /* NVMET requests */
19791 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19792 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smart1fbf9742019-01-28 11:14:26 -080019793 wq = qp->nvme_wq;
19794 pring = wq->pring;
James Smartf358dd02017-02-12 13:52:34 -080019795
James Smartf358dd02017-02-12 13:52:34 -080019796 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070019797 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080019798 if (pwqe->sli4_xritag == NO_XRI) {
19799 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19800 pwqe->sli4_xritag = sglq->sli4_xritag;
19801 }
19802 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19803 pwqe->sli4_xritag);
James Smart1fbf9742019-01-28 11:14:26 -080019804 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->nvme_cq_map);
19805
James Smart6a828b02019-01-28 11:14:31 -080019806 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
19807 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070019808 ret = lpfc_sli4_wq_put(wq, wqe);
19809 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080019810 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019811 return ret;
James Smartf358dd02017-02-12 13:52:34 -080019812 }
19813 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19814 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19815 return 0;
19816 }
James Smart895427b2017-02-12 13:52:30 -080019817 return WQE_ERROR;
19818}
James Smartc4908502019-01-28 11:14:28 -080019819
19820#ifdef LPFC_MXP_STAT
19821/**
19822 * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
19823 * @phba: pointer to lpfc hba data structure.
19824 * @hwqid: belong to which HWQ.
19825 *
19826 * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
19827 * 15 seconds after a test case is running.
19828 *
19829 * The user should call lpfc_debugfs_multixripools_write before running a test
19830 * case to clear stat_snapshot_taken. Then the user starts a test case. During
19831 * test case is running, stat_snapshot_taken is incremented by 1 every time when
19832 * this routine is called from heartbeat timer. When stat_snapshot_taken is
19833 * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
19834 **/
19835void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
19836{
19837 struct lpfc_sli4_hdw_queue *qp;
19838 struct lpfc_multixri_pool *multixri_pool;
19839 struct lpfc_pvt_pool *pvt_pool;
19840 struct lpfc_pbl_pool *pbl_pool;
19841 u32 txcmplq_cnt;
19842
19843 qp = &phba->sli4_hba.hdwq[hwqid];
19844 multixri_pool = qp->p_multixri_pool;
19845 if (!multixri_pool)
19846 return;
19847
19848 if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
19849 pvt_pool = &qp->p_multixri_pool->pvt_pool;
19850 pbl_pool = &qp->p_multixri_pool->pbl_pool;
19851 txcmplq_cnt = qp->fcp_wq->pring->txcmplq_cnt;
19852 if (qp->nvme_wq)
19853 txcmplq_cnt += qp->nvme_wq->pring->txcmplq_cnt;
19854
19855 multixri_pool->stat_pbl_count = pbl_pool->count;
19856 multixri_pool->stat_pvt_count = pvt_pool->count;
19857 multixri_pool->stat_busy_count = txcmplq_cnt;
19858 }
19859
19860 multixri_pool->stat_snapshot_taken++;
19861}
19862#endif
19863
19864/**
19865 * lpfc_adjust_pvt_pool_count - Adjust private pool count
19866 * @phba: pointer to lpfc hba data structure.
19867 * @hwqid: belong to which HWQ.
19868 *
19869 * This routine moves some XRIs from private to public pool when private pool
19870 * is not busy.
19871 **/
19872void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
19873{
19874 struct lpfc_multixri_pool *multixri_pool;
19875 u32 io_req_count;
19876 u32 prev_io_req_count;
19877
19878 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
19879 if (!multixri_pool)
19880 return;
19881 io_req_count = multixri_pool->io_req_count;
19882 prev_io_req_count = multixri_pool->prev_io_req_count;
19883
19884 if (prev_io_req_count != io_req_count) {
19885 /* Private pool is busy */
19886 multixri_pool->prev_io_req_count = io_req_count;
19887 } else {
19888 /* Private pool is not busy.
19889 * Move XRIs from private to public pool.
19890 */
19891 lpfc_move_xri_pvt_to_pbl(phba, hwqid);
19892 }
19893}
19894
19895/**
19896 * lpfc_adjust_high_watermark - Adjust high watermark
19897 * @phba: pointer to lpfc hba data structure.
19898 * @hwqid: belong to which HWQ.
19899 *
19900 * This routine sets high watermark as number of outstanding XRIs,
19901 * but make sure the new value is between xri_limit/2 and xri_limit.
19902 **/
19903void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
19904{
19905 u32 new_watermark;
19906 u32 watermark_max;
19907 u32 watermark_min;
19908 u32 xri_limit;
19909 u32 txcmplq_cnt;
19910 u32 abts_io_bufs;
19911 struct lpfc_multixri_pool *multixri_pool;
19912 struct lpfc_sli4_hdw_queue *qp;
19913
19914 qp = &phba->sli4_hba.hdwq[hwqid];
19915 multixri_pool = qp->p_multixri_pool;
19916 if (!multixri_pool)
19917 return;
19918 xri_limit = multixri_pool->xri_limit;
19919
19920 watermark_max = xri_limit;
19921 watermark_min = xri_limit / 2;
19922
19923 txcmplq_cnt = qp->fcp_wq->pring->txcmplq_cnt;
19924 abts_io_bufs = qp->abts_scsi_io_bufs;
19925 if (qp->nvme_wq) {
19926 txcmplq_cnt += qp->nvme_wq->pring->txcmplq_cnt;
19927 abts_io_bufs += qp->abts_nvme_io_bufs;
19928 }
19929
19930 new_watermark = txcmplq_cnt + abts_io_bufs;
19931 new_watermark = min(watermark_max, new_watermark);
19932 new_watermark = max(watermark_min, new_watermark);
19933 multixri_pool->pvt_pool.high_watermark = new_watermark;
19934
19935#ifdef LPFC_MXP_STAT
19936 multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
19937 new_watermark);
19938#endif
19939}
19940
19941/**
19942 * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
19943 * @phba: pointer to lpfc hba data structure.
19944 * @hwqid: belong to which HWQ.
19945 *
19946 * This routine is called from hearbeat timer when pvt_pool is idle.
19947 * All free XRIs are moved from private to public pool on hwqid with 2 steps.
19948 * The first step moves (all - low_watermark) amount of XRIs.
19949 * The second step moves the rest of XRIs.
19950 **/
19951void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
19952{
19953 struct lpfc_pbl_pool *pbl_pool;
19954 struct lpfc_pvt_pool *pvt_pool;
James Smart6a828b02019-01-28 11:14:31 -080019955 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080019956 struct lpfc_io_buf *lpfc_ncmd;
19957 struct lpfc_io_buf *lpfc_ncmd_next;
19958 unsigned long iflag;
19959 struct list_head tmp_list;
19960 u32 tmp_count;
19961
James Smart6a828b02019-01-28 11:14:31 -080019962 qp = &phba->sli4_hba.hdwq[hwqid];
19963 pbl_pool = &qp->p_multixri_pool->pbl_pool;
19964 pvt_pool = &qp->p_multixri_pool->pvt_pool;
James Smartc4908502019-01-28 11:14:28 -080019965 tmp_count = 0;
19966
James Smart6a828b02019-01-28 11:14:31 -080019967 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
19968 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080019969
19970 if (pvt_pool->count > pvt_pool->low_watermark) {
19971 /* Step 1: move (all - low_watermark) from pvt_pool
19972 * to pbl_pool
19973 */
19974
19975 /* Move low watermark of bufs from pvt_pool to tmp_list */
19976 INIT_LIST_HEAD(&tmp_list);
19977 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
19978 &pvt_pool->list, list) {
19979 list_move_tail(&lpfc_ncmd->list, &tmp_list);
19980 tmp_count++;
19981 if (tmp_count >= pvt_pool->low_watermark)
19982 break;
19983 }
19984
19985 /* Move all bufs from pvt_pool to pbl_pool */
19986 list_splice_init(&pvt_pool->list, &pbl_pool->list);
19987
19988 /* Move all bufs from tmp_list to pvt_pool */
19989 list_splice(&tmp_list, &pvt_pool->list);
19990
19991 pbl_pool->count += (pvt_pool->count - tmp_count);
19992 pvt_pool->count = tmp_count;
19993 } else {
19994 /* Step 2: move the rest from pvt_pool to pbl_pool */
19995 list_splice_init(&pvt_pool->list, &pbl_pool->list);
19996 pbl_pool->count += pvt_pool->count;
19997 pvt_pool->count = 0;
19998 }
19999
20000 spin_unlock(&pvt_pool->lock);
20001 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20002}
20003
20004/**
20005 * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
20006 * @phba: pointer to lpfc hba data structure
20007 * @pbl_pool: specified public free XRI pool
20008 * @pvt_pool: specified private free XRI pool
20009 * @count: number of XRIs to move
20010 *
20011 * This routine tries to move some free common bufs from the specified pbl_pool
20012 * to the specified pvt_pool. It might move less than count XRIs if there's not
20013 * enough in public pool.
20014 *
20015 * Return:
20016 * true - if XRIs are successfully moved from the specified pbl_pool to the
20017 * specified pvt_pool
20018 * false - if the specified pbl_pool is empty or locked by someone else
20019 **/
20020static bool
James Smart6a828b02019-01-28 11:14:31 -080020021_lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
20022 struct lpfc_pbl_pool *pbl_pool,
James Smartc4908502019-01-28 11:14:28 -080020023 struct lpfc_pvt_pool *pvt_pool, u32 count)
20024{
20025 struct lpfc_io_buf *lpfc_ncmd;
20026 struct lpfc_io_buf *lpfc_ncmd_next;
20027 unsigned long iflag;
20028 int ret;
20029
20030 ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
20031 if (ret) {
20032 if (pbl_pool->count) {
20033 /* Move a batch of XRIs from public to private pool */
James Smart6a828b02019-01-28 11:14:31 -080020034 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020035 list_for_each_entry_safe(lpfc_ncmd,
20036 lpfc_ncmd_next,
20037 &pbl_pool->list,
20038 list) {
20039 list_move_tail(&lpfc_ncmd->list,
20040 &pvt_pool->list);
20041 pvt_pool->count++;
20042 pbl_pool->count--;
20043 count--;
20044 if (count == 0)
20045 break;
20046 }
20047
20048 spin_unlock(&pvt_pool->lock);
20049 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20050 return true;
20051 }
20052 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20053 }
20054
20055 return false;
20056}
20057
20058/**
20059 * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
20060 * @phba: pointer to lpfc hba data structure.
20061 * @hwqid: belong to which HWQ.
20062 * @count: number of XRIs to move
20063 *
20064 * This routine tries to find some free common bufs in one of public pools with
20065 * Round Robin method. The search always starts from local hwqid, then the next
20066 * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
20067 * a batch of free common bufs are moved to private pool on hwqid.
20068 * It might move less than count XRIs if there's not enough in public pool.
20069 **/
20070void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
20071{
20072 struct lpfc_multixri_pool *multixri_pool;
20073 struct lpfc_multixri_pool *next_multixri_pool;
20074 struct lpfc_pvt_pool *pvt_pool;
20075 struct lpfc_pbl_pool *pbl_pool;
James Smart6a828b02019-01-28 11:14:31 -080020076 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080020077 u32 next_hwqid;
20078 u32 hwq_count;
20079 int ret;
20080
James Smart6a828b02019-01-28 11:14:31 -080020081 qp = &phba->sli4_hba.hdwq[hwqid];
20082 multixri_pool = qp->p_multixri_pool;
James Smartc4908502019-01-28 11:14:28 -080020083 pvt_pool = &multixri_pool->pvt_pool;
20084 pbl_pool = &multixri_pool->pbl_pool;
20085
20086 /* Check if local pbl_pool is available */
James Smart6a828b02019-01-28 11:14:31 -080020087 ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080020088 if (ret) {
20089#ifdef LPFC_MXP_STAT
20090 multixri_pool->local_pbl_hit_count++;
20091#endif
20092 return;
20093 }
20094
20095 hwq_count = phba->cfg_hdw_queue;
20096
20097 /* Get the next hwqid which was found last time */
20098 next_hwqid = multixri_pool->rrb_next_hwqid;
20099
20100 do {
20101 /* Go to next hwq */
20102 next_hwqid = (next_hwqid + 1) % hwq_count;
20103
20104 next_multixri_pool =
20105 phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
20106 pbl_pool = &next_multixri_pool->pbl_pool;
20107
20108 /* Check if the public free xri pool is available */
20109 ret = _lpfc_move_xri_pbl_to_pvt(
James Smart6a828b02019-01-28 11:14:31 -080020110 phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080020111
20112 /* Exit while-loop if success or all hwqid are checked */
20113 } while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
20114
20115 /* Starting point for the next time */
20116 multixri_pool->rrb_next_hwqid = next_hwqid;
20117
20118 if (!ret) {
20119 /* stats: all public pools are empty*/
20120 multixri_pool->pbl_empty_count++;
20121 }
20122
20123#ifdef LPFC_MXP_STAT
20124 if (ret) {
20125 if (next_hwqid == hwqid)
20126 multixri_pool->local_pbl_hit_count++;
20127 else
20128 multixri_pool->other_pbl_hit_count++;
20129 }
20130#endif
20131}
20132
20133/**
20134 * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
20135 * @phba: pointer to lpfc hba data structure.
20136 * @qp: belong to which HWQ.
20137 *
20138 * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
20139 * low watermark.
20140 **/
20141void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
20142{
20143 struct lpfc_multixri_pool *multixri_pool;
20144 struct lpfc_pvt_pool *pvt_pool;
20145
20146 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
20147 pvt_pool = &multixri_pool->pvt_pool;
20148
20149 if (pvt_pool->count < pvt_pool->low_watermark)
20150 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20151}
20152
20153/**
20154 * lpfc_release_io_buf - Return one IO buf back to free pool
20155 * @phba: pointer to lpfc hba data structure.
20156 * @lpfc_ncmd: IO buf to be returned.
20157 * @qp: belong to which HWQ.
20158 *
20159 * This routine returns one IO buf back to free pool. If this is an urgent IO,
20160 * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
20161 * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
20162 * xri_limit. If cfg_xri_rebalancing==0, the IO buf is returned to
20163 * lpfc_io_buf_list_put.
20164 **/
20165void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
20166 struct lpfc_sli4_hdw_queue *qp)
20167{
20168 unsigned long iflag;
20169 struct lpfc_pbl_pool *pbl_pool;
20170 struct lpfc_pvt_pool *pvt_pool;
20171 struct lpfc_epd_pool *epd_pool;
20172 u32 txcmplq_cnt;
20173 u32 xri_owned;
20174 u32 xri_limit;
20175 u32 abts_io_bufs;
20176
20177 /* MUST zero fields if buffer is reused by another protocol */
20178 lpfc_ncmd->nvmeCmd = NULL;
20179 lpfc_ncmd->cur_iocbq.wqe_cmpl = NULL;
20180 lpfc_ncmd->cur_iocbq.iocb_cmpl = NULL;
20181
20182 if (phba->cfg_xri_rebalancing) {
20183 if (lpfc_ncmd->expedite) {
20184 /* Return to expedite pool */
20185 epd_pool = &phba->epd_pool;
20186 spin_lock_irqsave(&epd_pool->lock, iflag);
20187 list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
20188 epd_pool->count++;
20189 spin_unlock_irqrestore(&epd_pool->lock, iflag);
20190 return;
20191 }
20192
20193 /* Avoid invalid access if an IO sneaks in and is being rejected
20194 * just _after_ xri pools are destroyed in lpfc_offline.
20195 * Nothing much can be done at this point.
20196 */
20197 if (!qp->p_multixri_pool)
20198 return;
20199
20200 pbl_pool = &qp->p_multixri_pool->pbl_pool;
20201 pvt_pool = &qp->p_multixri_pool->pvt_pool;
20202
20203 txcmplq_cnt = qp->fcp_wq->pring->txcmplq_cnt;
20204 abts_io_bufs = qp->abts_scsi_io_bufs;
20205 if (qp->nvme_wq) {
20206 txcmplq_cnt += qp->nvme_wq->pring->txcmplq_cnt;
20207 abts_io_bufs += qp->abts_nvme_io_bufs;
20208 }
20209
20210 xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
20211 xri_limit = qp->p_multixri_pool->xri_limit;
20212
20213#ifdef LPFC_MXP_STAT
20214 if (xri_owned <= xri_limit)
20215 qp->p_multixri_pool->below_limit_count++;
20216 else
20217 qp->p_multixri_pool->above_limit_count++;
20218#endif
20219
20220 /* XRI goes to either public or private free xri pool
20221 * based on watermark and xri_limit
20222 */
20223 if ((pvt_pool->count < pvt_pool->low_watermark) ||
20224 (xri_owned < xri_limit &&
20225 pvt_pool->count < pvt_pool->high_watermark)) {
James Smart6a828b02019-01-28 11:14:31 -080020226 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
20227 qp, free_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020228 list_add_tail(&lpfc_ncmd->list,
20229 &pvt_pool->list);
20230 pvt_pool->count++;
20231 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20232 } else {
James Smart6a828b02019-01-28 11:14:31 -080020233 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
20234 qp, free_pub_pool);
James Smartc4908502019-01-28 11:14:28 -080020235 list_add_tail(&lpfc_ncmd->list,
20236 &pbl_pool->list);
20237 pbl_pool->count++;
20238 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20239 }
20240 } else {
James Smart6a828b02019-01-28 11:14:31 -080020241 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
20242 qp, free_xri);
James Smartc4908502019-01-28 11:14:28 -080020243 list_add_tail(&lpfc_ncmd->list,
20244 &qp->lpfc_io_buf_list_put);
20245 qp->put_io_bufs++;
20246 spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
20247 iflag);
20248 }
20249}
20250
20251/**
20252 * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
20253 * @phba: pointer to lpfc hba data structure.
20254 * @pvt_pool: pointer to private pool data structure.
20255 * @ndlp: pointer to lpfc nodelist data structure.
20256 *
20257 * This routine tries to get one free IO buf from private pool.
20258 *
20259 * Return:
20260 * pointer to one free IO buf - if private pool is not empty
20261 * NULL - if private pool is empty
20262 **/
20263static struct lpfc_io_buf *
20264lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
James Smart6a828b02019-01-28 11:14:31 -080020265 struct lpfc_sli4_hdw_queue *qp,
James Smartc4908502019-01-28 11:14:28 -080020266 struct lpfc_pvt_pool *pvt_pool,
20267 struct lpfc_nodelist *ndlp)
20268{
20269 struct lpfc_io_buf *lpfc_ncmd;
20270 struct lpfc_io_buf *lpfc_ncmd_next;
20271 unsigned long iflag;
20272
James Smart6a828b02019-01-28 11:14:31 -080020273 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020274 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20275 &pvt_pool->list, list) {
20276 if (lpfc_test_rrq_active(
20277 phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
20278 continue;
20279 list_del(&lpfc_ncmd->list);
20280 pvt_pool->count--;
20281 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20282 return lpfc_ncmd;
20283 }
20284 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20285
20286 return NULL;
20287}
20288
20289/**
20290 * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
20291 * @phba: pointer to lpfc hba data structure.
20292 *
20293 * This routine tries to get one free IO buf from expedite pool.
20294 *
20295 * Return:
20296 * pointer to one free IO buf - if expedite pool is not empty
20297 * NULL - if expedite pool is empty
20298 **/
20299static struct lpfc_io_buf *
20300lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
20301{
20302 struct lpfc_io_buf *lpfc_ncmd;
20303 struct lpfc_io_buf *lpfc_ncmd_next;
20304 unsigned long iflag;
20305 struct lpfc_epd_pool *epd_pool;
20306
20307 epd_pool = &phba->epd_pool;
20308 lpfc_ncmd = NULL;
20309
20310 spin_lock_irqsave(&epd_pool->lock, iflag);
20311 if (epd_pool->count > 0) {
20312 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20313 &epd_pool->list, list) {
20314 list_del(&lpfc_ncmd->list);
20315 epd_pool->count--;
20316 break;
20317 }
20318 }
20319 spin_unlock_irqrestore(&epd_pool->lock, iflag);
20320
20321 return lpfc_ncmd;
20322}
20323
20324/**
20325 * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
20326 * @phba: pointer to lpfc hba data structure.
20327 * @ndlp: pointer to lpfc nodelist data structure.
20328 * @hwqid: belong to which HWQ
20329 * @expedite: 1 means this request is urgent.
20330 *
20331 * This routine will do the following actions and then return a pointer to
20332 * one free IO buf.
20333 *
20334 * 1. If private free xri count is empty, move some XRIs from public to
20335 * private pool.
20336 * 2. Get one XRI from private free xri pool.
20337 * 3. If we fail to get one from pvt_pool and this is an expedite request,
20338 * get one free xri from expedite pool.
20339 *
20340 * Note: ndlp is only used on SCSI side for RRQ testing.
20341 * The caller should pass NULL for ndlp on NVME side.
20342 *
20343 * Return:
20344 * pointer to one free IO buf - if private pool is not empty
20345 * NULL - if private pool is empty
20346 **/
20347static struct lpfc_io_buf *
20348lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
20349 struct lpfc_nodelist *ndlp,
20350 int hwqid, int expedite)
20351{
20352 struct lpfc_sli4_hdw_queue *qp;
20353 struct lpfc_multixri_pool *multixri_pool;
20354 struct lpfc_pvt_pool *pvt_pool;
20355 struct lpfc_io_buf *lpfc_ncmd;
20356
20357 qp = &phba->sli4_hba.hdwq[hwqid];
20358 lpfc_ncmd = NULL;
20359 multixri_pool = qp->p_multixri_pool;
20360 pvt_pool = &multixri_pool->pvt_pool;
20361 multixri_pool->io_req_count++;
20362
20363 /* If pvt_pool is empty, move some XRIs from public to private pool */
20364 if (pvt_pool->count == 0)
20365 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20366
20367 /* Get one XRI from private free xri pool */
James Smart6a828b02019-01-28 11:14:31 -080020368 lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
James Smartc4908502019-01-28 11:14:28 -080020369
20370 if (lpfc_ncmd) {
20371 lpfc_ncmd->hdwq = qp;
20372 lpfc_ncmd->hdwq_no = hwqid;
20373 } else if (expedite) {
20374 /* If we fail to get one from pvt_pool and this is an expedite
20375 * request, get one free xri from expedite pool.
20376 */
20377 lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
20378 }
20379
20380 return lpfc_ncmd;
20381}
20382
20383static inline struct lpfc_io_buf *
20384lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
20385{
20386 struct lpfc_sli4_hdw_queue *qp;
20387 struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
20388
20389 qp = &phba->sli4_hba.hdwq[idx];
20390 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
20391 &qp->lpfc_io_buf_list_get, list) {
20392 if (lpfc_test_rrq_active(phba, ndlp,
20393 lpfc_cmd->cur_iocbq.sli4_lxritag))
20394 continue;
20395
20396 if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
20397 continue;
20398
20399 list_del_init(&lpfc_cmd->list);
20400 qp->get_io_bufs--;
20401 lpfc_cmd->hdwq = qp;
20402 lpfc_cmd->hdwq_no = idx;
20403 return lpfc_cmd;
20404 }
20405 return NULL;
20406}
20407
20408/**
20409 * lpfc_get_io_buf - Get one IO buffer from free pool
20410 * @phba: The HBA for which this call is being executed.
20411 * @ndlp: pointer to lpfc nodelist data structure.
20412 * @hwqid: belong to which HWQ
20413 * @expedite: 1 means this request is urgent.
20414 *
20415 * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
20416 * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
20417 * a IO buffer from head of @hdwq io_buf_list and returns to caller.
20418 *
20419 * Note: ndlp is only used on SCSI side for RRQ testing.
20420 * The caller should pass NULL for ndlp on NVME side.
20421 *
20422 * Return codes:
20423 * NULL - Error
20424 * Pointer to lpfc_io_buf - Success
20425 **/
20426struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
20427 struct lpfc_nodelist *ndlp,
20428 u32 hwqid, int expedite)
20429{
20430 struct lpfc_sli4_hdw_queue *qp;
20431 unsigned long iflag;
20432 struct lpfc_io_buf *lpfc_cmd;
20433
20434 qp = &phba->sli4_hba.hdwq[hwqid];
20435 lpfc_cmd = NULL;
20436
20437 if (phba->cfg_xri_rebalancing)
20438 lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
20439 phba, ndlp, hwqid, expedite);
20440 else {
James Smart6a828b02019-01-28 11:14:31 -080020441 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
20442 qp, alloc_xri_get);
James Smartc4908502019-01-28 11:14:28 -080020443 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
20444 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
20445 if (!lpfc_cmd) {
James Smart6a828b02019-01-28 11:14:31 -080020446 lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
20447 qp, alloc_xri_put);
James Smartc4908502019-01-28 11:14:28 -080020448 list_splice(&qp->lpfc_io_buf_list_put,
20449 &qp->lpfc_io_buf_list_get);
20450 qp->get_io_bufs += qp->put_io_bufs;
20451 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
20452 qp->put_io_bufs = 0;
20453 spin_unlock(&qp->io_buf_list_put_lock);
20454 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
20455 expedite)
20456 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
20457 }
20458 spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
20459 }
20460
20461 return lpfc_cmd;
20462}