blob: 313441a3c4cff648bbf9a6b0f852a51282c5d3cb [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 Smart05580562011-05-24 11:40:48 -040090
James Smart4f774512009-05-22 14:52:35 -040091static IOCB_t *
92lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
93{
94 return &iocbq->iocb;
95}
96
James Smart48f8fdb2018-05-04 20:37:51 -070097#if defined(CONFIG_64BIT) && defined(__LITTLE_ENDIAN)
98/**
99 * lpfc_sli4_pcimem_bcopy - SLI4 memory copy function
100 * @srcp: Source memory pointer.
101 * @destp: Destination memory pointer.
102 * @cnt: Number of words required to be copied.
103 * Must be a multiple of sizeof(uint64_t)
104 *
105 * This function is used for copying data between driver memory
106 * and the SLI WQ. This function also changes the endianness
107 * of each word if native endianness is different from SLI
108 * endianness. This function can be called with or without
109 * lock.
110 **/
YueHaibingd7b761b2019-05-31 23:28:41 +0800111static void
James Smart48f8fdb2018-05-04 20:37:51 -0700112lpfc_sli4_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
113{
114 uint64_t *src = srcp;
115 uint64_t *dest = destp;
116 int i;
117
118 for (i = 0; i < (int)cnt; i += sizeof(uint64_t))
119 *dest++ = *src++;
120}
121#else
122#define lpfc_sli4_pcimem_bcopy(a, b, c) lpfc_sli_pcimem_bcopy(a, b, c)
123#endif
124
James Smart4f774512009-05-22 14:52:35 -0400125/**
126 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
127 * @q: The Work Queue to operate on.
128 * @wqe: The work Queue Entry to put on the Work queue.
129 *
130 * This routine will copy the contents of @wqe to the next available entry on
131 * the @q. This function will then ring the Work Queue Doorbell to signal the
132 * HBA to start processing the Work Queue Entry. This function returns 0 if
133 * successful. If no entries are available on @q then this function will return
134 * -ENOMEM.
135 * The caller is expected to hold the hbalock when calling this routine.
136 **/
Dick Kennedycd22d602017-08-23 16:55:35 -0700137static int
James Smart205e8242018-03-05 12:04:03 -0800138lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -0400139{
James Smart2e90f4b2011-12-13 13:22:37 -0500140 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400141 struct lpfc_register doorbell;
142 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400143 uint32_t idx;
James Smart1351e692018-02-22 08:18:43 -0800144 uint32_t i = 0;
145 uint8_t *tmp;
James Smart5cc167d2018-06-26 08:24:23 -0700146 u32 if_type;
James Smart4f774512009-05-22 14:52:35 -0400147
James Smart2e90f4b2011-12-13 13:22:37 -0500148 /* sanity check on queue memory */
149 if (unlikely(!q))
150 return -ENOMEM;
James Smart9afbee32019-03-12 16:30:28 -0700151 temp_wqe = lpfc_sli4_qe(q, q->host_index);
James Smart2e90f4b2011-12-13 13:22:37 -0500152
James Smart4f774512009-05-22 14:52:35 -0400153 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400154 idx = ((q->host_index + 1) % q->entry_count);
155 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400156 q->WQ_overflow++;
Dick Kennedycd22d602017-08-23 16:55:35 -0700157 return -EBUSY;
James Smartb84daac2012-08-03 12:35:13 -0400158 }
159 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400160 /* set consumption flag every once in a while */
James Smart32517fc2019-01-28 11:14:33 -0800161 if (!((q->host_index + 1) % q->notify_interval))
James Smartf0d9bcc2010-10-22 11:07:09 -0400162 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smart04673e32018-01-30 15:58:45 -0800163 else
164 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
James Smartfedd3b72011-02-16 12:39:24 -0500165 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
166 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart48f8fdb2018-05-04 20:37:51 -0700167 lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
James Smart1351e692018-02-22 08:18:43 -0800168 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
169 /* write to DPP aperture taking advatage of Combined Writes */
James Smart4c066192018-03-05 10:29:03 -0800170 tmp = (uint8_t *)temp_wqe;
171#ifdef __raw_writeq
James Smart1351e692018-02-22 08:18:43 -0800172 for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
James Smart4c066192018-03-05 10:29:03 -0800173 __raw_writeq(*((uint64_t *)(tmp + i)),
174 q->dpp_regaddr + i);
175#else
176 for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
177 __raw_writel(*((uint32_t *)(tmp + i)),
178 q->dpp_regaddr + i);
179#endif
James Smart1351e692018-02-22 08:18:43 -0800180 }
181 /* ensure WQE bcopy and DPP flushed before doorbell write */
James Smart6b3b3bd2016-12-19 15:07:30 -0800182 wmb();
James Smart4f774512009-05-22 14:52:35 -0400183
184 /* Update the host index before invoking device */
185 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400186
187 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400188
189 /* Ring Doorbell */
190 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500191 if (q->db_format == LPFC_DB_LIST_FORMAT) {
James Smart1351e692018-02-22 08:18:43 -0800192 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
193 bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
194 bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
195 bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
196 q->dpp_id);
197 bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
198 q->queue_id);
199 } else {
200 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
James Smart1351e692018-02-22 08:18:43 -0800201 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
James Smart5cc167d2018-06-26 08:24:23 -0700202
203 /* Leave bits <23:16> clear for if_type 6 dpp */
204 if_type = bf_get(lpfc_sli_intf_if_type,
205 &q->phba->sli4_hba.sli_intf);
206 if (if_type != LPFC_SLI_INTF_IF_TYPE_6)
207 bf_set(lpfc_wq_db_list_fm_index, &doorbell,
208 host_index);
James Smart1351e692018-02-22 08:18:43 -0800209 }
James Smart962bc512013-01-03 15:44:00 -0500210 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
211 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
212 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
213 } else {
214 return -EINVAL;
215 }
216 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400217
218 return 0;
219}
220
221/**
222 * lpfc_sli4_wq_release - Updates internal hba index for WQ
223 * @q: The Work Queue to operate on.
224 * @index: The index to advance the hba index to.
225 *
226 * This routine will update the HBA index of a queue to reflect consumption of
227 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
228 * an entry the host calls this function to update the queue's internal
229 * pointers. This routine returns the number of entries that were consumed by
230 * the HBA.
231 **/
232static uint32_t
233lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
234{
235 uint32_t released = 0;
236
James Smart2e90f4b2011-12-13 13:22:37 -0500237 /* sanity check on queue memory */
238 if (unlikely(!q))
239 return 0;
240
James Smart4f774512009-05-22 14:52:35 -0400241 if (q->hba_index == index)
242 return 0;
243 do {
244 q->hba_index = ((q->hba_index + 1) % q->entry_count);
245 released++;
246 } while (q->hba_index != index);
247 return released;
248}
249
250/**
251 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
252 * @q: The Mailbox Queue to operate on.
253 * @wqe: The Mailbox Queue Entry to put on the Work queue.
254 *
255 * This routine will copy the contents of @mqe to the next available entry on
256 * the @q. This function will then ring the Work Queue Doorbell to signal the
257 * HBA to start processing the Work Queue Entry. This function returns 0 if
258 * successful. If no entries are available on @q then this function will return
259 * -ENOMEM.
260 * The caller is expected to hold the hbalock when calling this routine.
261 **/
262static uint32_t
263lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
264{
James Smart2e90f4b2011-12-13 13:22:37 -0500265 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400266 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400267
James Smart2e90f4b2011-12-13 13:22:37 -0500268 /* sanity check on queue memory */
269 if (unlikely(!q))
270 return -ENOMEM;
James Smart9afbee32019-03-12 16:30:28 -0700271 temp_mqe = lpfc_sli4_qe(q, q->host_index);
James Smart2e90f4b2011-12-13 13:22:37 -0500272
James Smart4f774512009-05-22 14:52:35 -0400273 /* If the host has not yet processed the next entry then we are done */
274 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
275 return -ENOMEM;
James Smart48f8fdb2018-05-04 20:37:51 -0700276 lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
James Smart4f774512009-05-22 14:52:35 -0400277 /* Save off the mailbox pointer for completion */
278 q->phba->mbox = (MAILBOX_t *)temp_mqe;
279
280 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400281 q->host_index = ((q->host_index + 1) % q->entry_count);
282
283 /* Ring Doorbell */
284 doorbell.word0 = 0;
285 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
286 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
287 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400288 return 0;
289}
290
291/**
292 * lpfc_sli4_mq_release - Updates internal hba index for MQ
293 * @q: The Mailbox Queue to operate on.
294 *
295 * This routine will update the HBA index of a queue to reflect consumption of
296 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
297 * an entry the host calls this function to update the queue's internal
298 * pointers. This routine returns the number of entries that were consumed by
299 * the HBA.
300 **/
301static uint32_t
302lpfc_sli4_mq_release(struct lpfc_queue *q)
303{
James Smart2e90f4b2011-12-13 13:22:37 -0500304 /* sanity check on queue memory */
305 if (unlikely(!q))
306 return 0;
307
James Smart4f774512009-05-22 14:52:35 -0400308 /* Clear the mailbox pointer for completion */
309 q->phba->mbox = NULL;
310 q->hba_index = ((q->hba_index + 1) % q->entry_count);
311 return 1;
312}
313
314/**
315 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
316 * @q: The Event Queue to get the first valid EQE from
317 *
318 * This routine will get the first valid Event Queue Entry from @q, update
319 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
320 * the Queue (no more work to do), or the Queue is full of EQEs that have been
321 * processed, but not popped back to the HBA then this routine will return NULL.
322 **/
323static struct lpfc_eqe *
324lpfc_sli4_eq_get(struct lpfc_queue *q)
325{
James Smart2e90f4b2011-12-13 13:22:37 -0500326 struct lpfc_eqe *eqe;
327
328 /* sanity check on queue memory */
329 if (unlikely(!q))
330 return NULL;
James Smart9afbee32019-03-12 16:30:28 -0700331 eqe = lpfc_sli4_qe(q, q->host_index);
James Smart4f774512009-05-22 14:52:35 -0400332
333 /* If the next EQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800334 if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400335 return NULL;
James Smart27f344e2014-05-07 17:16:46 -0400336
337 /*
338 * insert barrier for instruction interlock : data from the hardware
339 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800340 * upon. Speculative instructions were allowing a bcopy at the start
341 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
342 * after our return, to copy data before the valid bit check above
343 * was done. As such, some of the copied data was stale. The barrier
344 * ensures the check is before any data is copied.
James Smart27f344e2014-05-07 17:16:46 -0400345 */
346 mb();
James Smart4f774512009-05-22 14:52:35 -0400347 return eqe;
348}
349
350/**
James Smartba20c852012-08-03 12:36:52 -0400351 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
352 * @q: The Event Queue to disable interrupts
353 *
354 **/
James Smart92f3b322019-03-20 10:44:22 -0700355void
James Smartba20c852012-08-03 12:36:52 -0400356lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
357{
358 struct lpfc_register doorbell;
359
360 doorbell.word0 = 0;
361 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
362 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
363 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
364 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
365 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800366 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smartba20c852012-08-03 12:36:52 -0400367}
368
369/**
James Smart27d6ac02018-02-22 08:18:42 -0800370 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
371 * @q: The Event Queue to disable interrupts
372 *
373 **/
James Smart92f3b322019-03-20 10:44:22 -0700374void
James Smart27d6ac02018-02-22 08:18:42 -0800375lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
376{
377 struct lpfc_register doorbell;
378
379 doorbell.word0 = 0;
James Smartaad59d52018-09-10 10:30:47 -0700380 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
James Smart27d6ac02018-02-22 08:18:42 -0800381 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
382}
383
384/**
James Smart32517fc2019-01-28 11:14:33 -0800385 * lpfc_sli4_write_eq_db - write EQ DB for eqe's consumed or arm state
386 * @phba: adapter with EQ
James Smart4f774512009-05-22 14:52:35 -0400387 * @q: The Event Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800388 * @count: Number of elements that have been consumed
James Smart4f774512009-05-22 14:52:35 -0400389 * @arm: Indicates whether the host wants to arms this CQ.
390 *
James Smart32517fc2019-01-28 11:14:33 -0800391 * This routine will notify the HBA, by ringing the doorbell, that count
392 * number of EQEs have been processed. The @arm parameter indicates whether
393 * the queue should be rearmed when ringing the doorbell.
James Smart4f774512009-05-22 14:52:35 -0400394 **/
James Smart32517fc2019-01-28 11:14:33 -0800395void
396lpfc_sli4_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
397 uint32_t count, bool arm)
James Smart4f774512009-05-22 14:52:35 -0400398{
James Smart4f774512009-05-22 14:52:35 -0400399 struct lpfc_register doorbell;
400
James Smart2e90f4b2011-12-13 13:22:37 -0500401 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800402 if (unlikely(!q || (count == 0 && !arm)))
403 return;
James Smart4f774512009-05-22 14:52:35 -0400404
405 /* ring doorbell for number popped */
406 doorbell.word0 = 0;
407 if (arm) {
408 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
409 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
410 }
James Smart32517fc2019-01-28 11:14:33 -0800411 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
James Smart4f774512009-05-22 14:52:35 -0400412 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500413 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
414 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
415 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800416 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500417 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
418 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
James Smart9dd35422018-02-22 08:18:41 -0800419 readl(q->phba->sli4_hba.EQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400420}
421
422/**
James Smart32517fc2019-01-28 11:14:33 -0800423 * lpfc_sli4_if6_write_eq_db - write EQ DB for eqe's consumed or arm state
424 * @phba: adapter with EQ
James Smart27d6ac02018-02-22 08:18:42 -0800425 * @q: The Event Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800426 * @count: Number of elements that have been consumed
James Smart27d6ac02018-02-22 08:18:42 -0800427 * @arm: Indicates whether the host wants to arms this CQ.
428 *
James Smart32517fc2019-01-28 11:14:33 -0800429 * This routine will notify the HBA, by ringing the doorbell, that count
430 * number of EQEs have been processed. The @arm parameter indicates whether
431 * the queue should be rearmed when ringing the doorbell.
James Smart27d6ac02018-02-22 08:18:42 -0800432 **/
James Smart32517fc2019-01-28 11:14:33 -0800433void
434lpfc_sli4_if6_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
435 uint32_t count, bool arm)
James Smart27d6ac02018-02-22 08:18:42 -0800436{
James Smart27d6ac02018-02-22 08:18:42 -0800437 struct lpfc_register doorbell;
438
439 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800440 if (unlikely(!q || (count == 0 && !arm)))
441 return;
James Smart27d6ac02018-02-22 08:18:42 -0800442
443 /* ring doorbell for number popped */
444 doorbell.word0 = 0;
445 if (arm)
446 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
James Smart32517fc2019-01-28 11:14:33 -0800447 bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, count);
James Smart27d6ac02018-02-22 08:18:42 -0800448 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
449 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
450 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
451 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
452 readl(q->phba->sli4_hba.EQDBregaddr);
James Smart32517fc2019-01-28 11:14:33 -0800453}
454
455static void
456__lpfc_sli4_consume_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
457 struct lpfc_eqe *eqe)
458{
459 if (!phba->sli4_hba.pc_sli4_params.eqav)
460 bf_set_le32(lpfc_eqe_valid, eqe, 0);
461
462 eq->host_index = ((eq->host_index + 1) % eq->entry_count);
463
464 /* if the index wrapped around, toggle the valid bit */
465 if (phba->sli4_hba.pc_sli4_params.eqav && !eq->host_index)
466 eq->qe_valid = (eq->qe_valid) ? 0 : 1;
467}
468
469static void
470lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
471{
472 struct lpfc_eqe *eqe;
473 uint32_t count = 0;
474
475 /* walk all the EQ entries and drop on the floor */
476 eqe = lpfc_sli4_eq_get(eq);
477 while (eqe) {
478 __lpfc_sli4_consume_eqe(phba, eq, eqe);
479 count++;
480 eqe = lpfc_sli4_eq_get(eq);
481 }
482
483 /* Clear and re-arm the EQ */
484 phba->sli4_hba.sli4_write_eq_db(phba, eq, count, LPFC_QUEUE_REARM);
485}
486
487static int
488lpfc_sli4_process_eq(struct lpfc_hba *phba, struct lpfc_queue *eq)
489{
490 struct lpfc_eqe *eqe;
491 int count = 0, consumed = 0;
492
493 if (cmpxchg(&eq->queue_claimed, 0, 1) != 0)
494 goto rearm_and_exit;
495
496 eqe = lpfc_sli4_eq_get(eq);
497 while (eqe) {
498 lpfc_sli4_hba_handle_eqe(phba, eq, eqe);
499 __lpfc_sli4_consume_eqe(phba, eq, eqe);
500
501 consumed++;
502 if (!(++count % eq->max_proc_limit))
503 break;
504
505 if (!(count % eq->notify_interval)) {
506 phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed,
507 LPFC_QUEUE_NOARM);
508 consumed = 0;
509 }
510
511 eqe = lpfc_sli4_eq_get(eq);
512 }
513 eq->EQ_processed += count;
514
515 /* Track the max number of EQEs processed in 1 intr */
516 if (count > eq->EQ_max_eqe)
517 eq->EQ_max_eqe = count;
518
519 eq->queue_claimed = 0;
520
521rearm_and_exit:
522 /* Always clear and re-arm the EQ */
523 phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed, LPFC_QUEUE_REARM);
524
525 return count;
James Smart27d6ac02018-02-22 08:18:42 -0800526}
527
528/**
James Smart4f774512009-05-22 14:52:35 -0400529 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
530 * @q: The Completion Queue to get the first valid CQE from
531 *
532 * This routine will get the first valid Completion Queue Entry from @q, update
533 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
534 * the Queue (no more work to do), or the Queue is full of CQEs that have been
535 * processed, but not popped back to the HBA then this routine will return NULL.
536 **/
537static struct lpfc_cqe *
538lpfc_sli4_cq_get(struct lpfc_queue *q)
539{
540 struct lpfc_cqe *cqe;
541
James Smart2e90f4b2011-12-13 13:22:37 -0500542 /* sanity check on queue memory */
543 if (unlikely(!q))
544 return NULL;
James Smart9afbee32019-03-12 16:30:28 -0700545 cqe = lpfc_sli4_qe(q, q->host_index);
James Smart2e90f4b2011-12-13 13:22:37 -0500546
James Smart4f774512009-05-22 14:52:35 -0400547 /* If the next CQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800548 if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400549 return NULL;
James Smart27f344e2014-05-07 17:16:46 -0400550
551 /*
552 * insert barrier for instruction interlock : data from the hardware
553 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800554 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
555 * instructions allowing action on content before valid bit checked,
556 * add barrier here as well. May not be needed as "content" is a
557 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400558 */
559 mb();
James Smart4f774512009-05-22 14:52:35 -0400560 return cqe;
561}
562
James Smart32517fc2019-01-28 11:14:33 -0800563static void
564__lpfc_sli4_consume_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
565 struct lpfc_cqe *cqe)
566{
567 if (!phba->sli4_hba.pc_sli4_params.cqav)
568 bf_set_le32(lpfc_cqe_valid, cqe, 0);
569
570 cq->host_index = ((cq->host_index + 1) % cq->entry_count);
571
572 /* if the index wrapped around, toggle the valid bit */
573 if (phba->sli4_hba.pc_sli4_params.cqav && !cq->host_index)
574 cq->qe_valid = (cq->qe_valid) ? 0 : 1;
575}
576
James Smart4f774512009-05-22 14:52:35 -0400577/**
James Smart32517fc2019-01-28 11:14:33 -0800578 * lpfc_sli4_write_cq_db - write cq DB for entries consumed or arm state.
579 * @phba: the adapter with the CQ
James Smart4f774512009-05-22 14:52:35 -0400580 * @q: The Completion Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800581 * @count: the number of elements that were consumed
James Smart4f774512009-05-22 14:52:35 -0400582 * @arm: Indicates whether the host wants to arms this CQ.
583 *
James Smart32517fc2019-01-28 11:14:33 -0800584 * This routine will notify the HBA, by ringing the doorbell, that the
585 * CQEs have been processed. The @arm parameter specifies whether the
586 * queue should be rearmed when ringing the doorbell.
James Smart4f774512009-05-22 14:52:35 -0400587 **/
James Smart32517fc2019-01-28 11:14:33 -0800588void
589lpfc_sli4_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
590 uint32_t count, bool arm)
James Smart4f774512009-05-22 14:52:35 -0400591{
James Smart4f774512009-05-22 14:52:35 -0400592 struct lpfc_register doorbell;
593
James Smart2e90f4b2011-12-13 13:22:37 -0500594 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800595 if (unlikely(!q || (count == 0 && !arm)))
596 return;
James Smart4f774512009-05-22 14:52:35 -0400597
598 /* ring doorbell for number popped */
599 doorbell.word0 = 0;
600 if (arm)
601 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
James Smart32517fc2019-01-28 11:14:33 -0800602 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
James Smart4f774512009-05-22 14:52:35 -0400603 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500604 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
605 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
606 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800607 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400608}
609
610/**
James Smart32517fc2019-01-28 11:14:33 -0800611 * lpfc_sli4_if6_write_cq_db - write cq DB for entries consumed or arm state.
612 * @phba: the adapter with the CQ
James Smart27d6ac02018-02-22 08:18:42 -0800613 * @q: The Completion Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800614 * @count: the number of elements that were consumed
James Smart27d6ac02018-02-22 08:18:42 -0800615 * @arm: Indicates whether the host wants to arms this CQ.
616 *
James Smart32517fc2019-01-28 11:14:33 -0800617 * This routine will notify the HBA, by ringing the doorbell, that the
618 * CQEs have been processed. The @arm parameter specifies whether the
619 * queue should be rearmed when ringing the doorbell.
James Smart27d6ac02018-02-22 08:18:42 -0800620 **/
James Smart32517fc2019-01-28 11:14:33 -0800621void
622lpfc_sli4_if6_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
623 uint32_t count, bool arm)
James Smart27d6ac02018-02-22 08:18:42 -0800624{
James Smart27d6ac02018-02-22 08:18:42 -0800625 struct lpfc_register doorbell;
626
627 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800628 if (unlikely(!q || (count == 0 && !arm)))
629 return;
James Smart27d6ac02018-02-22 08:18:42 -0800630
631 /* ring doorbell for number popped */
632 doorbell.word0 = 0;
633 if (arm)
634 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
James Smart32517fc2019-01-28 11:14:33 -0800635 bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, count);
James Smart27d6ac02018-02-22 08:18:42 -0800636 bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
637 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
James Smart27d6ac02018-02-22 08:18:42 -0800638}
639
640/**
James Smart4f774512009-05-22 14:52:35 -0400641 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
642 * @q: The Header Receive Queue to operate on.
643 * @wqe: The Receive Queue Entry to put on the Receive queue.
644 *
645 * This routine will copy the contents of @wqe to the next available entry on
646 * the @q. This function will then ring the Receive Queue Doorbell to signal the
647 * HBA to start processing the Receive Queue Entry. This function returns the
648 * index that the rqe was copied to if successful. If no entries are available
649 * on @q then this function will return -ENOMEM.
650 * The caller is expected to hold the hbalock when calling this routine.
651 **/
James Smart895427b2017-02-12 13:52:30 -0800652int
James Smart4f774512009-05-22 14:52:35 -0400653lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
654 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
655{
James Smart2e90f4b2011-12-13 13:22:37 -0500656 struct lpfc_rqe *temp_hrqe;
657 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400658 struct lpfc_register doorbell;
James Smartcbc5de12017-12-08 17:18:04 -0800659 int hq_put_index;
660 int dq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400661
James Smart2e90f4b2011-12-13 13:22:37 -0500662 /* sanity check on queue memory */
663 if (unlikely(!hq) || unlikely(!dq))
664 return -ENOMEM;
James Smartcbc5de12017-12-08 17:18:04 -0800665 hq_put_index = hq->host_index;
666 dq_put_index = dq->host_index;
James Smart9afbee32019-03-12 16:30:28 -0700667 temp_hrqe = lpfc_sli4_qe(hq, hq_put_index);
668 temp_drqe = lpfc_sli4_qe(dq, dq_put_index);
James Smart2e90f4b2011-12-13 13:22:37 -0500669
James Smart4f774512009-05-22 14:52:35 -0400670 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
671 return -EINVAL;
James Smartcbc5de12017-12-08 17:18:04 -0800672 if (hq_put_index != dq_put_index)
James Smart4f774512009-05-22 14:52:35 -0400673 return -EINVAL;
674 /* If the host has not yet processed the next entry then we are done */
James Smartcbc5de12017-12-08 17:18:04 -0800675 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
James Smart4f774512009-05-22 14:52:35 -0400676 return -EBUSY;
James Smart48f8fdb2018-05-04 20:37:51 -0700677 lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
678 lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
James Smart4f774512009-05-22 14:52:35 -0400679
680 /* Update the host index to point to the next slot */
James Smartcbc5de12017-12-08 17:18:04 -0800681 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
682 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
James Smart61f3d4b2017-05-15 15:20:41 -0700683 hq->RQ_buf_posted++;
James Smart4f774512009-05-22 14:52:35 -0400684
685 /* Ring The Header Receive Queue Doorbell */
James Smart32517fc2019-01-28 11:14:33 -0800686 if (!(hq->host_index % hq->notify_interval)) {
James Smart4f774512009-05-22 14:52:35 -0400687 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500688 if (hq->db_format == LPFC_DB_RING_FORMAT) {
689 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
James Smart32517fc2019-01-28 11:14:33 -0800690 hq->notify_interval);
James Smart962bc512013-01-03 15:44:00 -0500691 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
692 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
693 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
James Smart32517fc2019-01-28 11:14:33 -0800694 hq->notify_interval);
James Smart962bc512013-01-03 15:44:00 -0500695 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
696 hq->host_index);
697 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
698 } else {
699 return -EINVAL;
700 }
701 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400702 }
James Smartcbc5de12017-12-08 17:18:04 -0800703 return hq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400704}
705
706/**
707 * lpfc_sli4_rq_release - Updates internal hba index for RQ
708 * @q: The Header Receive Queue to operate on.
709 *
710 * This routine will update the HBA index of a queue to reflect consumption of
711 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
712 * consumed an entry the host calls this function to update the queue's
713 * internal pointers. This routine returns the number of entries that were
714 * consumed by the HBA.
715 **/
716static uint32_t
717lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
718{
James Smart2e90f4b2011-12-13 13:22:37 -0500719 /* sanity check on queue memory */
720 if (unlikely(!hq) || unlikely(!dq))
721 return 0;
722
James Smart4f774512009-05-22 14:52:35 -0400723 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
724 return 0;
725 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
726 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
727 return 1;
728}
729
James Smarte59058c2008-08-24 21:49:00 -0400730/**
James Smart3621a712009-04-06 18:47:14 -0400731 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400732 * @phba: Pointer to HBA context object.
733 * @pring: Pointer to driver SLI ring object.
734 *
735 * This function returns pointer to next command iocb entry
736 * in the command ring. The caller must hold hbalock to prevent
737 * other threads consume the next command iocb.
738 * SLI-2/SLI-3 provide different sized iocbs.
739 **/
James Smarted957682007-06-17 19:56:37 -0500740static inline IOCB_t *
741lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
742{
James Smart7e56aa22012-08-03 12:35:34 -0400743 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
744 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500745}
746
James Smarte59058c2008-08-24 21:49:00 -0400747/**
James Smart3621a712009-04-06 18:47:14 -0400748 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400749 * @phba: Pointer to HBA context object.
750 * @pring: Pointer to driver SLI ring object.
751 *
752 * This function returns pointer to next response iocb entry
753 * in the response ring. The caller must hold hbalock to make sure
754 * that no other thread consume the next response iocb.
755 * SLI-2/SLI-3 provide different sized iocbs.
756 **/
James Smarted957682007-06-17 19:56:37 -0500757static inline IOCB_t *
758lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
759{
James Smart7e56aa22012-08-03 12:35:34 -0400760 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
761 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500762}
763
James Smarte59058c2008-08-24 21:49:00 -0400764/**
James Smart3621a712009-04-06 18:47:14 -0400765 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400766 * @phba: Pointer to HBA context object.
767 *
768 * This function is called with hbalock held. This function
769 * allocates a new driver iocb object from the iocb pool. If the
770 * allocation is successful, it returns pointer to the newly
771 * allocated iocb object else it returns NULL.
772 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400773struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500774__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400775{
776 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
777 struct lpfc_iocbq * iocbq = NULL;
778
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100779 lockdep_assert_held(&phba->hbalock);
780
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400781 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400782 if (iocbq)
783 phba->iocb_cnt++;
784 if (phba->iocb_cnt > phba->iocb_max)
785 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400786 return iocbq;
787}
788
James Smarte59058c2008-08-24 21:49:00 -0400789/**
James Smartda0436e2009-05-22 14:51:39 -0400790 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
791 * @phba: Pointer to HBA context object.
792 * @xritag: XRI value.
793 *
794 * This function clears the sglq pointer from the array of acive
795 * sglq's. The xritag that is passed in is used to index into the
796 * array. Before the xritag can be used it needs to be adjusted
797 * by subtracting the xribase.
798 *
799 * Returns sglq ponter = success, NULL = Failure.
800 **/
James Smart895427b2017-02-12 13:52:30 -0800801struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400802__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
803{
James Smartda0436e2009-05-22 14:51:39 -0400804 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400805
806 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
807 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400808 return sglq;
809}
810
811/**
812 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
813 * @phba: Pointer to HBA context object.
814 * @xritag: XRI value.
815 *
816 * This function returns the sglq pointer from the array of acive
817 * sglq's. The xritag that is passed in is used to index into the
818 * array. Before the xritag can be used it needs to be adjusted
819 * by subtracting the xribase.
820 *
821 * Returns sglq ponter = success, NULL = Failure.
822 **/
James Smart0f65ff62010-02-26 14:14:23 -0500823struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400824__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
825{
James Smartda0436e2009-05-22 14:51:39 -0400826 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400827
828 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400829 return sglq;
830}
831
832/**
James Smart1151e3e2011-02-16 12:39:35 -0500833 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500834 * @phba: Pointer to HBA context object.
835 * @xritag: xri used in this exchange.
836 * @rrq: The RRQ to be cleared.
837 *
James Smart19ca7602010-11-20 23:11:55 -0500838 **/
James Smart1151e3e2011-02-16 12:39:35 -0500839void
840lpfc_clr_rrq_active(struct lpfc_hba *phba,
841 uint16_t xritag,
842 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500843{
James Smart1151e3e2011-02-16 12:39:35 -0500844 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500845
James Smart1151e3e2011-02-16 12:39:35 -0500846 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
847 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500848
849 /* The target DID could have been swapped (cable swap)
850 * we should use the ndlp from the findnode if it is
851 * available.
852 */
James Smart1151e3e2011-02-16 12:39:35 -0500853 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500854 ndlp = rrq->ndlp;
855
James Smart1151e3e2011-02-16 12:39:35 -0500856 if (!ndlp)
857 goto out;
858
James Smartcff261f2013-12-17 20:29:47 -0500859 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500860 rrq->send_rrq = 0;
861 rrq->xritag = 0;
862 rrq->rrq_stop_time = 0;
863 }
James Smart1151e3e2011-02-16 12:39:35 -0500864out:
James Smart19ca7602010-11-20 23:11:55 -0500865 mempool_free(rrq, phba->rrq_pool);
866}
867
868/**
869 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
870 * @phba: Pointer to HBA context object.
871 *
872 * This function is called with hbalock held. This function
873 * Checks if stop_time (ratov from setting rrq active) has
874 * been reached, if it has and the send_rrq flag is set then
875 * it will call lpfc_send_rrq. If the send_rrq flag is not set
876 * then it will just call the routine to clear the rrq and
877 * free the rrq resource.
878 * The timer is set to the next rrq that is going to expire before
879 * leaving the routine.
880 *
881 **/
882void
883lpfc_handle_rrq_active(struct lpfc_hba *phba)
884{
885 struct lpfc_node_rrq *rrq;
886 struct lpfc_node_rrq *nextrrq;
887 unsigned long next_time;
888 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500889 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500890
891 spin_lock_irqsave(&phba->hbalock, iflags);
892 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400893 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500894 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500895 &phba->active_rrq_list, list) {
896 if (time_after(jiffies, rrq->rrq_stop_time))
897 list_move(&rrq->list, &send_rrq);
898 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500899 next_time = rrq->rrq_stop_time;
900 }
901 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500902 if ((!list_empty(&phba->active_rrq_list)) &&
903 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500904 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500905 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
906 list_del(&rrq->list);
Bart Van Asscheffd43812019-03-28 11:06:17 -0700907 if (!rrq->send_rrq) {
James Smart1151e3e2011-02-16 12:39:35 -0500908 /* this call will free the rrq */
Bart Van Asscheffd43812019-03-28 11:06:17 -0700909 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
910 } else if (lpfc_send_rrq(phba, rrq)) {
James Smart1151e3e2011-02-16 12:39:35 -0500911 /* if we send the rrq then the completion handler
912 * will clear the bit in the xribitmap.
913 */
914 lpfc_clr_rrq_active(phba, rrq->xritag,
915 rrq);
916 }
917 }
James Smart19ca7602010-11-20 23:11:55 -0500918}
919
920/**
921 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
922 * @vport: Pointer to vport context object.
923 * @xri: The xri used in the exchange.
924 * @did: The targets DID for this exchange.
925 *
926 * returns NULL = rrq not found in the phba->active_rrq_list.
927 * rrq = rrq for this xri and target.
928 **/
929struct lpfc_node_rrq *
930lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
931{
932 struct lpfc_hba *phba = vport->phba;
933 struct lpfc_node_rrq *rrq;
934 struct lpfc_node_rrq *nextrrq;
935 unsigned long iflags;
936
937 if (phba->sli_rev != LPFC_SLI_REV4)
938 return NULL;
939 spin_lock_irqsave(&phba->hbalock, iflags);
940 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
941 if (rrq->vport == vport && rrq->xritag == xri &&
942 rrq->nlp_DID == did){
943 list_del(&rrq->list);
944 spin_unlock_irqrestore(&phba->hbalock, iflags);
945 return rrq;
946 }
947 }
948 spin_unlock_irqrestore(&phba->hbalock, iflags);
949 return NULL;
950}
951
952/**
953 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
954 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500955 * @ndlp: Pointer to the lpfc_node_list structure.
956 * If ndlp is NULL Remove all active RRQs for this vport from the
957 * phba->active_rrq_list and clear the rrq.
958 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500959 **/
960void
James Smart1151e3e2011-02-16 12:39:35 -0500961lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500962
963{
964 struct lpfc_hba *phba = vport->phba;
965 struct lpfc_node_rrq *rrq;
966 struct lpfc_node_rrq *nextrrq;
967 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500968 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500969
970 if (phba->sli_rev != LPFC_SLI_REV4)
971 return;
James Smart1151e3e2011-02-16 12:39:35 -0500972 if (!ndlp) {
973 lpfc_sli4_vport_delete_els_xri_aborted(vport);
974 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500975 }
James Smart1151e3e2011-02-16 12:39:35 -0500976 spin_lock_irqsave(&phba->hbalock, iflags);
977 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
978 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
979 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500980 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500981
982 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
983 list_del(&rrq->list);
984 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
985 }
James Smart19ca7602010-11-20 23:11:55 -0500986}
987
988/**
James Smart1151e3e2011-02-16 12:39:35 -0500989 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500990 * @phba: Pointer to HBA context object.
991 * @ndlp: Targets nodelist pointer for this exchange.
992 * @xritag the xri in the bitmap to test.
993 *
James Smarte2a8be52019-05-06 17:26:47 -0700994 * This function returns:
995 * 0 = rrq not active for this xri
996 * 1 = rrq is valid for this xri.
James Smart19ca7602010-11-20 23:11:55 -0500997 **/
James Smart1151e3e2011-02-16 12:39:35 -0500998int
999lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -05001000 uint16_t xritag)
1001{
James Smart19ca7602010-11-20 23:11:55 -05001002 if (!ndlp)
1003 return 0;
James Smartcff261f2013-12-17 20:29:47 -05001004 if (!ndlp->active_rrqs_xri_bitmap)
1005 return 0;
1006 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
Colin Ian King258f84f2019-02-12 15:29:45 +00001007 return 1;
James Smart19ca7602010-11-20 23:11:55 -05001008 else
1009 return 0;
1010}
1011
1012/**
1013 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1014 * @phba: Pointer to HBA context object.
1015 * @ndlp: nodelist pointer for this target.
1016 * @xritag: xri used in this exchange.
1017 * @rxid: Remote Exchange ID.
1018 * @send_rrq: Flag used to determine if we should send rrq els cmd.
1019 *
1020 * This function takes the hbalock.
1021 * The active bit is always set in the active rrq xri_bitmap even
1022 * if there is no slot avaiable for the other rrq information.
1023 *
1024 * returns 0 rrq actived for this xri
1025 * < 0 No memory or invalid ndlp.
1026 **/
1027int
1028lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -05001029 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -05001030{
James Smart19ca7602010-11-20 23:11:55 -05001031 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -05001032 struct lpfc_node_rrq *rrq;
1033 int empty;
1034
1035 if (!ndlp)
1036 return -EINVAL;
1037
1038 if (!phba->cfg_enable_rrq)
1039 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001040
1041 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001042 if (phba->pport->load_flag & FC_UNLOADING) {
1043 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1044 goto out;
1045 }
1046
1047 /*
1048 * set the active bit even if there is no mem available.
1049 */
1050 if (NLP_CHK_FREE_REQ(ndlp))
1051 goto out;
1052
1053 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1054 goto out;
1055
James Smartcff261f2013-12-17 20:29:47 -05001056 if (!ndlp->active_rrqs_xri_bitmap)
1057 goto out;
1058
1059 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -05001060 goto out;
1061
James Smart19ca7602010-11-20 23:11:55 -05001062 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001063 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
1064 if (!rrq) {
1065 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1066 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1067 " DID:0x%x Send:%d\n",
1068 xritag, rxid, ndlp->nlp_DID, send_rrq);
1069 return -EINVAL;
1070 }
James Smarte5771b42013-03-01 16:37:14 -05001071 if (phba->cfg_enable_rrq == 1)
1072 rrq->send_rrq = send_rrq;
1073 else
1074 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -05001075 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -04001076 rrq->rrq_stop_time = jiffies +
1077 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -05001078 rrq->ndlp = ndlp;
1079 rrq->nlp_DID = ndlp->nlp_DID;
1080 rrq->vport = ndlp->vport;
1081 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -05001082 spin_lock_irqsave(&phba->hbalock, iflags);
1083 empty = list_empty(&phba->active_rrq_list);
1084 list_add_tail(&rrq->list, &phba->active_rrq_list);
1085 phba->hba_flag |= HBA_RRQ_ACTIVE;
1086 if (empty)
1087 lpfc_worker_wake_up(phba);
1088 spin_unlock_irqrestore(&phba->hbalock, iflags);
1089 return 0;
1090out:
1091 spin_unlock_irqrestore(&phba->hbalock, iflags);
1092 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1093 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1094 " DID:0x%x Send:%d\n",
1095 xritag, rxid, ndlp->nlp_DID, send_rrq);
1096 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001097}
1098
1099/**
James Smart895427b2017-02-12 13:52:30 -08001100 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -04001101 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -05001102 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -04001103 *
James Smarte2a8be52019-05-06 17:26:47 -07001104 * The driver calls this function with either the nvme ls ring lock
1105 * or the fc els ring lock held depending on the iocb usage. This function
1106 * gets a new driver sglq object from the sglq list. If the list is not empty
1107 * then it is successful, it returns pointer to the newly allocated sglq
1108 * object else it returns NULL.
James Smartda0436e2009-05-22 14:51:39 -04001109 **/
1110static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -08001111__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -04001112{
James Smart895427b2017-02-12 13:52:30 -08001113 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -04001114 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001115 struct lpfc_sglq *start_sglq = NULL;
James Smartc4908502019-01-28 11:14:28 -08001116 struct lpfc_io_buf *lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001117 struct lpfc_nodelist *ndlp;
James Smarte2a8be52019-05-06 17:26:47 -07001118 struct lpfc_sli_ring *pring = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001119 int found = 0;
1120
James Smarte2a8be52019-05-06 17:26:47 -07001121 if (piocbq->iocb_flag & LPFC_IO_NVME_LS)
1122 pring = phba->sli4_hba.nvmels_wq->pring;
1123 else
1124 pring = lpfc_phba_elsring(phba);
1125
1126 lockdep_assert_held(&pring->ring_lock);
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001127
James Smart19ca7602010-11-20 23:11:55 -05001128 if (piocbq->iocb_flag & LPFC_IO_FCP) {
James Smartc4908502019-01-28 11:14:28 -08001129 lpfc_cmd = (struct lpfc_io_buf *) piocbq->context1;
James Smart19ca7602010-11-20 23:11:55 -05001130 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -05001131 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -04001132 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -05001133 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -04001134 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1135 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1136 ndlp = NULL;
1137 else
1138 ndlp = piocbq->context_un.ndlp;
1139 } else {
James Smart19ca7602010-11-20 23:11:55 -05001140 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -04001141 }
James Smart19ca7602010-11-20 23:11:55 -05001142
James Smart895427b2017-02-12 13:52:30 -08001143 spin_lock(&phba->sli4_hba.sgl_list_lock);
1144 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -05001145 start_sglq = sglq;
1146 while (!found) {
1147 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -08001148 break;
James Smart895427b2017-02-12 13:52:30 -08001149 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1150 test_bit(sglq->sli4_lxritag,
1151 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -05001152 /* This xri has an rrq outstanding for this DID.
1153 * put it back in the list and get another xri.
1154 */
James Smart895427b2017-02-12 13:52:30 -08001155 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001156 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -08001157 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -05001158 struct lpfc_sglq, list);
1159 if (sglq == start_sglq) {
James Smart14041bd2017-06-01 21:07:01 -07001160 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001161 sglq = NULL;
1162 break;
1163 } else
1164 continue;
1165 }
1166 sglq->ndlp = ndlp;
1167 found = 1;
James Smart6d368e52011-05-24 11:44:12 -04001168 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -05001169 sglq->state = SGL_ALLOCATED;
1170 }
James Smart895427b2017-02-12 13:52:30 -08001171 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001172 return sglq;
1173}
1174
1175/**
James Smartf358dd02017-02-12 13:52:34 -08001176 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1177 * @phba: Pointer to HBA context object.
1178 * @piocb: Pointer to the iocbq.
1179 *
1180 * This function is called with the sgl_list lock held. This function
1181 * gets a new driver sglq object from the sglq list. If the
1182 * list is not empty then it is successful, it returns pointer to the newly
1183 * allocated sglq object else it returns NULL.
1184 **/
1185struct lpfc_sglq *
1186__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1187{
1188 struct list_head *lpfc_nvmet_sgl_list;
1189 struct lpfc_sglq *sglq = NULL;
1190
1191 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1192
1193 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1194
1195 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1196 if (!sglq)
1197 return NULL;
1198 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1199 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001200 return sglq;
1201}
1202
James Smarte59058c2008-08-24 21:49:00 -04001203/**
James Smart3621a712009-04-06 18:47:14 -04001204 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001205 * @phba: Pointer to HBA context object.
1206 *
1207 * This function is called with no lock held. This function
1208 * allocates a new driver iocb object from the iocb pool. If the
1209 * allocation is successful, it returns pointer to the newly
1210 * allocated iocb object else it returns NULL.
1211 **/
James Smart2e0fef82007-06-17 19:56:36 -05001212struct lpfc_iocbq *
1213lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001214{
James Smart2e0fef82007-06-17 19:56:36 -05001215 struct lpfc_iocbq * iocbq = NULL;
1216 unsigned long iflags;
1217
1218 spin_lock_irqsave(&phba->hbalock, iflags);
1219 iocbq = __lpfc_sli_get_iocbq(phba);
1220 spin_unlock_irqrestore(&phba->hbalock, iflags);
1221 return iocbq;
1222}
1223
James Smarte59058c2008-08-24 21:49:00 -04001224/**
James Smart4f774512009-05-22 14:52:35 -04001225 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1226 * @phba: Pointer to HBA context object.
1227 * @iocbq: Pointer to driver iocb object.
1228 *
1229 * This function is called with hbalock held to release driver
1230 * iocb object to the iocb pool. The iotag in the iocb object
1231 * does not change for each use of the iocb object. This function
1232 * clears all other fields of the iocb object when it is freed.
1233 * The sqlq structure that holds the xritag and phys and virtual
1234 * mappings for the scatter gather list is retrieved from the
1235 * active array of sglq. The get of the sglq pointer also clears
1236 * the entry in the array. If the status of the IO indiactes that
1237 * this IO was aborted then the sglq entry it put on the
1238 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1239 * IO has good status or fails for any other reason then the sglq
James Smart895427b2017-02-12 13:52:30 -08001240 * entry is added to the free list (lpfc_els_sgl_list).
James Smart4f774512009-05-22 14:52:35 -04001241 **/
1242static void
1243__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1244{
1245 struct lpfc_sglq *sglq;
1246 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001247 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001248 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001249
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001250 lockdep_assert_held(&phba->hbalock);
1251
James Smart4f774512009-05-22 14:52:35 -04001252 if (iocbq->sli4_xritag == NO_XRI)
1253 sglq = NULL;
1254 else
James Smart6d368e52011-05-24 11:44:12 -04001255 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1256
James Smart0e9bb8d2013-03-01 16:35:12 -05001257
James Smart4f774512009-05-22 14:52:35 -04001258 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001259 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1260 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1261 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001262 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001263 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001264 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001265 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1266 spin_unlock_irqrestore(
1267 &phba->sli4_hba.sgl_list_lock, iflag);
1268 goto out;
1269 }
1270
James Smart895427b2017-02-12 13:52:30 -08001271 pring = phba->sli4_hba.els_wq->pring;
James Smart4f774512009-05-22 14:52:35 -04001272 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1273 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001274 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1275 iflag);
James Smart341af102010-01-26 23:07:37 -05001276 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001277 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001278 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001279 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001280 } else {
James Smart895427b2017-02-12 13:52:30 -08001281 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1282 iflag);
James Smart4f774512009-05-22 14:52:35 -04001283 sglq->state = SGL_FREED;
1284 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001285 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001286 &phba->sli4_hba.lpfc_els_sgl_list);
1287 spin_unlock_irqrestore(
1288 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001289
1290 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001291 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001292 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001293 }
James Smart4f774512009-05-22 14:52:35 -04001294 }
1295
James Smartf358dd02017-02-12 13:52:34 -08001296out:
James Smart4f774512009-05-22 14:52:35 -04001297 /*
1298 * Clean all volatile data fields, preserve iotag and node struct.
1299 */
1300 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001301 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001302 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001303 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1304 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001305 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1306}
1307
James Smart2a9bf3d2010-06-07 15:24:45 -04001308
James Smart4f774512009-05-22 14:52:35 -04001309/**
James Smart3772a992009-05-22 14:50:54 -04001310 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1311 * @phba: Pointer to HBA context object.
1312 * @iocbq: Pointer to driver iocb object.
1313 *
1314 * This function is called with hbalock held to release driver
1315 * iocb object to the iocb pool. The iotag in the iocb object
1316 * does not change for each use of the iocb object. This function
1317 * clears all other fields of the iocb object when it is freed.
1318 **/
1319static void
1320__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1321{
1322 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1323
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001324 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001325
1326 /*
James Smart3772a992009-05-22 14:50:54 -04001327 * Clean all volatile data fields, preserve iotag and node struct.
1328 */
1329 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1330 iocbq->sli4_xritag = NO_XRI;
1331 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1332}
1333
1334/**
James Smart3621a712009-04-06 18:47:14 -04001335 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001336 * @phba: Pointer to HBA context object.
1337 * @iocbq: Pointer to driver iocb object.
1338 *
1339 * This function is called with hbalock held to release driver
1340 * iocb object to the iocb pool. The iotag in the iocb object
1341 * does not change for each use of the iocb object. This function
1342 * clears all other fields of the iocb object when it is freed.
1343 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001344static void
James Smart2e0fef82007-06-17 19:56:36 -05001345__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1346{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001347 lockdep_assert_held(&phba->hbalock);
1348
James Smart3772a992009-05-22 14:50:54 -04001349 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001350 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001351}
1352
James Smarte59058c2008-08-24 21:49:00 -04001353/**
James Smart3621a712009-04-06 18:47:14 -04001354 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001355 * @phba: Pointer to HBA context object.
1356 * @iocbq: Pointer to driver iocb object.
1357 *
1358 * This function is called with no lock held to release the iocb to
1359 * iocb pool.
1360 **/
James Smart2e0fef82007-06-17 19:56:36 -05001361void
1362lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1363{
1364 unsigned long iflags;
1365
1366 /*
1367 * Clean all volatile data fields, preserve iotag and node struct.
1368 */
1369 spin_lock_irqsave(&phba->hbalock, iflags);
1370 __lpfc_sli_release_iocbq(phba, iocbq);
1371 spin_unlock_irqrestore(&phba->hbalock, iflags);
1372}
1373
James Smarte59058c2008-08-24 21:49:00 -04001374/**
James Smarta257bf92009-04-06 18:48:10 -04001375 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1376 * @phba: Pointer to HBA context object.
1377 * @iocblist: List of IOCBs.
1378 * @ulpstatus: ULP status in IOCB command field.
1379 * @ulpWord4: ULP word-4 in IOCB command field.
1380 *
1381 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1382 * on the list by invoking the complete callback function associated with the
1383 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1384 * fields.
1385 **/
1386void
1387lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1388 uint32_t ulpstatus, uint32_t ulpWord4)
1389{
1390 struct lpfc_iocbq *piocb;
1391
1392 while (!list_empty(iocblist)) {
1393 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smart84f2ddf2019-08-14 16:56:55 -07001394 if (!piocb->iocb_cmpl) {
1395 if (piocb->iocb_flag & LPFC_IO_NVME)
1396 lpfc_nvme_cancel_iocb(phba, piocb);
1397 else
1398 lpfc_sli_release_iocbq(phba, piocb);
1399 } else {
James Smarta257bf92009-04-06 18:48:10 -04001400 piocb->iocb.ulpStatus = ulpstatus;
1401 piocb->iocb.un.ulpWord[4] = ulpWord4;
1402 (piocb->iocb_cmpl) (phba, piocb, piocb);
1403 }
1404 }
1405 return;
1406}
1407
1408/**
James Smart3621a712009-04-06 18:47:14 -04001409 * lpfc_sli_iocb_cmd_type - Get the iocb type
1410 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001411 *
1412 * This function is called by ring event handler function to get the iocb type.
1413 * This function translates the iocb command to an iocb command type used to
1414 * decide the final disposition of each completed IOCB.
1415 * The function returns
1416 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1417 * LPFC_SOL_IOCB if it is a solicited iocb completion
1418 * LPFC_ABORT_IOCB if it is an abort iocb
1419 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1420 *
1421 * The caller is not required to hold any lock.
1422 **/
dea31012005-04-17 16:05:31 -05001423static lpfc_iocb_type
1424lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1425{
1426 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1427
1428 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1429 return 0;
1430
1431 switch (iocb_cmnd) {
1432 case CMD_XMIT_SEQUENCE_CR:
1433 case CMD_XMIT_SEQUENCE_CX:
1434 case CMD_XMIT_BCAST_CN:
1435 case CMD_XMIT_BCAST_CX:
1436 case CMD_ELS_REQUEST_CR:
1437 case CMD_ELS_REQUEST_CX:
1438 case CMD_CREATE_XRI_CR:
1439 case CMD_CREATE_XRI_CX:
1440 case CMD_GET_RPI_CN:
1441 case CMD_XMIT_ELS_RSP_CX:
1442 case CMD_GET_RPI_CR:
1443 case CMD_FCP_IWRITE_CR:
1444 case CMD_FCP_IWRITE_CX:
1445 case CMD_FCP_IREAD_CR:
1446 case CMD_FCP_IREAD_CX:
1447 case CMD_FCP_ICMND_CR:
1448 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001449 case CMD_FCP_TSEND_CX:
1450 case CMD_FCP_TRSP_CX:
1451 case CMD_FCP_TRECEIVE_CX:
1452 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001453 case CMD_ADAPTER_MSG:
1454 case CMD_ADAPTER_DUMP:
1455 case CMD_XMIT_SEQUENCE64_CR:
1456 case CMD_XMIT_SEQUENCE64_CX:
1457 case CMD_XMIT_BCAST64_CN:
1458 case CMD_XMIT_BCAST64_CX:
1459 case CMD_ELS_REQUEST64_CR:
1460 case CMD_ELS_REQUEST64_CX:
1461 case CMD_FCP_IWRITE64_CR:
1462 case CMD_FCP_IWRITE64_CX:
1463 case CMD_FCP_IREAD64_CR:
1464 case CMD_FCP_IREAD64_CX:
1465 case CMD_FCP_ICMND64_CR:
1466 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001467 case CMD_FCP_TSEND64_CX:
1468 case CMD_FCP_TRSP64_CX:
1469 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001470 case CMD_GEN_REQUEST64_CR:
1471 case CMD_GEN_REQUEST64_CX:
1472 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001473 case DSSCMD_IWRITE64_CR:
1474 case DSSCMD_IWRITE64_CX:
1475 case DSSCMD_IREAD64_CR:
1476 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001477 type = LPFC_SOL_IOCB;
1478 break;
1479 case CMD_ABORT_XRI_CN:
1480 case CMD_ABORT_XRI_CX:
1481 case CMD_CLOSE_XRI_CN:
1482 case CMD_CLOSE_XRI_CX:
1483 case CMD_XRI_ABORTED_CX:
1484 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001485 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001486 type = LPFC_ABORT_IOCB;
1487 break;
1488 case CMD_RCV_SEQUENCE_CX:
1489 case CMD_RCV_ELS_REQ_CX:
1490 case CMD_RCV_SEQUENCE64_CX:
1491 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001492 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001493 case CMD_IOCB_RCV_SEQ64_CX:
1494 case CMD_IOCB_RCV_ELS64_CX:
1495 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001496 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001497 type = LPFC_UNSOL_IOCB;
1498 break;
James Smart3163f722008-02-08 18:50:25 -05001499 case CMD_IOCB_XMIT_MSEQ64_CR:
1500 case CMD_IOCB_XMIT_MSEQ64_CX:
1501 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1502 case CMD_IOCB_RCV_ELS_LIST64_CX:
1503 case CMD_IOCB_CLOSE_EXTENDED_CN:
1504 case CMD_IOCB_ABORT_EXTENDED_CN:
1505 case CMD_IOCB_RET_HBQE64_CN:
1506 case CMD_IOCB_FCP_IBIDIR64_CR:
1507 case CMD_IOCB_FCP_IBIDIR64_CX:
1508 case CMD_IOCB_FCP_ITASKMGT64_CX:
1509 case CMD_IOCB_LOGENTRY_CN:
1510 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1511 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001512 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001513 type = LPFC_UNKNOWN_IOCB;
1514 break;
dea31012005-04-17 16:05:31 -05001515 default:
1516 type = LPFC_UNKNOWN_IOCB;
1517 break;
1518 }
1519
1520 return type;
1521}
1522
James Smarte59058c2008-08-24 21:49:00 -04001523/**
James Smart3621a712009-04-06 18:47:14 -04001524 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001525 * @phba: Pointer to HBA context object.
1526 *
1527 * This function is called from SLI initialization code
1528 * to configure every ring of the HBA's SLI interface. The
1529 * caller is not required to hold any lock. This function issues
1530 * a config_ring mailbox command for each ring.
1531 * This function returns zero if successful else returns a negative
1532 * error code.
1533 **/
dea31012005-04-17 16:05:31 -05001534static int
James Smarted957682007-06-17 19:56:37 -05001535lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001536{
1537 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001538 LPFC_MBOXQ_t *pmb;
1539 MAILBOX_t *pmbox;
1540 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001541
James Smarted957682007-06-17 19:56:37 -05001542 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1543 if (!pmb)
1544 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001545 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001546 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001547 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001548 lpfc_config_ring(phba, i, pmb);
1549 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1550 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001551 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001552 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001553 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1554 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001555 rc, pmbox->mbxCommand,
1556 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001557 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001558 ret = -ENXIO;
1559 break;
dea31012005-04-17 16:05:31 -05001560 }
1561 }
James Smarted957682007-06-17 19:56:37 -05001562 mempool_free(pmb, phba->mbox_mem_pool);
1563 return ret;
dea31012005-04-17 16:05:31 -05001564}
1565
James Smarte59058c2008-08-24 21:49:00 -04001566/**
James Smart3621a712009-04-06 18:47:14 -04001567 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001568 * @phba: Pointer to HBA context object.
1569 * @pring: Pointer to driver SLI ring object.
1570 * @piocb: Pointer to the driver iocb object.
1571 *
James Smarte2a8be52019-05-06 17:26:47 -07001572 * The driver calls this function with the hbalock held for SLI3 ports or
1573 * the ring lock held for SLI4 ports. The function adds the
James Smarte59058c2008-08-24 21:49:00 -04001574 * new iocb to txcmplq of the given ring. This function always returns
1575 * 0. If this function is called for ELS ring, this function checks if
1576 * there is a vport associated with the ELS command. This function also
1577 * starts els_tmofunc timer if this is an ELS command.
1578 **/
dea31012005-04-17 16:05:31 -05001579static int
James Smart2e0fef82007-06-17 19:56:36 -05001580lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1581 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001582{
James Smarte2a8be52019-05-06 17:26:47 -07001583 if (phba->sli_rev == LPFC_SLI_REV4)
1584 lockdep_assert_held(&pring->ring_lock);
1585 else
1586 lockdep_assert_held(&phba->hbalock);
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001587
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001588 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001589
dea31012005-04-17 16:05:31 -05001590 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001591 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08001592 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001593
James Smart92d7f7b2007-06-17 19:56:38 -05001594 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1595 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001596 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1597 BUG_ON(!piocb->vport);
1598 if (!(piocb->vport->load_flag & FC_UNLOADING))
1599 mod_timer(&piocb->vport->els_tmofunc,
1600 jiffies +
1601 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1602 }
dea31012005-04-17 16:05:31 -05001603
James Smart2e0fef82007-06-17 19:56:36 -05001604 return 0;
dea31012005-04-17 16:05:31 -05001605}
1606
James Smarte59058c2008-08-24 21:49:00 -04001607/**
James Smart3621a712009-04-06 18:47:14 -04001608 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001609 * @phba: Pointer to HBA context object.
1610 * @pring: Pointer to driver SLI ring object.
1611 *
1612 * This function is called with hbalock held to get next
1613 * iocb in txq of the given ring. If there is any iocb in
1614 * the txq, the function returns first iocb in the list after
1615 * removing the iocb from the list, else it returns NULL.
1616 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001617struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001618lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001619{
dea31012005-04-17 16:05:31 -05001620 struct lpfc_iocbq *cmd_iocb;
1621
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001622 lockdep_assert_held(&phba->hbalock);
1623
James Smart858c9f62007-06-17 19:56:39 -05001624 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001625 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001626}
1627
James Smarte59058c2008-08-24 21:49:00 -04001628/**
James Smart3621a712009-04-06 18:47:14 -04001629 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001630 * @phba: Pointer to HBA context object.
1631 * @pring: Pointer to driver SLI ring object.
1632 *
1633 * This function is called with hbalock held and the caller must post the
1634 * iocb without releasing the lock. If the caller releases the lock,
1635 * iocb slot returned by the function is not guaranteed to be available.
1636 * The function returns pointer to the next available iocb slot if there
1637 * is available slot in the ring, else it returns NULL.
1638 * If the get index of the ring is ahead of the put index, the function
1639 * will post an error attention event to the worker thread to take the
1640 * HBA to offline state.
1641 **/
dea31012005-04-17 16:05:31 -05001642static IOCB_t *
1643lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1644{
James Smart34b02dc2008-08-24 21:49:55 -04001645 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001646 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001647
1648 lockdep_assert_held(&phba->hbalock);
1649
James Smart7e56aa22012-08-03 12:35:34 -04001650 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1651 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1652 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001653
James Smart7e56aa22012-08-03 12:35:34 -04001654 if (unlikely(pring->sli.sli3.local_getidx ==
1655 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001656
James Smart7e56aa22012-08-03 12:35:34 -04001657 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001658
James Smart7e56aa22012-08-03 12:35:34 -04001659 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001660 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001661 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001662 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001663 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001664 pring->sli.sli3.local_getidx,
1665 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001666
James Smart2e0fef82007-06-17 19:56:36 -05001667 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001668 /*
1669 * All error attention handlers are posted to
1670 * worker thread
1671 */
1672 phba->work_ha |= HA_ERATT;
1673 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001674
James Smart5e9d9b82008-06-14 22:52:53 -04001675 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001676
1677 return NULL;
1678 }
1679
James Smart7e56aa22012-08-03 12:35:34 -04001680 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001681 return NULL;
1682 }
1683
James Smarted957682007-06-17 19:56:37 -05001684 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001685}
1686
James Smarte59058c2008-08-24 21:49:00 -04001687/**
James Smart3621a712009-04-06 18:47:14 -04001688 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001689 * @phba: Pointer to HBA context object.
1690 * @iocbq: Pointer to driver iocb object.
1691 *
1692 * This function gets an iotag for the iocb. If there is no unused iotag and
1693 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1694 * array and assigns a new iotag.
1695 * The function returns the allocated iotag if successful, else returns zero.
1696 * Zero is not a valid iotag.
1697 * The caller is not required to hold any lock.
1698 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001699uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001700lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001701{
James Smart2e0fef82007-06-17 19:56:36 -05001702 struct lpfc_iocbq **new_arr;
1703 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001704 size_t new_len;
1705 struct lpfc_sli *psli = &phba->sli;
1706 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001707
James Smart2e0fef82007-06-17 19:56:36 -05001708 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001709 iotag = psli->last_iotag;
1710 if(++iotag < psli->iocbq_lookup_len) {
1711 psli->last_iotag = iotag;
1712 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001713 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001714 iocbq->iotag = iotag;
1715 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001716 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001717 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1718 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001719 spin_unlock_irq(&phba->hbalock);
Kees Cook6396bb22018-06-12 14:03:40 -07001720 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001721 GFP_KERNEL);
1722 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001723 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001724 old_arr = psli->iocbq_lookup;
1725 if (new_len <= psli->iocbq_lookup_len) {
1726 /* highly unprobable case */
1727 kfree(new_arr);
1728 iotag = psli->last_iotag;
1729 if(++iotag < psli->iocbq_lookup_len) {
1730 psli->last_iotag = iotag;
1731 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001732 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001733 iocbq->iotag = iotag;
1734 return iotag;
1735 }
James Smart2e0fef82007-06-17 19:56:36 -05001736 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001737 return 0;
1738 }
1739 if (psli->iocbq_lookup)
1740 memcpy(new_arr, old_arr,
1741 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001742 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001743 psli->iocbq_lookup = new_arr;
1744 psli->iocbq_lookup_len = new_len;
1745 psli->last_iotag = iotag;
1746 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001747 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001748 iocbq->iotag = iotag;
1749 kfree(old_arr);
1750 return iotag;
1751 }
James Smart8f6d98d2006-08-01 07:34:00 -04001752 } else
James Smart2e0fef82007-06-17 19:56:36 -05001753 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001754
James Smartbc739052010-08-04 16:11:18 -04001755 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001756 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1757 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001758
James Bottomley604a3e32005-10-29 10:28:33 -05001759 return 0;
dea31012005-04-17 16:05:31 -05001760}
1761
James Smarte59058c2008-08-24 21:49:00 -04001762/**
James Smart3621a712009-04-06 18:47:14 -04001763 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001764 * @phba: Pointer to HBA context object.
1765 * @pring: Pointer to driver SLI ring object.
1766 * @iocb: Pointer to iocb slot in the ring.
1767 * @nextiocb: Pointer to driver iocb object which need to be
1768 * posted to firmware.
1769 *
1770 * This function is called with hbalock held to post a new iocb to
1771 * the firmware. This function copies the new iocb to ring iocb slot and
1772 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1773 * a completion call back for this iocb else the function will free the
1774 * iocb object.
1775 **/
dea31012005-04-17 16:05:31 -05001776static void
1777lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1778 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1779{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001780 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001781 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001782 * Set up an iotag
dea31012005-04-17 16:05:31 -05001783 */
James Bottomley604a3e32005-10-29 10:28:33 -05001784 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001785
James Smarte2a0a9d2008-12-04 22:40:02 -05001786
James Smarta58cbd52007-08-02 11:09:43 -04001787 if (pring->ringno == LPFC_ELS_RING) {
1788 lpfc_debugfs_slow_ring_trc(phba,
1789 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1790 *(((uint32_t *) &nextiocb->iocb) + 4),
1791 *(((uint32_t *) &nextiocb->iocb) + 6),
1792 *(((uint32_t *) &nextiocb->iocb) + 7));
1793 }
1794
dea31012005-04-17 16:05:31 -05001795 /*
1796 * Issue iocb command to adapter
1797 */
James Smart92d7f7b2007-06-17 19:56:38 -05001798 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001799 wmb();
1800 pring->stats.iocb_cmd++;
1801
1802 /*
1803 * If there is no completion routine to call, we can release the
1804 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1805 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1806 */
1807 if (nextiocb->iocb_cmpl)
1808 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001809 else
James Smart2e0fef82007-06-17 19:56:36 -05001810 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001811
1812 /*
1813 * Let the HBA know what IOCB slot will be the next one the
1814 * driver will put a command into.
1815 */
James Smart7e56aa22012-08-03 12:35:34 -04001816 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1817 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001818}
1819
James Smarte59058c2008-08-24 21:49:00 -04001820/**
James Smart3621a712009-04-06 18:47:14 -04001821 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001822 * @phba: Pointer to HBA context object.
1823 * @pring: Pointer to driver SLI ring object.
1824 *
1825 * The caller is not required to hold any lock for calling this function.
1826 * This function updates the chip attention bits for the ring to inform firmware
1827 * that there are pending work to be done for this ring and requests an
1828 * interrupt when there is space available in the ring. This function is
1829 * called when the driver is unable to post more iocbs to the ring due
1830 * to unavailability of space in the ring.
1831 **/
dea31012005-04-17 16:05:31 -05001832static void
James Smart2e0fef82007-06-17 19:56:36 -05001833lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001834{
1835 int ringno = pring->ringno;
1836
1837 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1838
1839 wmb();
1840
1841 /*
1842 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1843 * The HBA will tell us when an IOCB entry is available.
1844 */
1845 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1846 readl(phba->CAregaddr); /* flush */
1847
1848 pring->stats.iocb_cmd_full++;
1849}
1850
James Smarte59058c2008-08-24 21:49:00 -04001851/**
James Smart3621a712009-04-06 18:47:14 -04001852 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001853 * @phba: Pointer to HBA context object.
1854 * @pring: Pointer to driver SLI ring object.
1855 *
1856 * This function updates the chip attention register bit for the
1857 * given ring to inform HBA that there is more work to be done
1858 * in this ring. The caller is not required to hold any lock.
1859 **/
dea31012005-04-17 16:05:31 -05001860static void
James Smart2e0fef82007-06-17 19:56:36 -05001861lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001862{
1863 int ringno = pring->ringno;
1864
1865 /*
1866 * Tell the HBA that there is work to do in this ring.
1867 */
James Smart34b02dc2008-08-24 21:49:55 -04001868 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1869 wmb();
1870 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1871 readl(phba->CAregaddr); /* flush */
1872 }
dea31012005-04-17 16:05:31 -05001873}
1874
James Smarte59058c2008-08-24 21:49:00 -04001875/**
James Smart3621a712009-04-06 18:47:14 -04001876 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001877 * @phba: Pointer to HBA context object.
1878 * @pring: Pointer to driver SLI ring object.
1879 *
1880 * This function is called with hbalock held to post pending iocbs
1881 * in the txq to the firmware. This function is called when driver
1882 * detects space available in the ring.
1883 **/
dea31012005-04-17 16:05:31 -05001884static void
James Smart2e0fef82007-06-17 19:56:36 -05001885lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001886{
1887 IOCB_t *iocb;
1888 struct lpfc_iocbq *nextiocb;
1889
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001890 lockdep_assert_held(&phba->hbalock);
1891
dea31012005-04-17 16:05:31 -05001892 /*
1893 * Check to see if:
1894 * (a) there is anything on the txq to send
1895 * (b) link is up
1896 * (c) link attention events can be processed (fcp ring only)
1897 * (d) IOCB processing is not blocked by the outstanding mbox command.
1898 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001899
1900 if (lpfc_is_link_up(phba) &&
1901 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001902 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001903 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001904
1905 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1906 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1907 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1908
1909 if (iocb)
1910 lpfc_sli_update_ring(phba, pring);
1911 else
1912 lpfc_sli_update_full_ring(phba, pring);
1913 }
1914
1915 return;
1916}
1917
James Smarte59058c2008-08-24 21:49:00 -04001918/**
James Smart3621a712009-04-06 18:47:14 -04001919 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001920 * @phba: Pointer to HBA context object.
1921 * @hbqno: HBQ number.
1922 *
1923 * This function is called with hbalock held to get the next
1924 * available slot for the given HBQ. If there is free slot
1925 * available for the HBQ it will return pointer to the next available
1926 * HBQ entry else it will return NULL.
1927 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001928static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001929lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1930{
1931 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1932
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001933 lockdep_assert_held(&phba->hbalock);
1934
James Smarted957682007-06-17 19:56:37 -05001935 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1936 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1937 hbqp->next_hbqPutIdx = 0;
1938
1939 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001940 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001941 uint32_t getidx = le32_to_cpu(raw_index);
1942
1943 hbqp->local_hbqGetIdx = getidx;
1944
1945 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1946 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001947 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001948 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001949 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001950 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001951 hbqp->entry_count);
1952
1953 phba->link_state = LPFC_HBA_ERROR;
1954 return NULL;
1955 }
1956
1957 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1958 return NULL;
1959 }
1960
James Smart51ef4c22007-08-02 11:10:31 -04001961 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1962 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001963}
1964
James Smarte59058c2008-08-24 21:49:00 -04001965/**
James Smart3621a712009-04-06 18:47:14 -04001966 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001967 * @phba: Pointer to HBA context object.
1968 *
1969 * This function is called with no lock held to free all the
1970 * hbq buffers while uninitializing the SLI interface. It also
1971 * frees the HBQ buffers returned by the firmware but not yet
1972 * processed by the upper layers.
1973 **/
James Smarted957682007-06-17 19:56:37 -05001974void
1975lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1976{
James Smart92d7f7b2007-06-17 19:56:38 -05001977 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1978 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001979 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001980 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001981
James Smart51ef4c22007-08-02 11:10:31 -04001982 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001983 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001984 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001985 for (i = 0; i < hbq_count; ++i) {
1986 list_for_each_entry_safe(dmabuf, next_dmabuf,
1987 &phba->hbqs[i].hbq_buffer_list, list) {
1988 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1989 list_del(&hbq_buf->dbuf.list);
1990 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1991 }
James Smarta8adb832007-10-27 13:37:53 -04001992 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001993 }
James Smart3163f722008-02-08 18:50:25 -05001994
1995 /* Mark the HBQs not in use */
1996 phba->hbq_in_use = 0;
1997 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001998}
1999
James Smarte59058c2008-08-24 21:49:00 -04002000/**
James Smart3621a712009-04-06 18:47:14 -04002001 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002002 * @phba: Pointer to HBA context object.
2003 * @hbqno: HBQ number.
2004 * @hbq_buf: Pointer to HBQ buffer.
2005 *
2006 * This function is called with the hbalock held to post a
2007 * hbq buffer to the firmware. If the function finds an empty
2008 * slot in the HBQ, it will post the buffer. The function will return
2009 * pointer to the hbq entry if it successfully post the buffer
2010 * else it will return NULL.
2011 **/
James Smart3772a992009-05-22 14:50:54 -04002012static int
James Smarted957682007-06-17 19:56:37 -05002013lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05002014 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05002015{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002016 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002017 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2018}
2019
2020/**
2021 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2022 * @phba: Pointer to HBA context object.
2023 * @hbqno: HBQ number.
2024 * @hbq_buf: Pointer to HBQ buffer.
2025 *
2026 * This function is called with the hbalock held to post a hbq buffer to the
2027 * firmware. If the function finds an empty slot in the HBQ, it will post the
2028 * buffer and place it on the hbq_buffer_list. The function will return zero if
2029 * it successfully post the buffer else it will return an error.
2030 **/
2031static int
2032lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2033 struct hbq_dmabuf *hbq_buf)
2034{
James Smarted957682007-06-17 19:56:37 -05002035 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05002036 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05002037
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002038 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05002039 /* Get next HBQ entry slot to use */
2040 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2041 if (hbqe) {
2042 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2043
James Smart92d7f7b2007-06-17 19:56:38 -05002044 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2045 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08002046 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05002047 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002048 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2049 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2050 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05002051 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2052 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05002053 /* flush */
James Smarted957682007-06-17 19:56:37 -05002054 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04002055 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04002056 return 0;
2057 } else
2058 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05002059}
2060
James Smart4f774512009-05-22 14:52:35 -04002061/**
2062 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2063 * @phba: Pointer to HBA context object.
2064 * @hbqno: HBQ number.
2065 * @hbq_buf: Pointer to HBQ buffer.
2066 *
2067 * This function is called with the hbalock held to post an RQE to the SLI4
2068 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2069 * the hbq_buffer_list and return zero, otherwise it will return an error.
2070 **/
2071static int
2072lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2073 struct hbq_dmabuf *hbq_buf)
2074{
2075 int rc;
2076 struct lpfc_rqe hrqe;
2077 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08002078 struct lpfc_queue *hrq;
2079 struct lpfc_queue *drq;
2080
2081 if (hbqno != LPFC_ELS_HBQ)
2082 return 1;
2083 hrq = phba->sli4_hba.hdr_rq;
2084 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04002085
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002086 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04002087 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2088 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2089 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2090 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08002091 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04002092 if (rc < 0)
2093 return rc;
James Smart895427b2017-02-12 13:52:30 -08002094 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04002095 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2096 return 0;
2097}
2098
James Smarte59058c2008-08-24 21:49:00 -04002099/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05002100static struct lpfc_hbq_init lpfc_els_hbq = {
2101 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05002102 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05002103 .mask_count = 0,
2104 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04002105 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05002106 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04002107 .init_count = 40,
2108 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05002109};
James Smarted957682007-06-17 19:56:37 -05002110
James Smarte59058c2008-08-24 21:49:00 -04002111/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04002112struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05002113 &lpfc_els_hbq,
2114};
2115
James Smarte59058c2008-08-24 21:49:00 -04002116/**
James Smart3621a712009-04-06 18:47:14 -04002117 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04002118 * @phba: Pointer to HBA context object.
2119 * @hbqno: HBQ number.
2120 * @count: Number of HBQ buffers to be posted.
2121 *
James Smartd7c255b2008-08-24 21:50:00 -04002122 * This function is called with no lock held to post more hbq buffers to the
2123 * given HBQ. The function returns the number of HBQ buffers successfully
2124 * posted.
James Smarte59058c2008-08-24 21:49:00 -04002125 **/
James Smart311464e2007-08-02 11:10:37 -04002126static int
James Smart92d7f7b2007-06-17 19:56:38 -05002127lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2128{
James Smartd7c255b2008-08-24 21:50:00 -04002129 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05002130 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05002131 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04002132 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002133 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04002134 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04002135
James Smartd7c255b2008-08-24 21:50:00 -04002136 if ((phba->hbqs[hbqno].buffer_count + count) >
2137 lpfc_hbq_defs[hbqno]->entry_count)
2138 count = lpfc_hbq_defs[hbqno]->entry_count -
2139 phba->hbqs[hbqno].buffer_count;
2140 if (!count)
2141 return 0;
2142 /* Allocate HBQ entries */
2143 for (i = 0; i < count; i++) {
2144 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2145 if (!hbq_buffer)
2146 break;
2147 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2148 }
James Smart3163f722008-02-08 18:50:25 -05002149 /* Check whether HBQ is still in use */
2150 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002151 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04002152 goto err;
2153 while (!list_empty(&hbq_buf_list)) {
2154 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2155 dbuf.list);
2156 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2157 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04002158 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04002159 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04002160 posted++;
2161 } else
James Smart51ef4c22007-08-02 11:10:31 -04002162 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05002163 }
James Smart3163f722008-02-08 18:50:25 -05002164 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04002165 return posted;
2166err:
2167 spin_unlock_irqrestore(&phba->hbalock, flags);
2168 while (!list_empty(&hbq_buf_list)) {
2169 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2170 dbuf.list);
2171 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2172 }
James Smart92d7f7b2007-06-17 19:56:38 -05002173 return 0;
James Smarted957682007-06-17 19:56:37 -05002174}
2175
James Smarte59058c2008-08-24 21:49:00 -04002176/**
James Smart3621a712009-04-06 18:47:14 -04002177 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04002178 * @phba: Pointer to HBA context object.
2179 * @qno: HBQ number.
2180 *
2181 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04002182 * is called with no lock held. The function returns the number of HBQ entries
2183 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002184 **/
James Smarted957682007-06-17 19:56:37 -05002185int
James Smart92d7f7b2007-06-17 19:56:38 -05002186lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002187{
James Smartdef9c7a2009-12-21 17:02:28 -05002188 if (phba->sli_rev == LPFC_SLI_REV4)
2189 return 0;
2190 else
2191 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2192 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05002193}
2194
James Smarte59058c2008-08-24 21:49:00 -04002195/**
James Smart3621a712009-04-06 18:47:14 -04002196 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002197 * @phba: Pointer to HBA context object.
2198 * @qno: HBQ queue number.
2199 *
2200 * This function is called from SLI initialization code path with
2201 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002202 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002203 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002204static int
James Smart92d7f7b2007-06-17 19:56:38 -05002205lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002206{
James Smartdef9c7a2009-12-21 17:02:28 -05002207 if (phba->sli_rev == LPFC_SLI_REV4)
2208 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002209 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002210 else
2211 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2212 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002213}
2214
James Smarte59058c2008-08-24 21:49:00 -04002215/**
James Smart3772a992009-05-22 14:50:54 -04002216 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2217 * @phba: Pointer to HBA context object.
2218 * @hbqno: HBQ number.
2219 *
2220 * This function removes the first hbq buffer on an hbq list and returns a
2221 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2222 **/
2223static struct hbq_dmabuf *
2224lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2225{
2226 struct lpfc_dmabuf *d_buf;
2227
2228 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2229 if (!d_buf)
2230 return NULL;
2231 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2232}
2233
2234/**
James Smart2d7dbc42017-02-12 13:52:35 -08002235 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2236 * @phba: Pointer to HBA context object.
2237 * @hbqno: HBQ number.
2238 *
2239 * This function removes the first RQ buffer on an RQ buffer list and returns a
2240 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2241 **/
2242static struct rqb_dmabuf *
2243lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2244{
2245 struct lpfc_dmabuf *h_buf;
2246 struct lpfc_rqb *rqbp;
2247
2248 rqbp = hrq->rqbp;
2249 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2250 struct lpfc_dmabuf, list);
2251 if (!h_buf)
2252 return NULL;
2253 rqbp->buffer_count--;
2254 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2255}
2256
2257/**
James Smart3621a712009-04-06 18:47:14 -04002258 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002259 * @phba: Pointer to HBA context object.
2260 * @tag: Tag of the hbq buffer.
2261 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002262 * This function searches for the hbq buffer associated with the given tag in
2263 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2264 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002265 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002266static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002267lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2268{
James Smart92d7f7b2007-06-17 19:56:38 -05002269 struct lpfc_dmabuf *d_buf;
2270 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002271 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002272
James Smart51ef4c22007-08-02 11:10:31 -04002273 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002274 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002275 return NULL;
2276
James Smart3772a992009-05-22 14:50:54 -04002277 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002278 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002279 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002280 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002281 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002282 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002283 }
2284 }
James Smart3772a992009-05-22 14:50:54 -04002285 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002286 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002287 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002288 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002289 return NULL;
James Smarted957682007-06-17 19:56:37 -05002290}
2291
James Smarte59058c2008-08-24 21:49:00 -04002292/**
James Smart3621a712009-04-06 18:47:14 -04002293 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002294 * @phba: Pointer to HBA context object.
2295 * @hbq_buffer: Pointer to HBQ buffer.
2296 *
2297 * This function is called with hbalock. This function gives back
2298 * the hbq buffer to firmware. If the HBQ does not have space to
2299 * post the buffer, it will free the buffer.
2300 **/
James Smarted957682007-06-17 19:56:37 -05002301void
James Smart51ef4c22007-08-02 11:10:31 -04002302lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002303{
2304 uint32_t hbqno;
2305
James Smart51ef4c22007-08-02 11:10:31 -04002306 if (hbq_buffer) {
2307 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002308 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002309 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002310 }
2311}
2312
James Smarte59058c2008-08-24 21:49:00 -04002313/**
James Smart3621a712009-04-06 18:47:14 -04002314 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002315 * @mbxCommand: mailbox command code.
2316 *
2317 * This function is called by the mailbox event handler function to verify
2318 * that the completed mailbox command is a legitimate mailbox command. If the
2319 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2320 * and the mailbox event handler will take the HBA offline.
2321 **/
dea31012005-04-17 16:05:31 -05002322static int
2323lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2324{
2325 uint8_t ret;
2326
2327 switch (mbxCommand) {
2328 case MBX_LOAD_SM:
2329 case MBX_READ_NV:
2330 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002331 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002332 case MBX_RUN_BIU_DIAG:
2333 case MBX_INIT_LINK:
2334 case MBX_DOWN_LINK:
2335 case MBX_CONFIG_LINK:
2336 case MBX_CONFIG_RING:
2337 case MBX_RESET_RING:
2338 case MBX_READ_CONFIG:
2339 case MBX_READ_RCONFIG:
2340 case MBX_READ_SPARM:
2341 case MBX_READ_STATUS:
2342 case MBX_READ_RPI:
2343 case MBX_READ_XRI:
2344 case MBX_READ_REV:
2345 case MBX_READ_LNK_STAT:
2346 case MBX_REG_LOGIN:
2347 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002348 case MBX_CLEAR_LA:
2349 case MBX_DUMP_MEMORY:
2350 case MBX_DUMP_CONTEXT:
2351 case MBX_RUN_DIAGS:
2352 case MBX_RESTART:
2353 case MBX_UPDATE_CFG:
2354 case MBX_DOWN_LOAD:
2355 case MBX_DEL_LD_ENTRY:
2356 case MBX_RUN_PROGRAM:
2357 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002358 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002359 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002360 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002361 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002362 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002363 case MBX_LOAD_AREA:
2364 case MBX_RUN_BIU_DIAG64:
2365 case MBX_CONFIG_PORT:
2366 case MBX_READ_SPARM64:
2367 case MBX_READ_RPI64:
2368 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002369 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002370 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002371 case MBX_SET_DEBUG:
2372 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002373 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002374 case MBX_REG_VPI:
2375 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002376 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002377 case MBX_PORT_CAPABILITIES:
2378 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002379 case MBX_SLI4_CONFIG:
2380 case MBX_SLI4_REQ_FTRS:
2381 case MBX_REG_FCFI:
2382 case MBX_UNREG_FCFI:
2383 case MBX_REG_VFI:
2384 case MBX_UNREG_VFI:
2385 case MBX_INIT_VPI:
2386 case MBX_INIT_VFI:
2387 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002388 case MBX_READ_EVENT_LOG_STATUS:
2389 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002390 case MBX_SECURITY_MGMT:
2391 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002392 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002393 ret = mbxCommand;
2394 break;
2395 default:
2396 ret = MBX_SHUTDOWN;
2397 break;
2398 }
James Smart2e0fef82007-06-17 19:56:36 -05002399 return ret;
dea31012005-04-17 16:05:31 -05002400}
James Smarte59058c2008-08-24 21:49:00 -04002401
2402/**
James Smart3621a712009-04-06 18:47:14 -04002403 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002404 * @phba: Pointer to HBA context object.
2405 * @pmboxq: Pointer to mailbox command.
2406 *
2407 * This is completion handler function for mailbox commands issued from
2408 * lpfc_sli_issue_mbox_wait function. This function is called by the
2409 * mailbox event handler function with no lock held. This function
2410 * will wake up thread waiting on the wait queue pointed by context1
2411 * of the mailbox.
2412 **/
James Smart04c68492009-05-22 14:52:52 -04002413void
James Smart2e0fef82007-06-17 19:56:36 -05002414lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002415{
James Smart858c9f62007-06-17 19:56:39 -05002416 unsigned long drvr_flag;
James Smarte29d74f2018-03-05 12:04:07 -08002417 struct completion *pmbox_done;
dea31012005-04-17 16:05:31 -05002418
2419 /*
James Smarte29d74f2018-03-05 12:04:07 -08002420 * If pmbox_done is empty, the driver thread gave up waiting and
dea31012005-04-17 16:05:31 -05002421 * continued running.
2422 */
James Smart7054a602007-04-25 09:52:34 -04002423 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002424 spin_lock_irqsave(&phba->hbalock, drvr_flag);
James Smarte29d74f2018-03-05 12:04:07 -08002425 pmbox_done = (struct completion *)pmboxq->context3;
2426 if (pmbox_done)
2427 complete(pmbox_done);
James Smart858c9f62007-06-17 19:56:39 -05002428 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002429 return;
2430}
2431
James Smartb95b2112019-08-14 16:56:47 -07002432static void
2433__lpfc_sli_rpi_release(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2434{
2435 unsigned long iflags;
2436
2437 if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
2438 lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
2439 spin_lock_irqsave(&vport->phba->ndlp_lock, iflags);
2440 ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
2441 ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
2442 spin_unlock_irqrestore(&vport->phba->ndlp_lock, iflags);
2443 }
2444 ndlp->nlp_flag &= ~NLP_UNREG_INP;
2445}
James Smarte59058c2008-08-24 21:49:00 -04002446
2447/**
James Smart3621a712009-04-06 18:47:14 -04002448 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002449 * @phba: Pointer to HBA context object.
2450 * @pmb: Pointer to mailbox object.
2451 *
2452 * This function is the default mailbox completion handler. It
2453 * frees the memory resources associated with the completed mailbox
2454 * command. If the completed command is a REG_LOGIN mailbox command,
2455 * this function will issue a UREG_LOGIN to re-claim the RPI.
2456 **/
dea31012005-04-17 16:05:31 -05002457void
James Smart2e0fef82007-06-17 19:56:36 -05002458lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002459{
James Smartd439d282010-09-29 11:18:45 -04002460 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002461 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002462 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002463 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002464 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002465 int rc;
2466
James Smart3e1f0712018-11-29 16:09:29 -08002467 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
James Smart7054a602007-04-25 09:52:34 -04002468
dea31012005-04-17 16:05:31 -05002469 if (mp) {
2470 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2471 kfree(mp);
2472 }
James Smart7054a602007-04-25 09:52:34 -04002473
2474 /*
2475 * If a REG_LOGIN succeeded after node is destroyed or node
2476 * is in re-discovery driver need to cleanup the RPI.
2477 */
James Smart2e0fef82007-06-17 19:56:36 -05002478 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002479 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2480 !pmb->u.mb.mbxStatus) {
2481 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002482 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002483 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002484 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002485 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002486 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2487 if (rc != MBX_NOT_FINISHED)
2488 return;
2489 }
2490
James Smart695a8142010-01-26 23:08:03 -05002491 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2492 !(phba->pport->load_flag & FC_UNLOADING) &&
2493 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002494 shost = lpfc_shost_from_vport(vport);
2495 spin_lock_irq(shost->host_lock);
2496 vport->vpi_state |= LPFC_VPI_REGISTERED;
2497 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2498 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002499 }
2500
James Smartd439d282010-09-29 11:18:45 -04002501 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -08002502 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -04002503 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002504 pmb->ctx_buf = NULL;
2505 pmb->ctx_ndlp = NULL;
2506 }
2507
2508 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2509 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2510
2511 /* Check to see if there are any deferred events to process */
2512 if (ndlp) {
2513 lpfc_printf_vlog(
2514 vport,
2515 KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2516 "1438 UNREG cmpl deferred mbox x%x "
James Smart32350662019-08-14 16:57:06 -07002517 "on NPort x%x Data: x%x x%x %px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002518 ndlp->nlp_rpi, ndlp->nlp_DID,
2519 ndlp->nlp_flag, ndlp->nlp_defer_did, ndlp);
2520
2521 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2522 (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
James Smart00292e02018-12-13 15:17:55 -08002523 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002524 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2525 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002526 } else {
James Smartb95b2112019-08-14 16:56:47 -07002527 __lpfc_sli_rpi_release(vport, ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002528 }
James Smart97acd002019-09-21 20:58:51 -07002529 if (vport->load_flag & FC_UNLOADING)
2530 lpfc_nlp_put(ndlp);
James Smart9b164062019-03-12 16:30:06 -07002531 pmb->ctx_ndlp = NULL;
James Smartdea16bd2018-11-29 16:09:30 -08002532 }
James Smartd439d282010-09-29 11:18:45 -04002533 }
2534
James Smartdcf2a4e2010-09-29 11:18:53 -04002535 /* Check security permission status on INIT_LINK mailbox command */
2536 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2537 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2538 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2539 "2860 SLI authentication is required "
2540 "for INIT_LINK but has not done yet\n");
2541
James Smart04c68492009-05-22 14:52:52 -04002542 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2543 lpfc_sli4_mbox_cmd_free(phba, pmb);
2544 else
2545 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002546}
James Smartbe6bb942015-04-07 15:07:22 -04002547 /**
2548 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2549 * @phba: Pointer to HBA context object.
2550 * @pmb: Pointer to mailbox object.
2551 *
2552 * This function is the unreg rpi mailbox completion handler. It
2553 * frees the memory resources associated with the completed mailbox
2554 * command. An additional refrenece is put on the ndlp to prevent
2555 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2556 * the unreg mailbox command completes, this routine puts the
2557 * reference back.
2558 *
2559 **/
2560void
2561lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2562{
2563 struct lpfc_vport *vport = pmb->vport;
2564 struct lpfc_nodelist *ndlp;
2565
James Smart3e1f0712018-11-29 16:09:29 -08002566 ndlp = pmb->ctx_ndlp;
James Smartbe6bb942015-04-07 15:07:22 -04002567 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2568 if (phba->sli_rev == LPFC_SLI_REV4 &&
2569 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002570 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002571 LPFC_SLI_INTF_IF_TYPE_2)) {
2572 if (ndlp) {
James Smartdea16bd2018-11-29 16:09:30 -08002573 lpfc_printf_vlog(
2574 vport, KERN_INFO, LOG_MBOX | LOG_SLI,
2575 "0010 UNREG_LOGIN vpi:%x "
2576 "rpi:%x DID:%x defer x%x flg x%x "
James Smart32350662019-08-14 16:57:06 -07002577 "map:%x %px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002578 vport->vpi, ndlp->nlp_rpi,
2579 ndlp->nlp_DID, ndlp->nlp_defer_did,
2580 ndlp->nlp_flag,
2581 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002582 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002583 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002584
2585 /* Check to see if there are any deferred
2586 * events to process
2587 */
2588 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2589 (ndlp->nlp_defer_did !=
2590 NLP_EVT_NOTHING_PENDING)) {
2591 lpfc_printf_vlog(
2592 vport, KERN_INFO, LOG_DISCOVERY,
2593 "4111 UNREG cmpl deferred "
2594 "clr x%x on "
James Smart32350662019-08-14 16:57:06 -07002595 "NPort x%x Data: x%x x%px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002596 ndlp->nlp_rpi, ndlp->nlp_DID,
2597 ndlp->nlp_defer_did, ndlp);
James Smart00292e02018-12-13 15:17:55 -08002598 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002599 ndlp->nlp_defer_did =
2600 NLP_EVT_NOTHING_PENDING;
2601 lpfc_issue_els_plogi(
2602 vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002603 } else {
James Smartb95b2112019-08-14 16:56:47 -07002604 __lpfc_sli_rpi_release(vport, ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002605 }
James Smartbe6bb942015-04-07 15:07:22 -04002606 }
2607 }
2608 }
2609
2610 mempool_free(pmb, phba->mbox_mem_pool);
2611}
dea31012005-04-17 16:05:31 -05002612
James Smarte59058c2008-08-24 21:49:00 -04002613/**
James Smart3621a712009-04-06 18:47:14 -04002614 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002615 * @phba: Pointer to HBA context object.
2616 *
2617 * This function is called with no lock held. This function processes all
2618 * the completed mailbox commands and gives it to upper layers. The interrupt
2619 * service routine processes mailbox completion interrupt and adds completed
2620 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2621 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2622 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2623 * function returns the mailbox commands to the upper layer by calling the
2624 * completion handler function of each mailbox.
2625 **/
dea31012005-04-17 16:05:31 -05002626int
James Smart2e0fef82007-06-17 19:56:36 -05002627lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002628{
James Smart92d7f7b2007-06-17 19:56:38 -05002629 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002630 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002631 int rc;
2632 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002633
2634 phba->sli.slistat.mbox_event++;
2635
James Smart92d7f7b2007-06-17 19:56:38 -05002636 /* Get all completed mailboxe buffers into the cmplq */
2637 spin_lock_irq(&phba->hbalock);
2638 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2639 spin_unlock_irq(&phba->hbalock);
2640
dea31012005-04-17 16:05:31 -05002641 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002642 do {
2643 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2644 if (pmb == NULL)
2645 break;
2646
James Smart04c68492009-05-22 14:52:52 -04002647 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002648
James Smart858c9f62007-06-17 19:56:39 -05002649 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2650 if (pmb->vport) {
2651 lpfc_debugfs_disc_trc(pmb->vport,
2652 LPFC_DISC_TRC_MBOX_VPORT,
2653 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2654 (uint32_t)pmbox->mbxCommand,
2655 pmbox->un.varWords[0],
2656 pmbox->un.varWords[1]);
2657 }
2658 else {
2659 lpfc_debugfs_disc_trc(phba->pport,
2660 LPFC_DISC_TRC_MBOX,
2661 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2662 (uint32_t)pmbox->mbxCommand,
2663 pmbox->un.varWords[0],
2664 pmbox->un.varWords[1]);
2665 }
2666 }
2667
dea31012005-04-17 16:05:31 -05002668 /*
2669 * It is a fatal error if unknown mbox command completion.
2670 */
2671 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2672 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002673 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002674 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002675 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002676 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002677 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002678 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002679 lpfc_sli_config_mbox_subsys_get(phba,
2680 pmb),
2681 lpfc_sli_config_mbox_opcode_get(phba,
2682 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002683 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002684 phba->work_hs = HS_FFER3;
2685 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002686 continue;
dea31012005-04-17 16:05:31 -05002687 }
2688
dea31012005-04-17 16:05:31 -05002689 if (pmbox->mbxStatus) {
2690 phba->sli.slistat.mbox_stat_err++;
2691 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2692 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002693 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002694 LOG_MBOX | LOG_SLI,
2695 "(%d):0305 Mbox cmd cmpl "
2696 "error - RETRYing Data: x%x "
2697 "(x%x/x%x) x%x x%x x%x\n",
2698 pmb->vport ? pmb->vport->vpi : 0,
2699 pmbox->mbxCommand,
2700 lpfc_sli_config_mbox_subsys_get(phba,
2701 pmb),
2702 lpfc_sli_config_mbox_opcode_get(phba,
2703 pmb),
2704 pmbox->mbxStatus,
2705 pmbox->un.varWords[0],
2706 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002707 pmbox->mbxStatus = 0;
2708 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002709 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002710 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002711 continue;
dea31012005-04-17 16:05:31 -05002712 }
2713 }
2714
2715 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002716 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
Sakari Ailus2d44d162019-09-04 19:04:23 +03002717 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl %ps "
James Smarte74c03c2013-04-17 20:15:19 -04002718 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2719 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002720 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002721 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002722 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2723 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002724 pmb->mbox_cmpl,
2725 *((uint32_t *) pmbox),
2726 pmbox->un.varWords[0],
2727 pmbox->un.varWords[1],
2728 pmbox->un.varWords[2],
2729 pmbox->un.varWords[3],
2730 pmbox->un.varWords[4],
2731 pmbox->un.varWords[5],
2732 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002733 pmbox->un.varWords[7],
2734 pmbox->un.varWords[8],
2735 pmbox->un.varWords[9],
2736 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002737
James Smart92d7f7b2007-06-17 19:56:38 -05002738 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002739 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002740 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002741 return 0;
dea31012005-04-17 16:05:31 -05002742}
James Smart92d7f7b2007-06-17 19:56:38 -05002743
James Smarte59058c2008-08-24 21:49:00 -04002744/**
James Smart3621a712009-04-06 18:47:14 -04002745 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002746 * @phba: Pointer to HBA context object.
2747 * @pring: Pointer to driver SLI ring object.
2748 * @tag: buffer tag.
2749 *
2750 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2751 * is set in the tag the buffer is posted for a particular exchange,
2752 * the function will return the buffer without replacing the buffer.
2753 * If the buffer is for unsolicited ELS or CT traffic, this function
2754 * returns the buffer and also posts another buffer to the firmware.
2755 **/
James Smart76bb24e2007-10-27 13:38:00 -04002756static struct lpfc_dmabuf *
2757lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002758 struct lpfc_sli_ring *pring,
2759 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002760{
James Smart9f1e1b52008-12-04 22:39:40 -05002761 struct hbq_dmabuf *hbq_entry;
2762
James Smart76bb24e2007-10-27 13:38:00 -04002763 if (tag & QUE_BUFTAG_BIT)
2764 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002765 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2766 if (!hbq_entry)
2767 return NULL;
2768 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002769}
James Smart57127f12007-10-27 13:37:05 -04002770
James Smart3772a992009-05-22 14:50:54 -04002771/**
2772 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2773 * @phba: Pointer to HBA context object.
2774 * @pring: Pointer to driver SLI ring object.
2775 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2776 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2777 * @fch_type: the type for the first frame of the sequence.
2778 *
2779 * This function is called with no lock held. This function uses the r_ctl and
2780 * type of the received sequence to find the correct callback function to call
2781 * to process the sequence.
2782 **/
2783static int
2784lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2785 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2786 uint32_t fch_type)
2787{
2788 int i;
2789
James Smartf358dd02017-02-12 13:52:34 -08002790 switch (fch_type) {
2791 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002792 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002793 return 1;
2794 default:
2795 break;
2796 }
2797
James Smart3772a992009-05-22 14:50:54 -04002798 /* unSolicited Responses */
2799 if (pring->prt[0].profile) {
2800 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2801 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2802 saveq);
2803 return 1;
2804 }
2805 /* We must search, based on rctl / type
2806 for the right routine */
2807 for (i = 0; i < pring->num_mask; i++) {
2808 if ((pring->prt[i].rctl == fch_r_ctl) &&
2809 (pring->prt[i].type == fch_type)) {
2810 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2811 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2812 (phba, pring, saveq);
2813 return 1;
2814 }
2815 }
2816 return 0;
2817}
James Smarte59058c2008-08-24 21:49:00 -04002818
2819/**
James Smart3621a712009-04-06 18:47:14 -04002820 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002821 * @phba: Pointer to HBA context object.
2822 * @pring: Pointer to driver SLI ring object.
2823 * @saveq: Pointer to the unsolicited iocb.
2824 *
2825 * This function is called with no lock held by the ring event handler
2826 * when there is an unsolicited iocb posted to the response ring by the
2827 * firmware. This function gets the buffer associated with the iocbs
2828 * and calls the event handler for the ring. This function handles both
2829 * qring buffers and hbq buffers.
2830 * When the function returns 1 the caller can free the iocb object otherwise
2831 * upper layer functions will free the iocb objects.
2832 **/
dea31012005-04-17 16:05:31 -05002833static int
2834lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2835 struct lpfc_iocbq *saveq)
2836{
2837 IOCB_t * irsp;
2838 WORD5 * w5p;
2839 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002840 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002841 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002842
dea31012005-04-17 16:05:31 -05002843 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002844
2845 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2846 if (pring->lpfc_sli_rcv_async_status)
2847 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2848 else
2849 lpfc_printf_log(phba,
2850 KERN_WARNING,
2851 LOG_SLI,
2852 "0316 Ring %d handler: unexpected "
2853 "ASYNC_STATUS iocb received evt_code "
2854 "0x%x\n",
2855 pring->ringno,
2856 irsp->un.asyncstat.evt_code);
2857 return 1;
2858 }
2859
James Smart3163f722008-02-08 18:50:25 -05002860 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2861 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2862 if (irsp->ulpBdeCount > 0) {
2863 dmzbuf = lpfc_sli_get_buff(phba, pring,
2864 irsp->un.ulpWord[3]);
2865 lpfc_in_buf_free(phba, dmzbuf);
2866 }
2867
2868 if (irsp->ulpBdeCount > 1) {
2869 dmzbuf = lpfc_sli_get_buff(phba, pring,
2870 irsp->unsli3.sli3Words[3]);
2871 lpfc_in_buf_free(phba, dmzbuf);
2872 }
2873
2874 if (irsp->ulpBdeCount > 2) {
2875 dmzbuf = lpfc_sli_get_buff(phba, pring,
2876 irsp->unsli3.sli3Words[7]);
2877 lpfc_in_buf_free(phba, dmzbuf);
2878 }
2879
2880 return 1;
2881 }
2882
James Smart92d7f7b2007-06-17 19:56:38 -05002883 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002884 if (irsp->ulpBdeCount != 0) {
2885 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002886 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002887 if (!saveq->context2)
2888 lpfc_printf_log(phba,
2889 KERN_ERR,
2890 LOG_SLI,
2891 "0341 Ring %d Cannot find buffer for "
2892 "an unsolicited iocb. tag 0x%x\n",
2893 pring->ringno,
2894 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002895 }
2896 if (irsp->ulpBdeCount == 2) {
2897 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002898 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002899 if (!saveq->context3)
2900 lpfc_printf_log(phba,
2901 KERN_ERR,
2902 LOG_SLI,
2903 "0342 Ring %d Cannot find buffer for an"
2904 " unsolicited iocb. tag 0x%x\n",
2905 pring->ringno,
2906 irsp->unsli3.sli3Words[7]);
2907 }
2908 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002909 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002910 if (irsp->ulpBdeCount != 0) {
2911 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2912 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002913 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002914 lpfc_printf_log(phba,
2915 KERN_ERR,
2916 LOG_SLI,
2917 "0343 Ring %d Cannot find "
2918 "buffer for an unsolicited iocb"
2919 ". tag 0x%x\n", pring->ringno,
2920 irsp->un.ulpWord[3]);
2921 }
2922 if (irsp->ulpBdeCount == 2) {
2923 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2924 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002925 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002926 lpfc_printf_log(phba,
2927 KERN_ERR,
2928 LOG_SLI,
2929 "0344 Ring %d Cannot find "
2930 "buffer for an unsolicited "
2931 "iocb. tag 0x%x\n",
2932 pring->ringno,
2933 irsp->unsli3.sli3Words[7]);
2934 }
2935 }
James Smart92d7f7b2007-06-17 19:56:38 -05002936 }
James Smart9c2face2008-01-11 01:53:18 -05002937 if (irsp->ulpBdeCount != 0 &&
2938 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2939 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2940 int found = 0;
2941
2942 /* search continue save q for same XRI */
2943 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002944 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2945 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002946 list_add_tail(&saveq->list, &iocbq->list);
2947 found = 1;
2948 break;
2949 }
2950 }
2951 if (!found)
2952 list_add_tail(&saveq->clist,
2953 &pring->iocb_continue_saveq);
2954 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2955 list_del_init(&iocbq->clist);
2956 saveq = iocbq;
2957 irsp = &(saveq->iocb);
2958 } else
2959 return 0;
2960 }
2961 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2962 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2963 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002964 Rctl = FC_RCTL_ELS_REQ;
2965 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002966 } else {
2967 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2968 Rctl = w5p->hcsw.Rctl;
2969 Type = w5p->hcsw.Type;
2970
2971 /* Firmware Workaround */
2972 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2973 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2974 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002975 Rctl = FC_RCTL_ELS_REQ;
2976 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002977 w5p->hcsw.Rctl = Rctl;
2978 w5p->hcsw.Type = Type;
2979 }
2980 }
James Smart92d7f7b2007-06-17 19:56:38 -05002981
James Smart3772a992009-05-22 14:50:54 -04002982 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002983 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002984 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002985 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002986 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002987
James Smart92d7f7b2007-06-17 19:56:38 -05002988 return 1;
dea31012005-04-17 16:05:31 -05002989}
2990
James Smarte59058c2008-08-24 21:49:00 -04002991/**
James Smart3621a712009-04-06 18:47:14 -04002992 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002993 * @phba: Pointer to HBA context object.
2994 * @pring: Pointer to driver SLI ring object.
2995 * @prspiocb: Pointer to response iocb object.
2996 *
2997 * This function looks up the iocb_lookup table to get the command iocb
2998 * corresponding to the given response iocb using the iotag of the
James Smarte2a8be52019-05-06 17:26:47 -07002999 * response iocb. The driver calls this function with the hbalock held
3000 * for SLI3 ports or the ring lock held for SLI4 ports.
James Smarte59058c2008-08-24 21:49:00 -04003001 * This function returns the command iocb object if it finds the command
3002 * iocb else returns NULL.
3003 **/
dea31012005-04-17 16:05:31 -05003004static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05003005lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
3006 struct lpfc_sli_ring *pring,
3007 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05003008{
dea31012005-04-17 16:05:31 -05003009 struct lpfc_iocbq *cmd_iocb = NULL;
3010 uint16_t iotag;
James Smarte2a8be52019-05-06 17:26:47 -07003011 spinlock_t *temp_lock = NULL;
3012 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05003013
James Smarte2a8be52019-05-06 17:26:47 -07003014 if (phba->sli_rev == LPFC_SLI_REV4)
3015 temp_lock = &pring->ring_lock;
3016 else
3017 temp_lock = &phba->hbalock;
3018
3019 spin_lock_irqsave(temp_lock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003020 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05003021
James Bottomley604a3e32005-10-29 10:28:33 -05003022 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3023 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003024 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07003025 /* remove from txcmpl queue list */
3026 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04003027 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003028 pring->txcmplq_cnt--;
James Smarte2a8be52019-05-06 17:26:47 -07003029 spin_unlock_irqrestore(temp_lock, iflag);
James Smart89533e92016-10-13 15:06:15 -07003030 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003031 }
dea31012005-04-17 16:05:31 -05003032 }
3033
James Smarte2a8be52019-05-06 17:26:47 -07003034 spin_unlock_irqrestore(temp_lock, iflag);
dea31012005-04-17 16:05:31 -05003035 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07003036 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05003037 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003038 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05003039 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05003040 return NULL;
3041}
3042
James Smarte59058c2008-08-24 21:49:00 -04003043/**
James Smart3772a992009-05-22 14:50:54 -04003044 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3045 * @phba: Pointer to HBA context object.
3046 * @pring: Pointer to driver SLI ring object.
3047 * @iotag: IOCB tag.
3048 *
3049 * This function looks up the iocb_lookup table to get the command iocb
James Smarte2a8be52019-05-06 17:26:47 -07003050 * corresponding to the given iotag. The driver calls this function with
3051 * the ring lock held because this function is an SLI4 port only helper.
James Smart3772a992009-05-22 14:50:54 -04003052 * This function returns the command iocb object if it finds the command
3053 * iocb else returns NULL.
3054 **/
3055static struct lpfc_iocbq *
3056lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3057 struct lpfc_sli_ring *pring, uint16_t iotag)
3058{
James Smart895427b2017-02-12 13:52:30 -08003059 struct lpfc_iocbq *cmd_iocb = NULL;
James Smarte2a8be52019-05-06 17:26:47 -07003060 spinlock_t *temp_lock = NULL;
3061 unsigned long iflag = 0;
James Smart3772a992009-05-22 14:50:54 -04003062
James Smarte2a8be52019-05-06 17:26:47 -07003063 if (phba->sli_rev == LPFC_SLI_REV4)
3064 temp_lock = &pring->ring_lock;
3065 else
3066 temp_lock = &phba->hbalock;
3067
3068 spin_lock_irqsave(temp_lock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003069 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3070 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003071 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3072 /* remove from txcmpl queue list */
3073 list_del_init(&cmd_iocb->list);
3074 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003075 pring->txcmplq_cnt--;
James Smarte2a8be52019-05-06 17:26:47 -07003076 spin_unlock_irqrestore(temp_lock, iflag);
James Smart4f2e66c2012-05-09 21:17:07 -04003077 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003078 }
James Smart3772a992009-05-22 14:50:54 -04003079 }
James Smart89533e92016-10-13 15:06:15 -07003080
James Smarte2a8be52019-05-06 17:26:47 -07003081 spin_unlock_irqrestore(temp_lock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003082 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08003083 "0372 iotag x%x lookup error: max iotag (x%x) "
3084 "iocb_flag x%x\n",
3085 iotag, phba->sli.last_iotag,
3086 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04003087 return NULL;
3088}
3089
3090/**
James Smart3621a712009-04-06 18:47:14 -04003091 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04003092 * @phba: Pointer to HBA context object.
3093 * @pring: Pointer to driver SLI ring object.
3094 * @saveq: Pointer to the response iocb to be processed.
3095 *
3096 * This function is called by the ring event handler for non-fcp
3097 * rings when there is a new response iocb in the response ring.
3098 * The caller is not required to hold any locks. This function
3099 * gets the command iocb associated with the response iocb and
3100 * calls the completion handler for the command iocb. If there
3101 * is no completion handler, the function will free the resources
3102 * associated with command iocb. If the response iocb is for
3103 * an already aborted command iocb, the status of the completion
3104 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3105 * This function always returns 1.
3106 **/
dea31012005-04-17 16:05:31 -05003107static int
James Smart2e0fef82007-06-17 19:56:36 -05003108lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003109 struct lpfc_iocbq *saveq)
3110{
James Smart2e0fef82007-06-17 19:56:36 -05003111 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05003112 int rc = 1;
3113 unsigned long iflag;
3114
James Bottomley604a3e32005-10-29 10:28:33 -05003115 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
dea31012005-04-17 16:05:31 -05003116 if (cmdiocbp) {
3117 if (cmdiocbp->iocb_cmpl) {
3118 /*
James Smartea2151b2008-09-07 11:52:10 -04003119 * If an ELS command failed send an event to mgmt
3120 * application.
3121 */
3122 if (saveq->iocb.ulpStatus &&
3123 (pring->ringno == LPFC_ELS_RING) &&
3124 (cmdiocbp->iocb.ulpCommand ==
3125 CMD_ELS_REQUEST64_CR))
3126 lpfc_send_els_failure_event(phba,
3127 cmdiocbp, saveq);
3128
3129 /*
dea31012005-04-17 16:05:31 -05003130 * Post all ELS completions to the worker thread.
3131 * All other are passed to the completion callback.
3132 */
3133 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05003134 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3135 (cmdiocbp->iocb_flag &
3136 LPFC_DRIVER_ABORTED)) {
3137 spin_lock_irqsave(&phba->hbalock,
3138 iflag);
James Smart07951072007-04-25 09:51:38 -04003139 cmdiocbp->iocb_flag &=
3140 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05003141 spin_unlock_irqrestore(&phba->hbalock,
3142 iflag);
James Smart07951072007-04-25 09:51:38 -04003143 saveq->iocb.ulpStatus =
3144 IOSTAT_LOCAL_REJECT;
3145 saveq->iocb.un.ulpWord[4] =
3146 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05003147
3148 /* Firmware could still be in progress
3149 * of DMAing payload, so don't free data
3150 * buffer till after a hbeat.
3151 */
James Smart341af102010-01-26 23:07:37 -05003152 spin_lock_irqsave(&phba->hbalock,
3153 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05003154 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05003155 spin_unlock_irqrestore(&phba->hbalock,
3156 iflag);
3157 }
James Smart0f65ff62010-02-26 14:14:23 -05003158 if (phba->sli_rev == LPFC_SLI_REV4) {
3159 if (saveq->iocb_flag &
3160 LPFC_EXCHANGE_BUSY) {
3161 /* Set cmdiocb flag for the
3162 * exchange busy so sgl (xri)
3163 * will not be released until
3164 * the abort xri is received
3165 * from hba.
3166 */
3167 spin_lock_irqsave(
3168 &phba->hbalock, iflag);
3169 cmdiocbp->iocb_flag |=
3170 LPFC_EXCHANGE_BUSY;
3171 spin_unlock_irqrestore(
3172 &phba->hbalock, iflag);
3173 }
3174 if (cmdiocbp->iocb_flag &
3175 LPFC_DRIVER_ABORTED) {
3176 /*
3177 * Clear LPFC_DRIVER_ABORTED
3178 * bit in case it was driver
3179 * initiated abort.
3180 */
3181 spin_lock_irqsave(
3182 &phba->hbalock, iflag);
3183 cmdiocbp->iocb_flag &=
3184 ~LPFC_DRIVER_ABORTED;
3185 spin_unlock_irqrestore(
3186 &phba->hbalock, iflag);
3187 cmdiocbp->iocb.ulpStatus =
3188 IOSTAT_LOCAL_REJECT;
3189 cmdiocbp->iocb.un.ulpWord[4] =
3190 IOERR_ABORT_REQUESTED;
3191 /*
3192 * For SLI4, irsiocb contains
3193 * NO_XRI in sli_xritag, it
3194 * shall not affect releasing
3195 * sgl (xri) process.
3196 */
3197 saveq->iocb.ulpStatus =
3198 IOSTAT_LOCAL_REJECT;
3199 saveq->iocb.un.ulpWord[4] =
3200 IOERR_SLI_ABORTED;
3201 spin_lock_irqsave(
3202 &phba->hbalock, iflag);
3203 saveq->iocb_flag |=
3204 LPFC_DELAY_MEM_FREE;
3205 spin_unlock_irqrestore(
3206 &phba->hbalock, iflag);
3207 }
James Smart07951072007-04-25 09:51:38 -04003208 }
dea31012005-04-17 16:05:31 -05003209 }
James Smart2e0fef82007-06-17 19:56:36 -05003210 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003211 } else
3212 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003213 } else {
3214 /*
3215 * Unknown initiating command based on the response iotag.
3216 * This could be the case on the ELS ring because of
3217 * lpfc_els_abort().
3218 */
3219 if (pring->ringno != LPFC_ELS_RING) {
3220 /*
3221 * Ring <ringno> handler: unexpected completion IoTag
3222 * <IoTag>
3223 */
James Smarta257bf92009-04-06 18:48:10 -04003224 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003225 "0322 Ring %d handler: "
3226 "unexpected completion IoTag x%x "
3227 "Data: x%x x%x x%x x%x\n",
3228 pring->ringno,
3229 saveq->iocb.ulpIoTag,
3230 saveq->iocb.ulpStatus,
3231 saveq->iocb.un.ulpWord[4],
3232 saveq->iocb.ulpCommand,
3233 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003234 }
3235 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003236
dea31012005-04-17 16:05:31 -05003237 return rc;
3238}
3239
James Smarte59058c2008-08-24 21:49:00 -04003240/**
James Smart3621a712009-04-06 18:47:14 -04003241 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003242 * @phba: Pointer to HBA context object.
3243 * @pring: Pointer to driver SLI ring object.
3244 *
3245 * This function is called from the iocb ring event handlers when
3246 * put pointer is ahead of the get pointer for a ring. This function signal
3247 * an error attention condition to the worker thread and the worker
3248 * thread will transition the HBA to offline state.
3249 **/
James Smart2e0fef82007-06-17 19:56:36 -05003250static void
3251lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003252{
James Smart34b02dc2008-08-24 21:49:55 -04003253 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003254 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003255 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003256 * rsp ring <portRspMax>
3257 */
3258 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003259 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003260 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003261 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003262 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003263
James Smart2e0fef82007-06-17 19:56:36 -05003264 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003265
3266 /*
3267 * All error attention handlers are posted to
3268 * worker thread
3269 */
3270 phba->work_ha |= HA_ERATT;
3271 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003272
James Smart5e9d9b82008-06-14 22:52:53 -04003273 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003274
3275 return;
3276}
3277
James Smarte59058c2008-08-24 21:49:00 -04003278/**
James Smart3621a712009-04-06 18:47:14 -04003279 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04003280 * @ptr: Pointer to address of HBA context object.
3281 *
3282 * This function is invoked by the Error Attention polling timer when the
3283 * timer times out. It will check the SLI Error Attention register for
3284 * possible attention events. If so, it will post an Error Attention event
3285 * and wake up worker thread to process it. Otherwise, it will set up the
3286 * Error Attention polling timer for the next poll.
3287 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003288void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003289{
3290 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003291 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003292 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003293
Kees Cookf22eb4d2017-09-06 20:24:26 -07003294 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003295
James Smartaa6fbb72012-08-03 12:36:03 -04003296 /* Here we will also keep track of interrupts per sec of the hba */
3297 sli_intr = phba->sli.slistat.sli_intr;
3298
3299 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3300 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3301 sli_intr);
3302 else
3303 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3304
James Smart65791f12016-07-06 12:35:56 -07003305 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3306 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003307 phba->sli.slistat.sli_ips = cnt;
3308
3309 phba->sli.slistat.sli_prev_intr = sli_intr;
3310
James Smart93996272008-08-24 21:50:30 -04003311 /* Check chip HA register for error event */
3312 eratt = lpfc_sli_check_eratt(phba);
3313
3314 if (eratt)
3315 /* Tell the worker thread there is work to do */
3316 lpfc_worker_wake_up(phba);
3317 else
3318 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003319 mod_timer(&phba->eratt_poll,
3320 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003321 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003322 return;
3323}
3324
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003325
James Smarte59058c2008-08-24 21:49:00 -04003326/**
James Smart3621a712009-04-06 18:47:14 -04003327 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003328 * @phba: Pointer to HBA context object.
3329 * @pring: Pointer to driver SLI ring object.
3330 * @mask: Host attention register mask for this ring.
3331 *
3332 * This function is called from the interrupt context when there is a ring
3333 * event for the fcp ring. The caller does not hold any lock.
3334 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003335 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003336 * LE bit set. The function will call the completion handler of the command iocb
3337 * if the response iocb indicates a completion for a command iocb or it is
3338 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3339 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003340 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003341 * to check it explicitly.
3342 */
3343int
James Smart2e0fef82007-06-17 19:56:36 -05003344lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3345 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003346{
James Smart34b02dc2008-08-24 21:49:55 -04003347 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003348 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003349 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003350 struct lpfc_iocbq *cmdiocbq = NULL;
3351 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003352 uint32_t status;
3353 uint32_t portRspPut, portRspMax;
3354 int rc = 1;
3355 lpfc_iocb_type type;
3356 unsigned long iflag;
3357 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003358
James Smart2e0fef82007-06-17 19:56:36 -05003359 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003360 pring->stats.iocb_event++;
3361
dea31012005-04-17 16:05:31 -05003362 /*
3363 * The next available response entry should never exceed the maximum
3364 * entries. If it does, treat it as an adapter hardware error.
3365 */
James Smart7e56aa22012-08-03 12:35:34 -04003366 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003367 portRspPut = le32_to_cpu(pgp->rspPutInx);
3368 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003369 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003370 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003371 return 1;
3372 }
James Smart45ed1192009-10-02 15:17:02 -04003373 if (phba->fcp_ring_in_use) {
3374 spin_unlock_irqrestore(&phba->hbalock, iflag);
3375 return 1;
3376 } else
3377 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003378
3379 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003380 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003381 /*
3382 * Fetch an entry off the ring and copy it into a local data
3383 * structure. The copy involves a byte-swap since the
3384 * network byte order and pci byte orders are different.
3385 */
James Smarted957682007-06-17 19:56:37 -05003386 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003387 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003388
James Smart7e56aa22012-08-03 12:35:34 -04003389 if (++pring->sli.sli3.rspidx >= portRspMax)
3390 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003391
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003392 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3393 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003394 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003395 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003396 irsp = &rspiocbq.iocb;
3397
dea31012005-04-17 16:05:31 -05003398 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3399 pring->stats.iocb_rsp++;
3400 rsp_cmpl++;
3401
3402 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003403 /*
3404 * If resource errors reported from HBA, reduce
3405 * queuedepths of the SCSI device.
3406 */
3407 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003408 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3409 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003410 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003411 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003412 spin_lock_irqsave(&phba->hbalock, iflag);
3413 }
3414
dea31012005-04-17 16:05:31 -05003415 /* Rsp ring <ringno> error: IOCB */
3416 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003417 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003418 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003419 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003420 irsp->un.ulpWord[0],
3421 irsp->un.ulpWord[1],
3422 irsp->un.ulpWord[2],
3423 irsp->un.ulpWord[3],
3424 irsp->un.ulpWord[4],
3425 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003426 *(uint32_t *)&irsp->un1,
3427 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003428 }
3429
3430 switch (type) {
3431 case LPFC_ABORT_IOCB:
3432 case LPFC_SOL_IOCB:
3433 /*
3434 * Idle exchange closed via ABTS from port. No iocb
3435 * resources need to be recovered.
3436 */
3437 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003438 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003439 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003440 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003441 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003442 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003443 break;
3444 }
3445
James Smarte2a8be52019-05-06 17:26:47 -07003446 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003447 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3448 &rspiocbq);
James Smarte2a8be52019-05-06 17:26:47 -07003449 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05003450 if (unlikely(!cmdiocbq))
3451 break;
3452 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3453 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3454 if (cmdiocbq->iocb_cmpl) {
3455 spin_unlock_irqrestore(&phba->hbalock, iflag);
3456 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3457 &rspiocbq);
3458 spin_lock_irqsave(&phba->hbalock, iflag);
3459 }
dea31012005-04-17 16:05:31 -05003460 break;
James Smarta4bc3372006-12-02 13:34:16 -05003461 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003462 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003463 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003464 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003465 break;
dea31012005-04-17 16:05:31 -05003466 default:
3467 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3468 char adaptermsg[LPFC_MAX_ADPTMSG];
3469 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3470 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3471 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003472 dev_warn(&((phba->pcidev)->dev),
3473 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003474 phba->brd_no, adaptermsg);
3475 } else {
3476 /* Unknown IOCB command */
3477 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003478 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003479 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003480 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003481 irsp->ulpStatus,
3482 irsp->ulpIoTag,
3483 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003484 }
3485 break;
3486 }
3487
3488 /*
3489 * The response IOCB has been processed. Update the ring
3490 * pointer in SLIM. If the port response put pointer has not
3491 * been updated, sync the pgp->rspPutInx and fetch the new port
3492 * response put pointer.
3493 */
James Smart7e56aa22012-08-03 12:35:34 -04003494 writel(pring->sli.sli3.rspidx,
3495 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003496
James Smart7e56aa22012-08-03 12:35:34 -04003497 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003498 portRspPut = le32_to_cpu(pgp->rspPutInx);
3499 }
3500
3501 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3502 pring->stats.iocb_rsp_full++;
3503 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3504 writel(status, phba->CAregaddr);
3505 readl(phba->CAregaddr);
3506 }
3507 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3508 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3509 pring->stats.iocb_cmd_empty++;
3510
3511 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003512 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003513 lpfc_sli_resume_iocb(phba, pring);
3514
3515 if ((pring->lpfc_sli_cmd_available))
3516 (pring->lpfc_sli_cmd_available) (phba, pring);
3517
3518 }
3519
James Smart45ed1192009-10-02 15:17:02 -04003520 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003521 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003522 return rc;
3523}
3524
James Smarte59058c2008-08-24 21:49:00 -04003525/**
James Smart3772a992009-05-22 14:50:54 -04003526 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3527 * @phba: Pointer to HBA context object.
3528 * @pring: Pointer to driver SLI ring object.
3529 * @rspiocbp: Pointer to driver response IOCB object.
3530 *
3531 * This function is called from the worker thread when there is a slow-path
3532 * response IOCB to process. This function chains all the response iocbs until
3533 * seeing the iocb with the LE bit set. The function will call
3534 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3535 * completion of a command iocb. The function will call the
3536 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3537 * The function frees the resources or calls the completion handler if this
3538 * iocb is an abort completion. The function returns NULL when the response
3539 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3540 * this function shall chain the iocb on to the iocb_continueq and return the
3541 * response iocb passed in.
3542 **/
3543static struct lpfc_iocbq *
3544lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3545 struct lpfc_iocbq *rspiocbp)
3546{
3547 struct lpfc_iocbq *saveq;
3548 struct lpfc_iocbq *cmdiocbp;
3549 struct lpfc_iocbq *next_iocb;
3550 IOCB_t *irsp = NULL;
3551 uint32_t free_saveq;
3552 uint8_t iocb_cmd_type;
3553 lpfc_iocb_type type;
3554 unsigned long iflag;
3555 int rc;
3556
3557 spin_lock_irqsave(&phba->hbalock, iflag);
3558 /* First add the response iocb to the countinueq list */
3559 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3560 pring->iocb_continueq_cnt++;
3561
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003562 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003563 irsp = &rspiocbp->iocb;
3564 if (irsp->ulpLe) {
3565 /*
3566 * By default, the driver expects to free all resources
3567 * associated with this iocb completion.
3568 */
3569 free_saveq = 1;
3570 saveq = list_get_first(&pring->iocb_continueq,
3571 struct lpfc_iocbq, list);
3572 irsp = &(saveq->iocb);
3573 list_del_init(&pring->iocb_continueq);
3574 pring->iocb_continueq_cnt = 0;
3575
3576 pring->stats.iocb_rsp++;
3577
3578 /*
3579 * If resource errors reported from HBA, reduce
3580 * queuedepths of the SCSI device.
3581 */
3582 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003583 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3584 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003585 spin_unlock_irqrestore(&phba->hbalock, iflag);
3586 phba->lpfc_rampdown_queue_depth(phba);
3587 spin_lock_irqsave(&phba->hbalock, iflag);
3588 }
3589
3590 if (irsp->ulpStatus) {
3591 /* Rsp ring <ringno> error: IOCB */
3592 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3593 "0328 Rsp Ring %d error: "
3594 "IOCB Data: "
3595 "x%x x%x x%x x%x "
3596 "x%x x%x x%x x%x "
3597 "x%x x%x x%x x%x "
3598 "x%x x%x x%x x%x\n",
3599 pring->ringno,
3600 irsp->un.ulpWord[0],
3601 irsp->un.ulpWord[1],
3602 irsp->un.ulpWord[2],
3603 irsp->un.ulpWord[3],
3604 irsp->un.ulpWord[4],
3605 irsp->un.ulpWord[5],
3606 *(((uint32_t *) irsp) + 6),
3607 *(((uint32_t *) irsp) + 7),
3608 *(((uint32_t *) irsp) + 8),
3609 *(((uint32_t *) irsp) + 9),
3610 *(((uint32_t *) irsp) + 10),
3611 *(((uint32_t *) irsp) + 11),
3612 *(((uint32_t *) irsp) + 12),
3613 *(((uint32_t *) irsp) + 13),
3614 *(((uint32_t *) irsp) + 14),
3615 *(((uint32_t *) irsp) + 15));
3616 }
3617
3618 /*
3619 * Fetch the IOCB command type and call the correct completion
3620 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3621 * get freed back to the lpfc_iocb_list by the discovery
3622 * kernel thread.
3623 */
3624 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3625 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3626 switch (type) {
3627 case LPFC_SOL_IOCB:
3628 spin_unlock_irqrestore(&phba->hbalock, iflag);
3629 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3630 spin_lock_irqsave(&phba->hbalock, iflag);
3631 break;
3632
3633 case LPFC_UNSOL_IOCB:
3634 spin_unlock_irqrestore(&phba->hbalock, iflag);
3635 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3636 spin_lock_irqsave(&phba->hbalock, iflag);
3637 if (!rc)
3638 free_saveq = 0;
3639 break;
3640
3641 case LPFC_ABORT_IOCB:
3642 cmdiocbp = NULL;
James Smarte2a8be52019-05-06 17:26:47 -07003643 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX) {
3644 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003645 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3646 saveq);
James Smarte2a8be52019-05-06 17:26:47 -07003647 spin_lock_irqsave(&phba->hbalock, iflag);
3648 }
James Smart3772a992009-05-22 14:50:54 -04003649 if (cmdiocbp) {
3650 /* Call the specified completion routine */
3651 if (cmdiocbp->iocb_cmpl) {
3652 spin_unlock_irqrestore(&phba->hbalock,
3653 iflag);
3654 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3655 saveq);
3656 spin_lock_irqsave(&phba->hbalock,
3657 iflag);
3658 } else
3659 __lpfc_sli_release_iocbq(phba,
3660 cmdiocbp);
3661 }
3662 break;
3663
3664 case LPFC_UNKNOWN_IOCB:
3665 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3666 char adaptermsg[LPFC_MAX_ADPTMSG];
3667 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3668 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3669 MAX_MSG_DATA);
3670 dev_warn(&((phba->pcidev)->dev),
3671 "lpfc%d: %s\n",
3672 phba->brd_no, adaptermsg);
3673 } else {
3674 /* Unknown IOCB command */
3675 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3676 "0335 Unknown IOCB "
3677 "command Data: x%x "
3678 "x%x x%x x%x\n",
3679 irsp->ulpCommand,
3680 irsp->ulpStatus,
3681 irsp->ulpIoTag,
3682 irsp->ulpContext);
3683 }
3684 break;
3685 }
3686
3687 if (free_saveq) {
3688 list_for_each_entry_safe(rspiocbp, next_iocb,
3689 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003690 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003691 __lpfc_sli_release_iocbq(phba, rspiocbp);
3692 }
3693 __lpfc_sli_release_iocbq(phba, saveq);
3694 }
3695 rspiocbp = NULL;
3696 }
3697 spin_unlock_irqrestore(&phba->hbalock, iflag);
3698 return rspiocbp;
3699}
3700
3701/**
3702 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003703 * @phba: Pointer to HBA context object.
3704 * @pring: Pointer to driver SLI ring object.
3705 * @mask: Host attention register mask for this ring.
3706 *
James Smart3772a992009-05-22 14:50:54 -04003707 * This routine wraps the actual slow_ring event process routine from the
3708 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003709 **/
James Smart3772a992009-05-22 14:50:54 -04003710void
James Smart2e0fef82007-06-17 19:56:36 -05003711lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3712 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003713{
James Smart3772a992009-05-22 14:50:54 -04003714 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3715}
3716
3717/**
3718 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3719 * @phba: Pointer to HBA context object.
3720 * @pring: Pointer to driver SLI ring object.
3721 * @mask: Host attention register mask for this ring.
3722 *
3723 * This function is called from the worker thread when there is a ring event
3724 * for non-fcp rings. The caller does not hold any lock. The function will
3725 * remove each response iocb in the response ring and calls the handle
3726 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3727 **/
3728static void
3729lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3730 struct lpfc_sli_ring *pring, uint32_t mask)
3731{
James Smart34b02dc2008-08-24 21:49:55 -04003732 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003733 IOCB_t *entry;
3734 IOCB_t *irsp = NULL;
3735 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003736 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003737 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003738 uint32_t status;
dea31012005-04-17 16:05:31 -05003739
James Smart34b02dc2008-08-24 21:49:55 -04003740 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003741 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003742 pring->stats.iocb_event++;
3743
dea31012005-04-17 16:05:31 -05003744 /*
3745 * The next available response entry should never exceed the maximum
3746 * entries. If it does, treat it as an adapter hardware error.
3747 */
James Smart7e56aa22012-08-03 12:35:34 -04003748 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003749 portRspPut = le32_to_cpu(pgp->rspPutInx);
3750 if (portRspPut >= portRspMax) {
3751 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003752 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003753 * rsp ring <portRspMax>
3754 */
James Smarted957682007-06-17 19:56:37 -05003755 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003756 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003757 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003758 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003759
James Smart2e0fef82007-06-17 19:56:36 -05003760 phba->link_state = LPFC_HBA_ERROR;
3761 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003762
3763 phba->work_hs = HS_FFER3;
3764 lpfc_handle_eratt(phba);
3765
James Smart3772a992009-05-22 14:50:54 -04003766 return;
dea31012005-04-17 16:05:31 -05003767 }
3768
3769 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003770 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003771 /*
3772 * Build a completion list and call the appropriate handler.
3773 * The process is to get the next available response iocb, get
3774 * a free iocb from the list, copy the response data into the
3775 * free iocb, insert to the continuation list, and update the
3776 * next response index to slim. This process makes response
3777 * iocb's in the ring available to DMA as fast as possible but
3778 * pays a penalty for a copy operation. Since the iocb is
3779 * only 32 bytes, this penalty is considered small relative to
3780 * the PCI reads for register values and a slim write. When
3781 * the ulpLe field is set, the entire Command has been
3782 * received.
3783 */
James Smarted957682007-06-17 19:56:37 -05003784 entry = lpfc_resp_iocb(phba, pring);
3785
James Smart858c9f62007-06-17 19:56:39 -05003786 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003787 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003788 if (rspiocbp == NULL) {
3789 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003790 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003791 break;
3792 }
3793
James Smarted957682007-06-17 19:56:37 -05003794 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3795 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003796 irsp = &rspiocbp->iocb;
3797
James Smart7e56aa22012-08-03 12:35:34 -04003798 if (++pring->sli.sli3.rspidx >= portRspMax)
3799 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003800
James Smarta58cbd52007-08-02 11:09:43 -04003801 if (pring->ringno == LPFC_ELS_RING) {
3802 lpfc_debugfs_slow_ring_trc(phba,
3803 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3804 *(((uint32_t *) irsp) + 4),
3805 *(((uint32_t *) irsp) + 6),
3806 *(((uint32_t *) irsp) + 7));
3807 }
3808
James Smart7e56aa22012-08-03 12:35:34 -04003809 writel(pring->sli.sli3.rspidx,
3810 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003811
James Smart3772a992009-05-22 14:50:54 -04003812 spin_unlock_irqrestore(&phba->hbalock, iflag);
3813 /* Handle the response IOCB */
3814 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3815 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003816
3817 /*
3818 * If the port response put pointer has not been updated, sync
3819 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3820 * response put pointer.
3821 */
James Smart7e56aa22012-08-03 12:35:34 -04003822 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003823 portRspPut = le32_to_cpu(pgp->rspPutInx);
3824 }
James Smart7e56aa22012-08-03 12:35:34 -04003825 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003826
James Smart92d7f7b2007-06-17 19:56:38 -05003827 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003828 /* At least one response entry has been freed */
3829 pring->stats.iocb_rsp_full++;
3830 /* SET RxRE_RSP in Chip Att register */
3831 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3832 writel(status, phba->CAregaddr);
3833 readl(phba->CAregaddr); /* flush */
3834 }
3835 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3836 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3837 pring->stats.iocb_cmd_empty++;
3838
3839 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003840 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003841 lpfc_sli_resume_iocb(phba, pring);
3842
3843 if ((pring->lpfc_sli_cmd_available))
3844 (pring->lpfc_sli_cmd_available) (phba, pring);
3845
3846 }
3847
James Smart2e0fef82007-06-17 19:56:36 -05003848 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003849 return;
dea31012005-04-17 16:05:31 -05003850}
3851
James Smarte59058c2008-08-24 21:49:00 -04003852/**
James Smart4f774512009-05-22 14:52:35 -04003853 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3854 * @phba: Pointer to HBA context object.
3855 * @pring: Pointer to driver SLI ring object.
3856 * @mask: Host attention register mask for this ring.
3857 *
3858 * This function is called from the worker thread when there is a pending
3859 * ELS response iocb on the driver internal slow-path response iocb worker
3860 * queue. The caller does not hold any lock. The function will remove each
3861 * response iocb from the response worker queue and calls the handle
3862 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3863 **/
3864static void
3865lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3866 struct lpfc_sli_ring *pring, uint32_t mask)
3867{
3868 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003869 struct hbq_dmabuf *dmabuf;
3870 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003871 unsigned long iflag;
James Smart0ef01a22018-09-10 10:30:45 -07003872 int count = 0;
James Smart4f774512009-05-22 14:52:35 -04003873
James Smart45ed1192009-10-02 15:17:02 -04003874 spin_lock_irqsave(&phba->hbalock, iflag);
3875 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3876 spin_unlock_irqrestore(&phba->hbalock, iflag);
3877 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003878 /* Get the response iocb from the head of work queue */
3879 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003880 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003881 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003882 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003883
3884 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3885 case CQE_CODE_COMPL_WQE:
3886 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3887 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003888 /* Translate ELS WCQE to response IOCBQ */
3889 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3890 irspiocbq);
3891 if (irspiocbq)
3892 lpfc_sli_sp_handle_rspiocb(phba, pring,
3893 irspiocbq);
James Smart0ef01a22018-09-10 10:30:45 -07003894 count++;
James Smart4d9ab992009-10-02 15:16:39 -04003895 break;
3896 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003897 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003898 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3899 cq_event);
3900 lpfc_sli4_handle_received_buffer(phba, dmabuf);
James Smart0ef01a22018-09-10 10:30:45 -07003901 count++;
James Smart4d9ab992009-10-02 15:16:39 -04003902 break;
3903 default:
3904 break;
3905 }
James Smart0ef01a22018-09-10 10:30:45 -07003906
3907 /* Limit the number of events to 64 to avoid soft lockups */
3908 if (count == 64)
3909 break;
James Smart4f774512009-05-22 14:52:35 -04003910 }
3911}
3912
3913/**
James Smart3621a712009-04-06 18:47:14 -04003914 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003915 * @phba: Pointer to HBA context object.
3916 * @pring: Pointer to driver SLI ring object.
3917 *
3918 * This function aborts all iocbs in the given ring and frees all the iocb
3919 * objects in txq. This function issues an abort iocb for all the iocb commands
3920 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3921 * the return of this function. The caller is not required to hold any locks.
3922 **/
James Smart2e0fef82007-06-17 19:56:36 -05003923void
dea31012005-04-17 16:05:31 -05003924lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3925{
James Smart2534ba72007-04-25 09:52:20 -04003926 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003927 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003928
James Smart92d7f7b2007-06-17 19:56:38 -05003929 if (pring->ringno == LPFC_ELS_RING) {
3930 lpfc_fabric_abort_hba(phba);
3931 }
3932
dea31012005-04-17 16:05:31 -05003933 /* Error everything on txq and txcmplq
3934 * First do the txq.
3935 */
James Smartdb55fba2014-04-04 13:52:02 -04003936 if (phba->sli_rev >= LPFC_SLI_REV4) {
3937 spin_lock_irq(&pring->ring_lock);
3938 list_splice_init(&pring->txq, &completions);
3939 pring->txq_cnt = 0;
3940 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003941
James Smartdb55fba2014-04-04 13:52:02 -04003942 spin_lock_irq(&phba->hbalock);
3943 /* Next issue ABTS for everything on the txcmplq */
3944 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3945 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3946 spin_unlock_irq(&phba->hbalock);
3947 } else {
3948 spin_lock_irq(&phba->hbalock);
3949 list_splice_init(&pring->txq, &completions);
3950 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003951
James Smartdb55fba2014-04-04 13:52:02 -04003952 /* Next issue ABTS for everything on the txcmplq */
3953 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3954 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3955 spin_unlock_irq(&phba->hbalock);
3956 }
James Smart2534ba72007-04-25 09:52:20 -04003957
James Smarta257bf92009-04-06 18:48:10 -04003958 /* Cancel all the IOCBs from the completions list */
3959 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3960 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003961}
3962
James Smarte59058c2008-08-24 21:49:00 -04003963/**
James Smartdb55fba2014-04-04 13:52:02 -04003964 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3965 * @phba: Pointer to HBA context object.
3966 * @pring: Pointer to driver SLI ring object.
3967 *
3968 * This function aborts all iocbs in FCP rings and frees all the iocb
3969 * objects in txq. This function issues an abort iocb for all the iocb commands
3970 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3971 * the return of this function. The caller is not required to hold any locks.
3972 **/
3973void
3974lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3975{
3976 struct lpfc_sli *psli = &phba->sli;
3977 struct lpfc_sli_ring *pring;
3978 uint32_t i;
3979
3980 /* Look on all the FCP Rings for the iotag */
3981 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08003982 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -07003983 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003984 lpfc_sli_abort_iocb_ring(phba, pring);
3985 }
3986 } else {
James Smart895427b2017-02-12 13:52:30 -08003987 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003988 lpfc_sli_abort_iocb_ring(phba, pring);
3989 }
3990}
3991
James Smart895427b2017-02-12 13:52:30 -08003992/**
James Smartc00f62e2019-08-14 16:57:11 -07003993 * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring
James Smarta8e497d2008-08-24 21:50:11 -04003994 * @phba: Pointer to HBA context object.
3995 *
James Smartc00f62e2019-08-14 16:57:11 -07003996 * This function flushes all iocbs in the IO ring and frees all the iocb
James Smarta8e497d2008-08-24 21:50:11 -04003997 * objects in txq and txcmplq. This function will not issue abort iocbs
3998 * for all the iocb commands in txcmplq, they will just be returned with
3999 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4000 * slot has been permanently disabled.
4001 **/
4002void
James Smartc00f62e2019-08-14 16:57:11 -07004003lpfc_sli_flush_io_rings(struct lpfc_hba *phba)
James Smarta8e497d2008-08-24 21:50:11 -04004004{
4005 LIST_HEAD(txq);
4006 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04004007 struct lpfc_sli *psli = &phba->sli;
4008 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04004009 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004010 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04004011
4012 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04004013 /* Indicate the I/O queues are flushed */
James Smartc00f62e2019-08-14 16:57:11 -07004014 phba->hba_flag |= HBA_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04004015 spin_unlock_irq(&phba->hbalock);
4016
James Smartdb55fba2014-04-04 13:52:02 -04004017 /* Look on all the FCP Rings for the iotag */
4018 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004019 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -07004020 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smarta8e497d2008-08-24 21:50:11 -04004021
James Smartdb55fba2014-04-04 13:52:02 -04004022 spin_lock_irq(&pring->ring_lock);
4023 /* Retrieve everything on txq */
4024 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004025 list_for_each_entry_safe(piocb, next_iocb,
4026 &pring->txcmplq, list)
4027 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004028 /* Retrieve everything on the txcmplq */
4029 list_splice_init(&pring->txcmplq, &txcmplq);
4030 pring->txq_cnt = 0;
4031 pring->txcmplq_cnt = 0;
4032 spin_unlock_irq(&pring->ring_lock);
4033
4034 /* Flush the txq */
4035 lpfc_sli_cancel_iocbs(phba, &txq,
4036 IOSTAT_LOCAL_REJECT,
4037 IOERR_SLI_DOWN);
4038 /* Flush the txcmpq */
4039 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4040 IOSTAT_LOCAL_REJECT,
4041 IOERR_SLI_DOWN);
4042 }
4043 } else {
James Smart895427b2017-02-12 13:52:30 -08004044 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004045
4046 spin_lock_irq(&phba->hbalock);
4047 /* Retrieve everything on txq */
4048 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004049 list_for_each_entry_safe(piocb, next_iocb,
4050 &pring->txcmplq, list)
4051 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004052 /* Retrieve everything on the txcmplq */
4053 list_splice_init(&pring->txcmplq, &txcmplq);
4054 pring->txq_cnt = 0;
4055 pring->txcmplq_cnt = 0;
4056 spin_unlock_irq(&phba->hbalock);
4057
4058 /* Flush the txq */
4059 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4060 IOERR_SLI_DOWN);
4061 /* Flush the txcmpq */
4062 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4063 IOERR_SLI_DOWN);
4064 }
James Smarta8e497d2008-08-24 21:50:11 -04004065}
4066
4067/**
James Smart3772a992009-05-22 14:50:54 -04004068 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004069 * @phba: Pointer to HBA context object.
4070 * @mask: Bit mask to be checked.
4071 *
4072 * This function reads the host status register and compares
4073 * with the provided bit mask to check if HBA completed
4074 * the restart. This function will wait in a loop for the
4075 * HBA to complete restart. If the HBA does not restart within
4076 * 15 iterations, the function will reset the HBA again. The
4077 * function returns 1 when HBA fail to restart otherwise returns
4078 * zero.
4079 **/
James Smart3772a992009-05-22 14:50:54 -04004080static int
4081lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004082{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004083 uint32_t status;
4084 int i = 0;
4085 int retval = 0;
dea31012005-04-17 16:05:31 -05004086
Jamie Wellnitz41415862006-02-28 19:25:27 -05004087 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004088 if (lpfc_readl(phba->HSregaddr, &status))
4089 return 1;
dea31012005-04-17 16:05:31 -05004090
Jamie Wellnitz41415862006-02-28 19:25:27 -05004091 /*
4092 * Check status register every 100ms for 5 retries, then every
4093 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4094 * every 2.5 sec for 4.
4095 * Break our of the loop if errors occurred during init.
4096 */
4097 while (((status & mask) != mask) &&
4098 !(status & HS_FFERM) &&
4099 i++ < 20) {
dea31012005-04-17 16:05:31 -05004100
Jamie Wellnitz41415862006-02-28 19:25:27 -05004101 if (i <= 5)
4102 msleep(10);
4103 else if (i <= 10)
4104 msleep(500);
4105 else
4106 msleep(2500);
dea31012005-04-17 16:05:31 -05004107
Jamie Wellnitz41415862006-02-28 19:25:27 -05004108 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004109 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004110 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004111 lpfc_sli_brdrestart(phba);
4112 }
4113 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004114 if (lpfc_readl(phba->HSregaddr, &status)) {
4115 retval = 1;
4116 break;
4117 }
dea31012005-04-17 16:05:31 -05004118 }
dea31012005-04-17 16:05:31 -05004119
Jamie Wellnitz41415862006-02-28 19:25:27 -05004120 /* Check to see if any errors occurred during init */
4121 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05004122 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4123 "2751 Adapter failed to restart, "
4124 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4125 status,
4126 readl(phba->MBslimaddr + 0xa8),
4127 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004128 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004129 retval = 1;
4130 }
dea31012005-04-17 16:05:31 -05004131
Jamie Wellnitz41415862006-02-28 19:25:27 -05004132 return retval;
dea31012005-04-17 16:05:31 -05004133}
4134
James Smartda0436e2009-05-22 14:51:39 -04004135/**
4136 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4137 * @phba: Pointer to HBA context object.
4138 * @mask: Bit mask to be checked.
4139 *
4140 * This function checks the host status register to check if HBA is
4141 * ready. This function will wait in a loop for the HBA to be ready
4142 * If the HBA is not ready , the function will will reset the HBA PCI
4143 * function again. The function returns 1 when HBA fail to be ready
4144 * otherwise returns zero.
4145 **/
4146static int
4147lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4148{
4149 uint32_t status;
4150 int retval = 0;
4151
4152 /* Read the HBA Host Status Register */
4153 status = lpfc_sli4_post_status_check(phba);
4154
4155 if (status) {
4156 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4157 lpfc_sli_brdrestart(phba);
4158 status = lpfc_sli4_post_status_check(phba);
4159 }
4160
4161 /* Check to see if any errors occurred during init */
4162 if (status) {
4163 phba->link_state = LPFC_HBA_ERROR;
4164 retval = 1;
4165 } else
4166 phba->sli4_hba.intr_enable = 0;
4167
4168 return retval;
4169}
4170
4171/**
4172 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4173 * @phba: Pointer to HBA context object.
4174 * @mask: Bit mask to be checked.
4175 *
4176 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4177 * from the API jump table function pointer from the lpfc_hba struct.
4178 **/
4179int
4180lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4181{
4182 return phba->lpfc_sli_brdready(phba, mask);
4183}
4184
James Smart92908312006-03-07 15:04:13 -05004185#define BARRIER_TEST_PATTERN (0xdeadbeef)
4186
James Smarte59058c2008-08-24 21:49:00 -04004187/**
James Smart3621a712009-04-06 18:47:14 -04004188 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004189 * @phba: Pointer to HBA context object.
4190 *
James Smart1b511972011-12-13 13:23:09 -05004191 * This function is called before resetting an HBA. This function is called
4192 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004193 **/
James Smart2e0fef82007-06-17 19:56:36 -05004194void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004195{
James Smart65a29c12006-07-06 15:50:50 -04004196 uint32_t __iomem *resp_buf;
4197 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004198 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004199 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004200 int i;
4201 uint8_t hdrtype;
4202
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004203 lockdep_assert_held(&phba->hbalock);
4204
James Smart92908312006-03-07 15:04:13 -05004205 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4206 if (hdrtype != 0x80 ||
4207 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4208 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4209 return;
4210
4211 /*
4212 * Tell the other part of the chip to suspend temporarily all
4213 * its DMA activity.
4214 */
James Smart65a29c12006-07-06 15:50:50 -04004215 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004216
4217 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004218 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4219 return;
James Smart92908312006-03-07 15:04:13 -05004220 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4221 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004222 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004223
James Smart9940b972011-03-11 16:06:12 -05004224 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4225 return;
4226 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004227 /* Clear Chip error bit */
4228 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004229 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004230 }
4231
4232 mbox = 0;
4233 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4234 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4235
4236 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004237 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004238 writel(mbox, mbox_buf);
4239
James Smart9940b972011-03-11 16:06:12 -05004240 for (i = 0; i < 50; i++) {
4241 if (lpfc_readl((resp_buf + 1), &resp_data))
4242 return;
4243 if (resp_data != ~(BARRIER_TEST_PATTERN))
4244 mdelay(1);
4245 else
4246 break;
4247 }
4248 resp_data = 0;
4249 if (lpfc_readl((resp_buf + 1), &resp_data))
4250 return;
4251 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004252 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004253 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004254 goto restore_hc;
4255 else
4256 goto clear_errat;
4257 }
4258
4259 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004260 resp_data = 0;
4261 for (i = 0; i < 500; i++) {
4262 if (lpfc_readl(resp_buf, &resp_data))
4263 return;
4264 if (resp_data != mbox)
4265 mdelay(1);
4266 else
4267 break;
4268 }
James Smart92908312006-03-07 15:04:13 -05004269
4270clear_errat:
4271
James Smart9940b972011-03-11 16:06:12 -05004272 while (++i < 500) {
4273 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4274 return;
4275 if (!(ha_copy & HA_ERATT))
4276 mdelay(1);
4277 else
4278 break;
4279 }
James Smart92908312006-03-07 15:04:13 -05004280
4281 if (readl(phba->HAregaddr) & HA_ERATT) {
4282 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004283 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004284 }
4285
4286restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004287 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004288 writel(hc_copy, phba->HCregaddr);
4289 readl(phba->HCregaddr); /* flush */
4290}
4291
James Smarte59058c2008-08-24 21:49:00 -04004292/**
James Smart3621a712009-04-06 18:47:14 -04004293 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004294 * @phba: Pointer to HBA context object.
4295 *
4296 * This function issues a kill_board mailbox command and waits for
4297 * the error attention interrupt. This function is called for stopping
4298 * the firmware processing. The caller is not required to hold any
4299 * locks. This function calls lpfc_hba_down_post function to free
4300 * any pending commands after the kill. The function will return 1 when it
4301 * fails to kill the board else will return 0.
4302 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004303int
James Smart2e0fef82007-06-17 19:56:36 -05004304lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004305{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004306 struct lpfc_sli *psli;
4307 LPFC_MBOXQ_t *pmb;
4308 uint32_t status;
4309 uint32_t ha_copy;
4310 int retval;
4311 int i = 0;
4312
4313 psli = &phba->sli;
4314
4315 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004316 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004317 "0329 Kill HBA Data: x%x x%x\n",
4318 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004319
James Smart98c9ea52007-10-27 13:37:33 -04004320 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4321 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004322 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004323
4324 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004325 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004326 if (lpfc_readl(phba->HCregaddr, &status)) {
4327 spin_unlock_irq(&phba->hbalock);
4328 mempool_free(pmb, phba->mbox_mem_pool);
4329 return 1;
4330 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004331 status &= ~HC_ERINT_ENA;
4332 writel(status, phba->HCregaddr);
4333 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004334 phba->link_flag |= LS_IGNORE_ERATT;
4335 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004336
4337 lpfc_kill_board(phba, pmb);
4338 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4339 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4340
4341 if (retval != MBX_SUCCESS) {
4342 if (retval != MBX_BUSY)
4343 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004344 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4345 "2752 KILL_BOARD command failed retval %d\n",
4346 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004347 spin_lock_irq(&phba->hbalock);
4348 phba->link_flag &= ~LS_IGNORE_ERATT;
4349 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004350 return 1;
4351 }
4352
James Smartf4b4c682009-05-22 14:53:12 -04004353 spin_lock_irq(&phba->hbalock);
4354 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4355 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004356
Jamie Wellnitz41415862006-02-28 19:25:27 -05004357 mempool_free(pmb, phba->mbox_mem_pool);
4358
4359 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4360 * attention every 100ms for 3 seconds. If we don't get ERATT after
4361 * 3 seconds we still set HBA_ERROR state because the status of the
4362 * board is now undefined.
4363 */
James Smart9940b972011-03-11 16:06:12 -05004364 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4365 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004366 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4367 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004368 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4369 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004370 }
4371
4372 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004373 if (ha_copy & HA_ERATT) {
4374 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004375 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004376 }
James Smart2e0fef82007-06-17 19:56:36 -05004377 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004378 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004379 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004380 phba->link_flag &= ~LS_IGNORE_ERATT;
4381 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004382
Jamie Wellnitz41415862006-02-28 19:25:27 -05004383 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004384 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004385
James Smart2e0fef82007-06-17 19:56:36 -05004386 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004387}
4388
James Smarte59058c2008-08-24 21:49:00 -04004389/**
James Smart3772a992009-05-22 14:50:54 -04004390 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004391 * @phba: Pointer to HBA context object.
4392 *
4393 * This function resets the HBA by writing HC_INITFF to the control
4394 * register. After the HBA resets, this function resets all the iocb ring
4395 * indices. This function disables PCI layer parity checking during
4396 * the reset.
4397 * This function returns 0 always.
4398 * The caller is not required to hold any locks.
4399 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004400int
James Smart2e0fef82007-06-17 19:56:36 -05004401lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004402{
4403 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004404 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004405 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004406 int i;
dea31012005-04-17 16:05:31 -05004407
Jamie Wellnitz41415862006-02-28 19:25:27 -05004408 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004409
Jamie Wellnitz41415862006-02-28 19:25:27 -05004410 /* Reset HBA */
4411 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004412 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004413 (phba->pport) ? phba->pport->port_state : 0,
4414 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004415
4416 /* perform board reset */
4417 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004418 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004419 if (phba->pport) {
4420 phba->pport->fc_myDID = 0;
4421 phba->pport->fc_prevDID = 0;
4422 }
dea31012005-04-17 16:05:31 -05004423
Jamie Wellnitz41415862006-02-28 19:25:27 -05004424 /* Turn off parity checking and serr during the physical reset */
James Smart32a93102019-03-12 16:30:13 -07004425 if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
4426 return -EIO;
4427
Jamie Wellnitz41415862006-02-28 19:25:27 -05004428 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4429 (cfg_value &
4430 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4431
James Smart3772a992009-05-22 14:50:54 -04004432 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4433
Jamie Wellnitz41415862006-02-28 19:25:27 -05004434 /* Now toggle INITFF bit in the Host Control Register */
4435 writel(HC_INITFF, phba->HCregaddr);
4436 mdelay(1);
4437 readl(phba->HCregaddr); /* flush */
4438 writel(0, phba->HCregaddr);
4439 readl(phba->HCregaddr); /* flush */
4440
4441 /* Restore PCI cmd register */
4442 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004443
4444 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004445 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004446 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004447 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004448 pring->sli.sli3.rspidx = 0;
4449 pring->sli.sli3.next_cmdidx = 0;
4450 pring->sli.sli3.local_getidx = 0;
4451 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004452 pring->missbufcnt = 0;
4453 }
dea31012005-04-17 16:05:31 -05004454
James Smart2e0fef82007-06-17 19:56:36 -05004455 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004456 return 0;
4457}
4458
James Smarte59058c2008-08-24 21:49:00 -04004459/**
James Smartda0436e2009-05-22 14:51:39 -04004460 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4461 * @phba: Pointer to HBA context object.
4462 *
4463 * This function resets a SLI4 HBA. This function disables PCI layer parity
4464 * checking during resets the device. The caller is not required to hold
4465 * any locks.
4466 *
James Smart8c24a4f2019-08-14 16:56:53 -07004467 * This function returns 0 on success else returns negative error code.
James Smartda0436e2009-05-22 14:51:39 -04004468 **/
4469int
4470lpfc_sli4_brdreset(struct lpfc_hba *phba)
4471{
4472 struct lpfc_sli *psli = &phba->sli;
4473 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004474 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004475
4476 /* Reset HBA */
4477 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004478 "0295 Reset HBA Data: x%x x%x x%x\n",
4479 phba->pport->port_state, psli->sli_flag,
4480 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004481
4482 /* perform board reset */
4483 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004484 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004485 phba->pport->fc_myDID = 0;
4486 phba->pport->fc_prevDID = 0;
4487
James Smartda0436e2009-05-22 14:51:39 -04004488 spin_lock_irq(&phba->hbalock);
4489 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4490 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004491 spin_unlock_irq(&phba->hbalock);
4492
James Smart02936352014-04-04 13:52:12 -04004493 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4494 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4495 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4496 return rc;
4497 }
4498
James Smartda0436e2009-05-22 14:51:39 -04004499 /* Now physically reset the device */
4500 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4501 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004502
4503 /* Turn off parity checking and serr during the physical reset */
James Smart32a93102019-03-12 16:30:13 -07004504 if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
4505 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4506 "3205 PCI read Config failed\n");
4507 return -EIO;
4508 }
4509
James Smartbe858b62010-12-15 17:57:20 -05004510 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4511 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4512
James Smart88318812012-09-29 11:29:29 -04004513 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004514 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004515
James Smartbe858b62010-12-15 17:57:20 -05004516 /* Restore PCI cmd register */
4517 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4518
James Smart27b01b82012-05-09 21:19:44 -04004519 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004520}
4521
4522/**
4523 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004524 * @phba: Pointer to HBA context object.
4525 *
4526 * This function is called in the SLI initialization code path to
4527 * restart the HBA. The caller is not required to hold any lock.
4528 * This function writes MBX_RESTART mailbox command to the SLIM and
4529 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4530 * function to free any pending commands. The function enables
4531 * POST only during the first initialization. The function returns zero.
4532 * The function does not guarantee completion of MBX_RESTART mailbox
4533 * command before the return of this function.
4534 **/
James Smartda0436e2009-05-22 14:51:39 -04004535static int
4536lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004537{
4538 MAILBOX_t *mb;
4539 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004540 volatile uint32_t word0;
4541 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004542 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004543
James Smart2e0fef82007-06-17 19:56:36 -05004544 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004545
James Smart0d878412009-10-02 15:16:56 -04004546 /* Take PCIe device Advanced Error Reporting (AER) state */
4547 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4548
Jamie Wellnitz41415862006-02-28 19:25:27 -05004549 psli = &phba->sli;
4550
4551 /* Restart HBA */
4552 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004553 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004554 (phba->pport) ? phba->pport->port_state : 0,
4555 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004556
4557 word0 = 0;
4558 mb = (MAILBOX_t *) &word0;
4559 mb->mbxCommand = MBX_RESTART;
4560 mb->mbxHc = 1;
4561
James Smart92908312006-03-07 15:04:13 -05004562 lpfc_reset_barrier(phba);
4563
Jamie Wellnitz41415862006-02-28 19:25:27 -05004564 to_slim = phba->MBslimaddr;
4565 writel(*(uint32_t *) mb, to_slim);
4566 readl(to_slim); /* flush */
4567
4568 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004569 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004570 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004571 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004572 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004573 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004574 writel(*(uint32_t *) mb, to_slim);
4575 readl(to_slim); /* flush */
4576
4577 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004578 if (phba->pport)
4579 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004580 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004581 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004582 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004583
James Smart64ba8812006-08-02 15:24:34 -04004584 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004585 psli->stats_start = ktime_get_seconds();
James Smart64ba8812006-08-02 15:24:34 -04004586
James Smarteaf15d52008-12-04 22:39:29 -05004587 /* Give the INITFF and Post time to settle. */
4588 mdelay(100);
dea31012005-04-17 16:05:31 -05004589
James Smart0d878412009-10-02 15:16:56 -04004590 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4591 if (hba_aer_enabled)
4592 pci_disable_pcie_error_reporting(phba->pcidev);
4593
Jamie Wellnitz41415862006-02-28 19:25:27 -05004594 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004595
4596 return 0;
4597}
4598
James Smarte59058c2008-08-24 21:49:00 -04004599/**
James Smartda0436e2009-05-22 14:51:39 -04004600 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4601 * @phba: Pointer to HBA context object.
4602 *
4603 * This function is called in the SLI initialization code path to restart
4604 * a SLI4 HBA. The caller is not required to hold any lock.
4605 * At the end of the function, it calls lpfc_hba_down_post function to
4606 * free any pending commands.
4607 **/
4608static int
4609lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4610{
4611 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004612 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004613 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004614
4615 /* Restart HBA */
4616 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4617 "0296 Restart HBA Data: x%x x%x\n",
4618 phba->pport->port_state, psli->sli_flag);
4619
James Smart75baf692010-06-08 18:31:21 -04004620 /* Take PCIe device Advanced Error Reporting (AER) state */
4621 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4622
James Smart27b01b82012-05-09 21:19:44 -04004623 rc = lpfc_sli4_brdreset(phba);
James Smart4fb86a62019-09-03 14:54:41 -07004624 if (rc) {
4625 phba->link_state = LPFC_HBA_ERROR;
4626 goto hba_down_queue;
4627 }
James Smartda0436e2009-05-22 14:51:39 -04004628
4629 spin_lock_irq(&phba->hbalock);
4630 phba->pport->stopped = 0;
4631 phba->link_state = LPFC_INIT_START;
4632 phba->hba_flag = 0;
4633 spin_unlock_irq(&phba->hbalock);
4634
4635 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004636 psli->stats_start = ktime_get_seconds();
James Smartda0436e2009-05-22 14:51:39 -04004637
James Smart75baf692010-06-08 18:31:21 -04004638 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4639 if (hba_aer_enabled)
4640 pci_disable_pcie_error_reporting(phba->pcidev);
4641
James Smart4fb86a62019-09-03 14:54:41 -07004642hba_down_queue:
James Smartda0436e2009-05-22 14:51:39 -04004643 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004644 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004645
James Smart27b01b82012-05-09 21:19:44 -04004646 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004647}
4648
4649/**
4650 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4651 * @phba: Pointer to HBA context object.
4652 *
4653 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4654 * API jump table function pointer from the lpfc_hba struct.
4655**/
4656int
4657lpfc_sli_brdrestart(struct lpfc_hba *phba)
4658{
4659 return phba->lpfc_sli_brdrestart(phba);
4660}
4661
4662/**
James Smart3621a712009-04-06 18:47:14 -04004663 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004664 * @phba: Pointer to HBA context object.
4665 *
4666 * This function is called after a HBA restart to wait for successful
4667 * restart of the HBA. Successful restart of the HBA is indicated by
4668 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4669 * iteration, the function will restart the HBA again. The function returns
4670 * zero if HBA successfully restarted else returns negative error code.
4671 **/
James Smart4492b732017-04-27 15:08:26 -07004672int
dea31012005-04-17 16:05:31 -05004673lpfc_sli_chipset_init(struct lpfc_hba *phba)
4674{
4675 uint32_t status, i = 0;
4676
4677 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004678 if (lpfc_readl(phba->HSregaddr, &status))
4679 return -EIO;
dea31012005-04-17 16:05:31 -05004680
4681 /* Check status register to see what current state is */
4682 i = 0;
4683 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4684
James Smartdcf2a4e2010-09-29 11:18:53 -04004685 /* Check every 10ms for 10 retries, then every 100ms for 90
4686 * retries, then every 1 sec for 50 retires for a total of
4687 * ~60 seconds before reset the board again and check every
4688 * 1 sec for 50 retries. The up to 60 seconds before the
4689 * board ready is required by the Falcon FIPS zeroization
4690 * complete, and any reset the board in between shall cause
4691 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004692 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004693 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004694 /* Adapter failed to init, timeout, status reg
4695 <status> */
James Smarted957682007-06-17 19:56:37 -05004696 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004697 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004698 "timeout, status reg x%x, "
4699 "FW Data: A8 x%x AC x%x\n", status,
4700 readl(phba->MBslimaddr + 0xa8),
4701 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004702 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004703 return -ETIMEDOUT;
4704 }
4705
4706 /* Check to see if any errors occurred during init */
4707 if (status & HS_FFERM) {
4708 /* ERROR: During chipset initialization */
4709 /* Adapter failed to init, chipset, status reg
4710 <status> */
James Smarted957682007-06-17 19:56:37 -05004711 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004712 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004713 "chipset, status reg x%x, "
4714 "FW Data: A8 x%x AC x%x\n", status,
4715 readl(phba->MBslimaddr + 0xa8),
4716 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004717 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004718 return -EIO;
4719 }
4720
James Smartdcf2a4e2010-09-29 11:18:53 -04004721 if (i <= 10)
dea31012005-04-17 16:05:31 -05004722 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004723 else if (i <= 100)
4724 msleep(100);
4725 else
4726 msleep(1000);
dea31012005-04-17 16:05:31 -05004727
James Smartdcf2a4e2010-09-29 11:18:53 -04004728 if (i == 150) {
4729 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004730 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004731 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004732 }
4733 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004734 if (lpfc_readl(phba->HSregaddr, &status))
4735 return -EIO;
dea31012005-04-17 16:05:31 -05004736 }
4737
4738 /* Check to see if any errors occurred during init */
4739 if (status & HS_FFERM) {
4740 /* ERROR: During chipset initialization */
4741 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004742 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004743 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004744 "status reg x%x, "
4745 "FW Data: A8 x%x AC x%x\n", status,
4746 readl(phba->MBslimaddr + 0xa8),
4747 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004748 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004749 return -EIO;
4750 }
4751
4752 /* Clear all interrupt enable conditions */
4753 writel(0, phba->HCregaddr);
4754 readl(phba->HCregaddr); /* flush */
4755
4756 /* setup host attn register */
4757 writel(0xffffffff, phba->HAregaddr);
4758 readl(phba->HAregaddr); /* flush */
4759 return 0;
4760}
4761
James Smarte59058c2008-08-24 21:49:00 -04004762/**
James Smart3621a712009-04-06 18:47:14 -04004763 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004764 *
4765 * This function calculates and returns the number of HBQs required to be
4766 * configured.
4767 **/
James Smart78b2d852007-08-02 11:10:21 -04004768int
James Smarted957682007-06-17 19:56:37 -05004769lpfc_sli_hbq_count(void)
4770{
James Smart92d7f7b2007-06-17 19:56:38 -05004771 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004772}
4773
James Smarte59058c2008-08-24 21:49:00 -04004774/**
James Smart3621a712009-04-06 18:47:14 -04004775 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004776 *
4777 * This function adds the number of hbq entries in every HBQ to get
4778 * the total number of hbq entries required for the HBA and returns
4779 * the total count.
4780 **/
James Smarted957682007-06-17 19:56:37 -05004781static int
4782lpfc_sli_hbq_entry_count(void)
4783{
4784 int hbq_count = lpfc_sli_hbq_count();
4785 int count = 0;
4786 int i;
4787
4788 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004789 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004790 return count;
4791}
4792
James Smarte59058c2008-08-24 21:49:00 -04004793/**
James Smart3621a712009-04-06 18:47:14 -04004794 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004795 *
4796 * This function calculates amount of memory required for all hbq entries
4797 * to be configured and returns the total memory required.
4798 **/
dea31012005-04-17 16:05:31 -05004799int
James Smarted957682007-06-17 19:56:37 -05004800lpfc_sli_hbq_size(void)
4801{
4802 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4803}
4804
James Smarte59058c2008-08-24 21:49:00 -04004805/**
James Smart3621a712009-04-06 18:47:14 -04004806 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004807 * @phba: Pointer to HBA context object.
4808 *
4809 * This function is called during the SLI initialization to configure
4810 * all the HBQs and post buffers to the HBQ. The caller is not
4811 * required to hold any locks. This function will return zero if successful
4812 * else it will return negative error code.
4813 **/
James Smarted957682007-06-17 19:56:37 -05004814static int
4815lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4816{
4817 int hbq_count = lpfc_sli_hbq_count();
4818 LPFC_MBOXQ_t *pmb;
4819 MAILBOX_t *pmbox;
4820 uint32_t hbqno;
4821 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004822
James Smart92d7f7b2007-06-17 19:56:38 -05004823 /* Get a Mailbox buffer to setup mailbox
4824 * commands for HBA initialization
4825 */
James Smarted957682007-06-17 19:56:37 -05004826 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4827
4828 if (!pmb)
4829 return -ENOMEM;
4830
James Smart04c68492009-05-22 14:52:52 -04004831 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004832
4833 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4834 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004835 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004836
4837 hbq_entry_index = 0;
4838 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4839 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4840 phba->hbqs[hbqno].hbqPutIdx = 0;
4841 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4842 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004843 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004844 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4845 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004846 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4847
4848 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4849 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4850 mbxStatus <status>, ring <num> */
4851
4852 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004853 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004854 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004855 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004856 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004857 pmbox->mbxStatus, hbqno);
4858
4859 phba->link_state = LPFC_HBA_ERROR;
4860 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004861 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004862 }
4863 }
4864 phba->hbq_count = hbq_count;
4865
James Smarted957682007-06-17 19:56:37 -05004866 mempool_free(pmb, phba->mbox_mem_pool);
4867
James Smart92d7f7b2007-06-17 19:56:38 -05004868 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004869 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4870 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004871 return 0;
4872}
4873
James Smarte59058c2008-08-24 21:49:00 -04004874/**
James Smart4f774512009-05-22 14:52:35 -04004875 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4876 * @phba: Pointer to HBA context object.
4877 *
4878 * This function is called during the SLI initialization to configure
4879 * all the HBQs and post buffers to the HBQ. The caller is not
4880 * required to hold any locks. This function will return zero if successful
4881 * else it will return negative error code.
4882 **/
4883static int
4884lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4885{
4886 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004887 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4888 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004889 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004890 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004891 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004892 return 0;
4893}
4894
4895/**
James Smart3621a712009-04-06 18:47:14 -04004896 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004897 * @phba: Pointer to HBA context object.
4898 * @sli_mode: sli mode - 2/3
4899 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004900 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004901 * to issue config_port mailbox command. This function restarts the
4902 * HBA firmware and issues a config_port mailbox command to configure
4903 * the SLI interface in the sli mode specified by sli_mode
4904 * variable. The caller is not required to hold any locks.
4905 * The function returns 0 if successful, else returns negative error
4906 * code.
4907 **/
James Smart93996272008-08-24 21:50:30 -04004908int
4909lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004910{
4911 LPFC_MBOXQ_t *pmb;
4912 uint32_t resetcount = 0, rc = 0, done = 0;
4913
4914 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4915 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004916 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004917 return -ENOMEM;
4918 }
4919
James Smarted957682007-06-17 19:56:37 -05004920 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004921 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004922 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004923 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004924 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004925 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004926 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004927 rc = lpfc_sli_chipset_init(phba);
4928 if (rc)
4929 break;
4930
James Smart2e0fef82007-06-17 19:56:36 -05004931 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004932 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004933 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004934 resetcount++;
4935
James Smarted957682007-06-17 19:56:37 -05004936 /* Call pre CONFIG_PORT mailbox command initialization. A
4937 * value of 0 means the call was successful. Any other
4938 * nonzero value is a failure, but if ERESTART is returned,
4939 * the driver may reset the HBA and try again.
4940 */
dea31012005-04-17 16:05:31 -05004941 rc = lpfc_config_port_prep(phba);
4942 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004943 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004944 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004945 } else if (rc)
dea31012005-04-17 16:05:31 -05004946 break;
James Smart6d368e52011-05-24 11:44:12 -04004947
James Smart2e0fef82007-06-17 19:56:36 -05004948 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004949 lpfc_config_port(phba, pmb);
4950 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004951 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4952 LPFC_SLI3_HBQ_ENABLED |
4953 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004954 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004955 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004956 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004957 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004958 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004959 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004960 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004961 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004962 spin_unlock_irq(&phba->hbalock);
4963 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004964 } else {
4965 /* Allow asynchronous mailbox command to go through */
4966 spin_lock_irq(&phba->hbalock);
4967 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4968 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004969 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004970
4971 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4972 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4973 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4974 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004975 }
dea31012005-04-17 16:05:31 -05004976 }
James Smarted957682007-06-17 19:56:37 -05004977 if (!done) {
4978 rc = -EINVAL;
4979 goto do_prep_failed;
4980 }
James Smart04c68492009-05-22 14:52:52 -04004981 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4982 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004983 rc = -ENXIO;
4984 goto do_prep_failed;
4985 }
James Smart04c68492009-05-22 14:52:52 -04004986 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004987 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004988 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4989 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4990 phba->max_vpi : phba->max_vports;
4991
James Smart34b02dc2008-08-24 21:49:55 -04004992 } else
4993 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004994 phba->fips_level = 0;
4995 phba->fips_spec_rev = 0;
4996 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004997 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004998 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4999 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
5000 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5001 "2850 Security Crypto Active. FIPS x%d "
5002 "(Spec Rev: x%d)",
5003 phba->fips_level, phba->fips_spec_rev);
5004 }
5005 if (pmb->u.mb.un.varCfgPort.sec_err) {
5006 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5007 "2856 Config Port Security Crypto "
5008 "Error: x%x ",
5009 pmb->u.mb.un.varCfgPort.sec_err);
5010 }
James Smart04c68492009-05-22 14:52:52 -04005011 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04005012 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005013 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04005014 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04005015
5016 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5017 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05005018
James Smartf44ac122018-03-05 12:04:08 -08005019 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5020 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5021 phba->cfg_enable_bg = 0;
5022 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smarte2a0a9d2008-12-04 22:40:02 -05005023 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5024 "0443 Adapter did not grant "
5025 "BlockGuard\n");
James Smartf44ac122018-03-05 12:04:08 -08005026 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005027 }
James Smart34b02dc2008-08-24 21:49:55 -04005028 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05005029 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04005030 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04005031 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05005032 }
James Smart92d7f7b2007-06-17 19:56:38 -05005033do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05005034 mempool_free(pmb, phba->mbox_mem_pool);
5035 return rc;
5036}
5037
James Smarte59058c2008-08-24 21:49:00 -04005038
5039/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08005040 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04005041 * @phba: Pointer to HBA context object.
5042 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005043 * This function is the main SLI initialization function. This function
5044 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04005045 * error attention handler code. Caller is not required to hold any
5046 * locks. This function issues config_port mailbox command to configure
5047 * the SLI, setup iocb rings and HBQ rings. In the end the function
5048 * calls the config_port_post function to issue init_link mailbox
5049 * command and to start the discovery. The function will return zero
5050 * if successful, else it will return negative error code.
5051 **/
James Smarted957682007-06-17 19:56:37 -05005052int
5053lpfc_sli_hba_setup(struct lpfc_hba *phba)
5054{
5055 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04005056 int mode = 3, i;
5057 int longs;
James Smarted957682007-06-17 19:56:37 -05005058
James Smart12247e82016-07-06 12:36:09 -07005059 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05005060 case 2:
James Smart78b2d852007-08-02 11:10:21 -04005061 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05005062 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005063 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05005064 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07005065 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05005066 break;
5067 }
James Smarted957682007-06-17 19:56:37 -05005068 mode = 2;
5069 break;
5070 case 0:
5071 case 3:
5072 break;
5073 default:
James Smart92d7f7b2007-06-17 19:56:38 -05005074 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005075 "1819 Unrecognized sli_mode parameter: %d.\n",
5076 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05005077
5078 break;
5079 }
James Smartb5c53952016-03-31 14:12:30 -07005080 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005081
James Smart93996272008-08-24 21:50:30 -04005082 rc = lpfc_sli_config_port(phba, mode);
5083
James Smart12247e82016-07-06 12:36:09 -07005084 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05005085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005086 "1820 Unable to select SLI-3. "
5087 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05005088 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04005089 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07005090 else if (rc && mode == 2)
5091 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05005092 if (rc)
dea31012005-04-17 16:05:31 -05005093 goto lpfc_sli_hba_setup_error;
5094
James Smart0d878412009-10-02 15:16:56 -04005095 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5096 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5097 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5098 if (!rc) {
5099 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5100 "2709 This device supports "
5101 "Advanced Error Reporting (AER)\n");
5102 spin_lock_irq(&phba->hbalock);
5103 phba->hba_flag |= HBA_AER_ENABLED;
5104 spin_unlock_irq(&phba->hbalock);
5105 } else {
5106 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5107 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005108 "Advanced Error Reporting (AER): %d\n",
5109 rc);
James Smart0d878412009-10-02 15:16:56 -04005110 phba->cfg_aer_support = 0;
5111 }
5112 }
5113
James Smarted957682007-06-17 19:56:37 -05005114 if (phba->sli_rev == 3) {
5115 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5116 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005117 } else {
5118 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5119 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005120 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005121 }
5122
5123 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005124 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5125 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005126 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005127
5128 if (rc)
5129 goto lpfc_sli_hba_setup_error;
5130
James Smart6d368e52011-05-24 11:44:12 -04005131 /* Initialize VPIs. */
5132 if (phba->sli_rev == LPFC_SLI_REV3) {
5133 /*
5134 * The VPI bitmask and physical ID array are allocated
5135 * and initialized once only - at driver load. A port
5136 * reset doesn't need to reinitialize this memory.
5137 */
5138 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5139 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07005140 phba->vpi_bmask = kcalloc(longs,
5141 sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005142 GFP_KERNEL);
5143 if (!phba->vpi_bmask) {
5144 rc = -ENOMEM;
5145 goto lpfc_sli_hba_setup_error;
5146 }
5147
Kees Cook6396bb22018-06-12 14:03:40 -07005148 phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5149 sizeof(uint16_t),
5150 GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -04005151 if (!phba->vpi_ids) {
5152 kfree(phba->vpi_bmask);
5153 rc = -ENOMEM;
5154 goto lpfc_sli_hba_setup_error;
5155 }
5156 for (i = 0; i < phba->max_vpi; i++)
5157 phba->vpi_ids[i] = i;
5158 }
5159 }
5160
James Smart93996272008-08-24 21:50:30 -04005161 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005162 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5163 rc = lpfc_sli_hbq_setup(phba);
5164 if (rc)
5165 goto lpfc_sli_hba_setup_error;
5166 }
James Smart04c68492009-05-22 14:52:52 -04005167 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005168 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005169 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005170
5171 rc = lpfc_config_port_post(phba);
5172 if (rc)
5173 goto lpfc_sli_hba_setup_error;
5174
James Smarted957682007-06-17 19:56:37 -05005175 return rc;
5176
James Smart92d7f7b2007-06-17 19:56:38 -05005177lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005178 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05005179 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005180 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005181 return rc;
5182}
5183
James Smartda0436e2009-05-22 14:51:39 -04005184/**
5185 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5186 * @phba: Pointer to HBA context object.
5187 * @mboxq: mailbox pointer.
5188 * This function issue a dump mailbox command to read config region
5189 * 23 and parse the records in the region and populate driver
5190 * data structure.
5191 **/
5192static int
James Smartff78d8f2011-12-13 13:21:35 -05005193lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005194{
James Smartff78d8f2011-12-13 13:21:35 -05005195 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005196 struct lpfc_dmabuf *mp;
5197 struct lpfc_mqe *mqe;
5198 uint32_t data_length;
5199 int rc;
5200
5201 /* Program the default value of vlan_id and fc_map */
5202 phba->valid_vlan = 0;
5203 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5204 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5205 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5206
James Smartff78d8f2011-12-13 13:21:35 -05005207 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5208 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005209 return -ENOMEM;
5210
James Smartff78d8f2011-12-13 13:21:35 -05005211 mqe = &mboxq->u.mqe;
5212 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5213 rc = -ENOMEM;
5214 goto out_free_mboxq;
5215 }
5216
James Smart3e1f0712018-11-29 16:09:29 -08005217 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04005218 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5219
5220 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5221 "(%d):2571 Mailbox cmd x%x Status x%x "
5222 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5223 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5224 "CQ: x%x x%x x%x x%x\n",
5225 mboxq->vport ? mboxq->vport->vpi : 0,
5226 bf_get(lpfc_mqe_command, mqe),
5227 bf_get(lpfc_mqe_status, mqe),
5228 mqe->un.mb_words[0], mqe->un.mb_words[1],
5229 mqe->un.mb_words[2], mqe->un.mb_words[3],
5230 mqe->un.mb_words[4], mqe->un.mb_words[5],
5231 mqe->un.mb_words[6], mqe->un.mb_words[7],
5232 mqe->un.mb_words[8], mqe->un.mb_words[9],
5233 mqe->un.mb_words[10], mqe->un.mb_words[11],
5234 mqe->un.mb_words[12], mqe->un.mb_words[13],
5235 mqe->un.mb_words[14], mqe->un.mb_words[15],
5236 mqe->un.mb_words[16], mqe->un.mb_words[50],
5237 mboxq->mcqe.word0,
5238 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5239 mboxq->mcqe.trailer);
5240
5241 if (rc) {
5242 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5243 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005244 rc = -EIO;
5245 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005246 }
5247 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005248 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005249 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5250 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005251 rc = -EIO;
5252 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005253 }
James Smartda0436e2009-05-22 14:51:39 -04005254
5255 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5256 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5257 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005258 rc = 0;
5259
5260out_free_mboxq:
5261 mempool_free(mboxq, phba->mbox_mem_pool);
5262 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005263}
5264
5265/**
5266 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5267 * @phba: pointer to lpfc hba data structure.
5268 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5269 * @vpd: pointer to the memory to hold resulting port vpd data.
5270 * @vpd_size: On input, the number of bytes allocated to @vpd.
5271 * On output, the number of data bytes in @vpd.
5272 *
5273 * This routine executes a READ_REV SLI4 mailbox command. In
5274 * addition, this routine gets the port vpd data.
5275 *
5276 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005277 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005278 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005279 **/
5280static int
5281lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5282 uint8_t *vpd, uint32_t *vpd_size)
5283{
5284 int rc = 0;
5285 uint32_t dma_size;
5286 struct lpfc_dmabuf *dmabuf;
5287 struct lpfc_mqe *mqe;
5288
5289 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5290 if (!dmabuf)
5291 return -ENOMEM;
5292
5293 /*
5294 * Get a DMA buffer for the vpd data resulting from the READ_REV
5295 * mailbox command.
5296 */
5297 dma_size = *vpd_size;
Luis Chamberlain750afb02019-01-04 09:23:09 +01005298 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
5299 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005300 if (!dmabuf->virt) {
5301 kfree(dmabuf);
5302 return -ENOMEM;
5303 }
James Smartda0436e2009-05-22 14:51:39 -04005304
5305 /*
5306 * The SLI4 implementation of READ_REV conflicts at word1,
5307 * bits 31:16 and SLI4 adds vpd functionality not present
5308 * in SLI3. This code corrects the conflicts.
5309 */
5310 lpfc_read_rev(phba, mboxq);
5311 mqe = &mboxq->u.mqe;
5312 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5313 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5314 mqe->un.read_rev.word1 &= 0x0000FFFF;
5315 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5316 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5317
5318 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5319 if (rc) {
5320 dma_free_coherent(&phba->pcidev->dev, dma_size,
5321 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005322 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005323 return -EIO;
5324 }
5325
James Smartda0436e2009-05-22 14:51:39 -04005326 /*
5327 * The available vpd length cannot be bigger than the
5328 * DMA buffer passed to the port. Catch the less than
5329 * case and update the caller's size.
5330 */
5331 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5332 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5333
James Smartd7c47992010-06-08 18:31:54 -04005334 memcpy(vpd, dmabuf->virt, *vpd_size);
5335
James Smartda0436e2009-05-22 14:51:39 -04005336 dma_free_coherent(&phba->pcidev->dev, dma_size,
5337 dmabuf->virt, dmabuf->phys);
5338 kfree(dmabuf);
5339 return 0;
5340}
5341
5342/**
James Smartb3b4f3e2019-03-12 16:30:23 -07005343 * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
James Smartcd1c8302011-10-10 21:33:25 -04005344 * @phba: pointer to lpfc hba data structure.
5345 *
5346 * This routine retrieves SLI4 device physical port name this PCI function
5347 * is attached to.
5348 *
5349 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005350 * 0 - successful
James Smartb3b4f3e2019-03-12 16:30:23 -07005351 * otherwise - failed to retrieve controller attributes
James Smartcd1c8302011-10-10 21:33:25 -04005352 **/
5353static int
James Smartb3b4f3e2019-03-12 16:30:23 -07005354lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
James Smartcd1c8302011-10-10 21:33:25 -04005355{
5356 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005357 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5358 struct lpfc_controller_attribute *cntl_attr;
James Smartcd1c8302011-10-10 21:33:25 -04005359 void *virtaddr = NULL;
5360 uint32_t alloclen, reqlen;
5361 uint32_t shdr_status, shdr_add_status;
5362 union lpfc_sli4_cfg_shdr *shdr;
James Smartcd1c8302011-10-10 21:33:25 -04005363 int rc;
5364
James Smartcd1c8302011-10-10 21:33:25 -04005365 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5366 if (!mboxq)
5367 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005368
James Smartb3b4f3e2019-03-12 16:30:23 -07005369 /* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
James Smartcd1c8302011-10-10 21:33:25 -04005370 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5371 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5372 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5373 LPFC_SLI4_MBX_NEMBED);
James Smartb3b4f3e2019-03-12 16:30:23 -07005374
James Smartcd1c8302011-10-10 21:33:25 -04005375 if (alloclen < reqlen) {
5376 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5377 "3084 Allocated DMA memory size (%d) is "
5378 "less than the requested DMA memory size "
5379 "(%d)\n", alloclen, reqlen);
5380 rc = -ENOMEM;
5381 goto out_free_mboxq;
5382 }
5383 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5384 virtaddr = mboxq->sge_array->addr[0];
5385 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5386 shdr = &mbx_cntl_attr->cfg_shdr;
5387 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5388 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5389 if (shdr_status || shdr_add_status || rc) {
5390 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5391 "3085 Mailbox x%x (x%x/x%x) failed, "
5392 "rc:x%x, status:x%x, add_status:x%x\n",
5393 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5394 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5395 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5396 rc, shdr_status, shdr_add_status);
5397 rc = -ENXIO;
5398 goto out_free_mboxq;
5399 }
James Smartb3b4f3e2019-03-12 16:30:23 -07005400
James Smartcd1c8302011-10-10 21:33:25 -04005401 cntl_attr = &mbx_cntl_attr->cntl_attr;
5402 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5403 phba->sli4_hba.lnk_info.lnk_tp =
5404 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5405 phba->sli4_hba.lnk_info.lnk_no =
5406 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
James Smartb3b4f3e2019-03-12 16:30:23 -07005407
5408 memset(phba->BIOSVersion, 0, sizeof(phba->BIOSVersion));
5409 strlcat(phba->BIOSVersion, (char *)cntl_attr->bios_ver_str,
5410 sizeof(phba->BIOSVersion));
5411
James Smartcd1c8302011-10-10 21:33:25 -04005412 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartb3b4f3e2019-03-12 16:30:23 -07005413 "3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s\n",
James Smartcd1c8302011-10-10 21:33:25 -04005414 phba->sli4_hba.lnk_info.lnk_tp,
James Smartb3b4f3e2019-03-12 16:30:23 -07005415 phba->sli4_hba.lnk_info.lnk_no,
5416 phba->BIOSVersion);
5417out_free_mboxq:
5418 if (rc != MBX_TIMEOUT) {
5419 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5420 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5421 else
5422 mempool_free(mboxq, phba->mbox_mem_pool);
5423 }
5424 return rc;
5425}
5426
5427/**
5428 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5429 * @phba: pointer to lpfc hba data structure.
5430 *
5431 * This routine retrieves SLI4 device physical port name this PCI function
5432 * is attached to.
5433 *
5434 * Return codes
5435 * 0 - successful
5436 * otherwise - failed to retrieve physical port name
5437 **/
5438static int
5439lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5440{
5441 LPFC_MBOXQ_t *mboxq;
5442 struct lpfc_mbx_get_port_name *get_port_name;
5443 uint32_t shdr_status, shdr_add_status;
5444 union lpfc_sli4_cfg_shdr *shdr;
5445 char cport_name = 0;
5446 int rc;
5447
5448 /* We assume nothing at this point */
5449 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5450 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5451
5452 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5453 if (!mboxq)
5454 return -ENOMEM;
5455 /* obtain link type and link number via READ_CONFIG */
5456 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5457 lpfc_sli4_read_config(phba);
5458 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5459 goto retrieve_ppname;
5460
5461 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5462 rc = lpfc_sli4_get_ctl_attr(phba);
5463 if (rc)
5464 goto out_free_mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005465
5466retrieve_ppname:
5467 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5468 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5469 sizeof(struct lpfc_mbx_get_port_name) -
5470 sizeof(struct lpfc_sli4_cfg_mhdr),
5471 LPFC_SLI4_MBX_EMBED);
5472 get_port_name = &mboxq->u.mqe.un.get_port_name;
5473 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5474 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5475 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5476 phba->sli4_hba.lnk_info.lnk_tp);
5477 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5478 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5479 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5480 if (shdr_status || shdr_add_status || rc) {
5481 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5482 "3087 Mailbox x%x (x%x/x%x) failed: "
5483 "rc:x%x, status:x%x, add_status:x%x\n",
5484 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5485 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5486 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5487 rc, shdr_status, shdr_add_status);
5488 rc = -ENXIO;
5489 goto out_free_mboxq;
5490 }
5491 switch (phba->sli4_hba.lnk_info.lnk_no) {
5492 case LPFC_LINK_NUMBER_0:
5493 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5494 &get_port_name->u.response);
5495 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5496 break;
5497 case LPFC_LINK_NUMBER_1:
5498 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5499 &get_port_name->u.response);
5500 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5501 break;
5502 case LPFC_LINK_NUMBER_2:
5503 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5504 &get_port_name->u.response);
5505 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5506 break;
5507 case LPFC_LINK_NUMBER_3:
5508 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5509 &get_port_name->u.response);
5510 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5511 break;
5512 default:
5513 break;
5514 }
5515
5516 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5517 phba->Port[0] = cport_name;
5518 phba->Port[1] = '\0';
5519 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5520 "3091 SLI get port name: %s\n", phba->Port);
5521 }
5522
5523out_free_mboxq:
5524 if (rc != MBX_TIMEOUT) {
5525 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5526 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5527 else
5528 mempool_free(mboxq, phba->mbox_mem_pool);
5529 }
5530 return rc;
5531}
5532
5533/**
James Smartda0436e2009-05-22 14:51:39 -04005534 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5535 * @phba: pointer to lpfc hba data structure.
5536 *
5537 * This routine is called to explicitly arm the SLI4 device's completion and
5538 * event queues
5539 **/
5540static void
5541lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5542{
James Smart895427b2017-02-12 13:52:30 -08005543 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08005544 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartcdb42be2019-01-28 11:14:21 -08005545 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07005546 struct lpfc_queue *eq;
James Smartda0436e2009-05-22 14:51:39 -04005547
James Smart32517fc2019-01-28 11:14:33 -08005548 sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
5549 sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
James Smartb71413d2018-02-22 08:18:40 -08005550 if (sli4_hba->nvmels_cq)
James Smart32517fc2019-01-28 11:14:33 -08005551 sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
5552 LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08005553
James Smartcdb42be2019-01-28 11:14:21 -08005554 if (sli4_hba->hdwq) {
James Smart657add42019-05-21 17:49:06 -07005555 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -08005556 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07005557 qp = &sli4_hba->hdwq[qidx];
5558 /* ARM the corresponding CQ */
James Smart01f2ef62019-08-28 16:19:11 -07005559 sli4_hba->sli4_write_cq_db(phba, qp->io_cq, 0,
James Smartc00f62e2019-08-14 16:57:11 -07005560 LPFC_QUEUE_REARM);
James Smartcdb42be2019-01-28 11:14:21 -08005561 }
James Smart895427b2017-02-12 13:52:30 -08005562
James Smart657add42019-05-21 17:49:06 -07005563 /* Loop thru all IRQ vectors */
5564 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
5565 eq = sli4_hba->hba_eq_hdl[qidx].eq;
5566 /* ARM the corresponding EQ */
5567 sli4_hba->sli4_write_eq_db(phba, eq,
5568 0, LPFC_QUEUE_REARM);
5569 }
James Smartcdb42be2019-01-28 11:14:21 -08005570 }
James Smart1ba981f2014-02-20 09:56:45 -05005571
James Smart2d7dbc42017-02-12 13:52:35 -08005572 if (phba->nvmet_support) {
5573 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smart32517fc2019-01-28 11:14:33 -08005574 sli4_hba->sli4_write_cq_db(phba,
5575 sli4_hba->nvmet_cqset[qidx], 0,
James Smart2d7dbc42017-02-12 13:52:35 -08005576 LPFC_QUEUE_REARM);
5577 }
James Smart2e90f4b2011-12-13 13:22:37 -05005578 }
James Smartda0436e2009-05-22 14:51:39 -04005579}
5580
5581/**
James Smart6d368e52011-05-24 11:44:12 -04005582 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5583 * @phba: Pointer to HBA context object.
5584 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005585 * @extnt_count: buffer to hold port available extent count.
5586 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005587 *
James Smartb76f2dc2011-07-22 18:37:42 -04005588 * This function calls the port and retrievs the number of available
5589 * extents and their size for a particular extent type.
5590 *
5591 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005592 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005593int
James Smart6d368e52011-05-24 11:44:12 -04005594lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5595 uint16_t *extnt_count, uint16_t *extnt_size)
5596{
5597 int rc = 0;
5598 uint32_t length;
5599 uint32_t mbox_tmo;
5600 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5601 LPFC_MBOXQ_t *mbox;
5602
5603 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5604 if (!mbox)
5605 return -ENOMEM;
5606
5607 /* Find out how many extents are available for this resource type */
5608 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5609 sizeof(struct lpfc_sli4_cfg_mhdr));
5610 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5611 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5612 length, LPFC_SLI4_MBX_EMBED);
5613
5614 /* Send an extents count of 0 - the GET doesn't use it. */
5615 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5616 LPFC_SLI4_MBX_EMBED);
5617 if (unlikely(rc)) {
5618 rc = -EIO;
5619 goto err_exit;
5620 }
5621
5622 if (!phba->sli4_hba.intr_enable)
5623 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5624 else {
James Smarta183a152011-10-10 21:32:43 -04005625 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005626 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5627 }
5628 if (unlikely(rc)) {
5629 rc = -EIO;
5630 goto err_exit;
5631 }
5632
5633 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5634 if (bf_get(lpfc_mbox_hdr_status,
5635 &rsrc_info->header.cfg_shdr.response)) {
5636 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5637 "2930 Failed to get resource extents "
5638 "Status 0x%x Add'l Status 0x%x\n",
5639 bf_get(lpfc_mbox_hdr_status,
5640 &rsrc_info->header.cfg_shdr.response),
5641 bf_get(lpfc_mbox_hdr_add_status,
5642 &rsrc_info->header.cfg_shdr.response));
5643 rc = -EIO;
5644 goto err_exit;
5645 }
5646
5647 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5648 &rsrc_info->u.rsp);
5649 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5650 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005651
5652 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5653 "3162 Retrieved extents type-%d from port: count:%d, "
5654 "size:%d\n", type, *extnt_count, *extnt_size);
5655
5656err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005657 mempool_free(mbox, phba->mbox_mem_pool);
5658 return rc;
5659}
5660
5661/**
5662 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5663 * @phba: Pointer to HBA context object.
5664 * @type: The extent type to check.
5665 *
5666 * This function reads the current available extents from the port and checks
5667 * if the extent count or extent size has changed since the last access.
5668 * Callers use this routine post port reset to understand if there is a
5669 * extent reprovisioning requirement.
5670 *
5671 * Returns:
5672 * -Error: error indicates problem.
5673 * 1: Extent count or size has changed.
5674 * 0: No changes.
5675 **/
5676static int
5677lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5678{
5679 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5680 uint16_t size_diff, rsrc_ext_size;
5681 int rc = 0;
5682 struct lpfc_rsrc_blks *rsrc_entry;
5683 struct list_head *rsrc_blk_list = NULL;
5684
5685 size_diff = 0;
5686 curr_ext_cnt = 0;
5687 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5688 &rsrc_ext_cnt,
5689 &rsrc_ext_size);
5690 if (unlikely(rc))
5691 return -EIO;
5692
5693 switch (type) {
5694 case LPFC_RSC_TYPE_FCOE_RPI:
5695 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5696 break;
5697 case LPFC_RSC_TYPE_FCOE_VPI:
5698 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5699 break;
5700 case LPFC_RSC_TYPE_FCOE_XRI:
5701 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5702 break;
5703 case LPFC_RSC_TYPE_FCOE_VFI:
5704 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5705 break;
5706 default:
5707 break;
5708 }
5709
5710 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5711 curr_ext_cnt++;
5712 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5713 size_diff++;
5714 }
5715
5716 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5717 rc = 1;
5718
5719 return rc;
5720}
5721
5722/**
5723 * lpfc_sli4_cfg_post_extnts -
5724 * @phba: Pointer to HBA context object.
5725 * @extnt_cnt - number of available extents.
5726 * @type - the extent type (rpi, xri, vfi, vpi).
5727 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5728 * @mbox - pointer to the caller's allocated mailbox structure.
5729 *
5730 * This function executes the extents allocation request. It also
5731 * takes care of the amount of memory needed to allocate or get the
5732 * allocated extents. It is the caller's responsibility to evaluate
5733 * the response.
5734 *
5735 * Returns:
5736 * -Error: Error value describes the condition found.
5737 * 0: if successful
5738 **/
5739static int
James Smart8a9d2e82012-05-09 21:16:12 -04005740lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005741 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5742{
5743 int rc = 0;
5744 uint32_t req_len;
5745 uint32_t emb_len;
5746 uint32_t alloc_len, mbox_tmo;
5747
5748 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005749 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005750
5751 /*
5752 * Calculate the size of an embedded mailbox. The uint32_t
5753 * accounts for extents-specific word.
5754 */
5755 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5756 sizeof(uint32_t);
5757
5758 /*
5759 * Presume the allocation and response will fit into an embedded
5760 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5761 */
5762 *emb = LPFC_SLI4_MBX_EMBED;
5763 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005764 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005765 sizeof(union lpfc_sli4_cfg_shdr) +
5766 sizeof(uint32_t);
5767 *emb = LPFC_SLI4_MBX_NEMBED;
5768 }
5769
5770 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5771 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5772 req_len, *emb);
5773 if (alloc_len < req_len) {
5774 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005775 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005776 "less than the requested DMA memory "
5777 "size (x%x)\n", alloc_len, req_len);
5778 return -ENOMEM;
5779 }
James Smart8a9d2e82012-05-09 21:16:12 -04005780 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005781 if (unlikely(rc))
5782 return -EIO;
5783
5784 if (!phba->sli4_hba.intr_enable)
5785 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5786 else {
James Smarta183a152011-10-10 21:32:43 -04005787 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005788 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5789 }
5790
5791 if (unlikely(rc))
5792 rc = -EIO;
5793 return rc;
5794}
5795
5796/**
5797 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5798 * @phba: Pointer to HBA context object.
5799 * @type: The resource extent type to allocate.
5800 *
5801 * This function allocates the number of elements for the specified
5802 * resource type.
5803 **/
5804static int
5805lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5806{
5807 bool emb = false;
5808 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5809 uint16_t rsrc_id, rsrc_start, j, k;
5810 uint16_t *ids;
5811 int i, rc;
5812 unsigned long longs;
5813 unsigned long *bmask;
5814 struct lpfc_rsrc_blks *rsrc_blks;
5815 LPFC_MBOXQ_t *mbox;
5816 uint32_t length;
5817 struct lpfc_id_range *id_array = NULL;
5818 void *virtaddr = NULL;
5819 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5820 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5821 struct list_head *ext_blk_list;
5822
5823 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5824 &rsrc_cnt,
5825 &rsrc_size);
5826 if (unlikely(rc))
5827 return -EIO;
5828
5829 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5830 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5831 "3009 No available Resource Extents "
5832 "for resource type 0x%x: Count: 0x%x, "
5833 "Size 0x%x\n", type, rsrc_cnt,
5834 rsrc_size);
5835 return -ENOMEM;
5836 }
5837
James Smart8a9d2e82012-05-09 21:16:12 -04005838 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5839 "2903 Post resource extents type-0x%x: "
5840 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005841
5842 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5843 if (!mbox)
5844 return -ENOMEM;
5845
James Smart8a9d2e82012-05-09 21:16:12 -04005846 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005847 if (unlikely(rc)) {
5848 rc = -EIO;
5849 goto err_exit;
5850 }
5851
5852 /*
5853 * Figure out where the response is located. Then get local pointers
5854 * to the response data. The port does not guarantee to respond to
5855 * all extents counts request so update the local variable with the
5856 * allocated count from the port.
5857 */
5858 if (emb == LPFC_SLI4_MBX_EMBED) {
5859 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5860 id_array = &rsrc_ext->u.rsp.id[0];
5861 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5862 } else {
5863 virtaddr = mbox->sge_array->addr[0];
5864 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5865 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5866 id_array = &n_rsrc->id;
5867 }
5868
5869 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5870 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5871
5872 /*
5873 * Based on the resource size and count, correct the base and max
5874 * resource values.
5875 */
5876 length = sizeof(struct lpfc_rsrc_blks);
5877 switch (type) {
5878 case LPFC_RSC_TYPE_FCOE_RPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005879 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005880 sizeof(unsigned long),
5881 GFP_KERNEL);
5882 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5883 rc = -ENOMEM;
5884 goto err_exit;
5885 }
Kees Cook6396bb22018-06-12 14:03:40 -07005886 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005887 sizeof(uint16_t),
5888 GFP_KERNEL);
5889 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5890 kfree(phba->sli4_hba.rpi_bmask);
5891 rc = -ENOMEM;
5892 goto err_exit;
5893 }
5894
5895 /*
5896 * The next_rpi was initialized with the maximum available
5897 * count but the port may allocate a smaller number. Catch
5898 * that case and update the next_rpi.
5899 */
5900 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5901
5902 /* Initialize local ptrs for common extent processing later. */
5903 bmask = phba->sli4_hba.rpi_bmask;
5904 ids = phba->sli4_hba.rpi_ids;
5905 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5906 break;
5907 case LPFC_RSC_TYPE_FCOE_VPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005908 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005909 GFP_KERNEL);
5910 if (unlikely(!phba->vpi_bmask)) {
5911 rc = -ENOMEM;
5912 goto err_exit;
5913 }
Kees Cook6396bb22018-06-12 14:03:40 -07005914 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04005915 GFP_KERNEL);
5916 if (unlikely(!phba->vpi_ids)) {
5917 kfree(phba->vpi_bmask);
5918 rc = -ENOMEM;
5919 goto err_exit;
5920 }
5921
5922 /* Initialize local ptrs for common extent processing later. */
5923 bmask = phba->vpi_bmask;
5924 ids = phba->vpi_ids;
5925 ext_blk_list = &phba->lpfc_vpi_blk_list;
5926 break;
5927 case LPFC_RSC_TYPE_FCOE_XRI:
Kees Cook6396bb22018-06-12 14:03:40 -07005928 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005929 sizeof(unsigned long),
5930 GFP_KERNEL);
5931 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5932 rc = -ENOMEM;
5933 goto err_exit;
5934 }
James Smart8a9d2e82012-05-09 21:16:12 -04005935 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07005936 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005937 sizeof(uint16_t),
5938 GFP_KERNEL);
5939 if (unlikely(!phba->sli4_hba.xri_ids)) {
5940 kfree(phba->sli4_hba.xri_bmask);
5941 rc = -ENOMEM;
5942 goto err_exit;
5943 }
5944
5945 /* Initialize local ptrs for common extent processing later. */
5946 bmask = phba->sli4_hba.xri_bmask;
5947 ids = phba->sli4_hba.xri_ids;
5948 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5949 break;
5950 case LPFC_RSC_TYPE_FCOE_VFI:
Kees Cook6396bb22018-06-12 14:03:40 -07005951 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005952 sizeof(unsigned long),
5953 GFP_KERNEL);
5954 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5955 rc = -ENOMEM;
5956 goto err_exit;
5957 }
Kees Cook6396bb22018-06-12 14:03:40 -07005958 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005959 sizeof(uint16_t),
5960 GFP_KERNEL);
5961 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5962 kfree(phba->sli4_hba.vfi_bmask);
5963 rc = -ENOMEM;
5964 goto err_exit;
5965 }
5966
5967 /* Initialize local ptrs for common extent processing later. */
5968 bmask = phba->sli4_hba.vfi_bmask;
5969 ids = phba->sli4_hba.vfi_ids;
5970 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5971 break;
5972 default:
5973 /* Unsupported Opcode. Fail call. */
5974 id_array = NULL;
5975 bmask = NULL;
5976 ids = NULL;
5977 ext_blk_list = NULL;
5978 goto err_exit;
5979 }
5980
5981 /*
5982 * Complete initializing the extent configuration with the
5983 * allocated ids assigned to this function. The bitmask serves
5984 * as an index into the array and manages the available ids. The
5985 * array just stores the ids communicated to the port via the wqes.
5986 */
5987 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5988 if ((i % 2) == 0)
5989 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5990 &id_array[k]);
5991 else
5992 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5993 &id_array[k]);
5994
5995 rsrc_blks = kzalloc(length, GFP_KERNEL);
5996 if (unlikely(!rsrc_blks)) {
5997 rc = -ENOMEM;
5998 kfree(bmask);
5999 kfree(ids);
6000 goto err_exit;
6001 }
6002 rsrc_blks->rsrc_start = rsrc_id;
6003 rsrc_blks->rsrc_size = rsrc_size;
6004 list_add_tail(&rsrc_blks->list, ext_blk_list);
6005 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08006006 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart5e5b5112019-01-28 11:14:22 -08006007 phba->sli4_hba.io_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08006008 lpfc_sli4_get_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08006009 }
James Smart6d368e52011-05-24 11:44:12 -04006010
6011 while (rsrc_id < (rsrc_start + rsrc_size)) {
6012 ids[j] = rsrc_id;
6013 rsrc_id++;
6014 j++;
6015 }
6016 /* Entire word processed. Get next word.*/
6017 if ((i % 2) == 1)
6018 k++;
6019 }
6020 err_exit:
6021 lpfc_sli4_mbox_cmd_free(phba, mbox);
6022 return rc;
6023}
6024
James Smart895427b2017-02-12 13:52:30 -08006025
6026
James Smart6d368e52011-05-24 11:44:12 -04006027/**
6028 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6029 * @phba: Pointer to HBA context object.
6030 * @type: the extent's type.
6031 *
6032 * This function deallocates all extents of a particular resource type.
6033 * SLI4 does not allow for deallocating a particular extent range. It
6034 * is the caller's responsibility to release all kernel memory resources.
6035 **/
6036static int
6037lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6038{
6039 int rc;
6040 uint32_t length, mbox_tmo = 0;
6041 LPFC_MBOXQ_t *mbox;
6042 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6043 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6044
6045 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6046 if (!mbox)
6047 return -ENOMEM;
6048
6049 /*
6050 * This function sends an embedded mailbox because it only sends the
6051 * the resource type. All extents of this type are released by the
6052 * port.
6053 */
6054 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6055 sizeof(struct lpfc_sli4_cfg_mhdr));
6056 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6057 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6058 length, LPFC_SLI4_MBX_EMBED);
6059
6060 /* Send an extents count of 0 - the dealloc doesn't use it. */
6061 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6062 LPFC_SLI4_MBX_EMBED);
6063 if (unlikely(rc)) {
6064 rc = -EIO;
6065 goto out_free_mbox;
6066 }
6067 if (!phba->sli4_hba.intr_enable)
6068 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6069 else {
James Smarta183a152011-10-10 21:32:43 -04006070 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006071 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6072 }
6073 if (unlikely(rc)) {
6074 rc = -EIO;
6075 goto out_free_mbox;
6076 }
6077
6078 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6079 if (bf_get(lpfc_mbox_hdr_status,
6080 &dealloc_rsrc->header.cfg_shdr.response)) {
6081 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6082 "2919 Failed to release resource extents "
6083 "for type %d - Status 0x%x Add'l Status 0x%x. "
6084 "Resource memory not released.\n",
6085 type,
6086 bf_get(lpfc_mbox_hdr_status,
6087 &dealloc_rsrc->header.cfg_shdr.response),
6088 bf_get(lpfc_mbox_hdr_add_status,
6089 &dealloc_rsrc->header.cfg_shdr.response));
6090 rc = -EIO;
6091 goto out_free_mbox;
6092 }
6093
6094 /* Release kernel memory resources for the specific type. */
6095 switch (type) {
6096 case LPFC_RSC_TYPE_FCOE_VPI:
6097 kfree(phba->vpi_bmask);
6098 kfree(phba->vpi_ids);
6099 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6100 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6101 &phba->lpfc_vpi_blk_list, list) {
6102 list_del_init(&rsrc_blk->list);
6103 kfree(rsrc_blk);
6104 }
James Smart16a3a202013-04-17 20:14:38 -04006105 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006106 break;
6107 case LPFC_RSC_TYPE_FCOE_XRI:
6108 kfree(phba->sli4_hba.xri_bmask);
6109 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006110 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6111 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6112 list_del_init(&rsrc_blk->list);
6113 kfree(rsrc_blk);
6114 }
6115 break;
6116 case LPFC_RSC_TYPE_FCOE_VFI:
6117 kfree(phba->sli4_hba.vfi_bmask);
6118 kfree(phba->sli4_hba.vfi_ids);
6119 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6120 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6121 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6122 list_del_init(&rsrc_blk->list);
6123 kfree(rsrc_blk);
6124 }
6125 break;
6126 case LPFC_RSC_TYPE_FCOE_RPI:
6127 /* RPI bitmask and physical id array are cleaned up earlier. */
6128 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6129 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6130 list_del_init(&rsrc_blk->list);
6131 kfree(rsrc_blk);
6132 }
6133 break;
6134 default:
6135 break;
6136 }
6137
6138 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6139
6140 out_free_mbox:
6141 mempool_free(mbox, phba->mbox_mem_pool);
6142 return rc;
6143}
6144
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006145static void
James Smart7bdedb32016-07-06 12:36:00 -07006146lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6147 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006148{
James Smart65791f12016-07-06 12:35:56 -07006149 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07006150
James Smart65791f12016-07-06 12:35:56 -07006151 len = sizeof(struct lpfc_mbx_set_feature) -
6152 sizeof(struct lpfc_sli4_cfg_mhdr);
6153 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6154 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6155 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006156
James Smart7bdedb32016-07-06 12:36:00 -07006157 switch (feature) {
6158 case LPFC_SET_UE_RECOVERY:
6159 bf_set(lpfc_mbx_set_feature_UER,
6160 &mbox->u.mqe.un.set_feature, 1);
6161 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6162 mbox->u.mqe.un.set_feature.param_len = 8;
6163 break;
6164 case LPFC_SET_MDS_DIAGS:
6165 bf_set(lpfc_mbx_set_feature_mds,
6166 &mbox->u.mqe.un.set_feature, 1);
6167 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006168 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006169 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6170 mbox->u.mqe.un.set_feature.param_len = 8;
6171 break;
James Smart65791f12016-07-06 12:35:56 -07006172 }
James Smart7bdedb32016-07-06 12:36:00 -07006173
6174 return;
James Smart65791f12016-07-06 12:35:56 -07006175}
6176
James Smart6d368e52011-05-24 11:44:12 -04006177/**
James Smart1165a5c2018-11-29 16:09:39 -08006178 * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6179 * @phba: Pointer to HBA context object.
6180 *
6181 * Disable FW logging into host memory on the adapter. To
6182 * be done before reading logs from the host memory.
6183 **/
6184void
6185lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6186{
6187 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6188
6189 ras_fwlog->ras_active = false;
6190
6191 /* Disable FW logging to host memory */
6192 writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6193 phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
6194}
6195
6196/**
James Smartd2cc9bc2018-09-10 10:30:50 -07006197 * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6198 * @phba: Pointer to HBA context object.
6199 *
6200 * This function is called to free memory allocated for RAS FW logging
6201 * support in the driver.
6202 **/
6203void
6204lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6205{
6206 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6207 struct lpfc_dmabuf *dmabuf, *next;
6208
6209 if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6210 list_for_each_entry_safe(dmabuf, next,
6211 &ras_fwlog->fwlog_buff_list,
6212 list) {
6213 list_del(&dmabuf->list);
6214 dma_free_coherent(&phba->pcidev->dev,
6215 LPFC_RAS_MAX_ENTRY_SIZE,
6216 dmabuf->virt, dmabuf->phys);
6217 kfree(dmabuf);
6218 }
6219 }
6220
6221 if (ras_fwlog->lwpd.virt) {
6222 dma_free_coherent(&phba->pcidev->dev,
6223 sizeof(uint32_t) * 2,
6224 ras_fwlog->lwpd.virt,
6225 ras_fwlog->lwpd.phys);
6226 ras_fwlog->lwpd.virt = NULL;
6227 }
6228
6229 ras_fwlog->ras_active = false;
6230}
6231
6232/**
6233 * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6234 * @phba: Pointer to HBA context object.
6235 * @fwlog_buff_count: Count of buffers to be created.
6236 *
6237 * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6238 * to update FW log is posted to the adapter.
6239 * Buffer count is calculated based on module param ras_fwlog_buffsize
6240 * Size of each buffer posted to FW is 64K.
6241 **/
6242
6243static int
6244lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6245 uint32_t fwlog_buff_count)
6246{
6247 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6248 struct lpfc_dmabuf *dmabuf;
6249 int rc = 0, i = 0;
6250
6251 /* Initialize List */
6252 INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6253
6254 /* Allocate memory for the LWPD */
6255 ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6256 sizeof(uint32_t) * 2,
6257 &ras_fwlog->lwpd.phys,
6258 GFP_KERNEL);
6259 if (!ras_fwlog->lwpd.virt) {
James Smartcb349902018-11-29 16:09:27 -08006260 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006261 "6185 LWPD Memory Alloc Failed\n");
6262
6263 return -ENOMEM;
6264 }
6265
6266 ras_fwlog->fw_buffcount = fwlog_buff_count;
6267 for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6268 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6269 GFP_KERNEL);
6270 if (!dmabuf) {
6271 rc = -ENOMEM;
6272 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6273 "6186 Memory Alloc failed FW logging");
6274 goto free_mem;
6275 }
6276
Luis Chamberlain750afb02019-01-04 09:23:09 +01006277 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartd2cc9bc2018-09-10 10:30:50 -07006278 LPFC_RAS_MAX_ENTRY_SIZE,
Luis Chamberlain750afb02019-01-04 09:23:09 +01006279 &dmabuf->phys, GFP_KERNEL);
James Smartd2cc9bc2018-09-10 10:30:50 -07006280 if (!dmabuf->virt) {
6281 kfree(dmabuf);
6282 rc = -ENOMEM;
6283 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6284 "6187 DMA Alloc Failed FW logging");
6285 goto free_mem;
6286 }
James Smartd2cc9bc2018-09-10 10:30:50 -07006287 dmabuf->buffer_tag = i;
6288 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6289 }
6290
6291free_mem:
6292 if (rc)
6293 lpfc_sli4_ras_dma_free(phba);
6294
6295 return rc;
6296}
6297
6298/**
6299 * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6300 * @phba: pointer to lpfc hba data structure.
6301 * @pmboxq: pointer to the driver internal queue element for mailbox command.
6302 *
6303 * Completion handler for driver's RAS MBX command to the device.
6304 **/
6305static void
6306lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6307{
6308 MAILBOX_t *mb;
6309 union lpfc_sli4_cfg_shdr *shdr;
6310 uint32_t shdr_status, shdr_add_status;
6311 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6312
6313 mb = &pmb->u.mb;
6314
6315 shdr = (union lpfc_sli4_cfg_shdr *)
6316 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6317 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6318 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6319
6320 if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
James Smartcb349902018-11-29 16:09:27 -08006321 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smartd2cc9bc2018-09-10 10:30:50 -07006322 "6188 FW LOG mailbox "
6323 "completed with status x%x add_status x%x,"
6324 " mbx status x%x\n",
6325 shdr_status, shdr_add_status, mb->mbxStatus);
James Smartcb349902018-11-29 16:09:27 -08006326
6327 ras_fwlog->ras_hwsupport = false;
James Smartd2cc9bc2018-09-10 10:30:50 -07006328 goto disable_ras;
6329 }
6330
6331 ras_fwlog->ras_active = true;
6332 mempool_free(pmb, phba->mbox_mem_pool);
6333
6334 return;
6335
6336disable_ras:
6337 /* Free RAS DMA memory */
6338 lpfc_sli4_ras_dma_free(phba);
6339 mempool_free(pmb, phba->mbox_mem_pool);
6340}
6341
6342/**
6343 * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6344 * @phba: pointer to lpfc hba data structure.
6345 * @fwlog_level: Logging verbosity level.
6346 * @fwlog_enable: Enable/Disable logging.
6347 *
6348 * Initialize memory and post mailbox command to enable FW logging in host
6349 * memory.
6350 **/
6351int
6352lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6353 uint32_t fwlog_level,
6354 uint32_t fwlog_enable)
6355{
6356 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6357 struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6358 struct lpfc_dmabuf *dmabuf;
6359 LPFC_MBOXQ_t *mbox;
6360 uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6361 int rc = 0;
6362
6363 fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6364 phba->cfg_ras_fwlog_buffsize);
6365 fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6366
6367 /*
6368 * If re-enabling FW logging support use earlier allocated
6369 * DMA buffers while posting MBX command.
6370 **/
6371 if (!ras_fwlog->lwpd.virt) {
6372 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6373 if (rc) {
6374 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smartcb349902018-11-29 16:09:27 -08006375 "6189 FW Log Memory Allocation Failed");
James Smartd2cc9bc2018-09-10 10:30:50 -07006376 return rc;
6377 }
6378 }
6379
6380 /* Setup Mailbox command */
6381 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6382 if (!mbox) {
James Smartcb349902018-11-29 16:09:27 -08006383 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006384 "6190 RAS MBX Alloc Failed");
6385 rc = -ENOMEM;
6386 goto mem_free;
6387 }
6388
6389 ras_fwlog->fw_loglevel = fwlog_level;
6390 len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6391 sizeof(struct lpfc_sli4_cfg_mhdr));
6392
6393 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6394 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6395 len, LPFC_SLI4_MBX_EMBED);
6396
6397 mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6398 bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6399 fwlog_enable);
6400 bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6401 ras_fwlog->fw_loglevel);
6402 bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6403 ras_fwlog->fw_buffcount);
6404 bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6405 LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6406
6407 /* Update DMA buffer address */
6408 list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6409 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6410
6411 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6412 putPaddrLow(dmabuf->phys);
6413
6414 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
6415 putPaddrHigh(dmabuf->phys);
6416 }
6417
6418 /* Update LPWD address */
6419 mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
6420 mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
6421
6422 mbox->vport = phba->pport;
6423 mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
6424
6425 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6426
6427 if (rc == MBX_NOT_FINISHED) {
James Smartcb349902018-11-29 16:09:27 -08006428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6429 "6191 FW-Log Mailbox failed. "
James Smartd2cc9bc2018-09-10 10:30:50 -07006430 "status %d mbxStatus : x%x", rc,
6431 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6432 mempool_free(mbox, phba->mbox_mem_pool);
6433 rc = -EIO;
6434 goto mem_free;
6435 } else
6436 rc = 0;
6437mem_free:
6438 if (rc)
6439 lpfc_sli4_ras_dma_free(phba);
6440
6441 return rc;
6442}
6443
6444/**
6445 * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
6446 * @phba: Pointer to HBA context object.
6447 *
6448 * Check if RAS is supported on the adapter and initialize it.
6449 **/
6450void
6451lpfc_sli4_ras_setup(struct lpfc_hba *phba)
6452{
6453 /* Check RAS FW Log needs to be enabled or not */
6454 if (lpfc_check_fwlog_support(phba))
6455 return;
6456
6457 lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
6458 LPFC_RAS_ENABLE_LOGGING);
6459}
6460
6461/**
James Smart6d368e52011-05-24 11:44:12 -04006462 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6463 * @phba: Pointer to HBA context object.
6464 *
6465 * This function allocates all SLI4 resource identifiers.
6466 **/
6467int
6468lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6469{
6470 int i, rc, error = 0;
6471 uint16_t count, base;
6472 unsigned long longs;
6473
James Smartff78d8f2011-12-13 13:21:35 -05006474 if (!phba->sli4_hba.rpi_hdrs_in_use)
6475 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04006476 if (phba->sli4_hba.extents_in_use) {
6477 /*
6478 * The port supports resource extents. The XRI, VPI, VFI, RPI
6479 * resource extent count must be read and allocated before
6480 * provisioning the resource id arrays.
6481 */
6482 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6483 LPFC_IDX_RSRC_RDY) {
6484 /*
6485 * Extent-based resources are set - the driver could
6486 * be in a port reset. Figure out if any corrective
6487 * actions need to be taken.
6488 */
6489 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6490 LPFC_RSC_TYPE_FCOE_VFI);
6491 if (rc != 0)
6492 error++;
6493 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6494 LPFC_RSC_TYPE_FCOE_VPI);
6495 if (rc != 0)
6496 error++;
6497 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6498 LPFC_RSC_TYPE_FCOE_XRI);
6499 if (rc != 0)
6500 error++;
6501 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6502 LPFC_RSC_TYPE_FCOE_RPI);
6503 if (rc != 0)
6504 error++;
6505
6506 /*
6507 * It's possible that the number of resources
6508 * provided to this port instance changed between
6509 * resets. Detect this condition and reallocate
6510 * resources. Otherwise, there is no action.
6511 */
6512 if (error) {
6513 lpfc_printf_log(phba, KERN_INFO,
6514 LOG_MBOX | LOG_INIT,
6515 "2931 Detected extent resource "
6516 "change. Reallocating all "
6517 "extents.\n");
6518 rc = lpfc_sli4_dealloc_extent(phba,
6519 LPFC_RSC_TYPE_FCOE_VFI);
6520 rc = lpfc_sli4_dealloc_extent(phba,
6521 LPFC_RSC_TYPE_FCOE_VPI);
6522 rc = lpfc_sli4_dealloc_extent(phba,
6523 LPFC_RSC_TYPE_FCOE_XRI);
6524 rc = lpfc_sli4_dealloc_extent(phba,
6525 LPFC_RSC_TYPE_FCOE_RPI);
6526 } else
6527 return 0;
6528 }
6529
6530 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6531 if (unlikely(rc))
6532 goto err_exit;
6533
6534 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6535 if (unlikely(rc))
6536 goto err_exit;
6537
6538 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6539 if (unlikely(rc))
6540 goto err_exit;
6541
6542 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6543 if (unlikely(rc))
6544 goto err_exit;
6545 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6546 LPFC_IDX_RSRC_RDY);
6547 return rc;
6548 } else {
6549 /*
6550 * The port does not support resource extents. The XRI, VPI,
6551 * VFI, RPI resource ids were determined from READ_CONFIG.
6552 * Just allocate the bitmasks and provision the resource id
6553 * arrays. If a port reset is active, the resources don't
6554 * need any action - just exit.
6555 */
6556 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006557 LPFC_IDX_RSRC_RDY) {
6558 lpfc_sli4_dealloc_resource_identifiers(phba);
6559 lpfc_sli4_remove_rpis(phba);
6560 }
James Smart6d368e52011-05-24 11:44:12 -04006561 /* RPIs. */
6562 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006563 if (count <= 0) {
6564 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6565 "3279 Invalid provisioning of "
6566 "rpi:%d\n", count);
6567 rc = -EINVAL;
6568 goto err_exit;
6569 }
James Smart6d368e52011-05-24 11:44:12 -04006570 base = phba->sli4_hba.max_cfg_param.rpi_base;
6571 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006572 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006573 sizeof(unsigned long),
6574 GFP_KERNEL);
6575 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6576 rc = -ENOMEM;
6577 goto err_exit;
6578 }
Kees Cook6396bb22018-06-12 14:03:40 -07006579 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006580 GFP_KERNEL);
6581 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6582 rc = -ENOMEM;
6583 goto free_rpi_bmask;
6584 }
6585
6586 for (i = 0; i < count; i++)
6587 phba->sli4_hba.rpi_ids[i] = base + i;
6588
6589 /* VPIs. */
6590 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006591 if (count <= 0) {
6592 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6593 "3280 Invalid provisioning of "
6594 "vpi:%d\n", count);
6595 rc = -EINVAL;
6596 goto free_rpi_ids;
6597 }
James Smart6d368e52011-05-24 11:44:12 -04006598 base = phba->sli4_hba.max_cfg_param.vpi_base;
6599 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006600 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006601 GFP_KERNEL);
6602 if (unlikely(!phba->vpi_bmask)) {
6603 rc = -ENOMEM;
6604 goto free_rpi_ids;
6605 }
Kees Cook6396bb22018-06-12 14:03:40 -07006606 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006607 GFP_KERNEL);
6608 if (unlikely(!phba->vpi_ids)) {
6609 rc = -ENOMEM;
6610 goto free_vpi_bmask;
6611 }
6612
6613 for (i = 0; i < count; i++)
6614 phba->vpi_ids[i] = base + i;
6615
6616 /* XRIs. */
6617 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006618 if (count <= 0) {
6619 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6620 "3281 Invalid provisioning of "
6621 "xri:%d\n", count);
6622 rc = -EINVAL;
6623 goto free_vpi_ids;
6624 }
James Smart6d368e52011-05-24 11:44:12 -04006625 base = phba->sli4_hba.max_cfg_param.xri_base;
6626 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006627 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006628 sizeof(unsigned long),
6629 GFP_KERNEL);
6630 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6631 rc = -ENOMEM;
6632 goto free_vpi_ids;
6633 }
James Smart41899be2012-03-01 22:34:19 -05006634 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006635 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006636 GFP_KERNEL);
6637 if (unlikely(!phba->sli4_hba.xri_ids)) {
6638 rc = -ENOMEM;
6639 goto free_xri_bmask;
6640 }
6641
6642 for (i = 0; i < count; i++)
6643 phba->sli4_hba.xri_ids[i] = base + i;
6644
6645 /* VFIs. */
6646 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006647 if (count <= 0) {
6648 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6649 "3282 Invalid provisioning of "
6650 "vfi:%d\n", count);
6651 rc = -EINVAL;
6652 goto free_xri_ids;
6653 }
James Smart6d368e52011-05-24 11:44:12 -04006654 base = phba->sli4_hba.max_cfg_param.vfi_base;
6655 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006656 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006657 sizeof(unsigned long),
6658 GFP_KERNEL);
6659 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6660 rc = -ENOMEM;
6661 goto free_xri_ids;
6662 }
Kees Cook6396bb22018-06-12 14:03:40 -07006663 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006664 GFP_KERNEL);
6665 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6666 rc = -ENOMEM;
6667 goto free_vfi_bmask;
6668 }
6669
6670 for (i = 0; i < count; i++)
6671 phba->sli4_hba.vfi_ids[i] = base + i;
6672
6673 /*
6674 * Mark all resources ready. An HBA reset doesn't need
6675 * to reset the initialization.
6676 */
6677 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6678 LPFC_IDX_RSRC_RDY);
6679 return 0;
6680 }
6681
6682 free_vfi_bmask:
6683 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006684 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006685 free_xri_ids:
6686 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006687 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006688 free_xri_bmask:
6689 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006690 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006691 free_vpi_ids:
6692 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006693 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006694 free_vpi_bmask:
6695 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006696 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006697 free_rpi_ids:
6698 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006699 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006700 free_rpi_bmask:
6701 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006702 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006703 err_exit:
6704 return rc;
6705}
6706
6707/**
6708 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6709 * @phba: Pointer to HBA context object.
6710 *
6711 * This function allocates the number of elements for the specified
6712 * resource type.
6713 **/
6714int
6715lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6716{
6717 if (phba->sli4_hba.extents_in_use) {
6718 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6719 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6720 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6721 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6722 } else {
6723 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006724 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006725 kfree(phba->vpi_ids);
6726 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6727 kfree(phba->sli4_hba.xri_bmask);
6728 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006729 kfree(phba->sli4_hba.vfi_bmask);
6730 kfree(phba->sli4_hba.vfi_ids);
6731 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6732 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6733 }
6734
6735 return 0;
6736}
6737
6738/**
James Smartb76f2dc2011-07-22 18:37:42 -04006739 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6740 * @phba: Pointer to HBA context object.
6741 * @type: The resource extent type.
6742 * @extnt_count: buffer to hold port extent count response
6743 * @extnt_size: buffer to hold port extent size response.
6744 *
6745 * This function calls the port to read the host allocated extents
6746 * for a particular type.
6747 **/
6748int
6749lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6750 uint16_t *extnt_cnt, uint16_t *extnt_size)
6751{
6752 bool emb;
6753 int rc = 0;
6754 uint16_t curr_blks = 0;
6755 uint32_t req_len, emb_len;
6756 uint32_t alloc_len, mbox_tmo;
6757 struct list_head *blk_list_head;
6758 struct lpfc_rsrc_blks *rsrc_blk;
6759 LPFC_MBOXQ_t *mbox;
6760 void *virtaddr = NULL;
6761 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6762 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6763 union lpfc_sli4_cfg_shdr *shdr;
6764
6765 switch (type) {
6766 case LPFC_RSC_TYPE_FCOE_VPI:
6767 blk_list_head = &phba->lpfc_vpi_blk_list;
6768 break;
6769 case LPFC_RSC_TYPE_FCOE_XRI:
6770 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6771 break;
6772 case LPFC_RSC_TYPE_FCOE_VFI:
6773 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6774 break;
6775 case LPFC_RSC_TYPE_FCOE_RPI:
6776 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6777 break;
6778 default:
6779 return -EIO;
6780 }
6781
6782 /* Count the number of extents currently allocatd for this type. */
6783 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6784 if (curr_blks == 0) {
6785 /*
6786 * The GET_ALLOCATED mailbox does not return the size,
6787 * just the count. The size should be just the size
6788 * stored in the current allocated block and all sizes
6789 * for an extent type are the same so set the return
6790 * value now.
6791 */
6792 *extnt_size = rsrc_blk->rsrc_size;
6793 }
6794 curr_blks++;
6795 }
6796
James Smartb76f2dc2011-07-22 18:37:42 -04006797 /*
6798 * Calculate the size of an embedded mailbox. The uint32_t
6799 * accounts for extents-specific word.
6800 */
6801 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6802 sizeof(uint32_t);
6803
6804 /*
6805 * Presume the allocation and response will fit into an embedded
6806 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6807 */
6808 emb = LPFC_SLI4_MBX_EMBED;
6809 req_len = emb_len;
6810 if (req_len > emb_len) {
6811 req_len = curr_blks * sizeof(uint16_t) +
6812 sizeof(union lpfc_sli4_cfg_shdr) +
6813 sizeof(uint32_t);
6814 emb = LPFC_SLI4_MBX_NEMBED;
6815 }
6816
6817 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6818 if (!mbox)
6819 return -ENOMEM;
6820 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6821
6822 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6823 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6824 req_len, emb);
6825 if (alloc_len < req_len) {
6826 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6827 "2983 Allocated DMA memory size (x%x) is "
6828 "less than the requested DMA memory "
6829 "size (x%x)\n", alloc_len, req_len);
6830 rc = -ENOMEM;
6831 goto err_exit;
6832 }
6833 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6834 if (unlikely(rc)) {
6835 rc = -EIO;
6836 goto err_exit;
6837 }
6838
6839 if (!phba->sli4_hba.intr_enable)
6840 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6841 else {
James Smarta183a152011-10-10 21:32:43 -04006842 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006843 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6844 }
6845
6846 if (unlikely(rc)) {
6847 rc = -EIO;
6848 goto err_exit;
6849 }
6850
6851 /*
6852 * Figure out where the response is located. Then get local pointers
6853 * to the response data. The port does not guarantee to respond to
6854 * all extents counts request so update the local variable with the
6855 * allocated count from the port.
6856 */
6857 if (emb == LPFC_SLI4_MBX_EMBED) {
6858 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6859 shdr = &rsrc_ext->header.cfg_shdr;
6860 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6861 } else {
6862 virtaddr = mbox->sge_array->addr[0];
6863 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6864 shdr = &n_rsrc->cfg_shdr;
6865 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6866 }
6867
6868 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6869 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6870 "2984 Failed to read allocated resources "
6871 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6872 type,
6873 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6874 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6875 rc = -EIO;
6876 goto err_exit;
6877 }
6878 err_exit:
6879 lpfc_sli4_mbox_cmd_free(phba, mbox);
6880 return rc;
6881}
6882
6883/**
James Smart0ef69962017-04-21 16:04:50 -07006884 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006885 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006886 * @pring: Pointer to driver SLI ring object.
6887 * @sgl_list: linked link of sgl buffers to post
6888 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006889 *
James Smart895427b2017-02-12 13:52:30 -08006890 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006891 * repost them to the port by using SGL block post. This is needed after a
6892 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006893 * of buffer sgls which contains contiguous xris and uses the non-embedded
6894 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006895 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6896 * mailbox command for posting.
6897 *
6898 * Returns: 0 = success, non-zero failure.
6899 **/
6900static int
James Smart895427b2017-02-12 13:52:30 -08006901lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6902 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006903{
6904 struct lpfc_sglq *sglq_entry = NULL;
6905 struct lpfc_sglq *sglq_entry_next = NULL;
6906 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006907 int status, total_cnt;
6908 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006909 int last_xritag = NO_XRI;
6910 LIST_HEAD(prep_sgl_list);
6911 LIST_HEAD(blck_sgl_list);
6912 LIST_HEAD(allc_sgl_list);
6913 LIST_HEAD(post_sgl_list);
6914 LIST_HEAD(free_sgl_list);
6915
James Smart38c20672013-03-01 16:37:44 -05006916 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006917 spin_lock(&phba->sli4_hba.sgl_list_lock);
6918 list_splice_init(sgl_list, &allc_sgl_list);
6919 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006920 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006921
James Smart895427b2017-02-12 13:52:30 -08006922 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006923 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6924 &allc_sgl_list, list) {
6925 list_del_init(&sglq_entry->list);
6926 block_cnt++;
6927 if ((last_xritag != NO_XRI) &&
6928 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6929 /* a hole in xri block, form a sgl posting block */
6930 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6931 post_cnt = block_cnt - 1;
6932 /* prepare list for next posting block */
6933 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6934 block_cnt = 1;
6935 } else {
6936 /* prepare list for next posting block */
6937 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6938 /* enough sgls for non-embed sgl mbox command */
6939 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6940 list_splice_init(&prep_sgl_list,
6941 &blck_sgl_list);
6942 post_cnt = block_cnt;
6943 block_cnt = 0;
6944 }
6945 }
6946 num_posted++;
6947
6948 /* keep track of last sgl's xritag */
6949 last_xritag = sglq_entry->sli4_xritag;
6950
James Smart895427b2017-02-12 13:52:30 -08006951 /* end of repost sgl list condition for buffers */
6952 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006953 if (post_cnt == 0) {
6954 list_splice_init(&prep_sgl_list,
6955 &blck_sgl_list);
6956 post_cnt = block_cnt;
6957 } else if (block_cnt == 1) {
6958 status = lpfc_sli4_post_sgl(phba,
6959 sglq_entry->phys, 0,
6960 sglq_entry->sli4_xritag);
6961 if (!status) {
6962 /* successful, put sgl to posted list */
6963 list_add_tail(&sglq_entry->list,
6964 &post_sgl_list);
6965 } else {
6966 /* Failure, put sgl to free list */
6967 lpfc_printf_log(phba, KERN_WARNING,
6968 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006969 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006970 "sgl, xritag:x%x\n",
6971 sglq_entry->sli4_xritag);
6972 list_add_tail(&sglq_entry->list,
6973 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006974 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006975 }
6976 }
6977 }
6978
6979 /* continue until a nembed page worth of sgls */
6980 if (post_cnt == 0)
6981 continue;
6982
James Smart895427b2017-02-12 13:52:30 -08006983 /* post the buffer list sgls as a block */
6984 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6985 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006986
6987 if (!status) {
6988 /* success, put sgl list to posted sgl list */
6989 list_splice_init(&blck_sgl_list, &post_sgl_list);
6990 } else {
6991 /* Failure, put sgl list to free sgl list */
6992 sglq_entry_first = list_first_entry(&blck_sgl_list,
6993 struct lpfc_sglq,
6994 list);
6995 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006996 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04006997 "xritag:x%x-x%x\n",
6998 sglq_entry_first->sli4_xritag,
6999 (sglq_entry_first->sli4_xritag +
7000 post_cnt - 1));
7001 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04007002 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007003 }
7004
7005 /* don't reset xirtag due to hole in xri block */
7006 if (block_cnt == 0)
7007 last_xritag = NO_XRI;
7008
James Smart895427b2017-02-12 13:52:30 -08007009 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04007010 post_cnt = 0;
7011 }
7012
James Smart895427b2017-02-12 13:52:30 -08007013 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04007014 lpfc_free_sgl_list(phba, &free_sgl_list);
7015
James Smart895427b2017-02-12 13:52:30 -08007016 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04007017 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05007018 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007019 spin_lock(&phba->sli4_hba.sgl_list_lock);
7020 list_splice_init(&post_sgl_list, sgl_list);
7021 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05007022 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04007023 } else {
7024 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007025 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04007026 return -EIO;
7027 }
James Smart895427b2017-02-12 13:52:30 -08007028
7029 /* return the number of XRIs actually posted */
7030 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007031}
7032
James Smart0794d602019-01-28 11:14:19 -08007033/**
James Smart5e5b5112019-01-28 11:14:22 -08007034 * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
James Smart0794d602019-01-28 11:14:19 -08007035 * @phba: pointer to lpfc hba data structure.
7036 *
7037 * This routine walks the list of nvme buffers that have been allocated and
7038 * repost them to the port by using SGL block post. This is needed after a
7039 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7040 * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
James Smart5e5b5112019-01-28 11:14:22 -08007041 * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
James Smart0794d602019-01-28 11:14:19 -08007042 *
7043 * Returns: 0 = success, non-zero failure.
7044 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07007045static int
James Smart5e5b5112019-01-28 11:14:22 -08007046lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
James Smart0794d602019-01-28 11:14:19 -08007047{
7048 LIST_HEAD(post_nblist);
7049 int num_posted, rc = 0;
7050
7051 /* get all NVME buffers need to repost to a local list */
James Smart5e5b5112019-01-28 11:14:22 -08007052 lpfc_io_buf_flush(phba, &post_nblist);
James Smart0794d602019-01-28 11:14:19 -08007053
7054 /* post the list of nvme buffer sgls to port if available */
7055 if (!list_empty(&post_nblist)) {
James Smart5e5b5112019-01-28 11:14:22 -08007056 num_posted = lpfc_sli4_post_io_sgl_list(
7057 phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
James Smart0794d602019-01-28 11:14:19 -08007058 /* failed to post any nvme buffer, return error */
7059 if (num_posted == 0)
7060 rc = -EIO;
7061 }
7062 return rc;
7063}
7064
Bart Van Assche3999df72019-03-28 11:06:16 -07007065static void
James Smart61bda8f2016-10-13 15:06:05 -07007066lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7067{
7068 uint32_t len;
7069
7070 len = sizeof(struct lpfc_mbx_set_host_data) -
7071 sizeof(struct lpfc_sli4_cfg_mhdr);
7072 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7073 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7074 LPFC_SLI4_MBX_EMBED);
7075
7076 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08007077 mbox->u.mqe.un.set_host_data.param_len =
7078 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07007079 snprintf(mbox->u.mqe.un.set_host_data.data,
7080 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7081 "Linux %s v"LPFC_DRIVER_VERSION,
7082 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7083}
7084
James Smarta8cf5df2017-05-15 15:20:46 -07007085int
James Smart6c621a22017-05-15 15:20:45 -07007086lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07007087 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07007088{
7089 int rc, i;
7090 struct lpfc_rqe hrqe;
7091 struct lpfc_rqe drqe;
7092 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08007093 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07007094 struct rqb_dmabuf *rqb_buffer;
7095 LIST_HEAD(rqb_buf_list);
7096
James Smart411de512018-01-30 15:58:52 -08007097 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007098 rqbp = hrq->rqbp;
7099 for (i = 0; i < count; i++) {
7100 /* IF RQ is already full, don't bother */
7101 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
7102 break;
7103 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7104 if (!rqb_buffer)
7105 break;
7106 rqb_buffer->hrq = hrq;
7107 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07007108 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07007109 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7110 }
7111 while (!list_empty(&rqb_buf_list)) {
7112 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7113 hbuf.list);
7114
7115 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7116 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7117 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7118 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7119 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7120 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08007121 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7122 "6421 Cannot post to HRQ %d: %x %x %x "
7123 "DRQ %x %x\n",
7124 hrq->queue_id,
7125 hrq->host_index,
7126 hrq->hba_index,
7127 hrq->entry_count,
7128 drq->host_index,
7129 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07007130 rqbp->rqb_free_buffer(phba, rqb_buffer);
7131 } else {
7132 list_add_tail(&rqb_buffer->hbuf.list,
7133 &rqbp->rqb_buffer_list);
7134 rqbp->buffer_count++;
7135 }
7136 }
James Smart411de512018-01-30 15:58:52 -08007137 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007138 return 1;
7139}
7140
James Smart8a9d2e82012-05-09 21:16:12 -04007141/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08007142 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04007143 * @phba: Pointer to HBA context object.
7144 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08007145 * This function is the main SLI4 device initialization PCI function. This
7146 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04007147 * HBA error attention handler code. Caller is not required to hold any
7148 * locks.
7149 **/
7150int
7151lpfc_sli4_hba_setup(struct lpfc_hba *phba)
7152{
James Smartc4908502019-01-28 11:14:28 -08007153 int rc, i, cnt, len;
James Smartda0436e2009-05-22 14:51:39 -04007154 LPFC_MBOXQ_t *mboxq;
7155 struct lpfc_mqe *mqe;
7156 uint8_t *vpd;
7157 uint32_t vpd_size;
7158 uint32_t ftr_rsp = 0;
7159 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
7160 struct lpfc_vport *vport = phba->pport;
7161 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08007162 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04007163
7164 /* Perform a PCI function reset to start from clean */
7165 rc = lpfc_pci_function_reset(phba);
7166 if (unlikely(rc))
7167 return -ENODEV;
7168
7169 /* Check the HBA Host Status Register for readyness */
7170 rc = lpfc_sli4_post_status_check(phba);
7171 if (unlikely(rc))
7172 return -ENODEV;
7173 else {
7174 spin_lock_irq(&phba->hbalock);
7175 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
7176 spin_unlock_irq(&phba->hbalock);
7177 }
7178
7179 /*
7180 * Allocate a single mailbox container for initializing the
7181 * port.
7182 */
7183 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7184 if (!mboxq)
7185 return -ENOMEM;
7186
James Smartda0436e2009-05-22 14:51:39 -04007187 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04007188 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04007189 vpd = kzalloc(vpd_size, GFP_KERNEL);
7190 if (!vpd) {
7191 rc = -ENOMEM;
7192 goto out_free_mbox;
7193 }
7194
7195 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05007196 if (unlikely(rc)) {
7197 kfree(vpd);
7198 goto out_free_mbox;
7199 }
James Smart572709e2013-07-15 18:32:43 -04007200
James Smartda0436e2009-05-22 14:51:39 -04007201 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04007202 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07007203 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05007204 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007205 phba->fcp_embed_io = 0; /* SLI4 FC support only */
7206 } else {
James Smart76a95d72010-11-20 23:11:48 -05007207 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007208 }
James Smart45ed1192009-10-02 15:17:02 -04007209
7210 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
7211 LPFC_DCBX_CEE_MODE)
7212 phba->hba_flag |= HBA_FIP_SUPPORT;
7213 else
7214 phba->hba_flag &= ~HBA_FIP_SUPPORT;
7215
James Smartc00f62e2019-08-14 16:57:11 -07007216 phba->hba_flag &= ~HBA_IOQ_FLUSH;
James Smart4f2e66c2012-05-09 21:17:07 -04007217
James Smartc31098c2011-04-16 11:03:33 -04007218 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007219 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7220 "0376 READ_REV Error. SLI Level %d "
7221 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05007222 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04007223 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007224 kfree(vpd);
7225 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007226 }
James Smartcd1c8302011-10-10 21:33:25 -04007227
7228 /*
James Smartff78d8f2011-12-13 13:21:35 -05007229 * Continue initialization with default values even if driver failed
7230 * to read FCoE param config regions, only read parameters if the
7231 * board is FCoE
7232 */
7233 if (phba->hba_flag & HBA_FCOE_MODE &&
7234 lpfc_sli4_read_fcoe_params(phba))
7235 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
7236 "2570 Failed to read FCoE parameters\n");
7237
7238 /*
James Smartcd1c8302011-10-10 21:33:25 -04007239 * Retrieve sli4 device physical port name, failure of doing it
7240 * is considered as non-fatal.
7241 */
7242 rc = lpfc_sli4_retrieve_pport_name(phba);
7243 if (!rc)
7244 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7245 "3080 Successful retrieving SLI4 device "
7246 "physical port name: %s.\n", phba->Port);
7247
James Smartb3b4f3e2019-03-12 16:30:23 -07007248 rc = lpfc_sli4_get_ctl_attr(phba);
7249 if (!rc)
7250 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7251 "8351 Successful retrieving SLI4 device "
7252 "CTL ATTR\n");
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
James Smarta5f73372019-09-21 20:58:50 -07007528 /* We allocate an iocbq for every receive context SGL.
7529 * The additional allocation is for abort and ls handling.
7530 */
7531 cnt = phba->sli4_hba.nvmet_xri_cnt +
7532 phba->sli4_hba.max_cfg_param.max_xri;
James Smartf358dd02017-02-12 13:52:34 -08007533 } else {
James Smart0794d602019-01-28 11:14:19 -08007534 /* update host common xri-sgl sizes and mappings */
James Smart5e5b5112019-01-28 11:14:22 -08007535 rc = lpfc_sli4_io_sgl_update(phba);
James Smart895427b2017-02-12 13:52:30 -08007536 if (unlikely(rc)) {
7537 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7538 "6082 Failed to update nvme-sgl size "
7539 "and mapping: %d\n", rc);
7540 goto out_destroy_queue;
7541 }
James Smart6c621a22017-05-15 15:20:45 -07007542
James Smart0794d602019-01-28 11:14:19 -08007543 /* register the allocated common sgl pool to the port */
James Smart5e5b5112019-01-28 11:14:22 -08007544 rc = lpfc_sli4_repost_io_sgl_list(phba);
James Smart0794d602019-01-28 11:14:19 -08007545 if (unlikely(rc)) {
7546 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7547 "6116 Error %d during nvme sgl post "
7548 "operation\n", rc);
7549 /* Some NVME buffers were moved to abort nvme list */
7550 /* A pci function reset will repost them */
7551 rc = -ENODEV;
7552 goto out_destroy_queue;
7553 }
James Smarta5f73372019-09-21 20:58:50 -07007554 /* Each lpfc_io_buf job structure has an iocbq element.
7555 * This cnt provides for abort, els, ct and ls requests.
7556 */
7557 cnt = phba->sli4_hba.max_cfg_param.max_xri;
James Smart11e644e2017-06-15 22:56:48 -07007558 }
7559
7560 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07007561 /* Initialize and populate the iocb list per host */
7562 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta5f73372019-09-21 20:58:50 -07007563 "2821 initialize iocb list with %d entries\n",
7564 cnt);
James Smart6c621a22017-05-15 15:20:45 -07007565 rc = lpfc_init_iocb_list(phba, cnt);
7566 if (rc) {
7567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007568 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07007569 goto out_destroy_queue;
7570 }
James Smart8a9d2e82012-05-09 21:16:12 -04007571 }
7572
James Smart11e644e2017-06-15 22:56:48 -07007573 if (phba->nvmet_support)
7574 lpfc_nvmet_create_targetport(phba);
7575
James Smart2d7dbc42017-02-12 13:52:35 -08007576 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007577 /* Post initial buffers to all RQs created */
7578 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7579 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7580 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7581 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7582 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007583 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007584 rqbp->buffer_count = 0;
7585
James Smart2d7dbc42017-02-12 13:52:35 -08007586 lpfc_post_rq_buffer(
7587 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7588 phba->sli4_hba.nvmet_mrq_data[i],
James Smart2448e482018-04-09 14:24:24 -07007589 phba->cfg_nvmet_mrq_post, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007590 }
7591 }
7592
James Smartda0436e2009-05-22 14:51:39 -04007593 /* Post the rpi header region to the device. */
7594 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7595 if (unlikely(rc)) {
7596 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7597 "0393 Error %d during rpi post operation\n",
7598 rc);
7599 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007600 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007601 }
James Smart97f2ecf2012-03-01 22:35:23 -05007602 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007603
James Smart895427b2017-02-12 13:52:30 -08007604 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007605 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007606 /*
7607 * The FC Port needs to register FCFI (index 0)
7608 */
7609 lpfc_reg_fcfi(phba, mboxq);
7610 mboxq->vport = phba->pport;
7611 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7612 if (rc != MBX_SUCCESS)
7613 goto out_unset_queue;
7614 rc = 0;
7615 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7616 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007617 } else {
7618 /* We are a NVME Target mode with MRQ > 1 */
7619
7620 /* First register the FCFI */
7621 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7622 mboxq->vport = phba->pport;
7623 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7624 if (rc != MBX_SUCCESS)
7625 goto out_unset_queue;
7626 rc = 0;
7627 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7628 &mboxq->u.mqe.un.reg_fcfi_mrq);
7629
7630 /* Next register the MRQs */
7631 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7632 mboxq->vport = phba->pport;
7633 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7634 if (rc != MBX_SUCCESS)
7635 goto out_unset_queue;
7636 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007637 }
7638 /* Check if the port is configured to be disabled */
7639 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007640 }
7641
James Smartc4908502019-01-28 11:14:28 -08007642 /* Don't post more new bufs if repost already recovered
7643 * the nvme sgls.
7644 */
7645 if (phba->nvmet_support == 0) {
7646 if (phba->sli4_hba.io_xri_cnt == 0) {
7647 len = lpfc_new_io_buf(
7648 phba, phba->sli4_hba.io_xri_max);
7649 if (len == 0) {
7650 rc = -ENOMEM;
7651 goto out_unset_queue;
7652 }
7653
7654 if (phba->cfg_xri_rebalancing)
7655 lpfc_create_multixri_pools(phba);
7656 }
7657 } else {
7658 phba->cfg_xri_rebalancing = 0;
7659 }
7660
James Smartda0436e2009-05-22 14:51:39 -04007661 /* Allow asynchronous mailbox command to go through */
7662 spin_lock_irq(&phba->hbalock);
7663 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7664 spin_unlock_irq(&phba->hbalock);
7665
7666 /* Post receive buffers to the device */
7667 lpfc_sli4_rb_setup(phba);
7668
James Smartfc2b9892010-02-26 14:15:29 -05007669 /* Reset HBA FCF states after HBA reset */
7670 phba->fcf.fcf_flag = 0;
7671 phba->fcf.current_rec.flag = 0;
7672
James Smartda0436e2009-05-22 14:51:39 -04007673 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007674 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007675 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007676
7677 /* Start heart beat timer */
7678 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007679 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007680 phba->hb_outstanding = 0;
7681 phba->last_completion_time = jiffies;
7682
James Smart32517fc2019-01-28 11:14:33 -08007683 /* start eq_delay heartbeat */
7684 if (phba->cfg_auto_imax)
7685 queue_delayed_work(phba->wq, &phba->eq_delay_work,
7686 msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
7687
James Smartda0436e2009-05-22 14:51:39 -04007688 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007689 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007690 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007691
James Smart75baf692010-06-08 18:31:21 -04007692 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7693 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7694 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7695 if (!rc) {
7696 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7697 "2829 This device supports "
7698 "Advanced Error Reporting (AER)\n");
7699 spin_lock_irq(&phba->hbalock);
7700 phba->hba_flag |= HBA_AER_ENABLED;
7701 spin_unlock_irq(&phba->hbalock);
7702 } else {
7703 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7704 "2830 This device does not support "
7705 "Advanced Error Reporting (AER)\n");
7706 phba->cfg_aer_support = 0;
7707 }
James Smart0a96e972011-07-22 18:37:28 -04007708 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007709 }
7710
James Smartda0436e2009-05-22 14:51:39 -04007711 /*
7712 * The port is ready, set the host's link state to LINK_DOWN
7713 * in preparation for link interrupts.
7714 */
James Smartda0436e2009-05-22 14:51:39 -04007715 spin_lock_irq(&phba->hbalock);
7716 phba->link_state = LPFC_LINK_DOWN;
James Smart1dc5ec22018-10-23 13:41:11 -07007717
7718 /* Check if physical ports are trunked */
7719 if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
7720 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
7721 if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
7722 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
7723 if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
7724 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
7725 if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
7726 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04007727 spin_unlock_irq(&phba->hbalock);
James Smart1dc5ec22018-10-23 13:41:11 -07007728
James Smarte8869f52019-03-12 16:30:18 -07007729 /* Arm the CQs and then EQs on device */
7730 lpfc_sli4_arm_cqeq_intr(phba);
7731
7732 /* Indicate device interrupt mode */
7733 phba->sli4_hba.intr_enable = 1;
7734
James Smart026abb82011-12-13 13:20:45 -05007735 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7736 (phba->hba_flag & LINK_DISABLED)) {
7737 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7738 "3103 Adapter Link is disabled.\n");
7739 lpfc_down_link(phba, mboxq);
7740 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7741 if (rc != MBX_SUCCESS) {
7742 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7743 "3104 Adapter failed to issue "
7744 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
James Smartc4908502019-01-28 11:14:28 -08007745 goto out_io_buff_free;
James Smart026abb82011-12-13 13:20:45 -05007746 }
7747 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007748 /* don't perform init_link on SLI4 FC port loopback test */
7749 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7750 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7751 if (rc)
James Smartc4908502019-01-28 11:14:28 -08007752 goto out_io_buff_free;
James Smart1b511972011-12-13 13:23:09 -05007753 }
James Smart5350d872011-10-10 21:33:49 -04007754 }
7755 mempool_free(mboxq, phba->mbox_mem_pool);
7756 return rc;
James Smartc4908502019-01-28 11:14:28 -08007757out_io_buff_free:
7758 /* Free allocated IO Buffers */
7759 lpfc_io_free(phba);
James Smart76a95d72010-11-20 23:11:48 -05007760out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007761 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007762 lpfc_sli4_queue_unset(phba);
7763out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007764 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007765 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007766out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007767 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007768out_free_mbox:
7769 mempool_free(mboxq, phba->mbox_mem_pool);
7770 return rc;
7771}
James Smarte59058c2008-08-24 21:49:00 -04007772
7773/**
James Smart3621a712009-04-06 18:47:14 -04007774 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007775 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007776 *
James Smarte59058c2008-08-24 21:49:00 -04007777 * This is the callback function for mailbox timer. The mailbox
7778 * timer is armed when a new mailbox command is issued and the timer
7779 * is deleted when the mailbox complete. The function is called by
7780 * the kernel timer code when a mailbox does not complete within
7781 * expected time. This function wakes up the worker thread to
7782 * process the mailbox timeout and returns. All the processing is
7783 * done by the worker thread function lpfc_mbox_timeout_handler.
7784 **/
dea31012005-04-17 16:05:31 -05007785void
Kees Cookf22eb4d2017-09-06 20:24:26 -07007786lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05007787{
Kees Cookf22eb4d2017-09-06 20:24:26 -07007788 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05007789 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007790 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007791
James Smart2e0fef82007-06-17 19:56:36 -05007792 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007793 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007794 if (!tmo_posted)
7795 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7796 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7797
James Smart5e9d9b82008-06-14 22:52:53 -04007798 if (!tmo_posted)
7799 lpfc_worker_wake_up(phba);
7800 return;
dea31012005-04-17 16:05:31 -05007801}
7802
James Smarte8d3c3b2013-10-10 12:21:30 -04007803/**
7804 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7805 * are pending
7806 * @phba: Pointer to HBA context object.
7807 *
7808 * This function checks if any mailbox completions are present on the mailbox
7809 * completion queue.
7810 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007811static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007812lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7813{
7814
7815 uint32_t idx;
7816 struct lpfc_queue *mcq;
7817 struct lpfc_mcqe *mcqe;
7818 bool pending_completions = false;
James Smart7365f6f2018-02-22 08:18:46 -08007819 uint8_t qe_valid;
James Smarte8d3c3b2013-10-10 12:21:30 -04007820
7821 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7822 return false;
7823
7824 /* Check for completions on mailbox completion queue */
7825
7826 mcq = phba->sli4_hba.mbx_cq;
7827 idx = mcq->hba_index;
James Smart7365f6f2018-02-22 08:18:46 -08007828 qe_valid = mcq->qe_valid;
James Smart9afbee32019-03-12 16:30:28 -07007829 while (bf_get_le32(lpfc_cqe_valid,
7830 (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
7831 mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
James Smarte8d3c3b2013-10-10 12:21:30 -04007832 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7833 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7834 pending_completions = true;
7835 break;
7836 }
7837 idx = (idx + 1) % mcq->entry_count;
7838 if (mcq->hba_index == idx)
7839 break;
James Smart7365f6f2018-02-22 08:18:46 -08007840
7841 /* if the index wrapped around, toggle the valid bit */
7842 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7843 qe_valid = (qe_valid) ? 0 : 1;
James Smarte8d3c3b2013-10-10 12:21:30 -04007844 }
7845 return pending_completions;
7846
7847}
7848
7849/**
7850 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7851 * that were missed.
7852 * @phba: Pointer to HBA context object.
7853 *
7854 * For sli4, it is possible to miss an interrupt. As such mbox completions
7855 * maybe missed causing erroneous mailbox timeouts to occur. This function
7856 * checks to see if mbox completions are on the mailbox completion queue
7857 * and will process all the completions associated with the eq for the
7858 * mailbox completion queue.
7859 **/
YueHaibingd7b761b2019-05-31 23:28:41 +08007860static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007861lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7862{
James Smartb71413d2018-02-22 08:18:40 -08007863 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04007864 uint32_t eqidx;
7865 struct lpfc_queue *fpeq = NULL;
James Smart657add42019-05-21 17:49:06 -07007866 struct lpfc_queue *eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04007867 bool mbox_pending;
7868
7869 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7870 return false;
7871
James Smart657add42019-05-21 17:49:06 -07007872 /* Find the EQ associated with the mbox CQ */
7873 if (sli4_hba->hdwq) {
7874 for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
7875 eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
7876 if (eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
7877 fpeq = eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04007878 break;
7879 }
James Smart657add42019-05-21 17:49:06 -07007880 }
7881 }
James Smarte8d3c3b2013-10-10 12:21:30 -04007882 if (!fpeq)
7883 return false;
7884
7885 /* Turn off interrupts from this EQ */
7886
James Smartb71413d2018-02-22 08:18:40 -08007887 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04007888
7889 /* Check to see if a mbox completion is pending */
7890
7891 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7892
7893 /*
7894 * If a mbox completion is pending, process all the events on EQ
7895 * associated with the mbox completion queue (this could include
7896 * mailbox commands, async events, els commands, receive queue data
7897 * and fcp commands)
7898 */
7899
7900 if (mbox_pending)
James Smart32517fc2019-01-28 11:14:33 -08007901 /* process and rearm the EQ */
7902 lpfc_sli4_process_eq(phba, fpeq);
7903 else
7904 /* Always clear and re-arm the EQ */
7905 sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04007906
7907 return mbox_pending;
7908
7909}
James Smarte59058c2008-08-24 21:49:00 -04007910
7911/**
James Smart3621a712009-04-06 18:47:14 -04007912 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007913 * @phba: Pointer to HBA context object.
7914 *
7915 * This function is called from worker thread when a mailbox command times out.
7916 * The caller is not required to hold any locks. This function will reset the
7917 * HBA and recover all the pending commands.
7918 **/
dea31012005-04-17 16:05:31 -05007919void
7920lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7921{
James Smart2e0fef82007-06-17 19:56:36 -05007922 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007923 MAILBOX_t *mb = NULL;
7924
James Smart1dcb58e2007-04-25 09:51:30 -04007925 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007926
James Smarte8d3c3b2013-10-10 12:21:30 -04007927 /* If the mailbox completed, process the completion and return */
7928 if (lpfc_sli4_process_missed_mbox_completions(phba))
7929 return;
7930
James Smarteb016562014-09-03 12:58:06 -04007931 if (pmbox != NULL)
7932 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007933 /* Check the pmbox pointer first. There is a race condition
7934 * between the mbox timeout handler getting executed in the
7935 * worklist and the mailbox actually completing. When this
7936 * race condition occurs, the mbox_active will be NULL.
7937 */
7938 spin_lock_irq(&phba->hbalock);
7939 if (pmbox == NULL) {
7940 lpfc_printf_log(phba, KERN_WARNING,
7941 LOG_MBOX | LOG_SLI,
7942 "0353 Active Mailbox cleared - mailbox timeout "
7943 "exiting\n");
7944 spin_unlock_irq(&phba->hbalock);
7945 return;
7946 }
7947
dea31012005-04-17 16:05:31 -05007948 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007949 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart32350662019-08-14 16:57:06 -07007950 "0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007951 mb->mbxCommand,
7952 phba->pport->port_state,
7953 phba->sli.sli_flag,
7954 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007955 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007956
James Smart1dcb58e2007-04-25 09:51:30 -04007957 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7958 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007959 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007960 */
James Smart2e0fef82007-06-17 19:56:36 -05007961 spin_lock_irq(&phba->pport->work_port_lock);
7962 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7963 spin_unlock_irq(&phba->pport->work_port_lock);
7964 spin_lock_irq(&phba->hbalock);
7965 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007966 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007967 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007968
James Smartdb55fba2014-04-04 13:52:02 -04007969 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007970
7971 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007972 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007973
7974 /* Reset the HBA device */
7975 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007976}
7977
James Smarte59058c2008-08-24 21:49:00 -04007978/**
James Smart3772a992009-05-22 14:50:54 -04007979 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007980 * @phba: Pointer to HBA context object.
7981 * @pmbox: Pointer to mailbox object.
7982 * @flag: Flag indicating how the mailbox need to be processed.
7983 *
7984 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007985 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7986 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007987 * The mailbox command can be submitted in polling mode, in which case
7988 * this function will wait in a polling loop for the completion of the
7989 * mailbox.
7990 * If the mailbox is submitted in no_wait mode (not polling) the
7991 * function will submit the command and returns immediately without waiting
7992 * for the mailbox completion. The no_wait is supported only when HBA
7993 * is in SLI2/SLI3 mode - interrupts are enabled.
7994 * The SLI interface allows only one mailbox pending at a time. If the
7995 * mailbox is issued in polling mode and there is already a mailbox
7996 * pending, then the function will return an error. If the mailbox is issued
7997 * in NO_WAIT mode and there is a mailbox pending already, the function
7998 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7999 * The sli layer owns the mailbox object until the completion of mailbox
8000 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
8001 * return codes the caller owns the mailbox command after the return of
8002 * the function.
8003 **/
James Smart3772a992009-05-22 14:50:54 -04008004static int
8005lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
8006 uint32_t flag)
dea31012005-04-17 16:05:31 -05008007{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008008 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05008009 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008010 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05008011 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008012 int i;
James Smart09372822008-01-11 01:52:54 -05008013 unsigned long timeout;
dea31012005-04-17 16:05:31 -05008014 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04008015 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05008016 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04008017 int processing_queue = 0;
8018
8019 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8020 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04008021 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04008022 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04008023 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8024 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8025 return MBX_SUCCESS;
8026 }
James Smart58da1ff2008-04-07 10:15:56 -04008027 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04008028 pmbox = lpfc_mbox_get(phba);
8029 if (!pmbox) {
8030 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8031 return MBX_SUCCESS;
8032 }
8033 }
dea31012005-04-17 16:05:31 -05008034
James Smarted957682007-06-17 19:56:37 -05008035 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05008036 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05008037 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04008038 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05008039 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05008040 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008041 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04008042 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05008043 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04008044 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05008045 }
8046 }
8047
Linas Vepstas8d63f372007-02-14 14:28:36 -06008048 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04008049 if (unlikely(pci_channel_offline(phba->pcidev))) {
8050 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8051 goto out_not_finished;
8052 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06008053
James Smarta257bf92009-04-06 18:48:10 -04008054 /* If HBA has a deferred error attention, fail the iocb. */
8055 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8056 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8057 goto out_not_finished;
8058 }
8059
dea31012005-04-17 16:05:31 -05008060 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05008061
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008062 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05008063 status = MBX_SUCCESS;
8064
James Smart2e0fef82007-06-17 19:56:36 -05008065 if (phba->link_state == LPFC_HBA_ERROR) {
8066 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05008067
8068 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008069 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8070 "(%d):0311 Mailbox command x%x cannot "
8071 "issue Data: x%x x%x\n",
8072 pmbox->vport ? pmbox->vport->vpi : 0,
8073 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008074 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05008075 }
8076
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008077 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05008078 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
8079 !(hc_copy & HC_MBINT_ENA)) {
8080 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8081 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04008082 "(%d):2528 Mailbox command x%x cannot "
8083 "issue Data: x%x x%x\n",
8084 pmbox->vport ? pmbox->vport->vpi : 0,
8085 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05008086 goto out_not_finished;
8087 }
James Smart92908312006-03-07 15:04:13 -05008088 }
8089
dea31012005-04-17 16:05:31 -05008090 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8091 /* Polling for a mbox command when another one is already active
8092 * is not allowed in SLI. Also, the driver must have established
8093 * SLI2 mode to queue and process multiple mbox commands.
8094 */
8095
8096 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05008097 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008098
8099 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008100 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8101 "(%d):2529 Mailbox command x%x "
8102 "cannot issue Data: x%x x%x\n",
8103 pmbox->vport ? pmbox->vport->vpi : 0,
8104 pmbox->u.mb.mbxCommand,
8105 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008106 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008107 }
8108
James Smart3772a992009-05-22 14:50:54 -04008109 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05008110 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008111 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008112 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8113 "(%d):2530 Mailbox command x%x "
8114 "cannot issue Data: x%x x%x\n",
8115 pmbox->vport ? pmbox->vport->vpi : 0,
8116 pmbox->u.mb.mbxCommand,
8117 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008118 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008119 }
8120
dea31012005-04-17 16:05:31 -05008121 /* Another mailbox command is still being processed, queue this
8122 * command to be processed later.
8123 */
8124 lpfc_mbox_put(phba, pmbox);
8125
8126 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05008127 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008128 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05008129 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008130 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07008131 mbx->mbxCommand,
8132 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008133 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008134
8135 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05008136 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008137
James Smart858c9f62007-06-17 19:56:39 -05008138 if (pmbox->vport) {
8139 lpfc_debugfs_disc_trc(pmbox->vport,
8140 LPFC_DISC_TRC_MBOX_VPORT,
8141 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008142 (uint32_t)mbx->mbxCommand,
8143 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008144 }
8145 else {
8146 lpfc_debugfs_disc_trc(phba->pport,
8147 LPFC_DISC_TRC_MBOX,
8148 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008149 (uint32_t)mbx->mbxCommand,
8150 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008151 }
8152
James Smart2e0fef82007-06-17 19:56:36 -05008153 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05008154 }
8155
dea31012005-04-17 16:05:31 -05008156 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8157
8158 /* If we are not polling, we MUST be in SLI2 mode */
8159 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04008160 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008161 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05008162 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008163 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008164 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008165 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8166 "(%d):2531 Mailbox command x%x "
8167 "cannot issue Data: x%x x%x\n",
8168 pmbox->vport ? pmbox->vport->vpi : 0,
8169 pmbox->u.mb.mbxCommand,
8170 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008171 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008172 }
8173 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04008174 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8175 1000);
8176 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05008177 }
8178
8179 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05008180 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008181 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05008182 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008183 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07008184 mbx->mbxCommand,
8185 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008186 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008187
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008188 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05008189 if (pmbox->vport) {
8190 lpfc_debugfs_disc_trc(pmbox->vport,
8191 LPFC_DISC_TRC_MBOX_VPORT,
8192 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008193 (uint32_t)mbx->mbxCommand,
8194 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008195 }
8196 else {
8197 lpfc_debugfs_disc_trc(phba->pport,
8198 LPFC_DISC_TRC_MBOX,
8199 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008200 (uint32_t)mbx->mbxCommand,
8201 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008202 }
8203 }
8204
dea31012005-04-17 16:05:31 -05008205 psli->slistat.mbox_cmd++;
8206 evtctr = psli->slistat.mbox_event;
8207
8208 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008209 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05008210
James Smart3772a992009-05-22 14:50:54 -04008211 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04008212 /* Populate mbox extension offset word. */
8213 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008214 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008215 = (uint8_t *)phba->mbox_ext
8216 - (uint8_t *)phba->mbox;
8217 }
8218
8219 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008220 if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
8221 lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
8222 (uint8_t *)phba->mbox_ext,
8223 pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008224 }
8225 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008226 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008227 } else {
James Smart7a470272010-03-15 11:25:20 -04008228 /* Populate mbox extension offset word. */
8229 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008230 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008231 = MAILBOX_HBA_EXT_OFFSET;
8232
8233 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008234 if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -04008235 lpfc_memcpy_to_slim(phba->MBslimaddr +
8236 MAILBOX_HBA_EXT_OFFSET,
James Smart3e1f0712018-11-29 16:09:29 -08008237 pmbox->ctx_buf, pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008238
James Smart895427b2017-02-12 13:52:30 -08008239 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008240 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08008241 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
8242 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008243
8244 /* First copy mbox command data to HBA SLIM, skip past first
8245 word */
8246 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008247 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05008248 MAILBOX_CMD_SIZE - sizeof (uint32_t));
8249
8250 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008251 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05008252 to_slim = phba->MBslimaddr;
8253 writel(ldata, to_slim);
8254 readl(to_slim); /* flush */
8255
James Smart895427b2017-02-12 13:52:30 -08008256 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008257 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04008258 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008259 }
8260
8261 wmb();
dea31012005-04-17 16:05:31 -05008262
8263 switch (flag) {
8264 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05008265 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05008266 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05008267 /* Interrupt board to do it */
8268 writel(CA_MBATT, phba->CAregaddr);
8269 readl(phba->CAregaddr); /* flush */
8270 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05008271 break;
8272
8273 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05008274 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05008275 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05008276 /* Interrupt board to do it */
8277 writel(CA_MBATT, phba->CAregaddr);
8278 readl(phba->CAregaddr); /* flush */
8279
James Smart3772a992009-05-22 14:50:54 -04008280 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008281 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04008282 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008283 word0 = le32_to_cpu(word0);
8284 } else {
8285 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05008286 if (lpfc_readl(phba->MBslimaddr, &word0)) {
8287 spin_unlock_irqrestore(&phba->hbalock,
8288 drvr_flag);
8289 goto out_not_finished;
8290 }
dea31012005-04-17 16:05:31 -05008291 }
8292
8293 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008294 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8295 spin_unlock_irqrestore(&phba->hbalock,
8296 drvr_flag);
8297 goto out_not_finished;
8298 }
James Smarta183a152011-10-10 21:32:43 -04008299 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8300 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05008301 i = 0;
dea31012005-04-17 16:05:31 -05008302 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05008303 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
8304 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05008305 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05008306 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05008307 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008308 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05008309 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04008310 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008311 }
8312
8313 /* Check if we took a mbox interrupt while we were
8314 polling */
8315 if (((word0 & OWN_CHIP) != OWN_CHIP)
8316 && (evtctr != psli->slistat.mbox_event))
8317 break;
8318
James Smart09372822008-01-11 01:52:54 -05008319 if (i++ > 10) {
8320 spin_unlock_irqrestore(&phba->hbalock,
8321 drvr_flag);
8322 msleep(1);
8323 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8324 }
dea31012005-04-17 16:05:31 -05008325
James Smart3772a992009-05-22 14:50:54 -04008326 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008327 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04008328 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008329 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008330 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05008331 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04008332 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05008333 /* Check real SLIM for any errors */
8334 slimword0 = readl(phba->MBslimaddr);
8335 slimmb = (MAILBOX_t *) & slimword0;
8336 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
8337 && slimmb->mbxStatus) {
8338 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04008339 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008340 word0 = slimword0;
8341 }
8342 }
8343 } else {
8344 /* First copy command data */
8345 word0 = readl(phba->MBslimaddr);
8346 }
8347 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008348 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8349 spin_unlock_irqrestore(&phba->hbalock,
8350 drvr_flag);
8351 goto out_not_finished;
8352 }
dea31012005-04-17 16:05:31 -05008353 }
8354
James Smart3772a992009-05-22 14:50:54 -04008355 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008356 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08008357 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
8358 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008359 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008360 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
James Smart7a470272010-03-15 11:25:20 -04008361 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -08008362 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008363 pmbox->out_ext_byte_len);
8364 }
dea31012005-04-17 16:05:31 -05008365 } else {
8366 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008367 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08008368 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008369 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008370 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
8371 lpfc_memcpy_from_slim(
8372 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008373 phba->MBslimaddr +
8374 MAILBOX_HBA_EXT_OFFSET,
8375 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05008376 }
8377 }
8378
8379 writel(HA_MBATT, phba->HAregaddr);
8380 readl(phba->HAregaddr); /* flush */
8381
8382 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008383 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05008384 }
8385
James Smart2e0fef82007-06-17 19:56:36 -05008386 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8387 return status;
James Smart58da1ff2008-04-07 10:15:56 -04008388
8389out_not_finished:
8390 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04008391 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04008392 lpfc_mbox_cmpl_put(phba, pmbox);
8393 }
8394 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008395}
8396
James Smarte59058c2008-08-24 21:49:00 -04008397/**
James Smartf1126682009-06-10 17:22:44 -04008398 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8399 * @phba: Pointer to HBA context object.
8400 *
8401 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8402 * the driver internal pending mailbox queue. It will then try to wait out the
8403 * possible outstanding mailbox command before return.
8404 *
8405 * Returns:
8406 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8407 * the outstanding mailbox command timed out.
8408 **/
8409static int
8410lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8411{
8412 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04008413 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04008414 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04008415
8416 /* Mark the asynchronous mailbox command posting as blocked */
8417 spin_lock_irq(&phba->hbalock);
8418 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04008419 /* Determine how long we might wait for the active mailbox
8420 * command to be gracefully completed by firmware.
8421 */
James Smarta183a152011-10-10 21:32:43 -04008422 if (phba->sli.mbox_active)
8423 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8424 phba->sli.mbox_active) *
8425 1000) + jiffies;
8426 spin_unlock_irq(&phba->hbalock);
8427
James Smarte8d3c3b2013-10-10 12:21:30 -04008428 /* Make sure the mailbox is really active */
8429 if (timeout)
8430 lpfc_sli4_process_missed_mbox_completions(phba);
8431
James Smartf1126682009-06-10 17:22:44 -04008432 /* Wait for the outstnading mailbox command to complete */
8433 while (phba->sli.mbox_active) {
8434 /* Check active mailbox complete status every 2ms */
8435 msleep(2);
8436 if (time_after(jiffies, timeout)) {
8437 /* Timeout, marked the outstanding cmd not complete */
8438 rc = 1;
8439 break;
8440 }
8441 }
8442
8443 /* Can not cleanly block async mailbox command, fails it */
8444 if (rc) {
8445 spin_lock_irq(&phba->hbalock);
8446 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8447 spin_unlock_irq(&phba->hbalock);
8448 }
8449 return rc;
8450}
8451
8452/**
8453 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8454 * @phba: Pointer to HBA context object.
8455 *
8456 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8457 * commands from the driver internal pending mailbox queue. It makes sure
8458 * that there is no outstanding mailbox command before resuming posting
8459 * asynchronous mailbox commands. If, for any reason, there is outstanding
8460 * mailbox command, it will try to wait it out before resuming asynchronous
8461 * mailbox command posting.
8462 **/
8463static void
8464lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8465{
8466 struct lpfc_sli *psli = &phba->sli;
8467
8468 spin_lock_irq(&phba->hbalock);
8469 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8470 /* Asynchronous mailbox posting is not blocked, do nothing */
8471 spin_unlock_irq(&phba->hbalock);
8472 return;
8473 }
8474
8475 /* Outstanding synchronous mailbox command is guaranteed to be done,
8476 * successful or timeout, after timing-out the outstanding mailbox
8477 * command shall always be removed, so just unblock posting async
8478 * mailbox command and resume
8479 */
8480 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8481 spin_unlock_irq(&phba->hbalock);
8482
8483 /* wake up worker thread to post asynchronlous mailbox command */
8484 lpfc_worker_wake_up(phba);
8485}
8486
8487/**
James Smart2d843ed2012-09-29 11:29:06 -04008488 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8489 * @phba: Pointer to HBA context object.
8490 * @mboxq: Pointer to mailbox object.
8491 *
8492 * The function waits for the bootstrap mailbox register ready bit from
8493 * port for twice the regular mailbox command timeout value.
8494 *
8495 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8496 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8497 **/
8498static int
8499lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8500{
8501 uint32_t db_ready;
8502 unsigned long timeout;
8503 struct lpfc_register bmbx_reg;
8504
8505 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8506 * 1000) + jiffies;
8507
8508 do {
8509 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8510 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8511 if (!db_ready)
James Smarte2ffe4d2019-03-12 16:30:15 -07008512 mdelay(2);
James Smart2d843ed2012-09-29 11:29:06 -04008513
8514 if (time_after(jiffies, timeout))
8515 return MBXERR_ERROR;
8516 } while (!db_ready);
8517
8518 return 0;
8519}
8520
8521/**
James Smartda0436e2009-05-22 14:51:39 -04008522 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8523 * @phba: Pointer to HBA context object.
8524 * @mboxq: Pointer to mailbox object.
8525 *
8526 * The function posts a mailbox to the port. The mailbox is expected
8527 * to be comletely filled in and ready for the port to operate on it.
8528 * This routine executes a synchronous completion operation on the
8529 * mailbox by polling for its completion.
8530 *
8531 * The caller must not be holding any locks when calling this routine.
8532 *
8533 * Returns:
8534 * MBX_SUCCESS - mailbox posted successfully
8535 * Any of the MBX error values.
8536 **/
8537static int
8538lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8539{
8540 int rc = MBX_SUCCESS;
8541 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04008542 uint32_t mcqe_status;
8543 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04008544 struct lpfc_sli *psli = &phba->sli;
8545 struct lpfc_mqe *mb = &mboxq->u.mqe;
8546 struct lpfc_bmbx_create *mbox_rgn;
8547 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04008548
8549 /*
8550 * Only one mailbox can be active to the bootstrap mailbox region
8551 * at a time and there is no queueing provided.
8552 */
8553 spin_lock_irqsave(&phba->hbalock, iflag);
8554 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8555 spin_unlock_irqrestore(&phba->hbalock, iflag);
8556 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008557 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008558 "cannot issue Data: x%x x%x\n",
8559 mboxq->vport ? mboxq->vport->vpi : 0,
8560 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008561 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8562 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008563 psli->sli_flag, MBX_POLL);
8564 return MBXERR_ERROR;
8565 }
8566 /* The server grabs the token and owns it until release */
8567 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8568 phba->sli.mbox_active = mboxq;
8569 spin_unlock_irqrestore(&phba->hbalock, iflag);
8570
James Smart2d843ed2012-09-29 11:29:06 -04008571 /* wait for bootstrap mbox register for readyness */
8572 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8573 if (rc)
8574 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008575 /*
8576 * Initialize the bootstrap memory region to avoid stale data areas
8577 * in the mailbox post. Then copy the caller's mailbox contents to
8578 * the bmbx mailbox region.
8579 */
8580 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8581 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
James Smart48f8fdb2018-05-04 20:37:51 -07008582 lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8583 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008584
8585 /* Post the high mailbox dma address to the port and wait for ready. */
8586 dma_address = &phba->sli4_hba.bmbx.dma_address;
8587 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8588
James Smart2d843ed2012-09-29 11:29:06 -04008589 /* wait for bootstrap mbox register for hi-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 /* Post the low mailbox dma address to the port. */
8595 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008596
James Smart2d843ed2012-09-29 11:29:06 -04008597 /* wait for bootstrap mbox register for low address write done */
8598 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8599 if (rc)
8600 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008601
8602 /*
8603 * Read the CQ to ensure the mailbox has completed.
8604 * If so, update the mailbox status so that the upper layers
8605 * can complete the request normally.
8606 */
James Smart48f8fdb2018-05-04 20:37:51 -07008607 lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8608 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008609 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
James Smart48f8fdb2018-05-04 20:37:51 -07008610 lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8611 sizeof(struct lpfc_mcqe));
James Smartda0436e2009-05-22 14:51:39 -04008612 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008613 /*
8614 * When the CQE status indicates a failure and the mailbox status
8615 * indicates success then copy the CQE status into the mailbox status
8616 * (and prefix it with x4000).
8617 */
James Smartda0436e2009-05-22 14:51:39 -04008618 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008619 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8620 bf_set(lpfc_mqe_status, mb,
8621 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008622 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008623 } else
8624 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008625
8626 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008627 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008628 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8629 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008630 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8631 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8632 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008633 bf_get(lpfc_mqe_status, mb),
8634 mb->un.mb_words[0], mb->un.mb_words[1],
8635 mb->un.mb_words[2], mb->un.mb_words[3],
8636 mb->un.mb_words[4], mb->un.mb_words[5],
8637 mb->un.mb_words[6], mb->un.mb_words[7],
8638 mb->un.mb_words[8], mb->un.mb_words[9],
8639 mb->un.mb_words[10], mb->un.mb_words[11],
8640 mb->un.mb_words[12], mboxq->mcqe.word0,
8641 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8642 mboxq->mcqe.trailer);
8643exit:
8644 /* We are holding the token, no needed for lock when release */
8645 spin_lock_irqsave(&phba->hbalock, iflag);
8646 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8647 phba->sli.mbox_active = NULL;
8648 spin_unlock_irqrestore(&phba->hbalock, iflag);
8649 return rc;
8650}
8651
8652/**
8653 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8654 * @phba: Pointer to HBA context object.
8655 * @pmbox: Pointer to mailbox object.
8656 * @flag: Flag indicating how the mailbox need to be processed.
8657 *
8658 * This function is called by discovery code and HBA management code to submit
8659 * a mailbox command to firmware with SLI-4 interface spec.
8660 *
8661 * Return codes the caller owns the mailbox command after the return of the
8662 * function.
8663 **/
8664static int
8665lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8666 uint32_t flag)
8667{
8668 struct lpfc_sli *psli = &phba->sli;
8669 unsigned long iflags;
8670 int rc;
8671
James Smartb76f2dc2011-07-22 18:37:42 -04008672 /* dump from issue mailbox command if setup */
8673 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8674
James Smart8fa38512009-07-19 10:01:03 -04008675 rc = lpfc_mbox_dev_check(phba);
8676 if (unlikely(rc)) {
8677 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008678 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008679 "cannot issue Data: x%x x%x\n",
8680 mboxq->vport ? mboxq->vport->vpi : 0,
8681 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008682 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8683 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008684 psli->sli_flag, flag);
8685 goto out_not_finished;
8686 }
8687
James Smartda0436e2009-05-22 14:51:39 -04008688 /* Detect polling mode and jump to a handler */
8689 if (!phba->sli4_hba.intr_enable) {
8690 if (flag == MBX_POLL)
8691 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8692 else
8693 rc = -EIO;
8694 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008695 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008696 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008697 "(x%x/x%x) failure: "
8698 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8699 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008700 mboxq->vport ? mboxq->vport->vpi : 0,
8701 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008702 lpfc_sli_config_mbox_subsys_get(phba,
8703 mboxq),
8704 lpfc_sli_config_mbox_opcode_get(phba,
8705 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008706 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8707 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8708 bf_get(lpfc_mcqe_ext_status,
8709 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008710 psli->sli_flag, flag);
8711 return rc;
8712 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008713 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8714 "(%d):2542 Try to issue mailbox command "
James Smart7365f6f2018-02-22 08:18:46 -08008715 "x%x (x%x/x%x) synchronously ahead of async "
James Smartf1126682009-06-10 17:22:44 -04008716 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008717 mboxq->vport ? mboxq->vport->vpi : 0,
8718 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008719 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8720 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008721 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008722 /* Try to block the asynchronous mailbox posting */
8723 rc = lpfc_sli4_async_mbox_block(phba);
8724 if (!rc) {
8725 /* Successfully blocked, now issue sync mbox cmd */
8726 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8727 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008728 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008729 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008730 "(%d):2597 Sync Mailbox command "
8731 "x%x (x%x/x%x) failure: "
8732 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8733 "Data: x%x x%x\n,",
8734 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008735 mboxq->u.mb.mbxCommand,
8736 lpfc_sli_config_mbox_subsys_get(phba,
8737 mboxq),
8738 lpfc_sli_config_mbox_opcode_get(phba,
8739 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008740 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8741 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8742 bf_get(lpfc_mcqe_ext_status,
8743 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008744 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008745 /* Unblock the async mailbox posting afterward */
8746 lpfc_sli4_async_mbox_unblock(phba);
8747 }
8748 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008749 }
8750
8751 /* Now, interrupt mode asynchrous mailbox command */
8752 rc = lpfc_mbox_cmd_check(phba, mboxq);
8753 if (rc) {
8754 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008755 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008756 "cannot issue Data: x%x x%x\n",
8757 mboxq->vport ? mboxq->vport->vpi : 0,
8758 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008759 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8760 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008761 psli->sli_flag, flag);
8762 goto out_not_finished;
8763 }
James Smartda0436e2009-05-22 14:51:39 -04008764
8765 /* Put the mailbox command to the driver internal FIFO */
8766 psli->slistat.mbox_busy++;
8767 spin_lock_irqsave(&phba->hbalock, iflags);
8768 lpfc_mbox_put(phba, mboxq);
8769 spin_unlock_irqrestore(&phba->hbalock, iflags);
8770 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8771 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008772 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008773 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8774 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008775 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8776 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008777 phba->pport->port_state,
8778 psli->sli_flag, MBX_NOWAIT);
8779 /* Wake up worker thread to transport mailbox command from head */
8780 lpfc_worker_wake_up(phba);
8781
8782 return MBX_BUSY;
8783
8784out_not_finished:
8785 return MBX_NOT_FINISHED;
8786}
8787
8788/**
8789 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8790 * @phba: Pointer to HBA context object.
8791 *
8792 * This function is called by worker thread to send a mailbox command to
8793 * SLI4 HBA firmware.
8794 *
8795 **/
8796int
8797lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8798{
8799 struct lpfc_sli *psli = &phba->sli;
8800 LPFC_MBOXQ_t *mboxq;
8801 int rc = MBX_SUCCESS;
8802 unsigned long iflags;
8803 struct lpfc_mqe *mqe;
8804 uint32_t mbx_cmnd;
8805
8806 /* Check interrupt mode before post async mailbox command */
8807 if (unlikely(!phba->sli4_hba.intr_enable))
8808 return MBX_NOT_FINISHED;
8809
8810 /* Check for mailbox command service token */
8811 spin_lock_irqsave(&phba->hbalock, iflags);
8812 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8813 spin_unlock_irqrestore(&phba->hbalock, iflags);
8814 return MBX_NOT_FINISHED;
8815 }
8816 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8817 spin_unlock_irqrestore(&phba->hbalock, iflags);
8818 return MBX_NOT_FINISHED;
8819 }
8820 if (unlikely(phba->sli.mbox_active)) {
8821 spin_unlock_irqrestore(&phba->hbalock, iflags);
8822 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8823 "0384 There is pending active mailbox cmd\n");
8824 return MBX_NOT_FINISHED;
8825 }
8826 /* Take the mailbox command service token */
8827 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8828
8829 /* Get the next mailbox command from head of queue */
8830 mboxq = lpfc_mbox_get(phba);
8831
8832 /* If no more mailbox command waiting for post, we're done */
8833 if (!mboxq) {
8834 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8835 spin_unlock_irqrestore(&phba->hbalock, iflags);
8836 return MBX_SUCCESS;
8837 }
8838 phba->sli.mbox_active = mboxq;
8839 spin_unlock_irqrestore(&phba->hbalock, iflags);
8840
8841 /* Check device readiness for posting mailbox command */
8842 rc = lpfc_mbox_dev_check(phba);
8843 if (unlikely(rc))
8844 /* Driver clean routine will clean up pending mailbox */
8845 goto out_not_finished;
8846
8847 /* Prepare the mbox command to be posted */
8848 mqe = &mboxq->u.mqe;
8849 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8850
8851 /* Start timer for the mbox_tmo and log some mailbox post messages */
8852 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008853 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008854
8855 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008856 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008857 "x%x x%x\n",
8858 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008859 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8860 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008861 phba->pport->port_state, psli->sli_flag);
8862
8863 if (mbx_cmnd != MBX_HEARTBEAT) {
8864 if (mboxq->vport) {
8865 lpfc_debugfs_disc_trc(mboxq->vport,
8866 LPFC_DISC_TRC_MBOX_VPORT,
8867 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8868 mbx_cmnd, mqe->un.mb_words[0],
8869 mqe->un.mb_words[1]);
8870 } else {
8871 lpfc_debugfs_disc_trc(phba->pport,
8872 LPFC_DISC_TRC_MBOX,
8873 "MBOX Send: cmd:x%x mb:x%x x%x",
8874 mbx_cmnd, mqe->un.mb_words[0],
8875 mqe->un.mb_words[1]);
8876 }
8877 }
8878 psli->slistat.mbox_cmd++;
8879
8880 /* Post the mailbox command to the port */
8881 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8882 if (rc != MBX_SUCCESS) {
8883 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008884 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008885 "cannot issue Data: x%x x%x\n",
8886 mboxq->vport ? mboxq->vport->vpi : 0,
8887 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008888 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8889 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008890 psli->sli_flag, MBX_NOWAIT);
8891 goto out_not_finished;
8892 }
8893
8894 return rc;
8895
8896out_not_finished:
8897 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008898 if (phba->sli.mbox_active) {
8899 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8900 __lpfc_mbox_cmpl_put(phba, mboxq);
8901 /* Release the token */
8902 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8903 phba->sli.mbox_active = NULL;
8904 }
James Smartda0436e2009-05-22 14:51:39 -04008905 spin_unlock_irqrestore(&phba->hbalock, iflags);
8906
8907 return MBX_NOT_FINISHED;
8908}
8909
8910/**
8911 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8912 * @phba: Pointer to HBA context object.
8913 * @pmbox: Pointer to mailbox object.
8914 * @flag: Flag indicating how the mailbox need to be processed.
8915 *
8916 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8917 * the API jump table function pointer from the lpfc_hba struct.
8918 *
8919 * Return codes the caller owns the mailbox command after the return of the
8920 * function.
8921 **/
8922int
8923lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8924{
8925 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8926}
8927
8928/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008929 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008930 * @phba: The hba struct for which this call is being executed.
8931 * @dev_grp: The HBA PCI-Device group number.
8932 *
8933 * This routine sets up the mbox interface API function jump table in @phba
8934 * struct.
8935 * Returns: 0 - success, -ENODEV - failure.
8936 **/
8937int
8938lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8939{
8940
8941 switch (dev_grp) {
8942 case LPFC_PCI_DEV_LP:
8943 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8944 phba->lpfc_sli_handle_slow_ring_event =
8945 lpfc_sli_handle_slow_ring_event_s3;
8946 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8947 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8948 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8949 break;
8950 case LPFC_PCI_DEV_OC:
8951 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8952 phba->lpfc_sli_handle_slow_ring_event =
8953 lpfc_sli_handle_slow_ring_event_s4;
8954 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8955 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8956 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8957 break;
8958 default:
8959 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8960 "1420 Invalid HBA PCI-device group: 0x%x\n",
8961 dev_grp);
8962 return -ENODEV;
8963 break;
8964 }
8965 return 0;
8966}
8967
8968/**
James Smart3621a712009-04-06 18:47:14 -04008969 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008970 * @phba: Pointer to HBA context object.
8971 * @pring: Pointer to driver SLI ring object.
8972 * @piocb: Pointer to address of newly added command iocb.
8973 *
8974 * This function is called with hbalock held to add a command
8975 * iocb to the txq when SLI layer cannot submit the command iocb
8976 * to the ring.
8977 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008978void
James Smart92d7f7b2007-06-17 19:56:38 -05008979__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008980 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008981{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008982 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008983 /* Insert the caller's iocb in the txq tail for later processing. */
8984 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008985}
8986
James Smarte59058c2008-08-24 21:49:00 -04008987/**
James Smart3621a712009-04-06 18:47:14 -04008988 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008989 * @phba: Pointer to HBA context object.
8990 * @pring: Pointer to driver SLI ring object.
8991 * @piocb: Pointer to address of newly added command iocb.
8992 *
8993 * This function is called with hbalock held before a new
8994 * iocb is submitted to the firmware. This function checks
8995 * txq to flush the iocbs in txq to Firmware before
8996 * submitting new iocbs to the Firmware.
8997 * If there are iocbs in the txq which need to be submitted
8998 * to firmware, lpfc_sli_next_iocb returns the first element
8999 * of the txq after dequeuing it from txq.
9000 * If there is no iocb in the txq then the function will return
9001 * *piocb and *piocb is set to NULL. Caller needs to check
9002 * *piocb to find if there are more commands in the txq.
9003 **/
dea31012005-04-17 16:05:31 -05009004static struct lpfc_iocbq *
9005lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009006 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05009007{
9008 struct lpfc_iocbq * nextiocb;
9009
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009010 lockdep_assert_held(&phba->hbalock);
9011
dea31012005-04-17 16:05:31 -05009012 nextiocb = lpfc_sli_ringtx_get(phba, pring);
9013 if (!nextiocb) {
9014 nextiocb = *piocb;
9015 *piocb = NULL;
9016 }
9017
9018 return nextiocb;
9019}
9020
James Smarte59058c2008-08-24 21:49:00 -04009021/**
James Smart3772a992009-05-22 14:50:54 -04009022 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009023 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04009024 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04009025 * @piocb: Pointer to command iocb.
9026 * @flag: Flag indicating if this command can be put into txq.
9027 *
James Smart3772a992009-05-22 14:50:54 -04009028 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
9029 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
9030 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
9031 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
9032 * this function allows only iocbs for posting buffers. This function finds
9033 * next available slot in the command ring and posts the command to the
9034 * available slot and writes the port attention register to request HBA start
9035 * processing new iocb. If there is no slot available in the ring and
9036 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
9037 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04009038 *
James Smart3772a992009-05-22 14:50:54 -04009039 * This function is called with hbalock held. The function will return success
9040 * after it successfully submit the iocb to firmware or after adding to the
9041 * txq.
James Smarte59058c2008-08-24 21:49:00 -04009042 **/
James Smart98c9ea52007-10-27 13:37:33 -04009043static int
James Smart3772a992009-05-22 14:50:54 -04009044__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05009045 struct lpfc_iocbq *piocb, uint32_t flag)
9046{
9047 struct lpfc_iocbq *nextiocb;
9048 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08009049 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05009050
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009051 lockdep_assert_held(&phba->hbalock);
9052
James Smart92d7f7b2007-06-17 19:56:38 -05009053 if (piocb->iocb_cmpl && (!piocb->vport) &&
9054 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
9055 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
9056 lpfc_printf_log(phba, KERN_ERR,
9057 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04009058 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05009059 piocb->iocb.ulpCommand);
9060 dump_stack();
9061 return IOCB_ERROR;
9062 }
9063
9064
Linas Vepstas8d63f372007-02-14 14:28:36 -06009065 /* If the PCI channel is in offline state, do not post iocbs. */
9066 if (unlikely(pci_channel_offline(phba->pcidev)))
9067 return IOCB_ERROR;
9068
James Smarta257bf92009-04-06 18:48:10 -04009069 /* If HBA has a deferred error attention, fail the iocb. */
9070 if (unlikely(phba->hba_flag & DEFER_ERATT))
9071 return IOCB_ERROR;
9072
dea31012005-04-17 16:05:31 -05009073 /*
9074 * We should never get an IOCB if we are in a < LINK_DOWN state
9075 */
James Smart2e0fef82007-06-17 19:56:36 -05009076 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05009077 return IOCB_ERROR;
9078
9079 /*
9080 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04009081 * outstanding event.
dea31012005-04-17 16:05:31 -05009082 */
James Smart0b727fe2007-10-27 13:37:25 -04009083 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05009084 goto iocb_busy;
9085
James Smart2e0fef82007-06-17 19:56:36 -05009086 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05009087 /*
James Smart2680eea2007-04-25 09:52:55 -04009088 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05009089 * can be issued if the link is not up.
9090 */
9091 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04009092 case CMD_GEN_REQUEST64_CR:
9093 case CMD_GEN_REQUEST64_CX:
9094 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
9095 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04009096 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04009097 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
9098 MENLO_TRANSPORT_TYPE))
9099
9100 goto iocb_busy;
9101 break;
dea31012005-04-17 16:05:31 -05009102 case CMD_QUE_RING_BUF_CN:
9103 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05009104 /*
9105 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
9106 * completion, iocb_cmpl MUST be 0.
9107 */
9108 if (piocb->iocb_cmpl)
9109 piocb->iocb_cmpl = NULL;
9110 /*FALLTHROUGH*/
9111 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04009112 case CMD_CLOSE_XRI_CN:
9113 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05009114 break;
9115 default:
9116 goto iocb_busy;
9117 }
9118
9119 /*
9120 * For FCP commands, we must be in a state where we can process link
9121 * attention events.
9122 */
James Smart895427b2017-02-12 13:52:30 -08009123 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05009124 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05009125 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05009126 }
dea31012005-04-17 16:05:31 -05009127
dea31012005-04-17 16:05:31 -05009128 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
9129 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
9130 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
9131
9132 if (iocb)
9133 lpfc_sli_update_ring(phba, pring);
9134 else
9135 lpfc_sli_update_full_ring(phba, pring);
9136
9137 if (!piocb)
9138 return IOCB_SUCCESS;
9139
9140 goto out_busy;
9141
9142 iocb_busy:
9143 pring->stats.iocb_cmd_delay++;
9144
9145 out_busy:
9146
9147 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05009148 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05009149 return IOCB_SUCCESS;
9150 }
9151
9152 return IOCB_BUSY;
9153}
9154
James Smart3772a992009-05-22 14:50:54 -04009155/**
James Smart4f774512009-05-22 14:52:35 -04009156 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
9157 * @phba: Pointer to HBA context object.
9158 * @piocb: Pointer to command iocb.
9159 * @sglq: Pointer to the scatter gather queue object.
9160 *
9161 * This routine converts the bpl or bde that is in the IOCB
9162 * to a sgl list for the sli4 hardware. The physical address
9163 * of the bpl/bde is converted back to a virtual address.
9164 * If the IOCB contains a BPL then the list of BDE's is
9165 * converted to sli4_sge's. If the IOCB contains a single
9166 * BDE then it is converted to a single sli_sge.
9167 * The IOCB is still in cpu endianess so the contents of
9168 * the bpl can be used without byte swapping.
9169 *
9170 * Returns valid XRI = Success, NO_XRI = Failure.
9171**/
9172static uint16_t
9173lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
9174 struct lpfc_sglq *sglq)
9175{
9176 uint16_t xritag = NO_XRI;
9177 struct ulp_bde64 *bpl = NULL;
9178 struct ulp_bde64 bde;
9179 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05009180 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04009181 IOCB_t *icmd;
9182 int numBdes = 0;
9183 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05009184 uint32_t offset = 0; /* accumulated offset in the sg request list */
9185 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04009186
9187 if (!piocbq || !sglq)
9188 return xritag;
9189
9190 sgl = (struct sli4_sge *)sglq->sgl;
9191 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05009192 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
9193 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009194 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9195 numBdes = icmd->un.genreq64.bdl.bdeSize /
9196 sizeof(struct ulp_bde64);
9197 /* The addrHigh and addrLow fields within the IOCB
9198 * have not been byteswapped yet so there is no
9199 * need to swap them back.
9200 */
James Smart1b511972011-12-13 13:23:09 -05009201 if (piocbq->context3)
9202 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
9203 else
9204 return xritag;
James Smart4f774512009-05-22 14:52:35 -04009205
James Smart1b511972011-12-13 13:23:09 -05009206 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04009207 if (!bpl)
9208 return xritag;
9209
9210 for (i = 0; i < numBdes; i++) {
9211 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05009212 sgl->addr_hi = bpl->addrHigh;
9213 sgl->addr_lo = bpl->addrLow;
9214
James Smart05580562011-05-24 11:40:48 -04009215 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009216 if ((i+1) == numBdes)
9217 bf_set(lpfc_sli4_sge_last, sgl, 1);
9218 else
9219 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05009220 /* swap the size field back to the cpu so we
9221 * can assign it to the sgl.
9222 */
9223 bde.tus.w = le32_to_cpu(bpl->tus.w);
9224 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05009225 /* The offsets in the sgl need to be accumulated
9226 * separately for the request and reply lists.
9227 * The request is always first, the reply follows.
9228 */
9229 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
9230 /* add up the reply sg entries */
9231 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
9232 inbound++;
9233 /* first inbound? reset the offset */
9234 if (inbound == 1)
9235 offset = 0;
9236 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04009237 bf_set(lpfc_sli4_sge_type, sgl,
9238 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05009239 offset += bde.tus.f.bdeSize;
9240 }
James Smart546fc852011-03-11 16:06:29 -05009241 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009242 bpl++;
9243 sgl++;
9244 }
9245 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
9246 /* The addrHigh and addrLow fields of the BDE have not
9247 * been byteswapped yet so they need to be swapped
9248 * before putting them in the sgl.
9249 */
9250 sgl->addr_hi =
9251 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
9252 sgl->addr_lo =
9253 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04009254 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009255 bf_set(lpfc_sli4_sge_last, sgl, 1);
9256 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05009257 sgl->sge_len =
9258 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04009259 }
9260 return sglq->sli4_xritag;
9261}
9262
9263/**
James Smart4f774512009-05-22 14:52:35 -04009264 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
9265 * @phba: Pointer to HBA context object.
9266 * @piocb: Pointer to command iocb.
9267 * @wqe: Pointer to the work queue entry.
9268 *
9269 * This routine converts the iocb command to its Work Queue Entry
9270 * equivalent. The wqe pointer should not have any fields set when
9271 * this routine is called because it will memcpy over them.
9272 * This routine does not set the CQ_ID or the WQEC bits in the
9273 * wqe.
9274 *
9275 * Returns: 0 = Success, IOCB_ERROR = Failure.
9276 **/
9277static int
9278lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
James Smart205e8242018-03-05 12:04:03 -08009279 union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -04009280{
James Smart5ffc2662009-11-18 15:39:44 -05009281 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04009282 uint8_t ct = 0;
9283 uint32_t fip;
9284 uint32_t abort_tag;
9285 uint8_t command_type = ELS_COMMAND_NON_FIP;
9286 uint8_t cmnd;
9287 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04009288 uint16_t abrt_iotag;
9289 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04009290 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04009291 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05009292 int numBdes, i;
9293 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04009294 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05009295 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05009296 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04009297
James Smart45ed1192009-10-02 15:17:02 -04009298 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04009299 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04009300 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04009301 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05009302 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04009303 command_type = ELS_COMMAND_FIP;
9304 else
9305 command_type = ELS_COMMAND_NON_FIP;
9306
James Smartb5c53952016-03-31 14:12:30 -07009307 if (phba->fcp_embed_io)
9308 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04009309 /* Some of the fields are in the right position already */
9310 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smarte62245d2019-08-14 16:57:08 -07009311 /* The ct field has moved so reset */
9312 wqe->generic.wqe_com.word7 = 0;
9313 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07009314
9315 abort_tag = (uint32_t) iocbq->iotag;
9316 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009317 /* words0-2 bpl convert bde */
9318 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05009319 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9320 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04009321 bpl = (struct ulp_bde64 *)
9322 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
9323 if (!bpl)
9324 return IOCB_ERROR;
9325
9326 /* Should already be byte swapped. */
9327 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
9328 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
9329 /* swap the size field back to the cpu so we
9330 * can assign it to the sgl.
9331 */
9332 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05009333 xmit_len = wqe->generic.bde.tus.f.bdeSize;
9334 total_len = 0;
9335 for (i = 0; i < numBdes; i++) {
9336 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
9337 total_len += bde.tus.f.bdeSize;
9338 }
James Smart4f774512009-05-22 14:52:35 -04009339 } else
James Smart5ffc2662009-11-18 15:39:44 -05009340 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04009341
9342 iocbq->iocb.ulpIoTag = iocbq->iotag;
9343 cmnd = iocbq->iocb.ulpCommand;
9344
9345 switch (iocbq->iocb.ulpCommand) {
9346 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04009347 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
9348 ndlp = iocbq->context_un.ndlp;
9349 else
9350 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009351 if (!iocbq->iocb.ulpLe) {
9352 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9353 "2007 Only Limited Edition cmd Format"
9354 " supported 0x%x\n",
9355 iocbq->iocb.ulpCommand);
9356 return IOCB_ERROR;
9357 }
James Smartff78d8f2011-12-13 13:21:35 -05009358
James Smart5ffc2662009-11-18 15:39:44 -05009359 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009360 /* Els_reguest64 has a TMO */
9361 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
9362 iocbq->iocb.ulpTimeout);
9363 /* Need a VF for word 4 set the vf bit*/
9364 bf_set(els_req64_vf, &wqe->els_req, 0);
9365 /* And a VFID for word 12 */
9366 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04009367 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04009368 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9369 iocbq->iocb.ulpContext);
9370 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9371 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04009372 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05009373 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05009374 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9375 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05009376 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9377 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05009378 if_type = bf_get(lpfc_sli_intf_if_type,
9379 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009380 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05009381 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05009382 *pcmd == ELS_CMD_SCR ||
James Smartf60cb932019-05-14 14:58:05 -07009383 *pcmd == ELS_CMD_RSCN_XMT ||
James Smart6b5151f2012-01-18 16:24:06 -05009384 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05009385 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05009386 *pcmd == ELS_CMD_PLOGI)) {
9387 bf_set(els_req64_sp, &wqe->els_req, 1);
9388 bf_set(els_req64_sid, &wqe->els_req,
9389 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009390 if ((*pcmd == ELS_CMD_FLOGI) &&
9391 !(phba->fc_topology ==
9392 LPFC_TOPOLOGY_LOOP))
9393 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05009394 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9395 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04009396 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05009397 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05009398 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9399 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9400 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9401 }
James Smartc8685952009-11-18 15:39:16 -05009402 }
James Smart6d368e52011-05-24 11:44:12 -04009403 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9404 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009405 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9406 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9407 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9408 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9409 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9410 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009411 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04009412 break;
James Smart5ffc2662009-11-18 15:39:44 -05009413 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04009414 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9415 iocbq->iocb.un.ulpWord[3]);
9416 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009417 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05009418 /* The entire sequence is transmitted for this IOCB */
9419 xmit_len = total_len;
9420 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05009421 if (phba->link_flag & LS_LOOPBACK_MODE)
9422 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -06009423 /* fall through */
James Smart4f774512009-05-22 14:52:35 -04009424 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04009425 /* word3 iocb=io_tag32 wqe=reserved */
9426 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04009427 /* word4 relative_offset memcpy */
9428 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009429 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9430 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9431 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9432 LPFC_WQE_IOD_WRITE);
9433 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9434 LPFC_WQE_LENLOC_WORD12);
9435 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05009436 wqe->xmit_sequence.xmit_len = xmit_len;
9437 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009438 break;
James Smart4f774512009-05-22 14:52:35 -04009439 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04009440 /* word3 iocb=iotag32 wqe=seq_payload_len */
9441 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009442 /* word4 iocb=rsvd wqe=rsvd */
9443 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9444 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04009445 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009446 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04009447 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9448 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9449 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9450 LPFC_WQE_LENLOC_WORD3);
9451 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04009452 break;
James Smart4f774512009-05-22 14:52:35 -04009453 case CMD_FCP_IWRITE64_CR:
9454 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04009455 /* word3 iocb=iotag wqe=payload_offset_len */
9456 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009457 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9458 xmit_len + sizeof(struct fcp_rsp));
9459 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9460 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009461 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9462 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9463 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9464 iocbq->iocb.ulpFCP2Rcvy);
9465 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9466 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009467 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9468 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9469 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009470 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009471 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009472 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9473 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009474 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9475 if (iocbq->priority) {
9476 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9477 (iocbq->priority << 1));
9478 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009479 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9480 (phba->cfg_XLanePriority << 1));
9481 }
9482 }
James Smartb5c53952016-03-31 14:12:30 -07009483 /* Note, word 10 is already initialized to 0 */
9484
James Smart414abe02018-06-26 08:24:26 -07009485 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9486 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009487 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9488 else
9489 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9490
James Smartb5c53952016-03-31 14:12:30 -07009491 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009492 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009493 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009494 struct fcp_cmnd *fcp_cmnd;
9495 uint32_t *ptr;
9496
9497 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009498
9499 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009500 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009501 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9502
9503 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009504 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009505 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009506 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9507 wqe->generic.bde.addrHigh = 0;
9508 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009509
James Smart205e8242018-03-05 12:04:03 -08009510 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9511 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009512
9513 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009514 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009515 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9516 }
James Smart7851fe22011-07-22 18:36:52 -04009517 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04009518 case CMD_FCP_IREAD64_CR:
9519 /* word3 iocb=iotag wqe=payload_offset_len */
9520 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009521 bf_set(payload_offset_len, &wqe->fcp_iread,
9522 xmit_len + sizeof(struct fcp_rsp));
9523 bf_set(cmd_buff_len, &wqe->fcp_iread,
9524 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009525 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9526 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9527 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9528 iocbq->iocb.ulpFCP2Rcvy);
9529 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04009530 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009531 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9532 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9533 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009534 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009535 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009536 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9537 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009538 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9539 if (iocbq->priority) {
9540 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9541 (iocbq->priority << 1));
9542 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009543 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9544 (phba->cfg_XLanePriority << 1));
9545 }
9546 }
James Smartb5c53952016-03-31 14:12:30 -07009547 /* Note, word 10 is already initialized to 0 */
9548
James Smart414abe02018-06-26 08:24:26 -07009549 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9550 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009551 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9552 else
9553 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9554
James Smartb5c53952016-03-31 14:12:30 -07009555 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009556 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009557 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009558 struct fcp_cmnd *fcp_cmnd;
9559 uint32_t *ptr;
9560
9561 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009562
9563 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009564 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009565 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9566
9567 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009568 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009569 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009570 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9571 wqe->generic.bde.addrHigh = 0;
9572 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009573
James Smart205e8242018-03-05 12:04:03 -08009574 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9575 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009576
9577 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009578 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009579 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9580 }
James Smart7851fe22011-07-22 18:36:52 -04009581 break;
James Smartf1126682009-06-10 17:22:44 -04009582 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04009583 /* word3 iocb=iotag wqe=payload_offset_len */
9584 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9585 bf_set(payload_offset_len, &wqe->fcp_icmd,
9586 xmit_len + sizeof(struct fcp_rsp));
9587 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9588 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009589 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04009590 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04009591 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009592 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9593 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9594 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9595 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9596 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04009597 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9598 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009599 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9600 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009601 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9602 if (iocbq->priority) {
9603 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9604 (iocbq->priority << 1));
9605 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009606 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9607 (phba->cfg_XLanePriority << 1));
9608 }
9609 }
James Smartb5c53952016-03-31 14:12:30 -07009610 /* Note, word 10 is already initialized to 0 */
9611
9612 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009613 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009614 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009615 struct fcp_cmnd *fcp_cmnd;
9616 uint32_t *ptr;
9617
9618 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009619
9620 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009621 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009622 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9623
9624 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009625 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009626 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009627 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9628 wqe->generic.bde.addrHigh = 0;
9629 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009630
James Smart205e8242018-03-05 12:04:03 -08009631 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9632 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009633
9634 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009635 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009636 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9637 }
James Smart7851fe22011-07-22 18:36:52 -04009638 break;
James Smart4f774512009-05-22 14:52:35 -04009639 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009640 /* For this command calculate the xmit length of the
9641 * request bde.
9642 */
9643 xmit_len = 0;
9644 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9645 sizeof(struct ulp_bde64);
9646 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009647 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009648 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9649 break;
James Smart63e801c2010-11-20 23:14:19 -05009650 xmit_len += bde.tus.f.bdeSize;
9651 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009652 /* word3 iocb=IO_TAG wqe=request_payload_len */
9653 wqe->gen_req.request_payload_len = xmit_len;
9654 /* word4 iocb=parameter wqe=relative_offset memcpy */
9655 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009656 /* word6 context tag copied in memcpy */
9657 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9658 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9659 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9660 "2015 Invalid CT %x command 0x%x\n",
9661 ct, iocbq->iocb.ulpCommand);
9662 return IOCB_ERROR;
9663 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009664 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9665 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9666 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9667 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9668 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9669 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9670 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9671 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009672 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009673 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009674 break;
James Smart4f774512009-05-22 14:52:35 -04009675 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009676 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009677 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009678 /* word3 iocb=iotag32 wqe=response_payload_len */
9679 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009680 /* word4 */
9681 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009682 /* word5 iocb=rsvd wge=did */
9683 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009684 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9685
9686 if_type = bf_get(lpfc_sli_intf_if_type,
9687 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009688 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -04009689 if (iocbq->vport->fc_flag & FC_PT2PT) {
9690 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9691 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9692 iocbq->vport->fc_myDID);
9693 if (iocbq->vport->fc_myDID == Fabric_DID) {
9694 bf_set(wqe_els_did,
9695 &wqe->xmit_els_rsp.wqe_dest, 0);
9696 }
9697 }
9698 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009699 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9700 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9701 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9702 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009703 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009704 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009705 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009706 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009707 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9708 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9709 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9710 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9711 LPFC_WQE_LENLOC_WORD3);
9712 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009713 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9714 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009715 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9716 iocbq->context2)->virt);
9717 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009718 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9719 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009720 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009721 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9722 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009723 phba->vpi_ids[phba->pport->vpi]);
9724 }
James Smart4f774512009-05-22 14:52:35 -04009725 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009726 break;
James Smart4f774512009-05-22 14:52:35 -04009727 case CMD_CLOSE_XRI_CN:
9728 case CMD_ABORT_XRI_CN:
9729 case CMD_ABORT_XRI_CX:
9730 /* words 0-2 memcpy should be 0 rserved */
9731 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009732 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9733 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9734 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9735 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9736 } else
9737 fip = 0;
9738
9739 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009740 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009741 * The link is down, or the command was ELS_FIP
9742 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009743 * on the wire.
9744 */
9745 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9746 else
9747 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9748 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009749 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9750 wqe->abort_cmd.rsrvd5 = 0;
9751 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009752 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9753 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009754 /*
9755 * The abort handler will send us CMD_ABORT_XRI_CN or
9756 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9757 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009758 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9759 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9760 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9761 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009762 cmnd = CMD_ABORT_XRI_CX;
9763 command_type = OTHER_COMMAND;
9764 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009765 break;
James Smart6669f9b2009-10-02 15:16:45 -04009766 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009767 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009768 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009769 * we re-construct this WQE here based on information in
9770 * iocbq from scratch.
9771 */
James Smartd9f492a2019-08-14 16:57:04 -07009772 memset(wqe, 0, sizeof(*wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009773 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009774 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009775 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9776 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009777 LPFC_ABTS_UNSOL_INT) {
9778 /* ABTS sent by initiator to CT exchange, the
9779 * RX_ID field will be filled with the newly
9780 * allocated responder XRI.
9781 */
9782 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9783 iocbq->sli4_xritag);
9784 } else {
9785 /* ABTS sent by responder to CT exchange, the
9786 * RX_ID field will be filled with the responder
9787 * RX_ID from ABTS.
9788 */
9789 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009790 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009791 }
James Smart6669f9b2009-10-02 15:16:45 -04009792 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9793 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009794
9795 /* Use CT=VPI */
9796 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9797 ndlp->nlp_DID);
9798 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9799 iocbq->iocb.ulpContext);
9800 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009801 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009802 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009803 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9804 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9805 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009806 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9807 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009808 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9809 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9810 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9811 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9812 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9813 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9814 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9815 }
9816
James Smart7851fe22011-07-22 18:36:52 -04009817 break;
James Smartae9e28f2017-05-15 15:20:51 -07009818 case CMD_SEND_FRAME:
James Smarte62245d2019-08-14 16:57:08 -07009819 bf_set(wqe_cmnd, &wqe->generic.wqe_com, CMD_SEND_FRAME);
9820 bf_set(wqe_sof, &wqe->generic.wqe_com, 0x2E); /* SOF byte */
9821 bf_set(wqe_eof, &wqe->generic.wqe_com, 0x41); /* EOF byte */
9822 bf_set(wqe_lenloc, &wqe->generic.wqe_com, 1);
9823 bf_set(wqe_xbl, &wqe->generic.wqe_com, 1);
9824 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
9825 bf_set(wqe_xc, &wqe->generic.wqe_com, 1);
9826 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, 0xA);
9827 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smartae9e28f2017-05-15 15:20:51 -07009828 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9829 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9830 return 0;
James Smart4f774512009-05-22 14:52:35 -04009831 case CMD_XRI_ABORTED_CX:
9832 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009833 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9834 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9835 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9836 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9837 default:
9838 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9839 "2014 Invalid command 0x%x\n",
9840 iocbq->iocb.ulpCommand);
9841 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009842 break;
James Smart4f774512009-05-22 14:52:35 -04009843 }
James Smart6d368e52011-05-24 11:44:12 -04009844
James Smart8012cc32012-10-31 14:44:49 -04009845 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9846 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9847 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9848 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9849 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9850 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9851 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9852 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009853 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9854 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9855 wqe->generic.wqe_com.abort_tag = abort_tag;
9856 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9857 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9858 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9859 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009860 return 0;
9861}
9862
9863/**
9864 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9865 * @phba: Pointer to HBA context object.
9866 * @ring_number: SLI ring number to issue iocb on.
9867 * @piocb: Pointer to command iocb.
9868 * @flag: Flag indicating if this command can be put into txq.
9869 *
9870 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9871 * an iocb command to an HBA with SLI-4 interface spec.
9872 *
9873 * This function is called with hbalock held. The function will return success
9874 * after it successfully submit the iocb to firmware or after adding to the
9875 * txq.
9876 **/
9877static int
9878__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9879 struct lpfc_iocbq *piocb, uint32_t flag)
9880{
9881 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -08009882 union lpfc_wqe128 wqe;
James Smart1ba981f2014-02-20 09:56:45 -05009883 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009884 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009885
James Smart895427b2017-02-12 13:52:30 -08009886 /* Get the WQ */
9887 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9888 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc00f62e2019-08-14 16:57:11 -07009889 wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;
James Smart895427b2017-02-12 13:52:30 -08009890 } else {
9891 wq = phba->sli4_hba.els_wq;
9892 }
9893
9894 /* Get corresponding ring */
9895 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009896
James Smartb5c53952016-03-31 14:12:30 -07009897 /*
9898 * The WQE can be either 64 or 128 bytes,
James Smartb5c53952016-03-31 14:12:30 -07009899 */
James Smartb5c53952016-03-31 14:12:30 -07009900
James Smartcda7fa182019-03-04 15:15:43 -08009901 lockdep_assert_held(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -08009902
James Smart4f774512009-05-22 14:52:35 -04009903 if (piocb->sli4_xritag == NO_XRI) {
9904 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009905 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009906 sglq = NULL;
9907 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009908 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009909 if (!(flag & SLI_IOCB_RET_IOCB)) {
9910 __lpfc_sli_ringtx_put(phba,
9911 pring, piocb);
9912 return IOCB_SUCCESS;
9913 } else {
9914 return IOCB_BUSY;
9915 }
9916 } else {
James Smart895427b2017-02-12 13:52:30 -08009917 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009918 if (!sglq) {
9919 if (!(flag & SLI_IOCB_RET_IOCB)) {
9920 __lpfc_sli_ringtx_put(phba,
9921 pring,
9922 piocb);
9923 return IOCB_SUCCESS;
9924 } else
9925 return IOCB_BUSY;
9926 }
9927 }
James Smart4f774512009-05-22 14:52:35 -04009928 }
James Smart2ea259e2017-02-12 13:52:27 -08009929 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009930 /* These IO's already have an XRI and a mapped sgl. */
9931 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009932 else {
James Smart6d368e52011-05-24 11:44:12 -04009933 /*
9934 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009935 * sglq is on the active list
9936 */
James Smartedccdc12013-01-03 15:43:45 -05009937 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009938 if (!sglq)
9939 return IOCB_ERROR;
9940 }
9941
9942 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009943 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009944 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009945 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009946 return IOCB_ERROR;
9947 }
9948
James Smart205e8242018-03-05 12:04:03 -08009949 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
James Smart4f774512009-05-22 14:52:35 -04009950 return IOCB_ERROR;
9951
James Smart205e8242018-03-05 12:04:03 -08009952 if (lpfc_sli4_wq_put(wq, &wqe))
James Smart895427b2017-02-12 13:52:30 -08009953 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009954 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9955
9956 return 0;
9957}
9958
9959/**
James Smart3772a992009-05-22 14:50:54 -04009960 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9961 *
9962 * This routine wraps the actual lockless version for issusing IOCB function
9963 * pointer from the lpfc_hba struct.
9964 *
9965 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009966 * IOCB_ERROR - Error
9967 * IOCB_SUCCESS - Success
9968 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009969 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009970int
James Smart3772a992009-05-22 14:50:54 -04009971__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9972 struct lpfc_iocbq *piocb, uint32_t flag)
9973{
9974 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9975}
9976
9977/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009978 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009979 * @phba: The hba struct for which this call is being executed.
9980 * @dev_grp: The HBA PCI-Device group number.
9981 *
9982 * This routine sets up the SLI interface API function jump table in @phba
9983 * struct.
9984 * Returns: 0 - success, -ENODEV - failure.
9985 **/
9986int
9987lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9988{
9989
9990 switch (dev_grp) {
9991 case LPFC_PCI_DEV_LP:
9992 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9993 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9994 break;
James Smart4f774512009-05-22 14:52:35 -04009995 case LPFC_PCI_DEV_OC:
9996 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9997 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9998 break;
James Smart3772a992009-05-22 14:50:54 -04009999 default:
10000 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10001 "1419 Invalid HBA PCI-device group: 0x%x\n",
10002 dev_grp);
10003 return -ENODEV;
10004 break;
10005 }
10006 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
10007 return 0;
10008}
James Smart92d7f7b2007-06-17 19:56:38 -050010009
James Smarta1efe162015-05-21 13:55:20 -040010010/**
James Smart895427b2017-02-12 13:52:30 -080010011 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -040010012 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -040010013 * @piocb: Pointer to command iocb.
10014 *
James Smart895427b2017-02-12 13:52:30 -080010015 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
10016 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -040010017 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -080010018 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -040010019 */
James Smart895427b2017-02-12 13:52:30 -080010020struct lpfc_sli_ring *
10021lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -040010022{
James Smartc4908502019-01-28 11:14:28 -080010023 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -080010024
James Smart895427b2017-02-12 13:52:30 -080010025 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smartcdb42be2019-01-28 11:14:21 -080010026 if (unlikely(!phba->sli4_hba.hdwq))
James Smart7370d102019-01-28 11:14:20 -080010027 return NULL;
10028 /*
10029 * for abort iocb hba_wqidx should already
10030 * be setup based on what work queue we used.
10031 */
10032 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc4908502019-01-28 11:14:28 -080010033 lpfc_cmd = (struct lpfc_io_buf *)piocb->context1;
James Smart1fbf9742019-01-28 11:14:26 -080010034 piocb->hba_wqidx = lpfc_cmd->hdwq_no;
James Smart9bd2bff52014-09-03 12:57:30 -040010035 }
James Smartc00f62e2019-08-14 16:57:11 -070010036 return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010037 } else {
10038 if (unlikely(!phba->sli4_hba.els_wq))
10039 return NULL;
10040 piocb->hba_wqidx = 0;
10041 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -040010042 }
James Smart9bd2bff52014-09-03 12:57:30 -040010043}
10044
James Smarte59058c2008-08-24 21:49:00 -040010045/**
James Smart3621a712009-04-06 18:47:14 -040010046 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -040010047 * @phba: Pointer to HBA context object.
10048 * @pring: Pointer to driver SLI ring object.
10049 * @piocb: Pointer to command iocb.
10050 * @flag: Flag indicating if this command can be put into txq.
10051 *
10052 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
10053 * function. This function gets the hbalock and calls
10054 * __lpfc_sli_issue_iocb function and will return the error returned
10055 * by __lpfc_sli_issue_iocb function. This wrapper is used by
10056 * functions which do not hold hbalock.
10057 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010058int
James Smart3772a992009-05-22 14:50:54 -040010059lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -050010060 struct lpfc_iocbq *piocb, uint32_t flag)
10061{
James Smart2a76a282012-08-03 12:35:54 -040010062 struct lpfc_sli_ring *pring;
James Smart92d7f7b2007-06-17 19:56:38 -050010063 unsigned long iflags;
James Smart6a828b02019-01-28 11:14:31 -080010064 int rc;
James Smart92d7f7b2007-06-17 19:56:38 -050010065
James Smart7e56aa22012-08-03 12:35:34 -040010066 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010067 pring = lpfc_sli4_calc_ring(phba, piocb);
10068 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010069 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -040010070
James Smart9bd2bff52014-09-03 12:57:30 -040010071 spin_lock_irqsave(&pring->ring_lock, iflags);
10072 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10073 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart7e56aa22012-08-03 12:35:34 -040010074 } else {
10075 /* For now, SLI2/3 will still use hbalock */
10076 spin_lock_irqsave(&phba->hbalock, iflags);
10077 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10078 spin_unlock_irqrestore(&phba->hbalock, iflags);
10079 }
James Smart92d7f7b2007-06-17 19:56:38 -050010080 return rc;
10081}
10082
James Smarte59058c2008-08-24 21:49:00 -040010083/**
James Smart3621a712009-04-06 18:47:14 -040010084 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010085 * @phba: Pointer to HBA context object.
10086 *
10087 * This function is called while driver attaches with the
10088 * HBA to setup the extra ring. The extra ring is used
10089 * only when driver needs to support target mode functionality
10090 * or IP over FC functionalities.
10091 *
James Smart895427b2017-02-12 13:52:30 -080010092 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -040010093 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010094static int
10095lpfc_extra_ring_setup( struct lpfc_hba *phba)
10096{
10097 struct lpfc_sli *psli;
10098 struct lpfc_sli_ring *pring;
10099
10100 psli = &phba->sli;
10101
10102 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -050010103
10104 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -080010105 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -040010106 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10107 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10108 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10109 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010110
James Smarta4bc3372006-12-02 13:34:16 -050010111 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -080010112 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -050010113
James Smart7e56aa22012-08-03 12:35:34 -040010114 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10115 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10116 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10117 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010118
10119 /* Setup default profile for this ring */
10120 pring->iotag_max = 4096;
10121 pring->num_mask = 1;
10122 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -050010123 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
10124 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010125 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
10126 return 0;
10127}
10128
James Smartcb69f7d2011-12-13 13:21:57 -050010129/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
10130 * @phba: Pointer to HBA context object.
10131 * @iocbq: Pointer to iocb object.
10132 *
10133 * The async_event handler calls this routine when it receives
10134 * an ASYNC_STATUS_CN event from the port. The port generates
10135 * this event when an Abort Sequence request to an rport fails
10136 * twice in succession. The abort could be originated by the
10137 * driver or by the port. The ABTS could have been for an ELS
10138 * or FCP IO. The port only generates this event when an ABTS
10139 * fails to complete after one retry.
10140 */
10141static void
10142lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
10143 struct lpfc_iocbq *iocbq)
10144{
10145 struct lpfc_nodelist *ndlp = NULL;
10146 uint16_t rpi = 0, vpi = 0;
10147 struct lpfc_vport *vport = NULL;
10148
10149 /* The rpi in the ulpContext is vport-sensitive. */
10150 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
10151 rpi = iocbq->iocb.ulpContext;
10152
10153 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10154 "3092 Port generated ABTS async event "
10155 "on vpi %d rpi %d status 0x%x\n",
10156 vpi, rpi, iocbq->iocb.ulpStatus);
10157
10158 vport = lpfc_find_vport_by_vpid(phba, vpi);
10159 if (!vport)
10160 goto err_exit;
10161 ndlp = lpfc_findnode_rpi(vport, rpi);
10162 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
10163 goto err_exit;
10164
10165 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
10166 lpfc_sli_abts_recover_port(vport, ndlp);
10167 return;
10168
10169 err_exit:
10170 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10171 "3095 Event Context not found, no "
10172 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
10173 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
10174 vpi, rpi);
10175}
10176
10177/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
10178 * @phba: pointer to HBA context object.
10179 * @ndlp: nodelist pointer for the impacted rport.
10180 * @axri: pointer to the wcqe containing the failed exchange.
10181 *
10182 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
10183 * port. The port generates this event when an abort exchange request to an
10184 * rport fails twice in succession with no reply. The abort could be originated
10185 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
10186 */
10187void
10188lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
10189 struct lpfc_nodelist *ndlp,
10190 struct sli4_wcqe_xri_aborted *axri)
10191{
10192 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -050010193 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -050010194
James Smart6b5151f2012-01-18 16:24:06 -050010195 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -050010196 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10197 "3115 Node Context not found, driver "
10198 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -050010199 return;
10200 }
10201
James Smartcb69f7d2011-12-13 13:21:57 -050010202 vport = ndlp->vport;
10203 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10204 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -050010205 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -040010206 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -050010207 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -050010208 bf_get(lpfc_wcqe_xa_status, axri),
10209 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -050010210
James Smart5c1db2a2012-03-01 22:34:36 -050010211 /*
10212 * Catch the ABTS protocol failure case. Older OCe FW releases returned
10213 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
10214 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
10215 */
James Smarte3d2b802012-08-14 14:25:43 -040010216 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -050010217 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
10218 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -050010219 lpfc_sli_abts_recover_port(vport, ndlp);
10220}
10221
James Smarte59058c2008-08-24 21:49:00 -040010222/**
James Smart3621a712009-04-06 18:47:14 -040010223 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -040010224 * @phba: Pointer to HBA context object.
10225 * @pring: Pointer to driver SLI ring object.
10226 * @iocbq: Pointer to iocb object.
10227 *
10228 * This function is called by the slow ring event handler
10229 * function when there is an ASYNC event iocb in the ring.
10230 * This function is called with no lock held.
10231 * Currently this function handles only temperature related
10232 * ASYNC events. The function decodes the temperature sensor
10233 * event message and posts events for the management applications.
10234 **/
James Smart98c9ea52007-10-27 13:37:33 -040010235static void
James Smart57127f12007-10-27 13:37:05 -040010236lpfc_sli_async_event_handler(struct lpfc_hba * phba,
10237 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
10238{
10239 IOCB_t *icmd;
10240 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -040010241 struct temp_event temp_event_data;
10242 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -040010243 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -040010244
10245 icmd = &iocbq->iocb;
10246 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -040010247
James Smartcb69f7d2011-12-13 13:21:57 -050010248 switch (evt_code) {
10249 case ASYNC_TEMP_WARN:
10250 case ASYNC_TEMP_SAFE:
10251 temp_event_data.data = (uint32_t) icmd->ulpContext;
10252 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
10253 if (evt_code == ASYNC_TEMP_WARN) {
10254 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
10255 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10256 "0347 Adapter is very hot, please take "
10257 "corrective action. temperature : %d Celsius\n",
10258 (uint32_t) icmd->ulpContext);
10259 } else {
10260 temp_event_data.event_code = LPFC_NORMAL_TEMP;
10261 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10262 "0340 Adapter temperature is OK now. "
10263 "temperature : %d Celsius\n",
10264 (uint32_t) icmd->ulpContext);
10265 }
10266
10267 /* Send temperature change event to applications */
10268 shost = lpfc_shost_from_vport(phba->pport);
10269 fc_host_post_vendor_event(shost, fc_get_event_number(),
10270 sizeof(temp_event_data), (char *) &temp_event_data,
10271 LPFC_NL_VENDOR_ID);
10272 break;
10273 case ASYNC_STATUS_CN:
10274 lpfc_sli_abts_err_handler(phba, iocbq);
10275 break;
10276 default:
James Smarta257bf92009-04-06 18:48:10 -040010277 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -050010278 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -040010279 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -040010280 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -040010281 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
10282 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
10283 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
10284 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -050010285 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -040010286 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
10287 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
10288 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
10289 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
10290
James Smartcb69f7d2011-12-13 13:21:57 -050010291 break;
James Smart57127f12007-10-27 13:37:05 -040010292 }
James Smart57127f12007-10-27 13:37:05 -040010293}
10294
10295
James Smarte59058c2008-08-24 21:49:00 -040010296/**
James Smart895427b2017-02-12 13:52:30 -080010297 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010298 * @phba: Pointer to HBA context object.
10299 *
10300 * lpfc_sli_setup sets up rings of the SLI interface with
10301 * number of iocbs per ring and iotags. This function is
10302 * called while driver attach to the HBA and before the
10303 * interrupts are enabled. So there is no need for locking.
10304 *
10305 * This function always returns 0.
10306 **/
dea31012005-04-17 16:05:31 -050010307int
James Smart895427b2017-02-12 13:52:30 -080010308lpfc_sli4_setup(struct lpfc_hba *phba)
10309{
10310 struct lpfc_sli_ring *pring;
10311
10312 pring = phba->sli4_hba.els_wq->pring;
10313 pring->num_mask = LPFC_MAX_RING_MASK;
10314 pring->prt[0].profile = 0; /* Mask 0 */
10315 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10316 pring->prt[0].type = FC_TYPE_ELS;
10317 pring->prt[0].lpfc_sli_rcv_unsol_event =
10318 lpfc_els_unsol_event;
10319 pring->prt[1].profile = 0; /* Mask 1 */
10320 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10321 pring->prt[1].type = FC_TYPE_ELS;
10322 pring->prt[1].lpfc_sli_rcv_unsol_event =
10323 lpfc_els_unsol_event;
10324 pring->prt[2].profile = 0; /* Mask 2 */
10325 /* NameServer Inquiry */
10326 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10327 /* NameServer */
10328 pring->prt[2].type = FC_TYPE_CT;
10329 pring->prt[2].lpfc_sli_rcv_unsol_event =
10330 lpfc_ct_unsol_event;
10331 pring->prt[3].profile = 0; /* Mask 3 */
10332 /* NameServer response */
10333 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10334 /* NameServer */
10335 pring->prt[3].type = FC_TYPE_CT;
10336 pring->prt[3].lpfc_sli_rcv_unsol_event =
10337 lpfc_ct_unsol_event;
10338 return 0;
10339}
10340
10341/**
10342 * lpfc_sli_setup - SLI ring setup function
10343 * @phba: Pointer to HBA context object.
10344 *
10345 * lpfc_sli_setup sets up rings of the SLI interface with
10346 * number of iocbs per ring and iotags. This function is
10347 * called while driver attach to the HBA and before the
10348 * interrupts are enabled. So there is no need for locking.
10349 *
10350 * This function always returns 0. SLI3 only.
10351 **/
10352int
dea31012005-04-17 16:05:31 -050010353lpfc_sli_setup(struct lpfc_hba *phba)
10354{
James Smarted957682007-06-17 19:56:37 -050010355 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -050010356 struct lpfc_sli *psli = &phba->sli;
10357 struct lpfc_sli_ring *pring;
10358
James Smart2a76a282012-08-03 12:35:54 -040010359 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -050010360 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -050010361
James Bottomley604a3e32005-10-29 10:28:33 -050010362 psli->iocbq_lookup = NULL;
10363 psli->iocbq_lookup_len = 0;
10364 psli->last_iotag = 0;
10365
dea31012005-04-17 16:05:31 -050010366 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010367 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010368 switch (i) {
10369 case LPFC_FCP_RING: /* ring 0 - FCP */
10370 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010371 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10372 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10373 pring->sli.sli3.numCiocb +=
10374 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10375 pring->sli.sli3.numRiocb +=
10376 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10377 pring->sli.sli3.numCiocb +=
10378 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10379 pring->sli.sli3.numRiocb +=
10380 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10381 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010382 SLI3_IOCB_CMD_SIZE :
10383 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010384 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010385 SLI3_IOCB_RSP_SIZE :
10386 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010387 pring->iotag_ctr = 0;
10388 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -050010389 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -050010390 pring->fast_iotag = pring->iotag_max;
10391 pring->num_mask = 0;
10392 break;
James Smarta4bc3372006-12-02 13:34:16 -050010393 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -050010394 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010395 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10396 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10397 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010398 SLI3_IOCB_CMD_SIZE :
10399 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010400 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010401 SLI3_IOCB_RSP_SIZE :
10402 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -050010403 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -050010404 pring->num_mask = 0;
10405 break;
10406 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10407 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010408 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10409 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10410 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010411 SLI3_IOCB_CMD_SIZE :
10412 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010413 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010414 SLI3_IOCB_RSP_SIZE :
10415 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010416 pring->fast_iotag = 0;
10417 pring->iotag_ctr = 0;
10418 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -040010419 pring->lpfc_sli_rcv_async_status =
10420 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -040010421 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -050010422 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -040010423 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10424 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010425 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010426 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010427 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -040010428 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10429 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010430 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010431 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010432 pring->prt[2].profile = 0; /* Mask 2 */
10433 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -040010434 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -050010435 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010436 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010437 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010438 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010439 pring->prt[3].profile = 0; /* Mask 3 */
10440 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -040010441 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -050010442 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010443 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010444 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010445 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010446 break;
10447 }
James Smart7e56aa22012-08-03 12:35:34 -040010448 totiocbsize += (pring->sli.sli3.numCiocb *
10449 pring->sli.sli3.sizeCiocb) +
10450 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050010451 }
James Smarted957682007-06-17 19:56:37 -050010452 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050010453 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040010454 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10455 "SLI2 SLIM Data: x%x x%lx\n",
10456 phba->brd_no, totiocbsize,
10457 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050010458 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010459 if (phba->cfg_multi_ring_support == 2)
10460 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050010461
10462 return 0;
10463}
10464
James Smarte59058c2008-08-24 21:49:00 -040010465/**
James Smart895427b2017-02-12 13:52:30 -080010466 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040010467 * @phba: Pointer to HBA context object.
10468 *
James Smart895427b2017-02-12 13:52:30 -080010469 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040010470 * ring. This function also initializes ring indices of each ring.
10471 * This function is called during the initialization of the SLI
10472 * interface of an HBA.
10473 * This function is called with no lock held and always returns
10474 * 1.
10475 **/
James Smart895427b2017-02-12 13:52:30 -080010476void
10477lpfc_sli4_queue_init(struct lpfc_hba *phba)
10478{
10479 struct lpfc_sli *psli;
10480 struct lpfc_sli_ring *pring;
10481 int i;
10482
10483 psli = &phba->sli;
10484 spin_lock_irq(&phba->hbalock);
10485 INIT_LIST_HEAD(&psli->mboxq);
10486 INIT_LIST_HEAD(&psli->mboxq_cmpl);
10487 /* Initialize list headers for txq and txcmplq as double linked lists */
James Smartcdb42be2019-01-28 11:14:21 -080010488 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -070010489 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010490 pring->flag = 0;
10491 pring->ringno = LPFC_FCP_RING;
James Smartc4908502019-01-28 11:14:28 -080010492 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010493 INIT_LIST_HEAD(&pring->txq);
10494 INIT_LIST_HEAD(&pring->txcmplq);
10495 INIT_LIST_HEAD(&pring->iocb_continueq);
10496 spin_lock_init(&pring->ring_lock);
10497 }
10498 pring = phba->sli4_hba.els_wq->pring;
10499 pring->flag = 0;
10500 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080010501 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010502 INIT_LIST_HEAD(&pring->txq);
10503 INIT_LIST_HEAD(&pring->txcmplq);
10504 INIT_LIST_HEAD(&pring->iocb_continueq);
10505 spin_lock_init(&pring->ring_lock);
10506
James Smartcdb42be2019-01-28 11:14:21 -080010507 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080010508 pring = phba->sli4_hba.nvmels_wq->pring;
10509 pring->flag = 0;
10510 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080010511 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010512 INIT_LIST_HEAD(&pring->txq);
10513 INIT_LIST_HEAD(&pring->txcmplq);
10514 INIT_LIST_HEAD(&pring->iocb_continueq);
10515 spin_lock_init(&pring->ring_lock);
10516 }
10517
10518 spin_unlock_irq(&phba->hbalock);
10519}
10520
10521/**
10522 * lpfc_sli_queue_init - Queue initialization function
10523 * @phba: Pointer to HBA context object.
10524 *
10525 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10526 * ring. This function also initializes ring indices of each ring.
10527 * This function is called during the initialization of the SLI
10528 * interface of an HBA.
10529 * This function is called with no lock held and always returns
10530 * 1.
10531 **/
10532void
10533lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010534{
10535 struct lpfc_sli *psli;
10536 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050010537 int i;
dea31012005-04-17 16:05:31 -050010538
10539 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050010540 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010541 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050010542 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050010543 /* Initialize list headers for txq and txcmplq as double linked lists */
10544 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010545 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010546 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040010547 pring->sli.sli3.next_cmdidx = 0;
10548 pring->sli.sli3.local_getidx = 0;
10549 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050010550 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050010551 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050010552 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080010553 pring->flag = 0;
10554 INIT_LIST_HEAD(&pring->txq);
10555 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010556 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010557 }
James Smart2e0fef82007-06-17 19:56:36 -050010558 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010559}
10560
James Smarte59058c2008-08-24 21:49:00 -040010561/**
James Smart04c68492009-05-22 14:52:52 -040010562 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10563 * @phba: Pointer to HBA context object.
10564 *
10565 * This routine flushes the mailbox command subsystem. It will unconditionally
10566 * flush all the mailbox commands in the three possible stages in the mailbox
10567 * command sub-system: pending mailbox command queue; the outstanding mailbox
10568 * command; and completed mailbox command queue. It is caller's responsibility
10569 * to make sure that the driver is in the proper state to flush the mailbox
10570 * command sub-system. Namely, the posting of mailbox commands into the
10571 * pending mailbox command queue from the various clients must be stopped;
10572 * either the HBA is in a state that it will never works on the outstanding
10573 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10574 * mailbox command has been completed.
10575 **/
10576static void
10577lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10578{
10579 LIST_HEAD(completions);
10580 struct lpfc_sli *psli = &phba->sli;
10581 LPFC_MBOXQ_t *pmb;
10582 unsigned long iflag;
10583
James Smart523128e2018-09-10 10:30:46 -070010584 /* Disable softirqs, including timers from obtaining phba->hbalock */
10585 local_bh_disable();
10586
James Smart04c68492009-05-22 14:52:52 -040010587 /* Flush all the mailbox commands in the mbox system */
10588 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart523128e2018-09-10 10:30:46 -070010589
James Smart04c68492009-05-22 14:52:52 -040010590 /* The pending mailbox command queue */
10591 list_splice_init(&phba->sli.mboxq, &completions);
10592 /* The outstanding active mailbox command */
10593 if (psli->mbox_active) {
10594 list_add_tail(&psli->mbox_active->list, &completions);
10595 psli->mbox_active = NULL;
10596 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10597 }
10598 /* The completed mailbox command queue */
10599 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10600 spin_unlock_irqrestore(&phba->hbalock, iflag);
10601
James Smart523128e2018-09-10 10:30:46 -070010602 /* Enable softirqs again, done with phba->hbalock */
10603 local_bh_enable();
10604
James Smart04c68492009-05-22 14:52:52 -040010605 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10606 while (!list_empty(&completions)) {
10607 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10608 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10609 if (pmb->mbox_cmpl)
10610 pmb->mbox_cmpl(phba, pmb);
10611 }
10612}
10613
10614/**
James Smart3621a712009-04-06 18:47:14 -040010615 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010616 * @vport: Pointer to virtual port object.
10617 *
10618 * lpfc_sli_host_down is called to clean up the resources
10619 * associated with a vport before destroying virtual
10620 * port data structures.
10621 * This function does following operations:
10622 * - Free discovery resources associated with this virtual
10623 * port.
10624 * - Free iocbs associated with this virtual port in
10625 * the txq.
10626 * - Send abort for all iocb commands associated with this
10627 * vport in txcmplq.
10628 *
10629 * This function is called with no lock held and always returns 1.
10630 **/
dea31012005-04-17 16:05:31 -050010631int
James Smart92d7f7b2007-06-17 19:56:38 -050010632lpfc_sli_host_down(struct lpfc_vport *vport)
10633{
James Smart858c9f62007-06-17 19:56:39 -050010634 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010635 struct lpfc_hba *phba = vport->phba;
10636 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010637 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010638 struct lpfc_sli_ring *pring;
10639 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010640 int i;
10641 unsigned long flags = 0;
10642 uint16_t prev_pring_flag;
10643
10644 lpfc_cleanup_discovery_resources(vport);
10645
10646 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010647
James Smart895427b2017-02-12 13:52:30 -080010648 /*
10649 * Error everything on the txq since these iocbs
10650 * have not been given to the FW yet.
10651 * Also issue ABTS for everything on the txcmplq
10652 */
10653 if (phba->sli_rev != LPFC_SLI_REV4) {
10654 for (i = 0; i < psli->num_rings; i++) {
10655 pring = &psli->sli3_ring[i];
10656 prev_pring_flag = pring->flag;
10657 /* Only slow rings */
10658 if (pring->ringno == LPFC_ELS_RING) {
10659 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10660 /* Set the lpfc data pending flag */
10661 set_bit(LPFC_DATA_READY, &phba->data_flags);
10662 }
10663 list_for_each_entry_safe(iocb, next_iocb,
10664 &pring->txq, list) {
10665 if (iocb->vport != vport)
10666 continue;
10667 list_move_tail(&iocb->list, &completions);
10668 }
10669 list_for_each_entry_safe(iocb, next_iocb,
10670 &pring->txcmplq, list) {
10671 if (iocb->vport != vport)
10672 continue;
10673 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10674 }
10675 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010676 }
James Smart895427b2017-02-12 13:52:30 -080010677 } else {
10678 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10679 pring = qp->pring;
10680 if (!pring)
10681 continue;
10682 if (pring == phba->sli4_hba.els_wq->pring) {
10683 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10684 /* Set the lpfc data pending flag */
10685 set_bit(LPFC_DATA_READY, &phba->data_flags);
10686 }
10687 prev_pring_flag = pring->flag;
James Smart65a3df62019-09-21 20:58:48 -070010688 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080010689 list_for_each_entry_safe(iocb, next_iocb,
10690 &pring->txq, list) {
10691 if (iocb->vport != vport)
10692 continue;
10693 list_move_tail(&iocb->list, &completions);
10694 }
James Smart65a3df62019-09-21 20:58:48 -070010695 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080010696 list_for_each_entry_safe(iocb, next_iocb,
10697 &pring->txcmplq, list) {
10698 if (iocb->vport != vport)
10699 continue;
10700 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10701 }
10702 pring->flag = prev_pring_flag;
10703 }
James Smart92d7f7b2007-06-17 19:56:38 -050010704 }
James Smart92d7f7b2007-06-17 19:56:38 -050010705 spin_unlock_irqrestore(&phba->hbalock, flags);
10706
James Smarta257bf92009-04-06 18:48:10 -040010707 /* Cancel all the IOCBs from the completions list */
10708 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10709 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010710 return 1;
10711}
10712
James Smarte59058c2008-08-24 21:49:00 -040010713/**
James Smart3621a712009-04-06 18:47:14 -040010714 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010715 * @phba: Pointer to HBA context object.
10716 *
10717 * This function cleans up all iocb, buffers, mailbox commands
10718 * while shutting down the HBA. This function is called with no
10719 * lock held and always returns 1.
10720 * This function does the following to cleanup driver resources:
10721 * - Free discovery resources for each virtual port
10722 * - Cleanup any pending fabric iocbs
10723 * - Iterate through the iocb txq and free each entry
10724 * in the list.
10725 * - Free up any buffer posted to the HBA
10726 * - Free mailbox commands in the mailbox queue.
10727 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010728int
James Smart2e0fef82007-06-17 19:56:36 -050010729lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010730{
James Smart2534ba72007-04-25 09:52:20 -040010731 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010732 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010733 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010734 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010735 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010736 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010737 int i;
10738
10739 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010740 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010741
dea31012005-04-17 16:05:31 -050010742 lpfc_hba_down_prep(phba);
10743
James Smart523128e2018-09-10 10:30:46 -070010744 /* Disable softirqs, including timers from obtaining phba->hbalock */
10745 local_bh_disable();
10746
James Smart92d7f7b2007-06-17 19:56:38 -050010747 lpfc_fabric_abort_hba(phba);
10748
James Smart2e0fef82007-06-17 19:56:36 -050010749 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010750
James Smart895427b2017-02-12 13:52:30 -080010751 /*
10752 * Error everything on the txq since these iocbs
10753 * have not been given to the FW yet.
10754 */
10755 if (phba->sli_rev != LPFC_SLI_REV4) {
10756 for (i = 0; i < psli->num_rings; i++) {
10757 pring = &psli->sli3_ring[i];
10758 /* Only slow rings */
10759 if (pring->ringno == LPFC_ELS_RING) {
10760 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10761 /* Set the lpfc data pending flag */
10762 set_bit(LPFC_DATA_READY, &phba->data_flags);
10763 }
10764 list_splice_init(&pring->txq, &completions);
10765 }
10766 } else {
10767 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10768 pring = qp->pring;
10769 if (!pring)
10770 continue;
James Smart4b0a42b2019-08-14 16:56:42 -070010771 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080010772 list_splice_init(&pring->txq, &completions);
James Smart4b0a42b2019-08-14 16:56:42 -070010773 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080010774 if (pring == phba->sli4_hba.els_wq->pring) {
10775 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10776 /* Set the lpfc data pending flag */
10777 set_bit(LPFC_DATA_READY, &phba->data_flags);
10778 }
10779 }
dea31012005-04-17 16:05:31 -050010780 }
James Smart2e0fef82007-06-17 19:56:36 -050010781 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010782
James Smarta257bf92009-04-06 18:48:10 -040010783 /* Cancel all the IOCBs from the completions list */
10784 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10785 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010786
James Smart0ff10d42008-01-11 01:52:36 -050010787 spin_lock_irqsave(&phba->hbalock, flags);
10788 list_splice_init(&phba->elsbuf, &completions);
10789 phba->elsbuf_cnt = 0;
10790 phba->elsbuf_prev_cnt = 0;
10791 spin_unlock_irqrestore(&phba->hbalock, flags);
10792
10793 while (!list_empty(&completions)) {
10794 list_remove_head(&completions, buf_ptr,
10795 struct lpfc_dmabuf, list);
10796 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10797 kfree(buf_ptr);
10798 }
10799
James Smart523128e2018-09-10 10:30:46 -070010800 /* Enable softirqs again, done with phba->hbalock */
10801 local_bh_enable();
10802
dea31012005-04-17 16:05:31 -050010803 /* Return any active mbox cmds */
10804 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010805
James Smartda0436e2009-05-22 14:51:39 -040010806 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010807 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010808 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010809
James Smartda0436e2009-05-22 14:51:39 -040010810 return 1;
10811}
James Smart92d7f7b2007-06-17 19:56:38 -050010812
James Smartda0436e2009-05-22 14:51:39 -040010813/**
James Smart3621a712009-04-06 18:47:14 -040010814 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010815 * @srcp: Source memory pointer.
10816 * @destp: Destination memory pointer.
10817 * @cnt: Number of words required to be copied.
10818 *
10819 * This function is used for copying data between driver memory
10820 * and the SLI memory. This function also changes the endianness
10821 * of each word if native endianness is different from SLI
10822 * endianness. This function can be called with or without
10823 * lock.
10824 **/
dea31012005-04-17 16:05:31 -050010825void
10826lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10827{
10828 uint32_t *src = srcp;
10829 uint32_t *dest = destp;
10830 uint32_t ldata;
10831 int i;
10832
10833 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10834 ldata = *src;
10835 ldata = le32_to_cpu(ldata);
10836 *dest = ldata;
10837 src++;
10838 dest++;
10839 }
10840}
10841
James Smarte59058c2008-08-24 21:49:00 -040010842
10843/**
James Smarta0c87cb2009-07-19 10:01:10 -040010844 * lpfc_sli_bemem_bcopy - SLI memory copy function
10845 * @srcp: Source memory pointer.
10846 * @destp: Destination memory pointer.
10847 * @cnt: Number of words required to be copied.
10848 *
10849 * This function is used for copying data between a data structure
10850 * with big endian representation to local endianness.
10851 * This function can be called with or without lock.
10852 **/
10853void
10854lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10855{
10856 uint32_t *src = srcp;
10857 uint32_t *dest = destp;
10858 uint32_t ldata;
10859 int i;
10860
10861 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10862 ldata = *src;
10863 ldata = be32_to_cpu(ldata);
10864 *dest = ldata;
10865 src++;
10866 dest++;
10867 }
10868}
10869
10870/**
James Smart3621a712009-04-06 18:47:14 -040010871 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010872 * @phba: Pointer to HBA context object.
10873 * @pring: Pointer to driver SLI ring object.
10874 * @mp: Pointer to driver buffer object.
10875 *
10876 * This function is called with no lock held.
10877 * It always return zero after adding the buffer to the postbufq
10878 * buffer list.
10879 **/
dea31012005-04-17 16:05:31 -050010880int
James Smart2e0fef82007-06-17 19:56:36 -050010881lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10882 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010883{
10884 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10885 later */
James Smart2e0fef82007-06-17 19:56:36 -050010886 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010887 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010888 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010889 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010890 return 0;
10891}
10892
James Smarte59058c2008-08-24 21:49:00 -040010893/**
James Smart3621a712009-04-06 18:47:14 -040010894 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010895 * @phba: Pointer to HBA context object.
10896 *
10897 * When HBQ is enabled, buffers are searched based on tags. This function
10898 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10899 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10900 * does not conflict with tags of buffer posted for unsolicited events.
10901 * The function returns the allocated tag. The function is called with
10902 * no locks held.
10903 **/
James Smart76bb24e2007-10-27 13:38:00 -040010904uint32_t
10905lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10906{
10907 spin_lock_irq(&phba->hbalock);
10908 phba->buffer_tag_count++;
10909 /*
10910 * Always set the QUE_BUFTAG_BIT to distiguish between
10911 * a tag assigned by HBQ.
10912 */
10913 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10914 spin_unlock_irq(&phba->hbalock);
10915 return phba->buffer_tag_count;
10916}
10917
James Smarte59058c2008-08-24 21:49:00 -040010918/**
James Smart3621a712009-04-06 18:47:14 -040010919 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010920 * @phba: Pointer to HBA context object.
10921 * @pring: Pointer to driver SLI ring object.
10922 * @tag: Buffer tag.
10923 *
10924 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10925 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10926 * iocb is posted to the response ring with the tag of the buffer.
10927 * This function searches the pring->postbufq list using the tag
10928 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10929 * iocb. If the buffer is found then lpfc_dmabuf object of the
10930 * buffer is returned to the caller else NULL is returned.
10931 * This function is called with no lock held.
10932 **/
James Smart76bb24e2007-10-27 13:38:00 -040010933struct lpfc_dmabuf *
10934lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10935 uint32_t tag)
10936{
10937 struct lpfc_dmabuf *mp, *next_mp;
10938 struct list_head *slp = &pring->postbufq;
10939
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010940 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010941 spin_lock_irq(&phba->hbalock);
10942 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10943 if (mp->buffer_tag == tag) {
10944 list_del_init(&mp->list);
10945 pring->postbufq_cnt--;
10946 spin_unlock_irq(&phba->hbalock);
10947 return mp;
10948 }
10949 }
10950
10951 spin_unlock_irq(&phba->hbalock);
10952 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010953 "0402 Cannot find virtual addr for buffer tag on "
James Smart32350662019-08-14 16:57:06 -070010954 "ring %d Data x%lx x%px x%px x%x\n",
James Smart76bb24e2007-10-27 13:38:00 -040010955 pring->ringno, (unsigned long) tag,
10956 slp->next, slp->prev, pring->postbufq_cnt);
10957
10958 return NULL;
10959}
dea31012005-04-17 16:05:31 -050010960
James Smarte59058c2008-08-24 21:49:00 -040010961/**
James Smart3621a712009-04-06 18:47:14 -040010962 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010963 * @phba: Pointer to HBA context object.
10964 * @pring: Pointer to driver SLI ring object.
10965 * @phys: DMA address of the buffer.
10966 *
10967 * This function searches the buffer list using the dma_address
10968 * of unsolicited event to find the driver's lpfc_dmabuf object
10969 * corresponding to the dma_address. The function returns the
10970 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10971 * This function is called by the ct and els unsolicited event
10972 * handlers to get the buffer associated with the unsolicited
10973 * event.
10974 *
10975 * This function is called with no lock held.
10976 **/
dea31012005-04-17 16:05:31 -050010977struct lpfc_dmabuf *
10978lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10979 dma_addr_t phys)
10980{
10981 struct lpfc_dmabuf *mp, *next_mp;
10982 struct list_head *slp = &pring->postbufq;
10983
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010984 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010985 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010986 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10987 if (mp->phys == phys) {
10988 list_del_init(&mp->list);
10989 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010990 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010991 return mp;
10992 }
10993 }
10994
James Smart2e0fef82007-06-17 19:56:36 -050010995 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010997 "0410 Cannot find virtual addr for mapped buf on "
James Smart32350662019-08-14 16:57:06 -070010998 "ring %d Data x%llx x%px x%px x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010999 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050011000 slp->next, slp->prev, pring->postbufq_cnt);
11001 return NULL;
11002}
11003
James Smarte59058c2008-08-24 21:49:00 -040011004/**
James Smart3621a712009-04-06 18:47:14 -040011005 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040011006 * @phba: Pointer to HBA context object.
11007 * @cmdiocb: Pointer to driver command iocb object.
11008 * @rspiocb: Pointer to driver response iocb object.
11009 *
11010 * This function is the completion handler for the abort iocbs for
11011 * ELS commands. This function is called from the ELS ring event
11012 * handler with no lock held. This function frees memory resources
11013 * associated with the abort iocb.
11014 **/
dea31012005-04-17 16:05:31 -050011015static void
James Smart2e0fef82007-06-17 19:56:36 -050011016lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11017 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050011018{
James Smart2e0fef82007-06-17 19:56:36 -050011019 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040011020 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050011021 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040011022
11023 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050011024
11025 /*
11026 * Assume that the port already completed and returned, or
11027 * will return the iocb. Just Log the message.
11028 */
James Smart2680eea2007-04-25 09:52:55 -040011029 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
11030 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
11031
James Smart2e0fef82007-06-17 19:56:36 -050011032 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040011033 if (phba->sli_rev < LPFC_SLI_REV4) {
James Smartfaa832e2018-07-31 17:23:18 -070011034 if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
11035 irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
11036 irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
11037 spin_unlock_irq(&phba->hbalock);
11038 goto release_iocb;
11039 }
James Smart45ed1192009-10-02 15:17:02 -040011040 if (abort_iotag != 0 &&
11041 abort_iotag <= phba->sli.last_iotag)
11042 abort_iocb =
11043 phba->sli.iocbq_lookup[abort_iotag];
11044 } else
11045 /* For sli4 the abort_tag is the XRI,
11046 * so the abort routine puts the iotag of the iocb
11047 * being aborted in the context field of the abort
11048 * IOCB.
11049 */
11050 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040011051
James Smart2a9bf3d2010-06-07 15:24:45 -040011052 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
James Smart32350662019-08-14 16:57:06 -070011053 "0327 Cannot abort els iocb x%px "
James Smart2a9bf3d2010-06-07 15:24:45 -040011054 "with tag %x context %x, abort status %x, "
11055 "abort code %x\n",
11056 abort_iocb, abort_iotag, abort_context,
11057 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040011058
James Smartff78d8f2011-12-13 13:21:35 -050011059 spin_unlock_irq(&phba->hbalock);
James Smart29601222019-08-14 16:56:36 -070011060 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
11061 irsp->un.ulpWord[4] == IOERR_SLI_ABORTED)
11062 lpfc_sli_release_iocbq(phba, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -040011063 }
James Smartfaa832e2018-07-31 17:23:18 -070011064release_iocb:
James Bottomley604a3e32005-10-29 10:28:33 -050011065 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050011066 return;
11067}
11068
James Smarte59058c2008-08-24 21:49:00 -040011069/**
James Smart3621a712009-04-06 18:47:14 -040011070 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040011071 * @phba: Pointer to HBA context object.
11072 * @cmdiocb: Pointer to driver command iocb object.
11073 * @rspiocb: Pointer to driver response iocb object.
11074 *
11075 * The function is called from SLI ring event handler with no
11076 * lock held. This function is the completion handler for ELS commands
11077 * which are aborted. The function frees memory resources used for
11078 * the aborted ELS commands.
11079 **/
James Smart92d7f7b2007-06-17 19:56:38 -050011080static void
11081lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11082 struct lpfc_iocbq *rspiocb)
11083{
11084 IOCB_t *irsp = &rspiocb->iocb;
11085
11086 /* ELS cmd tag <ulpIoTag> completes */
11087 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040011088 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050011089 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011090 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050011091 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050011092 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
11093 lpfc_ct_free_iocb(phba, cmdiocb);
11094 else
11095 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050011096 return;
11097}
11098
James Smarte59058c2008-08-24 21:49:00 -040011099/**
James Smart5af5eee2010-10-22 11:06:38 -040011100 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040011101 * @phba: Pointer to HBA context object.
11102 * @pring: Pointer to driver SLI ring object.
11103 * @cmdiocb: Pointer to driver command iocb object.
11104 *
James Smart5af5eee2010-10-22 11:06:38 -040011105 * This function issues an abort iocb for the provided command iocb down to
11106 * the port. Other than the case the outstanding command iocb is an abort
11107 * request, this function issues abort out unconditionally. This function is
11108 * called with hbalock held. The function returns 0 when it fails due to
11109 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040011110 **/
James Smart5af5eee2010-10-22 11:06:38 -040011111static int
11112lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050011113 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050011114{
James Smart2e0fef82007-06-17 19:56:36 -050011115 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011116 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050011117 IOCB_t *icmd = NULL;
11118 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011119 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040011120 unsigned long iflags;
James Smartfaa832e2018-07-31 17:23:18 -070011121 struct lpfc_nodelist *ndlp;
James Smart07951072007-04-25 09:51:38 -040011122
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011123 lockdep_assert_held(&phba->hbalock);
11124
James Smart92d7f7b2007-06-17 19:56:38 -050011125 /*
11126 * There are certain command types we don't want to abort. And we
11127 * don't want to abort commands that are already in the process of
11128 * being aborted.
James Smart07951072007-04-25 09:51:38 -040011129 */
11130 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050011131 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050011132 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11133 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040011134 return 0;
11135
dea31012005-04-17 16:05:31 -050011136 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050011137 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011138 if (abtsiocbp == NULL)
11139 return 0;
dea31012005-04-17 16:05:31 -050011140
James Smart07951072007-04-25 09:51:38 -040011141 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050011142 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040011143 */
11144 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11145
dea31012005-04-17 16:05:31 -050011146 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040011147 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
11148 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040011149 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040011150 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040011151 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
James Smartfaa832e2018-07-31 17:23:18 -070011152 } else {
James Smartda0436e2009-05-22 14:51:39 -040011153 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
James Smartfaa832e2018-07-31 17:23:18 -070011154 if (pring->ringno == LPFC_ELS_RING) {
11155 ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
11156 iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
11157 }
11158 }
dea31012005-04-17 16:05:31 -050011159 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040011160 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050011161
James Smart5ffc2662009-11-18 15:39:44 -050011162 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011163 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011164 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
11165 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011166 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
11167 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011168
James Smart2e0fef82007-06-17 19:56:36 -050011169 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040011170 iabt->ulpCommand = CMD_ABORT_XRI_CN;
11171 else
11172 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
11173
11174 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080011175 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040011176
James Smarte8b62012007-08-02 11:10:09 -040011177 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
11178 "0339 Abort xri x%x, original iotag x%x, "
11179 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040011180 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040011181 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040011182 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040011183
11184 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011185 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
11186 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040011187 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040011188 /* Note: both hbalock and ring_lock need to be set here */
11189 spin_lock_irqsave(&pring->ring_lock, iflags);
11190 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11191 abtsiocbp, 0);
11192 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11193 } else {
11194 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11195 abtsiocbp, 0);
11196 }
James Smart07951072007-04-25 09:51:38 -040011197
James Smartd7c255b2008-08-24 21:50:00 -040011198 if (retval)
11199 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040011200
11201 /*
11202 * Caller to this routine should check for IOCB_ERROR
11203 * and handle it properly. This routine no longer removes
11204 * iocb off txcmplq and call compl in case of IOCB_ERROR.
11205 */
11206 return retval;
11207}
11208
11209/**
11210 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
11211 * @phba: Pointer to HBA context object.
11212 * @pring: Pointer to driver SLI ring object.
11213 * @cmdiocb: Pointer to driver command iocb object.
11214 *
11215 * This function issues an abort iocb for the provided command iocb. In case
11216 * of unloading, the abort iocb will not be issued to commands on the ELS
11217 * ring. Instead, the callback function shall be changed to those commands
11218 * so that nothing happens when them finishes. This function is called with
11219 * hbalock held. The function returns 0 when the command iocb is an abort
11220 * request.
11221 **/
11222int
11223lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11224 struct lpfc_iocbq *cmdiocb)
11225{
11226 struct lpfc_vport *vport = cmdiocb->vport;
11227 int retval = IOCB_ERROR;
11228 IOCB_t *icmd = NULL;
11229
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011230 lockdep_assert_held(&phba->hbalock);
11231
James Smart5af5eee2010-10-22 11:06:38 -040011232 /*
11233 * There are certain command types we don't want to abort. And we
11234 * don't want to abort commands that are already in the process of
11235 * being aborted.
11236 */
11237 icmd = &cmdiocb->iocb;
11238 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
11239 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11240 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11241 return 0;
11242
Dick Kennedy1234a6d2017-09-29 17:34:29 -070011243 if (!pring) {
11244 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11245 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11246 else
11247 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11248 goto abort_iotag_exit;
11249 }
11250
James Smart5af5eee2010-10-22 11:06:38 -040011251 /*
11252 * If we're unloading, don't abort iocb on the ELS ring, but change
11253 * the callback so that nothing happens when it finishes.
11254 */
11255 if ((vport->load_flag & FC_UNLOADING) &&
11256 (pring->ringno == LPFC_ELS_RING)) {
11257 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11258 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11259 else
11260 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11261 goto abort_iotag_exit;
11262 }
11263
11264 /* Now, we try to issue the abort to the cmdiocb out */
11265 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
11266
James Smart07951072007-04-25 09:51:38 -040011267abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050011268 /*
11269 * Caller to this routine should check for IOCB_ERROR
11270 * and handle it properly. This routine no longer removes
11271 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040011272 */
James Smart2e0fef82007-06-17 19:56:36 -050011273 return retval;
dea31012005-04-17 16:05:31 -050011274}
11275
James Smarte59058c2008-08-24 21:49:00 -040011276/**
James Smart5af5eee2010-10-22 11:06:38 -040011277 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11278 * @phba: pointer to lpfc HBA data structure.
11279 *
11280 * This routine will abort all pending and outstanding iocbs to an HBA.
11281 **/
11282void
11283lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11284{
11285 struct lpfc_sli *psli = &phba->sli;
11286 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080011287 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011288 int i;
11289
James Smart895427b2017-02-12 13:52:30 -080011290 if (phba->sli_rev != LPFC_SLI_REV4) {
11291 for (i = 0; i < psli->num_rings; i++) {
11292 pring = &psli->sli3_ring[i];
11293 lpfc_sli_abort_iocb_ring(phba, pring);
11294 }
11295 return;
11296 }
11297 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11298 pring = qp->pring;
11299 if (!pring)
11300 continue;
James Smartdb55fba2014-04-04 13:52:02 -040011301 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040011302 }
11303}
11304
11305/**
James Smart3621a712009-04-06 18:47:14 -040011306 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040011307 * @iocbq: Pointer to driver iocb object.
11308 * @vport: Pointer to driver virtual port object.
11309 * @tgt_id: SCSI ID of the target.
11310 * @lun_id: LUN ID of the scsi device.
11311 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11312 *
James Smart3621a712009-04-06 18:47:14 -040011313 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040011314 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11315 * 0 if the filtering criteria is met for the given iocb and will return
11316 * 1 if the filtering criteria is not met.
11317 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11318 * given iocb is for the SCSI device specified by vport, tgt_id and
11319 * lun_id parameter.
11320 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11321 * given iocb is for the SCSI target specified by vport and tgt_id
11322 * parameters.
11323 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11324 * given iocb is for the SCSI host associated with the given vport.
11325 * This function is called with no locks held.
11326 **/
dea31012005-04-17 16:05:31 -050011327static int
James Smart51ef4c22007-08-02 11:10:31 -040011328lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11329 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011330 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011331{
James Smartc4908502019-01-28 11:14:28 -080011332 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011333 int rc = 1;
11334
James Smartb0e83012018-06-26 08:24:29 -070011335 if (iocbq->vport != vport)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011336 return rc;
11337
James Smartb0e83012018-06-26 08:24:29 -070011338 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
11339 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
James Smart51ef4c22007-08-02 11:10:31 -040011340 return rc;
11341
James Smartc4908502019-01-28 11:14:28 -080011342 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011343
James Smart495a7142008-06-14 22:52:59 -040011344 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050011345 return rc;
11346
11347 switch (ctx_cmd) {
11348 case LPFC_CTX_LUN:
James Smartb0e83012018-06-26 08:24:29 -070011349 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011350 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11351 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050011352 rc = 0;
11353 break;
11354 case LPFC_CTX_TGT:
James Smartb0e83012018-06-26 08:24:29 -070011355 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011356 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050011357 rc = 0;
11358 break;
dea31012005-04-17 16:05:31 -050011359 case LPFC_CTX_HOST:
11360 rc = 0;
11361 break;
11362 default:
11363 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070011364 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050011365 break;
11366 }
11367
11368 return rc;
11369}
11370
James Smarte59058c2008-08-24 21:49:00 -040011371/**
James Smart3621a712009-04-06 18:47:14 -040011372 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040011373 * @vport: Pointer to virtual port.
11374 * @tgt_id: SCSI ID of the target.
11375 * @lun_id: LUN ID of the scsi device.
11376 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11377 *
11378 * This function returns number of FCP commands pending for the vport.
11379 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11380 * commands pending on the vport associated with SCSI device specified
11381 * by tgt_id and lun_id parameters.
11382 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11383 * commands pending on the vport associated with SCSI target specified
11384 * by tgt_id parameter.
11385 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11386 * commands pending on the vport.
11387 * This function returns the number of iocbs which satisfy the filter.
11388 * This function is called without any lock held.
11389 **/
dea31012005-04-17 16:05:31 -050011390int
James Smart51ef4c22007-08-02 11:10:31 -040011391lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11392 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011393{
James Smart51ef4c22007-08-02 11:10:31 -040011394 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011395 struct lpfc_iocbq *iocbq;
11396 int sum, i;
dea31012005-04-17 16:05:31 -050011397
Johannes Thumshirn31979002016-07-18 16:06:03 +020011398 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011399 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11400 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011401
James Smart51ef4c22007-08-02 11:10:31 -040011402 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11403 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011404 sum++;
dea31012005-04-17 16:05:31 -050011405 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020011406 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011407
dea31012005-04-17 16:05:31 -050011408 return sum;
11409}
11410
James Smarte59058c2008-08-24 21:49:00 -040011411/**
James Smart3621a712009-04-06 18:47:14 -040011412 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040011413 * @phba: Pointer to HBA context object
11414 * @cmdiocb: Pointer to command iocb object.
11415 * @rspiocb: Pointer to response iocb object.
11416 *
11417 * This function is called when an aborted FCP iocb completes. This
11418 * function is called by the ring event handler with no lock held.
11419 * This function frees the iocb.
11420 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011421void
James Smart2e0fef82007-06-17 19:56:36 -050011422lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11423 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011424{
James Smartcb69f7d2011-12-13 13:21:57 -050011425 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040011426 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050011427 "original iotag x%x, abort cmd iotag x%x "
11428 "status 0x%x, reason 0x%x\n",
11429 cmdiocb->iocb.un.acxri.abortContextTag,
11430 cmdiocb->iocb.un.acxri.abortIoTag,
11431 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11432 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050011433 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011434 return;
11435}
11436
James Smarte59058c2008-08-24 21:49:00 -040011437/**
James Smart3621a712009-04-06 18:47:14 -040011438 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040011439 * @vport: Pointer to virtual port.
11440 * @pring: Pointer to driver SLI ring object.
11441 * @tgt_id: SCSI ID of the target.
11442 * @lun_id: LUN ID of the scsi device.
11443 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11444 *
11445 * This function sends an abort command for every SCSI command
11446 * associated with the given virtual port pending on the ring
11447 * filtered by lpfc_sli_validate_fcp_iocb function.
11448 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11449 * FCP iocbs associated with lun specified by tgt_id and lun_id
11450 * parameters
11451 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11452 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11453 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11454 * FCP iocbs associated with virtual port.
11455 * This function returns number of iocbs it failed to abort.
11456 * This function is called with no locks held.
11457 **/
dea31012005-04-17 16:05:31 -050011458int
James Smart51ef4c22007-08-02 11:10:31 -040011459lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11460 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050011461{
James Smart51ef4c22007-08-02 11:10:31 -040011462 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011463 struct lpfc_iocbq *iocbq;
11464 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070011465 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050011466 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050011467 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011468 int i;
dea31012005-04-17 16:05:31 -050011469
James Smartb0e83012018-06-26 08:24:29 -070011470 /* all I/Os are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -070011471 if (phba->hba_flag & HBA_IOQ_FLUSH)
James Smartb0e83012018-06-26 08:24:29 -070011472 return errcnt;
11473
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011474 for (i = 1; i <= phba->sli.last_iotag; i++) {
11475 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011476
James Smart51ef4c22007-08-02 11:10:31 -040011477 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011478 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011479 continue;
11480
James Smartafbd8d82013-09-06 12:22:13 -040011481 /*
11482 * If the iocbq is already being aborted, don't take a second
11483 * action, but do count it.
11484 */
11485 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11486 continue;
11487
dea31012005-04-17 16:05:31 -050011488 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011489 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011490 if (abtsiocb == NULL) {
11491 errcnt++;
11492 continue;
11493 }
dea31012005-04-17 16:05:31 -050011494
James Smartafbd8d82013-09-06 12:22:13 -040011495 /* indicate the IO is being aborted by the driver. */
11496 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11497
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011498 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011499 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11500 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011501 if (phba->sli_rev == LPFC_SLI_REV4)
11502 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11503 else
11504 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011505 abtsiocb->iocb.ulpLe = 1;
11506 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011507 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011508
James Smart5ffc2662009-11-18 15:39:44 -050011509 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011510 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011511 if (iocbq->iocb_flag & LPFC_IO_FCP)
11512 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011513 if (iocbq->iocb_flag & LPFC_IO_FOF)
11514 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011515
James Smart2e0fef82007-06-17 19:56:36 -050011516 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011517 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11518 else
11519 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11520
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011521 /* Setup callback routine and issue the command. */
11522 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011523 if (phba->sli_rev == LPFC_SLI_REV4) {
11524 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11525 if (!pring_s4)
11526 continue;
11527 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11528 abtsiocb, 0);
11529 } else
11530 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11531 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011532 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011533 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011534 errcnt++;
11535 continue;
11536 }
11537 }
11538
11539 return errcnt;
11540}
11541
James Smarte59058c2008-08-24 21:49:00 -040011542/**
James Smart98912dda2014-04-04 13:52:31 -040011543 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11544 * @vport: Pointer to virtual port.
11545 * @pring: Pointer to driver SLI ring object.
11546 * @tgt_id: SCSI ID of the target.
11547 * @lun_id: LUN ID of the scsi device.
11548 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11549 *
11550 * This function sends an abort command for every SCSI command
11551 * associated with the given virtual port pending on the ring
11552 * filtered by lpfc_sli_validate_fcp_iocb function.
11553 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11554 * FCP iocbs associated with lun specified by tgt_id and lun_id
11555 * parameters
11556 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11557 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11558 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11559 * FCP iocbs associated with virtual port.
11560 * This function returns number of iocbs it aborted .
11561 * This function is called with no locks held right after a taskmgmt
11562 * command is sent.
11563 **/
11564int
11565lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11566 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11567{
11568 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -080011569 struct lpfc_io_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011570 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011571 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011572 struct lpfc_iocbq *iocbq;
11573 IOCB_t *icmd;
11574 int sum, i, ret_val;
11575 unsigned long iflags;
James Smartc2017262019-01-28 11:14:37 -080011576 struct lpfc_sli_ring *pring_s4 = NULL;
James Smart98912dda2014-04-04 13:52:31 -040011577
James Smart59c68ea2018-04-09 14:24:25 -070011578 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011579
11580 /* all I/Os are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -070011581 if (phba->hba_flag & HBA_IOQ_FLUSH) {
James Smart59c68ea2018-04-09 14:24:25 -070011582 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011583 return 0;
11584 }
11585 sum = 0;
11586
11587 for (i = 1; i <= phba->sli.last_iotag; i++) {
11588 iocbq = phba->sli.iocbq_lookup[i];
11589
11590 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11591 cmd) != 0)
11592 continue;
11593
James Smartc2017262019-01-28 11:14:37 -080011594 /* Guard against IO completion being called at same time */
11595 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
11596 spin_lock(&lpfc_cmd->buf_lock);
11597
11598 if (!lpfc_cmd->pCmd) {
11599 spin_unlock(&lpfc_cmd->buf_lock);
11600 continue;
11601 }
11602
11603 if (phba->sli_rev == LPFC_SLI_REV4) {
11604 pring_s4 =
James Smartc00f62e2019-08-14 16:57:11 -070011605 phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;
James Smartc2017262019-01-28 11:14:37 -080011606 if (!pring_s4) {
11607 spin_unlock(&lpfc_cmd->buf_lock);
11608 continue;
11609 }
11610 /* Note: both hbalock and ring_lock must be set here */
11611 spin_lock(&pring_s4->ring_lock);
11612 }
11613
James Smart98912dda2014-04-04 13:52:31 -040011614 /*
11615 * If the iocbq is already being aborted, don't take a second
11616 * action, but do count it.
11617 */
James Smartc2017262019-01-28 11:14:37 -080011618 if ((iocbq->iocb_flag & LPFC_DRIVER_ABORTED) ||
11619 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
11620 if (phba->sli_rev == LPFC_SLI_REV4)
11621 spin_unlock(&pring_s4->ring_lock);
11622 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011623 continue;
James Smartc2017262019-01-28 11:14:37 -080011624 }
James Smart98912dda2014-04-04 13:52:31 -040011625
11626 /* issue ABTS for this IOCB based on iotag */
11627 abtsiocbq = __lpfc_sli_get_iocbq(phba);
James Smartc2017262019-01-28 11:14:37 -080011628 if (!abtsiocbq) {
11629 if (phba->sli_rev == LPFC_SLI_REV4)
11630 spin_unlock(&pring_s4->ring_lock);
11631 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011632 continue;
James Smartc2017262019-01-28 11:14:37 -080011633 }
James Smart98912dda2014-04-04 13:52:31 -040011634
11635 icmd = &iocbq->iocb;
11636 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11637 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11638 if (phba->sli_rev == LPFC_SLI_REV4)
11639 abtsiocbq->iocb.un.acxri.abortIoTag =
11640 iocbq->sli4_xritag;
11641 else
11642 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11643 abtsiocbq->iocb.ulpLe = 1;
11644 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11645 abtsiocbq->vport = vport;
11646
11647 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011648 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011649 if (iocbq->iocb_flag & LPFC_IO_FCP)
11650 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011651 if (iocbq->iocb_flag & LPFC_IO_FOF)
11652 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011653
James Smart8c50d252014-09-03 12:58:16 -040011654 ndlp = lpfc_cmd->rdata->pnode;
11655
11656 if (lpfc_is_link_up(phba) &&
11657 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011658 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11659 else
11660 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11661
11662 /* Setup callback routine and issue the command. */
11663 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11664
11665 /*
11666 * Indicate the IO is being aborted by the driver and set
11667 * the caller's flag into the aborted IO.
11668 */
11669 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11670
11671 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart98912dda2014-04-04 13:52:31 -040011672 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11673 abtsiocbq, 0);
James Smart59c68ea2018-04-09 14:24:25 -070011674 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011675 } else {
11676 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11677 abtsiocbq, 0);
11678 }
11679
James Smartc2017262019-01-28 11:14:37 -080011680 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011681
11682 if (ret_val == IOCB_ERROR)
11683 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11684 else
11685 sum++;
11686 }
James Smart59c68ea2018-04-09 14:24:25 -070011687 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011688 return sum;
11689}
11690
11691/**
James Smart3621a712009-04-06 18:47:14 -040011692 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011693 * @phba: Pointer to HBA context object.
11694 * @cmdiocbq: Pointer to command iocb.
11695 * @rspiocbq: Pointer to response iocb.
11696 *
11697 * This function is the completion handler for iocbs issued using
11698 * lpfc_sli_issue_iocb_wait function. This function is called by the
11699 * ring event handler function without any lock held. This function
11700 * can be called from both worker thread context and interrupt
11701 * context. This function also can be called from other thread which
11702 * cleans up the SLI layer objects.
11703 * This function copy the contents of the response iocb to the
11704 * response iocb memory object provided by the caller of
11705 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11706 * sleeps for the iocb completion.
11707 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011708static void
11709lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11710 struct lpfc_iocbq *cmdiocbq,
11711 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011712{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011713 wait_queue_head_t *pdone_q;
11714 unsigned long iflags;
James Smartc4908502019-01-28 11:14:28 -080011715 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011716
James Smart2e0fef82007-06-17 19:56:36 -050011717 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011718 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11719
11720 /*
11721 * A time out has occurred for the iocb. If a time out
11722 * completion handler has been supplied, call it. Otherwise,
11723 * just free the iocbq.
11724 */
11725
11726 spin_unlock_irqrestore(&phba->hbalock, iflags);
11727 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11728 cmdiocbq->wait_iocb_cmpl = NULL;
11729 if (cmdiocbq->iocb_cmpl)
11730 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11731 else
11732 lpfc_sli_release_iocbq(phba, cmdiocbq);
11733 return;
11734 }
11735
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011736 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11737 if (cmdiocbq->context2 && rspiocbq)
11738 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11739 &rspiocbq->iocb, sizeof(IOCB_t));
11740
James Smart0f65ff62010-02-26 14:14:23 -050011741 /* Set the exchange busy flag for task management commands */
11742 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11743 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smartc4908502019-01-28 11:14:28 -080011744 lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
James Smart0f65ff62010-02-26 14:14:23 -050011745 cur_iocbq);
11746 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11747 }
11748
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011749 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011750 if (pdone_q)
11751 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011752 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011753 return;
11754}
11755
James Smarte59058c2008-08-24 21:49:00 -040011756/**
James Smartd11e31d2009-06-10 17:23:06 -040011757 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11758 * @phba: Pointer to HBA context object..
11759 * @piocbq: Pointer to command iocb.
11760 * @flag: Flag to test.
11761 *
11762 * This routine grabs the hbalock and then test the iocb_flag to
11763 * see if the passed in flag is set.
11764 * Returns:
11765 * 1 if flag is set.
11766 * 0 if flag is not set.
11767 **/
11768static int
11769lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11770 struct lpfc_iocbq *piocbq, uint32_t flag)
11771{
11772 unsigned long iflags;
11773 int ret;
11774
11775 spin_lock_irqsave(&phba->hbalock, iflags);
11776 ret = piocbq->iocb_flag & flag;
11777 spin_unlock_irqrestore(&phba->hbalock, iflags);
11778 return ret;
11779
11780}
11781
11782/**
James Smart3621a712009-04-06 18:47:14 -040011783 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011784 * @phba: Pointer to HBA context object..
11785 * @pring: Pointer to sli ring.
11786 * @piocb: Pointer to command iocb.
11787 * @prspiocbq: Pointer to response iocb.
11788 * @timeout: Timeout in number of seconds.
11789 *
11790 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011791 * iocb to complete. The iocb_cmpl field of the shall be used
11792 * to handle iocbs which time out. If the field is NULL, the
11793 * function shall free the iocbq structure. If more clean up is
11794 * needed, the caller is expected to provide a completion function
11795 * that will provide the needed clean up. If the iocb command is
11796 * not completed within timeout seconds, the function will either
11797 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11798 * completion function set in the iocb_cmpl field and then return
11799 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11800 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011801 * The function waits for the iocb completion using an
11802 * non-interruptible wait.
11803 * This function will sleep while waiting for iocb completion.
11804 * So, this function should not be called from any context which
11805 * does not allow sleeping. Due to the same reason, this function
11806 * cannot be called with interrupt disabled.
11807 * This function assumes that the iocb completions occur while
11808 * this function sleep. So, this function cannot be called from
11809 * the thread which process iocb completion for this ring.
11810 * This function clears the iocb_flag of the iocb object before
11811 * issuing the iocb and the iocb completion handler sets this
11812 * flag and wakes this thread when the iocb completes.
11813 * The contents of the response iocb will be copied to prspiocbq
11814 * by the completion handler when the command completes.
11815 * This function returns IOCB_SUCCESS when success.
11816 * This function is called with no lock held.
11817 **/
dea31012005-04-17 16:05:31 -050011818int
James Smart2e0fef82007-06-17 19:56:36 -050011819lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011820 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011821 struct lpfc_iocbq *piocb,
11822 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011823 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011824{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011825 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011826 long timeleft, timeout_req = 0;
11827 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011828 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011829 struct lpfc_iocbq *iocb;
11830 int txq_cnt = 0;
11831 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011832 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011833 unsigned long iflags;
11834 bool iocb_completed = true;
11835
James Smart895427b2017-02-12 13:52:30 -080011836 if (phba->sli_rev >= LPFC_SLI_REV4)
11837 pring = lpfc_sli4_calc_ring(phba, piocb);
11838 else
11839 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011840 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011841 * If the caller has provided a response iocbq buffer, then context2
11842 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011843 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011844 if (prspiocbq) {
11845 if (piocb->context2)
11846 return IOCB_ERROR;
11847 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011848 }
11849
James Smart5a0916b2013-07-15 18:31:42 -040011850 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011851 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11852 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011853 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011854
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011855 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011856 if (lpfc_readl(phba->HCregaddr, &creg_val))
11857 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011858 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11859 writel(creg_val, phba->HCregaddr);
11860 readl(phba->HCregaddr); /* flush */
11861 }
11862
James Smart2a9bf3d2010-06-07 15:24:45 -040011863 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11864 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011865 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011866 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011867 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011868 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011869 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011870 spin_lock_irqsave(&phba->hbalock, iflags);
11871 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011872
James Smart5a0916b2013-07-15 18:31:42 -040011873 /*
11874 * IOCB timed out. Inform the wake iocb wait
11875 * completion function and set local status
11876 */
11877
11878 iocb_completed = false;
11879 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11880 }
11881 spin_unlock_irqrestore(&phba->hbalock, iflags);
11882 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011883 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011884 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011885 /* Note: we are not indicating if the IOCB has a success
11886 * status or not - that's for the caller to check.
11887 * IOCB_SUCCESS means just that the command was sent and
11888 * completed. Not that it completed successfully.
11889 * */
James Smart7054a602007-04-25 09:52:34 -040011890 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011891 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011892 "0338 IOCB wait timeout error - no "
11893 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011894 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011895 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011896 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011897 "0330 IOCB wake NOT set, "
11898 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011899 timeout, (timeleft / jiffies));
11900 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011901 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011902 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011903 if (phba->cfg_log_verbose & LOG_SLI) {
11904 list_for_each_entry(iocb, &pring->txq, list) {
11905 txq_cnt++;
11906 }
11907 list_for_each_entry(iocb, &pring->txcmplq, list) {
11908 txcmplq_cnt++;
11909 }
11910 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11911 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11912 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11913 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011914 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011915 } else {
11916 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011917 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011918 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011919 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011920 }
11921
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011922 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011923 if (lpfc_readl(phba->HCregaddr, &creg_val))
11924 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011925 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11926 writel(creg_val, phba->HCregaddr);
11927 readl(phba->HCregaddr); /* flush */
11928 }
11929
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011930 if (prspiocbq)
11931 piocb->context2 = NULL;
11932
11933 piocb->context_un.wait_queue = NULL;
11934 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011935 return retval;
11936}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011937
James Smarte59058c2008-08-24 21:49:00 -040011938/**
James Smart3621a712009-04-06 18:47:14 -040011939 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011940 * @phba: Pointer to HBA context object.
11941 * @pmboxq: Pointer to driver mailbox object.
11942 * @timeout: Timeout in number of seconds.
11943 *
11944 * This function issues the mailbox to firmware and waits for the
11945 * mailbox command to complete. If the mailbox command is not
11946 * completed within timeout seconds, it returns MBX_TIMEOUT.
11947 * The function waits for the mailbox completion using an
11948 * interruptible wait. If the thread is woken up due to a
11949 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11950 * should not free the mailbox resources, if this function returns
11951 * MBX_TIMEOUT.
11952 * This function will sleep while waiting for mailbox completion.
11953 * So, this function should not be called from any context which
11954 * does not allow sleeping. Due to the same reason, this function
11955 * cannot be called with interrupt disabled.
11956 * This function assumes that the mailbox completion occurs while
11957 * this function sleep. So, this function cannot be called from
11958 * the worker thread which processes mailbox completion.
11959 * This function is called in the context of HBA management
11960 * applications.
11961 * This function returns MBX_SUCCESS when successful.
11962 * This function is called with no lock held.
11963 **/
dea31012005-04-17 16:05:31 -050011964int
James Smart2e0fef82007-06-17 19:56:36 -050011965lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011966 uint32_t timeout)
11967{
James Smarte29d74f2018-03-05 12:04:07 -080011968 struct completion mbox_done;
dea31012005-04-17 16:05:31 -050011969 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011970 unsigned long flag;
dea31012005-04-17 16:05:31 -050011971
James Smart495a7142008-06-14 22:52:59 -040011972 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011973 /* setup wake call as IOCB callback */
11974 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea31012005-04-17 16:05:31 -050011975
James Smarte29d74f2018-03-05 12:04:07 -080011976 /* setup context3 field to pass wait_queue pointer to wake function */
11977 init_completion(&mbox_done);
11978 pmboxq->context3 = &mbox_done;
dea31012005-04-17 16:05:31 -050011979 /* now issue the command */
11980 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011981 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smarte29d74f2018-03-05 12:04:07 -080011982 wait_for_completion_timeout(&mbox_done,
11983 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011984
James Smart858c9f62007-06-17 19:56:39 -050011985 spin_lock_irqsave(&phba->hbalock, flag);
James Smarte29d74f2018-03-05 12:04:07 -080011986 pmboxq->context3 = NULL;
James Smart7054a602007-04-25 09:52:34 -040011987 /*
11988 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11989 * else do not free the resources.
11990 */
James Smartd7c47992010-06-08 18:31:54 -040011991 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011992 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011993 } else {
James Smart7054a602007-04-25 09:52:34 -040011994 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011995 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11996 }
11997 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050011998 }
dea31012005-04-17 16:05:31 -050011999 return retval;
12000}
12001
James Smarte59058c2008-08-24 21:49:00 -040012002/**
James Smart3772a992009-05-22 14:50:54 -040012003 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040012004 * @phba: Pointer to HBA context.
12005 *
James Smart3772a992009-05-22 14:50:54 -040012006 * This function is called to shutdown the driver's mailbox sub-system.
12007 * It first marks the mailbox sub-system is in a block state to prevent
12008 * the asynchronous mailbox command from issued off the pending mailbox
12009 * command queue. If the mailbox command sub-system shutdown is due to
12010 * HBA error conditions such as EEH or ERATT, this routine shall invoke
12011 * the mailbox sub-system flush routine to forcefully bring down the
12012 * mailbox sub-system. Otherwise, if it is due to normal condition (such
12013 * as with offline or HBA function reset), this routine will wait for the
12014 * outstanding mailbox command to complete before invoking the mailbox
12015 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040012016 **/
James Smart3772a992009-05-22 14:50:54 -040012017void
James Smart618a5232012-06-12 13:54:36 -040012018lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040012019{
James Smart3772a992009-05-22 14:50:54 -040012020 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040012021 unsigned long timeout;
12022
James Smart618a5232012-06-12 13:54:36 -040012023 if (mbx_action == LPFC_MBX_NO_WAIT) {
12024 /* delay 100ms for port state */
12025 msleep(100);
12026 lpfc_sli_mbox_sys_flush(phba);
12027 return;
12028 }
James Smarta183a152011-10-10 21:32:43 -040012029 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050012030
James Smart523128e2018-09-10 10:30:46 -070012031 /* Disable softirqs, including timers from obtaining phba->hbalock */
12032 local_bh_disable();
12033
James Smart3772a992009-05-22 14:50:54 -040012034 spin_lock_irq(&phba->hbalock);
12035 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040012036
12037 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040012038 /* Determine how long we might wait for the active mailbox
12039 * command to be gracefully completed by firmware.
12040 */
James Smarta183a152011-10-10 21:32:43 -040012041 if (phba->sli.mbox_active)
12042 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
12043 phba->sli.mbox_active) *
12044 1000) + jiffies;
12045 spin_unlock_irq(&phba->hbalock);
12046
James Smart523128e2018-09-10 10:30:46 -070012047 /* Enable softirqs again, done with phba->hbalock */
12048 local_bh_enable();
12049
James Smart3772a992009-05-22 14:50:54 -040012050 while (phba->sli.mbox_active) {
12051 /* Check active mailbox complete status every 2ms */
12052 msleep(2);
12053 if (time_after(jiffies, timeout))
12054 /* Timeout, let the mailbox flush routine to
12055 * forcefully release active mailbox command
12056 */
12057 break;
12058 }
James Smart523128e2018-09-10 10:30:46 -070012059 } else {
James Smartd7069f02012-03-01 22:36:29 -050012060 spin_unlock_irq(&phba->hbalock);
12061
James Smart523128e2018-09-10 10:30:46 -070012062 /* Enable softirqs again, done with phba->hbalock */
12063 local_bh_enable();
12064 }
12065
James Smart3772a992009-05-22 14:50:54 -040012066 lpfc_sli_mbox_sys_flush(phba);
12067}
12068
12069/**
12070 * lpfc_sli_eratt_read - read sli-3 error attention events
12071 * @phba: Pointer to HBA context.
12072 *
12073 * This function is called to read the SLI3 device error attention registers
12074 * for possible error attention events. The caller must hold the hostlock
12075 * with spin_lock_irq().
12076 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012077 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040012078 * Register and returns 0 otherwise.
12079 **/
12080static int
12081lpfc_sli_eratt_read(struct lpfc_hba *phba)
12082{
James Smarted957682007-06-17 19:56:37 -050012083 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040012084
James Smart3772a992009-05-22 14:50:54 -040012085 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050012086 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12087 goto unplug_err;
12088
James Smart3772a992009-05-22 14:50:54 -040012089 if (ha_copy & HA_ERATT) {
12090 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050012091 if (lpfc_sli_read_hs(phba))
12092 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040012093
James Smart3772a992009-05-22 14:50:54 -040012094 /* Check if there is a deferred error condition is active */
12095 if ((HS_FFER1 & phba->work_hs) &&
12096 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012097 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040012098 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040012099 /* Clear all interrupt enable conditions */
12100 writel(0, phba->HCregaddr);
12101 readl(phba->HCregaddr);
12102 }
12103
12104 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040012105 phba->work_ha |= HA_ERATT;
12106 /* Indicate polling handles this ERATT */
12107 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012108 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012109 }
James Smart3772a992009-05-22 14:50:54 -040012110 return 0;
James Smart9940b972011-03-11 16:06:12 -050012111
12112unplug_err:
12113 /* Set the driver HS work bitmap */
12114 phba->work_hs |= UNPLUG_ERR;
12115 /* Set the driver HA work bitmap */
12116 phba->work_ha |= HA_ERATT;
12117 /* Indicate polling handles this ERATT */
12118 phba->hba_flag |= HBA_ERATT_HANDLED;
12119 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012120}
12121
James Smarte59058c2008-08-24 21:49:00 -040012122/**
James Smartda0436e2009-05-22 14:51:39 -040012123 * lpfc_sli4_eratt_read - read sli-4 error attention events
12124 * @phba: Pointer to HBA context.
12125 *
12126 * This function is called to read the SLI4 device error attention registers
12127 * for possible error attention events. The caller must hold the hostlock
12128 * with spin_lock_irq().
12129 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012130 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040012131 * Register and returns 0 otherwise.
12132 **/
12133static int
12134lpfc_sli4_eratt_read(struct lpfc_hba *phba)
12135{
12136 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050012137 uint32_t if_type, portsmphr;
12138 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040012139
James Smart2fcee4b2010-12-15 17:57:46 -050012140 /*
12141 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040012142 * registers for error attention. This can be changed later.
12143 */
James Smart2fcee4b2010-12-15 17:57:46 -050012144 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
12145 switch (if_type) {
12146 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050012147 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
12148 &uerr_sta_lo) ||
12149 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
12150 &uerr_sta_hi)) {
12151 phba->work_hs |= UNPLUG_ERR;
12152 phba->work_ha |= HA_ERATT;
12153 phba->hba_flag |= HBA_ERATT_HANDLED;
12154 return 1;
12155 }
James Smart2fcee4b2010-12-15 17:57:46 -050012156 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
12157 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
12158 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12159 "1423 HBA Unrecoverable error: "
12160 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
12161 "ue_mask_lo_reg=0x%x, "
12162 "ue_mask_hi_reg=0x%x\n",
12163 uerr_sta_lo, uerr_sta_hi,
12164 phba->sli4_hba.ue_mask_lo,
12165 phba->sli4_hba.ue_mask_hi);
12166 phba->work_status[0] = uerr_sta_lo;
12167 phba->work_status[1] = uerr_sta_hi;
12168 phba->work_ha |= HA_ERATT;
12169 phba->hba_flag |= HBA_ERATT_HANDLED;
12170 return 1;
12171 }
12172 break;
12173 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080012174 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050012175 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
12176 &portstat_reg.word0) ||
12177 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
12178 &portsmphr)){
12179 phba->work_hs |= UNPLUG_ERR;
12180 phba->work_ha |= HA_ERATT;
12181 phba->hba_flag |= HBA_ERATT_HANDLED;
12182 return 1;
12183 }
James Smart2fcee4b2010-12-15 17:57:46 -050012184 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
12185 phba->work_status[0] =
12186 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
12187 phba->work_status[1] =
12188 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
12189 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050012190 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050012191 "port status reg 0x%x, "
12192 "port smphr reg 0x%x, "
12193 "error 1=0x%x, error 2=0x%x\n",
12194 portstat_reg.word0,
12195 portsmphr,
12196 phba->work_status[0],
12197 phba->work_status[1]);
12198 phba->work_ha |= HA_ERATT;
12199 phba->hba_flag |= HBA_ERATT_HANDLED;
12200 return 1;
12201 }
12202 break;
12203 case LPFC_SLI_INTF_IF_TYPE_1:
12204 default:
James Smarta747c9c2009-11-18 15:41:10 -050012205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050012206 "2886 HBA Error Attention on unsupported "
12207 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050012208 return 1;
James Smartda0436e2009-05-22 14:51:39 -040012209 }
James Smart2fcee4b2010-12-15 17:57:46 -050012210
James Smartda0436e2009-05-22 14:51:39 -040012211 return 0;
12212}
12213
12214/**
James Smart3621a712009-04-06 18:47:14 -040012215 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040012216 * @phba: Pointer to HBA context.
12217 *
James Smart3772a992009-05-22 14:50:54 -040012218 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040012219 * error attention register bit for error attention events.
12220 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012221 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040012222 * Register and returns 0 otherwise.
12223 **/
12224int
12225lpfc_sli_check_eratt(struct lpfc_hba *phba)
12226{
12227 uint32_t ha_copy;
12228
12229 /* If somebody is waiting to handle an eratt, don't process it
12230 * here. The brdkill function will do this.
12231 */
12232 if (phba->link_flag & LS_IGNORE_ERATT)
12233 return 0;
12234
12235 /* Check if interrupt handler handles this ERATT */
12236 spin_lock_irq(&phba->hbalock);
12237 if (phba->hba_flag & HBA_ERATT_HANDLED) {
12238 /* Interrupt handler has handled ERATT */
12239 spin_unlock_irq(&phba->hbalock);
12240 return 0;
12241 }
12242
James Smarta257bf92009-04-06 18:48:10 -040012243 /*
12244 * If there is deferred error attention, do not check for error
12245 * attention
12246 */
12247 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12248 spin_unlock_irq(&phba->hbalock);
12249 return 0;
12250 }
12251
James Smart3772a992009-05-22 14:50:54 -040012252 /* If PCI channel is offline, don't process it */
12253 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040012254 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012255 return 0;
12256 }
12257
12258 switch (phba->sli_rev) {
12259 case LPFC_SLI_REV2:
12260 case LPFC_SLI_REV3:
12261 /* Read chip Host Attention (HA) register */
12262 ha_copy = lpfc_sli_eratt_read(phba);
12263 break;
James Smartda0436e2009-05-22 14:51:39 -040012264 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050012265 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040012266 ha_copy = lpfc_sli4_eratt_read(phba);
12267 break;
James Smart3772a992009-05-22 14:50:54 -040012268 default:
12269 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12270 "0299 Invalid SLI revision (%d)\n",
12271 phba->sli_rev);
12272 ha_copy = 0;
12273 break;
James Smart93996272008-08-24 21:50:30 -040012274 }
12275 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012276
12277 return ha_copy;
12278}
12279
12280/**
12281 * lpfc_intr_state_check - Check device state for interrupt handling
12282 * @phba: Pointer to HBA context.
12283 *
12284 * This inline routine checks whether a device or its PCI slot is in a state
12285 * that the interrupt should be handled.
12286 *
12287 * This function returns 0 if the device or the PCI slot is in a state that
12288 * interrupt should be handled, otherwise -EIO.
12289 */
12290static inline int
12291lpfc_intr_state_check(struct lpfc_hba *phba)
12292{
12293 /* If the pci channel is offline, ignore all the interrupts */
12294 if (unlikely(pci_channel_offline(phba->pcidev)))
12295 return -EIO;
12296
12297 /* Update device level interrupt statistics */
12298 phba->sli.slistat.sli_intr++;
12299
12300 /* Ignore all interrupts during initialization. */
12301 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12302 return -EIO;
12303
James Smart93996272008-08-24 21:50:30 -040012304 return 0;
12305}
12306
12307/**
James Smart3772a992009-05-22 14:50:54 -040012308 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040012309 * @irq: Interrupt number.
12310 * @dev_id: The device context pointer.
12311 *
James Smart93996272008-08-24 21:50:30 -040012312 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012313 * service routine when device with SLI-3 interface spec is enabled with
12314 * MSI-X multi-message interrupt mode and there are slow-path events in
12315 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12316 * interrupt mode, this function is called as part of the device-level
12317 * interrupt handler. When the PCI slot is in error recovery or the HBA
12318 * is undergoing initialization, the interrupt handler will not process
12319 * the interrupt. The link attention and ELS ring attention events are
12320 * handled by the worker thread. The interrupt handler signals the worker
12321 * thread and returns for these events. This function is called without
12322 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040012323 * structures.
12324 *
12325 * This function returns IRQ_HANDLED when interrupt is handled else it
12326 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040012327 **/
dea31012005-04-17 16:05:31 -050012328irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012329lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050012330{
James Smart2e0fef82007-06-17 19:56:36 -050012331 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050012332 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050012333 uint32_t work_ha_copy;
12334 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012335 unsigned long iflag;
dea31012005-04-17 16:05:31 -050012336 uint32_t control;
12337
James Smart92d7f7b2007-06-17 19:56:38 -050012338 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050012339 struct lpfc_vport *vport;
12340 struct lpfc_nodelist *ndlp;
12341 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050012342 LPFC_MBOXQ_t *pmb;
12343 int rc;
12344
dea31012005-04-17 16:05:31 -050012345 /*
12346 * Get the driver's phba structure from the dev_id and
12347 * assume the HBA is not interrupting.
12348 */
James Smart93996272008-08-24 21:50:30 -040012349 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050012350
12351 if (unlikely(!phba))
12352 return IRQ_NONE;
12353
dea31012005-04-17 16:05:31 -050012354 /*
James Smart93996272008-08-24 21:50:30 -040012355 * Stuff needs to be attented to when this function is invoked as an
12356 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012357 */
James Smart93996272008-08-24 21:50:30 -040012358 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012359 /* Check device state for handling interrupt */
12360 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012361 return IRQ_NONE;
12362 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050012363 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012364 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12365 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040012366 /* If somebody is waiting to handle an eratt don't process it
12367 * here. The brdkill function will do this.
12368 */
12369 if (phba->link_flag & LS_IGNORE_ERATT)
12370 ha_copy &= ~HA_ERATT;
12371 /* Check the need for handling ERATT in interrupt handler */
12372 if (ha_copy & HA_ERATT) {
12373 if (phba->hba_flag & HBA_ERATT_HANDLED)
12374 /* ERATT polling has handled ERATT */
12375 ha_copy &= ~HA_ERATT;
12376 else
12377 /* Indicate interrupt handler handles ERATT */
12378 phba->hba_flag |= HBA_ERATT_HANDLED;
12379 }
James Smarta257bf92009-04-06 18:48:10 -040012380
12381 /*
12382 * If there is deferred error attention, do not check for any
12383 * interrupt.
12384 */
12385 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012386 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012387 return IRQ_NONE;
12388 }
12389
James Smart93996272008-08-24 21:50:30 -040012390 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050012391 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12392 goto unplug_error;
12393
James Smarta747c9c2009-11-18 15:41:10 -050012394 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12395 HC_LAINT_ENA | HC_ERINT_ENA),
12396 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012397 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12398 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012399 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012400 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012401 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012402 } else
12403 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050012404
dea31012005-04-17 16:05:31 -050012405 work_ha_copy = ha_copy & phba->work_ha_mask;
12406
James Smart93996272008-08-24 21:50:30 -040012407 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050012408 if (work_ha_copy & HA_LATT) {
12409 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12410 /*
12411 * Turn off Link Attention interrupts
12412 * until CLEAR_LA done
12413 */
James Smart5b75da22008-12-04 22:39:35 -050012414 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012415 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050012416 if (lpfc_readl(phba->HCregaddr, &control))
12417 goto unplug_error;
dea31012005-04-17 16:05:31 -050012418 control &= ~HC_LAINT_ENA;
12419 writel(control, phba->HCregaddr);
12420 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012421 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012422 }
12423 else
12424 work_ha_copy &= ~HA_LATT;
12425 }
12426
James Smart93996272008-08-24 21:50:30 -040012427 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050012428 /*
12429 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12430 * the only slow ring.
12431 */
12432 status = (work_ha_copy &
12433 (HA_RXMASK << (4*LPFC_ELS_RING)));
12434 status >>= (4*LPFC_ELS_RING);
12435 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050012436 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012437 if (lpfc_readl(phba->HCregaddr, &control))
12438 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040012439
12440 lpfc_debugfs_slow_ring_trc(phba,
12441 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12442 control, status,
12443 (uint32_t)phba->sli.slistat.sli_intr);
12444
James Smart858c9f62007-06-17 19:56:39 -050012445 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040012446 lpfc_debugfs_slow_ring_trc(phba,
12447 "ISR Disable ring:"
12448 "pwork:x%x hawork:x%x wait:x%x",
12449 phba->work_ha, work_ha_copy,
12450 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012451 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012452
James Smart858c9f62007-06-17 19:56:39 -050012453 control &=
12454 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050012455 writel(control, phba->HCregaddr);
12456 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050012457 }
James Smarta58cbd52007-08-02 11:09:43 -040012458 else {
12459 lpfc_debugfs_slow_ring_trc(phba,
12460 "ISR slow ring: pwork:"
12461 "x%x hawork:x%x wait:x%x",
12462 phba->work_ha, work_ha_copy,
12463 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012464 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012465 }
James Smart5b75da22008-12-04 22:39:35 -050012466 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012467 }
12468 }
James Smart5b75da22008-12-04 22:39:35 -050012469 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012470 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050012471 if (lpfc_sli_read_hs(phba))
12472 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040012473 /*
12474 * Check if there is a deferred error condition
12475 * is active
12476 */
12477 if ((HS_FFER1 & phba->work_hs) &&
12478 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012479 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12480 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040012481 phba->hba_flag |= DEFER_ERATT;
12482 /* Clear all interrupt enable conditions */
12483 writel(0, phba->HCregaddr);
12484 readl(phba->HCregaddr);
12485 }
12486 }
12487
James Smart93996272008-08-24 21:50:30 -040012488 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050012489 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040012490 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040012491 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050012492 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050012493
12494 /* First check out the status word */
12495 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12496 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050012497 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050012498 /*
12499 * Stray Mailbox Interrupt, mbxCommand <cmd>
12500 * mbxStatus <status>
12501 */
James Smart09372822008-01-11 01:52:54 -050012502 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012503 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012504 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012505 "Interrupt mbxCommand x%x "
12506 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012507 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012508 pmbox->mbxCommand,
12509 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012510 /* clear mailbox attention bit */
12511 work_ha_copy &= ~HA_MBATT;
12512 } else {
James Smart97eab632008-04-07 10:16:05 -040012513 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012514 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012515 phba->last_completion_time = jiffies;
12516 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012517 if (pmb->mbox_cmpl) {
12518 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12519 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012520 if (pmb->out_ext_byte_len &&
James Smart3e1f0712018-11-29 16:09:29 -080012521 pmb->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -040012522 lpfc_sli_pcimem_bcopy(
12523 phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -080012524 pmb->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -040012525 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012526 }
James Smart09372822008-01-11 01:52:54 -050012527 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12528 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12529
12530 lpfc_debugfs_disc_trc(vport,
12531 LPFC_DISC_TRC_MBOX_VPORT,
12532 "MBOX dflt rpi: : "
12533 "status:x%x rpi:x%x",
12534 (uint32_t)pmbox->mbxStatus,
12535 pmbox->un.varWords[0], 0);
12536
12537 if (!pmbox->mbxStatus) {
12538 mp = (struct lpfc_dmabuf *)
James Smart3e1f0712018-11-29 16:09:29 -080012539 (pmb->ctx_buf);
James Smart09372822008-01-11 01:52:54 -050012540 ndlp = (struct lpfc_nodelist *)
James Smart3e1f0712018-11-29 16:09:29 -080012541 pmb->ctx_ndlp;
James Smart09372822008-01-11 01:52:54 -050012542
12543 /* Reg_LOGIN of dflt RPI was
12544 * successful. new lets get
12545 * rid of the RPI using the
12546 * same mbox buffer.
12547 */
12548 lpfc_unreg_login(phba,
12549 vport->vpi,
12550 pmbox->un.varWords[0],
12551 pmb);
12552 pmb->mbox_cmpl =
12553 lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080012554 pmb->ctx_buf = mp;
12555 pmb->ctx_ndlp = ndlp;
James Smart09372822008-01-11 01:52:54 -050012556 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012557 rc = lpfc_sli_issue_mbox(phba,
12558 pmb,
12559 MBX_NOWAIT);
12560 if (rc != MBX_BUSY)
12561 lpfc_printf_log(phba,
12562 KERN_ERR,
12563 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012564 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012565 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012566 if (rc != MBX_NOT_FINISHED)
12567 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012568 }
12569 }
James Smart5b75da22008-12-04 22:39:35 -050012570 spin_lock_irqsave(
12571 &phba->pport->work_port_lock,
12572 iflag);
James Smart09372822008-01-11 01:52:54 -050012573 phba->pport->work_port_events &=
12574 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012575 spin_unlock_irqrestore(
12576 &phba->pport->work_port_lock,
12577 iflag);
James Smart09372822008-01-11 01:52:54 -050012578 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012579 }
James Smart97eab632008-04-07 10:16:05 -040012580 } else
James Smart5b75da22008-12-04 22:39:35 -050012581 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012582
James Smart92d7f7b2007-06-17 19:56:38 -050012583 if ((work_ha_copy & HA_MBATT) &&
12584 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012585send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012586 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012587 do {
12588 rc = lpfc_sli_issue_mbox(phba, NULL,
12589 MBX_NOWAIT);
12590 } while (rc == MBX_NOT_FINISHED);
12591 if (rc != MBX_SUCCESS)
12592 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12593 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012594 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012595 }
12596
James Smart5b75da22008-12-04 22:39:35 -050012597 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012598 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012599 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012600 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012601 }
James Smart93996272008-08-24 21:50:30 -040012602 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012603unplug_error:
12604 spin_unlock_irqrestore(&phba->hbalock, iflag);
12605 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012606
James Smart3772a992009-05-22 14:50:54 -040012607} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012608
12609/**
James Smart3772a992009-05-22 14:50:54 -040012610 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012611 * @irq: Interrupt number.
12612 * @dev_id: The device context pointer.
12613 *
12614 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012615 * service routine when device with SLI-3 interface spec is enabled with
12616 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12617 * ring event in the HBA. However, when the device is enabled with either
12618 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12619 * device-level interrupt handler. When the PCI slot is in error recovery
12620 * or the HBA is undergoing initialization, the interrupt handler will not
12621 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12622 * the intrrupt context. This function is called without any lock held.
12623 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012624 *
12625 * This function returns IRQ_HANDLED when interrupt is handled else it
12626 * returns IRQ_NONE.
12627 **/
12628irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012629lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012630{
12631 struct lpfc_hba *phba;
12632 uint32_t ha_copy;
12633 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012634 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012635 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012636
12637 /* Get the driver's phba structure from the dev_id and
12638 * assume the HBA is not interrupting.
12639 */
12640 phba = (struct lpfc_hba *) dev_id;
12641
12642 if (unlikely(!phba))
12643 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012644
12645 /*
James Smart93996272008-08-24 21:50:30 -040012646 * Stuff needs to be attented to when this function is invoked as an
12647 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012648 */
James Smart93996272008-08-24 21:50:30 -040012649 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012650 /* Check device state for handling interrupt */
12651 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012652 return IRQ_NONE;
12653 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012654 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12655 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012656 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012657 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012658 /*
12659 * If there is deferred error attention, do not check for
12660 * any interrupt.
12661 */
12662 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012663 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012664 return IRQ_NONE;
12665 }
James Smart93996272008-08-24 21:50:30 -040012666 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12667 phba->HAregaddr);
12668 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012669 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012670 } else
12671 ha_copy = phba->ha_copy;
12672
12673 /*
12674 * Process all events on FCP ring. Take the optimized path for FCP IO.
12675 */
12676 ha_copy &= ~(phba->work_ha_mask);
12677
12678 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012679 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012680 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012681 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012682 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012683
12684 if (phba->cfg_multi_ring_support == 2) {
12685 /*
James Smart93996272008-08-24 21:50:30 -040012686 * Process all events on extra ring. Take the optimized path
12687 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012688 */
James Smart93996272008-08-24 21:50:30 -040012689 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012690 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012691 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012692 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012693 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012694 status);
12695 }
12696 }
dea31012005-04-17 16:05:31 -050012697 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012698} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012699
James Smart93996272008-08-24 21:50:30 -040012700/**
James Smart3772a992009-05-22 14:50:54 -040012701 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012702 * @irq: Interrupt number.
12703 * @dev_id: The device context pointer.
12704 *
James Smart3772a992009-05-22 14:50:54 -040012705 * This function is the HBA device-level interrupt handler to device with
12706 * SLI-3 interface spec, called from the PCI layer when either MSI or
12707 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12708 * requires driver attention. This function invokes the slow-path interrupt
12709 * attention handling function and fast-path interrupt attention handling
12710 * function in turn to process the relevant HBA attention events. This
12711 * function is called without any lock held. It gets the hbalock to access
12712 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012713 *
12714 * This function returns IRQ_HANDLED when interrupt is handled, else it
12715 * returns IRQ_NONE.
12716 **/
12717irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012718lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012719{
12720 struct lpfc_hba *phba;
12721 irqreturn_t sp_irq_rc, fp_irq_rc;
12722 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012723 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012724
12725 /*
12726 * Get the driver's phba structure from the dev_id and
12727 * assume the HBA is not interrupting.
12728 */
12729 phba = (struct lpfc_hba *) dev_id;
12730
12731 if (unlikely(!phba))
12732 return IRQ_NONE;
12733
James Smart3772a992009-05-22 14:50:54 -040012734 /* Check device state for handling interrupt */
12735 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012736 return IRQ_NONE;
12737
12738 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012739 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12740 spin_unlock(&phba->hbalock);
12741 return IRQ_HANDLED;
12742 }
12743
James Smart93996272008-08-24 21:50:30 -040012744 if (unlikely(!phba->ha_copy)) {
12745 spin_unlock(&phba->hbalock);
12746 return IRQ_NONE;
12747 } else if (phba->ha_copy & HA_ERATT) {
12748 if (phba->hba_flag & HBA_ERATT_HANDLED)
12749 /* ERATT polling has handled ERATT */
12750 phba->ha_copy &= ~HA_ERATT;
12751 else
12752 /* Indicate interrupt handler handles ERATT */
12753 phba->hba_flag |= HBA_ERATT_HANDLED;
12754 }
12755
James Smarta257bf92009-04-06 18:48:10 -040012756 /*
12757 * If there is deferred error attention, do not check for any interrupt.
12758 */
12759 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012760 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012761 return IRQ_NONE;
12762 }
12763
James Smart93996272008-08-24 21:50:30 -040012764 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012765 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12766 spin_unlock(&phba->hbalock);
12767 return IRQ_HANDLED;
12768 }
James Smarta747c9c2009-11-18 15:41:10 -050012769 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12770 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12771 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012772 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012773 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012774 readl(phba->HAregaddr); /* flush */
12775 spin_unlock(&phba->hbalock);
12776
12777 /*
12778 * Invokes slow-path host attention interrupt handling as appropriate.
12779 */
12780
12781 /* status of events with mailbox and link attention */
12782 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12783
12784 /* status of events with ELS ring */
12785 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12786 status2 >>= (4*LPFC_ELS_RING);
12787
12788 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012789 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012790 else
12791 sp_irq_rc = IRQ_NONE;
12792
12793 /*
12794 * Invoke fast-path host attention interrupt handling as appropriate.
12795 */
12796
12797 /* status of events with FCP ring */
12798 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12799 status1 >>= (4*LPFC_FCP_RING);
12800
12801 /* status of events with extra ring */
12802 if (phba->cfg_multi_ring_support == 2) {
12803 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12804 status2 >>= (4*LPFC_EXTRA_RING);
12805 } else
12806 status2 = 0;
12807
12808 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012809 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012810 else
12811 fp_irq_rc = IRQ_NONE;
12812
12813 /* Return device-level interrupt handling status */
12814 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012815} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012816
12817/**
James Smart4f774512009-05-22 14:52:35 -040012818 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12819 * @phba: pointer to lpfc hba data structure.
12820 *
12821 * This routine is invoked by the worker thread to process all the pending
12822 * SLI4 els abort xri events.
12823 **/
12824void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12825{
12826 struct lpfc_cq_event *cq_event;
12827
12828 /* First, declare the els xri abort event has been handled */
12829 spin_lock_irq(&phba->hbalock);
12830 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12831 spin_unlock_irq(&phba->hbalock);
12832 /* Now, handle all the els xri abort events */
12833 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12834 /* Get the first event from the head of the event queue */
12835 spin_lock_irq(&phba->hbalock);
12836 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12837 cq_event, struct lpfc_cq_event, list);
12838 spin_unlock_irq(&phba->hbalock);
12839 /* Notify aborted XRI for ELS work queue */
12840 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12841 /* Free the event processed back to the free pool */
12842 lpfc_sli4_cq_event_release(phba, cq_event);
12843 }
12844}
12845
James Smart341af102010-01-26 23:07:37 -050012846/**
12847 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12848 * @phba: pointer to lpfc hba data structure
12849 * @pIocbIn: pointer to the rspiocbq
12850 * @pIocbOut: pointer to the cmdiocbq
12851 * @wcqe: pointer to the complete wcqe
12852 *
12853 * This routine transfers the fields of a command iocbq to a response iocbq
12854 * by copying all the IOCB fields from command iocbq and transferring the
12855 * completion status information from the complete wcqe.
12856 **/
James Smart4f774512009-05-22 14:52:35 -040012857static void
James Smart341af102010-01-26 23:07:37 -050012858lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12859 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012860 struct lpfc_iocbq *pIocbOut,
12861 struct lpfc_wcqe_complete *wcqe)
12862{
James Smartaf227412013-10-10 12:23:10 -040012863 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012864 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012865 uint32_t status, max_response;
12866 struct lpfc_dmabuf *dmabuf;
12867 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012868 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12869
12870 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12871 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012872 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012873 status = bf_get(lpfc_wcqe_c_status, wcqe);
12874 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012875 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12876 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12877 pIocbIn->iocb.un.fcpi.fcpi_parm =
12878 pIocbOut->iocb.un.fcpi.fcpi_parm -
12879 wcqe->total_data_placed;
12880 else
12881 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012882 else {
James Smart4f774512009-05-22 14:52:35 -040012883 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012884 switch (pIocbOut->iocb.ulpCommand) {
12885 case CMD_ELS_REQUEST64_CR:
12886 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12887 bpl = (struct ulp_bde64 *)dmabuf->virt;
12888 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12889 max_response = bde.tus.f.bdeSize;
12890 break;
12891 case CMD_GEN_REQUEST64_CR:
12892 max_response = 0;
12893 if (!pIocbOut->context3)
12894 break;
12895 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12896 sizeof(struct ulp_bde64);
12897 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12898 bpl = (struct ulp_bde64 *)dmabuf->virt;
12899 for (i = 0; i < numBdes; i++) {
12900 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12901 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12902 max_response += bde.tus.f.bdeSize;
12903 }
12904 break;
12905 default:
12906 max_response = wcqe->total_data_placed;
12907 break;
12908 }
12909 if (max_response < wcqe->total_data_placed)
12910 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12911 else
12912 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12913 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012914 }
James Smart341af102010-01-26 23:07:37 -050012915
James Smartacd68592012-01-18 16:25:09 -050012916 /* Convert BG errors for completion status */
12917 if (status == CQE_STATUS_DI_ERROR) {
12918 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12919
12920 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12921 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12922 else
12923 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12924
12925 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12926 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12927 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12928 BGS_GUARD_ERR_MASK;
12929 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12930 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12931 BGS_APPTAG_ERR_MASK;
12932 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12933 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12934 BGS_REFTAG_ERR_MASK;
12935
12936 /* Check to see if there was any good data before the error */
12937 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12938 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12939 BGS_HI_WATER_MARK_PRESENT_MASK;
12940 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12941 wcqe->total_data_placed;
12942 }
12943
12944 /*
12945 * Set ALL the error bits to indicate we don't know what
12946 * type of error it is.
12947 */
12948 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12949 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12950 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12951 BGS_GUARD_ERR_MASK);
12952 }
12953
James Smart341af102010-01-26 23:07:37 -050012954 /* Pick up HBA exchange busy condition */
12955 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12956 spin_lock_irqsave(&phba->hbalock, iflags);
12957 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12958 spin_unlock_irqrestore(&phba->hbalock, iflags);
12959 }
James Smart4f774512009-05-22 14:52:35 -040012960}
12961
12962/**
James Smart45ed1192009-10-02 15:17:02 -040012963 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12964 * @phba: Pointer to HBA context object.
12965 * @wcqe: Pointer to work-queue completion queue entry.
12966 *
12967 * This routine handles an ELS work-queue completion event and construct
12968 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12969 * discovery engine to handle.
12970 *
12971 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12972 **/
12973static struct lpfc_iocbq *
12974lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12975 struct lpfc_iocbq *irspiocbq)
12976{
James Smart895427b2017-02-12 13:52:30 -080012977 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012978 struct lpfc_iocbq *cmdiocbq;
12979 struct lpfc_wcqe_complete *wcqe;
12980 unsigned long iflags;
12981
James Smart895427b2017-02-12 13:52:30 -080012982 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070012983 if (unlikely(!pring))
12984 return NULL;
James Smart895427b2017-02-12 13:52:30 -080012985
James Smart45ed1192009-10-02 15:17:02 -040012986 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart45ed1192009-10-02 15:17:02 -040012987 pring->stats.iocb_event++;
12988 /* Look up the ELS command IOCB and create pseudo response IOCB */
12989 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12990 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040012991 if (unlikely(!cmdiocbq)) {
12992 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12993 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070012994 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12995 wcqe->word0, wcqe->total_data_placed,
12996 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040012997 lpfc_sli_release_iocbq(phba, irspiocbq);
12998 return NULL;
12999 }
13000
James Smarte2a8be52019-05-06 17:26:47 -070013001 spin_lock_irqsave(&pring->ring_lock, iflags);
Dick Kennedy401bb412017-09-29 17:34:28 -070013002 /* Put the iocb back on the txcmplq */
13003 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
13004 spin_unlock_irqrestore(&pring->ring_lock, iflags);
13005
James Smart45ed1192009-10-02 15:17:02 -040013006 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013007 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040013008
13009 return irspiocbq;
13010}
13011
James Smart8a5ca102017-11-20 16:00:30 -080013012inline struct lpfc_cq_event *
13013lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
13014{
13015 struct lpfc_cq_event *cq_event;
13016
13017 /* Allocate a new internal CQ_EVENT entry */
13018 cq_event = lpfc_sli4_cq_event_alloc(phba);
13019 if (!cq_event) {
13020 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13021 "0602 Failed to alloc CQ_EVENT entry\n");
13022 return NULL;
13023 }
13024
13025 /* Move the CQE into the event */
13026 memcpy(&cq_event->cqe, entry, size);
13027 return cq_event;
13028}
13029
James Smart45ed1192009-10-02 15:17:02 -040013030/**
James Smart04c68492009-05-22 14:52:52 -040013031 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
13032 * @phba: Pointer to HBA context object.
13033 * @cqe: Pointer to mailbox completion queue entry.
13034 *
13035 * This routine process a mailbox completion queue entry with asynchrous
13036 * event.
13037 *
13038 * Return: true if work posted to worker thread, otherwise false.
13039 **/
13040static bool
13041lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13042{
13043 struct lpfc_cq_event *cq_event;
13044 unsigned long iflags;
13045
13046 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13047 "0392 Async Event: word0:x%x, word1:x%x, "
13048 "word2:x%x, word3:x%x\n", mcqe->word0,
13049 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
13050
James Smart8a5ca102017-11-20 16:00:30 -080013051 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
13052 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040013053 return false;
James Smart04c68492009-05-22 14:52:52 -040013054 spin_lock_irqsave(&phba->hbalock, iflags);
13055 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
13056 /* Set the async event flag */
13057 phba->hba_flag |= ASYNC_EVENT;
13058 spin_unlock_irqrestore(&phba->hbalock, iflags);
13059
13060 return true;
13061}
13062
13063/**
13064 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
13065 * @phba: Pointer to HBA context object.
13066 * @cqe: Pointer to mailbox completion queue entry.
13067 *
13068 * This routine process a mailbox completion queue entry with mailbox
13069 * completion event.
13070 *
13071 * Return: true if work posted to worker thread, otherwise false.
13072 **/
13073static bool
13074lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13075{
13076 uint32_t mcqe_status;
13077 MAILBOX_t *mbox, *pmbox;
13078 struct lpfc_mqe *mqe;
13079 struct lpfc_vport *vport;
13080 struct lpfc_nodelist *ndlp;
13081 struct lpfc_dmabuf *mp;
13082 unsigned long iflags;
13083 LPFC_MBOXQ_t *pmb;
13084 bool workposted = false;
13085 int rc;
13086
13087 /* If not a mailbox complete MCQE, out by checking mailbox consume */
13088 if (!bf_get(lpfc_trailer_completed, mcqe))
13089 goto out_no_mqe_complete;
13090
13091 /* Get the reference to the active mbox command */
13092 spin_lock_irqsave(&phba->hbalock, iflags);
13093 pmb = phba->sli.mbox_active;
13094 if (unlikely(!pmb)) {
13095 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
13096 "1832 No pending MBOX command to handle\n");
13097 spin_unlock_irqrestore(&phba->hbalock, iflags);
13098 goto out_no_mqe_complete;
13099 }
13100 spin_unlock_irqrestore(&phba->hbalock, iflags);
13101 mqe = &pmb->u.mqe;
13102 pmbox = (MAILBOX_t *)&pmb->u.mqe;
13103 mbox = phba->mbox;
13104 vport = pmb->vport;
13105
13106 /* Reset heartbeat timer */
13107 phba->last_completion_time = jiffies;
13108 del_timer(&phba->sli.mbox_tmo);
13109
13110 /* Move mbox data to caller's mailbox region, do endian swapping */
13111 if (pmb->mbox_cmpl && mbox)
James Smart48f8fdb2018-05-04 20:37:51 -070013112 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040013113
James Smart73d91e52011-10-10 21:32:10 -040013114 /*
13115 * For mcqe errors, conditionally move a modified error code to
13116 * the mbox so that the error will not be missed.
13117 */
13118 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
13119 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
13120 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
13121 bf_set(lpfc_mqe_status, mqe,
13122 (LPFC_MBX_ERROR_RANGE | mcqe_status));
13123 }
James Smart04c68492009-05-22 14:52:52 -040013124 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13125 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13126 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
13127 "MBOX dflt rpi: status:x%x rpi:x%x",
13128 mcqe_status,
13129 pmbox->un.varWords[0], 0);
13130 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
James Smart3e1f0712018-11-29 16:09:29 -080013131 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
13132 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smart04c68492009-05-22 14:52:52 -040013133 /* Reg_LOGIN of dflt RPI was successful. Now lets get
13134 * RID of the PPI using the same mbox buffer.
13135 */
13136 lpfc_unreg_login(phba, vport->vpi,
13137 pmbox->un.varWords[0], pmb);
13138 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080013139 pmb->ctx_buf = mp;
13140 pmb->ctx_ndlp = ndlp;
James Smart04c68492009-05-22 14:52:52 -040013141 pmb->vport = vport;
13142 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
13143 if (rc != MBX_BUSY)
13144 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
13145 LOG_SLI, "0385 rc should "
13146 "have been MBX_BUSY\n");
13147 if (rc != MBX_NOT_FINISHED)
13148 goto send_current_mbox;
13149 }
13150 }
13151 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
13152 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
13153 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
13154
13155 /* There is mailbox completion work to do */
13156 spin_lock_irqsave(&phba->hbalock, iflags);
13157 __lpfc_mbox_cmpl_put(phba, pmb);
13158 phba->work_ha |= HA_MBATT;
13159 spin_unlock_irqrestore(&phba->hbalock, iflags);
13160 workposted = true;
13161
13162send_current_mbox:
13163 spin_lock_irqsave(&phba->hbalock, iflags);
13164 /* Release the mailbox command posting token */
13165 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13166 /* Setting active mailbox pointer need to be in sync to flag clear */
13167 phba->sli.mbox_active = NULL;
James Smart07b85822019-09-21 20:58:53 -070013168 if (bf_get(lpfc_trailer_consumed, mcqe))
13169 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
James Smart04c68492009-05-22 14:52:52 -040013170 spin_unlock_irqrestore(&phba->hbalock, iflags);
13171 /* Wake up worker thread to post the next pending mailbox command */
13172 lpfc_worker_wake_up(phba);
James Smart07b85822019-09-21 20:58:53 -070013173 return workposted;
13174
James Smart04c68492009-05-22 14:52:52 -040013175out_no_mqe_complete:
James Smart07b85822019-09-21 20:58:53 -070013176 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart04c68492009-05-22 14:52:52 -040013177 if (bf_get(lpfc_trailer_consumed, mcqe))
13178 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
James Smart07b85822019-09-21 20:58:53 -070013179 spin_unlock_irqrestore(&phba->hbalock, iflags);
13180 return false;
James Smart04c68492009-05-22 14:52:52 -040013181}
13182
13183/**
13184 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
13185 * @phba: Pointer to HBA context object.
13186 * @cqe: Pointer to mailbox completion queue entry.
13187 *
13188 * This routine process a mailbox completion queue entry, it invokes the
13189 * proper mailbox complete handling or asynchrous event handling routine
13190 * according to the MCQE's async bit.
13191 *
13192 * Return: true if work posted to worker thread, otherwise false.
13193 **/
13194static bool
James Smart32517fc2019-01-28 11:14:33 -080013195lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13196 struct lpfc_cqe *cqe)
James Smart04c68492009-05-22 14:52:52 -040013197{
13198 struct lpfc_mcqe mcqe;
13199 bool workposted;
13200
James Smart32517fc2019-01-28 11:14:33 -080013201 cq->CQ_mbox++;
13202
James Smart04c68492009-05-22 14:52:52 -040013203 /* Copy the mailbox MCQE and convert endian order as needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013204 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
James Smart04c68492009-05-22 14:52:52 -040013205
13206 /* Invoke the proper event handling routine */
13207 if (!bf_get(lpfc_trailer_async, &mcqe))
13208 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
13209 else
13210 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
13211 return workposted;
13212}
13213
13214/**
James Smart4f774512009-05-22 14:52:35 -040013215 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
13216 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040013217 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040013218 * @wcqe: Pointer to work-queue completion queue entry.
13219 *
13220 * This routine handles an ELS work-queue completion event.
13221 *
13222 * Return: true if work posted to worker thread, otherwise false.
13223 **/
13224static bool
James Smart2a76a282012-08-03 12:35:54 -040013225lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013226 struct lpfc_wcqe_complete *wcqe)
13227{
James Smart4f774512009-05-22 14:52:35 -040013228 struct lpfc_iocbq *irspiocbq;
13229 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040013230 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050013231 int txq_cnt = 0;
13232 int txcmplq_cnt = 0;
13233 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040013234
James Smart11f0e342018-05-04 20:37:57 -070013235 /* Check for response status */
13236 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13237 /* Log the error status */
13238 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13239 "0357 ELS CQE error: status=x%x: "
13240 "CQE: %08x %08x %08x %08x\n",
13241 bf_get(lpfc_wcqe_c_status, wcqe),
13242 wcqe->word0, wcqe->total_data_placed,
13243 wcqe->parameter, wcqe->word3);
13244 }
13245
James Smart45ed1192009-10-02 15:17:02 -040013246 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040013247 irspiocbq = lpfc_sli_get_iocbq(phba);
13248 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050013249 if (!list_empty(&pring->txq))
13250 txq_cnt++;
13251 if (!list_empty(&pring->txcmplq))
13252 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040013253 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040013254 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
13255 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050013256 txq_cnt, phba->iocb_cnt,
13257 fcp_txcmplq_cnt,
13258 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040013259 return false;
James Smart4f774512009-05-22 14:52:35 -040013260 }
James Smart4f774512009-05-22 14:52:35 -040013261
James Smart45ed1192009-10-02 15:17:02 -040013262 /* Save off the slow-path queue event for work thread to process */
13263 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040013264 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040013265 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013266 &phba->sli4_hba.sp_queue_event);
13267 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040013268 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013269
James Smart45ed1192009-10-02 15:17:02 -040013270 return true;
James Smart4f774512009-05-22 14:52:35 -040013271}
13272
13273/**
13274 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13275 * @phba: Pointer to HBA context object.
13276 * @wcqe: Pointer to work-queue completion queue entry.
13277 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013278 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013279 * proper WQ release routine to the slow-path WQ.
13280 **/
13281static void
13282lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13283 struct lpfc_wcqe_release *wcqe)
13284{
James Smart2e90f4b2011-12-13 13:22:37 -050013285 /* sanity check on queue memory */
13286 if (unlikely(!phba->sli4_hba.els_wq))
13287 return;
James Smart4f774512009-05-22 14:52:35 -040013288 /* Check for the slow-path ELS work queue */
13289 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13290 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13291 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13292 else
13293 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13294 "2579 Slow-path wqe consume event carries "
13295 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13296 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13297 phba->sli4_hba.els_wq->queue_id);
13298}
13299
13300/**
13301 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13302 * @phba: Pointer to HBA context object.
13303 * @cq: Pointer to a WQ completion queue.
13304 * @wcqe: Pointer to work-queue completion queue entry.
13305 *
13306 * This routine handles an XRI abort event.
13307 *
13308 * Return: true if work posted to worker thread, otherwise false.
13309 **/
13310static bool
13311lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13312 struct lpfc_queue *cq,
13313 struct sli4_wcqe_xri_aborted *wcqe)
13314{
13315 bool workposted = false;
13316 struct lpfc_cq_event *cq_event;
13317 unsigned long iflags;
13318
James Smart4f774512009-05-22 14:52:35 -040013319 switch (cq->subtype) {
James Smartc00f62e2019-08-14 16:57:11 -070013320 case LPFC_IO:
13321 lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);
13322 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
13323 /* Notify aborted XRI for NVME work queue */
13324 if (phba->nvmet_support)
13325 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13326 }
James Smart5e5b5112019-01-28 11:14:22 -080013327 workposted = false;
James Smart4f774512009-05-22 14:52:35 -040013328 break;
James Smart422c4cb2017-11-20 16:00:32 -080013329 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040013330 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080013331 cq_event = lpfc_cq_event_setup(
13332 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13333 if (!cq_event)
13334 return false;
James Smart5e5b5112019-01-28 11:14:22 -080013335 cq_event->hdwq = cq->hdwq;
James Smart4f774512009-05-22 14:52:35 -040013336 spin_lock_irqsave(&phba->hbalock, iflags);
13337 list_add_tail(&cq_event->list,
13338 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13339 /* Set the els xri abort event flag */
13340 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13341 spin_unlock_irqrestore(&phba->hbalock, iflags);
13342 workposted = true;
13343 break;
13344 default:
13345 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080013346 "0603 Invalid CQ subtype %d: "
13347 "%08x %08x %08x %08x\n",
13348 cq->subtype, wcqe->word0, wcqe->parameter,
13349 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013350 workposted = false;
13351 break;
13352 }
13353 return workposted;
13354}
13355
James Smarte817e5d2018-12-13 15:17:53 -080013356#define FC_RCTL_MDS_DIAGS 0xF4
13357
James Smart4f774512009-05-22 14:52:35 -040013358/**
James Smart4d9ab992009-10-02 15:16:39 -040013359 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040013360 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040013361 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013362 *
James Smart4d9ab992009-10-02 15:16:39 -040013363 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013364 *
13365 * Return: true if work posted to worker thread, otherwise false.
13366 **/
13367static bool
James Smart4d9ab992009-10-02 15:16:39 -040013368lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13369{
13370 bool workposted = false;
James Smarte817e5d2018-12-13 15:17:53 -080013371 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040013372 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13373 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070013374 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040013375 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040013376 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040013377 unsigned long iflags;
13378
James Smart2e90f4b2011-12-13 13:22:37 -050013379 /* sanity check on queue memory */
13380 if (unlikely(!hrq) || unlikely(!drq))
13381 return workposted;
13382
James Smart7851fe22011-07-22 18:36:52 -040013383 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13384 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13385 else
13386 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13387 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040013388 goto out;
13389
13390 status = bf_get(lpfc_rcqe_status, rcqe);
13391 switch (status) {
13392 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13393 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13394 "2537 Receive Frame Truncated!!\n");
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060013395 /* fall through */
James Smart4d9ab992009-10-02 15:16:39 -040013396 case FC_STATUS_RQ_SUCCESS:
13397 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013398 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040013399 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13400 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040013401 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040013402 spin_unlock_irqrestore(&phba->hbalock, iflags);
13403 goto out;
13404 }
James Smartb84daac2012-08-03 12:35:13 -040013405 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013406 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040013407 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080013408
James Smarte817e5d2018-12-13 15:17:53 -080013409 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13410
13411 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
13412 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
13413 spin_unlock_irqrestore(&phba->hbalock, iflags);
13414 /* Handle MDS Loopback frames */
13415 lpfc_sli4_handle_mds_loopback(phba->pport, dma_buf);
13416 break;
13417 }
13418
13419 /* save off the frame for the work thread to process */
James Smart4d9ab992009-10-02 15:16:39 -040013420 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013421 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040013422 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040013423 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040013424 spin_unlock_irqrestore(&phba->hbalock, iflags);
13425 workposted = true;
13426 break;
James Smart4d9ab992009-10-02 15:16:39 -040013427 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013428 if (phba->nvmet_support) {
13429 tgtp = phba->targetport->private;
13430 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13431 "6402 RQE Error x%x, posted %d err_cnt "
13432 "%d: %x %x %x\n",
13433 status, hrq->RQ_buf_posted,
13434 hrq->RQ_no_posted_buf,
13435 atomic_read(&tgtp->rcv_fcp_cmd_in),
13436 atomic_read(&tgtp->rcv_fcp_cmd_out),
13437 atomic_read(&tgtp->xmt_fcp_release));
13438 }
13439 /* fallthrough */
13440
13441 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040013442 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040013443 /* Post more buffers if possible */
13444 spin_lock_irqsave(&phba->hbalock, iflags);
13445 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13446 spin_unlock_irqrestore(&phba->hbalock, iflags);
13447 workposted = true;
13448 break;
13449 }
13450out:
13451 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040013452}
13453
13454/**
13455 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13456 * @phba: Pointer to HBA context object.
13457 * @cq: Pointer to the completion queue.
James Smart32517fc2019-01-28 11:14:33 -080013458 * @cqe: Pointer to a completion queue entry.
James Smart4d9ab992009-10-02 15:16:39 -040013459 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013460 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040013461 * entry.
13462 *
13463 * Return: true if work posted to worker thread, otherwise false.
13464 **/
13465static bool
13466lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013467 struct lpfc_cqe *cqe)
13468{
James Smart45ed1192009-10-02 15:17:02 -040013469 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040013470 bool workposted = false;
13471
13472 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013473 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013474
13475 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040013476 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040013477 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040013478 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040013479 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040013480 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013481 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013482 break;
13483 case CQE_CODE_RELEASE_WQE:
13484 /* Process the WQ release event */
13485 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013486 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013487 break;
13488 case CQE_CODE_XRI_ABORTED:
13489 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013490 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013491 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013492 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013493 break;
James Smart4d9ab992009-10-02 15:16:39 -040013494 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013495 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013496 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013497 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013498 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013499 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013500 break;
James Smart4f774512009-05-22 14:52:35 -040013501 default:
13502 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13503 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013504 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013505 break;
13506 }
13507 return workposted;
13508}
13509
13510/**
James Smart4f774512009-05-22 14:52:35 -040013511 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13512 * @phba: Pointer to HBA context object.
13513 * @eqe: Pointer to fast-path event queue entry.
13514 *
13515 * This routine process a event queue entry from the slow-path event queue.
13516 * It will check the MajorCode and MinorCode to determine this is for a
13517 * completion event on a completion queue, if not, an error shall be logged
13518 * and just return. Otherwise, it will get to the corresponding completion
13519 * queue and process all the entries on that completion queue, rearm the
13520 * completion queue, and then return.
13521 *
13522 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013523static void
James Smart67d12732012-08-03 12:36:13 -040013524lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13525 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013526{
James Smart67d12732012-08-03 12:36:13 -040013527 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013528 uint16_t cqid;
13529
James Smart4f774512009-05-22 14:52:35 -040013530 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013531 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013532
James Smart4f774512009-05-22 14:52:35 -040013533 list_for_each_entry(childq, &speq->child_list, list) {
13534 if (childq->queue_id == cqid) {
13535 cq = childq;
13536 break;
13537 }
13538 }
13539 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013540 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13541 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13542 "0365 Slow-path CQ identifier "
13543 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013544 return;
James Smart4f774512009-05-22 14:52:35 -040013545 }
13546
James Smart895427b2017-02-12 13:52:30 -080013547 /* Save EQ associated with this CQ */
13548 cq->assoc_qp = speq;
13549
James Smart6a828b02019-01-28 11:14:31 -080013550 if (!queue_work_on(cq->chann, phba->wq, &cq->spwork))
Dick Kennedyf485c182017-09-29 17:34:34 -070013551 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13552 "0390 Cannot schedule soft IRQ "
13553 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
Bart Van Assched6d189c2019-03-28 11:06:22 -070013554 cqid, cq->queue_id, raw_smp_processor_id());
Dick Kennedyf485c182017-09-29 17:34:34 -070013555}
13556
13557/**
James Smart32517fc2019-01-28 11:14:33 -080013558 * __lpfc_sli4_process_cq - Process elements of a CQ
Dick Kennedyf485c182017-09-29 17:34:34 -070013559 * @phba: Pointer to HBA context object.
James Smart32517fc2019-01-28 11:14:33 -080013560 * @cq: Pointer to CQ to be processed
13561 * @handler: Routine to process each cqe
13562 * @delay: Pointer to usdelay to set in case of rescheduling of the handler
Dick Kennedyf485c182017-09-29 17:34:34 -070013563 *
James Smart32517fc2019-01-28 11:14:33 -080013564 * This routine processes completion queue entries in a CQ. While a valid
13565 * queue element is found, the handler is called. During processing checks
13566 * are made for periodic doorbell writes to let the hardware know of
13567 * element consumption.
Dick Kennedyf485c182017-09-29 17:34:34 -070013568 *
James Smart32517fc2019-01-28 11:14:33 -080013569 * If the max limit on cqes to process is hit, or there are no more valid
13570 * entries, the loop stops. If we processed a sufficient number of elements,
13571 * meaning there is sufficient load, rather than rearming and generating
13572 * another interrupt, a cq rescheduling delay will be set. A delay of 0
13573 * indicates no rescheduling.
13574 *
13575 * Returns True if work scheduled, False otherwise.
Dick Kennedyf485c182017-09-29 17:34:34 -070013576 **/
James Smart32517fc2019-01-28 11:14:33 -080013577static bool
13578__lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
13579 bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
13580 struct lpfc_cqe *), unsigned long *delay)
Dick Kennedyf485c182017-09-29 17:34:34 -070013581{
Dick Kennedyf485c182017-09-29 17:34:34 -070013582 struct lpfc_cqe *cqe;
13583 bool workposted = false;
James Smart32517fc2019-01-28 11:14:33 -080013584 int count = 0, consumed = 0;
13585 bool arm = true;
13586
13587 /* default - no reschedule */
13588 *delay = 0;
13589
13590 if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
13591 goto rearm_and_exit;
Dick Kennedyf485c182017-09-29 17:34:34 -070013592
James Smart4f774512009-05-22 14:52:35 -040013593 /* Process all the entries to the CQ */
James Smartd74a89a2019-05-21 17:48:55 -070013594 cq->q_flag = 0;
James Smart32517fc2019-01-28 11:14:33 -080013595 cqe = lpfc_sli4_cq_get(cq);
13596 while (cqe) {
James Smart32517fc2019-01-28 11:14:33 -080013597 workposted |= handler(phba, cq, cqe);
13598 __lpfc_sli4_consume_cqe(phba, cq, cqe);
13599
13600 consumed++;
13601 if (!(++count % cq->max_proc_limit))
13602 break;
13603
13604 if (!(count % cq->notify_interval)) {
13605 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
13606 LPFC_QUEUE_NOARM);
13607 consumed = 0;
James Smart4f774512009-05-22 14:52:35 -040013608 }
James Smartb84daac2012-08-03 12:35:13 -040013609
James Smartd74a89a2019-05-21 17:48:55 -070013610 if (count == LPFC_NVMET_CQ_NOTIFY)
13611 cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
13612
James Smart32517fc2019-01-28 11:14:33 -080013613 cqe = lpfc_sli4_cq_get(cq);
13614 }
13615 if (count >= phba->cfg_cq_poll_threshold) {
13616 *delay = 1;
13617 arm = false;
13618 }
13619
13620 /* Track the max number of CQEs processed in 1 EQ */
13621 if (count > cq->CQ_max_cqe)
13622 cq->CQ_max_cqe = count;
13623
13624 cq->assoc_qp->EQ_cqe_cnt += count;
13625
13626 /* Catch the no cq entry condition */
13627 if (unlikely(count == 0))
13628 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13629 "0369 No entry from completion queue "
13630 "qid=%d\n", cq->queue_id);
13631
13632 cq->queue_claimed = 0;
13633
13634rearm_and_exit:
13635 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
13636 arm ? LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
13637
13638 return workposted;
13639}
13640
13641/**
13642 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13643 * @cq: pointer to CQ to process
13644 *
13645 * This routine calls the cq processing routine with a handler specific
13646 * to the type of queue bound to it.
13647 *
13648 * The CQ routine returns two values: the first is the calling status,
13649 * which indicates whether work was queued to the background discovery
13650 * thread. If true, the routine should wakeup the discovery thread;
13651 * the second is the delay parameter. If non-zero, rather than rearming
13652 * the CQ and yet another interrupt, the CQ handler should be queued so
13653 * that it is processed in a subsequent polling action. The value of
13654 * the delay indicates when to reschedule it.
13655 **/
13656static void
13657__lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
13658{
13659 struct lpfc_hba *phba = cq->phba;
13660 unsigned long delay;
13661 bool workposted = false;
13662
13663 /* Process and rearm the CQ */
13664 switch (cq->type) {
13665 case LPFC_MCQ:
13666 workposted |= __lpfc_sli4_process_cq(phba, cq,
13667 lpfc_sli4_sp_handle_mcqe,
13668 &delay);
13669 break;
13670 case LPFC_WCQ:
James Smartc00f62e2019-08-14 16:57:11 -070013671 if (cq->subtype == LPFC_IO)
James Smart32517fc2019-01-28 11:14:33 -080013672 workposted |= __lpfc_sli4_process_cq(phba, cq,
13673 lpfc_sli4_fp_handle_cqe,
13674 &delay);
13675 else
13676 workposted |= __lpfc_sli4_process_cq(phba, cq,
13677 lpfc_sli4_sp_handle_cqe,
13678 &delay);
James Smart4f774512009-05-22 14:52:35 -040013679 break;
13680 default:
13681 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13682 "0370 Invalid completion queue type (%d)\n",
13683 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013684 return;
James Smart4f774512009-05-22 14:52:35 -040013685 }
13686
James Smart32517fc2019-01-28 11:14:33 -080013687 if (delay) {
13688 if (!queue_delayed_work_on(cq->chann, phba->wq,
13689 &cq->sched_spwork, delay))
13690 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13691 "0394 Cannot schedule soft IRQ "
13692 "for cqid=%d on CPU %d\n",
13693 cq->queue_id, cq->chann);
13694 }
James Smart4f774512009-05-22 14:52:35 -040013695
13696 /* wake up worker thread if there are works to be done */
13697 if (workposted)
13698 lpfc_worker_wake_up(phba);
13699}
13700
13701/**
James Smart32517fc2019-01-28 11:14:33 -080013702 * lpfc_sli4_sp_process_cq - slow-path work handler when started by
13703 * interrupt
13704 * @work: pointer to work element
13705 *
13706 * translates from the work handler and calls the slow-path handler.
13707 **/
13708static void
13709lpfc_sli4_sp_process_cq(struct work_struct *work)
13710{
13711 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
13712
13713 __lpfc_sli4_sp_process_cq(cq);
13714}
13715
13716/**
13717 * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
13718 * @work: pointer to work element
13719 *
13720 * translates from the work handler and calls the slow-path handler.
13721 **/
13722static void
13723lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
13724{
13725 struct lpfc_queue *cq = container_of(to_delayed_work(work),
13726 struct lpfc_queue, sched_spwork);
13727
13728 __lpfc_sli4_sp_process_cq(cq);
13729}
13730
13731/**
James Smart4f774512009-05-22 14:52:35 -040013732 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013733 * @phba: Pointer to HBA context object.
13734 * @cq: Pointer to associated CQ
13735 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013736 *
13737 * This routine process a fast-path work queue completion entry from fast-path
13738 * event queue for FCP command response completion.
13739 **/
13740static void
James Smart2a76a282012-08-03 12:35:54 -040013741lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013742 struct lpfc_wcqe_complete *wcqe)
13743{
James Smart2a76a282012-08-03 12:35:54 -040013744 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013745 struct lpfc_iocbq *cmdiocbq;
13746 struct lpfc_iocbq irspiocbq;
13747 unsigned long iflags;
13748
James Smart4f774512009-05-22 14:52:35 -040013749 /* Check for response status */
13750 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13751 /* If resource errors reported from HBA, reduce queue
13752 * depth of the SCSI device.
13753 */
James Smarte3d2b802012-08-14 14:25:43 -040013754 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13755 IOSTAT_LOCAL_REJECT)) &&
13756 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13757 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013758 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013759
James Smart4f774512009-05-22 14:52:35 -040013760 /* Log the error status */
James Smart11f0e342018-05-04 20:37:57 -070013761 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13762 "0373 FCP CQE error: status=x%x: "
13763 "CQE: %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013764 bf_get(lpfc_wcqe_c_status, wcqe),
James Smart11f0e342018-05-04 20:37:57 -070013765 wcqe->word0, wcqe->total_data_placed,
13766 wcqe->parameter, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013767 }
13768
13769 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013770 spin_lock_irqsave(&pring->ring_lock, iflags);
13771 pring->stats.iocb_event++;
James Smarte2a8be52019-05-06 17:26:47 -070013772 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013773 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13774 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart4f774512009-05-22 14:52:35 -040013775 if (unlikely(!cmdiocbq)) {
13776 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13777 "0374 FCP complete with no corresponding "
13778 "cmdiocb: iotag (%d)\n",
13779 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13780 return;
13781 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013782#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13783 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13784#endif
James Smart895427b2017-02-12 13:52:30 -080013785 if (cmdiocbq->iocb_cmpl == NULL) {
13786 if (cmdiocbq->wqe_cmpl) {
13787 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13788 spin_lock_irqsave(&phba->hbalock, iflags);
13789 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13790 spin_unlock_irqrestore(&phba->hbalock, iflags);
13791 }
13792
13793 /* Pass the cmd_iocb and the wcqe to the upper layer */
13794 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13795 return;
13796 }
James Smart4f774512009-05-22 14:52:35 -040013797 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13798 "0375 FCP cmdiocb not callback function "
13799 "iotag: (%d)\n",
13800 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13801 return;
13802 }
13803
13804 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013805 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013806
James Smart0f65ff62010-02-26 14:14:23 -050013807 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13808 spin_lock_irqsave(&phba->hbalock, iflags);
13809 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13810 spin_unlock_irqrestore(&phba->hbalock, iflags);
13811 }
13812
James Smart4f774512009-05-22 14:52:35 -040013813 /* Pass the cmd_iocb and the rsp state to the upper layer */
13814 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13815}
13816
13817/**
13818 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13819 * @phba: Pointer to HBA context object.
13820 * @cq: Pointer to completion queue.
13821 * @wcqe: Pointer to work-queue completion queue entry.
13822 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013823 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013824 * proper WQ release routine to the slow-path WQ.
13825 **/
13826static void
13827lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13828 struct lpfc_wcqe_release *wcqe)
13829{
13830 struct lpfc_queue *childwq;
13831 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013832 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013833
13834 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013835 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013836 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013837 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013838 lpfc_sli4_wq_release(childwq,
13839 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080013840 if (childwq->q_flag & HBA_NVMET_WQFULL)
13841 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040013842 wqid_matched = true;
13843 break;
13844 }
13845 }
13846 /* Report warning log message if no match found */
13847 if (wqid_matched != true)
13848 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13849 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013850 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013851}
13852
13853/**
James Smart2d7dbc42017-02-12 13:52:35 -080013854 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13855 * @phba: Pointer to HBA context object.
13856 * @rcqe: Pointer to receive-queue completion queue entry.
13857 *
13858 * This routine process a receive-queue completion queue entry.
13859 *
13860 * Return: true if work posted to worker thread, otherwise false.
13861 **/
13862static bool
13863lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13864 struct lpfc_rcqe *rcqe)
13865{
13866 bool workposted = false;
13867 struct lpfc_queue *hrq;
13868 struct lpfc_queue *drq;
13869 struct rqb_dmabuf *dma_buf;
13870 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013871 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013872 uint32_t status, rq_id;
13873 unsigned long iflags;
13874 uint32_t fctl, idx;
13875
13876 if ((phba->nvmet_support == 0) ||
13877 (phba->sli4_hba.nvmet_cqset == NULL))
13878 return workposted;
13879
13880 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13881 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13882 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13883
13884 /* sanity check on queue memory */
13885 if (unlikely(!hrq) || unlikely(!drq))
13886 return workposted;
13887
13888 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13889 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13890 else
13891 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13892
13893 if ((phba->nvmet_support == 0) ||
13894 (rq_id != hrq->queue_id))
13895 return workposted;
13896
13897 status = bf_get(lpfc_rcqe_status, rcqe);
13898 switch (status) {
13899 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13900 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13901 "6126 Receive Frame Truncated!!\n");
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060013902 /* fall through */
James Smart2d7dbc42017-02-12 13:52:35 -080013903 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080013904 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013905 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080013906 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13907 if (!dma_buf) {
13908 hrq->RQ_no_buf_found++;
13909 spin_unlock_irqrestore(&phba->hbalock, iflags);
13910 goto out;
13911 }
13912 spin_unlock_irqrestore(&phba->hbalock, iflags);
13913 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013914 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013915 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13916
13917 /* Just some basic sanity checks on FCP Command frame */
13918 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13919 fc_hdr->fh_f_ctl[1] << 8 |
13920 fc_hdr->fh_f_ctl[2]);
13921 if (((fctl &
13922 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13923 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13924 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13925 goto drop;
13926
13927 if (fc_hdr->fh_type == FC_TYPE_FCP) {
James Smartd74a89a2019-05-21 17:48:55 -070013928 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013929 lpfc_nvmet_unsol_fcp_event(
James Smartd74a89a2019-05-21 17:48:55 -070013930 phba, idx, dma_buf, cq->isr_timestamp,
13931 cq->q_flag & HBA_NVMET_CQ_NOTIFY);
James Smart2d7dbc42017-02-12 13:52:35 -080013932 return false;
13933 }
13934drop:
James Smart22b738a2019-03-12 16:30:11 -070013935 lpfc_rq_buf_free(phba, &dma_buf->hbuf);
James Smart2d7dbc42017-02-12 13:52:35 -080013936 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013937 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013938 if (phba->nvmet_support) {
13939 tgtp = phba->targetport->private;
13940 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13941 "6401 RQE Error x%x, posted %d err_cnt "
13942 "%d: %x %x %x\n",
13943 status, hrq->RQ_buf_posted,
13944 hrq->RQ_no_posted_buf,
13945 atomic_read(&tgtp->rcv_fcp_cmd_in),
13946 atomic_read(&tgtp->rcv_fcp_cmd_out),
13947 atomic_read(&tgtp->xmt_fcp_release));
13948 }
13949 /* fallthrough */
13950
13951 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013952 hrq->RQ_no_posted_buf++;
13953 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013954 break;
13955 }
13956out:
13957 return workposted;
13958}
13959
13960/**
James Smart895427b2017-02-12 13:52:30 -080013961 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart32517fc2019-01-28 11:14:33 -080013962 * @phba: adapter with cq
James Smart4f774512009-05-22 14:52:35 -040013963 * @cq: Pointer to the completion queue.
13964 * @eqe: Pointer to fast-path completion queue entry.
13965 *
13966 * This routine process a fast-path work queue completion entry from fast-path
13967 * event queue for FCP command response completion.
James Smart32517fc2019-01-28 11:14:33 -080013968 *
13969 * Return: true if work posted to worker thread, otherwise false.
James Smart4f774512009-05-22 14:52:35 -040013970 **/
James Smart32517fc2019-01-28 11:14:33 -080013971static bool
James Smart895427b2017-02-12 13:52:30 -080013972lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013973 struct lpfc_cqe *cqe)
13974{
13975 struct lpfc_wcqe_release wcqe;
13976 bool workposted = false;
13977
13978 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013979 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013980
13981 /* Check and process for different type of WCQE and dispatch */
13982 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13983 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013984 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013985 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013986 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013987 phba->last_completion_time = jiffies;
James Smartc00f62e2019-08-14 16:57:11 -070013988 if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)
James Smart895427b2017-02-12 13:52:30 -080013989 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013990 (struct lpfc_wcqe_complete *)&wcqe);
13991 break;
13992 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013993 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013994 /* Process the WQ release event */
13995 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13996 (struct lpfc_wcqe_release *)&wcqe);
13997 break;
13998 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013999 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040014000 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040014001 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040014002 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14003 (struct sli4_wcqe_xri_aborted *)&wcqe);
14004 break;
James Smart895427b2017-02-12 13:52:30 -080014005 case CQE_CODE_RECEIVE_V1:
14006 case CQE_CODE_RECEIVE:
14007 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080014008 if (cq->subtype == LPFC_NVMET) {
14009 workposted = lpfc_sli4_nvmet_handle_rcqe(
14010 phba, cq, (struct lpfc_rcqe *)&wcqe);
14011 }
James Smart895427b2017-02-12 13:52:30 -080014012 break;
James Smart4f774512009-05-22 14:52:35 -040014013 default:
14014 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080014015 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040014016 bf_get(lpfc_wcqe_c_code, &wcqe));
14017 break;
14018 }
14019 return workposted;
14020}
14021
14022/**
James Smart67d12732012-08-03 12:36:13 -040014023 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040014024 * @phba: Pointer to HBA context object.
14025 * @eqe: Pointer to fast-path event queue entry.
14026 *
14027 * This routine process a event queue entry from the fast-path event queue.
14028 * It will check the MajorCode and MinorCode to determine this is for a
14029 * completion event on a completion queue, if not, an error shall be logged
14030 * and just return. Otherwise, it will get to the corresponding completion
14031 * queue and process all the entries on the completion queue, rearm the
14032 * completion queue, and then return.
14033 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070014034static void
James Smart32517fc2019-01-28 11:14:33 -080014035lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
14036 struct lpfc_eqe *eqe)
James Smart4f774512009-05-22 14:52:35 -040014037{
James Smart895427b2017-02-12 13:52:30 -080014038 struct lpfc_queue *cq = NULL;
James Smart32517fc2019-01-28 11:14:33 -080014039 uint32_t qidx = eq->hdwq;
James Smart2d7dbc42017-02-12 13:52:35 -080014040 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040014041
James Smartcb5172e2010-03-15 11:25:07 -040014042 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040014043 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040014044 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040014045 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040014046 bf_get_le32(lpfc_eqe_major_code, eqe),
14047 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070014048 return;
James Smart4f774512009-05-22 14:52:35 -040014049 }
14050
James Smart67d12732012-08-03 12:36:13 -040014051 /* Get the reference to the corresponding CQ */
14052 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14053
James Smart6a828b02019-01-28 11:14:31 -080014054 /* Use the fast lookup method first */
14055 if (cqid <= phba->sli4_hba.cq_max) {
14056 cq = phba->sli4_hba.cq_lookup[cqid];
14057 if (cq)
14058 goto work_cq;
James Smartcdb42be2019-01-28 11:14:21 -080014059 }
14060
14061 /* Next check for NVMET completion */
James Smart2d7dbc42017-02-12 13:52:35 -080014062 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
14063 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
14064 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
14065 /* Process NVMET unsol rcv */
14066 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
14067 goto process_cq;
14068 }
14069 }
14070
James Smart895427b2017-02-12 13:52:30 -080014071 if (phba->sli4_hba.nvmels_cq &&
14072 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
14073 /* Process NVME unsol rcv */
14074 cq = phba->sli4_hba.nvmels_cq;
14075 }
14076
14077 /* Otherwise this is a Slow path event */
14078 if (cq == NULL) {
James Smartcdb42be2019-01-28 11:14:21 -080014079 lpfc_sli4_sp_handle_eqe(phba, eqe,
14080 phba->sli4_hba.hdwq[qidx].hba_eq);
Dick Kennedyf485c182017-09-29 17:34:34 -070014081 return;
James Smart67d12732012-08-03 12:36:13 -040014082 }
14083
James Smart895427b2017-02-12 13:52:30 -080014084process_cq:
James Smart4f774512009-05-22 14:52:35 -040014085 if (unlikely(cqid != cq->queue_id)) {
14086 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14087 "0368 Miss-matched fast-path completion "
14088 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
14089 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070014090 return;
James Smart4f774512009-05-22 14:52:35 -040014091 }
14092
James Smart6a828b02019-01-28 11:14:31 -080014093work_cq:
James Smartd74a89a2019-05-21 17:48:55 -070014094#if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
14095 if (phba->ktime_on)
14096 cq->isr_timestamp = ktime_get_ns();
14097 else
14098 cq->isr_timestamp = 0;
14099#endif
James Smart45aa3122019-01-28 11:14:29 -080014100 if (!queue_work_on(cq->chann, phba->wq, &cq->irqwork))
Dick Kennedyf485c182017-09-29 17:34:34 -070014101 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14102 "0363 Cannot schedule soft IRQ "
14103 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
Bart Van Assched6d189c2019-03-28 11:06:22 -070014104 cqid, cq->queue_id, raw_smp_processor_id());
Dick Kennedyf485c182017-09-29 17:34:34 -070014105}
14106
14107/**
James Smart32517fc2019-01-28 11:14:33 -080014108 * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
14109 * @cq: Pointer to CQ to be processed
Dick Kennedyf485c182017-09-29 17:34:34 -070014110 *
James Smart32517fc2019-01-28 11:14:33 -080014111 * This routine calls the cq processing routine with the handler for
14112 * fast path CQEs.
14113 *
14114 * The CQ routine returns two values: the first is the calling status,
14115 * which indicates whether work was queued to the background discovery
14116 * thread. If true, the routine should wakeup the discovery thread;
14117 * the second is the delay parameter. If non-zero, rather than rearming
14118 * the CQ and yet another interrupt, the CQ handler should be queued so
14119 * that it is processed in a subsequent polling action. The value of
14120 * the delay indicates when to reschedule it.
Dick Kennedyf485c182017-09-29 17:34:34 -070014121 **/
14122static void
James Smart32517fc2019-01-28 11:14:33 -080014123__lpfc_sli4_hba_process_cq(struct lpfc_queue *cq)
Dick Kennedyf485c182017-09-29 17:34:34 -070014124{
Dick Kennedyf485c182017-09-29 17:34:34 -070014125 struct lpfc_hba *phba = cq->phba;
James Smart32517fc2019-01-28 11:14:33 -080014126 unsigned long delay;
Dick Kennedyf485c182017-09-29 17:34:34 -070014127 bool workposted = false;
Dick Kennedyf485c182017-09-29 17:34:34 -070014128
James Smart32517fc2019-01-28 11:14:33 -080014129 /* process and rearm the CQ */
14130 workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
14131 &delay);
14132
14133 if (delay) {
14134 if (!queue_delayed_work_on(cq->chann, phba->wq,
14135 &cq->sched_irqwork, delay))
14136 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14137 "0367 Cannot schedule soft IRQ "
14138 "for cqid=%d on CPU %d\n",
14139 cq->queue_id, cq->chann);
James Smart4f774512009-05-22 14:52:35 -040014140 }
14141
James Smart4f774512009-05-22 14:52:35 -040014142 /* wake up worker thread if there are works to be done */
14143 if (workposted)
14144 lpfc_worker_wake_up(phba);
14145}
14146
James Smart1ba981f2014-02-20 09:56:45 -050014147/**
James Smart32517fc2019-01-28 11:14:33 -080014148 * lpfc_sli4_hba_process_cq - fast-path work handler when started by
14149 * interrupt
14150 * @work: pointer to work element
James Smart1ba981f2014-02-20 09:56:45 -050014151 *
James Smart32517fc2019-01-28 11:14:33 -080014152 * translates from the work handler and calls the fast-path handler.
James Smart1ba981f2014-02-20 09:56:45 -050014153 **/
14154static void
James Smart32517fc2019-01-28 11:14:33 -080014155lpfc_sli4_hba_process_cq(struct work_struct *work)
James Smart1ba981f2014-02-20 09:56:45 -050014156{
James Smart32517fc2019-01-28 11:14:33 -080014157 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
James Smart1ba981f2014-02-20 09:56:45 -050014158
James Smart32517fc2019-01-28 11:14:33 -080014159 __lpfc_sli4_hba_process_cq(cq);
James Smart1ba981f2014-02-20 09:56:45 -050014160}
14161
14162/**
James Smart32517fc2019-01-28 11:14:33 -080014163 * lpfc_sli4_hba_process_cq - fast-path work handler when started by timer
14164 * @work: pointer to work element
James Smart1ba981f2014-02-20 09:56:45 -050014165 *
James Smart32517fc2019-01-28 11:14:33 -080014166 * translates from the work handler and calls the fast-path handler.
James Smart1ba981f2014-02-20 09:56:45 -050014167 **/
James Smart32517fc2019-01-28 11:14:33 -080014168static void
14169lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
James Smart1ba981f2014-02-20 09:56:45 -050014170{
James Smart32517fc2019-01-28 11:14:33 -080014171 struct lpfc_queue *cq = container_of(to_delayed_work(work),
14172 struct lpfc_queue, sched_irqwork);
James Smart1ba981f2014-02-20 09:56:45 -050014173
James Smart32517fc2019-01-28 11:14:33 -080014174 __lpfc_sli4_hba_process_cq(cq);
James Smart1ba981f2014-02-20 09:56:45 -050014175}
14176
James Smart4f774512009-05-22 14:52:35 -040014177/**
James Smart67d12732012-08-03 12:36:13 -040014178 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040014179 * @irq: Interrupt number.
14180 * @dev_id: The device context pointer.
14181 *
14182 * This function is directly called from the PCI layer as an interrupt
14183 * service routine when device with SLI-4 interface spec is enabled with
14184 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
14185 * ring event in the HBA. However, when the device is enabled with either
14186 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14187 * device-level interrupt handler. When the PCI slot is in error recovery
14188 * or the HBA is undergoing initialization, the interrupt handler will not
14189 * process the interrupt. The SCSI FCP fast-path ring event are handled in
14190 * the intrrupt context. This function is called without any lock held.
14191 * It gets the hbalock to access and update SLI data structures. Note that,
14192 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14193 * equal to that of FCP CQ index.
14194 *
James Smart67d12732012-08-03 12:36:13 -040014195 * The link attention and ELS ring attention events are handled
14196 * by the worker thread. The interrupt handler signals the worker thread
14197 * and returns for these events. This function is called without any lock
14198 * held. It gets the hbalock to access and update SLI data structures.
14199 *
James Smart4f774512009-05-22 14:52:35 -040014200 * This function returns IRQ_HANDLED when interrupt is handled else it
14201 * returns IRQ_NONE.
14202 **/
14203irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040014204lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040014205{
14206 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080014207 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040014208 struct lpfc_queue *fpeq;
James Smart4f774512009-05-22 14:52:35 -040014209 unsigned long iflag;
14210 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080014211 int hba_eqidx;
James Smart32517fc2019-01-28 11:14:33 -080014212 struct lpfc_eq_intr_info *eqi;
14213 uint32_t icnt;
James Smart4f774512009-05-22 14:52:35 -040014214
14215 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080014216 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14217 phba = hba_eq_hdl->phba;
14218 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040014219
14220 if (unlikely(!phba))
14221 return IRQ_NONE;
James Smartcdb42be2019-01-28 11:14:21 -080014222 if (unlikely(!phba->sli4_hba.hdwq))
James Smart5350d872011-10-10 21:33:49 -040014223 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014224
14225 /* Get to the EQ struct associated with this vector */
James Smart657add42019-05-21 17:49:06 -070014226 fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
James Smart2e90f4b2011-12-13 13:22:37 -050014227 if (unlikely(!fpeq))
14228 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014229
14230 /* Check device state for handling interrupt */
14231 if (unlikely(lpfc_intr_state_check(phba))) {
14232 /* Check again for link_state with lock held */
14233 spin_lock_irqsave(&phba->hbalock, iflag);
14234 if (phba->link_state < LPFC_LINK_DOWN)
14235 /* Flush, clear interrupt, and rearm the EQ */
14236 lpfc_sli4_eq_flush(phba, fpeq);
14237 spin_unlock_irqrestore(&phba->hbalock, iflag);
14238 return IRQ_NONE;
14239 }
14240
James Smart32517fc2019-01-28 11:14:33 -080014241 eqi = phba->sli4_hba.eq_info;
14242 icnt = this_cpu_inc_return(eqi->icnt);
Bart Van Assched6d189c2019-03-28 11:06:22 -070014243 fpeq->last_cpu = raw_smp_processor_id();
James Smart4f774512009-05-22 14:52:35 -040014244
James Smart32517fc2019-01-28 11:14:33 -080014245 if (icnt > LPFC_EQD_ISR_TRIGGER &&
14246 phba->cfg_irq_chann == 1 &&
14247 phba->cfg_auto_imax &&
14248 fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
14249 phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
14250 lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
James Smartb84daac2012-08-03 12:35:13 -040014251
James Smart32517fc2019-01-28 11:14:33 -080014252 /* process and rearm the EQ */
14253 ecount = lpfc_sli4_process_eq(phba, fpeq);
James Smart4f774512009-05-22 14:52:35 -040014254
14255 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040014256 fpeq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040014257 if (phba->intr_type == MSIX)
14258 /* MSI-X treated interrupt served as no EQ share INT */
14259 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14260 "0358 MSI-X interrupt with no EQE\n");
14261 else
14262 /* Non MSI-X treated on interrupt as EQ share INT */
14263 return IRQ_NONE;
14264 }
14265
14266 return IRQ_HANDLED;
14267} /* lpfc_sli4_fp_intr_handler */
14268
14269/**
14270 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14271 * @irq: Interrupt number.
14272 * @dev_id: The device context pointer.
14273 *
14274 * This function is the device-level interrupt handler to device with SLI-4
14275 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14276 * interrupt mode is enabled and there is an event in the HBA which requires
14277 * driver attention. This function invokes the slow-path interrupt attention
14278 * handling function and fast-path interrupt attention handling function in
14279 * turn to process the relevant HBA attention events. This function is called
14280 * without any lock held. It gets the hbalock to access and update SLI data
14281 * structures.
14282 *
14283 * This function returns IRQ_HANDLED when interrupt is handled, else it
14284 * returns IRQ_NONE.
14285 **/
14286irqreturn_t
14287lpfc_sli4_intr_handler(int irq, void *dev_id)
14288{
14289 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040014290 irqreturn_t hba_irq_rc;
14291 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080014292 int qidx;
James Smart4f774512009-05-22 14:52:35 -040014293
14294 /* Get the driver's phba structure from the dev_id */
14295 phba = (struct lpfc_hba *)dev_id;
14296
14297 if (unlikely(!phba))
14298 return IRQ_NONE;
14299
14300 /*
James Smart4f774512009-05-22 14:52:35 -040014301 * Invoke fast-path host attention interrupt handling as appropriate.
14302 */
James Smart6a828b02019-01-28 11:14:31 -080014303 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040014304 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014305 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040014306 if (hba_irq_rc == IRQ_HANDLED)
14307 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040014308 }
14309
James Smart67d12732012-08-03 12:36:13 -040014310 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014311} /* lpfc_sli4_intr_handler */
14312
14313/**
14314 * lpfc_sli4_queue_free - free a queue structure and associated memory
14315 * @queue: The queue structure to free.
14316 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040014317 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040014318 * the host resident queue. This function must be called after destroying the
14319 * queue on the HBA.
14320 **/
14321void
14322lpfc_sli4_queue_free(struct lpfc_queue *queue)
14323{
14324 struct lpfc_dmabuf *dmabuf;
14325
14326 if (!queue)
14327 return;
14328
James Smart4645f7b2019-03-12 16:30:14 -070014329 if (!list_empty(&queue->wq_list))
14330 list_del(&queue->wq_list);
14331
James Smart4f774512009-05-22 14:52:35 -040014332 while (!list_empty(&queue->page_list)) {
14333 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14334 list);
James Smart81b96ed2017-11-20 16:00:29 -080014335 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040014336 dmabuf->virt, dmabuf->phys);
14337 kfree(dmabuf);
14338 }
James Smart895427b2017-02-12 13:52:30 -080014339 if (queue->rqbp) {
14340 lpfc_free_rq_buffer(queue->phba, queue);
14341 kfree(queue->rqbp);
14342 }
James Smartd1f525a2017-04-21 16:04:55 -070014343
James Smart32517fc2019-01-28 11:14:33 -080014344 if (!list_empty(&queue->cpu_list))
14345 list_del(&queue->cpu_list);
14346
James Smart4f774512009-05-22 14:52:35 -040014347 kfree(queue);
14348 return;
14349}
14350
14351/**
14352 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14353 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080014354 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040014355 * @entry_size: The size of each queue entry for this queue.
14356 * @entry count: The number of entries that this queue will handle.
James Smartc1a21eb2019-03-12 16:30:29 -070014357 * @cpu: The cpu that will primarily utilize this queue.
James Smart4f774512009-05-22 14:52:35 -040014358 *
14359 * This function allocates a queue structure and the DMAable memory used for
14360 * the host resident queue. This function must be called before creating the
14361 * queue on the HBA.
14362 **/
14363struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080014364lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
James Smartc1a21eb2019-03-12 16:30:29 -070014365 uint32_t entry_size, uint32_t entry_count, int cpu)
James Smart4f774512009-05-22 14:52:35 -040014366{
14367 struct lpfc_queue *queue;
14368 struct lpfc_dmabuf *dmabuf;
James Smartcb5172e2010-03-15 11:25:07 -040014369 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart9afbee32019-03-12 16:30:28 -070014370 uint16_t x, pgcnt;
James Smart4f774512009-05-22 14:52:35 -040014371
James Smartcb5172e2010-03-15 11:25:07 -040014372 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014373 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040014374
James Smart9afbee32019-03-12 16:30:28 -070014375 pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080014376
14377 /* If needed, Adjust page count to match the max the adapter supports */
James Smart9afbee32019-03-12 16:30:28 -070014378 if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
14379 pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
14380
James Smartc1a21eb2019-03-12 16:30:29 -070014381 queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),
14382 GFP_KERNEL, cpu_to_node(cpu));
James Smart9afbee32019-03-12 16:30:28 -070014383 if (!queue)
14384 return NULL;
James Smart895427b2017-02-12 13:52:30 -080014385
James Smart4f774512009-05-22 14:52:35 -040014386 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080014387 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080014388 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040014389 INIT_LIST_HEAD(&queue->page_list);
14390 INIT_LIST_HEAD(&queue->child_list);
James Smart32517fc2019-01-28 11:14:33 -080014391 INIT_LIST_HEAD(&queue->cpu_list);
James Smart81b96ed2017-11-20 16:00:29 -080014392
14393 /* Set queue parameters now. If the system cannot provide memory
14394 * resources, the free routine needs to know what was allocated.
14395 */
James Smart9afbee32019-03-12 16:30:28 -070014396 queue->page_count = pgcnt;
14397 queue->q_pgs = (void **)&queue[1];
14398 queue->entry_cnt_per_pg = hw_page_size / entry_size;
James Smart81b96ed2017-11-20 16:00:29 -080014399 queue->entry_size = entry_size;
14400 queue->entry_count = entry_count;
14401 queue->page_size = hw_page_size;
14402 queue->phba = phba;
14403
James Smart9afbee32019-03-12 16:30:28 -070014404 for (x = 0; x < queue->page_count; x++) {
James Smartc1a21eb2019-03-12 16:30:29 -070014405 dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
14406 dev_to_node(&phba->pcidev->dev));
James Smart4f774512009-05-22 14:52:35 -040014407 if (!dmabuf)
14408 goto out_fail;
Luis Chamberlain750afb02019-01-04 09:23:09 +010014409 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
14410 hw_page_size, &dmabuf->phys,
14411 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040014412 if (!dmabuf->virt) {
14413 kfree(dmabuf);
14414 goto out_fail;
14415 }
14416 dmabuf->buffer_tag = x;
14417 list_add_tail(&dmabuf->list, &queue->page_list);
James Smart9afbee32019-03-12 16:30:28 -070014418 /* use lpfc_sli4_qe to index a paritcular entry in this page */
14419 queue->q_pgs[x] = dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -040014420 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014421 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14422 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart32517fc2019-01-28 11:14:33 -080014423 INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
14424 INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014425
James Smart32517fc2019-01-28 11:14:33 -080014426 /* notify_interval will be set during q creation */
James Smart64eb4dc2017-05-15 15:20:49 -070014427
James Smart4f774512009-05-22 14:52:35 -040014428 return queue;
14429out_fail:
14430 lpfc_sli4_queue_free(queue);
14431 return NULL;
14432}
14433
14434/**
James Smart962bc512013-01-03 15:44:00 -050014435 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14436 * @phba: HBA structure that indicates port to create a queue on.
14437 * @pci_barset: PCI BAR set flag.
14438 *
14439 * This function shall perform iomap of the specified PCI BAR address to host
14440 * memory address if not already done so and return it. The returned host
14441 * memory address can be NULL.
14442 */
14443static void __iomem *
14444lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14445{
James Smart962bc512013-01-03 15:44:00 -050014446 if (!phba->pcidev)
14447 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014448
14449 switch (pci_barset) {
14450 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014451 return phba->pci_bar0_memmap_p;
14452 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014453 return phba->pci_bar2_memmap_p;
14454 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014455 return phba->pci_bar4_memmap_p;
14456 default:
14457 break;
14458 }
14459 return NULL;
14460}
14461
14462/**
James Smartcb733e32019-01-28 11:14:32 -080014463 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
14464 * @phba: HBA structure that EQs are on.
14465 * @startq: The starting EQ index to modify
14466 * @numq: The number of EQs (consecutive indexes) to modify
14467 * @usdelay: amount of delay
James Smart173edbb2012-06-12 13:54:50 -040014468 *
James Smartcb733e32019-01-28 11:14:32 -080014469 * This function revises the EQ delay on 1 or more EQs. The EQ delay
14470 * is set either by writing to a register (if supported by the SLI Port)
14471 * or by mailbox command. The mailbox command allows several EQs to be
14472 * updated at once.
James Smart173edbb2012-06-12 13:54:50 -040014473 *
James Smartcb733e32019-01-28 11:14:32 -080014474 * The @phba struct is used to send a mailbox command to HBA. The @startq
14475 * is used to get the starting EQ index to change. The @numq value is
14476 * used to specify how many consecutive EQ indexes, starting at EQ index,
14477 * are to be changed. This function is asynchronous and will wait for any
14478 * mailbox commands to finish before returning.
James Smart173edbb2012-06-12 13:54:50 -040014479 *
James Smartcb733e32019-01-28 11:14:32 -080014480 * On success this function will return a zero. If unable to allocate
14481 * enough memory this function will return -ENOMEM. If a mailbox command
14482 * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
14483 * have had their delay multipler changed.
James Smart173edbb2012-06-12 13:54:50 -040014484 **/
James Smartcb733e32019-01-28 11:14:32 -080014485void
James Smart0cf07f842017-06-01 21:07:10 -070014486lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
James Smartcb733e32019-01-28 11:14:32 -080014487 uint32_t numq, uint32_t usdelay)
James Smart173edbb2012-06-12 13:54:50 -040014488{
14489 struct lpfc_mbx_modify_eq_delay *eq_delay;
14490 LPFC_MBOXQ_t *mbox;
14491 struct lpfc_queue *eq;
James Smartcb733e32019-01-28 11:14:32 -080014492 int cnt = 0, rc, length;
James Smart173edbb2012-06-12 13:54:50 -040014493 uint32_t shdr_status, shdr_add_status;
James Smartcb733e32019-01-28 11:14:32 -080014494 uint32_t dmult;
James Smart895427b2017-02-12 13:52:30 -080014495 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014496 union lpfc_sli4_cfg_shdr *shdr;
James Smart173edbb2012-06-12 13:54:50 -040014497
James Smart6a828b02019-01-28 11:14:31 -080014498 if (startq >= phba->cfg_irq_chann)
James Smartcb733e32019-01-28 11:14:32 -080014499 return;
14500
14501 if (usdelay > 0xFFFF) {
14502 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
14503 "6429 usdelay %d too large. Scaled down to "
14504 "0xFFFF.\n", usdelay);
14505 usdelay = 0xFFFF;
14506 }
14507
14508 /* set values by EQ_DELAY register if supported */
14509 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14510 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070014511 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smartcb733e32019-01-28 11:14:32 -080014512 if (!eq)
14513 continue;
14514
James Smart32517fc2019-01-28 11:14:33 -080014515 lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
James Smartcb733e32019-01-28 11:14:32 -080014516
14517 if (++cnt >= numq)
14518 break;
14519 }
James Smartcb733e32019-01-28 11:14:32 -080014520 return;
14521 }
14522
14523 /* Otherwise, set values by mailbox cmd */
James Smart173edbb2012-06-12 13:54:50 -040014524
14525 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smartcb733e32019-01-28 11:14:32 -080014526 if (!mbox) {
14527 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_FCP | LOG_NVME,
14528 "6428 Failed allocating mailbox cmd buffer."
14529 " EQ delay was not set.\n");
14530 return;
14531 }
James Smart173edbb2012-06-12 13:54:50 -040014532 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14533 sizeof(struct lpfc_sli4_cfg_mhdr));
14534 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14535 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14536 length, LPFC_SLI4_MBX_EMBED);
14537 eq_delay = &mbox->u.mqe.un.eq_delay;
14538
14539 /* Calculate delay multiper from maximum interrupt per second */
James Smartcb733e32019-01-28 11:14:32 -080014540 dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
14541 if (dmult)
14542 dmult--;
James Smart0cf07f842017-06-01 21:07:10 -070014543 if (dmult > LPFC_DMULT_MAX)
14544 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014545
James Smart6a828b02019-01-28 11:14:31 -080014546 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070014547 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smart173edbb2012-06-12 13:54:50 -040014548 if (!eq)
14549 continue;
James Smartcb733e32019-01-28 11:14:32 -080014550 eq->q_mode = usdelay;
James Smart173edbb2012-06-12 13:54:50 -040014551 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14552 eq_delay->u.request.eq[cnt].phase = 0;
14553 eq_delay->u.request.eq[cnt].delay_multi = dmult;
James Smart0cf07f842017-06-01 21:07:10 -070014554
James Smartcb733e32019-01-28 11:14:32 -080014555 if (++cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014556 break;
14557 }
14558 eq_delay->u.request.num_eq = cnt;
14559
14560 mbox->vport = phba->pport;
14561 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080014562 mbox->ctx_buf = NULL;
14563 mbox->ctx_ndlp = NULL;
James Smart173edbb2012-06-12 13:54:50 -040014564 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14565 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14566 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14567 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14568 if (shdr_status || shdr_add_status || rc) {
14569 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14570 "2512 MODIFY_EQ_DELAY mailbox failed with "
14571 "status x%x add_status x%x, mbx status x%x\n",
14572 shdr_status, shdr_add_status, rc);
James Smart173edbb2012-06-12 13:54:50 -040014573 }
14574 mempool_free(mbox, phba->mbox_mem_pool);
James Smartcb733e32019-01-28 11:14:32 -080014575 return;
James Smart173edbb2012-06-12 13:54:50 -040014576}
14577
14578/**
James Smart4f774512009-05-22 14:52:35 -040014579 * lpfc_eq_create - Create an Event Queue on the HBA
14580 * @phba: HBA structure that indicates port to create a queue on.
14581 * @eq: The queue structure to use to create the event queue.
14582 * @imax: The maximum interrupt per second limit.
14583 *
14584 * This function creates an event queue, as detailed in @eq, on a port,
14585 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14586 *
14587 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14588 * is used to get the entry count and entry size that are necessary to
14589 * determine the number of pages to allocate and use for this queue. This
14590 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14591 * event queue. This function is asynchronous and will wait for the mailbox
14592 * command to finish before continuing.
14593 *
14594 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014595 * memory this function will return -ENOMEM. If the queue create mailbox command
14596 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014597 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014598int
James Smartee020062012-09-29 11:28:52 -040014599lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014600{
14601 struct lpfc_mbx_eq_create *eq_create;
14602 LPFC_MBOXQ_t *mbox;
14603 int rc, length, status = 0;
14604 struct lpfc_dmabuf *dmabuf;
14605 uint32_t shdr_status, shdr_add_status;
14606 union lpfc_sli4_cfg_shdr *shdr;
14607 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014608 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14609
James Smart2e90f4b2011-12-13 13:22:37 -050014610 /* sanity check on queue memory */
14611 if (!eq)
14612 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014613 if (!phba->sli4_hba.pc_sli4_params.supported)
14614 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014615
14616 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14617 if (!mbox)
14618 return -ENOMEM;
14619 length = (sizeof(struct lpfc_mbx_eq_create) -
14620 sizeof(struct lpfc_sli4_cfg_mhdr));
14621 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14622 LPFC_MBOX_OPCODE_EQ_CREATE,
14623 length, LPFC_SLI4_MBX_EMBED);
14624 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080014625 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014626 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14627 eq->page_count);
14628 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14629 LPFC_EQE_SIZE);
14630 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080014631
14632 /* Use version 2 of CREATE_EQ if eqav is set */
14633 if (phba->sli4_hba.pc_sli4_params.eqav) {
14634 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14635 LPFC_Q_CREATE_VERSION_2);
14636 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14637 phba->sli4_hba.pc_sli4_params.eqav);
14638 }
14639
James Smart2c9c5a02015-04-07 15:07:15 -040014640 /* don't setup delay multiplier using EQ_CREATE */
14641 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014642 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14643 dmult);
14644 switch (eq->entry_count) {
14645 default:
14646 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14647 "0360 Unsupported EQ count. (%d)\n",
14648 eq->entry_count);
James Smart04d210c2019-05-21 17:49:03 -070014649 if (eq->entry_count < 256) {
14650 status = -EINVAL;
14651 goto out;
14652 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014653 /* fall through - otherwise default to smallest count */
James Smart4f774512009-05-22 14:52:35 -040014654 case 256:
14655 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14656 LPFC_EQ_CNT_256);
14657 break;
14658 case 512:
14659 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14660 LPFC_EQ_CNT_512);
14661 break;
14662 case 1024:
14663 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14664 LPFC_EQ_CNT_1024);
14665 break;
14666 case 2048:
14667 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14668 LPFC_EQ_CNT_2048);
14669 break;
14670 case 4096:
14671 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14672 LPFC_EQ_CNT_4096);
14673 break;
14674 }
14675 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014676 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014677 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14678 putPaddrLow(dmabuf->phys);
14679 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14680 putPaddrHigh(dmabuf->phys);
14681 }
14682 mbox->vport = phba->pport;
14683 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080014684 mbox->ctx_buf = NULL;
14685 mbox->ctx_ndlp = NULL;
James Smart4f774512009-05-22 14:52:35 -040014686 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040014687 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14688 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14689 if (shdr_status || shdr_add_status || rc) {
14690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14691 "2500 EQ_CREATE mailbox failed with "
14692 "status x%x add_status x%x, mbx status x%x\n",
14693 shdr_status, shdr_add_status, rc);
14694 status = -ENXIO;
14695 }
14696 eq->type = LPFC_EQ;
14697 eq->subtype = LPFC_NONE;
14698 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14699 if (eq->queue_id == 0xFFFF)
14700 status = -ENXIO;
14701 eq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080014702 eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
14703 eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
James Smart04d210c2019-05-21 17:49:03 -070014704out:
James Smart8fa38512009-07-19 10:01:03 -040014705 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014706 return status;
14707}
14708
14709/**
14710 * lpfc_cq_create - Create a Completion Queue on the HBA
14711 * @phba: HBA structure that indicates port to create a queue on.
14712 * @cq: The queue structure to use to create the completion queue.
14713 * @eq: The event queue to bind this completion queue to.
14714 *
14715 * This function creates a completion queue, as detailed in @wq, on a port,
14716 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14717 *
14718 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14719 * is used to get the entry count and entry size that are necessary to
14720 * determine the number of pages to allocate and use for this queue. The @eq
14721 * is used to indicate which event queue to bind this completion queue to. This
14722 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14723 * completion queue. This function is asynchronous and will wait for the mailbox
14724 * command to finish before continuing.
14725 *
14726 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014727 * memory this function will return -ENOMEM. If the queue create mailbox command
14728 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014729 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014730int
James Smart4f774512009-05-22 14:52:35 -040014731lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14732 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14733{
14734 struct lpfc_mbx_cq_create *cq_create;
14735 struct lpfc_dmabuf *dmabuf;
14736 LPFC_MBOXQ_t *mbox;
14737 int rc, length, status = 0;
14738 uint32_t shdr_status, shdr_add_status;
14739 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014740
James Smart2e90f4b2011-12-13 13:22:37 -050014741 /* sanity check on queue memory */
14742 if (!cq || !eq)
14743 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014744
James Smart4f774512009-05-22 14:52:35 -040014745 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14746 if (!mbox)
14747 return -ENOMEM;
14748 length = (sizeof(struct lpfc_mbx_cq_create) -
14749 sizeof(struct lpfc_sli4_cfg_mhdr));
14750 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14751 LPFC_MBOX_OPCODE_CQ_CREATE,
14752 length, LPFC_SLI4_MBX_EMBED);
14753 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014754 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014755 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14756 cq->page_count);
14757 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14758 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014759 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14760 phba->sli4_hba.pc_sli4_params.cqv);
14761 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080014762 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14763 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014764 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14765 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080014766 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14767 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050014768 } else {
14769 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14770 eq->queue_id);
14771 }
James Smart4f774512009-05-22 14:52:35 -040014772 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014773 case 2048:
14774 case 4096:
14775 if (phba->sli4_hba.pc_sli4_params.cqv ==
14776 LPFC_Q_CREATE_VERSION_2) {
14777 cq_create->u.request.context.lpfc_cq_context_count =
14778 cq->entry_count;
14779 bf_set(lpfc_cq_context_count,
14780 &cq_create->u.request.context,
14781 LPFC_CQ_CNT_WORD7);
14782 break;
14783 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014784 /* fall through */
James Smart4f774512009-05-22 14:52:35 -040014785 default:
14786 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014787 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014788 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014789 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014790 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014791 if (cq->entry_count < 256) {
14792 status = -EINVAL;
14793 goto out;
14794 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014795 /* fall through - otherwise default to smallest count */
James Smart4f774512009-05-22 14:52:35 -040014796 case 256:
14797 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14798 LPFC_CQ_CNT_256);
14799 break;
14800 case 512:
14801 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14802 LPFC_CQ_CNT_512);
14803 break;
14804 case 1024:
14805 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14806 LPFC_CQ_CNT_1024);
14807 break;
14808 }
14809 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080014810 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040014811 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14812 putPaddrLow(dmabuf->phys);
14813 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14814 putPaddrHigh(dmabuf->phys);
14815 }
14816 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14817
14818 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014819 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14820 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14821 if (shdr_status || shdr_add_status || rc) {
14822 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14823 "2501 CQ_CREATE mailbox failed with "
14824 "status x%x add_status x%x, mbx status x%x\n",
14825 shdr_status, shdr_add_status, rc);
14826 status = -ENXIO;
14827 goto out;
14828 }
14829 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14830 if (cq->queue_id == 0xFFFF) {
14831 status = -ENXIO;
14832 goto out;
14833 }
14834 /* link the cq onto the parent eq child list */
14835 list_add_tail(&cq->list, &eq->child_list);
14836 /* Set up completion queue's type and subtype */
14837 cq->type = type;
14838 cq->subtype = subtype;
14839 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014840 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080014841 cq->assoc_qp = eq;
James Smart4f774512009-05-22 14:52:35 -040014842 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080014843 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
14844 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
James Smart4f774512009-05-22 14:52:35 -040014845
James Smart6a828b02019-01-28 11:14:31 -080014846 if (cq->queue_id > phba->sli4_hba.cq_max)
14847 phba->sli4_hba.cq_max = cq->queue_id;
James Smart8fa38512009-07-19 10:01:03 -040014848out:
14849 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014850 return status;
14851}
14852
14853/**
James Smart2d7dbc42017-02-12 13:52:35 -080014854 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14855 * @phba: HBA structure that indicates port to create a queue on.
14856 * @cqp: The queue structure array to use to create the completion queues.
James Smartcdb42be2019-01-28 11:14:21 -080014857 * @hdwq: The hardware queue array with the EQ to bind completion queues to.
James Smart2d7dbc42017-02-12 13:52:35 -080014858 *
14859 * This function creates a set of completion queue, s to support MRQ
14860 * as detailed in @cqp, on a port,
14861 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14862 *
14863 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14864 * is used to get the entry count and entry size that are necessary to
14865 * determine the number of pages to allocate and use for this queue. The @eq
14866 * is used to indicate which event queue to bind this completion queue to. This
14867 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14868 * completion queue. This function is asynchronous and will wait for the mailbox
14869 * command to finish before continuing.
14870 *
14871 * On success this function will return a zero. If unable to allocate enough
14872 * memory this function will return -ENOMEM. If the queue create mailbox command
14873 * fails this function will return -ENXIO.
14874 **/
14875int
14876lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
James Smartcdb42be2019-01-28 11:14:21 -080014877 struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
14878 uint32_t subtype)
James Smart2d7dbc42017-02-12 13:52:35 -080014879{
14880 struct lpfc_queue *cq;
14881 struct lpfc_queue *eq;
14882 struct lpfc_mbx_cq_create_set *cq_set;
14883 struct lpfc_dmabuf *dmabuf;
14884 LPFC_MBOXQ_t *mbox;
14885 int rc, length, alloclen, status = 0;
14886 int cnt, idx, numcq, page_idx = 0;
14887 uint32_t shdr_status, shdr_add_status;
14888 union lpfc_sli4_cfg_shdr *shdr;
14889 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14890
14891 /* sanity check on queue memory */
14892 numcq = phba->cfg_nvmet_mrq;
James Smartcdb42be2019-01-28 11:14:21 -080014893 if (!cqp || !hdwq || !numcq)
James Smart2d7dbc42017-02-12 13:52:35 -080014894 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080014895
14896 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14897 if (!mbox)
14898 return -ENOMEM;
14899
14900 length = sizeof(struct lpfc_mbx_cq_create_set);
14901 length += ((numcq * cqp[0]->page_count) *
14902 sizeof(struct dma_address));
14903 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14904 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14905 LPFC_SLI4_MBX_NEMBED);
14906 if (alloclen < length) {
14907 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14908 "3098 Allocated DMA memory size (%d) is "
14909 "less than the requested DMA memory size "
14910 "(%d)\n", alloclen, length);
14911 status = -ENOMEM;
14912 goto out;
14913 }
14914 cq_set = mbox->sge_array->addr[0];
14915 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14916 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14917
14918 for (idx = 0; idx < numcq; idx++) {
14919 cq = cqp[idx];
James Smartcdb42be2019-01-28 11:14:21 -080014920 eq = hdwq[idx].hba_eq;
James Smart2d7dbc42017-02-12 13:52:35 -080014921 if (!cq || !eq) {
14922 status = -ENOMEM;
14923 goto out;
14924 }
James Smart81b96ed2017-11-20 16:00:29 -080014925 if (!phba->sli4_hba.pc_sli4_params.supported)
14926 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080014927
14928 switch (idx) {
14929 case 0:
14930 bf_set(lpfc_mbx_cq_create_set_page_size,
14931 &cq_set->u.request,
14932 (hw_page_size / SLI4_PAGE_SIZE));
14933 bf_set(lpfc_mbx_cq_create_set_num_pages,
14934 &cq_set->u.request, cq->page_count);
14935 bf_set(lpfc_mbx_cq_create_set_evt,
14936 &cq_set->u.request, 1);
14937 bf_set(lpfc_mbx_cq_create_set_valid,
14938 &cq_set->u.request, 1);
14939 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14940 &cq_set->u.request, 0);
14941 bf_set(lpfc_mbx_cq_create_set_num_cq,
14942 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080014943 bf_set(lpfc_mbx_cq_create_set_autovalid,
14944 &cq_set->u.request,
14945 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080014946 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014947 case 2048:
14948 case 4096:
14949 if (phba->sli4_hba.pc_sli4_params.cqv ==
14950 LPFC_Q_CREATE_VERSION_2) {
14951 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14952 &cq_set->u.request,
14953 cq->entry_count);
14954 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14955 &cq_set->u.request,
14956 LPFC_CQ_CNT_WORD7);
14957 break;
14958 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014959 /* fall through */
James Smart2d7dbc42017-02-12 13:52:35 -080014960 default:
14961 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14962 "3118 Bad CQ count. (%d)\n",
14963 cq->entry_count);
14964 if (cq->entry_count < 256) {
14965 status = -EINVAL;
14966 goto out;
14967 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014968 /* fall through - otherwise default to smallest */
James Smart2d7dbc42017-02-12 13:52:35 -080014969 case 256:
14970 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14971 &cq_set->u.request, LPFC_CQ_CNT_256);
14972 break;
14973 case 512:
14974 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14975 &cq_set->u.request, LPFC_CQ_CNT_512);
14976 break;
14977 case 1024:
14978 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14979 &cq_set->u.request, LPFC_CQ_CNT_1024);
14980 break;
14981 }
14982 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14983 &cq_set->u.request, eq->queue_id);
14984 break;
14985 case 1:
14986 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14987 &cq_set->u.request, eq->queue_id);
14988 break;
14989 case 2:
14990 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14991 &cq_set->u.request, eq->queue_id);
14992 break;
14993 case 3:
14994 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14995 &cq_set->u.request, eq->queue_id);
14996 break;
14997 case 4:
14998 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14999 &cq_set->u.request, eq->queue_id);
15000 break;
15001 case 5:
15002 bf_set(lpfc_mbx_cq_create_set_eq_id5,
15003 &cq_set->u.request, eq->queue_id);
15004 break;
15005 case 6:
15006 bf_set(lpfc_mbx_cq_create_set_eq_id6,
15007 &cq_set->u.request, eq->queue_id);
15008 break;
15009 case 7:
15010 bf_set(lpfc_mbx_cq_create_set_eq_id7,
15011 &cq_set->u.request, eq->queue_id);
15012 break;
15013 case 8:
15014 bf_set(lpfc_mbx_cq_create_set_eq_id8,
15015 &cq_set->u.request, eq->queue_id);
15016 break;
15017 case 9:
15018 bf_set(lpfc_mbx_cq_create_set_eq_id9,
15019 &cq_set->u.request, eq->queue_id);
15020 break;
15021 case 10:
15022 bf_set(lpfc_mbx_cq_create_set_eq_id10,
15023 &cq_set->u.request, eq->queue_id);
15024 break;
15025 case 11:
15026 bf_set(lpfc_mbx_cq_create_set_eq_id11,
15027 &cq_set->u.request, eq->queue_id);
15028 break;
15029 case 12:
15030 bf_set(lpfc_mbx_cq_create_set_eq_id12,
15031 &cq_set->u.request, eq->queue_id);
15032 break;
15033 case 13:
15034 bf_set(lpfc_mbx_cq_create_set_eq_id13,
15035 &cq_set->u.request, eq->queue_id);
15036 break;
15037 case 14:
15038 bf_set(lpfc_mbx_cq_create_set_eq_id14,
15039 &cq_set->u.request, eq->queue_id);
15040 break;
15041 case 15:
15042 bf_set(lpfc_mbx_cq_create_set_eq_id15,
15043 &cq_set->u.request, eq->queue_id);
15044 break;
15045 }
15046
15047 /* link the cq onto the parent eq child list */
15048 list_add_tail(&cq->list, &eq->child_list);
15049 /* Set up completion queue's type and subtype */
15050 cq->type = type;
15051 cq->subtype = subtype;
15052 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080015053 cq->assoc_qp = eq;
James Smart2d7dbc42017-02-12 13:52:35 -080015054 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015055 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
15056 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
15057 cq->entry_count);
James Smart81b96ed2017-11-20 16:00:29 -080015058 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080015059
15060 rc = 0;
15061 list_for_each_entry(dmabuf, &cq->page_list, list) {
15062 memset(dmabuf->virt, 0, hw_page_size);
15063 cnt = page_idx + dmabuf->buffer_tag;
15064 cq_set->u.request.page[cnt].addr_lo =
15065 putPaddrLow(dmabuf->phys);
15066 cq_set->u.request.page[cnt].addr_hi =
15067 putPaddrHigh(dmabuf->phys);
15068 rc++;
15069 }
15070 page_idx += rc;
15071 }
15072
15073 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15074
15075 /* The IOCTL status is embedded in the mailbox subheader. */
15076 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15077 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15078 if (shdr_status || shdr_add_status || rc) {
15079 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15080 "3119 CQ_CREATE_SET mailbox failed with "
15081 "status x%x add_status x%x, mbx status x%x\n",
15082 shdr_status, shdr_add_status, rc);
15083 status = -ENXIO;
15084 goto out;
15085 }
15086 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
15087 if (rc == 0xFFFF) {
15088 status = -ENXIO;
15089 goto out;
15090 }
15091
15092 for (idx = 0; idx < numcq; idx++) {
15093 cq = cqp[idx];
15094 cq->queue_id = rc + idx;
James Smart6a828b02019-01-28 11:14:31 -080015095 if (cq->queue_id > phba->sli4_hba.cq_max)
15096 phba->sli4_hba.cq_max = cq->queue_id;
James Smart2d7dbc42017-02-12 13:52:35 -080015097 }
15098
15099out:
15100 lpfc_sli4_mbox_cmd_free(phba, mbox);
15101 return status;
15102}
15103
15104/**
James Smartb19a0612010-04-06 14:48:51 -040015105 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040015106 * @phba: HBA structure that indicates port to create a queue on.
15107 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040015108 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
15109 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040015110 *
James Smartb19a0612010-04-06 14:48:51 -040015111 * This function provides failback (fb) functionality when the
15112 * mq_create_ext fails on older FW generations. It's purpose is identical
15113 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040015114 *
James Smartb19a0612010-04-06 14:48:51 -040015115 * This routine cannot fail as all attributes were previously accessed and
15116 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040015117 **/
James Smartb19a0612010-04-06 14:48:51 -040015118static void
15119lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
15120 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040015121{
15122 struct lpfc_mbx_mq_create *mq_create;
15123 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040015124 int length;
James Smart04c68492009-05-22 14:52:52 -040015125
James Smart04c68492009-05-22 14:52:52 -040015126 length = (sizeof(struct lpfc_mbx_mq_create) -
15127 sizeof(struct lpfc_sli4_cfg_mhdr));
15128 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15129 LPFC_MBOX_OPCODE_MQ_CREATE,
15130 length, LPFC_SLI4_MBX_EMBED);
15131 mq_create = &mbox->u.mqe.un.mq_create;
15132 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040015133 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040015134 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040015135 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040015136 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
15137 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040015138 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015139 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15140 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040015141 break;
15142 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015143 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15144 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040015145 break;
15146 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015147 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15148 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040015149 break;
15150 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015151 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15152 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040015153 break;
15154 }
15155 list_for_each_entry(dmabuf, &mq->page_list, list) {
15156 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040015157 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040015158 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040015159 putPaddrHigh(dmabuf->phys);
15160 }
15161}
15162
15163/**
15164 * lpfc_mq_create - Create a mailbox Queue on the HBA
15165 * @phba: HBA structure that indicates port to create a queue on.
15166 * @mq: The queue structure to use to create the mailbox queue.
15167 * @cq: The completion queue to associate with this cq.
15168 * @subtype: The queue's subtype.
15169 *
15170 * This function creates a mailbox queue, as detailed in @mq, on a port,
15171 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
15172 *
15173 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15174 * is used to get the entry count and entry size that are necessary to
15175 * determine the number of pages to allocate and use for this queue. This
15176 * function will send the MQ_CREATE mailbox command to the HBA to setup the
15177 * mailbox queue. This function is asynchronous and will wait for the mailbox
15178 * command to finish before continuing.
15179 *
15180 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015181 * memory this function will return -ENOMEM. If the queue create mailbox command
15182 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040015183 **/
15184int32_t
15185lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15186 struct lpfc_queue *cq, uint32_t subtype)
15187{
15188 struct lpfc_mbx_mq_create *mq_create;
15189 struct lpfc_mbx_mq_create_ext *mq_create_ext;
15190 struct lpfc_dmabuf *dmabuf;
15191 LPFC_MBOXQ_t *mbox;
15192 int rc, length, status = 0;
15193 uint32_t shdr_status, shdr_add_status;
15194 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015195 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040015196
James Smart2e90f4b2011-12-13 13:22:37 -050015197 /* sanity check on queue memory */
15198 if (!mq || !cq)
15199 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015200 if (!phba->sli4_hba.pc_sli4_params.supported)
15201 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040015202
15203 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15204 if (!mbox)
15205 return -ENOMEM;
15206 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15207 sizeof(struct lpfc_sli4_cfg_mhdr));
15208 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15209 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15210 length, LPFC_SLI4_MBX_EMBED);
15211
15212 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050015213 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050015214 bf_set(lpfc_mbx_mq_create_ext_num_pages,
15215 &mq_create_ext->u.request, mq->page_count);
15216 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15217 &mq_create_ext->u.request, 1);
15218 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040015219 &mq_create_ext->u.request, 1);
15220 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15221 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050015222 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15223 &mq_create_ext->u.request, 1);
15224 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15225 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040015226 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050015227 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15228 phba->sli4_hba.pc_sli4_params.mqv);
15229 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15230 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15231 cq->queue_id);
15232 else
15233 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15234 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040015235 switch (mq->entry_count) {
15236 default:
15237 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15238 "0362 Unsupported MQ count. (%d)\n",
15239 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015240 if (mq->entry_count < 16) {
15241 status = -EINVAL;
15242 goto out;
15243 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015244 /* fall through - otherwise default to smallest count */
James Smartb19a0612010-04-06 14:48:51 -040015245 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015246 bf_set(lpfc_mq_context_ring_size,
15247 &mq_create_ext->u.request.context,
15248 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040015249 break;
15250 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015251 bf_set(lpfc_mq_context_ring_size,
15252 &mq_create_ext->u.request.context,
15253 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040015254 break;
15255 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015256 bf_set(lpfc_mq_context_ring_size,
15257 &mq_create_ext->u.request.context,
15258 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040015259 break;
15260 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015261 bf_set(lpfc_mq_context_ring_size,
15262 &mq_create_ext->u.request.context,
15263 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040015264 break;
15265 }
15266 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015267 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040015268 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15269 putPaddrLow(dmabuf->phys);
15270 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040015271 putPaddrHigh(dmabuf->phys);
15272 }
15273 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040015274 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15275 &mq_create_ext->u.response);
15276 if (rc != MBX_SUCCESS) {
15277 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15278 "2795 MQ_CREATE_EXT failed with "
15279 "status x%x. Failback to MQ_CREATE.\n",
15280 rc);
15281 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15282 mq_create = &mbox->u.mqe.un.mq_create;
15283 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15284 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15285 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15286 &mq_create->u.response);
15287 }
15288
James Smart04c68492009-05-22 14:52:52 -040015289 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040015290 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15291 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15292 if (shdr_status || shdr_add_status || rc) {
15293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15294 "2502 MQ_CREATE mailbox failed with "
15295 "status x%x add_status x%x, mbx status x%x\n",
15296 shdr_status, shdr_add_status, rc);
15297 status = -ENXIO;
15298 goto out;
15299 }
James Smart04c68492009-05-22 14:52:52 -040015300 if (mq->queue_id == 0xFFFF) {
15301 status = -ENXIO;
15302 goto out;
15303 }
15304 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050015305 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040015306 mq->subtype = subtype;
15307 mq->host_index = 0;
15308 mq->hba_index = 0;
15309
15310 /* link the mq onto the parent cq child list */
15311 list_add_tail(&mq->list, &cq->child_list);
15312out:
James Smart8fa38512009-07-19 10:01:03 -040015313 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015314 return status;
15315}
15316
15317/**
James Smart4f774512009-05-22 14:52:35 -040015318 * lpfc_wq_create - Create a Work Queue on the HBA
15319 * @phba: HBA structure that indicates port to create a queue on.
15320 * @wq: The queue structure to use to create the work queue.
15321 * @cq: The completion queue to bind this work queue to.
15322 * @subtype: The subtype of the work queue indicating its functionality.
15323 *
15324 * This function creates a work queue, as detailed in @wq, on a port, described
15325 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15326 *
15327 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15328 * is used to get the entry count and entry size that are necessary to
15329 * determine the number of pages to allocate and use for this queue. The @cq
15330 * is used to indicate which completion queue to bind this work queue to. This
15331 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15332 * work queue. This function is asynchronous and will wait for the mailbox
15333 * command to finish before continuing.
15334 *
15335 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015336 * memory this function will return -ENOMEM. If the queue create mailbox command
15337 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015338 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015339int
James Smart4f774512009-05-22 14:52:35 -040015340lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15341 struct lpfc_queue *cq, uint32_t subtype)
15342{
15343 struct lpfc_mbx_wq_create *wq_create;
15344 struct lpfc_dmabuf *dmabuf;
15345 LPFC_MBOXQ_t *mbox;
15346 int rc, length, status = 0;
15347 uint32_t shdr_status, shdr_add_status;
15348 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015349 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050015350 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050015351 void __iomem *bar_memmap_p;
15352 uint32_t db_offset;
15353 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080015354 uint8_t dpp_barset;
15355 uint32_t dpp_offset;
15356 unsigned long pg_addr;
James Smart81b96ed2017-11-20 16:00:29 -080015357 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040015358
James Smart2e90f4b2011-12-13 13:22:37 -050015359 /* sanity check on queue memory */
15360 if (!wq || !cq)
15361 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015362 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015363 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040015364
15365 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15366 if (!mbox)
15367 return -ENOMEM;
15368 length = (sizeof(struct lpfc_mbx_wq_create) -
15369 sizeof(struct lpfc_sli4_cfg_mhdr));
15370 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15371 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15372 length, LPFC_SLI4_MBX_EMBED);
15373 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015374 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015375 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15376 wq->page_count);
15377 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15378 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040015379
15380 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050015381 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15382 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050015383
James Smartc176ffa2018-01-30 15:58:46 -080015384 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15385 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080015386 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15387 else
15388 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15389
James Smart0c651872013-07-15 18:33:23 -040015390
James Smart1351e692018-02-22 08:18:43 -080015391 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15392 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15393 else
15394 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15395
15396 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040015397 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050015398 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15399 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070015400 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15401 LPFC_Q_CREATE_VERSION_1);
15402
James Smart5a6f1332011-03-11 16:05:35 -050015403 switch (wq->entry_size) {
15404 default:
15405 case 64:
15406 bf_set(lpfc_mbx_wq_create_wqe_size,
15407 &wq_create->u.request_1,
15408 LPFC_WQ_WQE_SIZE_64);
15409 break;
15410 case 128:
15411 bf_set(lpfc_mbx_wq_create_wqe_size,
15412 &wq_create->u.request_1,
15413 LPFC_WQ_WQE_SIZE_128);
15414 break;
15415 }
James Smart1351e692018-02-22 08:18:43 -080015416 /* Request DPP by default */
15417 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080015418 bf_set(lpfc_mbx_wq_create_page_size,
15419 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015420 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015421 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015422 break;
15423 default:
James Smart1351e692018-02-22 08:18:43 -080015424 page = wq_create->u.request.page;
15425 break;
James Smart5a6f1332011-03-11 16:05:35 -050015426 }
James Smart0c651872013-07-15 18:33:23 -040015427
James Smart4f774512009-05-22 14:52:35 -040015428 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015429 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015430 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15431 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015432 }
James Smart962bc512013-01-03 15:44:00 -050015433
15434 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15435 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15436
James Smart4f774512009-05-22 14:52:35 -040015437 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15438 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015439 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15440 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15441 if (shdr_status || shdr_add_status || rc) {
15442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15443 "2503 WQ_CREATE mailbox failed with "
15444 "status x%x add_status x%x, mbx status x%x\n",
15445 shdr_status, shdr_add_status, rc);
15446 status = -ENXIO;
15447 goto out;
15448 }
James Smart1351e692018-02-22 08:18:43 -080015449
15450 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15451 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15452 &wq_create->u.response);
15453 else
15454 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15455 &wq_create->u.response_1);
15456
James Smart4f774512009-05-22 14:52:35 -040015457 if (wq->queue_id == 0xFFFF) {
15458 status = -ENXIO;
15459 goto out;
15460 }
James Smart1351e692018-02-22 08:18:43 -080015461
15462 wq->db_format = LPFC_DB_LIST_FORMAT;
15463 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15464 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15465 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15466 &wq_create->u.response);
15467 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15468 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15469 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15470 "3265 WQ[%d] doorbell format "
15471 "not supported: x%x\n",
15472 wq->queue_id, wq->db_format);
15473 status = -EINVAL;
15474 goto out;
15475 }
15476 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15477 &wq_create->u.response);
15478 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15479 pci_barset);
15480 if (!bar_memmap_p) {
15481 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15482 "3263 WQ[%d] failed to memmap "
15483 "pci barset:x%x\n",
15484 wq->queue_id, pci_barset);
15485 status = -ENOMEM;
15486 goto out;
15487 }
15488 db_offset = wq_create->u.response.doorbell_offset;
15489 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15490 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15492 "3252 WQ[%d] doorbell offset "
15493 "not supported: x%x\n",
15494 wq->queue_id, db_offset);
15495 status = -EINVAL;
15496 goto out;
15497 }
15498 wq->db_regaddr = bar_memmap_p + db_offset;
15499 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15500 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15501 "format:x%x\n", wq->queue_id,
15502 pci_barset, db_offset, wq->db_format);
15503 } else
15504 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015505 } else {
James Smart1351e692018-02-22 08:18:43 -080015506 /* Check if DPP was honored by the firmware */
15507 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15508 &wq_create->u.response_1);
15509 if (wq->dpp_enable) {
15510 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15511 &wq_create->u.response_1);
15512 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15513 pci_barset);
15514 if (!bar_memmap_p) {
15515 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15516 "3267 WQ[%d] failed to memmap "
15517 "pci barset:x%x\n",
15518 wq->queue_id, pci_barset);
15519 status = -ENOMEM;
15520 goto out;
15521 }
15522 db_offset = wq_create->u.response_1.doorbell_offset;
15523 wq->db_regaddr = bar_memmap_p + db_offset;
15524 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15525 &wq_create->u.response_1);
15526 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15527 &wq_create->u.response_1);
15528 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15529 dpp_barset);
15530 if (!bar_memmap_p) {
15531 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15532 "3268 WQ[%d] failed to memmap "
15533 "pci barset:x%x\n",
15534 wq->queue_id, dpp_barset);
15535 status = -ENOMEM;
15536 goto out;
15537 }
15538 dpp_offset = wq_create->u.response_1.dpp_offset;
15539 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15540 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15541 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15542 "dpp_id:x%x dpp_barset:x%x "
15543 "dpp_offset:x%x\n",
15544 wq->queue_id, pci_barset, db_offset,
15545 wq->dpp_id, dpp_barset, dpp_offset);
15546
15547 /* Enable combined writes for DPP aperture */
15548 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15549#ifdef CONFIG_X86
15550 rc = set_memory_wc(pg_addr, 1);
15551 if (rc) {
15552 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15553 "3272 Cannot setup Combined "
15554 "Write on WQ[%d] - disable DPP\n",
15555 wq->queue_id);
15556 phba->cfg_enable_dpp = 0;
15557 }
15558#else
15559 phba->cfg_enable_dpp = 0;
15560#endif
15561 } else
15562 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015563 }
James Smart895427b2017-02-12 13:52:30 -080015564 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15565 if (wq->pring == NULL) {
15566 status = -ENOMEM;
15567 goto out;
15568 }
James Smart4f774512009-05-22 14:52:35 -040015569 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015570 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015571 wq->subtype = subtype;
15572 wq->host_index = 0;
15573 wq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015574 wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040015575
15576 /* link the wq onto the parent cq child list */
15577 list_add_tail(&wq->list, &cq->child_list);
15578out:
James Smart8fa38512009-07-19 10:01:03 -040015579 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015580 return status;
15581}
15582
15583/**
15584 * lpfc_rq_create - Create a Receive Queue on the HBA
15585 * @phba: HBA structure that indicates port to create a queue on.
15586 * @hrq: The queue structure to use to create the header receive queue.
15587 * @drq: The queue structure to use to create the data receive queue.
15588 * @cq: The completion queue to bind this work queue to.
15589 *
15590 * This function creates a receive buffer queue pair , as detailed in @hrq and
15591 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15592 * to the HBA.
15593 *
15594 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15595 * struct is used to get the entry count that is necessary to determine the
15596 * number of pages to use for this queue. The @cq is used to indicate which
15597 * completion queue to bind received buffers that are posted to these queues to.
15598 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15599 * receive queue pair. This function is asynchronous and will wait for the
15600 * mailbox command to finish before continuing.
15601 *
15602 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015603 * memory this function will return -ENOMEM. If the queue create mailbox command
15604 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015605 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015606int
James Smart4f774512009-05-22 14:52:35 -040015607lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15608 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15609{
15610 struct lpfc_mbx_rq_create *rq_create;
15611 struct lpfc_dmabuf *dmabuf;
15612 LPFC_MBOXQ_t *mbox;
15613 int rc, length, status = 0;
15614 uint32_t shdr_status, shdr_add_status;
15615 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015616 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015617 void __iomem *bar_memmap_p;
15618 uint32_t db_offset;
15619 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015620
James Smart2e90f4b2011-12-13 13:22:37 -050015621 /* sanity check on queue memory */
15622 if (!hrq || !drq || !cq)
15623 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015624 if (!phba->sli4_hba.pc_sli4_params.supported)
15625 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015626
15627 if (hrq->entry_count != drq->entry_count)
15628 return -EINVAL;
15629 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15630 if (!mbox)
15631 return -ENOMEM;
15632 length = (sizeof(struct lpfc_mbx_rq_create) -
15633 sizeof(struct lpfc_sli4_cfg_mhdr));
15634 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15635 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15636 length, LPFC_SLI4_MBX_EMBED);
15637 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015638 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15639 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15640 phba->sli4_hba.pc_sli4_params.rqv);
15641 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15642 bf_set(lpfc_rq_context_rqe_count_1,
15643 &rq_create->u.request.context,
15644 hrq->entry_count);
15645 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015646 bf_set(lpfc_rq_context_rqe_size,
15647 &rq_create->u.request.context,
15648 LPFC_RQE_SIZE_8);
15649 bf_set(lpfc_rq_context_page_size,
15650 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015651 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015652 } else {
15653 switch (hrq->entry_count) {
15654 default:
15655 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15656 "2535 Unsupported RQ count. (%d)\n",
15657 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015658 if (hrq->entry_count < 512) {
15659 status = -EINVAL;
15660 goto out;
15661 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015662 /* fall through - otherwise default to smallest count */
James Smart5a6f1332011-03-11 16:05:35 -050015663 case 512:
15664 bf_set(lpfc_rq_context_rqe_count,
15665 &rq_create->u.request.context,
15666 LPFC_RQ_RING_SIZE_512);
15667 break;
15668 case 1024:
15669 bf_set(lpfc_rq_context_rqe_count,
15670 &rq_create->u.request.context,
15671 LPFC_RQ_RING_SIZE_1024);
15672 break;
15673 case 2048:
15674 bf_set(lpfc_rq_context_rqe_count,
15675 &rq_create->u.request.context,
15676 LPFC_RQ_RING_SIZE_2048);
15677 break;
15678 case 4096:
15679 bf_set(lpfc_rq_context_rqe_count,
15680 &rq_create->u.request.context,
15681 LPFC_RQ_RING_SIZE_4096);
15682 break;
15683 }
15684 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15685 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015686 }
15687 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15688 cq->queue_id);
15689 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15690 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015691 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015692 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015693 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15694 putPaddrLow(dmabuf->phys);
15695 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15696 putPaddrHigh(dmabuf->phys);
15697 }
James Smart962bc512013-01-03 15:44:00 -050015698 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15699 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15700
James Smart4f774512009-05-22 14:52:35 -040015701 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15702 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015703 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15704 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15705 if (shdr_status || shdr_add_status || rc) {
15706 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15707 "2504 RQ_CREATE mailbox failed with "
15708 "status x%x add_status x%x, mbx status x%x\n",
15709 shdr_status, shdr_add_status, rc);
15710 status = -ENXIO;
15711 goto out;
15712 }
15713 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15714 if (hrq->queue_id == 0xFFFF) {
15715 status = -ENXIO;
15716 goto out;
15717 }
James Smart962bc512013-01-03 15:44:00 -050015718
15719 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15720 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15721 &rq_create->u.response);
15722 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15723 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15724 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15725 "3262 RQ [%d] doorbell format not "
15726 "supported: x%x\n", hrq->queue_id,
15727 hrq->db_format);
15728 status = -EINVAL;
15729 goto out;
15730 }
15731
15732 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15733 &rq_create->u.response);
15734 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15735 if (!bar_memmap_p) {
15736 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15737 "3269 RQ[%d] failed to memmap pci "
15738 "barset:x%x\n", hrq->queue_id,
15739 pci_barset);
15740 status = -ENOMEM;
15741 goto out;
15742 }
15743
15744 db_offset = rq_create->u.response.doorbell_offset;
15745 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15746 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15747 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15748 "3270 RQ[%d] doorbell offset not "
15749 "supported: x%x\n", hrq->queue_id,
15750 db_offset);
15751 status = -EINVAL;
15752 goto out;
15753 }
15754 hrq->db_regaddr = bar_memmap_p + db_offset;
15755 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015756 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15757 "format:x%x\n", hrq->queue_id, pci_barset,
15758 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015759 } else {
15760 hrq->db_format = LPFC_DB_RING_FORMAT;
15761 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15762 }
James Smart4f774512009-05-22 14:52:35 -040015763 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015764 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015765 hrq->subtype = subtype;
15766 hrq->host_index = 0;
15767 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015768 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040015769
15770 /* now create the data queue */
15771 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15772 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15773 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015774 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15775 phba->sli4_hba.pc_sli4_params.rqv);
15776 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15777 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015778 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015779 if (subtype == LPFC_NVMET)
15780 rq_create->u.request.context.buffer_size =
15781 LPFC_NVMET_DATA_BUF_SIZE;
15782 else
15783 rq_create->u.request.context.buffer_size =
15784 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015785 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15786 LPFC_RQE_SIZE_8);
15787 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15788 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015789 } else {
15790 switch (drq->entry_count) {
15791 default:
15792 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15793 "2536 Unsupported RQ count. (%d)\n",
15794 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015795 if (drq->entry_count < 512) {
15796 status = -EINVAL;
15797 goto out;
15798 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015799 /* fall through - otherwise default to smallest count */
James Smart5a6f1332011-03-11 16:05:35 -050015800 case 512:
15801 bf_set(lpfc_rq_context_rqe_count,
15802 &rq_create->u.request.context,
15803 LPFC_RQ_RING_SIZE_512);
15804 break;
15805 case 1024:
15806 bf_set(lpfc_rq_context_rqe_count,
15807 &rq_create->u.request.context,
15808 LPFC_RQ_RING_SIZE_1024);
15809 break;
15810 case 2048:
15811 bf_set(lpfc_rq_context_rqe_count,
15812 &rq_create->u.request.context,
15813 LPFC_RQ_RING_SIZE_2048);
15814 break;
15815 case 4096:
15816 bf_set(lpfc_rq_context_rqe_count,
15817 &rq_create->u.request.context,
15818 LPFC_RQ_RING_SIZE_4096);
15819 break;
15820 }
James Smart3c603be2017-05-15 15:20:44 -070015821 if (subtype == LPFC_NVMET)
15822 bf_set(lpfc_rq_context_buf_size,
15823 &rq_create->u.request.context,
15824 LPFC_NVMET_DATA_BUF_SIZE);
15825 else
15826 bf_set(lpfc_rq_context_buf_size,
15827 &rq_create->u.request.context,
15828 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015829 }
15830 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15831 cq->queue_id);
15832 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15833 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015834 list_for_each_entry(dmabuf, &drq->page_list, list) {
15835 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15836 putPaddrLow(dmabuf->phys);
15837 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15838 putPaddrHigh(dmabuf->phys);
15839 }
James Smart962bc512013-01-03 15:44:00 -050015840 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15841 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015842 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15843 /* The IOCTL status is embedded in the mailbox subheader. */
15844 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15845 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15846 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15847 if (shdr_status || shdr_add_status || rc) {
15848 status = -ENXIO;
15849 goto out;
15850 }
15851 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15852 if (drq->queue_id == 0xFFFF) {
15853 status = -ENXIO;
15854 goto out;
15855 }
15856 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015857 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015858 drq->subtype = subtype;
15859 drq->host_index = 0;
15860 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015861 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040015862
15863 /* link the header and data RQs onto the parent cq child list */
15864 list_add_tail(&hrq->list, &cq->child_list);
15865 list_add_tail(&drq->list, &cq->child_list);
15866
15867out:
James Smart8fa38512009-07-19 10:01:03 -040015868 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015869 return status;
15870}
15871
15872/**
James Smart2d7dbc42017-02-12 13:52:35 -080015873 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15874 * @phba: HBA structure that indicates port to create a queue on.
15875 * @hrqp: The queue structure array to use to create the header receive queues.
15876 * @drqp: The queue structure array to use to create the data receive queues.
15877 * @cqp: The completion queue array to bind these receive queues to.
15878 *
15879 * This function creates a receive buffer queue pair , as detailed in @hrq and
15880 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15881 * to the HBA.
15882 *
15883 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15884 * struct is used to get the entry count that is necessary to determine the
15885 * number of pages to use for this queue. The @cq is used to indicate which
15886 * completion queue to bind received buffers that are posted to these queues to.
15887 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15888 * receive queue pair. This function is asynchronous and will wait for the
15889 * mailbox command to finish before continuing.
15890 *
15891 * On success this function will return a zero. If unable to allocate enough
15892 * memory this function will return -ENOMEM. If the queue create mailbox command
15893 * fails this function will return -ENXIO.
15894 **/
15895int
15896lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15897 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15898 uint32_t subtype)
15899{
15900 struct lpfc_queue *hrq, *drq, *cq;
15901 struct lpfc_mbx_rq_create_v2 *rq_create;
15902 struct lpfc_dmabuf *dmabuf;
15903 LPFC_MBOXQ_t *mbox;
15904 int rc, length, alloclen, status = 0;
15905 int cnt, idx, numrq, page_idx = 0;
15906 uint32_t shdr_status, shdr_add_status;
15907 union lpfc_sli4_cfg_shdr *shdr;
15908 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15909
15910 numrq = phba->cfg_nvmet_mrq;
15911 /* sanity check on array memory */
15912 if (!hrqp || !drqp || !cqp || !numrq)
15913 return -ENODEV;
15914 if (!phba->sli4_hba.pc_sli4_params.supported)
15915 hw_page_size = SLI4_PAGE_SIZE;
15916
15917 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15918 if (!mbox)
15919 return -ENOMEM;
15920
15921 length = sizeof(struct lpfc_mbx_rq_create_v2);
15922 length += ((2 * numrq * hrqp[0]->page_count) *
15923 sizeof(struct dma_address));
15924
15925 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15926 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15927 LPFC_SLI4_MBX_NEMBED);
15928 if (alloclen < length) {
15929 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15930 "3099 Allocated DMA memory size (%d) is "
15931 "less than the requested DMA memory size "
15932 "(%d)\n", alloclen, length);
15933 status = -ENOMEM;
15934 goto out;
15935 }
15936
15937
15938
15939 rq_create = mbox->sge_array->addr[0];
15940 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15941
15942 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15943 cnt = 0;
15944
15945 for (idx = 0; idx < numrq; idx++) {
15946 hrq = hrqp[idx];
15947 drq = drqp[idx];
15948 cq = cqp[idx];
15949
James Smart2d7dbc42017-02-12 13:52:35 -080015950 /* sanity check on queue memory */
15951 if (!hrq || !drq || !cq) {
15952 status = -ENODEV;
15953 goto out;
15954 }
15955
James Smart7aabe842017-03-04 09:30:22 -080015956 if (hrq->entry_count != drq->entry_count) {
15957 status = -EINVAL;
15958 goto out;
15959 }
15960
James Smart2d7dbc42017-02-12 13:52:35 -080015961 if (idx == 0) {
15962 bf_set(lpfc_mbx_rq_create_num_pages,
15963 &rq_create->u.request,
15964 hrq->page_count);
15965 bf_set(lpfc_mbx_rq_create_rq_cnt,
15966 &rq_create->u.request, (numrq * 2));
15967 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15968 1);
15969 bf_set(lpfc_rq_context_base_cq,
15970 &rq_create->u.request.context,
15971 cq->queue_id);
15972 bf_set(lpfc_rq_context_data_size,
15973 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015974 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015975 bf_set(lpfc_rq_context_hdr_size,
15976 &rq_create->u.request.context,
15977 LPFC_HDR_BUF_SIZE);
15978 bf_set(lpfc_rq_context_rqe_count_1,
15979 &rq_create->u.request.context,
15980 hrq->entry_count);
15981 bf_set(lpfc_rq_context_rqe_size,
15982 &rq_create->u.request.context,
15983 LPFC_RQE_SIZE_8);
15984 bf_set(lpfc_rq_context_page_size,
15985 &rq_create->u.request.context,
15986 (PAGE_SIZE/SLI4_PAGE_SIZE));
15987 }
15988 rc = 0;
15989 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15990 memset(dmabuf->virt, 0, hw_page_size);
15991 cnt = page_idx + dmabuf->buffer_tag;
15992 rq_create->u.request.page[cnt].addr_lo =
15993 putPaddrLow(dmabuf->phys);
15994 rq_create->u.request.page[cnt].addr_hi =
15995 putPaddrHigh(dmabuf->phys);
15996 rc++;
15997 }
15998 page_idx += rc;
15999
16000 rc = 0;
16001 list_for_each_entry(dmabuf, &drq->page_list, list) {
16002 memset(dmabuf->virt, 0, hw_page_size);
16003 cnt = page_idx + dmabuf->buffer_tag;
16004 rq_create->u.request.page[cnt].addr_lo =
16005 putPaddrLow(dmabuf->phys);
16006 rq_create->u.request.page[cnt].addr_hi =
16007 putPaddrHigh(dmabuf->phys);
16008 rc++;
16009 }
16010 page_idx += rc;
16011
16012 hrq->db_format = LPFC_DB_RING_FORMAT;
16013 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16014 hrq->type = LPFC_HRQ;
16015 hrq->assoc_qid = cq->queue_id;
16016 hrq->subtype = subtype;
16017 hrq->host_index = 0;
16018 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016019 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080016020
16021 drq->db_format = LPFC_DB_RING_FORMAT;
16022 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16023 drq->type = LPFC_DRQ;
16024 drq->assoc_qid = cq->queue_id;
16025 drq->subtype = subtype;
16026 drq->host_index = 0;
16027 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016028 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080016029
16030 list_add_tail(&hrq->list, &cq->child_list);
16031 list_add_tail(&drq->list, &cq->child_list);
16032 }
16033
16034 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16035 /* The IOCTL status is embedded in the mailbox subheader. */
16036 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16037 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16038 if (shdr_status || shdr_add_status || rc) {
16039 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16040 "3120 RQ_CREATE mailbox failed with "
16041 "status x%x add_status x%x, mbx status x%x\n",
16042 shdr_status, shdr_add_status, rc);
16043 status = -ENXIO;
16044 goto out;
16045 }
16046 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16047 if (rc == 0xFFFF) {
16048 status = -ENXIO;
16049 goto out;
16050 }
16051
16052 /* Initialize all RQs with associated queue id */
16053 for (idx = 0; idx < numrq; idx++) {
16054 hrq = hrqp[idx];
16055 hrq->queue_id = rc + (2 * idx);
16056 drq = drqp[idx];
16057 drq->queue_id = rc + (2 * idx) + 1;
16058 }
16059
16060out:
16061 lpfc_sli4_mbox_cmd_free(phba, mbox);
16062 return status;
16063}
16064
16065/**
James Smart4f774512009-05-22 14:52:35 -040016066 * lpfc_eq_destroy - Destroy an event Queue on the HBA
16067 * @eq: The queue structure associated with the queue to destroy.
16068 *
16069 * This function destroys a queue, as detailed in @eq by sending an mailbox
16070 * command, specific to the type of queue, to the HBA.
16071 *
16072 * The @eq struct is used to get the queue ID of the queue to destroy.
16073 *
16074 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016075 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016076 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016077int
James Smart4f774512009-05-22 14:52:35 -040016078lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
16079{
16080 LPFC_MBOXQ_t *mbox;
16081 int rc, length, status = 0;
16082 uint32_t shdr_status, shdr_add_status;
16083 union lpfc_sli4_cfg_shdr *shdr;
16084
James Smart2e90f4b2011-12-13 13:22:37 -050016085 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016086 if (!eq)
16087 return -ENODEV;
James Smart32517fc2019-01-28 11:14:33 -080016088
James Smart4f774512009-05-22 14:52:35 -040016089 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
16090 if (!mbox)
16091 return -ENOMEM;
16092 length = (sizeof(struct lpfc_mbx_eq_destroy) -
16093 sizeof(struct lpfc_sli4_cfg_mhdr));
16094 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16095 LPFC_MBOX_OPCODE_EQ_DESTROY,
16096 length, LPFC_SLI4_MBX_EMBED);
16097 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
16098 eq->queue_id);
16099 mbox->vport = eq->phba->pport;
16100 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16101
16102 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
16103 /* The IOCTL status is embedded in the mailbox subheader. */
16104 shdr = (union lpfc_sli4_cfg_shdr *)
16105 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
16106 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16107 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16108 if (shdr_status || shdr_add_status || rc) {
16109 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16110 "2505 EQ_DESTROY mailbox failed with "
16111 "status x%x add_status x%x, mbx status x%x\n",
16112 shdr_status, shdr_add_status, rc);
16113 status = -ENXIO;
16114 }
16115
16116 /* Remove eq from any list */
16117 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040016118 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016119 return status;
16120}
16121
16122/**
16123 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
16124 * @cq: The queue structure associated with the queue to destroy.
16125 *
16126 * This function destroys a queue, as detailed in @cq by sending an mailbox
16127 * command, specific to the type of queue, to the HBA.
16128 *
16129 * The @cq struct is used to get the queue ID of the queue to destroy.
16130 *
16131 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016132 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016133 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016134int
James Smart4f774512009-05-22 14:52:35 -040016135lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
16136{
16137 LPFC_MBOXQ_t *mbox;
16138 int rc, length, status = 0;
16139 uint32_t shdr_status, shdr_add_status;
16140 union lpfc_sli4_cfg_shdr *shdr;
16141
James Smart2e90f4b2011-12-13 13:22:37 -050016142 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016143 if (!cq)
16144 return -ENODEV;
16145 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
16146 if (!mbox)
16147 return -ENOMEM;
16148 length = (sizeof(struct lpfc_mbx_cq_destroy) -
16149 sizeof(struct lpfc_sli4_cfg_mhdr));
16150 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16151 LPFC_MBOX_OPCODE_CQ_DESTROY,
16152 length, LPFC_SLI4_MBX_EMBED);
16153 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
16154 cq->queue_id);
16155 mbox->vport = cq->phba->pport;
16156 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16157 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
16158 /* The IOCTL status is embedded in the mailbox subheader. */
16159 shdr = (union lpfc_sli4_cfg_shdr *)
16160 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
16161 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16162 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16163 if (shdr_status || shdr_add_status || rc) {
16164 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16165 "2506 CQ_DESTROY mailbox failed with "
16166 "status x%x add_status x%x, mbx status x%x\n",
16167 shdr_status, shdr_add_status, rc);
16168 status = -ENXIO;
16169 }
16170 /* Remove cq from any list */
16171 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040016172 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016173 return status;
16174}
16175
16176/**
James Smart04c68492009-05-22 14:52:52 -040016177 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
16178 * @qm: The queue structure associated with the queue to destroy.
16179 *
16180 * This function destroys a queue, as detailed in @mq by sending an mailbox
16181 * command, specific to the type of queue, to the HBA.
16182 *
16183 * The @mq struct is used to get the queue ID of the queue to destroy.
16184 *
16185 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016186 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040016187 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016188int
James Smart04c68492009-05-22 14:52:52 -040016189lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16190{
16191 LPFC_MBOXQ_t *mbox;
16192 int rc, length, status = 0;
16193 uint32_t shdr_status, shdr_add_status;
16194 union lpfc_sli4_cfg_shdr *shdr;
16195
James Smart2e90f4b2011-12-13 13:22:37 -050016196 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040016197 if (!mq)
16198 return -ENODEV;
16199 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16200 if (!mbox)
16201 return -ENOMEM;
16202 length = (sizeof(struct lpfc_mbx_mq_destroy) -
16203 sizeof(struct lpfc_sli4_cfg_mhdr));
16204 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16205 LPFC_MBOX_OPCODE_MQ_DESTROY,
16206 length, LPFC_SLI4_MBX_EMBED);
16207 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16208 mq->queue_id);
16209 mbox->vport = mq->phba->pport;
16210 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16211 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16212 /* The IOCTL status is embedded in the mailbox subheader. */
16213 shdr = (union lpfc_sli4_cfg_shdr *)
16214 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16215 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16216 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16217 if (shdr_status || shdr_add_status || rc) {
16218 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16219 "2507 MQ_DESTROY mailbox failed with "
16220 "status x%x add_status x%x, mbx status x%x\n",
16221 shdr_status, shdr_add_status, rc);
16222 status = -ENXIO;
16223 }
16224 /* Remove mq from any list */
16225 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040016226 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040016227 return status;
16228}
16229
16230/**
James Smart4f774512009-05-22 14:52:35 -040016231 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16232 * @wq: The queue structure associated with the queue to destroy.
16233 *
16234 * This function destroys a queue, as detailed in @wq by sending an mailbox
16235 * command, specific to the type of queue, to the HBA.
16236 *
16237 * The @wq struct is used to get the queue ID of the queue to destroy.
16238 *
16239 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016240 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016241 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016242int
James Smart4f774512009-05-22 14:52:35 -040016243lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16244{
16245 LPFC_MBOXQ_t *mbox;
16246 int rc, length, status = 0;
16247 uint32_t shdr_status, shdr_add_status;
16248 union lpfc_sli4_cfg_shdr *shdr;
16249
James Smart2e90f4b2011-12-13 13:22:37 -050016250 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016251 if (!wq)
16252 return -ENODEV;
16253 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16254 if (!mbox)
16255 return -ENOMEM;
16256 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16257 sizeof(struct lpfc_sli4_cfg_mhdr));
16258 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16259 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16260 length, LPFC_SLI4_MBX_EMBED);
16261 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16262 wq->queue_id);
16263 mbox->vport = wq->phba->pport;
16264 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16265 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16266 shdr = (union lpfc_sli4_cfg_shdr *)
16267 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16268 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16269 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16270 if (shdr_status || shdr_add_status || rc) {
16271 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16272 "2508 WQ_DESTROY mailbox failed with "
16273 "status x%x add_status x%x, mbx status x%x\n",
16274 shdr_status, shdr_add_status, rc);
16275 status = -ENXIO;
16276 }
16277 /* Remove wq from any list */
16278 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070016279 kfree(wq->pring);
16280 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040016281 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016282 return status;
16283}
16284
16285/**
16286 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16287 * @rq: The queue structure associated with the queue to destroy.
16288 *
16289 * This function destroys a queue, as detailed in @rq by sending an mailbox
16290 * command, specific to the type of queue, to the HBA.
16291 *
16292 * The @rq struct is used to get the queue ID of the queue to destroy.
16293 *
16294 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016295 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016296 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016297int
James Smart4f774512009-05-22 14:52:35 -040016298lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16299 struct lpfc_queue *drq)
16300{
16301 LPFC_MBOXQ_t *mbox;
16302 int rc, length, status = 0;
16303 uint32_t shdr_status, shdr_add_status;
16304 union lpfc_sli4_cfg_shdr *shdr;
16305
James Smart2e90f4b2011-12-13 13:22:37 -050016306 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016307 if (!hrq || !drq)
16308 return -ENODEV;
16309 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16310 if (!mbox)
16311 return -ENOMEM;
16312 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050016313 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040016314 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16315 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16316 length, LPFC_SLI4_MBX_EMBED);
16317 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16318 hrq->queue_id);
16319 mbox->vport = hrq->phba->pport;
16320 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16321 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16322 /* The IOCTL status is embedded in the mailbox subheader. */
16323 shdr = (union lpfc_sli4_cfg_shdr *)
16324 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16325 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16326 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16327 if (shdr_status || shdr_add_status || rc) {
16328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16329 "2509 RQ_DESTROY mailbox failed with "
16330 "status x%x add_status x%x, mbx status x%x\n",
16331 shdr_status, shdr_add_status, rc);
16332 if (rc != MBX_TIMEOUT)
16333 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16334 return -ENXIO;
16335 }
16336 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16337 drq->queue_id);
16338 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16339 shdr = (union lpfc_sli4_cfg_shdr *)
16340 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16341 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16342 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16343 if (shdr_status || shdr_add_status || rc) {
16344 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16345 "2510 RQ_DESTROY mailbox failed with "
16346 "status x%x add_status x%x, mbx status x%x\n",
16347 shdr_status, shdr_add_status, rc);
16348 status = -ENXIO;
16349 }
16350 list_del_init(&hrq->list);
16351 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040016352 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016353 return status;
16354}
16355
16356/**
16357 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16358 * @phba: The virtual port for which this call being executed.
16359 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16360 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16361 * @xritag: the xritag that ties this io to the SGL pages.
16362 *
16363 * This routine will post the sgl pages for the IO that has the xritag
16364 * that is in the iocbq structure. The xritag is assigned during iocbq
16365 * creation and persists for as long as the driver is loaded.
16366 * if the caller has fewer than 256 scatter gather segments to map then
16367 * pdma_phys_addr1 should be 0.
16368 * If the caller needs to map more than 256 scatter gather segment then
16369 * pdma_phys_addr1 should be a valid physical address.
16370 * physical address for SGLs must be 64 byte aligned.
16371 * If you are going to map 2 SGL's then the first one must have 256 entries
16372 * the second sgl can have between 1 and 256 entries.
16373 *
16374 * Return codes:
16375 * 0 - Success
16376 * -ENXIO, -ENOMEM - Failure
16377 **/
16378int
16379lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16380 dma_addr_t pdma_phys_addr0,
16381 dma_addr_t pdma_phys_addr1,
16382 uint16_t xritag)
16383{
16384 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16385 LPFC_MBOXQ_t *mbox;
16386 int rc;
16387 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040016388 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040016389 union lpfc_sli4_cfg_shdr *shdr;
16390
16391 if (xritag == NO_XRI) {
16392 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16393 "0364 Invalid param:\n");
16394 return -EINVAL;
16395 }
16396
16397 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16398 if (!mbox)
16399 return -ENOMEM;
16400
16401 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16402 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16403 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050016404 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040016405
16406 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16407 &mbox->u.mqe.un.post_sgl_pages;
16408 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16409 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16410
16411 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16412 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16413 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16414 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16415
16416 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16417 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16418 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16419 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16420 if (!phba->sli4_hba.intr_enable)
16421 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040016422 else {
James Smarta183a152011-10-10 21:32:43 -040016423 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040016424 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16425 }
James Smart4f774512009-05-22 14:52:35 -040016426 /* The IOCTL status is embedded in the mailbox subheader. */
16427 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16428 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16429 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16430 if (rc != MBX_TIMEOUT)
16431 mempool_free(mbox, phba->mbox_mem_pool);
16432 if (shdr_status || shdr_add_status || rc) {
16433 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16434 "2511 POST_SGL mailbox failed with "
16435 "status x%x add_status x%x, mbx status x%x\n",
16436 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040016437 }
16438 return 0;
16439}
James Smart4f774512009-05-22 14:52:35 -040016440
16441/**
James Smart88a2cfb2011-07-22 18:36:33 -040016442 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040016443 * @phba: pointer to lpfc hba data structure.
16444 *
16445 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040016446 * HBA consistent with the SLI-4 interface spec. This routine
16447 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16448 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040016449 *
James Smart88a2cfb2011-07-22 18:36:33 -040016450 * Returns
16451 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16452 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16453 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016454static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016455lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16456{
16457 unsigned long xri;
16458
16459 /*
16460 * Fetch the next logical xri. Because this index is logical,
16461 * the driver starts at 0 each time.
16462 */
16463 spin_lock_irq(&phba->hbalock);
16464 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16465 phba->sli4_hba.max_cfg_param.max_xri, 0);
16466 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16467 spin_unlock_irq(&phba->hbalock);
16468 return NO_XRI;
16469 } else {
16470 set_bit(xri, phba->sli4_hba.xri_bmask);
16471 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040016472 }
James Smart6d368e52011-05-24 11:44:12 -040016473 spin_unlock_irq(&phba->hbalock);
16474 return xri;
16475}
16476
16477/**
16478 * lpfc_sli4_free_xri - Release an xri for reuse.
16479 * @phba: pointer to lpfc hba data structure.
16480 *
16481 * This routine is invoked to release an xri to the pool of
16482 * available rpis maintained by the driver.
16483 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016484static void
James Smart6d368e52011-05-24 11:44:12 -040016485__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16486{
16487 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016488 phba->sli4_hba.max_cfg_param.xri_used--;
16489 }
16490}
16491
16492/**
16493 * lpfc_sli4_free_xri - Release an xri for reuse.
16494 * @phba: pointer to lpfc hba data structure.
16495 *
16496 * This routine is invoked to release an xri to the pool of
16497 * available rpis maintained by the driver.
16498 **/
16499void
16500lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16501{
16502 spin_lock_irq(&phba->hbalock);
16503 __lpfc_sli4_free_xri(phba, xri);
16504 spin_unlock_irq(&phba->hbalock);
16505}
16506
16507/**
James Smart4f774512009-05-22 14:52:35 -040016508 * lpfc_sli4_next_xritag - Get an xritag for the io
16509 * @phba: Pointer to HBA context object.
16510 *
16511 * This function gets an xritag for the iocb. If there is no unused xritag
16512 * it will return 0xffff.
16513 * The function returns the allocated xritag if successful, else returns zero.
16514 * Zero is not a valid xritag.
16515 * The caller is not required to hold any lock.
16516 **/
16517uint16_t
16518lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16519{
James Smart6d368e52011-05-24 11:44:12 -040016520 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016521
James Smart6d368e52011-05-24 11:44:12 -040016522 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016523 if (xri_index == NO_XRI)
16524 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16525 "2004 Failed to allocate XRI.last XRITAG is %d"
16526 " Max XRI is %d, Used XRI is %d\n",
16527 xri_index,
16528 phba->sli4_hba.max_cfg_param.max_xri,
16529 phba->sli4_hba.max_cfg_param.xri_used);
16530 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016531}
16532
16533/**
James Smart895427b2017-02-12 13:52:30 -080016534 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016535 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016536 * @post_sgl_list: pointer to els sgl entry list.
16537 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016538 *
16539 * This routine is invoked to post a block of driver's sgl pages to the
16540 * HBA using non-embedded mailbox command. No Lock is held. This routine
16541 * is only called when the driver is loading and after all IO has been
16542 * stopped.
16543 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016544static int
James Smart895427b2017-02-12 13:52:30 -080016545lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016546 struct list_head *post_sgl_list,
16547 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016548{
James Smart8a9d2e82012-05-09 21:16:12 -040016549 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016550 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16551 struct sgl_page_pairs *sgl_pg_pairs;
16552 void *viraddr;
16553 LPFC_MBOXQ_t *mbox;
16554 uint32_t reqlen, alloclen, pg_pairs;
16555 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016556 uint16_t xritag_start = 0;
16557 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016558 uint32_t shdr_status, shdr_add_status;
16559 union lpfc_sli4_cfg_shdr *shdr;
16560
James Smart895427b2017-02-12 13:52:30 -080016561 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016562 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016563 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016564 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016565 "2559 Block sgl registration required DMA "
16566 "size (%d) great than a page\n", reqlen);
16567 return -ENOMEM;
16568 }
James Smart895427b2017-02-12 13:52:30 -080016569
James Smart4f774512009-05-22 14:52:35 -040016570 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016571 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016572 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016573
16574 /* Allocate DMA memory and set up the non-embedded mailbox command */
16575 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16576 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16577 LPFC_SLI4_MBX_NEMBED);
16578
16579 if (alloclen < reqlen) {
16580 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16581 "0285 Allocated DMA memory size (%d) is "
16582 "less than the requested DMA memory "
16583 "size (%d)\n", alloclen, reqlen);
16584 lpfc_sli4_mbox_cmd_free(phba, mbox);
16585 return -ENOMEM;
16586 }
James Smart4f774512009-05-22 14:52:35 -040016587 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016588 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016589 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16590 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16591
James Smart8a9d2e82012-05-09 21:16:12 -040016592 pg_pairs = 0;
16593 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016594 /* Set up the sge entry */
16595 sgl_pg_pairs->sgl_pg0_addr_lo =
16596 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16597 sgl_pg_pairs->sgl_pg0_addr_hi =
16598 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16599 sgl_pg_pairs->sgl_pg1_addr_lo =
16600 cpu_to_le32(putPaddrLow(0));
16601 sgl_pg_pairs->sgl_pg1_addr_hi =
16602 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016603
James Smart4f774512009-05-22 14:52:35 -040016604 /* Keep the first xritag on the list */
16605 if (pg_pairs == 0)
16606 xritag_start = sglq_entry->sli4_xritag;
16607 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016608 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016609 }
James Smart6d368e52011-05-24 11:44:12 -040016610
16611 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016612 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016613 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016614 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016615
James Smart4f774512009-05-22 14:52:35 -040016616 if (!phba->sli4_hba.intr_enable)
16617 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16618 else {
James Smarta183a152011-10-10 21:32:43 -040016619 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016620 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16621 }
16622 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16623 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16624 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16625 if (rc != MBX_TIMEOUT)
16626 lpfc_sli4_mbox_cmd_free(phba, mbox);
16627 if (shdr_status || shdr_add_status || rc) {
16628 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16629 "2513 POST_SGL_BLOCK mailbox command failed "
16630 "status x%x add_status x%x mbx status x%x\n",
16631 shdr_status, shdr_add_status, rc);
16632 rc = -ENXIO;
16633 }
16634 return rc;
16635}
16636
16637/**
James Smart5e5b5112019-01-28 11:14:22 -080016638 * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
James Smart4f774512009-05-22 14:52:35 -040016639 * @phba: pointer to lpfc hba data structure.
James Smart0794d602019-01-28 11:14:19 -080016640 * @nblist: pointer to nvme buffer list.
James Smart4f774512009-05-22 14:52:35 -040016641 * @count: number of scsi buffers on the list.
16642 *
16643 * This routine is invoked to post a block of @count scsi sgl pages from a
James Smart0794d602019-01-28 11:14:19 -080016644 * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
James Smart4f774512009-05-22 14:52:35 -040016645 * No Lock is held.
16646 *
16647 **/
James Smart0794d602019-01-28 11:14:19 -080016648static int
James Smart5e5b5112019-01-28 11:14:22 -080016649lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
16650 int count)
James Smart4f774512009-05-22 14:52:35 -040016651{
James Smartc4908502019-01-28 11:14:28 -080016652 struct lpfc_io_buf *lpfc_ncmd;
James Smart4f774512009-05-22 14:52:35 -040016653 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16654 struct sgl_page_pairs *sgl_pg_pairs;
16655 void *viraddr;
16656 LPFC_MBOXQ_t *mbox;
16657 uint32_t reqlen, alloclen, pg_pairs;
16658 uint32_t mbox_tmo;
16659 uint16_t xritag_start = 0;
16660 int rc = 0;
16661 uint32_t shdr_status, shdr_add_status;
16662 dma_addr_t pdma_phys_bpl1;
16663 union lpfc_sli4_cfg_shdr *shdr;
16664
16665 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016666 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016667 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016668 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016669 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016670 "6118 Block sgl registration required DMA "
James Smart4f774512009-05-22 14:52:35 -040016671 "size (%d) great than a page\n", reqlen);
16672 return -ENOMEM;
16673 }
16674 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16675 if (!mbox) {
16676 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016677 "6119 Failed to allocate mbox cmd memory\n");
James Smart4f774512009-05-22 14:52:35 -040016678 return -ENOMEM;
16679 }
16680
16681 /* Allocate DMA memory and set up the non-embedded mailbox command */
16682 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
James Smart0794d602019-01-28 11:14:19 -080016683 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16684 reqlen, LPFC_SLI4_MBX_NEMBED);
James Smart4f774512009-05-22 14:52:35 -040016685
16686 if (alloclen < reqlen) {
16687 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016688 "6120 Allocated DMA memory size (%d) is "
James Smart4f774512009-05-22 14:52:35 -040016689 "less than the requested DMA memory "
16690 "size (%d)\n", alloclen, reqlen);
16691 lpfc_sli4_mbox_cmd_free(phba, mbox);
16692 return -ENOMEM;
16693 }
James Smart6d368e52011-05-24 11:44:12 -040016694
James Smart4f774512009-05-22 14:52:35 -040016695 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016696 viraddr = mbox->sge_array->addr[0];
16697
16698 /* Set up the SGL pages in the non-embedded DMA pages */
16699 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16700 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16701
16702 pg_pairs = 0;
James Smart0794d602019-01-28 11:14:19 -080016703 list_for_each_entry(lpfc_ncmd, nblist, list) {
James Smart4f774512009-05-22 14:52:35 -040016704 /* Set up the sge entry */
16705 sgl_pg_pairs->sgl_pg0_addr_lo =
James Smart0794d602019-01-28 11:14:19 -080016706 cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040016707 sgl_pg_pairs->sgl_pg0_addr_hi =
James Smart0794d602019-01-28 11:14:19 -080016708 cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040016709 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
James Smart0794d602019-01-28 11:14:19 -080016710 pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
16711 SGL_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040016712 else
16713 pdma_phys_bpl1 = 0;
16714 sgl_pg_pairs->sgl_pg1_addr_lo =
16715 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16716 sgl_pg_pairs->sgl_pg1_addr_hi =
16717 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16718 /* Keep the first xritag on the list */
16719 if (pg_pairs == 0)
James Smart0794d602019-01-28 11:14:19 -080016720 xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -040016721 sgl_pg_pairs++;
16722 pg_pairs++;
16723 }
16724 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16725 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16726 /* Perform endian conversion if necessary */
16727 sgl->word0 = cpu_to_le32(sgl->word0);
16728
James Smart0794d602019-01-28 11:14:19 -080016729 if (!phba->sli4_hba.intr_enable) {
James Smart4f774512009-05-22 14:52:35 -040016730 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart0794d602019-01-28 11:14:19 -080016731 } else {
James Smarta183a152011-10-10 21:32:43 -040016732 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016733 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16734 }
James Smart0794d602019-01-28 11:14:19 -080016735 shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040016736 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16737 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16738 if (rc != MBX_TIMEOUT)
16739 lpfc_sli4_mbox_cmd_free(phba, mbox);
16740 if (shdr_status || shdr_add_status || rc) {
16741 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -080016742 "6125 POST_SGL_BLOCK mailbox command failed "
James Smart4f774512009-05-22 14:52:35 -040016743 "status x%x add_status x%x mbx status x%x\n",
16744 shdr_status, shdr_add_status, rc);
16745 rc = -ENXIO;
16746 }
16747 return rc;
16748}
16749
16750/**
James Smart5e5b5112019-01-28 11:14:22 -080016751 * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
James Smart0794d602019-01-28 11:14:19 -080016752 * @phba: pointer to lpfc hba data structure.
16753 * @post_nblist: pointer to the nvme buffer list.
16754 *
16755 * This routine walks a list of nvme buffers that was passed in. It attempts
16756 * to construct blocks of nvme buffer sgls which contains contiguous xris and
16757 * uses the non-embedded SGL block post mailbox commands to post to the port.
16758 * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
16759 * embedded SGL post mailbox command for posting. The @post_nblist passed in
16760 * must be local list, thus no lock is needed when manipulate the list.
16761 *
16762 * Returns: 0 = failure, non-zero number of successfully posted buffers.
16763 **/
16764int
James Smart5e5b5112019-01-28 11:14:22 -080016765lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
16766 struct list_head *post_nblist, int sb_count)
James Smart0794d602019-01-28 11:14:19 -080016767{
James Smartc4908502019-01-28 11:14:28 -080016768 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart0794d602019-01-28 11:14:19 -080016769 int status, sgl_size;
16770 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
16771 dma_addr_t pdma_phys_sgl1;
16772 int last_xritag = NO_XRI;
16773 int cur_xritag;
James Smart0794d602019-01-28 11:14:19 -080016774 LIST_HEAD(prep_nblist);
16775 LIST_HEAD(blck_nblist);
16776 LIST_HEAD(nvme_nblist);
16777
16778 /* sanity check */
16779 if (sb_count <= 0)
16780 return -EINVAL;
16781
16782 sgl_size = phba->cfg_sg_dma_buf_size;
16783 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
16784 list_del_init(&lpfc_ncmd->list);
16785 block_cnt++;
16786 if ((last_xritag != NO_XRI) &&
16787 (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
16788 /* a hole in xri block, form a sgl posting block */
16789 list_splice_init(&prep_nblist, &blck_nblist);
16790 post_cnt = block_cnt - 1;
16791 /* prepare list for next posting block */
16792 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
16793 block_cnt = 1;
16794 } else {
16795 /* prepare list for next posting block */
16796 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
16797 /* enough sgls for non-embed sgl mbox command */
16798 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
16799 list_splice_init(&prep_nblist, &blck_nblist);
16800 post_cnt = block_cnt;
16801 block_cnt = 0;
16802 }
16803 }
16804 num_posting++;
16805 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
16806
16807 /* end of repost sgl list condition for NVME buffers */
16808 if (num_posting == sb_count) {
16809 if (post_cnt == 0) {
16810 /* last sgl posting block */
16811 list_splice_init(&prep_nblist, &blck_nblist);
16812 post_cnt = block_cnt;
16813 } else if (block_cnt == 1) {
16814 /* last single sgl with non-contiguous xri */
16815 if (sgl_size > SGL_PAGE_SIZE)
16816 pdma_phys_sgl1 =
16817 lpfc_ncmd->dma_phys_sgl +
16818 SGL_PAGE_SIZE;
16819 else
16820 pdma_phys_sgl1 = 0;
16821 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
16822 status = lpfc_sli4_post_sgl(
16823 phba, lpfc_ncmd->dma_phys_sgl,
16824 pdma_phys_sgl1, cur_xritag);
16825 if (status) {
James Smartc4908502019-01-28 11:14:28 -080016826 /* Post error. Buffer unavailable. */
16827 lpfc_ncmd->flags |=
16828 LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016829 } else {
James Smartc4908502019-01-28 11:14:28 -080016830 /* Post success. Bffer available. */
16831 lpfc_ncmd->flags &=
16832 ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016833 lpfc_ncmd->status = IOSTAT_SUCCESS;
16834 num_posted++;
16835 }
16836 /* success, put on NVME buffer sgl list */
16837 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
16838 }
16839 }
16840
16841 /* continue until a nembed page worth of sgls */
16842 if (post_cnt == 0)
16843 continue;
16844
16845 /* post block of NVME buffer list sgls */
James Smart5e5b5112019-01-28 11:14:22 -080016846 status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
16847 post_cnt);
James Smart0794d602019-01-28 11:14:19 -080016848
16849 /* don't reset xirtag due to hole in xri block */
16850 if (block_cnt == 0)
16851 last_xritag = NO_XRI;
16852
16853 /* reset NVME buffer post count for next round of posting */
16854 post_cnt = 0;
16855
16856 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
16857 while (!list_empty(&blck_nblist)) {
16858 list_remove_head(&blck_nblist, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -080016859 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -080016860 if (status) {
James Smartc4908502019-01-28 11:14:28 -080016861 /* Post error. Mark buffer unavailable. */
16862 lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016863 } else {
James Smartc4908502019-01-28 11:14:28 -080016864 /* Post success, Mark buffer available. */
16865 lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016866 lpfc_ncmd->status = IOSTAT_SUCCESS;
16867 num_posted++;
16868 }
16869 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
16870 }
16871 }
16872 /* Push NVME buffers with sgl posted to the available list */
James Smart5e5b5112019-01-28 11:14:22 -080016873 lpfc_io_buf_replenish(phba, &nvme_nblist);
16874
James Smart0794d602019-01-28 11:14:19 -080016875 return num_posted;
16876}
16877
16878/**
James Smart4f774512009-05-22 14:52:35 -040016879 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16880 * @phba: pointer to lpfc_hba struct that the frame was received on
16881 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16882 *
16883 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16884 * valid type of frame that the LPFC driver will handle. This function will
16885 * return a zero if the frame is a valid frame or a non zero value when the
16886 * frame does not pass the check.
16887 **/
16888static int
16889lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16890{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016891 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016892 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016893 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016894
James Smarte62245d2019-08-14 16:57:08 -070016895#define FC_RCTL_MDS_DIAGS 0xF4
16896
James Smart4f774512009-05-22 14:52:35 -040016897 switch (fc_hdr->fh_r_ctl) {
16898 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16899 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16900 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16901 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16902 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16903 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16904 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16905 case FC_RCTL_DD_CMD_STATUS: /* command status */
16906 case FC_RCTL_ELS_REQ: /* extended link services request */
16907 case FC_RCTL_ELS_REP: /* extended link services reply */
16908 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16909 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16910 case FC_RCTL_BA_NOP: /* basic link service NOP */
16911 case FC_RCTL_BA_ABTS: /* basic link service abort */
16912 case FC_RCTL_BA_RMC: /* remove connection */
16913 case FC_RCTL_BA_ACC: /* basic accept */
16914 case FC_RCTL_BA_RJT: /* basic reject */
16915 case FC_RCTL_BA_PRMT:
16916 case FC_RCTL_ACK_1: /* acknowledge_1 */
16917 case FC_RCTL_ACK_0: /* acknowledge_0 */
16918 case FC_RCTL_P_RJT: /* port reject */
16919 case FC_RCTL_F_RJT: /* fabric reject */
16920 case FC_RCTL_P_BSY: /* port busy */
16921 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16922 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16923 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016924 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016925 case FC_RCTL_END: /* end */
16926 break;
16927 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16928 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16929 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16930 return lpfc_fc_frame_check(phba, fc_hdr);
16931 default:
16932 goto drop;
16933 }
James Smartae9e28f2017-05-15 15:20:51 -070016934
James Smart4f774512009-05-22 14:52:35 -040016935 switch (fc_hdr->fh_type) {
16936 case FC_TYPE_BLS:
16937 case FC_TYPE_ELS:
16938 case FC_TYPE_FCP:
16939 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016940 case FC_TYPE_NVME:
James Smart4f774512009-05-22 14:52:35 -040016941 break;
16942 case FC_TYPE_IP:
16943 case FC_TYPE_ILS:
16944 default:
16945 goto drop;
16946 }
James Smart546fc852011-03-11 16:06:29 -050016947
James Smart4f774512009-05-22 14:52:35 -040016948 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016949 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040016950 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070016951 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16952 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16953 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16954 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16955 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016956 return 0;
16957drop:
16958 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016959 "2539 Dropped frame rctl:x%x type:x%x\n",
16960 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040016961 return 1;
16962}
16963
16964/**
16965 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16966 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16967 *
16968 * This function processes the FC header to retrieve the VFI from the VF
16969 * header, if one exists. This function will return the VFI if one exists
16970 * or 0 if no VSAN Header exists.
16971 **/
16972static uint32_t
16973lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16974{
16975 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16976
16977 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16978 return 0;
16979 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16980}
16981
16982/**
16983 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16984 * @phba: Pointer to the HBA structure to search for the vport on
16985 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16986 * @fcfi: The FC Fabric ID that the frame came from
16987 *
16988 * This function searches the @phba for a vport that matches the content of the
16989 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16990 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16991 * returns the matching vport pointer or NULL if unable to match frame to a
16992 * vport.
16993 **/
16994static struct lpfc_vport *
16995lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016996 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016997{
16998 struct lpfc_vport **vports;
16999 struct lpfc_vport *vport = NULL;
17000 int i;
James Smart939723a2012-05-09 21:19:03 -040017001
James Smartbf086112011-08-21 21:48:13 -040017002 if (did == Fabric_DID)
17003 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040017004 if ((phba->pport->fc_flag & FC_PT2PT) &&
17005 !(phba->link_state == LPFC_HBA_READY))
17006 return phba->pport;
17007
James Smart4f774512009-05-22 14:52:35 -040017008 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080017009 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040017010 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
17011 if (phba->fcf.fcfi == fcfi &&
17012 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
17013 vports[i]->fc_myDID == did) {
17014 vport = vports[i];
17015 break;
17016 }
17017 }
James Smart895427b2017-02-12 13:52:30 -080017018 }
James Smart4f774512009-05-22 14:52:35 -040017019 lpfc_destroy_vport_work_array(phba, vports);
17020 return vport;
17021}
17022
17023/**
James Smart45ed1192009-10-02 15:17:02 -040017024 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
17025 * @vport: The vport to work on.
17026 *
17027 * This function updates the receive sequence time stamp for this vport. The
17028 * receive sequence time stamp indicates the time that the last frame of the
17029 * the sequence that has been idle for the longest amount of time was received.
17030 * the driver uses this time stamp to indicate if any received sequences have
17031 * timed out.
17032 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017033static void
James Smart45ed1192009-10-02 15:17:02 -040017034lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
17035{
17036 struct lpfc_dmabuf *h_buf;
17037 struct hbq_dmabuf *dmabuf = NULL;
17038
17039 /* get the oldest sequence on the rcv list */
17040 h_buf = list_get_first(&vport->rcv_buffer_list,
17041 struct lpfc_dmabuf, list);
17042 if (!h_buf)
17043 return;
17044 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17045 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
17046}
17047
17048/**
17049 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
17050 * @vport: The vport that the received sequences were sent to.
17051 *
17052 * This function cleans up all outstanding received sequences. This is called
17053 * by the driver when a link event or user action invalidates all the received
17054 * sequences.
17055 **/
17056void
17057lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
17058{
17059 struct lpfc_dmabuf *h_buf, *hnext;
17060 struct lpfc_dmabuf *d_buf, *dnext;
17061 struct hbq_dmabuf *dmabuf = NULL;
17062
17063 /* start with the oldest sequence on the rcv list */
17064 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17065 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17066 list_del_init(&dmabuf->hbuf.list);
17067 list_for_each_entry_safe(d_buf, dnext,
17068 &dmabuf->dbuf.list, list) {
17069 list_del_init(&d_buf->list);
17070 lpfc_in_buf_free(vport->phba, d_buf);
17071 }
17072 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17073 }
17074}
17075
17076/**
17077 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
17078 * @vport: The vport that the received sequences were sent to.
17079 *
17080 * This function determines whether any received sequences have timed out by
17081 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
17082 * indicates that there is at least one timed out sequence this routine will
17083 * go through the received sequences one at a time from most inactive to most
17084 * active to determine which ones need to be cleaned up. Once it has determined
17085 * that a sequence needs to be cleaned up it will simply free up the resources
17086 * without sending an abort.
17087 **/
17088void
17089lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
17090{
17091 struct lpfc_dmabuf *h_buf, *hnext;
17092 struct lpfc_dmabuf *d_buf, *dnext;
17093 struct hbq_dmabuf *dmabuf = NULL;
17094 unsigned long timeout;
17095 int abort_count = 0;
17096
17097 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17098 vport->rcv_buffer_time_stamp);
17099 if (list_empty(&vport->rcv_buffer_list) ||
17100 time_before(jiffies, timeout))
17101 return;
17102 /* start with the oldest sequence on the rcv list */
17103 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17104 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17105 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17106 dmabuf->time_stamp);
17107 if (time_before(jiffies, timeout))
17108 break;
17109 abort_count++;
17110 list_del_init(&dmabuf->hbuf.list);
17111 list_for_each_entry_safe(d_buf, dnext,
17112 &dmabuf->dbuf.list, list) {
17113 list_del_init(&d_buf->list);
17114 lpfc_in_buf_free(vport->phba, d_buf);
17115 }
17116 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17117 }
17118 if (abort_count)
17119 lpfc_update_rcv_time_stamp(vport);
17120}
17121
17122/**
James Smart4f774512009-05-22 14:52:35 -040017123 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
17124 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
17125 *
17126 * This function searches through the existing incomplete sequences that have
17127 * been sent to this @vport. If the frame matches one of the incomplete
17128 * sequences then the dbuf in the @dmabuf is added to the list of frames that
17129 * make up that sequence. If no sequence is found that matches this frame then
17130 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
17131 * This function returns a pointer to the first dmabuf in the sequence list that
17132 * the frame was linked to.
17133 **/
17134static struct hbq_dmabuf *
17135lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17136{
17137 struct fc_frame_header *new_hdr;
17138 struct fc_frame_header *temp_hdr;
17139 struct lpfc_dmabuf *d_buf;
17140 struct lpfc_dmabuf *h_buf;
17141 struct hbq_dmabuf *seq_dmabuf = NULL;
17142 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050017143 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040017144
James Smart4d9ab992009-10-02 15:16:39 -040017145 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017146 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040017147 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050017148
James Smart4f774512009-05-22 14:52:35 -040017149 /* Use the hdr_buf to find the sequence that this frame belongs to */
17150 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17151 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17152 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17153 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17154 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17155 continue;
17156 /* found a pending sequence that matches this frame */
17157 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17158 break;
17159 }
17160 if (!seq_dmabuf) {
17161 /*
17162 * This indicates first frame received for this sequence.
17163 * Queue the buffer on the vport's rcv_buffer_list.
17164 */
17165 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040017166 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017167 return dmabuf;
17168 }
17169 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050017170 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
17171 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040017172 list_del_init(&seq_dmabuf->hbuf.list);
17173 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17174 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017175 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017176 return dmabuf;
17177 }
James Smart45ed1192009-10-02 15:17:02 -040017178 /* move this sequence to the tail to indicate a young sequence */
17179 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
17180 seq_dmabuf->time_stamp = jiffies;
17181 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050017182 if (list_empty(&seq_dmabuf->dbuf.list)) {
17183 temp_hdr = dmabuf->hbuf.virt;
17184 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17185 return seq_dmabuf;
17186 }
James Smart4f774512009-05-22 14:52:35 -040017187 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050017188 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
17189 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040017190 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17191 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
17192 /*
17193 * If the frame's sequence count is greater than the frame on
17194 * the list then insert the frame right after this frame
17195 */
James Smarteeead812009-12-21 17:01:23 -050017196 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
17197 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040017198 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050017199 found = 1;
17200 break;
James Smart4f774512009-05-22 14:52:35 -040017201 }
James Smart4360ca92015-12-16 18:12:04 -050017202
17203 if (&d_buf->list == &seq_dmabuf->dbuf.list)
17204 break;
17205 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040017206 }
James Smart4360ca92015-12-16 18:12:04 -050017207
17208 if (found)
17209 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017210 return NULL;
17211}
17212
17213/**
James Smart6669f9b2009-10-02 15:16:45 -040017214 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
17215 * @vport: pointer to a vitural port
17216 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17217 *
17218 * This function tries to abort from the partially assembed sequence, described
17219 * by the information from basic abbort @dmabuf. It checks to see whether such
17220 * partially assembled sequence held by the driver. If so, it shall free up all
17221 * the frames from the partially assembled sequence.
17222 *
17223 * Return
17224 * true -- if there is matching partially assembled sequence present and all
17225 * the frames freed with the sequence;
17226 * false -- if there is no matching partially assembled sequence present so
17227 * nothing got aborted in the lower layer driver
17228 **/
17229static bool
17230lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
17231 struct hbq_dmabuf *dmabuf)
17232{
17233 struct fc_frame_header *new_hdr;
17234 struct fc_frame_header *temp_hdr;
17235 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
17236 struct hbq_dmabuf *seq_dmabuf = NULL;
17237
17238 /* Use the hdr_buf to find the sequence that matches this frame */
17239 INIT_LIST_HEAD(&dmabuf->dbuf.list);
17240 INIT_LIST_HEAD(&dmabuf->hbuf.list);
17241 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17242 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17243 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17244 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17245 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17246 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17247 continue;
17248 /* found a pending sequence that matches this frame */
17249 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17250 break;
17251 }
17252
17253 /* Free up all the frames from the partially assembled sequence */
17254 if (seq_dmabuf) {
17255 list_for_each_entry_safe(d_buf, n_buf,
17256 &seq_dmabuf->dbuf.list, list) {
17257 list_del_init(&d_buf->list);
17258 lpfc_in_buf_free(vport->phba, d_buf);
17259 }
17260 return true;
17261 }
17262 return false;
17263}
17264
17265/**
James Smart6dd9e312013-01-03 15:43:37 -050017266 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
17267 * @vport: pointer to a vitural port
17268 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17269 *
17270 * This function tries to abort from the assembed sequence from upper level
17271 * protocol, described by the information from basic abbort @dmabuf. It
17272 * checks to see whether such pending context exists at upper level protocol.
17273 * If so, it shall clean up the pending context.
17274 *
17275 * Return
17276 * true -- if there is matching pending context of the sequence cleaned
17277 * at ulp;
17278 * false -- if there is no matching pending context of the sequence present
17279 * at ulp.
17280 **/
17281static bool
17282lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17283{
17284 struct lpfc_hba *phba = vport->phba;
17285 int handled;
17286
17287 /* Accepting abort at ulp with SLI4 only */
17288 if (phba->sli_rev < LPFC_SLI_REV4)
17289 return false;
17290
17291 /* Register all caring upper level protocols to attend abort */
17292 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
17293 if (handled)
17294 return true;
17295
17296 return false;
17297}
17298
17299/**
James Smart546fc852011-03-11 16:06:29 -050017300 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040017301 * @phba: Pointer to HBA context object.
17302 * @cmd_iocbq: pointer to the command iocbq structure.
17303 * @rsp_iocbq: pointer to the response iocbq structure.
17304 *
James Smart546fc852011-03-11 16:06:29 -050017305 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040017306 * event. It properly releases the memory allocated to the sequence abort
17307 * accept iocb.
17308 **/
17309static void
James Smart546fc852011-03-11 16:06:29 -050017310lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040017311 struct lpfc_iocbq *cmd_iocbq,
17312 struct lpfc_iocbq *rsp_iocbq)
17313{
James Smart6dd9e312013-01-03 15:43:37 -050017314 struct lpfc_nodelist *ndlp;
17315
17316 if (cmd_iocbq) {
17317 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
17318 lpfc_nlp_put(ndlp);
17319 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017320 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050017321 }
James Smart6b5151f2012-01-18 16:24:06 -050017322
17323 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
17324 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
17325 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17326 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
17327 rsp_iocbq->iocb.ulpStatus,
17328 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040017329}
17330
17331/**
James Smart6d368e52011-05-24 11:44:12 -040017332 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
17333 * @phba: Pointer to HBA context object.
17334 * @xri: xri id in transaction.
17335 *
17336 * This function validates the xri maps to the known range of XRIs allocated an
17337 * used by the driver.
17338 **/
James Smart7851fe22011-07-22 18:36:52 -040017339uint16_t
James Smart6d368e52011-05-24 11:44:12 -040017340lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
17341 uint16_t xri)
17342{
James Smarta2fc4aef2014-09-03 12:57:55 -040017343 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040017344
17345 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
17346 if (xri == phba->sli4_hba.xri_ids[i])
17347 return i;
17348 }
17349 return NO_XRI;
17350}
17351
James Smart6d368e52011-05-24 11:44:12 -040017352/**
James Smart546fc852011-03-11 16:06:29 -050017353 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017354 * @phba: Pointer to HBA context object.
17355 * @fc_hdr: pointer to a FC frame header.
17356 *
James Smart546fc852011-03-11 16:06:29 -050017357 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017358 * event after aborting the sequence handling.
17359 **/
James Smart86c67372017-04-21 16:05:04 -070017360void
James Smart6dd9e312013-01-03 15:43:37 -050017361lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17362 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040017363{
James Smart6dd9e312013-01-03 15:43:37 -050017364 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040017365 struct lpfc_iocbq *ctiocb = NULL;
17366 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040017367 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050017368 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040017369 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050017370 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040017371
17372 if (!lpfc_is_link_up(phba))
17373 return;
17374
17375 sid = sli4_sid_from_fc_hdr(fc_hdr);
17376 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050017377 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040017378
James Smart6dd9e312013-01-03 15:43:37 -050017379 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040017380 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070017381 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050017382 if (!ndlp) {
17383 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17384 "1268 Failed to allocate ndlp for "
17385 "oxid:x%x SID:x%x\n", oxid, sid);
17386 return;
17387 }
James Smart6dd9e312013-01-03 15:43:37 -050017388 /* Put ndlp onto pport node list */
17389 lpfc_enqueue_node(vport, ndlp);
17390 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17391 /* re-setup ndlp without removing from node list */
17392 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17393 if (!ndlp) {
17394 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17395 "3275 Failed to active ndlp found "
17396 "for oxid:x%x SID:x%x\n", oxid, sid);
17397 return;
17398 }
James Smart6669f9b2009-10-02 15:16:45 -040017399 }
17400
James Smart546fc852011-03-11 16:06:29 -050017401 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040017402 ctiocb = lpfc_sli_get_iocbq(phba);
17403 if (!ctiocb)
17404 return;
17405
James Smart5ffc2662009-11-18 15:39:44 -050017406 /* Extract the F_CTL field from FC_HDR */
17407 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17408
James Smart6669f9b2009-10-02 15:16:45 -040017409 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040017410 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050017411 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040017412 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17413 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17414 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17415
17416 /* Fill in the rest of iocb fields */
17417 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17418 icmd->ulpBdeCount = 0;
17419 icmd->ulpLe = 1;
17420 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040017421 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050017422 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017423
James Smart6669f9b2009-10-02 15:16:45 -040017424 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050017425 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040017426 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050017427 ctiocb->sli4_xritag = NO_XRI;
17428
James Smartee0f4fe2012-05-09 21:19:14 -040017429 if (fctl & FC_FC_EX_CTX)
17430 /* Exchange responder sent the abort so we
17431 * own the oxid.
17432 */
17433 xri = oxid;
17434 else
17435 xri = rxid;
17436 lxri = lpfc_sli4_xri_inrange(phba, xri);
17437 if (lxri != NO_XRI)
17438 lpfc_set_rrq_active(phba, ndlp, lxri,
17439 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050017440 /* For BA_ABTS from exchange responder, if the logical xri with
17441 * the oxid maps to the FCP XRI range, the port no longer has
17442 * that exchange context, send a BLS_RJT. Override the IOCB for
17443 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050017444 */
James Smart6dd9e312013-01-03 15:43:37 -050017445 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080017446 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050017447 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17448 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17449 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17450 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17451 }
17452
17453 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17454 * the driver no longer has that exchange, send a BLS_RJT. Override
17455 * the IOCB for a BA_RJT.
17456 */
17457 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050017458 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17459 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17460 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17461 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17462 }
James Smart6669f9b2009-10-02 15:16:45 -040017463
James Smart5ffc2662009-11-18 15:39:44 -050017464 if (fctl & FC_FC_EX_CTX) {
17465 /* ABTS sent by responder to CT exchange, construction
17466 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17467 * field and RX_ID from ABTS for RX_ID field.
17468 */
James Smart546fc852011-03-11 16:06:29 -050017469 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050017470 } else {
17471 /* ABTS sent by initiator to CT exchange, construction
17472 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050017473 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050017474 */
James Smart546fc852011-03-11 16:06:29 -050017475 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050017476 }
James Smartf09c3ac2012-03-01 22:33:29 -050017477 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050017478 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050017479
James Smart546fc852011-03-11 16:06:29 -050017480 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050017481 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17482 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17483 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050017484
17485 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17486 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050017487 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17488 "2925 Failed to issue CT ABTS RSP x%x on "
17489 "xri x%x, Data x%x\n",
17490 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17491 phba->link_state);
17492 lpfc_nlp_put(ndlp);
17493 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050017494 lpfc_sli_release_iocbq(phba, ctiocb);
17495 }
James Smart6669f9b2009-10-02 15:16:45 -040017496}
17497
17498/**
17499 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17500 * @vport: Pointer to the vport on which this sequence was received
17501 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17502 *
17503 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17504 * receive sequence is only partially assembed by the driver, it shall abort
17505 * the partially assembled frames for the sequence. Otherwise, if the
17506 * unsolicited receive sequence has been completely assembled and passed to
17507 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17508 * unsolicited sequence has been aborted. After that, it will issue a basic
17509 * accept to accept the abort.
17510 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017511static void
James Smart6669f9b2009-10-02 15:16:45 -040017512lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17513 struct hbq_dmabuf *dmabuf)
17514{
17515 struct lpfc_hba *phba = vport->phba;
17516 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050017517 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050017518 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040017519
James Smart6669f9b2009-10-02 15:16:45 -040017520 /* Make a copy of fc_hdr before the dmabuf being released */
17521 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050017522 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040017523
James Smart5ffc2662009-11-18 15:39:44 -050017524 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050017525 /* ABTS by responder to exchange, no cleanup needed */
17526 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050017527 } else {
James Smart6dd9e312013-01-03 15:43:37 -050017528 /* ABTS by initiator to exchange, need to do cleanup */
17529 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17530 if (aborted == false)
17531 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050017532 }
James Smart6dd9e312013-01-03 15:43:37 -050017533 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17534
James Smart86c67372017-04-21 16:05:04 -070017535 if (phba->nvmet_support) {
17536 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17537 return;
17538 }
17539
James Smart6dd9e312013-01-03 15:43:37 -050017540 /* Respond with BA_ACC or BA_RJT accordingly */
17541 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040017542}
17543
17544/**
James Smart4f774512009-05-22 14:52:35 -040017545 * lpfc_seq_complete - Indicates if a sequence is complete
17546 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17547 *
17548 * This function checks the sequence, starting with the frame described by
17549 * @dmabuf, to see if all the frames associated with this sequence are present.
17550 * the frames associated with this sequence are linked to the @dmabuf using the
17551 * dbuf list. This function looks for two major things. 1) That the first frame
17552 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17553 * set. 3) That there are no holes in the sequence count. The function will
17554 * return 1 when the sequence is complete, otherwise it will return 0.
17555 **/
17556static int
17557lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17558{
17559 struct fc_frame_header *hdr;
17560 struct lpfc_dmabuf *d_buf;
17561 struct hbq_dmabuf *seq_dmabuf;
17562 uint32_t fctl;
17563 int seq_count = 0;
17564
17565 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17566 /* make sure first fame of sequence has a sequence count of zero */
17567 if (hdr->fh_seq_cnt != seq_count)
17568 return 0;
17569 fctl = (hdr->fh_f_ctl[0] << 16 |
17570 hdr->fh_f_ctl[1] << 8 |
17571 hdr->fh_f_ctl[2]);
17572 /* If last frame of sequence we can return success. */
17573 if (fctl & FC_FC_END_SEQ)
17574 return 1;
17575 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17576 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17577 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17578 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050017579 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040017580 return 0;
17581 fctl = (hdr->fh_f_ctl[0] << 16 |
17582 hdr->fh_f_ctl[1] << 8 |
17583 hdr->fh_f_ctl[2]);
17584 /* If last frame of sequence we can return success. */
17585 if (fctl & FC_FC_END_SEQ)
17586 return 1;
17587 }
17588 return 0;
17589}
17590
17591/**
17592 * lpfc_prep_seq - Prep sequence for ULP processing
17593 * @vport: Pointer to the vport on which this sequence was received
17594 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17595 *
17596 * This function takes a sequence, described by a list of frames, and creates
17597 * a list of iocbq structures to describe the sequence. This iocbq list will be
17598 * used to issue to the generic unsolicited sequence handler. This routine
17599 * returns a pointer to the first iocbq in the list. If the function is unable
17600 * to allocate an iocbq then it throw out the received frames that were not
17601 * able to be described and return a pointer to the first iocbq. If unable to
17602 * allocate any iocbqs (including the first) this function will return NULL.
17603 **/
17604static struct lpfc_iocbq *
17605lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17606{
James Smart7851fe22011-07-22 18:36:52 -040017607 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040017608 struct lpfc_dmabuf *d_buf, *n_buf;
17609 struct lpfc_iocbq *first_iocbq, *iocbq;
17610 struct fc_frame_header *fc_hdr;
17611 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040017612 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050017613 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040017614
17615 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17616 /* remove from receive buffer list */
17617 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017618 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017619 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040017620 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040017621 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017622 /* Get an iocbq struct to fill in. */
17623 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17624 if (first_iocbq) {
17625 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040017626 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017627 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080017628 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040017629
17630 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17631 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17632 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17633 first_iocbq->iocb.un.rcvels.parmRo =
17634 sli4_did_from_fc_hdr(fc_hdr);
17635 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17636 } else
17637 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040017638 first_iocbq->iocb.ulpContext = NO_XRI;
17639 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17640 be16_to_cpu(fc_hdr->fh_ox_id);
17641 /* iocbq is prepped for internal consumption. Physical vpi. */
17642 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17643 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040017644 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040017645 tot_len = bf_get(lpfc_rcqe_length,
17646 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17647
James Smart4f774512009-05-22 14:52:35 -040017648 first_iocbq->context2 = &seq_dmabuf->dbuf;
17649 first_iocbq->context3 = NULL;
17650 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040017651 if (tot_len > LPFC_DATA_BUF_SIZE)
17652 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040017653 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040017654 else
17655 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17656
James Smart4f774512009-05-22 14:52:35 -040017657 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040017658
James Smart7851fe22011-07-22 18:36:52 -040017659 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040017660 }
17661 iocbq = first_iocbq;
17662 /*
17663 * Each IOCBq can have two Buffers assigned, so go through the list
17664 * of buffers for this sequence and save two buffers in each IOCBq
17665 */
17666 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17667 if (!iocbq) {
17668 lpfc_in_buf_free(vport->phba, d_buf);
17669 continue;
17670 }
17671 if (!iocbq->context3) {
17672 iocbq->context3 = d_buf;
17673 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040017674 /* We need to get the size out of the right CQE */
17675 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17676 len = bf_get(lpfc_rcqe_length,
17677 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017678 pbde = (struct ulp_bde64 *)
17679 &iocbq->iocb.unsli3.sli3Words[4];
17680 if (len > LPFC_DATA_BUF_SIZE)
17681 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17682 else
17683 pbde->tus.f.bdeSize = len;
17684
James Smart7851fe22011-07-22 18:36:52 -040017685 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17686 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040017687 } else {
17688 iocbq = lpfc_sli_get_iocbq(vport->phba);
17689 if (!iocbq) {
17690 if (first_iocbq) {
17691 first_iocbq->iocb.ulpStatus =
17692 IOSTAT_FCP_RSP_ERROR;
17693 first_iocbq->iocb.un.ulpWord[4] =
17694 IOERR_NO_RESOURCES;
17695 }
17696 lpfc_in_buf_free(vport->phba, d_buf);
17697 continue;
17698 }
James Smart7851fe22011-07-22 18:36:52 -040017699 /* We need to get the size out of the right CQE */
17700 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17701 len = bf_get(lpfc_rcqe_length,
17702 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017703 iocbq->context2 = d_buf;
17704 iocbq->context3 = NULL;
17705 iocbq->iocb.ulpBdeCount = 1;
17706 if (len > LPFC_DATA_BUF_SIZE)
17707 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17708 LPFC_DATA_BUF_SIZE;
17709 else
17710 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17711
James Smart7851fe22011-07-22 18:36:52 -040017712 tot_len += len;
17713 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17714
James Smart4f774512009-05-22 14:52:35 -040017715 iocbq->iocb.un.rcvels.remoteID = sid;
17716 list_add_tail(&iocbq->list, &first_iocbq->list);
17717 }
17718 }
17719 return first_iocbq;
17720}
17721
James Smart6669f9b2009-10-02 15:16:45 -040017722static void
17723lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17724 struct hbq_dmabuf *seq_dmabuf)
17725{
17726 struct fc_frame_header *fc_hdr;
17727 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17728 struct lpfc_hba *phba = vport->phba;
17729
17730 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17731 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17732 if (!iocbq) {
17733 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17734 "2707 Ring %d handler: Failed to allocate "
17735 "iocb Rctl x%x Type x%x received\n",
17736 LPFC_ELS_RING,
17737 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17738 return;
17739 }
17740 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017741 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017742 iocbq, fc_hdr->fh_r_ctl,
17743 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017744 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017745 "2540 Ring %d handler: unexpected Rctl "
17746 "x%x Type x%x received\n",
17747 LPFC_ELS_RING,
17748 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17749
17750 /* Free iocb created in lpfc_prep_seq */
17751 list_for_each_entry_safe(curr_iocb, next_iocb,
17752 &iocbq->list, list) {
17753 list_del_init(&curr_iocb->list);
17754 lpfc_sli_release_iocbq(phba, curr_iocb);
17755 }
17756 lpfc_sli_release_iocbq(phba, iocbq);
17757}
17758
James Smartae9e28f2017-05-15 15:20:51 -070017759static void
17760lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17761 struct lpfc_iocbq *rspiocb)
17762{
17763 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17764
17765 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017766 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017767 kfree(pcmd);
17768 lpfc_sli_release_iocbq(phba, cmdiocb);
James Smarte817e5d2018-12-13 15:17:53 -080017769 lpfc_drain_txq(phba);
James Smartae9e28f2017-05-15 15:20:51 -070017770}
17771
17772static void
17773lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17774 struct hbq_dmabuf *dmabuf)
17775{
17776 struct fc_frame_header *fc_hdr;
17777 struct lpfc_hba *phba = vport->phba;
17778 struct lpfc_iocbq *iocbq = NULL;
17779 union lpfc_wqe *wqe;
17780 struct lpfc_dmabuf *pcmd = NULL;
17781 uint32_t frame_len;
17782 int rc;
James Smarte817e5d2018-12-13 15:17:53 -080017783 unsigned long iflags;
James Smartae9e28f2017-05-15 15:20:51 -070017784
17785 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17786 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17787
17788 /* Send the received frame back */
17789 iocbq = lpfc_sli_get_iocbq(phba);
James Smarte817e5d2018-12-13 15:17:53 -080017790 if (!iocbq) {
17791 /* Queue cq event and wakeup worker thread to process it */
17792 spin_lock_irqsave(&phba->hbalock, iflags);
17793 list_add_tail(&dmabuf->cq_event.list,
17794 &phba->sli4_hba.sp_queue_event);
17795 phba->hba_flag |= HBA_SP_QUEUE_EVT;
17796 spin_unlock_irqrestore(&phba->hbalock, iflags);
17797 lpfc_worker_wake_up(phba);
17798 return;
17799 }
James Smartae9e28f2017-05-15 15:20:51 -070017800
17801 /* Allocate buffer for command payload */
17802 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17803 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020017804 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070017805 &pcmd->phys);
17806 if (!pcmd || !pcmd->virt)
17807 goto exit;
17808
17809 INIT_LIST_HEAD(&pcmd->list);
17810
17811 /* copyin the payload */
17812 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17813
17814 /* fill in BDE's for command */
17815 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17816 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17817 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17818 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17819
17820 iocbq->context2 = pcmd;
17821 iocbq->vport = vport;
17822 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17823 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17824
17825 /*
17826 * Setup rest of the iocb as though it were a WQE
17827 * Build the SEND_FRAME WQE
17828 */
17829 wqe = (union lpfc_wqe *)&iocbq->iocb;
17830
17831 wqe->send_frame.frame_len = frame_len;
17832 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17833 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17834 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17835 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17836 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17837 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17838
17839 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17840 iocbq->iocb.ulpLe = 1;
17841 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17842 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17843 if (rc == IOCB_ERROR)
17844 goto exit;
17845
17846 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17847 return;
17848
17849exit:
17850 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17851 "2023 Unable to process MDS loopback frame\n");
17852 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017853 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017854 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070017855 if (iocbq)
17856 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070017857 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17858}
17859
James Smart4f774512009-05-22 14:52:35 -040017860/**
17861 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17862 * @phba: Pointer to HBA context object.
17863 *
17864 * This function is called with no lock held. This function processes all
17865 * the received buffers and gives it to upper layers when a received buffer
17866 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017867 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017868 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17869 * appropriate receive function when the final frame in a sequence is received.
17870 **/
James Smart4d9ab992009-10-02 15:16:39 -040017871void
17872lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17873 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017874{
James Smart4d9ab992009-10-02 15:16:39 -040017875 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017876 struct fc_frame_header *fc_hdr;
17877 struct lpfc_vport *vport;
17878 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017879 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017880
James Smart4f774512009-05-22 14:52:35 -040017881 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017882 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017883
James Smarte817e5d2018-12-13 15:17:53 -080017884 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
17885 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
17886 vport = phba->pport;
17887 /* Handle MDS Loopback frames */
17888 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17889 return;
17890 }
17891
James Smart4d9ab992009-10-02 15:16:39 -040017892 /* check to see if this a valid type of frame */
17893 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17894 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17895 return;
17896 }
James Smart2ea259e2017-02-12 13:52:27 -080017897
James Smart7851fe22011-07-22 18:36:52 -040017898 if ((bf_get(lpfc_cqe_code,
17899 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17900 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17901 &dmabuf->cq_event.cqe.rcqe_cmpl);
17902 else
17903 fcfi = bf_get(lpfc_rcqe_fcf_id,
17904 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017905
James Smarte62245d2019-08-14 16:57:08 -070017906 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17907 vport = phba->pport;
17908 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17909 "2023 MDS Loopback %d bytes\n",
17910 bf_get(lpfc_rcqe_length,
17911 &dmabuf->cq_event.cqe.rcqe_cmpl));
17912 /* Handle MDS Loopback frames */
17913 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17914 return;
17915 }
17916
James Smart895427b2017-02-12 13:52:30 -080017917 /* d_id this frame is directed to */
17918 did = sli4_did_from_fc_hdr(fc_hdr);
17919
17920 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017921 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017922 /* throw out the frame */
17923 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17924 return;
17925 }
James Smart939723a2012-05-09 21:19:03 -040017926
James Smart939723a2012-05-09 21:19:03 -040017927 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17928 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17929 (did != Fabric_DID)) {
17930 /*
17931 * Throw out the frame if we are not pt2pt.
17932 * The pt2pt protocol allows for discovery frames
17933 * to be received without a registered VPI.
17934 */
17935 if (!(vport->fc_flag & FC_PT2PT) ||
17936 (phba->link_state == LPFC_HBA_READY)) {
17937 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17938 return;
17939 }
17940 }
17941
James Smart6669f9b2009-10-02 15:16:45 -040017942 /* Handle the basic abort sequence (BA_ABTS) event */
17943 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17944 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17945 return;
17946 }
17947
James Smart4d9ab992009-10-02 15:16:39 -040017948 /* Link this frame */
17949 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17950 if (!seq_dmabuf) {
17951 /* unable to add frame to vport - throw it out */
17952 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17953 return;
17954 }
17955 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017956 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017957 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017958
James Smart6669f9b2009-10-02 15:16:45 -040017959 /* Send the complete sequence to the upper layer protocol */
17960 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017961}
James Smart6fb120a2009-05-22 14:52:59 -040017962
17963/**
17964 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17965 * @phba: pointer to lpfc hba data structure.
17966 *
17967 * This routine is invoked to post rpi header templates to the
17968 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017969 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17970 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017971 *
17972 * This routine does not require any locks. It's usage is expected
17973 * to be driver load or reset recovery when the driver is
17974 * sequential.
17975 *
17976 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017977 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017978 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017979 * When this error occurs, the driver is not guaranteed
17980 * to have any rpi regions posted to the device and
17981 * must either attempt to repost the regions or take a
17982 * fatal error.
17983 **/
17984int
17985lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17986{
17987 struct lpfc_rpi_hdr *rpi_page;
17988 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017989 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017990
James Smart6d368e52011-05-24 11:44:12 -040017991 /* SLI4 ports that support extents do not require RPI headers. */
17992 if (!phba->sli4_hba.rpi_hdrs_in_use)
17993 goto exit;
17994 if (phba->sli4_hba.extents_in_use)
17995 return -EIO;
17996
James Smart6fb120a2009-05-22 14:52:59 -040017997 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017998 /*
17999 * Assign the rpi headers a physical rpi only if the driver
18000 * has not initialized those resources. A port reset only
18001 * needs the headers posted.
18002 */
18003 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
18004 LPFC_RPI_RSRC_RDY)
18005 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
18006
James Smart6fb120a2009-05-22 14:52:59 -040018007 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
18008 if (rc != MBX_SUCCESS) {
18009 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18010 "2008 Error %d posting all rpi "
18011 "headers\n", rc);
18012 rc = -EIO;
18013 break;
18014 }
18015 }
18016
James Smart6d368e52011-05-24 11:44:12 -040018017 exit:
18018 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
18019 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040018020 return rc;
18021}
18022
18023/**
18024 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
18025 * @phba: pointer to lpfc hba data structure.
18026 * @rpi_page: pointer to the rpi memory region.
18027 *
18028 * This routine is invoked to post a single rpi header to the
18029 * HBA consistent with the SLI-4 interface spec. This memory region
18030 * maps up to 64 rpi context regions.
18031 *
18032 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018033 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040018034 * -ENOMEM - No available memory
18035 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040018036 **/
18037int
18038lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
18039{
18040 LPFC_MBOXQ_t *mboxq;
18041 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
18042 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018043 uint32_t shdr_status, shdr_add_status;
18044 union lpfc_sli4_cfg_shdr *shdr;
18045
James Smart6d368e52011-05-24 11:44:12 -040018046 /* SLI4 ports that support extents do not require RPI headers. */
18047 if (!phba->sli4_hba.rpi_hdrs_in_use)
18048 return rc;
18049 if (phba->sli4_hba.extents_in_use)
18050 return -EIO;
18051
James Smart6fb120a2009-05-22 14:52:59 -040018052 /* The port is notified of the header region via a mailbox command. */
18053 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18054 if (!mboxq) {
18055 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18056 "2001 Unable to allocate memory for issuing "
18057 "SLI_CONFIG_SPECIAL mailbox command\n");
18058 return -ENOMEM;
18059 }
18060
18061 /* Post all rpi memory regions to the port. */
18062 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040018063 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18064 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
18065 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050018066 sizeof(struct lpfc_sli4_cfg_mhdr),
18067 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040018068
18069
18070 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040018071 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
18072 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040018073 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
18074 hdr_tmpl, rpi_page->page_count);
18075
James Smart6fb120a2009-05-22 14:52:59 -040018076 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
18077 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040018078 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040018079 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
18080 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18081 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18082 if (rc != MBX_TIMEOUT)
18083 mempool_free(mboxq, phba->mbox_mem_pool);
18084 if (shdr_status || shdr_add_status || rc) {
18085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18086 "2514 POST_RPI_HDR mailbox failed with "
18087 "status x%x add_status x%x, mbx status x%x\n",
18088 shdr_status, shdr_add_status, rc);
18089 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070018090 } else {
18091 /*
18092 * The next_rpi stores the next logical module-64 rpi value used
18093 * to post physical rpis in subsequent rpi postings.
18094 */
18095 spin_lock_irq(&phba->hbalock);
18096 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
18097 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040018098 }
18099 return rc;
18100}
18101
18102/**
18103 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
18104 * @phba: pointer to lpfc hba data structure.
18105 *
18106 * This routine is invoked to post rpi header templates to the
18107 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040018108 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18109 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040018110 *
18111 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018112 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040018113 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
18114 **/
18115int
18116lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
18117{
James Smart6d368e52011-05-24 11:44:12 -040018118 unsigned long rpi;
18119 uint16_t max_rpi, rpi_limit;
18120 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018121 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040018122 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040018123
James Smart6fb120a2009-05-22 14:52:59 -040018124 /*
James Smart6d368e52011-05-24 11:44:12 -040018125 * Fetch the next logical rpi. Because this index is logical,
18126 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040018127 */
James Smart4902b382013-10-10 12:20:35 -040018128 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040018129 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
18130 rpi_limit = phba->sli4_hba.next_rpi;
18131
James Smart6d368e52011-05-24 11:44:12 -040018132 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
18133 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040018134 rpi = LPFC_RPI_ALLOC_ERROR;
18135 else {
18136 set_bit(rpi, phba->sli4_hba.rpi_bmask);
18137 phba->sli4_hba.max_cfg_param.rpi_used++;
18138 phba->sli4_hba.rpi_count++;
18139 }
James Smart0f154222019-09-21 20:58:52 -070018140 lpfc_printf_log(phba, KERN_INFO,
18141 LOG_NODE | LOG_DISCOVERY,
18142 "0001 Allocated rpi:x%x max:x%x lim:x%x\n",
James Smartbe6bb942015-04-07 15:07:22 -040018143 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040018144
18145 /*
18146 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040018147 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040018148 */
18149 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
18150 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040018151 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018152 return rpi;
18153 }
18154
18155 /*
James Smart6d368e52011-05-24 11:44:12 -040018156 * RPI header postings are not required for SLI4 ports capable of
18157 * extents.
18158 */
18159 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040018160 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040018161 return rpi;
18162 }
18163
18164 /*
James Smart6fb120a2009-05-22 14:52:59 -040018165 * If the driver is running low on rpi resources, allocate another
18166 * page now. Note that the next_rpi value is used because
18167 * it represents how many are actually in use whereas max_rpi notes
18168 * how many are supported max by the device.
18169 */
James Smart6d368e52011-05-24 11:44:12 -040018170 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040018171 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018172 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
18173 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
18174 if (!rpi_hdr) {
18175 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18176 "2002 Error Could not grow rpi "
18177 "count\n");
18178 } else {
James Smart6d368e52011-05-24 11:44:12 -040018179 lrpi = rpi_hdr->start_rpi;
18180 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040018181 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
18182 }
18183 }
18184
18185 return rpi;
18186}
18187
18188/**
18189 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18190 * @phba: pointer to lpfc hba data structure.
18191 *
18192 * This routine is invoked to release an rpi to the pool of
18193 * available rpis maintained by the driver.
18194 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018195static void
James Smartd7c47992010-06-08 18:31:54 -040018196__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18197{
18198 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
18199 phba->sli4_hba.rpi_count--;
18200 phba->sli4_hba.max_cfg_param.rpi_used--;
James Smartb95b2112019-08-14 16:56:47 -070018201 } else {
James Smart0f154222019-09-21 20:58:52 -070018202 lpfc_printf_log(phba, KERN_INFO,
18203 LOG_NODE | LOG_DISCOVERY,
James Smartb95b2112019-08-14 16:56:47 -070018204 "2016 rpi %x not inuse\n",
18205 rpi);
James Smartd7c47992010-06-08 18:31:54 -040018206 }
18207}
18208
18209/**
18210 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18211 * @phba: pointer to lpfc hba data structure.
18212 *
18213 * This routine is invoked to release an rpi to the pool of
18214 * available rpis maintained by the driver.
18215 **/
18216void
James Smart6fb120a2009-05-22 14:52:59 -040018217lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18218{
18219 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040018220 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040018221 spin_unlock_irq(&phba->hbalock);
18222}
18223
18224/**
18225 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
18226 * @phba: pointer to lpfc hba data structure.
18227 *
18228 * This routine is invoked to remove the memory region that
18229 * provided rpi via a bitmask.
18230 **/
18231void
18232lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
18233{
18234 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040018235 kfree(phba->sli4_hba.rpi_ids);
18236 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040018237}
18238
18239/**
18240 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
18241 * @phba: pointer to lpfc hba data structure.
18242 *
18243 * This routine is invoked to remove the memory region that
18244 * provided rpi via a bitmask.
18245 **/
18246int
James Smart6b5151f2012-01-18 16:24:06 -050018247lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
18248 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040018249{
18250 LPFC_MBOXQ_t *mboxq;
18251 struct lpfc_hba *phba = ndlp->phba;
18252 int rc;
18253
18254 /* The port is notified of the header region via a mailbox command. */
18255 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18256 if (!mboxq)
18257 return -ENOMEM;
18258
18259 /* Post all rpi memory regions to the port. */
18260 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050018261 if (cmpl) {
18262 mboxq->mbox_cmpl = cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080018263 mboxq->ctx_buf = arg;
18264 mboxq->ctx_ndlp = ndlp;
James Smart72859902012-01-18 16:25:38 -050018265 } else
18266 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050018267 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040018268 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18269 if (rc == MBX_NOT_FINISHED) {
18270 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18271 "2010 Resume RPI Mailbox failed "
18272 "status %d, mbxStatus x%x\n", rc,
18273 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
18274 mempool_free(mboxq, phba->mbox_mem_pool);
18275 return -EIO;
18276 }
18277 return 0;
18278}
18279
18280/**
18281 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050018282 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040018283 *
James Smart76a95d72010-11-20 23:11:48 -050018284 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040018285 *
18286 * Returns:
18287 * 0 success
18288 * -Evalue otherwise
18289 **/
18290int
James Smart76a95d72010-11-20 23:11:48 -050018291lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040018292{
18293 LPFC_MBOXQ_t *mboxq;
18294 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040018295 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040018296 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050018297 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040018298 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18299 if (!mboxq)
18300 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050018301 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040018302 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040018303 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040018304 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050018305 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040018306 "2022 INIT VPI Mailbox failed "
18307 "status %d, mbxStatus x%x\n", rc,
18308 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040018309 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040018310 }
James Smart6a9c52c2009-10-02 15:16:51 -040018311 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050018312 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040018313
18314 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040018315}
18316
18317/**
18318 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
18319 * @phba: pointer to lpfc hba data structure.
18320 * @mboxq: Pointer to mailbox object.
18321 *
18322 * This routine is invoked to manually add a single FCF record. The caller
18323 * must pass a completely initialized FCF_Record. This routine takes
18324 * care of the nonembedded mailbox operations.
18325 **/
18326static void
18327lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
18328{
18329 void *virt_addr;
18330 union lpfc_sli4_cfg_shdr *shdr;
18331 uint32_t shdr_status, shdr_add_status;
18332
18333 virt_addr = mboxq->sge_array->addr[0];
18334 /* The IOCTL status is embedded in the mailbox subheader. */
18335 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
18336 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18337 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18338
18339 if ((shdr_status || shdr_add_status) &&
18340 (shdr_status != STATUS_FCF_IN_USE))
18341 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18342 "2558 ADD_FCF_RECORD mailbox failed with "
18343 "status x%x add_status x%x\n",
18344 shdr_status, shdr_add_status);
18345
18346 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18347}
18348
18349/**
18350 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
18351 * @phba: pointer to lpfc hba data structure.
18352 * @fcf_record: pointer to the initialized fcf record to add.
18353 *
18354 * This routine is invoked to manually add a single FCF record. The caller
18355 * must pass a completely initialized FCF_Record. This routine takes
18356 * care of the nonembedded mailbox operations.
18357 **/
18358int
18359lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
18360{
18361 int rc = 0;
18362 LPFC_MBOXQ_t *mboxq;
18363 uint8_t *bytep;
18364 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040018365 struct lpfc_mbx_sge sge;
18366 uint32_t alloc_len, req_len;
18367 uint32_t fcfindex;
18368
18369 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18370 if (!mboxq) {
18371 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18372 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18373 return -ENOMEM;
18374 }
18375
18376 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18377 sizeof(uint32_t);
18378
18379 /* Allocate DMA memory and set up the non-embedded mailbox command */
18380 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18381 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18382 req_len, LPFC_SLI4_MBX_NEMBED);
18383 if (alloc_len < req_len) {
18384 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18385 "2523 Allocated DMA memory size (x%x) is "
18386 "less than the requested DMA memory "
18387 "size (x%x)\n", alloc_len, req_len);
18388 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18389 return -ENOMEM;
18390 }
18391
18392 /*
18393 * Get the first SGE entry from the non-embedded DMA memory. This
18394 * routine only uses a single SGE.
18395 */
18396 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040018397 virt_addr = mboxq->sge_array->addr[0];
18398 /*
18399 * Configure the FCF record for FCFI 0. This is the driver's
18400 * hardcoded default and gets used in nonFIP mode.
18401 */
18402 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18403 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18404 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18405
18406 /*
18407 * Copy the fcf_index and the FCF Record Data. The data starts after
18408 * the FCoE header plus word10. The data copy needs to be endian
18409 * correct.
18410 */
18411 bytep += sizeof(uint32_t);
18412 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18413 mboxq->vport = phba->pport;
18414 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18415 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18416 if (rc == MBX_NOT_FINISHED) {
18417 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18418 "2515 ADD_FCF_RECORD mailbox failed with "
18419 "status 0x%x\n", rc);
18420 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18421 rc = -EIO;
18422 } else
18423 rc = 0;
18424
18425 return rc;
18426}
18427
18428/**
18429 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18430 * @phba: pointer to lpfc hba data structure.
18431 * @fcf_record: pointer to the fcf record to write the default data.
18432 * @fcf_index: FCF table entry index.
18433 *
18434 * This routine is invoked to build the driver's default FCF record. The
18435 * values used are hardcoded. This routine handles memory initialization.
18436 *
18437 **/
18438void
18439lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18440 struct fcf_record *fcf_record,
18441 uint16_t fcf_index)
18442{
18443 memset(fcf_record, 0, sizeof(struct fcf_record));
18444 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18445 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18446 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18447 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18448 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18449 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18450 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18451 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18452 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18453 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18454 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18455 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18456 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040018457 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040018458 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18459 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18460 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18461 /* Set the VLAN bit map */
18462 if (phba->valid_vlan) {
18463 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18464 = 1 << (phba->vlan_id % 8);
18465 }
18466}
18467
18468/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018469 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040018470 * @phba: pointer to lpfc hba data structure.
18471 * @fcf_index: FCF table entry offset.
18472 *
James Smart0c9ab6f2010-02-26 14:15:57 -050018473 * This routine is invoked to scan the entire FCF table by reading FCF
18474 * record and processing it one at a time starting from the @fcf_index
18475 * for initial FCF discovery or fast FCF failover rediscovery.
18476 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018477 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018478 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040018479 **/
18480int
James Smart0c9ab6f2010-02-26 14:15:57 -050018481lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040018482{
18483 int rc = 0, error;
18484 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040018485
James Smart32b97932009-07-19 10:01:21 -040018486 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050018487 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040018488 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18489 if (!mboxq) {
18490 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18491 "2000 Failed to allocate mbox for "
18492 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040018493 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050018494 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018495 }
James Smartecfd03c2010-02-12 14:41:27 -050018496 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050018497 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050018498 if (rc) {
18499 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050018500 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018501 }
James Smartecfd03c2010-02-12 14:41:27 -050018502 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040018503 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050018504 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040018505
18506 spin_lock_irq(&phba->hbalock);
18507 phba->hba_flag |= FCF_TS_INPROG;
18508 spin_unlock_irq(&phba->hbalock);
18509
James Smart6fb120a2009-05-22 14:52:59 -040018510 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050018511 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040018512 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050018513 else {
James Smart38b92ef2010-08-04 16:11:39 -040018514 /* Reset eligible FCF count for new scan */
18515 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040018516 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018517 error = 0;
James Smart32b97932009-07-19 10:01:21 -040018518 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018519fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040018520 if (error) {
18521 if (mboxq)
18522 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040018523 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040018524 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040018525 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040018526 spin_unlock_irq(&phba->hbalock);
18527 }
James Smart6fb120a2009-05-22 14:52:59 -040018528 return error;
18529}
James Smarta0c87cb2009-07-19 10:01:10 -040018530
18531/**
James Smarta93ff372010-10-22 11:06:08 -040018532 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050018533 * @phba: pointer to lpfc hba data structure.
18534 * @fcf_index: FCF table entry offset.
18535 *
18536 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040018537 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050018538 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018539 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018540 * otherwise.
18541 **/
18542int
18543lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18544{
18545 int rc = 0, error;
18546 LPFC_MBOXQ_t *mboxq;
18547
18548 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18549 if (!mboxq) {
18550 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18551 "2763 Failed to allocate mbox for "
18552 "READ_FCF cmd\n");
18553 error = -ENOMEM;
18554 goto fail_fcf_read;
18555 }
18556 /* Construct the read FCF record mailbox command */
18557 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18558 if (rc) {
18559 error = -EINVAL;
18560 goto fail_fcf_read;
18561 }
18562 /* Issue the mailbox command asynchronously */
18563 mboxq->vport = phba->pport;
18564 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18565 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18566 if (rc == MBX_NOT_FINISHED)
18567 error = -EIO;
18568 else
18569 error = 0;
18570
18571fail_fcf_read:
18572 if (error && mboxq)
18573 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18574 return error;
18575}
18576
18577/**
18578 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18579 * @phba: pointer to lpfc hba data structure.
18580 * @fcf_index: FCF table entry offset.
18581 *
18582 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040018583 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050018584 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018585 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018586 * otherwise.
18587 **/
18588int
18589lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18590{
18591 int rc = 0, error;
18592 LPFC_MBOXQ_t *mboxq;
18593
18594 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18595 if (!mboxq) {
18596 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18597 "2758 Failed to allocate mbox for "
18598 "READ_FCF cmd\n");
18599 error = -ENOMEM;
18600 goto fail_fcf_read;
18601 }
18602 /* Construct the read FCF record mailbox command */
18603 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18604 if (rc) {
18605 error = -EINVAL;
18606 goto fail_fcf_read;
18607 }
18608 /* Issue the mailbox command asynchronously */
18609 mboxq->vport = phba->pport;
18610 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18611 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18612 if (rc == MBX_NOT_FINISHED)
18613 error = -EIO;
18614 else
18615 error = 0;
18616
18617fail_fcf_read:
18618 if (error && mboxq)
18619 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18620 return error;
18621}
18622
18623/**
James Smartf5cb5302015-12-16 18:11:52 -050018624 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040018625 * phba pointer to the lpfc_hba struct for this port.
18626 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18627 * routine when the rr_bmask is empty. The FCF indecies are put into the
18628 * rr_bmask based on their priority level. Starting from the highest priority
18629 * to the lowest. The most likely FCF candidate will be in the highest
18630 * priority group. When this routine is called it searches the fcf_pri list for
18631 * next lowest priority group and repopulates the rr_bmask with only those
18632 * fcf_indexes.
18633 * returns:
18634 * 1=success 0=failure
18635 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018636static int
James Smart7d791df2011-07-22 18:37:52 -040018637lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18638{
18639 uint16_t next_fcf_pri;
18640 uint16_t last_index;
18641 struct lpfc_fcf_pri *fcf_pri;
18642 int rc;
18643 int ret = 0;
18644
18645 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18646 LPFC_SLI4_FCF_TBL_INDX_MAX);
18647 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18648 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050018649
18650 /* Verify the priority list has 2 or more entries */
18651 spin_lock_irq(&phba->hbalock);
18652 if (list_empty(&phba->fcf.fcf_pri_list) ||
18653 list_is_singular(&phba->fcf.fcf_pri_list)) {
18654 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040018655 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18656 "3061 Last IDX %d\n", last_index);
18657 return 0; /* Empty rr list */
18658 }
James Smart25626692013-03-01 16:36:54 -050018659 spin_unlock_irq(&phba->hbalock);
18660
James Smart7d791df2011-07-22 18:37:52 -040018661 next_fcf_pri = 0;
18662 /*
18663 * Clear the rr_bmask and set all of the bits that are at this
18664 * priority.
18665 */
18666 memset(phba->fcf.fcf_rr_bmask, 0,
18667 sizeof(*phba->fcf.fcf_rr_bmask));
18668 spin_lock_irq(&phba->hbalock);
18669 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18670 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18671 continue;
18672 /*
18673 * the 1st priority that has not FLOGI failed
18674 * will be the highest.
18675 */
18676 if (!next_fcf_pri)
18677 next_fcf_pri = fcf_pri->fcf_rec.priority;
18678 spin_unlock_irq(&phba->hbalock);
18679 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18680 rc = lpfc_sli4_fcf_rr_index_set(phba,
18681 fcf_pri->fcf_rec.fcf_index);
18682 if (rc)
18683 return 0;
18684 }
18685 spin_lock_irq(&phba->hbalock);
18686 }
18687 /*
18688 * if next_fcf_pri was not set above and the list is not empty then
18689 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070018690 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040018691 */
18692 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18693 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18694 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18695 /*
18696 * the 1st priority that has not FLOGI failed
18697 * will be the highest.
18698 */
18699 if (!next_fcf_pri)
18700 next_fcf_pri = fcf_pri->fcf_rec.priority;
18701 spin_unlock_irq(&phba->hbalock);
18702 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18703 rc = lpfc_sli4_fcf_rr_index_set(phba,
18704 fcf_pri->fcf_rec.fcf_index);
18705 if (rc)
18706 return 0;
18707 }
18708 spin_lock_irq(&phba->hbalock);
18709 }
18710 } else
18711 ret = 1;
18712 spin_unlock_irq(&phba->hbalock);
18713
18714 return ret;
18715}
18716/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018717 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18718 * @phba: pointer to lpfc hba data structure.
18719 *
18720 * This routine is to get the next eligible FCF record index in a round
18721 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040018722 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050018723 * shall be returned, otherwise, the next eligible FCF record's index
18724 * shall be returned.
18725 **/
18726uint16_t
18727lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18728{
18729 uint16_t next_fcf_index;
18730
James Smart421c6622013-01-03 15:44:16 -050018731initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040018732 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050018733 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18734
James Smart7d791df2011-07-22 18:37:52 -040018735next_priority:
James Smart421c6622013-01-03 15:44:16 -050018736 /* Determine the next fcf index to check */
18737 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050018738 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18739 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040018740 next_fcf_index);
18741
James Smart0c9ab6f2010-02-26 14:15:57 -050018742 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018743 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18744 /*
18745 * If we have wrapped then we need to clear the bits that
18746 * have been tested so that we can detect when we should
18747 * change the priority level.
18748 */
James Smart0c9ab6f2010-02-26 14:15:57 -050018749 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18750 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040018751 }
18752
James Smart0c9ab6f2010-02-26 14:15:57 -050018753
James Smart3804dc82010-07-14 15:31:37 -040018754 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018755 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18756 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18757 /*
18758 * If next fcf index is not found check if there are lower
18759 * Priority level fcf's in the fcf_priority list.
18760 * Set up the rr_bmask with all of the avaiable fcf bits
18761 * at that level and continue the selection process.
18762 */
18763 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018764 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018765 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18766 "2844 No roundrobin failover FCF available\n");
James Smart036cad12018-10-23 13:41:06 -070018767
18768 return LPFC_FCOE_FCF_NEXT_NONE;
James Smart3804dc82010-07-14 15:31:37 -040018769 }
18770
James Smart7d791df2011-07-22 18:37:52 -040018771 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18772 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018773 LPFC_FCF_FLOGI_FAILED) {
18774 if (list_is_singular(&phba->fcf.fcf_pri_list))
18775 return LPFC_FCOE_FCF_NEXT_NONE;
18776
James Smart7d791df2011-07-22 18:37:52 -040018777 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018778 }
James Smart7d791df2011-07-22 18:37:52 -040018779
James Smart3804dc82010-07-14 15:31:37 -040018780 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018781 "2845 Get next roundrobin failover FCF (x%x)\n",
18782 next_fcf_index);
18783
James Smart0c9ab6f2010-02-26 14:15:57 -050018784 return next_fcf_index;
18785}
18786
18787/**
18788 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18789 * @phba: pointer to lpfc hba data structure.
18790 *
18791 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018792 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018793 * does not go beyond the range of the driver allocated bmask dimension
18794 * before setting the bit.
18795 *
18796 * Returns 0 if the index bit successfully set, otherwise, it returns
18797 * -EINVAL.
18798 **/
18799int
18800lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18801{
18802 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18803 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018804 "2610 FCF (x%x) reached driver's book "
18805 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018806 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18807 return -EINVAL;
18808 }
18809 /* Set the eligible FCF record index bmask */
18810 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18811
James Smart3804dc82010-07-14 15:31:37 -040018812 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018813 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018814 "bmask\n", fcf_index);
18815
James Smart0c9ab6f2010-02-26 14:15:57 -050018816 return 0;
18817}
18818
18819/**
James Smart3804dc82010-07-14 15:31:37 -040018820 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018821 * @phba: pointer to lpfc hba data structure.
18822 *
18823 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018824 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018825 * does not go beyond the range of the driver allocated bmask dimension
18826 * before clearing the bit.
18827 **/
18828void
18829lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18830{
James Smart9a803a72013-09-06 12:17:56 -040018831 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018832 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18833 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018834 "2762 FCF (x%x) reached driver's book "
18835 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018836 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18837 return;
18838 }
18839 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018840 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018841 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18842 list) {
James Smart7d791df2011-07-22 18:37:52 -040018843 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18844 list_del_init(&fcf_pri->list);
18845 break;
18846 }
18847 }
18848 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018849 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018850
18851 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018852 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018853 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018854}
18855
18856/**
James Smartecfd03c2010-02-12 14:41:27 -050018857 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18858 * @phba: pointer to lpfc hba data structure.
18859 *
18860 * This routine is the completion routine for the rediscover FCF table mailbox
18861 * command. If the mailbox command returned failure, it will try to stop the
18862 * FCF rediscover wait timer.
18863 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018864static void
James Smartecfd03c2010-02-12 14:41:27 -050018865lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18866{
18867 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18868 uint32_t shdr_status, shdr_add_status;
18869
18870 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18871
18872 shdr_status = bf_get(lpfc_mbox_hdr_status,
18873 &redisc_fcf->header.cfg_shdr.response);
18874 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18875 &redisc_fcf->header.cfg_shdr.response);
18876 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018877 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018878 "2746 Requesting for FCF rediscovery failed "
18879 "status x%x add_status x%x\n",
18880 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018881 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018882 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018883 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018884 spin_unlock_irq(&phba->hbalock);
18885 /*
18886 * CVL event triggered FCF rediscover request failed,
18887 * last resort to re-try current registered FCF entry.
18888 */
18889 lpfc_retry_pport_discovery(phba);
18890 } else {
18891 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018892 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018893 spin_unlock_irq(&phba->hbalock);
18894 /*
18895 * DEAD FCF event triggered FCF rediscover request
18896 * failed, last resort to fail over as a link down
18897 * to FCF registration.
18898 */
18899 lpfc_sli4_fcf_dead_failthrough(phba);
18900 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018901 } else {
18902 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018903 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018904 /*
18905 * Start FCF rediscovery wait timer for pending FCF
18906 * before rescan FCF record table.
18907 */
18908 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018909 }
James Smartecfd03c2010-02-12 14:41:27 -050018910
18911 mempool_free(mbox, phba->mbox_mem_pool);
18912}
18913
18914/**
James Smart3804dc82010-07-14 15:31:37 -040018915 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018916 * @phba: pointer to lpfc hba data structure.
18917 *
18918 * This routine is invoked to request for rediscovery of the entire FCF table
18919 * by the port.
18920 **/
18921int
18922lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18923{
18924 LPFC_MBOXQ_t *mbox;
18925 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18926 int rc, length;
18927
James Smart0c9ab6f2010-02-26 14:15:57 -050018928 /* Cancel retry delay timers to all vports before FCF rediscover */
18929 lpfc_cancel_all_vport_retry_delay_timer(phba);
18930
James Smartecfd03c2010-02-12 14:41:27 -050018931 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18932 if (!mbox) {
18933 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18934 "2745 Failed to allocate mbox for "
18935 "requesting FCF rediscover.\n");
18936 return -ENOMEM;
18937 }
18938
18939 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18940 sizeof(struct lpfc_sli4_cfg_mhdr));
18941 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18942 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18943 length, LPFC_SLI4_MBX_EMBED);
18944
18945 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18946 /* Set count to 0 for invalidating the entire FCF database */
18947 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18948
18949 /* Issue the mailbox command asynchronously */
18950 mbox->vport = phba->pport;
18951 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18952 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18953
18954 if (rc == MBX_NOT_FINISHED) {
18955 mempool_free(mbox, phba->mbox_mem_pool);
18956 return -EIO;
18957 }
18958 return 0;
18959}
18960
18961/**
James Smartfc2b9892010-02-26 14:15:29 -050018962 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18963 * @phba: pointer to lpfc hba data structure.
18964 *
18965 * This function is the failover routine as a last resort to the FCF DEAD
18966 * event when driver failed to perform fast FCF failover.
18967 **/
18968void
18969lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18970{
18971 uint32_t link_state;
18972
18973 /*
18974 * Last resort as FCF DEAD event failover will treat this as
18975 * a link down, but save the link state because we don't want
18976 * it to be changed to Link Down unless it is already down.
18977 */
18978 link_state = phba->link_state;
18979 lpfc_linkdown(phba);
18980 phba->link_state = link_state;
18981
18982 /* Unregister FCF if no devices connected to it */
18983 lpfc_unregister_unused_fcf(phba);
18984}
18985
18986/**
James Smart026abb82011-12-13 13:20:45 -050018987 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018988 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018989 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018990 *
James Smart026abb82011-12-13 13:20:45 -050018991 * This function gets SLI3 port configure region 23 data through memory dump
18992 * mailbox command. When it successfully retrieves data, the size of the data
18993 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018994 **/
James Smart026abb82011-12-13 13:20:45 -050018995static uint32_t
18996lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018997{
18998 LPFC_MBOXQ_t *pmb = NULL;
18999 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050019000 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040019001 int rc;
19002
James Smart026abb82011-12-13 13:20:45 -050019003 if (!rgn23_data)
19004 return 0;
19005
James Smarta0c87cb2009-07-19 10:01:10 -040019006 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19007 if (!pmb) {
19008 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050019009 "2600 failed to allocate mailbox memory\n");
19010 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040019011 }
19012 mb = &pmb->u.mb;
19013
James Smarta0c87cb2009-07-19 10:01:10 -040019014 do {
19015 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
19016 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
19017
19018 if (rc != MBX_SUCCESS) {
19019 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050019020 "2601 failed to read config "
19021 "region 23, rc 0x%x Status 0x%x\n",
19022 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040019023 mb->un.varDmp.word_cnt = 0;
19024 }
19025 /*
19026 * dump mem may return a zero when finished or we got a
19027 * mailbox error, either way we are done.
19028 */
19029 if (mb->un.varDmp.word_cnt == 0)
19030 break;
19031 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
19032 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
19033
19034 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050019035 rgn23_data + offset,
19036 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040019037 offset += mb->un.varDmp.word_cnt;
19038 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
19039
James Smart026abb82011-12-13 13:20:45 -050019040 mempool_free(pmb, phba->mbox_mem_pool);
19041 return offset;
19042}
19043
19044/**
19045 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
19046 * @phba: pointer to lpfc hba data structure.
19047 * @rgn23_data: pointer to configure region 23 data.
19048 *
19049 * This function gets SLI4 port configure region 23 data through memory dump
19050 * mailbox command. When it successfully retrieves data, the size of the data
19051 * will be returned, otherwise, 0 will be returned.
19052 **/
19053static uint32_t
19054lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
19055{
19056 LPFC_MBOXQ_t *mboxq = NULL;
19057 struct lpfc_dmabuf *mp = NULL;
19058 struct lpfc_mqe *mqe;
19059 uint32_t data_length = 0;
19060 int rc;
19061
19062 if (!rgn23_data)
19063 return 0;
19064
19065 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19066 if (!mboxq) {
19067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19068 "3105 failed to allocate mailbox memory\n");
19069 return 0;
19070 }
19071
19072 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
19073 goto out;
19074 mqe = &mboxq->u.mqe;
James Smart3e1f0712018-11-29 16:09:29 -080019075 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smart026abb82011-12-13 13:20:45 -050019076 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19077 if (rc)
19078 goto out;
19079 data_length = mqe->un.mb_words[5];
19080 if (data_length == 0)
19081 goto out;
19082 if (data_length > DMP_RGN23_SIZE) {
19083 data_length = 0;
19084 goto out;
19085 }
19086 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
19087out:
19088 mempool_free(mboxq, phba->mbox_mem_pool);
19089 if (mp) {
19090 lpfc_mbuf_free(phba, mp->virt, mp->phys);
19091 kfree(mp);
19092 }
19093 return data_length;
19094}
19095
19096/**
19097 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
19098 * @phba: pointer to lpfc hba data structure.
19099 *
19100 * This function read region 23 and parse TLV for port status to
19101 * decide if the user disaled the port. If the TLV indicates the
19102 * port is disabled, the hba_flag is set accordingly.
19103 **/
19104void
19105lpfc_sli_read_link_ste(struct lpfc_hba *phba)
19106{
19107 uint8_t *rgn23_data = NULL;
19108 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
19109 uint32_t offset = 0;
19110
19111 /* Get adapter Region 23 data */
19112 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
19113 if (!rgn23_data)
19114 goto out;
19115
19116 if (phba->sli_rev < LPFC_SLI_REV4)
19117 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
19118 else {
19119 if_type = bf_get(lpfc_sli_intf_if_type,
19120 &phba->sli4_hba.sli_intf);
19121 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
19122 goto out;
19123 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
19124 }
James Smarta0c87cb2009-07-19 10:01:10 -040019125
19126 if (!data_size)
19127 goto out;
19128
19129 /* Check the region signature first */
19130 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
19131 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19132 "2619 Config region 23 has bad signature\n");
19133 goto out;
19134 }
19135 offset += 4;
19136
19137 /* Check the data structure version */
19138 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
19139 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19140 "2620 Config region 23 has bad version\n");
19141 goto out;
19142 }
19143 offset += 4;
19144
19145 /* Parse TLV entries in the region */
19146 while (offset < data_size) {
19147 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
19148 break;
19149 /*
19150 * If the TLV is not driver specific TLV or driver id is
19151 * not linux driver id, skip the record.
19152 */
19153 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
19154 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
19155 (rgn23_data[offset + 3] != 0)) {
19156 offset += rgn23_data[offset + 1] * 4 + 4;
19157 continue;
19158 }
19159
19160 /* Driver found a driver specific TLV in the config region */
19161 sub_tlv_len = rgn23_data[offset + 1] * 4;
19162 offset += 4;
19163 tlv_offset = 0;
19164
19165 /*
19166 * Search for configured port state sub-TLV.
19167 */
19168 while ((offset < data_size) &&
19169 (tlv_offset < sub_tlv_len)) {
19170 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
19171 offset += 4;
19172 tlv_offset += 4;
19173 break;
19174 }
19175 if (rgn23_data[offset] != PORT_STE_TYPE) {
19176 offset += rgn23_data[offset + 1] * 4 + 4;
19177 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
19178 continue;
19179 }
19180
19181 /* This HBA contains PORT_STE configured */
19182 if (!rgn23_data[offset + 2])
19183 phba->hba_flag |= LINK_DISABLED;
19184
19185 goto out;
19186 }
19187 }
James Smart026abb82011-12-13 13:20:45 -050019188
James Smarta0c87cb2009-07-19 10:01:10 -040019189out:
James Smarta0c87cb2009-07-19 10:01:10 -040019190 kfree(rgn23_data);
19191 return;
19192}
James Smart695a8142010-01-26 23:08:03 -050019193
19194/**
James Smart52d52442011-05-24 11:42:45 -040019195 * lpfc_wr_object - write an object to the firmware
19196 * @phba: HBA structure that indicates port to create a queue on.
19197 * @dmabuf_list: list of dmabufs to write to the port.
19198 * @size: the total byte value of the objects to write to the port.
19199 * @offset: the current offset to be used to start the transfer.
19200 *
19201 * This routine will create a wr_object mailbox command to send to the port.
19202 * the mailbox command will be constructed using the dma buffers described in
19203 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
19204 * BDEs that the imbedded mailbox can support. The @offset variable will be
19205 * used to indicate the starting offset of the transfer and will also return
19206 * the offset after the write object mailbox has completed. @size is used to
19207 * determine the end of the object and whether the eof bit should be set.
19208 *
19209 * Return 0 is successful and offset will contain the the new offset to use
19210 * for the next write.
19211 * Return negative value for error cases.
19212 **/
19213int
19214lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
19215 uint32_t size, uint32_t *offset)
19216{
19217 struct lpfc_mbx_wr_object *wr_object;
19218 LPFC_MBOXQ_t *mbox;
19219 int rc = 0, i = 0;
James Smart50212672018-12-13 15:17:57 -080019220 uint32_t shdr_status, shdr_add_status, shdr_change_status;
James Smart52d52442011-05-24 11:42:45 -040019221 uint32_t mbox_tmo;
James Smart52d52442011-05-24 11:42:45 -040019222 struct lpfc_dmabuf *dmabuf;
19223 uint32_t written = 0;
James Smart50212672018-12-13 15:17:57 -080019224 bool check_change_status = false;
James Smart52d52442011-05-24 11:42:45 -040019225
19226 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19227 if (!mbox)
19228 return -ENOMEM;
19229
19230 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
19231 LPFC_MBOX_OPCODE_WRITE_OBJECT,
19232 sizeof(struct lpfc_mbx_wr_object) -
19233 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
19234
19235 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
19236 wr_object->u.request.write_offset = *offset;
19237 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
19238 wr_object->u.request.object_name[0] =
19239 cpu_to_le32(wr_object->u.request.object_name[0]);
19240 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
19241 list_for_each_entry(dmabuf, dmabuf_list, list) {
19242 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
19243 break;
19244 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
19245 wr_object->u.request.bde[i].addrHigh =
19246 putPaddrHigh(dmabuf->phys);
19247 if (written + SLI4_PAGE_SIZE >= size) {
19248 wr_object->u.request.bde[i].tus.f.bdeSize =
19249 (size - written);
19250 written += (size - written);
19251 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
James Smart50212672018-12-13 15:17:57 -080019252 bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
19253 check_change_status = true;
James Smart52d52442011-05-24 11:42:45 -040019254 } else {
19255 wr_object->u.request.bde[i].tus.f.bdeSize =
19256 SLI4_PAGE_SIZE;
19257 written += SLI4_PAGE_SIZE;
19258 }
19259 i++;
19260 }
19261 wr_object->u.request.bde_count = i;
19262 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
19263 if (!phba->sli4_hba.intr_enable)
19264 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
19265 else {
James Smarta183a152011-10-10 21:32:43 -040019266 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040019267 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
19268 }
19269 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart50212672018-12-13 15:17:57 -080019270 shdr_status = bf_get(lpfc_mbox_hdr_status,
19271 &wr_object->header.cfg_shdr.response);
19272 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
19273 &wr_object->header.cfg_shdr.response);
19274 if (check_change_status) {
19275 shdr_change_status = bf_get(lpfc_wr_object_change_status,
19276 &wr_object->u.response);
19277 switch (shdr_change_status) {
19278 case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
19279 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19280 "3198 Firmware write complete: System "
19281 "reboot required to instantiate\n");
19282 break;
19283 case (LPFC_CHANGE_STATUS_FW_RESET):
19284 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19285 "3199 Firmware write complete: Firmware"
19286 " reset required to instantiate\n");
19287 break;
19288 case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
19289 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19290 "3200 Firmware write complete: Port "
19291 "Migration or PCI Reset required to "
19292 "instantiate\n");
19293 break;
19294 case (LPFC_CHANGE_STATUS_PCI_RESET):
19295 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19296 "3201 Firmware write complete: PCI "
19297 "Reset required to instantiate\n");
19298 break;
19299 default:
19300 break;
19301 }
19302 }
James Smart52d52442011-05-24 11:42:45 -040019303 if (rc != MBX_TIMEOUT)
19304 mempool_free(mbox, phba->mbox_mem_pool);
19305 if (shdr_status || shdr_add_status || rc) {
19306 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19307 "3025 Write Object mailbox failed with "
19308 "status x%x add_status x%x, mbx status x%x\n",
19309 shdr_status, shdr_add_status, rc);
19310 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080019311 *offset = shdr_add_status;
James Smart52d52442011-05-24 11:42:45 -040019312 } else
19313 *offset += wr_object->u.response.actual_write_length;
19314 return rc;
19315}
19316
19317/**
James Smart695a8142010-01-26 23:08:03 -050019318 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
19319 * @vport: pointer to vport data structure.
19320 *
19321 * This function iterate through the mailboxq and clean up all REG_LOGIN
19322 * and REG_VPI mailbox commands associated with the vport. This function
19323 * is called when driver want to restart discovery of the vport due to
19324 * a Clear Virtual Link event.
19325 **/
19326void
19327lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
19328{
19329 struct lpfc_hba *phba = vport->phba;
19330 LPFC_MBOXQ_t *mb, *nextmb;
19331 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040019332 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040019333 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040019334 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040019335 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050019336 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050019337
James Smartd439d282010-09-29 11:18:45 -040019338 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050019339 spin_lock_irq(&phba->hbalock);
19340 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
19341 if (mb->vport != vport)
19342 continue;
19343
19344 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19345 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19346 continue;
19347
James Smartd439d282010-09-29 11:18:45 -040019348 list_del(&mb->list);
19349 list_add_tail(&mb->list, &mbox_cmd_list);
19350 }
19351 /* Clean up active mailbox command with the vport */
19352 mb = phba->sli.mbox_active;
19353 if (mb && (mb->vport == vport)) {
19354 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
19355 (mb->u.mb.mbxCommand == MBX_REG_VPI))
19356 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19357 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019358 act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -040019359 /* Put reference count for delayed processing */
19360 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
19361 /* Unregister the RPI when mailbox complete */
19362 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19363 }
19364 }
James Smart63e801c2010-11-20 23:14:19 -050019365 /* Cleanup any mailbox completions which are not yet processed */
19366 do {
19367 restart_loop = 0;
19368 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
19369 /*
19370 * If this mailox is already processed or it is
19371 * for another vport ignore it.
19372 */
19373 if ((mb->vport != vport) ||
19374 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
19375 continue;
19376
19377 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19378 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19379 continue;
19380
19381 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19382 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019383 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smart63e801c2010-11-20 23:14:19 -050019384 /* Unregister the RPI when mailbox complete */
19385 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19386 restart_loop = 1;
19387 spin_unlock_irq(&phba->hbalock);
19388 spin_lock(shost->host_lock);
19389 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19390 spin_unlock(shost->host_lock);
19391 spin_lock_irq(&phba->hbalock);
19392 break;
19393 }
19394 }
19395 } while (restart_loop);
19396
James Smartd439d282010-09-29 11:18:45 -040019397 spin_unlock_irq(&phba->hbalock);
19398
19399 /* Release the cleaned-up mailbox commands */
19400 while (!list_empty(&mbox_cmd_list)) {
19401 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050019402 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019403 mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
James Smart695a8142010-01-26 23:08:03 -050019404 if (mp) {
19405 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19406 kfree(mp);
19407 }
James Smart3e1f0712018-11-29 16:09:29 -080019408 mb->ctx_buf = NULL;
19409 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
19410 mb->ctx_ndlp = NULL;
James Smart78730cf2010-04-06 15:06:30 -040019411 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019412 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040019413 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019414 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040019415 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040019416 }
James Smart695a8142010-01-26 23:08:03 -050019417 }
James Smart695a8142010-01-26 23:08:03 -050019418 mempool_free(mb, phba->mbox_mem_pool);
19419 }
James Smartd439d282010-09-29 11:18:45 -040019420
19421 /* Release the ndlp with the cleaned-up active mailbox command */
19422 if (act_mbx_ndlp) {
19423 spin_lock(shost->host_lock);
19424 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19425 spin_unlock(shost->host_lock);
19426 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050019427 }
James Smart695a8142010-01-26 23:08:03 -050019428}
19429
James Smart2a9bf3d2010-06-07 15:24:45 -040019430/**
19431 * lpfc_drain_txq - Drain the txq
19432 * @phba: Pointer to HBA context object.
19433 *
19434 * This function attempt to submit IOCBs on the txq
19435 * to the adapter. For SLI4 adapters, the txq contains
19436 * ELS IOCBs that have been deferred because the there
19437 * are no SGLs. This congestion can occur with large
19438 * vport counts during node discovery.
19439 **/
19440
19441uint32_t
19442lpfc_drain_txq(struct lpfc_hba *phba)
19443{
19444 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080019445 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090019446 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040019447 unsigned long iflags = 0;
19448 char *fail_msg = NULL;
19449 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080019450 union lpfc_wqe128 wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040019451 uint32_t txq_cnt = 0;
James Smartdc19e3b2018-05-24 21:08:57 -070019452 struct lpfc_queue *wq;
James Smart2a9bf3d2010-06-07 15:24:45 -040019453
James Smartdc19e3b2018-05-24 21:08:57 -070019454 if (phba->link_flag & LS_MDS_LOOPBACK) {
19455 /* MDS WQE are posted only to first WQ*/
James Smartc00f62e2019-08-14 16:57:11 -070019456 wq = phba->sli4_hba.hdwq[0].io_wq;
James Smartdc19e3b2018-05-24 21:08:57 -070019457 if (unlikely(!wq))
19458 return 0;
19459 pring = wq->pring;
19460 } else {
19461 wq = phba->sli4_hba.els_wq;
19462 if (unlikely(!wq))
19463 return 0;
19464 pring = lpfc_phba_elsring(phba);
19465 }
19466
19467 if (unlikely(!pring) || list_empty(&pring->txq))
Dick Kennedy1234a6d2017-09-29 17:34:29 -070019468 return 0;
James Smart895427b2017-02-12 13:52:30 -080019469
James Smart398d81c2013-05-31 17:04:19 -040019470 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050019471 list_for_each_entry(piocbq, &pring->txq, list) {
19472 txq_cnt++;
19473 }
19474
19475 if (txq_cnt > pring->txq_max)
19476 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019477
James Smart398d81c2013-05-31 17:04:19 -040019478 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019479
James Smart0e9bb8d2013-03-01 16:35:12 -050019480 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040019481 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019482
James Smart19ca7602010-11-20 23:11:55 -050019483 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040019484 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040019485 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040019486 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19487 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050019488 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040019489 break;
19490 }
James Smart895427b2017-02-12 13:52:30 -080019491 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040019492 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050019493 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040019494 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019495 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040019496 }
James Smart0e9bb8d2013-03-01 16:35:12 -050019497 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040019498
19499 /* The xri and iocb resources secured,
19500 * attempt to issue request
19501 */
James Smart6d368e52011-05-24 11:44:12 -040019502 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040019503 piocbq->sli4_xritag = sglq->sli4_xritag;
19504 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19505 fail_msg = "to convert bpl to sgl";
James Smart205e8242018-03-05 12:04:03 -080019506 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019507 fail_msg = "to convert iocb to wqe";
James Smartdc19e3b2018-05-24 21:08:57 -070019508 else if (lpfc_sli4_wq_put(wq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019509 fail_msg = " - Wq is full";
19510 else
19511 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19512
19513 if (fail_msg) {
19514 /* Failed means we can't issue and need to cancel */
19515 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19516 "2822 IOCB failed %s iotag 0x%x "
19517 "xri 0x%x\n",
19518 fail_msg,
19519 piocbq->iotag, piocbq->sli4_xritag);
19520 list_add_tail(&piocbq->list, &completions);
19521 }
James Smart398d81c2013-05-31 17:04:19 -040019522 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019523 }
19524
James Smart2a9bf3d2010-06-07 15:24:45 -040019525 /* Cancel all the IOCBs that cannot be issued */
19526 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19527 IOERR_SLI_ABORTED);
19528
James Smart0e9bb8d2013-03-01 16:35:12 -050019529 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019530}
James Smart895427b2017-02-12 13:52:30 -080019531
19532/**
19533 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19534 * @phba: Pointer to HBA context object.
19535 * @pwqe: Pointer to command WQE.
19536 * @sglq: Pointer to the scatter gather queue object.
19537 *
19538 * This routine converts the bpl or bde that is in the WQE
19539 * to a sgl list for the sli4 hardware. The physical address
19540 * of the bpl/bde is converted back to a virtual address.
19541 * If the WQE contains a BPL then the list of BDE's is
19542 * converted to sli4_sge's. If the WQE contains a single
19543 * BDE then it is converted to a single sli_sge.
19544 * The WQE is still in cpu endianness so the contents of
19545 * the bpl can be used without byte swapping.
19546 *
19547 * Returns valid XRI = Success, NO_XRI = Failure.
19548 */
19549static uint16_t
19550lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19551 struct lpfc_sglq *sglq)
19552{
19553 uint16_t xritag = NO_XRI;
19554 struct ulp_bde64 *bpl = NULL;
19555 struct ulp_bde64 bde;
19556 struct sli4_sge *sgl = NULL;
19557 struct lpfc_dmabuf *dmabuf;
James Smart205e8242018-03-05 12:04:03 -080019558 union lpfc_wqe128 *wqe;
James Smart895427b2017-02-12 13:52:30 -080019559 int numBdes = 0;
19560 int i = 0;
19561 uint32_t offset = 0; /* accumulated offset in the sg request list */
19562 int inbound = 0; /* number of sg reply entries inbound from firmware */
19563 uint32_t cmd;
19564
19565 if (!pwqeq || !sglq)
19566 return xritag;
19567
19568 sgl = (struct sli4_sge *)sglq->sgl;
19569 wqe = &pwqeq->wqe;
19570 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19571
19572 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19573 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19574 return sglq->sli4_xritag;
19575 numBdes = pwqeq->rsvd2;
19576 if (numBdes) {
19577 /* The addrHigh and addrLow fields within the WQE
19578 * have not been byteswapped yet so there is no
19579 * need to swap them back.
19580 */
19581 if (pwqeq->context3)
19582 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19583 else
19584 return xritag;
19585
19586 bpl = (struct ulp_bde64 *)dmabuf->virt;
19587 if (!bpl)
19588 return xritag;
19589
19590 for (i = 0; i < numBdes; i++) {
19591 /* Should already be byte swapped. */
19592 sgl->addr_hi = bpl->addrHigh;
19593 sgl->addr_lo = bpl->addrLow;
19594
19595 sgl->word2 = le32_to_cpu(sgl->word2);
19596 if ((i+1) == numBdes)
19597 bf_set(lpfc_sli4_sge_last, sgl, 1);
19598 else
19599 bf_set(lpfc_sli4_sge_last, sgl, 0);
19600 /* swap the size field back to the cpu so we
19601 * can assign it to the sgl.
19602 */
19603 bde.tus.w = le32_to_cpu(bpl->tus.w);
19604 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19605 /* The offsets in the sgl need to be accumulated
19606 * separately for the request and reply lists.
19607 * The request is always first, the reply follows.
19608 */
19609 switch (cmd) {
19610 case CMD_GEN_REQUEST64_WQE:
19611 /* add up the reply sg entries */
19612 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19613 inbound++;
19614 /* first inbound? reset the offset */
19615 if (inbound == 1)
19616 offset = 0;
19617 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19618 bf_set(lpfc_sli4_sge_type, sgl,
19619 LPFC_SGE_TYPE_DATA);
19620 offset += bde.tus.f.bdeSize;
19621 break;
19622 case CMD_FCP_TRSP64_WQE:
19623 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19624 bf_set(lpfc_sli4_sge_type, sgl,
19625 LPFC_SGE_TYPE_DATA);
19626 break;
19627 case CMD_FCP_TSEND64_WQE:
19628 case CMD_FCP_TRECEIVE64_WQE:
19629 bf_set(lpfc_sli4_sge_type, sgl,
19630 bpl->tus.f.bdeFlags);
19631 if (i < 3)
19632 offset = 0;
19633 else
19634 offset += bde.tus.f.bdeSize;
19635 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19636 break;
19637 }
19638 sgl->word2 = cpu_to_le32(sgl->word2);
19639 bpl++;
19640 sgl++;
19641 }
19642 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19643 /* The addrHigh and addrLow fields of the BDE have not
19644 * been byteswapped yet so they need to be swapped
19645 * before putting them in the sgl.
19646 */
19647 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19648 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19649 sgl->word2 = le32_to_cpu(sgl->word2);
19650 bf_set(lpfc_sli4_sge_last, sgl, 1);
19651 sgl->word2 = cpu_to_le32(sgl->word2);
19652 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19653 }
19654 return sglq->sli4_xritag;
19655}
19656
19657/**
19658 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19659 * @phba: Pointer to HBA context object.
19660 * @ring_number: Base sli ring number
19661 * @pwqe: Pointer to command WQE.
19662 **/
19663int
James Smart1fbf9742019-01-28 11:14:26 -080019664lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
James Smart895427b2017-02-12 13:52:30 -080019665 struct lpfc_iocbq *pwqe)
19666{
James Smart205e8242018-03-05 12:04:03 -080019667 union lpfc_wqe128 *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080019668 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080019669 struct lpfc_queue *wq;
19670 struct lpfc_sglq *sglq;
19671 struct lpfc_sli_ring *pring;
19672 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070019673 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080019674
19675 /* NVME_LS and NVME_LS ABTS requests. */
19676 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19677 pring = phba->sli4_hba.nvmels_wq->pring;
James Smart6a828b02019-01-28 11:14:31 -080019678 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
19679 qp, wq_access);
James Smart895427b2017-02-12 13:52:30 -080019680 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19681 if (!sglq) {
19682 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19683 return WQE_BUSY;
19684 }
19685 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19686 pwqe->sli4_xritag = sglq->sli4_xritag;
19687 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19688 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19689 return WQE_ERROR;
19690 }
19691 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19692 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070019693 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19694 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019695 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019696 return ret;
James Smart895427b2017-02-12 13:52:30 -080019697 }
Dick Kennedycd22d602017-08-23 16:55:35 -070019698
James Smart895427b2017-02-12 13:52:30 -080019699 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19700 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19701 return 0;
19702 }
19703
19704 /* NVME_FCREQ and NVME_ABTS requests */
19705 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19706 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smartc00f62e2019-08-14 16:57:11 -070019707 wq = qp->io_wq;
James Smart1fbf9742019-01-28 11:14:26 -080019708 pring = wq->pring;
James Smart895427b2017-02-12 13:52:30 -080019709
James Smartc00f62e2019-08-14 16:57:11 -070019710 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
James Smart895427b2017-02-12 13:52:30 -080019711
James Smart6a828b02019-01-28 11:14:31 -080019712 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
19713 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070019714 ret = lpfc_sli4_wq_put(wq, wqe);
19715 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019716 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019717 return ret;
James Smart895427b2017-02-12 13:52:30 -080019718 }
19719 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19720 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19721 return 0;
19722 }
19723
James Smartf358dd02017-02-12 13:52:34 -080019724 /* NVMET requests */
19725 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19726 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smartc00f62e2019-08-14 16:57:11 -070019727 wq = qp->io_wq;
James Smart1fbf9742019-01-28 11:14:26 -080019728 pring = wq->pring;
James Smartf358dd02017-02-12 13:52:34 -080019729
James Smartf358dd02017-02-12 13:52:34 -080019730 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070019731 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080019732 if (pwqe->sli4_xritag == NO_XRI) {
19733 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19734 pwqe->sli4_xritag = sglq->sli4_xritag;
19735 }
19736 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19737 pwqe->sli4_xritag);
James Smartc00f62e2019-08-14 16:57:11 -070019738 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
James Smart1fbf9742019-01-28 11:14:26 -080019739
James Smart6a828b02019-01-28 11:14:31 -080019740 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
19741 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070019742 ret = lpfc_sli4_wq_put(wq, wqe);
19743 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080019744 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019745 return ret;
James Smartf358dd02017-02-12 13:52:34 -080019746 }
19747 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19748 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19749 return 0;
19750 }
James Smart895427b2017-02-12 13:52:30 -080019751 return WQE_ERROR;
19752}
James Smartc4908502019-01-28 11:14:28 -080019753
19754#ifdef LPFC_MXP_STAT
19755/**
19756 * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
19757 * @phba: pointer to lpfc hba data structure.
19758 * @hwqid: belong to which HWQ.
19759 *
19760 * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
19761 * 15 seconds after a test case is running.
19762 *
19763 * The user should call lpfc_debugfs_multixripools_write before running a test
19764 * case to clear stat_snapshot_taken. Then the user starts a test case. During
19765 * test case is running, stat_snapshot_taken is incremented by 1 every time when
19766 * this routine is called from heartbeat timer. When stat_snapshot_taken is
19767 * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
19768 **/
19769void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
19770{
19771 struct lpfc_sli4_hdw_queue *qp;
19772 struct lpfc_multixri_pool *multixri_pool;
19773 struct lpfc_pvt_pool *pvt_pool;
19774 struct lpfc_pbl_pool *pbl_pool;
19775 u32 txcmplq_cnt;
19776
19777 qp = &phba->sli4_hba.hdwq[hwqid];
19778 multixri_pool = qp->p_multixri_pool;
19779 if (!multixri_pool)
19780 return;
19781
19782 if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
19783 pvt_pool = &qp->p_multixri_pool->pvt_pool;
19784 pbl_pool = &qp->p_multixri_pool->pbl_pool;
James Smartc00f62e2019-08-14 16:57:11 -070019785 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080019786
19787 multixri_pool->stat_pbl_count = pbl_pool->count;
19788 multixri_pool->stat_pvt_count = pvt_pool->count;
19789 multixri_pool->stat_busy_count = txcmplq_cnt;
19790 }
19791
19792 multixri_pool->stat_snapshot_taken++;
19793}
19794#endif
19795
19796/**
19797 * lpfc_adjust_pvt_pool_count - Adjust private pool count
19798 * @phba: pointer to lpfc hba data structure.
19799 * @hwqid: belong to which HWQ.
19800 *
19801 * This routine moves some XRIs from private to public pool when private pool
19802 * is not busy.
19803 **/
19804void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
19805{
19806 struct lpfc_multixri_pool *multixri_pool;
19807 u32 io_req_count;
19808 u32 prev_io_req_count;
19809
19810 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
19811 if (!multixri_pool)
19812 return;
19813 io_req_count = multixri_pool->io_req_count;
19814 prev_io_req_count = multixri_pool->prev_io_req_count;
19815
19816 if (prev_io_req_count != io_req_count) {
19817 /* Private pool is busy */
19818 multixri_pool->prev_io_req_count = io_req_count;
19819 } else {
19820 /* Private pool is not busy.
19821 * Move XRIs from private to public pool.
19822 */
19823 lpfc_move_xri_pvt_to_pbl(phba, hwqid);
19824 }
19825}
19826
19827/**
19828 * lpfc_adjust_high_watermark - Adjust high watermark
19829 * @phba: pointer to lpfc hba data structure.
19830 * @hwqid: belong to which HWQ.
19831 *
19832 * This routine sets high watermark as number of outstanding XRIs,
19833 * but make sure the new value is between xri_limit/2 and xri_limit.
19834 **/
19835void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
19836{
19837 u32 new_watermark;
19838 u32 watermark_max;
19839 u32 watermark_min;
19840 u32 xri_limit;
19841 u32 txcmplq_cnt;
19842 u32 abts_io_bufs;
19843 struct lpfc_multixri_pool *multixri_pool;
19844 struct lpfc_sli4_hdw_queue *qp;
19845
19846 qp = &phba->sli4_hba.hdwq[hwqid];
19847 multixri_pool = qp->p_multixri_pool;
19848 if (!multixri_pool)
19849 return;
19850 xri_limit = multixri_pool->xri_limit;
19851
19852 watermark_max = xri_limit;
19853 watermark_min = xri_limit / 2;
19854
James Smartc00f62e2019-08-14 16:57:11 -070019855 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080019856 abts_io_bufs = qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -070019857 abts_io_bufs += qp->abts_nvme_io_bufs;
James Smartc4908502019-01-28 11:14:28 -080019858
19859 new_watermark = txcmplq_cnt + abts_io_bufs;
19860 new_watermark = min(watermark_max, new_watermark);
19861 new_watermark = max(watermark_min, new_watermark);
19862 multixri_pool->pvt_pool.high_watermark = new_watermark;
19863
19864#ifdef LPFC_MXP_STAT
19865 multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
19866 new_watermark);
19867#endif
19868}
19869
19870/**
19871 * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
19872 * @phba: pointer to lpfc hba data structure.
19873 * @hwqid: belong to which HWQ.
19874 *
19875 * This routine is called from hearbeat timer when pvt_pool is idle.
19876 * All free XRIs are moved from private to public pool on hwqid with 2 steps.
19877 * The first step moves (all - low_watermark) amount of XRIs.
19878 * The second step moves the rest of XRIs.
19879 **/
19880void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
19881{
19882 struct lpfc_pbl_pool *pbl_pool;
19883 struct lpfc_pvt_pool *pvt_pool;
James Smart6a828b02019-01-28 11:14:31 -080019884 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080019885 struct lpfc_io_buf *lpfc_ncmd;
19886 struct lpfc_io_buf *lpfc_ncmd_next;
19887 unsigned long iflag;
19888 struct list_head tmp_list;
19889 u32 tmp_count;
19890
James Smart6a828b02019-01-28 11:14:31 -080019891 qp = &phba->sli4_hba.hdwq[hwqid];
19892 pbl_pool = &qp->p_multixri_pool->pbl_pool;
19893 pvt_pool = &qp->p_multixri_pool->pvt_pool;
James Smartc4908502019-01-28 11:14:28 -080019894 tmp_count = 0;
19895
James Smart6a828b02019-01-28 11:14:31 -080019896 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
19897 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080019898
19899 if (pvt_pool->count > pvt_pool->low_watermark) {
19900 /* Step 1: move (all - low_watermark) from pvt_pool
19901 * to pbl_pool
19902 */
19903
19904 /* Move low watermark of bufs from pvt_pool to tmp_list */
19905 INIT_LIST_HEAD(&tmp_list);
19906 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
19907 &pvt_pool->list, list) {
19908 list_move_tail(&lpfc_ncmd->list, &tmp_list);
19909 tmp_count++;
19910 if (tmp_count >= pvt_pool->low_watermark)
19911 break;
19912 }
19913
19914 /* Move all bufs from pvt_pool to pbl_pool */
19915 list_splice_init(&pvt_pool->list, &pbl_pool->list);
19916
19917 /* Move all bufs from tmp_list to pvt_pool */
19918 list_splice(&tmp_list, &pvt_pool->list);
19919
19920 pbl_pool->count += (pvt_pool->count - tmp_count);
19921 pvt_pool->count = tmp_count;
19922 } else {
19923 /* Step 2: move the rest from pvt_pool to pbl_pool */
19924 list_splice_init(&pvt_pool->list, &pbl_pool->list);
19925 pbl_pool->count += pvt_pool->count;
19926 pvt_pool->count = 0;
19927 }
19928
19929 spin_unlock(&pvt_pool->lock);
19930 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
19931}
19932
19933/**
19934 * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
19935 * @phba: pointer to lpfc hba data structure
19936 * @pbl_pool: specified public free XRI pool
19937 * @pvt_pool: specified private free XRI pool
19938 * @count: number of XRIs to move
19939 *
19940 * This routine tries to move some free common bufs from the specified pbl_pool
19941 * to the specified pvt_pool. It might move less than count XRIs if there's not
19942 * enough in public pool.
19943 *
19944 * Return:
19945 * true - if XRIs are successfully moved from the specified pbl_pool to the
19946 * specified pvt_pool
19947 * false - if the specified pbl_pool is empty or locked by someone else
19948 **/
19949static bool
James Smart6a828b02019-01-28 11:14:31 -080019950_lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
19951 struct lpfc_pbl_pool *pbl_pool,
James Smartc4908502019-01-28 11:14:28 -080019952 struct lpfc_pvt_pool *pvt_pool, u32 count)
19953{
19954 struct lpfc_io_buf *lpfc_ncmd;
19955 struct lpfc_io_buf *lpfc_ncmd_next;
19956 unsigned long iflag;
19957 int ret;
19958
19959 ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
19960 if (ret) {
19961 if (pbl_pool->count) {
19962 /* Move a batch of XRIs from public to private pool */
James Smart6a828b02019-01-28 11:14:31 -080019963 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080019964 list_for_each_entry_safe(lpfc_ncmd,
19965 lpfc_ncmd_next,
19966 &pbl_pool->list,
19967 list) {
19968 list_move_tail(&lpfc_ncmd->list,
19969 &pvt_pool->list);
19970 pvt_pool->count++;
19971 pbl_pool->count--;
19972 count--;
19973 if (count == 0)
19974 break;
19975 }
19976
19977 spin_unlock(&pvt_pool->lock);
19978 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
19979 return true;
19980 }
19981 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
19982 }
19983
19984 return false;
19985}
19986
19987/**
19988 * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
19989 * @phba: pointer to lpfc hba data structure.
19990 * @hwqid: belong to which HWQ.
19991 * @count: number of XRIs to move
19992 *
19993 * This routine tries to find some free common bufs in one of public pools with
19994 * Round Robin method. The search always starts from local hwqid, then the next
19995 * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
19996 * a batch of free common bufs are moved to private pool on hwqid.
19997 * It might move less than count XRIs if there's not enough in public pool.
19998 **/
19999void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
20000{
20001 struct lpfc_multixri_pool *multixri_pool;
20002 struct lpfc_multixri_pool *next_multixri_pool;
20003 struct lpfc_pvt_pool *pvt_pool;
20004 struct lpfc_pbl_pool *pbl_pool;
James Smart6a828b02019-01-28 11:14:31 -080020005 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080020006 u32 next_hwqid;
20007 u32 hwq_count;
20008 int ret;
20009
James Smart6a828b02019-01-28 11:14:31 -080020010 qp = &phba->sli4_hba.hdwq[hwqid];
20011 multixri_pool = qp->p_multixri_pool;
James Smartc4908502019-01-28 11:14:28 -080020012 pvt_pool = &multixri_pool->pvt_pool;
20013 pbl_pool = &multixri_pool->pbl_pool;
20014
20015 /* Check if local pbl_pool is available */
James Smart6a828b02019-01-28 11:14:31 -080020016 ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080020017 if (ret) {
20018#ifdef LPFC_MXP_STAT
20019 multixri_pool->local_pbl_hit_count++;
20020#endif
20021 return;
20022 }
20023
20024 hwq_count = phba->cfg_hdw_queue;
20025
20026 /* Get the next hwqid which was found last time */
20027 next_hwqid = multixri_pool->rrb_next_hwqid;
20028
20029 do {
20030 /* Go to next hwq */
20031 next_hwqid = (next_hwqid + 1) % hwq_count;
20032
20033 next_multixri_pool =
20034 phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
20035 pbl_pool = &next_multixri_pool->pbl_pool;
20036
20037 /* Check if the public free xri pool is available */
20038 ret = _lpfc_move_xri_pbl_to_pvt(
James Smart6a828b02019-01-28 11:14:31 -080020039 phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080020040
20041 /* Exit while-loop if success or all hwqid are checked */
20042 } while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
20043
20044 /* Starting point for the next time */
20045 multixri_pool->rrb_next_hwqid = next_hwqid;
20046
20047 if (!ret) {
20048 /* stats: all public pools are empty*/
20049 multixri_pool->pbl_empty_count++;
20050 }
20051
20052#ifdef LPFC_MXP_STAT
20053 if (ret) {
20054 if (next_hwqid == hwqid)
20055 multixri_pool->local_pbl_hit_count++;
20056 else
20057 multixri_pool->other_pbl_hit_count++;
20058 }
20059#endif
20060}
20061
20062/**
20063 * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
20064 * @phba: pointer to lpfc hba data structure.
20065 * @qp: belong to which HWQ.
20066 *
20067 * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
20068 * low watermark.
20069 **/
20070void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
20071{
20072 struct lpfc_multixri_pool *multixri_pool;
20073 struct lpfc_pvt_pool *pvt_pool;
20074
20075 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
20076 pvt_pool = &multixri_pool->pvt_pool;
20077
20078 if (pvt_pool->count < pvt_pool->low_watermark)
20079 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20080}
20081
20082/**
20083 * lpfc_release_io_buf - Return one IO buf back to free pool
20084 * @phba: pointer to lpfc hba data structure.
20085 * @lpfc_ncmd: IO buf to be returned.
20086 * @qp: belong to which HWQ.
20087 *
20088 * This routine returns one IO buf back to free pool. If this is an urgent IO,
20089 * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
20090 * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
20091 * xri_limit. If cfg_xri_rebalancing==0, the IO buf is returned to
20092 * lpfc_io_buf_list_put.
20093 **/
20094void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
20095 struct lpfc_sli4_hdw_queue *qp)
20096{
20097 unsigned long iflag;
20098 struct lpfc_pbl_pool *pbl_pool;
20099 struct lpfc_pvt_pool *pvt_pool;
20100 struct lpfc_epd_pool *epd_pool;
20101 u32 txcmplq_cnt;
20102 u32 xri_owned;
20103 u32 xri_limit;
20104 u32 abts_io_bufs;
20105
20106 /* MUST zero fields if buffer is reused by another protocol */
20107 lpfc_ncmd->nvmeCmd = NULL;
20108 lpfc_ncmd->cur_iocbq.wqe_cmpl = NULL;
20109 lpfc_ncmd->cur_iocbq.iocb_cmpl = NULL;
20110
20111 if (phba->cfg_xri_rebalancing) {
20112 if (lpfc_ncmd->expedite) {
20113 /* Return to expedite pool */
20114 epd_pool = &phba->epd_pool;
20115 spin_lock_irqsave(&epd_pool->lock, iflag);
20116 list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
20117 epd_pool->count++;
20118 spin_unlock_irqrestore(&epd_pool->lock, iflag);
20119 return;
20120 }
20121
20122 /* Avoid invalid access if an IO sneaks in and is being rejected
20123 * just _after_ xri pools are destroyed in lpfc_offline.
20124 * Nothing much can be done at this point.
20125 */
20126 if (!qp->p_multixri_pool)
20127 return;
20128
20129 pbl_pool = &qp->p_multixri_pool->pbl_pool;
20130 pvt_pool = &qp->p_multixri_pool->pvt_pool;
20131
James Smartc00f62e2019-08-14 16:57:11 -070020132 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080020133 abts_io_bufs = qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -070020134 abts_io_bufs += qp->abts_nvme_io_bufs;
James Smartc4908502019-01-28 11:14:28 -080020135
20136 xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
20137 xri_limit = qp->p_multixri_pool->xri_limit;
20138
20139#ifdef LPFC_MXP_STAT
20140 if (xri_owned <= xri_limit)
20141 qp->p_multixri_pool->below_limit_count++;
20142 else
20143 qp->p_multixri_pool->above_limit_count++;
20144#endif
20145
20146 /* XRI goes to either public or private free xri pool
20147 * based on watermark and xri_limit
20148 */
20149 if ((pvt_pool->count < pvt_pool->low_watermark) ||
20150 (xri_owned < xri_limit &&
20151 pvt_pool->count < pvt_pool->high_watermark)) {
James Smart6a828b02019-01-28 11:14:31 -080020152 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
20153 qp, free_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020154 list_add_tail(&lpfc_ncmd->list,
20155 &pvt_pool->list);
20156 pvt_pool->count++;
20157 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20158 } else {
James Smart6a828b02019-01-28 11:14:31 -080020159 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
20160 qp, free_pub_pool);
James Smartc4908502019-01-28 11:14:28 -080020161 list_add_tail(&lpfc_ncmd->list,
20162 &pbl_pool->list);
20163 pbl_pool->count++;
20164 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20165 }
20166 } else {
James Smart6a828b02019-01-28 11:14:31 -080020167 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
20168 qp, free_xri);
James Smartc4908502019-01-28 11:14:28 -080020169 list_add_tail(&lpfc_ncmd->list,
20170 &qp->lpfc_io_buf_list_put);
20171 qp->put_io_bufs++;
20172 spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
20173 iflag);
20174 }
James Smartd79c9e92019-08-14 16:57:09 -070020175
20176 if (phba->cfg_xpsgl && !phba->nvmet_support &&
20177 !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))
20178 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
20179
20180 if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))
20181 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
James Smartc4908502019-01-28 11:14:28 -080020182}
20183
20184/**
20185 * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
20186 * @phba: pointer to lpfc hba data structure.
20187 * @pvt_pool: pointer to private pool data structure.
20188 * @ndlp: pointer to lpfc nodelist data structure.
20189 *
20190 * This routine tries to get one free IO buf from private pool.
20191 *
20192 * Return:
20193 * pointer to one free IO buf - if private pool is not empty
20194 * NULL - if private pool is empty
20195 **/
20196static struct lpfc_io_buf *
20197lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
James Smart6a828b02019-01-28 11:14:31 -080020198 struct lpfc_sli4_hdw_queue *qp,
James Smartc4908502019-01-28 11:14:28 -080020199 struct lpfc_pvt_pool *pvt_pool,
20200 struct lpfc_nodelist *ndlp)
20201{
20202 struct lpfc_io_buf *lpfc_ncmd;
20203 struct lpfc_io_buf *lpfc_ncmd_next;
20204 unsigned long iflag;
20205
James Smart6a828b02019-01-28 11:14:31 -080020206 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020207 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20208 &pvt_pool->list, list) {
20209 if (lpfc_test_rrq_active(
20210 phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
20211 continue;
20212 list_del(&lpfc_ncmd->list);
20213 pvt_pool->count--;
20214 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20215 return lpfc_ncmd;
20216 }
20217 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20218
20219 return NULL;
20220}
20221
20222/**
20223 * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
20224 * @phba: pointer to lpfc hba data structure.
20225 *
20226 * This routine tries to get one free IO buf from expedite pool.
20227 *
20228 * Return:
20229 * pointer to one free IO buf - if expedite pool is not empty
20230 * NULL - if expedite pool is empty
20231 **/
20232static struct lpfc_io_buf *
20233lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
20234{
20235 struct lpfc_io_buf *lpfc_ncmd;
20236 struct lpfc_io_buf *lpfc_ncmd_next;
20237 unsigned long iflag;
20238 struct lpfc_epd_pool *epd_pool;
20239
20240 epd_pool = &phba->epd_pool;
20241 lpfc_ncmd = NULL;
20242
20243 spin_lock_irqsave(&epd_pool->lock, iflag);
20244 if (epd_pool->count > 0) {
20245 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20246 &epd_pool->list, list) {
20247 list_del(&lpfc_ncmd->list);
20248 epd_pool->count--;
20249 break;
20250 }
20251 }
20252 spin_unlock_irqrestore(&epd_pool->lock, iflag);
20253
20254 return lpfc_ncmd;
20255}
20256
20257/**
20258 * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
20259 * @phba: pointer to lpfc hba data structure.
20260 * @ndlp: pointer to lpfc nodelist data structure.
20261 * @hwqid: belong to which HWQ
20262 * @expedite: 1 means this request is urgent.
20263 *
20264 * This routine will do the following actions and then return a pointer to
20265 * one free IO buf.
20266 *
20267 * 1. If private free xri count is empty, move some XRIs from public to
20268 * private pool.
20269 * 2. Get one XRI from private free xri pool.
20270 * 3. If we fail to get one from pvt_pool and this is an expedite request,
20271 * get one free xri from expedite pool.
20272 *
20273 * Note: ndlp is only used on SCSI side for RRQ testing.
20274 * The caller should pass NULL for ndlp on NVME side.
20275 *
20276 * Return:
20277 * pointer to one free IO buf - if private pool is not empty
20278 * NULL - if private pool is empty
20279 **/
20280static struct lpfc_io_buf *
20281lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
20282 struct lpfc_nodelist *ndlp,
20283 int hwqid, int expedite)
20284{
20285 struct lpfc_sli4_hdw_queue *qp;
20286 struct lpfc_multixri_pool *multixri_pool;
20287 struct lpfc_pvt_pool *pvt_pool;
20288 struct lpfc_io_buf *lpfc_ncmd;
20289
20290 qp = &phba->sli4_hba.hdwq[hwqid];
20291 lpfc_ncmd = NULL;
20292 multixri_pool = qp->p_multixri_pool;
20293 pvt_pool = &multixri_pool->pvt_pool;
20294 multixri_pool->io_req_count++;
20295
20296 /* If pvt_pool is empty, move some XRIs from public to private pool */
20297 if (pvt_pool->count == 0)
20298 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20299
20300 /* Get one XRI from private free xri pool */
James Smart6a828b02019-01-28 11:14:31 -080020301 lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
James Smartc4908502019-01-28 11:14:28 -080020302
20303 if (lpfc_ncmd) {
20304 lpfc_ncmd->hdwq = qp;
20305 lpfc_ncmd->hdwq_no = hwqid;
20306 } else if (expedite) {
20307 /* If we fail to get one from pvt_pool and this is an expedite
20308 * request, get one free xri from expedite pool.
20309 */
20310 lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
20311 }
20312
20313 return lpfc_ncmd;
20314}
20315
20316static inline struct lpfc_io_buf *
20317lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
20318{
20319 struct lpfc_sli4_hdw_queue *qp;
20320 struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
20321
20322 qp = &phba->sli4_hba.hdwq[idx];
20323 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
20324 &qp->lpfc_io_buf_list_get, list) {
20325 if (lpfc_test_rrq_active(phba, ndlp,
20326 lpfc_cmd->cur_iocbq.sli4_lxritag))
20327 continue;
20328
20329 if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
20330 continue;
20331
20332 list_del_init(&lpfc_cmd->list);
20333 qp->get_io_bufs--;
20334 lpfc_cmd->hdwq = qp;
20335 lpfc_cmd->hdwq_no = idx;
20336 return lpfc_cmd;
20337 }
20338 return NULL;
20339}
20340
20341/**
20342 * lpfc_get_io_buf - Get one IO buffer from free pool
20343 * @phba: The HBA for which this call is being executed.
20344 * @ndlp: pointer to lpfc nodelist data structure.
20345 * @hwqid: belong to which HWQ
20346 * @expedite: 1 means this request is urgent.
20347 *
20348 * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
20349 * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
20350 * a IO buffer from head of @hdwq io_buf_list and returns to caller.
20351 *
20352 * Note: ndlp is only used on SCSI side for RRQ testing.
20353 * The caller should pass NULL for ndlp on NVME side.
20354 *
20355 * Return codes:
20356 * NULL - Error
20357 * Pointer to lpfc_io_buf - Success
20358 **/
20359struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
20360 struct lpfc_nodelist *ndlp,
20361 u32 hwqid, int expedite)
20362{
20363 struct lpfc_sli4_hdw_queue *qp;
20364 unsigned long iflag;
20365 struct lpfc_io_buf *lpfc_cmd;
20366
20367 qp = &phba->sli4_hba.hdwq[hwqid];
20368 lpfc_cmd = NULL;
20369
20370 if (phba->cfg_xri_rebalancing)
20371 lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
20372 phba, ndlp, hwqid, expedite);
20373 else {
James Smart6a828b02019-01-28 11:14:31 -080020374 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
20375 qp, alloc_xri_get);
James Smartc4908502019-01-28 11:14:28 -080020376 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
20377 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
20378 if (!lpfc_cmd) {
James Smart6a828b02019-01-28 11:14:31 -080020379 lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
20380 qp, alloc_xri_put);
James Smartc4908502019-01-28 11:14:28 -080020381 list_splice(&qp->lpfc_io_buf_list_put,
20382 &qp->lpfc_io_buf_list_get);
20383 qp->get_io_bufs += qp->put_io_bufs;
20384 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
20385 qp->put_io_bufs = 0;
20386 spin_unlock(&qp->io_buf_list_put_lock);
20387 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
20388 expedite)
20389 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
20390 }
20391 spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
20392 }
20393
20394 return lpfc_cmd;
20395}
James Smartd79c9e92019-08-14 16:57:09 -070020396
20397/**
20398 * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool
20399 * @phba: The HBA for which this call is being executed.
20400 * @lpfc_buf: IO buf structure to append the SGL chunk
20401 *
20402 * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,
20403 * and will allocate an SGL chunk if the pool is empty.
20404 *
20405 * Return codes:
20406 * NULL - Error
20407 * Pointer to sli4_hybrid_sgl - Success
20408 **/
20409struct sli4_hybrid_sgl *
20410lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
20411{
20412 struct sli4_hybrid_sgl *list_entry = NULL;
20413 struct sli4_hybrid_sgl *tmp = NULL;
20414 struct sli4_hybrid_sgl *allocated_sgl = NULL;
20415 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
20416 struct list_head *buf_list = &hdwq->sgl_list;
20417
20418 spin_lock_irq(&hdwq->hdwq_lock);
20419
20420 if (likely(!list_empty(buf_list))) {
20421 /* break off 1 chunk from the sgl_list */
20422 list_for_each_entry_safe(list_entry, tmp,
20423 buf_list, list_node) {
20424 list_move_tail(&list_entry->list_node,
20425 &lpfc_buf->dma_sgl_xtra_list);
20426 break;
20427 }
20428 } else {
20429 /* allocate more */
20430 spin_unlock_irq(&hdwq->hdwq_lock);
20431 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
20432 cpu_to_node(smp_processor_id()));
20433 if (!tmp) {
20434 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
20435 "8353 error kmalloc memory for HDWQ "
20436 "%d %s\n",
20437 lpfc_buf->hdwq_no, __func__);
20438 return NULL;
20439 }
20440
20441 tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
20442 GFP_ATOMIC, &tmp->dma_phys_sgl);
20443 if (!tmp->dma_sgl) {
20444 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
20445 "8354 error pool_alloc memory for HDWQ "
20446 "%d %s\n",
20447 lpfc_buf->hdwq_no, __func__);
20448 kfree(tmp);
20449 return NULL;
20450 }
20451
20452 spin_lock_irq(&hdwq->hdwq_lock);
20453 list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);
20454 }
20455
20456 allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,
20457 struct sli4_hybrid_sgl,
20458 list_node);
20459
20460 spin_unlock_irq(&hdwq->hdwq_lock);
20461
20462 return allocated_sgl;
20463}
20464
20465/**
20466 * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool
20467 * @phba: The HBA for which this call is being executed.
20468 * @lpfc_buf: IO buf structure with the SGL chunk
20469 *
20470 * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.
20471 *
20472 * Return codes:
20473 * 0 - Success
20474 * -EINVAL - Error
20475 **/
20476int
20477lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
20478{
20479 int rc = 0;
20480 struct sli4_hybrid_sgl *list_entry = NULL;
20481 struct sli4_hybrid_sgl *tmp = NULL;
20482 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
20483 struct list_head *buf_list = &hdwq->sgl_list;
20484
20485 spin_lock_irq(&hdwq->hdwq_lock);
20486
20487 if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {
20488 list_for_each_entry_safe(list_entry, tmp,
20489 &lpfc_buf->dma_sgl_xtra_list,
20490 list_node) {
20491 list_move_tail(&list_entry->list_node,
20492 buf_list);
20493 }
20494 } else {
20495 rc = -EINVAL;
20496 }
20497
20498 spin_unlock_irq(&hdwq->hdwq_lock);
20499 return rc;
20500}
20501
20502/**
20503 * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool
20504 * @phba: phba object
20505 * @hdwq: hdwq to cleanup sgl buff resources on
20506 *
20507 * This routine frees all SGL chunks of hdwq SGL chunk pool.
20508 *
20509 * Return codes:
20510 * None
20511 **/
20512void
20513lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,
20514 struct lpfc_sli4_hdw_queue *hdwq)
20515{
20516 struct list_head *buf_list = &hdwq->sgl_list;
20517 struct sli4_hybrid_sgl *list_entry = NULL;
20518 struct sli4_hybrid_sgl *tmp = NULL;
20519
20520 spin_lock_irq(&hdwq->hdwq_lock);
20521
20522 /* Free sgl pool */
20523 list_for_each_entry_safe(list_entry, tmp,
20524 buf_list, list_node) {
20525 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
20526 list_entry->dma_sgl,
20527 list_entry->dma_phys_sgl);
20528 list_del(&list_entry->list_node);
20529 kfree(list_entry);
20530 }
20531
20532 spin_unlock_irq(&hdwq->hdwq_lock);
20533}
20534
20535/**
20536 * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq
20537 * @phba: The HBA for which this call is being executed.
20538 * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer
20539 *
20540 * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,
20541 * and will allocate an CMD/RSP buffer if the pool is empty.
20542 *
20543 * Return codes:
20544 * NULL - Error
20545 * Pointer to fcp_cmd_rsp_buf - Success
20546 **/
20547struct fcp_cmd_rsp_buf *
20548lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
20549 struct lpfc_io_buf *lpfc_buf)
20550{
20551 struct fcp_cmd_rsp_buf *list_entry = NULL;
20552 struct fcp_cmd_rsp_buf *tmp = NULL;
20553 struct fcp_cmd_rsp_buf *allocated_buf = NULL;
20554 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
20555 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
20556
20557 spin_lock_irq(&hdwq->hdwq_lock);
20558
20559 if (likely(!list_empty(buf_list))) {
20560 /* break off 1 chunk from the list */
20561 list_for_each_entry_safe(list_entry, tmp,
20562 buf_list,
20563 list_node) {
20564 list_move_tail(&list_entry->list_node,
20565 &lpfc_buf->dma_cmd_rsp_list);
20566 break;
20567 }
20568 } else {
20569 /* allocate more */
20570 spin_unlock_irq(&hdwq->hdwq_lock);
20571 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
20572 cpu_to_node(smp_processor_id()));
20573 if (!tmp) {
20574 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
20575 "8355 error kmalloc memory for HDWQ "
20576 "%d %s\n",
20577 lpfc_buf->hdwq_no, __func__);
20578 return NULL;
20579 }
20580
20581 tmp->fcp_cmnd = dma_pool_alloc(phba->lpfc_cmd_rsp_buf_pool,
20582 GFP_ATOMIC,
20583 &tmp->fcp_cmd_rsp_dma_handle);
20584
20585 if (!tmp->fcp_cmnd) {
20586 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
20587 "8356 error pool_alloc memory for HDWQ "
20588 "%d %s\n",
20589 lpfc_buf->hdwq_no, __func__);
20590 kfree(tmp);
20591 return NULL;
20592 }
20593
20594 tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +
20595 sizeof(struct fcp_cmnd));
20596
20597 spin_lock_irq(&hdwq->hdwq_lock);
20598 list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);
20599 }
20600
20601 allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,
20602 struct fcp_cmd_rsp_buf,
20603 list_node);
20604
20605 spin_unlock_irq(&hdwq->hdwq_lock);
20606
20607 return allocated_buf;
20608}
20609
20610/**
20611 * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool
20612 * @phba: The HBA for which this call is being executed.
20613 * @lpfc_buf: IO buf structure with the CMD/RSP buf
20614 *
20615 * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.
20616 *
20617 * Return codes:
20618 * 0 - Success
20619 * -EINVAL - Error
20620 **/
20621int
20622lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
20623 struct lpfc_io_buf *lpfc_buf)
20624{
20625 int rc = 0;
20626 struct fcp_cmd_rsp_buf *list_entry = NULL;
20627 struct fcp_cmd_rsp_buf *tmp = NULL;
20628 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
20629 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
20630
20631 spin_lock_irq(&hdwq->hdwq_lock);
20632
20633 if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {
20634 list_for_each_entry_safe(list_entry, tmp,
20635 &lpfc_buf->dma_cmd_rsp_list,
20636 list_node) {
20637 list_move_tail(&list_entry->list_node,
20638 buf_list);
20639 }
20640 } else {
20641 rc = -EINVAL;
20642 }
20643
20644 spin_unlock_irq(&hdwq->hdwq_lock);
20645 return rc;
20646}
20647
20648/**
20649 * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool
20650 * @phba: phba object
20651 * @hdwq: hdwq to cleanup cmd rsp buff resources on
20652 *
20653 * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.
20654 *
20655 * Return codes:
20656 * None
20657 **/
20658void
20659lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
20660 struct lpfc_sli4_hdw_queue *hdwq)
20661{
20662 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
20663 struct fcp_cmd_rsp_buf *list_entry = NULL;
20664 struct fcp_cmd_rsp_buf *tmp = NULL;
20665
20666 spin_lock_irq(&hdwq->hdwq_lock);
20667
20668 /* Free cmd_rsp buf pool */
20669 list_for_each_entry_safe(list_entry, tmp,
20670 buf_list,
20671 list_node) {
20672 dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,
20673 list_entry->fcp_cmnd,
20674 list_entry->fcp_cmd_rsp_dma_handle);
20675 list_del(&list_entry->list_node);
20676 kfree(list_entry);
20677 }
20678
20679 spin_unlock_irq(&hdwq->hdwq_lock);
20680}