blob: 0dfd30aa4d992da556668ea52f22740a12338902 [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 Smart9b164062019-03-12 16:30:06 -07002529 pmb->ctx_ndlp = NULL;
James Smartdea16bd2018-11-29 16:09:30 -08002530 }
James Smartd439d282010-09-29 11:18:45 -04002531 }
2532
James Smartdcf2a4e2010-09-29 11:18:53 -04002533 /* Check security permission status on INIT_LINK mailbox command */
2534 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2535 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2536 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2537 "2860 SLI authentication is required "
2538 "for INIT_LINK but has not done yet\n");
2539
James Smart04c68492009-05-22 14:52:52 -04002540 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2541 lpfc_sli4_mbox_cmd_free(phba, pmb);
2542 else
2543 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002544}
James Smartbe6bb942015-04-07 15:07:22 -04002545 /**
2546 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2547 * @phba: Pointer to HBA context object.
2548 * @pmb: Pointer to mailbox object.
2549 *
2550 * This function is the unreg rpi mailbox completion handler. It
2551 * frees the memory resources associated with the completed mailbox
2552 * command. An additional refrenece is put on the ndlp to prevent
2553 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2554 * the unreg mailbox command completes, this routine puts the
2555 * reference back.
2556 *
2557 **/
2558void
2559lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2560{
2561 struct lpfc_vport *vport = pmb->vport;
2562 struct lpfc_nodelist *ndlp;
2563
James Smart3e1f0712018-11-29 16:09:29 -08002564 ndlp = pmb->ctx_ndlp;
James Smartbe6bb942015-04-07 15:07:22 -04002565 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2566 if (phba->sli_rev == LPFC_SLI_REV4 &&
2567 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002568 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002569 LPFC_SLI_INTF_IF_TYPE_2)) {
2570 if (ndlp) {
James Smartdea16bd2018-11-29 16:09:30 -08002571 lpfc_printf_vlog(
2572 vport, KERN_INFO, LOG_MBOX | LOG_SLI,
2573 "0010 UNREG_LOGIN vpi:%x "
2574 "rpi:%x DID:%x defer x%x flg x%x "
James Smart32350662019-08-14 16:57:06 -07002575 "map:%x %px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002576 vport->vpi, ndlp->nlp_rpi,
2577 ndlp->nlp_DID, ndlp->nlp_defer_did,
2578 ndlp->nlp_flag,
2579 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002580 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002581 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002582
2583 /* Check to see if there are any deferred
2584 * events to process
2585 */
2586 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2587 (ndlp->nlp_defer_did !=
2588 NLP_EVT_NOTHING_PENDING)) {
2589 lpfc_printf_vlog(
2590 vport, KERN_INFO, LOG_DISCOVERY,
2591 "4111 UNREG cmpl deferred "
2592 "clr x%x on "
James Smart32350662019-08-14 16:57:06 -07002593 "NPort x%x Data: x%x x%px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002594 ndlp->nlp_rpi, ndlp->nlp_DID,
2595 ndlp->nlp_defer_did, ndlp);
James Smart00292e02018-12-13 15:17:55 -08002596 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002597 ndlp->nlp_defer_did =
2598 NLP_EVT_NOTHING_PENDING;
2599 lpfc_issue_els_plogi(
2600 vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002601 } else {
James Smartb95b2112019-08-14 16:56:47 -07002602 __lpfc_sli_rpi_release(vport, ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002603 }
James Smartbe6bb942015-04-07 15:07:22 -04002604 }
2605 }
2606 }
2607
2608 mempool_free(pmb, phba->mbox_mem_pool);
2609}
dea31012005-04-17 16:05:31 -05002610
James Smarte59058c2008-08-24 21:49:00 -04002611/**
James Smart3621a712009-04-06 18:47:14 -04002612 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002613 * @phba: Pointer to HBA context object.
2614 *
2615 * This function is called with no lock held. This function processes all
2616 * the completed mailbox commands and gives it to upper layers. The interrupt
2617 * service routine processes mailbox completion interrupt and adds completed
2618 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2619 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2620 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2621 * function returns the mailbox commands to the upper layer by calling the
2622 * completion handler function of each mailbox.
2623 **/
dea31012005-04-17 16:05:31 -05002624int
James Smart2e0fef82007-06-17 19:56:36 -05002625lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002626{
James Smart92d7f7b2007-06-17 19:56:38 -05002627 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002628 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002629 int rc;
2630 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002631
2632 phba->sli.slistat.mbox_event++;
2633
James Smart92d7f7b2007-06-17 19:56:38 -05002634 /* Get all completed mailboxe buffers into the cmplq */
2635 spin_lock_irq(&phba->hbalock);
2636 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2637 spin_unlock_irq(&phba->hbalock);
2638
dea31012005-04-17 16:05:31 -05002639 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002640 do {
2641 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2642 if (pmb == NULL)
2643 break;
2644
James Smart04c68492009-05-22 14:52:52 -04002645 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002646
James Smart858c9f62007-06-17 19:56:39 -05002647 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2648 if (pmb->vport) {
2649 lpfc_debugfs_disc_trc(pmb->vport,
2650 LPFC_DISC_TRC_MBOX_VPORT,
2651 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2652 (uint32_t)pmbox->mbxCommand,
2653 pmbox->un.varWords[0],
2654 pmbox->un.varWords[1]);
2655 }
2656 else {
2657 lpfc_debugfs_disc_trc(phba->pport,
2658 LPFC_DISC_TRC_MBOX,
2659 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2660 (uint32_t)pmbox->mbxCommand,
2661 pmbox->un.varWords[0],
2662 pmbox->un.varWords[1]);
2663 }
2664 }
2665
dea31012005-04-17 16:05:31 -05002666 /*
2667 * It is a fatal error if unknown mbox command completion.
2668 */
2669 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2670 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002671 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002672 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002673 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002674 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002675 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002676 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002677 lpfc_sli_config_mbox_subsys_get(phba,
2678 pmb),
2679 lpfc_sli_config_mbox_opcode_get(phba,
2680 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002681 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002682 phba->work_hs = HS_FFER3;
2683 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002684 continue;
dea31012005-04-17 16:05:31 -05002685 }
2686
dea31012005-04-17 16:05:31 -05002687 if (pmbox->mbxStatus) {
2688 phba->sli.slistat.mbox_stat_err++;
2689 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2690 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002691 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002692 LOG_MBOX | LOG_SLI,
2693 "(%d):0305 Mbox cmd cmpl "
2694 "error - RETRYing Data: x%x "
2695 "(x%x/x%x) x%x x%x x%x\n",
2696 pmb->vport ? pmb->vport->vpi : 0,
2697 pmbox->mbxCommand,
2698 lpfc_sli_config_mbox_subsys_get(phba,
2699 pmb),
2700 lpfc_sli_config_mbox_opcode_get(phba,
2701 pmb),
2702 pmbox->mbxStatus,
2703 pmbox->un.varWords[0],
2704 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002705 pmbox->mbxStatus = 0;
2706 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002707 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002708 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002709 continue;
dea31012005-04-17 16:05:31 -05002710 }
2711 }
2712
2713 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002714 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart32350662019-08-14 16:57:06 -07002715 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl %pf "
James Smarte74c03c2013-04-17 20:15:19 -04002716 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2717 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002718 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002719 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002720 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2721 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002722 pmb->mbox_cmpl,
2723 *((uint32_t *) pmbox),
2724 pmbox->un.varWords[0],
2725 pmbox->un.varWords[1],
2726 pmbox->un.varWords[2],
2727 pmbox->un.varWords[3],
2728 pmbox->un.varWords[4],
2729 pmbox->un.varWords[5],
2730 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002731 pmbox->un.varWords[7],
2732 pmbox->un.varWords[8],
2733 pmbox->un.varWords[9],
2734 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002735
James Smart92d7f7b2007-06-17 19:56:38 -05002736 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002737 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002738 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002739 return 0;
dea31012005-04-17 16:05:31 -05002740}
James Smart92d7f7b2007-06-17 19:56:38 -05002741
James Smarte59058c2008-08-24 21:49:00 -04002742/**
James Smart3621a712009-04-06 18:47:14 -04002743 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002744 * @phba: Pointer to HBA context object.
2745 * @pring: Pointer to driver SLI ring object.
2746 * @tag: buffer tag.
2747 *
2748 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2749 * is set in the tag the buffer is posted for a particular exchange,
2750 * the function will return the buffer without replacing the buffer.
2751 * If the buffer is for unsolicited ELS or CT traffic, this function
2752 * returns the buffer and also posts another buffer to the firmware.
2753 **/
James Smart76bb24e2007-10-27 13:38:00 -04002754static struct lpfc_dmabuf *
2755lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002756 struct lpfc_sli_ring *pring,
2757 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002758{
James Smart9f1e1b52008-12-04 22:39:40 -05002759 struct hbq_dmabuf *hbq_entry;
2760
James Smart76bb24e2007-10-27 13:38:00 -04002761 if (tag & QUE_BUFTAG_BIT)
2762 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002763 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2764 if (!hbq_entry)
2765 return NULL;
2766 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002767}
James Smart57127f12007-10-27 13:37:05 -04002768
James Smart3772a992009-05-22 14:50:54 -04002769/**
2770 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2771 * @phba: Pointer to HBA context object.
2772 * @pring: Pointer to driver SLI ring object.
2773 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2774 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2775 * @fch_type: the type for the first frame of the sequence.
2776 *
2777 * This function is called with no lock held. This function uses the r_ctl and
2778 * type of the received sequence to find the correct callback function to call
2779 * to process the sequence.
2780 **/
2781static int
2782lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2783 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2784 uint32_t fch_type)
2785{
2786 int i;
2787
James Smartf358dd02017-02-12 13:52:34 -08002788 switch (fch_type) {
2789 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002790 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002791 return 1;
2792 default:
2793 break;
2794 }
2795
James Smart3772a992009-05-22 14:50:54 -04002796 /* unSolicited Responses */
2797 if (pring->prt[0].profile) {
2798 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2799 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2800 saveq);
2801 return 1;
2802 }
2803 /* We must search, based on rctl / type
2804 for the right routine */
2805 for (i = 0; i < pring->num_mask; i++) {
2806 if ((pring->prt[i].rctl == fch_r_ctl) &&
2807 (pring->prt[i].type == fch_type)) {
2808 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2809 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2810 (phba, pring, saveq);
2811 return 1;
2812 }
2813 }
2814 return 0;
2815}
James Smarte59058c2008-08-24 21:49:00 -04002816
2817/**
James Smart3621a712009-04-06 18:47:14 -04002818 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002819 * @phba: Pointer to HBA context object.
2820 * @pring: Pointer to driver SLI ring object.
2821 * @saveq: Pointer to the unsolicited iocb.
2822 *
2823 * This function is called with no lock held by the ring event handler
2824 * when there is an unsolicited iocb posted to the response ring by the
2825 * firmware. This function gets the buffer associated with the iocbs
2826 * and calls the event handler for the ring. This function handles both
2827 * qring buffers and hbq buffers.
2828 * When the function returns 1 the caller can free the iocb object otherwise
2829 * upper layer functions will free the iocb objects.
2830 **/
dea31012005-04-17 16:05:31 -05002831static int
2832lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2833 struct lpfc_iocbq *saveq)
2834{
2835 IOCB_t * irsp;
2836 WORD5 * w5p;
2837 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002838 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002839 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002840
dea31012005-04-17 16:05:31 -05002841 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002842
2843 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2844 if (pring->lpfc_sli_rcv_async_status)
2845 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2846 else
2847 lpfc_printf_log(phba,
2848 KERN_WARNING,
2849 LOG_SLI,
2850 "0316 Ring %d handler: unexpected "
2851 "ASYNC_STATUS iocb received evt_code "
2852 "0x%x\n",
2853 pring->ringno,
2854 irsp->un.asyncstat.evt_code);
2855 return 1;
2856 }
2857
James Smart3163f722008-02-08 18:50:25 -05002858 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2859 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2860 if (irsp->ulpBdeCount > 0) {
2861 dmzbuf = lpfc_sli_get_buff(phba, pring,
2862 irsp->un.ulpWord[3]);
2863 lpfc_in_buf_free(phba, dmzbuf);
2864 }
2865
2866 if (irsp->ulpBdeCount > 1) {
2867 dmzbuf = lpfc_sli_get_buff(phba, pring,
2868 irsp->unsli3.sli3Words[3]);
2869 lpfc_in_buf_free(phba, dmzbuf);
2870 }
2871
2872 if (irsp->ulpBdeCount > 2) {
2873 dmzbuf = lpfc_sli_get_buff(phba, pring,
2874 irsp->unsli3.sli3Words[7]);
2875 lpfc_in_buf_free(phba, dmzbuf);
2876 }
2877
2878 return 1;
2879 }
2880
James Smart92d7f7b2007-06-17 19:56:38 -05002881 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002882 if (irsp->ulpBdeCount != 0) {
2883 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002884 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002885 if (!saveq->context2)
2886 lpfc_printf_log(phba,
2887 KERN_ERR,
2888 LOG_SLI,
2889 "0341 Ring %d Cannot find buffer for "
2890 "an unsolicited iocb. tag 0x%x\n",
2891 pring->ringno,
2892 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002893 }
2894 if (irsp->ulpBdeCount == 2) {
2895 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002896 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002897 if (!saveq->context3)
2898 lpfc_printf_log(phba,
2899 KERN_ERR,
2900 LOG_SLI,
2901 "0342 Ring %d Cannot find buffer for an"
2902 " unsolicited iocb. tag 0x%x\n",
2903 pring->ringno,
2904 irsp->unsli3.sli3Words[7]);
2905 }
2906 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002907 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002908 if (irsp->ulpBdeCount != 0) {
2909 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2910 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002911 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002912 lpfc_printf_log(phba,
2913 KERN_ERR,
2914 LOG_SLI,
2915 "0343 Ring %d Cannot find "
2916 "buffer for an unsolicited iocb"
2917 ". tag 0x%x\n", pring->ringno,
2918 irsp->un.ulpWord[3]);
2919 }
2920 if (irsp->ulpBdeCount == 2) {
2921 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2922 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002923 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002924 lpfc_printf_log(phba,
2925 KERN_ERR,
2926 LOG_SLI,
2927 "0344 Ring %d Cannot find "
2928 "buffer for an unsolicited "
2929 "iocb. tag 0x%x\n",
2930 pring->ringno,
2931 irsp->unsli3.sli3Words[7]);
2932 }
2933 }
James Smart92d7f7b2007-06-17 19:56:38 -05002934 }
James Smart9c2face2008-01-11 01:53:18 -05002935 if (irsp->ulpBdeCount != 0 &&
2936 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2937 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2938 int found = 0;
2939
2940 /* search continue save q for same XRI */
2941 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002942 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2943 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002944 list_add_tail(&saveq->list, &iocbq->list);
2945 found = 1;
2946 break;
2947 }
2948 }
2949 if (!found)
2950 list_add_tail(&saveq->clist,
2951 &pring->iocb_continue_saveq);
2952 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2953 list_del_init(&iocbq->clist);
2954 saveq = iocbq;
2955 irsp = &(saveq->iocb);
2956 } else
2957 return 0;
2958 }
2959 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2960 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2961 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002962 Rctl = FC_RCTL_ELS_REQ;
2963 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002964 } else {
2965 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2966 Rctl = w5p->hcsw.Rctl;
2967 Type = w5p->hcsw.Type;
2968
2969 /* Firmware Workaround */
2970 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2971 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2972 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002973 Rctl = FC_RCTL_ELS_REQ;
2974 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002975 w5p->hcsw.Rctl = Rctl;
2976 w5p->hcsw.Type = Type;
2977 }
2978 }
James Smart92d7f7b2007-06-17 19:56:38 -05002979
James Smart3772a992009-05-22 14:50:54 -04002980 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002981 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002982 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002983 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002984 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002985
James Smart92d7f7b2007-06-17 19:56:38 -05002986 return 1;
dea31012005-04-17 16:05:31 -05002987}
2988
James Smarte59058c2008-08-24 21:49:00 -04002989/**
James Smart3621a712009-04-06 18:47:14 -04002990 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002991 * @phba: Pointer to HBA context object.
2992 * @pring: Pointer to driver SLI ring object.
2993 * @prspiocb: Pointer to response iocb object.
2994 *
2995 * This function looks up the iocb_lookup table to get the command iocb
2996 * corresponding to the given response iocb using the iotag of the
James Smarte2a8be52019-05-06 17:26:47 -07002997 * response iocb. The driver calls this function with the hbalock held
2998 * for SLI3 ports or the ring lock held for SLI4 ports.
James Smarte59058c2008-08-24 21:49:00 -04002999 * This function returns the command iocb object if it finds the command
3000 * iocb else returns NULL.
3001 **/
dea31012005-04-17 16:05:31 -05003002static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05003003lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
3004 struct lpfc_sli_ring *pring,
3005 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05003006{
dea31012005-04-17 16:05:31 -05003007 struct lpfc_iocbq *cmd_iocb = NULL;
3008 uint16_t iotag;
James Smarte2a8be52019-05-06 17:26:47 -07003009 spinlock_t *temp_lock = NULL;
3010 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05003011
James Smarte2a8be52019-05-06 17:26:47 -07003012 if (phba->sli_rev == LPFC_SLI_REV4)
3013 temp_lock = &pring->ring_lock;
3014 else
3015 temp_lock = &phba->hbalock;
3016
3017 spin_lock_irqsave(temp_lock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003018 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05003019
James Bottomley604a3e32005-10-29 10:28:33 -05003020 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3021 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003022 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07003023 /* remove from txcmpl queue list */
3024 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04003025 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003026 pring->txcmplq_cnt--;
James Smarte2a8be52019-05-06 17:26:47 -07003027 spin_unlock_irqrestore(temp_lock, iflag);
James Smart89533e92016-10-13 15:06:15 -07003028 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003029 }
dea31012005-04-17 16:05:31 -05003030 }
3031
James Smarte2a8be52019-05-06 17:26:47 -07003032 spin_unlock_irqrestore(temp_lock, iflag);
dea31012005-04-17 16:05:31 -05003033 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07003034 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05003035 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003036 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05003037 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05003038 return NULL;
3039}
3040
James Smarte59058c2008-08-24 21:49:00 -04003041/**
James Smart3772a992009-05-22 14:50:54 -04003042 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3043 * @phba: Pointer to HBA context object.
3044 * @pring: Pointer to driver SLI ring object.
3045 * @iotag: IOCB tag.
3046 *
3047 * This function looks up the iocb_lookup table to get the command iocb
James Smarte2a8be52019-05-06 17:26:47 -07003048 * corresponding to the given iotag. The driver calls this function with
3049 * the ring lock held because this function is an SLI4 port only helper.
James Smart3772a992009-05-22 14:50:54 -04003050 * This function returns the command iocb object if it finds the command
3051 * iocb else returns NULL.
3052 **/
3053static struct lpfc_iocbq *
3054lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3055 struct lpfc_sli_ring *pring, uint16_t iotag)
3056{
James Smart895427b2017-02-12 13:52:30 -08003057 struct lpfc_iocbq *cmd_iocb = NULL;
James Smarte2a8be52019-05-06 17:26:47 -07003058 spinlock_t *temp_lock = NULL;
3059 unsigned long iflag = 0;
James Smart3772a992009-05-22 14:50:54 -04003060
James Smarte2a8be52019-05-06 17:26:47 -07003061 if (phba->sli_rev == LPFC_SLI_REV4)
3062 temp_lock = &pring->ring_lock;
3063 else
3064 temp_lock = &phba->hbalock;
3065
3066 spin_lock_irqsave(temp_lock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003067 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3068 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003069 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3070 /* remove from txcmpl queue list */
3071 list_del_init(&cmd_iocb->list);
3072 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003073 pring->txcmplq_cnt--;
James Smarte2a8be52019-05-06 17:26:47 -07003074 spin_unlock_irqrestore(temp_lock, iflag);
James Smart4f2e66c2012-05-09 21:17:07 -04003075 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003076 }
James Smart3772a992009-05-22 14:50:54 -04003077 }
James Smart89533e92016-10-13 15:06:15 -07003078
James Smarte2a8be52019-05-06 17:26:47 -07003079 spin_unlock_irqrestore(temp_lock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003080 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08003081 "0372 iotag x%x lookup error: max iotag (x%x) "
3082 "iocb_flag x%x\n",
3083 iotag, phba->sli.last_iotag,
3084 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04003085 return NULL;
3086}
3087
3088/**
James Smart3621a712009-04-06 18:47:14 -04003089 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04003090 * @phba: Pointer to HBA context object.
3091 * @pring: Pointer to driver SLI ring object.
3092 * @saveq: Pointer to the response iocb to be processed.
3093 *
3094 * This function is called by the ring event handler for non-fcp
3095 * rings when there is a new response iocb in the response ring.
3096 * The caller is not required to hold any locks. This function
3097 * gets the command iocb associated with the response iocb and
3098 * calls the completion handler for the command iocb. If there
3099 * is no completion handler, the function will free the resources
3100 * associated with command iocb. If the response iocb is for
3101 * an already aborted command iocb, the status of the completion
3102 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3103 * This function always returns 1.
3104 **/
dea31012005-04-17 16:05:31 -05003105static int
James Smart2e0fef82007-06-17 19:56:36 -05003106lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003107 struct lpfc_iocbq *saveq)
3108{
James Smart2e0fef82007-06-17 19:56:36 -05003109 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05003110 int rc = 1;
3111 unsigned long iflag;
3112
James Bottomley604a3e32005-10-29 10:28:33 -05003113 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
dea31012005-04-17 16:05:31 -05003114 if (cmdiocbp) {
3115 if (cmdiocbp->iocb_cmpl) {
3116 /*
James Smartea2151b2008-09-07 11:52:10 -04003117 * If an ELS command failed send an event to mgmt
3118 * application.
3119 */
3120 if (saveq->iocb.ulpStatus &&
3121 (pring->ringno == LPFC_ELS_RING) &&
3122 (cmdiocbp->iocb.ulpCommand ==
3123 CMD_ELS_REQUEST64_CR))
3124 lpfc_send_els_failure_event(phba,
3125 cmdiocbp, saveq);
3126
3127 /*
dea31012005-04-17 16:05:31 -05003128 * Post all ELS completions to the worker thread.
3129 * All other are passed to the completion callback.
3130 */
3131 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05003132 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3133 (cmdiocbp->iocb_flag &
3134 LPFC_DRIVER_ABORTED)) {
3135 spin_lock_irqsave(&phba->hbalock,
3136 iflag);
James Smart07951072007-04-25 09:51:38 -04003137 cmdiocbp->iocb_flag &=
3138 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05003139 spin_unlock_irqrestore(&phba->hbalock,
3140 iflag);
James Smart07951072007-04-25 09:51:38 -04003141 saveq->iocb.ulpStatus =
3142 IOSTAT_LOCAL_REJECT;
3143 saveq->iocb.un.ulpWord[4] =
3144 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05003145
3146 /* Firmware could still be in progress
3147 * of DMAing payload, so don't free data
3148 * buffer till after a hbeat.
3149 */
James Smart341af102010-01-26 23:07:37 -05003150 spin_lock_irqsave(&phba->hbalock,
3151 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05003152 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05003153 spin_unlock_irqrestore(&phba->hbalock,
3154 iflag);
3155 }
James Smart0f65ff62010-02-26 14:14:23 -05003156 if (phba->sli_rev == LPFC_SLI_REV4) {
3157 if (saveq->iocb_flag &
3158 LPFC_EXCHANGE_BUSY) {
3159 /* Set cmdiocb flag for the
3160 * exchange busy so sgl (xri)
3161 * will not be released until
3162 * the abort xri is received
3163 * from hba.
3164 */
3165 spin_lock_irqsave(
3166 &phba->hbalock, iflag);
3167 cmdiocbp->iocb_flag |=
3168 LPFC_EXCHANGE_BUSY;
3169 spin_unlock_irqrestore(
3170 &phba->hbalock, iflag);
3171 }
3172 if (cmdiocbp->iocb_flag &
3173 LPFC_DRIVER_ABORTED) {
3174 /*
3175 * Clear LPFC_DRIVER_ABORTED
3176 * bit in case it was driver
3177 * initiated abort.
3178 */
3179 spin_lock_irqsave(
3180 &phba->hbalock, iflag);
3181 cmdiocbp->iocb_flag &=
3182 ~LPFC_DRIVER_ABORTED;
3183 spin_unlock_irqrestore(
3184 &phba->hbalock, iflag);
3185 cmdiocbp->iocb.ulpStatus =
3186 IOSTAT_LOCAL_REJECT;
3187 cmdiocbp->iocb.un.ulpWord[4] =
3188 IOERR_ABORT_REQUESTED;
3189 /*
3190 * For SLI4, irsiocb contains
3191 * NO_XRI in sli_xritag, it
3192 * shall not affect releasing
3193 * sgl (xri) process.
3194 */
3195 saveq->iocb.ulpStatus =
3196 IOSTAT_LOCAL_REJECT;
3197 saveq->iocb.un.ulpWord[4] =
3198 IOERR_SLI_ABORTED;
3199 spin_lock_irqsave(
3200 &phba->hbalock, iflag);
3201 saveq->iocb_flag |=
3202 LPFC_DELAY_MEM_FREE;
3203 spin_unlock_irqrestore(
3204 &phba->hbalock, iflag);
3205 }
James Smart07951072007-04-25 09:51:38 -04003206 }
dea31012005-04-17 16:05:31 -05003207 }
James Smart2e0fef82007-06-17 19:56:36 -05003208 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003209 } else
3210 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003211 } else {
3212 /*
3213 * Unknown initiating command based on the response iotag.
3214 * This could be the case on the ELS ring because of
3215 * lpfc_els_abort().
3216 */
3217 if (pring->ringno != LPFC_ELS_RING) {
3218 /*
3219 * Ring <ringno> handler: unexpected completion IoTag
3220 * <IoTag>
3221 */
James Smarta257bf92009-04-06 18:48:10 -04003222 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003223 "0322 Ring %d handler: "
3224 "unexpected completion IoTag x%x "
3225 "Data: x%x x%x x%x x%x\n",
3226 pring->ringno,
3227 saveq->iocb.ulpIoTag,
3228 saveq->iocb.ulpStatus,
3229 saveq->iocb.un.ulpWord[4],
3230 saveq->iocb.ulpCommand,
3231 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003232 }
3233 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003234
dea31012005-04-17 16:05:31 -05003235 return rc;
3236}
3237
James Smarte59058c2008-08-24 21:49:00 -04003238/**
James Smart3621a712009-04-06 18:47:14 -04003239 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003240 * @phba: Pointer to HBA context object.
3241 * @pring: Pointer to driver SLI ring object.
3242 *
3243 * This function is called from the iocb ring event handlers when
3244 * put pointer is ahead of the get pointer for a ring. This function signal
3245 * an error attention condition to the worker thread and the worker
3246 * thread will transition the HBA to offline state.
3247 **/
James Smart2e0fef82007-06-17 19:56:36 -05003248static void
3249lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003250{
James Smart34b02dc2008-08-24 21:49:55 -04003251 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003252 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003253 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003254 * rsp ring <portRspMax>
3255 */
3256 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003257 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003258 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003259 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003260 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003261
James Smart2e0fef82007-06-17 19:56:36 -05003262 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003263
3264 /*
3265 * All error attention handlers are posted to
3266 * worker thread
3267 */
3268 phba->work_ha |= HA_ERATT;
3269 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003270
James Smart5e9d9b82008-06-14 22:52:53 -04003271 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003272
3273 return;
3274}
3275
James Smarte59058c2008-08-24 21:49:00 -04003276/**
James Smart3621a712009-04-06 18:47:14 -04003277 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04003278 * @ptr: Pointer to address of HBA context object.
3279 *
3280 * This function is invoked by the Error Attention polling timer when the
3281 * timer times out. It will check the SLI Error Attention register for
3282 * possible attention events. If so, it will post an Error Attention event
3283 * and wake up worker thread to process it. Otherwise, it will set up the
3284 * Error Attention polling timer for the next poll.
3285 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003286void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003287{
3288 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003289 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003290 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003291
Kees Cookf22eb4d2017-09-06 20:24:26 -07003292 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003293
James Smartaa6fbb72012-08-03 12:36:03 -04003294 /* Here we will also keep track of interrupts per sec of the hba */
3295 sli_intr = phba->sli.slistat.sli_intr;
3296
3297 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3298 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3299 sli_intr);
3300 else
3301 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3302
James Smart65791f12016-07-06 12:35:56 -07003303 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3304 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003305 phba->sli.slistat.sli_ips = cnt;
3306
3307 phba->sli.slistat.sli_prev_intr = sli_intr;
3308
James Smart93996272008-08-24 21:50:30 -04003309 /* Check chip HA register for error event */
3310 eratt = lpfc_sli_check_eratt(phba);
3311
3312 if (eratt)
3313 /* Tell the worker thread there is work to do */
3314 lpfc_worker_wake_up(phba);
3315 else
3316 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003317 mod_timer(&phba->eratt_poll,
3318 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003319 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003320 return;
3321}
3322
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003323
James Smarte59058c2008-08-24 21:49:00 -04003324/**
James Smart3621a712009-04-06 18:47:14 -04003325 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003326 * @phba: Pointer to HBA context object.
3327 * @pring: Pointer to driver SLI ring object.
3328 * @mask: Host attention register mask for this ring.
3329 *
3330 * This function is called from the interrupt context when there is a ring
3331 * event for the fcp ring. The caller does not hold any lock.
3332 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003333 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003334 * LE bit set. The function will call the completion handler of the command iocb
3335 * if the response iocb indicates a completion for a command iocb or it is
3336 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3337 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003338 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003339 * to check it explicitly.
3340 */
3341int
James Smart2e0fef82007-06-17 19:56:36 -05003342lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3343 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003344{
James Smart34b02dc2008-08-24 21:49:55 -04003345 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003346 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003347 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003348 struct lpfc_iocbq *cmdiocbq = NULL;
3349 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003350 uint32_t status;
3351 uint32_t portRspPut, portRspMax;
3352 int rc = 1;
3353 lpfc_iocb_type type;
3354 unsigned long iflag;
3355 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003356
James Smart2e0fef82007-06-17 19:56:36 -05003357 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003358 pring->stats.iocb_event++;
3359
dea31012005-04-17 16:05:31 -05003360 /*
3361 * The next available response entry should never exceed the maximum
3362 * entries. If it does, treat it as an adapter hardware error.
3363 */
James Smart7e56aa22012-08-03 12:35:34 -04003364 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003365 portRspPut = le32_to_cpu(pgp->rspPutInx);
3366 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003367 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003368 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003369 return 1;
3370 }
James Smart45ed1192009-10-02 15:17:02 -04003371 if (phba->fcp_ring_in_use) {
3372 spin_unlock_irqrestore(&phba->hbalock, iflag);
3373 return 1;
3374 } else
3375 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003376
3377 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003378 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003379 /*
3380 * Fetch an entry off the ring and copy it into a local data
3381 * structure. The copy involves a byte-swap since the
3382 * network byte order and pci byte orders are different.
3383 */
James Smarted957682007-06-17 19:56:37 -05003384 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003385 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003386
James Smart7e56aa22012-08-03 12:35:34 -04003387 if (++pring->sli.sli3.rspidx >= portRspMax)
3388 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003389
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003390 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3391 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003392 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003393 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003394 irsp = &rspiocbq.iocb;
3395
dea31012005-04-17 16:05:31 -05003396 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3397 pring->stats.iocb_rsp++;
3398 rsp_cmpl++;
3399
3400 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003401 /*
3402 * If resource errors reported from HBA, reduce
3403 * queuedepths of the SCSI device.
3404 */
3405 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003406 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3407 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003408 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003409 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003410 spin_lock_irqsave(&phba->hbalock, iflag);
3411 }
3412
dea31012005-04-17 16:05:31 -05003413 /* Rsp ring <ringno> error: IOCB */
3414 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003415 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003416 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003417 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003418 irsp->un.ulpWord[0],
3419 irsp->un.ulpWord[1],
3420 irsp->un.ulpWord[2],
3421 irsp->un.ulpWord[3],
3422 irsp->un.ulpWord[4],
3423 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003424 *(uint32_t *)&irsp->un1,
3425 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003426 }
3427
3428 switch (type) {
3429 case LPFC_ABORT_IOCB:
3430 case LPFC_SOL_IOCB:
3431 /*
3432 * Idle exchange closed via ABTS from port. No iocb
3433 * resources need to be recovered.
3434 */
3435 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003436 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003437 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003438 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003439 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003440 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003441 break;
3442 }
3443
James Smarte2a8be52019-05-06 17:26:47 -07003444 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003445 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3446 &rspiocbq);
James Smarte2a8be52019-05-06 17:26:47 -07003447 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05003448 if (unlikely(!cmdiocbq))
3449 break;
3450 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3451 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3452 if (cmdiocbq->iocb_cmpl) {
3453 spin_unlock_irqrestore(&phba->hbalock, iflag);
3454 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3455 &rspiocbq);
3456 spin_lock_irqsave(&phba->hbalock, iflag);
3457 }
dea31012005-04-17 16:05:31 -05003458 break;
James Smarta4bc3372006-12-02 13:34:16 -05003459 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003460 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003461 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003462 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003463 break;
dea31012005-04-17 16:05:31 -05003464 default:
3465 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3466 char adaptermsg[LPFC_MAX_ADPTMSG];
3467 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3468 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3469 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003470 dev_warn(&((phba->pcidev)->dev),
3471 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003472 phba->brd_no, adaptermsg);
3473 } else {
3474 /* Unknown IOCB command */
3475 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003476 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003477 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003478 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003479 irsp->ulpStatus,
3480 irsp->ulpIoTag,
3481 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003482 }
3483 break;
3484 }
3485
3486 /*
3487 * The response IOCB has been processed. Update the ring
3488 * pointer in SLIM. If the port response put pointer has not
3489 * been updated, sync the pgp->rspPutInx and fetch the new port
3490 * response put pointer.
3491 */
James Smart7e56aa22012-08-03 12:35:34 -04003492 writel(pring->sli.sli3.rspidx,
3493 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003494
James Smart7e56aa22012-08-03 12:35:34 -04003495 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003496 portRspPut = le32_to_cpu(pgp->rspPutInx);
3497 }
3498
3499 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3500 pring->stats.iocb_rsp_full++;
3501 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3502 writel(status, phba->CAregaddr);
3503 readl(phba->CAregaddr);
3504 }
3505 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3506 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3507 pring->stats.iocb_cmd_empty++;
3508
3509 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003510 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003511 lpfc_sli_resume_iocb(phba, pring);
3512
3513 if ((pring->lpfc_sli_cmd_available))
3514 (pring->lpfc_sli_cmd_available) (phba, pring);
3515
3516 }
3517
James Smart45ed1192009-10-02 15:17:02 -04003518 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003519 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003520 return rc;
3521}
3522
James Smarte59058c2008-08-24 21:49:00 -04003523/**
James Smart3772a992009-05-22 14:50:54 -04003524 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3525 * @phba: Pointer to HBA context object.
3526 * @pring: Pointer to driver SLI ring object.
3527 * @rspiocbp: Pointer to driver response IOCB object.
3528 *
3529 * This function is called from the worker thread when there is a slow-path
3530 * response IOCB to process. This function chains all the response iocbs until
3531 * seeing the iocb with the LE bit set. The function will call
3532 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3533 * completion of a command iocb. The function will call the
3534 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3535 * The function frees the resources or calls the completion handler if this
3536 * iocb is an abort completion. The function returns NULL when the response
3537 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3538 * this function shall chain the iocb on to the iocb_continueq and return the
3539 * response iocb passed in.
3540 **/
3541static struct lpfc_iocbq *
3542lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3543 struct lpfc_iocbq *rspiocbp)
3544{
3545 struct lpfc_iocbq *saveq;
3546 struct lpfc_iocbq *cmdiocbp;
3547 struct lpfc_iocbq *next_iocb;
3548 IOCB_t *irsp = NULL;
3549 uint32_t free_saveq;
3550 uint8_t iocb_cmd_type;
3551 lpfc_iocb_type type;
3552 unsigned long iflag;
3553 int rc;
3554
3555 spin_lock_irqsave(&phba->hbalock, iflag);
3556 /* First add the response iocb to the countinueq list */
3557 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3558 pring->iocb_continueq_cnt++;
3559
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003560 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003561 irsp = &rspiocbp->iocb;
3562 if (irsp->ulpLe) {
3563 /*
3564 * By default, the driver expects to free all resources
3565 * associated with this iocb completion.
3566 */
3567 free_saveq = 1;
3568 saveq = list_get_first(&pring->iocb_continueq,
3569 struct lpfc_iocbq, list);
3570 irsp = &(saveq->iocb);
3571 list_del_init(&pring->iocb_continueq);
3572 pring->iocb_continueq_cnt = 0;
3573
3574 pring->stats.iocb_rsp++;
3575
3576 /*
3577 * If resource errors reported from HBA, reduce
3578 * queuedepths of the SCSI device.
3579 */
3580 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003581 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3582 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003583 spin_unlock_irqrestore(&phba->hbalock, iflag);
3584 phba->lpfc_rampdown_queue_depth(phba);
3585 spin_lock_irqsave(&phba->hbalock, iflag);
3586 }
3587
3588 if (irsp->ulpStatus) {
3589 /* Rsp ring <ringno> error: IOCB */
3590 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3591 "0328 Rsp Ring %d error: "
3592 "IOCB Data: "
3593 "x%x x%x x%x x%x "
3594 "x%x x%x x%x x%x "
3595 "x%x x%x x%x x%x "
3596 "x%x x%x x%x x%x\n",
3597 pring->ringno,
3598 irsp->un.ulpWord[0],
3599 irsp->un.ulpWord[1],
3600 irsp->un.ulpWord[2],
3601 irsp->un.ulpWord[3],
3602 irsp->un.ulpWord[4],
3603 irsp->un.ulpWord[5],
3604 *(((uint32_t *) irsp) + 6),
3605 *(((uint32_t *) irsp) + 7),
3606 *(((uint32_t *) irsp) + 8),
3607 *(((uint32_t *) irsp) + 9),
3608 *(((uint32_t *) irsp) + 10),
3609 *(((uint32_t *) irsp) + 11),
3610 *(((uint32_t *) irsp) + 12),
3611 *(((uint32_t *) irsp) + 13),
3612 *(((uint32_t *) irsp) + 14),
3613 *(((uint32_t *) irsp) + 15));
3614 }
3615
3616 /*
3617 * Fetch the IOCB command type and call the correct completion
3618 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3619 * get freed back to the lpfc_iocb_list by the discovery
3620 * kernel thread.
3621 */
3622 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3623 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3624 switch (type) {
3625 case LPFC_SOL_IOCB:
3626 spin_unlock_irqrestore(&phba->hbalock, iflag);
3627 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3628 spin_lock_irqsave(&phba->hbalock, iflag);
3629 break;
3630
3631 case LPFC_UNSOL_IOCB:
3632 spin_unlock_irqrestore(&phba->hbalock, iflag);
3633 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3634 spin_lock_irqsave(&phba->hbalock, iflag);
3635 if (!rc)
3636 free_saveq = 0;
3637 break;
3638
3639 case LPFC_ABORT_IOCB:
3640 cmdiocbp = NULL;
James Smarte2a8be52019-05-06 17:26:47 -07003641 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX) {
3642 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003643 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3644 saveq);
James Smarte2a8be52019-05-06 17:26:47 -07003645 spin_lock_irqsave(&phba->hbalock, iflag);
3646 }
James Smart3772a992009-05-22 14:50:54 -04003647 if (cmdiocbp) {
3648 /* Call the specified completion routine */
3649 if (cmdiocbp->iocb_cmpl) {
3650 spin_unlock_irqrestore(&phba->hbalock,
3651 iflag);
3652 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3653 saveq);
3654 spin_lock_irqsave(&phba->hbalock,
3655 iflag);
3656 } else
3657 __lpfc_sli_release_iocbq(phba,
3658 cmdiocbp);
3659 }
3660 break;
3661
3662 case LPFC_UNKNOWN_IOCB:
3663 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3664 char adaptermsg[LPFC_MAX_ADPTMSG];
3665 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3666 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3667 MAX_MSG_DATA);
3668 dev_warn(&((phba->pcidev)->dev),
3669 "lpfc%d: %s\n",
3670 phba->brd_no, adaptermsg);
3671 } else {
3672 /* Unknown IOCB command */
3673 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3674 "0335 Unknown IOCB "
3675 "command Data: x%x "
3676 "x%x x%x x%x\n",
3677 irsp->ulpCommand,
3678 irsp->ulpStatus,
3679 irsp->ulpIoTag,
3680 irsp->ulpContext);
3681 }
3682 break;
3683 }
3684
3685 if (free_saveq) {
3686 list_for_each_entry_safe(rspiocbp, next_iocb,
3687 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003688 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003689 __lpfc_sli_release_iocbq(phba, rspiocbp);
3690 }
3691 __lpfc_sli_release_iocbq(phba, saveq);
3692 }
3693 rspiocbp = NULL;
3694 }
3695 spin_unlock_irqrestore(&phba->hbalock, iflag);
3696 return rspiocbp;
3697}
3698
3699/**
3700 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003701 * @phba: Pointer to HBA context object.
3702 * @pring: Pointer to driver SLI ring object.
3703 * @mask: Host attention register mask for this ring.
3704 *
James Smart3772a992009-05-22 14:50:54 -04003705 * This routine wraps the actual slow_ring event process routine from the
3706 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003707 **/
James Smart3772a992009-05-22 14:50:54 -04003708void
James Smart2e0fef82007-06-17 19:56:36 -05003709lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3710 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003711{
James Smart3772a992009-05-22 14:50:54 -04003712 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3713}
3714
3715/**
3716 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3717 * @phba: Pointer to HBA context object.
3718 * @pring: Pointer to driver SLI ring object.
3719 * @mask: Host attention register mask for this ring.
3720 *
3721 * This function is called from the worker thread when there is a ring event
3722 * for non-fcp rings. The caller does not hold any lock. The function will
3723 * remove each response iocb in the response ring and calls the handle
3724 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3725 **/
3726static void
3727lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3728 struct lpfc_sli_ring *pring, uint32_t mask)
3729{
James Smart34b02dc2008-08-24 21:49:55 -04003730 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003731 IOCB_t *entry;
3732 IOCB_t *irsp = NULL;
3733 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003734 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003735 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003736 uint32_t status;
dea31012005-04-17 16:05:31 -05003737
James Smart34b02dc2008-08-24 21:49:55 -04003738 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003739 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003740 pring->stats.iocb_event++;
3741
dea31012005-04-17 16:05:31 -05003742 /*
3743 * The next available response entry should never exceed the maximum
3744 * entries. If it does, treat it as an adapter hardware error.
3745 */
James Smart7e56aa22012-08-03 12:35:34 -04003746 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003747 portRspPut = le32_to_cpu(pgp->rspPutInx);
3748 if (portRspPut >= portRspMax) {
3749 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003750 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003751 * rsp ring <portRspMax>
3752 */
James Smarted957682007-06-17 19:56:37 -05003753 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003754 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003755 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003756 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003757
James Smart2e0fef82007-06-17 19:56:36 -05003758 phba->link_state = LPFC_HBA_ERROR;
3759 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003760
3761 phba->work_hs = HS_FFER3;
3762 lpfc_handle_eratt(phba);
3763
James Smart3772a992009-05-22 14:50:54 -04003764 return;
dea31012005-04-17 16:05:31 -05003765 }
3766
3767 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003768 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003769 /*
3770 * Build a completion list and call the appropriate handler.
3771 * The process is to get the next available response iocb, get
3772 * a free iocb from the list, copy the response data into the
3773 * free iocb, insert to the continuation list, and update the
3774 * next response index to slim. This process makes response
3775 * iocb's in the ring available to DMA as fast as possible but
3776 * pays a penalty for a copy operation. Since the iocb is
3777 * only 32 bytes, this penalty is considered small relative to
3778 * the PCI reads for register values and a slim write. When
3779 * the ulpLe field is set, the entire Command has been
3780 * received.
3781 */
James Smarted957682007-06-17 19:56:37 -05003782 entry = lpfc_resp_iocb(phba, pring);
3783
James Smart858c9f62007-06-17 19:56:39 -05003784 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003785 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003786 if (rspiocbp == NULL) {
3787 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003788 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003789 break;
3790 }
3791
James Smarted957682007-06-17 19:56:37 -05003792 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3793 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003794 irsp = &rspiocbp->iocb;
3795
James Smart7e56aa22012-08-03 12:35:34 -04003796 if (++pring->sli.sli3.rspidx >= portRspMax)
3797 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003798
James Smarta58cbd52007-08-02 11:09:43 -04003799 if (pring->ringno == LPFC_ELS_RING) {
3800 lpfc_debugfs_slow_ring_trc(phba,
3801 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3802 *(((uint32_t *) irsp) + 4),
3803 *(((uint32_t *) irsp) + 6),
3804 *(((uint32_t *) irsp) + 7));
3805 }
3806
James Smart7e56aa22012-08-03 12:35:34 -04003807 writel(pring->sli.sli3.rspidx,
3808 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003809
James Smart3772a992009-05-22 14:50:54 -04003810 spin_unlock_irqrestore(&phba->hbalock, iflag);
3811 /* Handle the response IOCB */
3812 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3813 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003814
3815 /*
3816 * If the port response put pointer has not been updated, sync
3817 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3818 * response put pointer.
3819 */
James Smart7e56aa22012-08-03 12:35:34 -04003820 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003821 portRspPut = le32_to_cpu(pgp->rspPutInx);
3822 }
James Smart7e56aa22012-08-03 12:35:34 -04003823 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003824
James Smart92d7f7b2007-06-17 19:56:38 -05003825 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003826 /* At least one response entry has been freed */
3827 pring->stats.iocb_rsp_full++;
3828 /* SET RxRE_RSP in Chip Att register */
3829 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3830 writel(status, phba->CAregaddr);
3831 readl(phba->CAregaddr); /* flush */
3832 }
3833 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3834 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3835 pring->stats.iocb_cmd_empty++;
3836
3837 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003838 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003839 lpfc_sli_resume_iocb(phba, pring);
3840
3841 if ((pring->lpfc_sli_cmd_available))
3842 (pring->lpfc_sli_cmd_available) (phba, pring);
3843
3844 }
3845
James Smart2e0fef82007-06-17 19:56:36 -05003846 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003847 return;
dea31012005-04-17 16:05:31 -05003848}
3849
James Smarte59058c2008-08-24 21:49:00 -04003850/**
James Smart4f774512009-05-22 14:52:35 -04003851 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3852 * @phba: Pointer to HBA context object.
3853 * @pring: Pointer to driver SLI ring object.
3854 * @mask: Host attention register mask for this ring.
3855 *
3856 * This function is called from the worker thread when there is a pending
3857 * ELS response iocb on the driver internal slow-path response iocb worker
3858 * queue. The caller does not hold any lock. The function will remove each
3859 * response iocb from the response worker queue and calls the handle
3860 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3861 **/
3862static void
3863lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3864 struct lpfc_sli_ring *pring, uint32_t mask)
3865{
3866 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003867 struct hbq_dmabuf *dmabuf;
3868 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003869 unsigned long iflag;
James Smart0ef01a22018-09-10 10:30:45 -07003870 int count = 0;
James Smart4f774512009-05-22 14:52:35 -04003871
James Smart45ed1192009-10-02 15:17:02 -04003872 spin_lock_irqsave(&phba->hbalock, iflag);
3873 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3874 spin_unlock_irqrestore(&phba->hbalock, iflag);
3875 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003876 /* Get the response iocb from the head of work queue */
3877 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003878 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003879 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003880 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003881
3882 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3883 case CQE_CODE_COMPL_WQE:
3884 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3885 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003886 /* Translate ELS WCQE to response IOCBQ */
3887 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3888 irspiocbq);
3889 if (irspiocbq)
3890 lpfc_sli_sp_handle_rspiocb(phba, pring,
3891 irspiocbq);
James Smart0ef01a22018-09-10 10:30:45 -07003892 count++;
James Smart4d9ab992009-10-02 15:16:39 -04003893 break;
3894 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003895 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003896 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3897 cq_event);
3898 lpfc_sli4_handle_received_buffer(phba, dmabuf);
James Smart0ef01a22018-09-10 10:30:45 -07003899 count++;
James Smart4d9ab992009-10-02 15:16:39 -04003900 break;
3901 default:
3902 break;
3903 }
James Smart0ef01a22018-09-10 10:30:45 -07003904
3905 /* Limit the number of events to 64 to avoid soft lockups */
3906 if (count == 64)
3907 break;
James Smart4f774512009-05-22 14:52:35 -04003908 }
3909}
3910
3911/**
James Smart3621a712009-04-06 18:47:14 -04003912 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003913 * @phba: Pointer to HBA context object.
3914 * @pring: Pointer to driver SLI ring object.
3915 *
3916 * This function aborts all iocbs in the given ring and frees all the iocb
3917 * objects in txq. This function issues an abort iocb for all the iocb commands
3918 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3919 * the return of this function. The caller is not required to hold any locks.
3920 **/
James Smart2e0fef82007-06-17 19:56:36 -05003921void
dea31012005-04-17 16:05:31 -05003922lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3923{
James Smart2534ba72007-04-25 09:52:20 -04003924 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003925 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003926
James Smart92d7f7b2007-06-17 19:56:38 -05003927 if (pring->ringno == LPFC_ELS_RING) {
3928 lpfc_fabric_abort_hba(phba);
3929 }
3930
dea31012005-04-17 16:05:31 -05003931 /* Error everything on txq and txcmplq
3932 * First do the txq.
3933 */
James Smartdb55fba2014-04-04 13:52:02 -04003934 if (phba->sli_rev >= LPFC_SLI_REV4) {
3935 spin_lock_irq(&pring->ring_lock);
3936 list_splice_init(&pring->txq, &completions);
3937 pring->txq_cnt = 0;
3938 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003939
James Smartdb55fba2014-04-04 13:52:02 -04003940 spin_lock_irq(&phba->hbalock);
3941 /* Next issue ABTS for everything on the txcmplq */
3942 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3943 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3944 spin_unlock_irq(&phba->hbalock);
3945 } else {
3946 spin_lock_irq(&phba->hbalock);
3947 list_splice_init(&pring->txq, &completions);
3948 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003949
James Smartdb55fba2014-04-04 13:52:02 -04003950 /* Next issue ABTS for everything on the txcmplq */
3951 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3952 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3953 spin_unlock_irq(&phba->hbalock);
3954 }
James Smart2534ba72007-04-25 09:52:20 -04003955
James Smarta257bf92009-04-06 18:48:10 -04003956 /* Cancel all the IOCBs from the completions list */
3957 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3958 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003959}
3960
James Smarte59058c2008-08-24 21:49:00 -04003961/**
James Smartdb55fba2014-04-04 13:52:02 -04003962 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3963 * @phba: Pointer to HBA context object.
3964 * @pring: Pointer to driver SLI ring object.
3965 *
3966 * This function aborts all iocbs in FCP rings and frees all the iocb
3967 * objects in txq. This function issues an abort iocb for all the iocb commands
3968 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3969 * the return of this function. The caller is not required to hold any locks.
3970 **/
3971void
3972lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3973{
3974 struct lpfc_sli *psli = &phba->sli;
3975 struct lpfc_sli_ring *pring;
3976 uint32_t i;
3977
3978 /* Look on all the FCP Rings for the iotag */
3979 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08003980 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -07003981 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003982 lpfc_sli_abort_iocb_ring(phba, pring);
3983 }
3984 } else {
James Smart895427b2017-02-12 13:52:30 -08003985 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003986 lpfc_sli_abort_iocb_ring(phba, pring);
3987 }
3988}
3989
James Smart895427b2017-02-12 13:52:30 -08003990/**
James Smartc00f62e2019-08-14 16:57:11 -07003991 * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring
James Smarta8e497d2008-08-24 21:50:11 -04003992 * @phba: Pointer to HBA context object.
3993 *
James Smartc00f62e2019-08-14 16:57:11 -07003994 * This function flushes all iocbs in the IO ring and frees all the iocb
James Smarta8e497d2008-08-24 21:50:11 -04003995 * objects in txq and txcmplq. This function will not issue abort iocbs
3996 * for all the iocb commands in txcmplq, they will just be returned with
3997 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3998 * slot has been permanently disabled.
3999 **/
4000void
James Smartc00f62e2019-08-14 16:57:11 -07004001lpfc_sli_flush_io_rings(struct lpfc_hba *phba)
James Smarta8e497d2008-08-24 21:50:11 -04004002{
4003 LIST_HEAD(txq);
4004 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04004005 struct lpfc_sli *psli = &phba->sli;
4006 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04004007 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004008 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04004009
4010 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04004011 /* Indicate the I/O queues are flushed */
James Smartc00f62e2019-08-14 16:57:11 -07004012 phba->hba_flag |= HBA_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04004013 spin_unlock_irq(&phba->hbalock);
4014
James Smartdb55fba2014-04-04 13:52:02 -04004015 /* Look on all the FCP Rings for the iotag */
4016 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004017 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -07004018 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smarta8e497d2008-08-24 21:50:11 -04004019
James Smartdb55fba2014-04-04 13:52:02 -04004020 spin_lock_irq(&pring->ring_lock);
4021 /* Retrieve everything on txq */
4022 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004023 list_for_each_entry_safe(piocb, next_iocb,
4024 &pring->txcmplq, list)
4025 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004026 /* Retrieve everything on the txcmplq */
4027 list_splice_init(&pring->txcmplq, &txcmplq);
4028 pring->txq_cnt = 0;
4029 pring->txcmplq_cnt = 0;
4030 spin_unlock_irq(&pring->ring_lock);
4031
4032 /* Flush the txq */
4033 lpfc_sli_cancel_iocbs(phba, &txq,
4034 IOSTAT_LOCAL_REJECT,
4035 IOERR_SLI_DOWN);
4036 /* Flush the txcmpq */
4037 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4038 IOSTAT_LOCAL_REJECT,
4039 IOERR_SLI_DOWN);
4040 }
4041 } else {
James Smart895427b2017-02-12 13:52:30 -08004042 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004043
4044 spin_lock_irq(&phba->hbalock);
4045 /* Retrieve everything on txq */
4046 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004047 list_for_each_entry_safe(piocb, next_iocb,
4048 &pring->txcmplq, list)
4049 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004050 /* Retrieve everything on the txcmplq */
4051 list_splice_init(&pring->txcmplq, &txcmplq);
4052 pring->txq_cnt = 0;
4053 pring->txcmplq_cnt = 0;
4054 spin_unlock_irq(&phba->hbalock);
4055
4056 /* Flush the txq */
4057 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4058 IOERR_SLI_DOWN);
4059 /* Flush the txcmpq */
4060 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4061 IOERR_SLI_DOWN);
4062 }
James Smarta8e497d2008-08-24 21:50:11 -04004063}
4064
4065/**
James Smart3772a992009-05-22 14:50:54 -04004066 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004067 * @phba: Pointer to HBA context object.
4068 * @mask: Bit mask to be checked.
4069 *
4070 * This function reads the host status register and compares
4071 * with the provided bit mask to check if HBA completed
4072 * the restart. This function will wait in a loop for the
4073 * HBA to complete restart. If the HBA does not restart within
4074 * 15 iterations, the function will reset the HBA again. The
4075 * function returns 1 when HBA fail to restart otherwise returns
4076 * zero.
4077 **/
James Smart3772a992009-05-22 14:50:54 -04004078static int
4079lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004080{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004081 uint32_t status;
4082 int i = 0;
4083 int retval = 0;
dea31012005-04-17 16:05:31 -05004084
Jamie Wellnitz41415862006-02-28 19:25:27 -05004085 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004086 if (lpfc_readl(phba->HSregaddr, &status))
4087 return 1;
dea31012005-04-17 16:05:31 -05004088
Jamie Wellnitz41415862006-02-28 19:25:27 -05004089 /*
4090 * Check status register every 100ms for 5 retries, then every
4091 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4092 * every 2.5 sec for 4.
4093 * Break our of the loop if errors occurred during init.
4094 */
4095 while (((status & mask) != mask) &&
4096 !(status & HS_FFERM) &&
4097 i++ < 20) {
dea31012005-04-17 16:05:31 -05004098
Jamie Wellnitz41415862006-02-28 19:25:27 -05004099 if (i <= 5)
4100 msleep(10);
4101 else if (i <= 10)
4102 msleep(500);
4103 else
4104 msleep(2500);
dea31012005-04-17 16:05:31 -05004105
Jamie Wellnitz41415862006-02-28 19:25:27 -05004106 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004107 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004108 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004109 lpfc_sli_brdrestart(phba);
4110 }
4111 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004112 if (lpfc_readl(phba->HSregaddr, &status)) {
4113 retval = 1;
4114 break;
4115 }
dea31012005-04-17 16:05:31 -05004116 }
dea31012005-04-17 16:05:31 -05004117
Jamie Wellnitz41415862006-02-28 19:25:27 -05004118 /* Check to see if any errors occurred during init */
4119 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05004120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4121 "2751 Adapter failed to restart, "
4122 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4123 status,
4124 readl(phba->MBslimaddr + 0xa8),
4125 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004126 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004127 retval = 1;
4128 }
dea31012005-04-17 16:05:31 -05004129
Jamie Wellnitz41415862006-02-28 19:25:27 -05004130 return retval;
dea31012005-04-17 16:05:31 -05004131}
4132
James Smartda0436e2009-05-22 14:51:39 -04004133/**
4134 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4135 * @phba: Pointer to HBA context object.
4136 * @mask: Bit mask to be checked.
4137 *
4138 * This function checks the host status register to check if HBA is
4139 * ready. This function will wait in a loop for the HBA to be ready
4140 * If the HBA is not ready , the function will will reset the HBA PCI
4141 * function again. The function returns 1 when HBA fail to be ready
4142 * otherwise returns zero.
4143 **/
4144static int
4145lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4146{
4147 uint32_t status;
4148 int retval = 0;
4149
4150 /* Read the HBA Host Status Register */
4151 status = lpfc_sli4_post_status_check(phba);
4152
4153 if (status) {
4154 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4155 lpfc_sli_brdrestart(phba);
4156 status = lpfc_sli4_post_status_check(phba);
4157 }
4158
4159 /* Check to see if any errors occurred during init */
4160 if (status) {
4161 phba->link_state = LPFC_HBA_ERROR;
4162 retval = 1;
4163 } else
4164 phba->sli4_hba.intr_enable = 0;
4165
4166 return retval;
4167}
4168
4169/**
4170 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4171 * @phba: Pointer to HBA context object.
4172 * @mask: Bit mask to be checked.
4173 *
4174 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4175 * from the API jump table function pointer from the lpfc_hba struct.
4176 **/
4177int
4178lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4179{
4180 return phba->lpfc_sli_brdready(phba, mask);
4181}
4182
James Smart92908312006-03-07 15:04:13 -05004183#define BARRIER_TEST_PATTERN (0xdeadbeef)
4184
James Smarte59058c2008-08-24 21:49:00 -04004185/**
James Smart3621a712009-04-06 18:47:14 -04004186 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004187 * @phba: Pointer to HBA context object.
4188 *
James Smart1b511972011-12-13 13:23:09 -05004189 * This function is called before resetting an HBA. This function is called
4190 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004191 **/
James Smart2e0fef82007-06-17 19:56:36 -05004192void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004193{
James Smart65a29c12006-07-06 15:50:50 -04004194 uint32_t __iomem *resp_buf;
4195 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004196 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004197 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004198 int i;
4199 uint8_t hdrtype;
4200
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004201 lockdep_assert_held(&phba->hbalock);
4202
James Smart92908312006-03-07 15:04:13 -05004203 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4204 if (hdrtype != 0x80 ||
4205 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4206 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4207 return;
4208
4209 /*
4210 * Tell the other part of the chip to suspend temporarily all
4211 * its DMA activity.
4212 */
James Smart65a29c12006-07-06 15:50:50 -04004213 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004214
4215 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004216 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4217 return;
James Smart92908312006-03-07 15:04:13 -05004218 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4219 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004220 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004221
James Smart9940b972011-03-11 16:06:12 -05004222 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4223 return;
4224 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004225 /* Clear Chip error bit */
4226 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004227 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004228 }
4229
4230 mbox = 0;
4231 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4232 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4233
4234 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004235 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004236 writel(mbox, mbox_buf);
4237
James Smart9940b972011-03-11 16:06:12 -05004238 for (i = 0; i < 50; i++) {
4239 if (lpfc_readl((resp_buf + 1), &resp_data))
4240 return;
4241 if (resp_data != ~(BARRIER_TEST_PATTERN))
4242 mdelay(1);
4243 else
4244 break;
4245 }
4246 resp_data = 0;
4247 if (lpfc_readl((resp_buf + 1), &resp_data))
4248 return;
4249 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004250 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004251 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004252 goto restore_hc;
4253 else
4254 goto clear_errat;
4255 }
4256
4257 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004258 resp_data = 0;
4259 for (i = 0; i < 500; i++) {
4260 if (lpfc_readl(resp_buf, &resp_data))
4261 return;
4262 if (resp_data != mbox)
4263 mdelay(1);
4264 else
4265 break;
4266 }
James Smart92908312006-03-07 15:04:13 -05004267
4268clear_errat:
4269
James Smart9940b972011-03-11 16:06:12 -05004270 while (++i < 500) {
4271 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4272 return;
4273 if (!(ha_copy & HA_ERATT))
4274 mdelay(1);
4275 else
4276 break;
4277 }
James Smart92908312006-03-07 15:04:13 -05004278
4279 if (readl(phba->HAregaddr) & HA_ERATT) {
4280 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004281 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004282 }
4283
4284restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004285 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004286 writel(hc_copy, phba->HCregaddr);
4287 readl(phba->HCregaddr); /* flush */
4288}
4289
James Smarte59058c2008-08-24 21:49:00 -04004290/**
James Smart3621a712009-04-06 18:47:14 -04004291 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004292 * @phba: Pointer to HBA context object.
4293 *
4294 * This function issues a kill_board mailbox command and waits for
4295 * the error attention interrupt. This function is called for stopping
4296 * the firmware processing. The caller is not required to hold any
4297 * locks. This function calls lpfc_hba_down_post function to free
4298 * any pending commands after the kill. The function will return 1 when it
4299 * fails to kill the board else will return 0.
4300 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004301int
James Smart2e0fef82007-06-17 19:56:36 -05004302lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004303{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004304 struct lpfc_sli *psli;
4305 LPFC_MBOXQ_t *pmb;
4306 uint32_t status;
4307 uint32_t ha_copy;
4308 int retval;
4309 int i = 0;
4310
4311 psli = &phba->sli;
4312
4313 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004314 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004315 "0329 Kill HBA Data: x%x x%x\n",
4316 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004317
James Smart98c9ea52007-10-27 13:37:33 -04004318 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4319 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004320 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004321
4322 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004323 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004324 if (lpfc_readl(phba->HCregaddr, &status)) {
4325 spin_unlock_irq(&phba->hbalock);
4326 mempool_free(pmb, phba->mbox_mem_pool);
4327 return 1;
4328 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004329 status &= ~HC_ERINT_ENA;
4330 writel(status, phba->HCregaddr);
4331 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004332 phba->link_flag |= LS_IGNORE_ERATT;
4333 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004334
4335 lpfc_kill_board(phba, pmb);
4336 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4337 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4338
4339 if (retval != MBX_SUCCESS) {
4340 if (retval != MBX_BUSY)
4341 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004342 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4343 "2752 KILL_BOARD command failed retval %d\n",
4344 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004345 spin_lock_irq(&phba->hbalock);
4346 phba->link_flag &= ~LS_IGNORE_ERATT;
4347 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004348 return 1;
4349 }
4350
James Smartf4b4c682009-05-22 14:53:12 -04004351 spin_lock_irq(&phba->hbalock);
4352 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4353 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004354
Jamie Wellnitz41415862006-02-28 19:25:27 -05004355 mempool_free(pmb, phba->mbox_mem_pool);
4356
4357 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4358 * attention every 100ms for 3 seconds. If we don't get ERATT after
4359 * 3 seconds we still set HBA_ERROR state because the status of the
4360 * board is now undefined.
4361 */
James Smart9940b972011-03-11 16:06:12 -05004362 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4363 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004364 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4365 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004366 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4367 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004368 }
4369
4370 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004371 if (ha_copy & HA_ERATT) {
4372 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004373 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004374 }
James Smart2e0fef82007-06-17 19:56:36 -05004375 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004376 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004377 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004378 phba->link_flag &= ~LS_IGNORE_ERATT;
4379 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004380
Jamie Wellnitz41415862006-02-28 19:25:27 -05004381 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004382 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004383
James Smart2e0fef82007-06-17 19:56:36 -05004384 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004385}
4386
James Smarte59058c2008-08-24 21:49:00 -04004387/**
James Smart3772a992009-05-22 14:50:54 -04004388 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004389 * @phba: Pointer to HBA context object.
4390 *
4391 * This function resets the HBA by writing HC_INITFF to the control
4392 * register. After the HBA resets, this function resets all the iocb ring
4393 * indices. This function disables PCI layer parity checking during
4394 * the reset.
4395 * This function returns 0 always.
4396 * The caller is not required to hold any locks.
4397 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004398int
James Smart2e0fef82007-06-17 19:56:36 -05004399lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004400{
4401 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004402 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004403 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004404 int i;
dea31012005-04-17 16:05:31 -05004405
Jamie Wellnitz41415862006-02-28 19:25:27 -05004406 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004407
Jamie Wellnitz41415862006-02-28 19:25:27 -05004408 /* Reset HBA */
4409 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004410 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004411 (phba->pport) ? phba->pport->port_state : 0,
4412 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004413
4414 /* perform board reset */
4415 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004416 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004417 if (phba->pport) {
4418 phba->pport->fc_myDID = 0;
4419 phba->pport->fc_prevDID = 0;
4420 }
dea31012005-04-17 16:05:31 -05004421
Jamie Wellnitz41415862006-02-28 19:25:27 -05004422 /* Turn off parity checking and serr during the physical reset */
James Smart32a93102019-03-12 16:30:13 -07004423 if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
4424 return -EIO;
4425
Jamie Wellnitz41415862006-02-28 19:25:27 -05004426 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4427 (cfg_value &
4428 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4429
James Smart3772a992009-05-22 14:50:54 -04004430 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4431
Jamie Wellnitz41415862006-02-28 19:25:27 -05004432 /* Now toggle INITFF bit in the Host Control Register */
4433 writel(HC_INITFF, phba->HCregaddr);
4434 mdelay(1);
4435 readl(phba->HCregaddr); /* flush */
4436 writel(0, phba->HCregaddr);
4437 readl(phba->HCregaddr); /* flush */
4438
4439 /* Restore PCI cmd register */
4440 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004441
4442 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004443 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004444 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004445 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004446 pring->sli.sli3.rspidx = 0;
4447 pring->sli.sli3.next_cmdidx = 0;
4448 pring->sli.sli3.local_getidx = 0;
4449 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004450 pring->missbufcnt = 0;
4451 }
dea31012005-04-17 16:05:31 -05004452
James Smart2e0fef82007-06-17 19:56:36 -05004453 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004454 return 0;
4455}
4456
James Smarte59058c2008-08-24 21:49:00 -04004457/**
James Smartda0436e2009-05-22 14:51:39 -04004458 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4459 * @phba: Pointer to HBA context object.
4460 *
4461 * This function resets a SLI4 HBA. This function disables PCI layer parity
4462 * checking during resets the device. The caller is not required to hold
4463 * any locks.
4464 *
James Smart8c24a4f2019-08-14 16:56:53 -07004465 * This function returns 0 on success else returns negative error code.
James Smartda0436e2009-05-22 14:51:39 -04004466 **/
4467int
4468lpfc_sli4_brdreset(struct lpfc_hba *phba)
4469{
4470 struct lpfc_sli *psli = &phba->sli;
4471 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004472 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004473
4474 /* Reset HBA */
4475 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004476 "0295 Reset HBA Data: x%x x%x x%x\n",
4477 phba->pport->port_state, psli->sli_flag,
4478 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004479
4480 /* perform board reset */
4481 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004482 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004483 phba->pport->fc_myDID = 0;
4484 phba->pport->fc_prevDID = 0;
4485
James Smartda0436e2009-05-22 14:51:39 -04004486 spin_lock_irq(&phba->hbalock);
4487 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4488 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004489 spin_unlock_irq(&phba->hbalock);
4490
James Smart02936352014-04-04 13:52:12 -04004491 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4492 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4493 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4494 return rc;
4495 }
4496
James Smartda0436e2009-05-22 14:51:39 -04004497 /* Now physically reset the device */
4498 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4499 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004500
4501 /* Turn off parity checking and serr during the physical reset */
James Smart32a93102019-03-12 16:30:13 -07004502 if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
4503 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4504 "3205 PCI read Config failed\n");
4505 return -EIO;
4506 }
4507
James Smartbe858b62010-12-15 17:57:20 -05004508 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4509 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4510
James Smart88318812012-09-29 11:29:29 -04004511 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004512 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004513
James Smartbe858b62010-12-15 17:57:20 -05004514 /* Restore PCI cmd register */
4515 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4516
James Smart27b01b82012-05-09 21:19:44 -04004517 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004518}
4519
4520/**
4521 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004522 * @phba: Pointer to HBA context object.
4523 *
4524 * This function is called in the SLI initialization code path to
4525 * restart the HBA. The caller is not required to hold any lock.
4526 * This function writes MBX_RESTART mailbox command to the SLIM and
4527 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4528 * function to free any pending commands. The function enables
4529 * POST only during the first initialization. The function returns zero.
4530 * The function does not guarantee completion of MBX_RESTART mailbox
4531 * command before the return of this function.
4532 **/
James Smartda0436e2009-05-22 14:51:39 -04004533static int
4534lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004535{
4536 MAILBOX_t *mb;
4537 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004538 volatile uint32_t word0;
4539 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004540 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004541
James Smart2e0fef82007-06-17 19:56:36 -05004542 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004543
James Smart0d878412009-10-02 15:16:56 -04004544 /* Take PCIe device Advanced Error Reporting (AER) state */
4545 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4546
Jamie Wellnitz41415862006-02-28 19:25:27 -05004547 psli = &phba->sli;
4548
4549 /* Restart HBA */
4550 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004551 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004552 (phba->pport) ? phba->pport->port_state : 0,
4553 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004554
4555 word0 = 0;
4556 mb = (MAILBOX_t *) &word0;
4557 mb->mbxCommand = MBX_RESTART;
4558 mb->mbxHc = 1;
4559
James Smart92908312006-03-07 15:04:13 -05004560 lpfc_reset_barrier(phba);
4561
Jamie Wellnitz41415862006-02-28 19:25:27 -05004562 to_slim = phba->MBslimaddr;
4563 writel(*(uint32_t *) mb, to_slim);
4564 readl(to_slim); /* flush */
4565
4566 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004567 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004568 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004569 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004570 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004571 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004572 writel(*(uint32_t *) mb, to_slim);
4573 readl(to_slim); /* flush */
4574
4575 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004576 if (phba->pport)
4577 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004578 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004579 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004580 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004581
James Smart64ba8812006-08-02 15:24:34 -04004582 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004583 psli->stats_start = ktime_get_seconds();
James Smart64ba8812006-08-02 15:24:34 -04004584
James Smarteaf15d52008-12-04 22:39:29 -05004585 /* Give the INITFF and Post time to settle. */
4586 mdelay(100);
dea31012005-04-17 16:05:31 -05004587
James Smart0d878412009-10-02 15:16:56 -04004588 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4589 if (hba_aer_enabled)
4590 pci_disable_pcie_error_reporting(phba->pcidev);
4591
Jamie Wellnitz41415862006-02-28 19:25:27 -05004592 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004593
4594 return 0;
4595}
4596
James Smarte59058c2008-08-24 21:49:00 -04004597/**
James Smartda0436e2009-05-22 14:51:39 -04004598 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4599 * @phba: Pointer to HBA context object.
4600 *
4601 * This function is called in the SLI initialization code path to restart
4602 * a SLI4 HBA. The caller is not required to hold any lock.
4603 * At the end of the function, it calls lpfc_hba_down_post function to
4604 * free any pending commands.
4605 **/
4606static int
4607lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4608{
4609 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004610 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004611 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004612
4613 /* Restart HBA */
4614 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4615 "0296 Restart HBA Data: x%x x%x\n",
4616 phba->pport->port_state, psli->sli_flag);
4617
James Smart75baf692010-06-08 18:31:21 -04004618 /* Take PCIe device Advanced Error Reporting (AER) state */
4619 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4620
James Smart27b01b82012-05-09 21:19:44 -04004621 rc = lpfc_sli4_brdreset(phba);
James Smart5a9eeff2018-11-29 16:09:32 -08004622 if (rc)
James Smart8c24a4f2019-08-14 16:56:53 -07004623 goto error;
James Smartda0436e2009-05-22 14:51:39 -04004624
4625 spin_lock_irq(&phba->hbalock);
4626 phba->pport->stopped = 0;
4627 phba->link_state = LPFC_INIT_START;
4628 phba->hba_flag = 0;
4629 spin_unlock_irq(&phba->hbalock);
4630
4631 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004632 psli->stats_start = ktime_get_seconds();
James Smartda0436e2009-05-22 14:51:39 -04004633
James Smart75baf692010-06-08 18:31:21 -04004634 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4635 if (hba_aer_enabled)
4636 pci_disable_pcie_error_reporting(phba->pcidev);
4637
James Smart8c24a4f2019-08-14 16:56:53 -07004638error:
4639 phba->link_state = LPFC_HBA_ERROR;
James Smartda0436e2009-05-22 14:51:39 -04004640 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004641 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004642
James Smart27b01b82012-05-09 21:19:44 -04004643 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004644}
4645
4646/**
4647 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4648 * @phba: Pointer to HBA context object.
4649 *
4650 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4651 * API jump table function pointer from the lpfc_hba struct.
4652**/
4653int
4654lpfc_sli_brdrestart(struct lpfc_hba *phba)
4655{
4656 return phba->lpfc_sli_brdrestart(phba);
4657}
4658
4659/**
James Smart3621a712009-04-06 18:47:14 -04004660 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004661 * @phba: Pointer to HBA context object.
4662 *
4663 * This function is called after a HBA restart to wait for successful
4664 * restart of the HBA. Successful restart of the HBA is indicated by
4665 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4666 * iteration, the function will restart the HBA again. The function returns
4667 * zero if HBA successfully restarted else returns negative error code.
4668 **/
James Smart4492b732017-04-27 15:08:26 -07004669int
dea31012005-04-17 16:05:31 -05004670lpfc_sli_chipset_init(struct lpfc_hba *phba)
4671{
4672 uint32_t status, i = 0;
4673
4674 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004675 if (lpfc_readl(phba->HSregaddr, &status))
4676 return -EIO;
dea31012005-04-17 16:05:31 -05004677
4678 /* Check status register to see what current state is */
4679 i = 0;
4680 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4681
James Smartdcf2a4e2010-09-29 11:18:53 -04004682 /* Check every 10ms for 10 retries, then every 100ms for 90
4683 * retries, then every 1 sec for 50 retires for a total of
4684 * ~60 seconds before reset the board again and check every
4685 * 1 sec for 50 retries. The up to 60 seconds before the
4686 * board ready is required by the Falcon FIPS zeroization
4687 * complete, and any reset the board in between shall cause
4688 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004689 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004690 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004691 /* Adapter failed to init, timeout, status reg
4692 <status> */
James Smarted957682007-06-17 19:56:37 -05004693 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004694 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004695 "timeout, status reg x%x, "
4696 "FW Data: A8 x%x AC x%x\n", status,
4697 readl(phba->MBslimaddr + 0xa8),
4698 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004699 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004700 return -ETIMEDOUT;
4701 }
4702
4703 /* Check to see if any errors occurred during init */
4704 if (status & HS_FFERM) {
4705 /* ERROR: During chipset initialization */
4706 /* Adapter failed to init, chipset, status reg
4707 <status> */
James Smarted957682007-06-17 19:56:37 -05004708 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004709 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004710 "chipset, status reg x%x, "
4711 "FW Data: A8 x%x AC x%x\n", status,
4712 readl(phba->MBslimaddr + 0xa8),
4713 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004714 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004715 return -EIO;
4716 }
4717
James Smartdcf2a4e2010-09-29 11:18:53 -04004718 if (i <= 10)
dea31012005-04-17 16:05:31 -05004719 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004720 else if (i <= 100)
4721 msleep(100);
4722 else
4723 msleep(1000);
dea31012005-04-17 16:05:31 -05004724
James Smartdcf2a4e2010-09-29 11:18:53 -04004725 if (i == 150) {
4726 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004727 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004728 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004729 }
4730 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004731 if (lpfc_readl(phba->HSregaddr, &status))
4732 return -EIO;
dea31012005-04-17 16:05:31 -05004733 }
4734
4735 /* Check to see if any errors occurred during init */
4736 if (status & HS_FFERM) {
4737 /* ERROR: During chipset initialization */
4738 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004739 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004740 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004741 "status reg x%x, "
4742 "FW Data: A8 x%x AC x%x\n", status,
4743 readl(phba->MBslimaddr + 0xa8),
4744 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004745 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004746 return -EIO;
4747 }
4748
4749 /* Clear all interrupt enable conditions */
4750 writel(0, phba->HCregaddr);
4751 readl(phba->HCregaddr); /* flush */
4752
4753 /* setup host attn register */
4754 writel(0xffffffff, phba->HAregaddr);
4755 readl(phba->HAregaddr); /* flush */
4756 return 0;
4757}
4758
James Smarte59058c2008-08-24 21:49:00 -04004759/**
James Smart3621a712009-04-06 18:47:14 -04004760 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004761 *
4762 * This function calculates and returns the number of HBQs required to be
4763 * configured.
4764 **/
James Smart78b2d852007-08-02 11:10:21 -04004765int
James Smarted957682007-06-17 19:56:37 -05004766lpfc_sli_hbq_count(void)
4767{
James Smart92d7f7b2007-06-17 19:56:38 -05004768 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004769}
4770
James Smarte59058c2008-08-24 21:49:00 -04004771/**
James Smart3621a712009-04-06 18:47:14 -04004772 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004773 *
4774 * This function adds the number of hbq entries in every HBQ to get
4775 * the total number of hbq entries required for the HBA and returns
4776 * the total count.
4777 **/
James Smarted957682007-06-17 19:56:37 -05004778static int
4779lpfc_sli_hbq_entry_count(void)
4780{
4781 int hbq_count = lpfc_sli_hbq_count();
4782 int count = 0;
4783 int i;
4784
4785 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004786 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004787 return count;
4788}
4789
James Smarte59058c2008-08-24 21:49:00 -04004790/**
James Smart3621a712009-04-06 18:47:14 -04004791 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004792 *
4793 * This function calculates amount of memory required for all hbq entries
4794 * to be configured and returns the total memory required.
4795 **/
dea31012005-04-17 16:05:31 -05004796int
James Smarted957682007-06-17 19:56:37 -05004797lpfc_sli_hbq_size(void)
4798{
4799 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4800}
4801
James Smarte59058c2008-08-24 21:49:00 -04004802/**
James Smart3621a712009-04-06 18:47:14 -04004803 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004804 * @phba: Pointer to HBA context object.
4805 *
4806 * This function is called during the SLI initialization to configure
4807 * all the HBQs and post buffers to the HBQ. The caller is not
4808 * required to hold any locks. This function will return zero if successful
4809 * else it will return negative error code.
4810 **/
James Smarted957682007-06-17 19:56:37 -05004811static int
4812lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4813{
4814 int hbq_count = lpfc_sli_hbq_count();
4815 LPFC_MBOXQ_t *pmb;
4816 MAILBOX_t *pmbox;
4817 uint32_t hbqno;
4818 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004819
James Smart92d7f7b2007-06-17 19:56:38 -05004820 /* Get a Mailbox buffer to setup mailbox
4821 * commands for HBA initialization
4822 */
James Smarted957682007-06-17 19:56:37 -05004823 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4824
4825 if (!pmb)
4826 return -ENOMEM;
4827
James Smart04c68492009-05-22 14:52:52 -04004828 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004829
4830 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4831 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004832 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004833
4834 hbq_entry_index = 0;
4835 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4836 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4837 phba->hbqs[hbqno].hbqPutIdx = 0;
4838 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4839 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004840 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004841 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4842 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004843 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4844
4845 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4846 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4847 mbxStatus <status>, ring <num> */
4848
4849 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004850 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004851 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004852 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004853 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004854 pmbox->mbxStatus, hbqno);
4855
4856 phba->link_state = LPFC_HBA_ERROR;
4857 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004858 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004859 }
4860 }
4861 phba->hbq_count = hbq_count;
4862
James Smarted957682007-06-17 19:56:37 -05004863 mempool_free(pmb, phba->mbox_mem_pool);
4864
James Smart92d7f7b2007-06-17 19:56:38 -05004865 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004866 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4867 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004868 return 0;
4869}
4870
James Smarte59058c2008-08-24 21:49:00 -04004871/**
James Smart4f774512009-05-22 14:52:35 -04004872 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4873 * @phba: Pointer to HBA context object.
4874 *
4875 * This function is called during the SLI initialization to configure
4876 * all the HBQs and post buffers to the HBQ. The caller is not
4877 * required to hold any locks. This function will return zero if successful
4878 * else it will return negative error code.
4879 **/
4880static int
4881lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4882{
4883 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004884 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4885 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004886 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004887 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004888 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004889 return 0;
4890}
4891
4892/**
James Smart3621a712009-04-06 18:47:14 -04004893 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004894 * @phba: Pointer to HBA context object.
4895 * @sli_mode: sli mode - 2/3
4896 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004897 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004898 * to issue config_port mailbox command. This function restarts the
4899 * HBA firmware and issues a config_port mailbox command to configure
4900 * the SLI interface in the sli mode specified by sli_mode
4901 * variable. The caller is not required to hold any locks.
4902 * The function returns 0 if successful, else returns negative error
4903 * code.
4904 **/
James Smart93996272008-08-24 21:50:30 -04004905int
4906lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004907{
4908 LPFC_MBOXQ_t *pmb;
4909 uint32_t resetcount = 0, rc = 0, done = 0;
4910
4911 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4912 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004913 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004914 return -ENOMEM;
4915 }
4916
James Smarted957682007-06-17 19:56:37 -05004917 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004918 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004919 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004920 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004921 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004922 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004923 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004924 rc = lpfc_sli_chipset_init(phba);
4925 if (rc)
4926 break;
4927
James Smart2e0fef82007-06-17 19:56:36 -05004928 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004929 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004930 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004931 resetcount++;
4932
James Smarted957682007-06-17 19:56:37 -05004933 /* Call pre CONFIG_PORT mailbox command initialization. A
4934 * value of 0 means the call was successful. Any other
4935 * nonzero value is a failure, but if ERESTART is returned,
4936 * the driver may reset the HBA and try again.
4937 */
dea31012005-04-17 16:05:31 -05004938 rc = lpfc_config_port_prep(phba);
4939 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004940 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004941 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004942 } else if (rc)
dea31012005-04-17 16:05:31 -05004943 break;
James Smart6d368e52011-05-24 11:44:12 -04004944
James Smart2e0fef82007-06-17 19:56:36 -05004945 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004946 lpfc_config_port(phba, pmb);
4947 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004948 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4949 LPFC_SLI3_HBQ_ENABLED |
4950 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004951 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004952 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004953 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004954 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004955 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004956 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004957 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004958 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004959 spin_unlock_irq(&phba->hbalock);
4960 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004961 } else {
4962 /* Allow asynchronous mailbox command to go through */
4963 spin_lock_irq(&phba->hbalock);
4964 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4965 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004966 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004967
4968 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4969 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4970 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4971 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004972 }
dea31012005-04-17 16:05:31 -05004973 }
James Smarted957682007-06-17 19:56:37 -05004974 if (!done) {
4975 rc = -EINVAL;
4976 goto do_prep_failed;
4977 }
James Smart04c68492009-05-22 14:52:52 -04004978 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4979 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004980 rc = -ENXIO;
4981 goto do_prep_failed;
4982 }
James Smart04c68492009-05-22 14:52:52 -04004983 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004984 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004985 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4986 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4987 phba->max_vpi : phba->max_vports;
4988
James Smart34b02dc2008-08-24 21:49:55 -04004989 } else
4990 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004991 phba->fips_level = 0;
4992 phba->fips_spec_rev = 0;
4993 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004994 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004995 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4996 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4997 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4998 "2850 Security Crypto Active. FIPS x%d "
4999 "(Spec Rev: x%d)",
5000 phba->fips_level, phba->fips_spec_rev);
5001 }
5002 if (pmb->u.mb.un.varCfgPort.sec_err) {
5003 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5004 "2856 Config Port Security Crypto "
5005 "Error: x%x ",
5006 pmb->u.mb.un.varCfgPort.sec_err);
5007 }
James Smart04c68492009-05-22 14:52:52 -04005008 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04005009 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005010 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04005011 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04005012
5013 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5014 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05005015
James Smartf44ac122018-03-05 12:04:08 -08005016 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5017 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5018 phba->cfg_enable_bg = 0;
5019 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smarte2a0a9d2008-12-04 22:40:02 -05005020 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5021 "0443 Adapter did not grant "
5022 "BlockGuard\n");
James Smartf44ac122018-03-05 12:04:08 -08005023 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005024 }
James Smart34b02dc2008-08-24 21:49:55 -04005025 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05005026 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04005027 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04005028 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05005029 }
James Smart92d7f7b2007-06-17 19:56:38 -05005030do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05005031 mempool_free(pmb, phba->mbox_mem_pool);
5032 return rc;
5033}
5034
James Smarte59058c2008-08-24 21:49:00 -04005035
5036/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08005037 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04005038 * @phba: Pointer to HBA context object.
5039 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005040 * This function is the main SLI initialization function. This function
5041 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04005042 * error attention handler code. Caller is not required to hold any
5043 * locks. This function issues config_port mailbox command to configure
5044 * the SLI, setup iocb rings and HBQ rings. In the end the function
5045 * calls the config_port_post function to issue init_link mailbox
5046 * command and to start the discovery. The function will return zero
5047 * if successful, else it will return negative error code.
5048 **/
James Smarted957682007-06-17 19:56:37 -05005049int
5050lpfc_sli_hba_setup(struct lpfc_hba *phba)
5051{
5052 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04005053 int mode = 3, i;
5054 int longs;
James Smarted957682007-06-17 19:56:37 -05005055
James Smart12247e82016-07-06 12:36:09 -07005056 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05005057 case 2:
James Smart78b2d852007-08-02 11:10:21 -04005058 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05005059 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005060 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05005061 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07005062 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05005063 break;
5064 }
James Smarted957682007-06-17 19:56:37 -05005065 mode = 2;
5066 break;
5067 case 0:
5068 case 3:
5069 break;
5070 default:
James Smart92d7f7b2007-06-17 19:56:38 -05005071 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005072 "1819 Unrecognized sli_mode parameter: %d.\n",
5073 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05005074
5075 break;
5076 }
James Smartb5c53952016-03-31 14:12:30 -07005077 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005078
James Smart93996272008-08-24 21:50:30 -04005079 rc = lpfc_sli_config_port(phba, mode);
5080
James Smart12247e82016-07-06 12:36:09 -07005081 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05005082 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005083 "1820 Unable to select SLI-3. "
5084 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05005085 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04005086 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07005087 else if (rc && mode == 2)
5088 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05005089 if (rc)
dea31012005-04-17 16:05:31 -05005090 goto lpfc_sli_hba_setup_error;
5091
James Smart0d878412009-10-02 15:16:56 -04005092 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5093 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5094 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5095 if (!rc) {
5096 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5097 "2709 This device supports "
5098 "Advanced Error Reporting (AER)\n");
5099 spin_lock_irq(&phba->hbalock);
5100 phba->hba_flag |= HBA_AER_ENABLED;
5101 spin_unlock_irq(&phba->hbalock);
5102 } else {
5103 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5104 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005105 "Advanced Error Reporting (AER): %d\n",
5106 rc);
James Smart0d878412009-10-02 15:16:56 -04005107 phba->cfg_aer_support = 0;
5108 }
5109 }
5110
James Smarted957682007-06-17 19:56:37 -05005111 if (phba->sli_rev == 3) {
5112 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5113 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005114 } else {
5115 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5116 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005117 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005118 }
5119
5120 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005121 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5122 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005123 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005124
5125 if (rc)
5126 goto lpfc_sli_hba_setup_error;
5127
James Smart6d368e52011-05-24 11:44:12 -04005128 /* Initialize VPIs. */
5129 if (phba->sli_rev == LPFC_SLI_REV3) {
5130 /*
5131 * The VPI bitmask and physical ID array are allocated
5132 * and initialized once only - at driver load. A port
5133 * reset doesn't need to reinitialize this memory.
5134 */
5135 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5136 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07005137 phba->vpi_bmask = kcalloc(longs,
5138 sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005139 GFP_KERNEL);
5140 if (!phba->vpi_bmask) {
5141 rc = -ENOMEM;
5142 goto lpfc_sli_hba_setup_error;
5143 }
5144
Kees Cook6396bb22018-06-12 14:03:40 -07005145 phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5146 sizeof(uint16_t),
5147 GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -04005148 if (!phba->vpi_ids) {
5149 kfree(phba->vpi_bmask);
5150 rc = -ENOMEM;
5151 goto lpfc_sli_hba_setup_error;
5152 }
5153 for (i = 0; i < phba->max_vpi; i++)
5154 phba->vpi_ids[i] = i;
5155 }
5156 }
5157
James Smart93996272008-08-24 21:50:30 -04005158 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005159 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5160 rc = lpfc_sli_hbq_setup(phba);
5161 if (rc)
5162 goto lpfc_sli_hba_setup_error;
5163 }
James Smart04c68492009-05-22 14:52:52 -04005164 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005165 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005166 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005167
5168 rc = lpfc_config_port_post(phba);
5169 if (rc)
5170 goto lpfc_sli_hba_setup_error;
5171
James Smarted957682007-06-17 19:56:37 -05005172 return rc;
5173
James Smart92d7f7b2007-06-17 19:56:38 -05005174lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005175 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05005176 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005177 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005178 return rc;
5179}
5180
James Smartda0436e2009-05-22 14:51:39 -04005181/**
5182 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5183 * @phba: Pointer to HBA context object.
5184 * @mboxq: mailbox pointer.
5185 * This function issue a dump mailbox command to read config region
5186 * 23 and parse the records in the region and populate driver
5187 * data structure.
5188 **/
5189static int
James Smartff78d8f2011-12-13 13:21:35 -05005190lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005191{
James Smartff78d8f2011-12-13 13:21:35 -05005192 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005193 struct lpfc_dmabuf *mp;
5194 struct lpfc_mqe *mqe;
5195 uint32_t data_length;
5196 int rc;
5197
5198 /* Program the default value of vlan_id and fc_map */
5199 phba->valid_vlan = 0;
5200 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5201 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5202 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5203
James Smartff78d8f2011-12-13 13:21:35 -05005204 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5205 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005206 return -ENOMEM;
5207
James Smartff78d8f2011-12-13 13:21:35 -05005208 mqe = &mboxq->u.mqe;
5209 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5210 rc = -ENOMEM;
5211 goto out_free_mboxq;
5212 }
5213
James Smart3e1f0712018-11-29 16:09:29 -08005214 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04005215 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5216
5217 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5218 "(%d):2571 Mailbox cmd x%x Status x%x "
5219 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5220 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5221 "CQ: x%x x%x x%x x%x\n",
5222 mboxq->vport ? mboxq->vport->vpi : 0,
5223 bf_get(lpfc_mqe_command, mqe),
5224 bf_get(lpfc_mqe_status, mqe),
5225 mqe->un.mb_words[0], mqe->un.mb_words[1],
5226 mqe->un.mb_words[2], mqe->un.mb_words[3],
5227 mqe->un.mb_words[4], mqe->un.mb_words[5],
5228 mqe->un.mb_words[6], mqe->un.mb_words[7],
5229 mqe->un.mb_words[8], mqe->un.mb_words[9],
5230 mqe->un.mb_words[10], mqe->un.mb_words[11],
5231 mqe->un.mb_words[12], mqe->un.mb_words[13],
5232 mqe->un.mb_words[14], mqe->un.mb_words[15],
5233 mqe->un.mb_words[16], mqe->un.mb_words[50],
5234 mboxq->mcqe.word0,
5235 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5236 mboxq->mcqe.trailer);
5237
5238 if (rc) {
5239 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5240 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005241 rc = -EIO;
5242 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005243 }
5244 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005245 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005246 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5247 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005248 rc = -EIO;
5249 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005250 }
James Smartda0436e2009-05-22 14:51:39 -04005251
5252 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5253 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5254 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005255 rc = 0;
5256
5257out_free_mboxq:
5258 mempool_free(mboxq, phba->mbox_mem_pool);
5259 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005260}
5261
5262/**
5263 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5264 * @phba: pointer to lpfc hba data structure.
5265 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5266 * @vpd: pointer to the memory to hold resulting port vpd data.
5267 * @vpd_size: On input, the number of bytes allocated to @vpd.
5268 * On output, the number of data bytes in @vpd.
5269 *
5270 * This routine executes a READ_REV SLI4 mailbox command. In
5271 * addition, this routine gets the port vpd data.
5272 *
5273 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005274 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005275 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005276 **/
5277static int
5278lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5279 uint8_t *vpd, uint32_t *vpd_size)
5280{
5281 int rc = 0;
5282 uint32_t dma_size;
5283 struct lpfc_dmabuf *dmabuf;
5284 struct lpfc_mqe *mqe;
5285
5286 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5287 if (!dmabuf)
5288 return -ENOMEM;
5289
5290 /*
5291 * Get a DMA buffer for the vpd data resulting from the READ_REV
5292 * mailbox command.
5293 */
5294 dma_size = *vpd_size;
Luis Chamberlain750afb02019-01-04 09:23:09 +01005295 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
5296 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005297 if (!dmabuf->virt) {
5298 kfree(dmabuf);
5299 return -ENOMEM;
5300 }
James Smartda0436e2009-05-22 14:51:39 -04005301
5302 /*
5303 * The SLI4 implementation of READ_REV conflicts at word1,
5304 * bits 31:16 and SLI4 adds vpd functionality not present
5305 * in SLI3. This code corrects the conflicts.
5306 */
5307 lpfc_read_rev(phba, mboxq);
5308 mqe = &mboxq->u.mqe;
5309 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5310 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5311 mqe->un.read_rev.word1 &= 0x0000FFFF;
5312 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5313 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5314
5315 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5316 if (rc) {
5317 dma_free_coherent(&phba->pcidev->dev, dma_size,
5318 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005319 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005320 return -EIO;
5321 }
5322
James Smartda0436e2009-05-22 14:51:39 -04005323 /*
5324 * The available vpd length cannot be bigger than the
5325 * DMA buffer passed to the port. Catch the less than
5326 * case and update the caller's size.
5327 */
5328 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5329 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5330
James Smartd7c47992010-06-08 18:31:54 -04005331 memcpy(vpd, dmabuf->virt, *vpd_size);
5332
James Smartda0436e2009-05-22 14:51:39 -04005333 dma_free_coherent(&phba->pcidev->dev, dma_size,
5334 dmabuf->virt, dmabuf->phys);
5335 kfree(dmabuf);
5336 return 0;
5337}
5338
5339/**
James Smartb3b4f3e2019-03-12 16:30:23 -07005340 * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
James Smartcd1c8302011-10-10 21:33:25 -04005341 * @phba: pointer to lpfc hba data structure.
5342 *
5343 * This routine retrieves SLI4 device physical port name this PCI function
5344 * is attached to.
5345 *
5346 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005347 * 0 - successful
James Smartb3b4f3e2019-03-12 16:30:23 -07005348 * otherwise - failed to retrieve controller attributes
James Smartcd1c8302011-10-10 21:33:25 -04005349 **/
5350static int
James Smartb3b4f3e2019-03-12 16:30:23 -07005351lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
James Smartcd1c8302011-10-10 21:33:25 -04005352{
5353 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005354 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5355 struct lpfc_controller_attribute *cntl_attr;
James Smartcd1c8302011-10-10 21:33:25 -04005356 void *virtaddr = NULL;
5357 uint32_t alloclen, reqlen;
5358 uint32_t shdr_status, shdr_add_status;
5359 union lpfc_sli4_cfg_shdr *shdr;
James Smartcd1c8302011-10-10 21:33:25 -04005360 int rc;
5361
James Smartcd1c8302011-10-10 21:33:25 -04005362 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5363 if (!mboxq)
5364 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005365
James Smartb3b4f3e2019-03-12 16:30:23 -07005366 /* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
James Smartcd1c8302011-10-10 21:33:25 -04005367 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5368 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5369 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5370 LPFC_SLI4_MBX_NEMBED);
James Smartb3b4f3e2019-03-12 16:30:23 -07005371
James Smartcd1c8302011-10-10 21:33:25 -04005372 if (alloclen < reqlen) {
5373 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5374 "3084 Allocated DMA memory size (%d) is "
5375 "less than the requested DMA memory size "
5376 "(%d)\n", alloclen, reqlen);
5377 rc = -ENOMEM;
5378 goto out_free_mboxq;
5379 }
5380 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5381 virtaddr = mboxq->sge_array->addr[0];
5382 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5383 shdr = &mbx_cntl_attr->cfg_shdr;
5384 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5385 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5386 if (shdr_status || shdr_add_status || rc) {
5387 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5388 "3085 Mailbox x%x (x%x/x%x) failed, "
5389 "rc:x%x, status:x%x, add_status:x%x\n",
5390 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5391 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5392 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5393 rc, shdr_status, shdr_add_status);
5394 rc = -ENXIO;
5395 goto out_free_mboxq;
5396 }
James Smartb3b4f3e2019-03-12 16:30:23 -07005397
James Smartcd1c8302011-10-10 21:33:25 -04005398 cntl_attr = &mbx_cntl_attr->cntl_attr;
5399 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5400 phba->sli4_hba.lnk_info.lnk_tp =
5401 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5402 phba->sli4_hba.lnk_info.lnk_no =
5403 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
James Smartb3b4f3e2019-03-12 16:30:23 -07005404
5405 memset(phba->BIOSVersion, 0, sizeof(phba->BIOSVersion));
5406 strlcat(phba->BIOSVersion, (char *)cntl_attr->bios_ver_str,
5407 sizeof(phba->BIOSVersion));
5408
James Smartcd1c8302011-10-10 21:33:25 -04005409 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartb3b4f3e2019-03-12 16:30:23 -07005410 "3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s\n",
James Smartcd1c8302011-10-10 21:33:25 -04005411 phba->sli4_hba.lnk_info.lnk_tp,
James Smartb3b4f3e2019-03-12 16:30:23 -07005412 phba->sli4_hba.lnk_info.lnk_no,
5413 phba->BIOSVersion);
5414out_free_mboxq:
5415 if (rc != MBX_TIMEOUT) {
5416 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5417 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5418 else
5419 mempool_free(mboxq, phba->mbox_mem_pool);
5420 }
5421 return rc;
5422}
5423
5424/**
5425 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5426 * @phba: pointer to lpfc hba data structure.
5427 *
5428 * This routine retrieves SLI4 device physical port name this PCI function
5429 * is attached to.
5430 *
5431 * Return codes
5432 * 0 - successful
5433 * otherwise - failed to retrieve physical port name
5434 **/
5435static int
5436lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5437{
5438 LPFC_MBOXQ_t *mboxq;
5439 struct lpfc_mbx_get_port_name *get_port_name;
5440 uint32_t shdr_status, shdr_add_status;
5441 union lpfc_sli4_cfg_shdr *shdr;
5442 char cport_name = 0;
5443 int rc;
5444
5445 /* We assume nothing at this point */
5446 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5447 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5448
5449 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5450 if (!mboxq)
5451 return -ENOMEM;
5452 /* obtain link type and link number via READ_CONFIG */
5453 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5454 lpfc_sli4_read_config(phba);
5455 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5456 goto retrieve_ppname;
5457
5458 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5459 rc = lpfc_sli4_get_ctl_attr(phba);
5460 if (rc)
5461 goto out_free_mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005462
5463retrieve_ppname:
5464 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5465 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5466 sizeof(struct lpfc_mbx_get_port_name) -
5467 sizeof(struct lpfc_sli4_cfg_mhdr),
5468 LPFC_SLI4_MBX_EMBED);
5469 get_port_name = &mboxq->u.mqe.un.get_port_name;
5470 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5471 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5472 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5473 phba->sli4_hba.lnk_info.lnk_tp);
5474 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5475 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5476 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5477 if (shdr_status || shdr_add_status || rc) {
5478 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5479 "3087 Mailbox x%x (x%x/x%x) failed: "
5480 "rc:x%x, status:x%x, add_status:x%x\n",
5481 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5482 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5483 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5484 rc, shdr_status, shdr_add_status);
5485 rc = -ENXIO;
5486 goto out_free_mboxq;
5487 }
5488 switch (phba->sli4_hba.lnk_info.lnk_no) {
5489 case LPFC_LINK_NUMBER_0:
5490 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5491 &get_port_name->u.response);
5492 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5493 break;
5494 case LPFC_LINK_NUMBER_1:
5495 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5496 &get_port_name->u.response);
5497 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5498 break;
5499 case LPFC_LINK_NUMBER_2:
5500 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5501 &get_port_name->u.response);
5502 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5503 break;
5504 case LPFC_LINK_NUMBER_3:
5505 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5506 &get_port_name->u.response);
5507 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5508 break;
5509 default:
5510 break;
5511 }
5512
5513 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5514 phba->Port[0] = cport_name;
5515 phba->Port[1] = '\0';
5516 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5517 "3091 SLI get port name: %s\n", phba->Port);
5518 }
5519
5520out_free_mboxq:
5521 if (rc != MBX_TIMEOUT) {
5522 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5523 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5524 else
5525 mempool_free(mboxq, phba->mbox_mem_pool);
5526 }
5527 return rc;
5528}
5529
5530/**
James Smartda0436e2009-05-22 14:51:39 -04005531 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5532 * @phba: pointer to lpfc hba data structure.
5533 *
5534 * This routine is called to explicitly arm the SLI4 device's completion and
5535 * event queues
5536 **/
5537static void
5538lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5539{
James Smart895427b2017-02-12 13:52:30 -08005540 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08005541 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartcdb42be2019-01-28 11:14:21 -08005542 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07005543 struct lpfc_queue *eq;
James Smartda0436e2009-05-22 14:51:39 -04005544
James Smart32517fc2019-01-28 11:14:33 -08005545 sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
5546 sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
James Smartb71413d2018-02-22 08:18:40 -08005547 if (sli4_hba->nvmels_cq)
James Smart32517fc2019-01-28 11:14:33 -08005548 sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
5549 LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08005550
James Smartcdb42be2019-01-28 11:14:21 -08005551 if (sli4_hba->hdwq) {
James Smart657add42019-05-21 17:49:06 -07005552 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -08005553 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07005554 qp = &sli4_hba->hdwq[qidx];
5555 /* ARM the corresponding CQ */
James Smartc00f62e2019-08-14 16:57:11 -07005556 sli4_hba->sli4_write_cq_db(phba, qp[qidx].io_cq, 0,
5557 LPFC_QUEUE_REARM);
James Smartcdb42be2019-01-28 11:14:21 -08005558 }
James Smart895427b2017-02-12 13:52:30 -08005559
James Smart657add42019-05-21 17:49:06 -07005560 /* Loop thru all IRQ vectors */
5561 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
5562 eq = sli4_hba->hba_eq_hdl[qidx].eq;
5563 /* ARM the corresponding EQ */
5564 sli4_hba->sli4_write_eq_db(phba, eq,
5565 0, LPFC_QUEUE_REARM);
5566 }
James Smartcdb42be2019-01-28 11:14:21 -08005567 }
James Smart1ba981f2014-02-20 09:56:45 -05005568
James Smart2d7dbc42017-02-12 13:52:35 -08005569 if (phba->nvmet_support) {
5570 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smart32517fc2019-01-28 11:14:33 -08005571 sli4_hba->sli4_write_cq_db(phba,
5572 sli4_hba->nvmet_cqset[qidx], 0,
James Smart2d7dbc42017-02-12 13:52:35 -08005573 LPFC_QUEUE_REARM);
5574 }
James Smart2e90f4b2011-12-13 13:22:37 -05005575 }
James Smartda0436e2009-05-22 14:51:39 -04005576}
5577
5578/**
James Smart6d368e52011-05-24 11:44:12 -04005579 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5580 * @phba: Pointer to HBA context object.
5581 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005582 * @extnt_count: buffer to hold port available extent count.
5583 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005584 *
James Smartb76f2dc2011-07-22 18:37:42 -04005585 * This function calls the port and retrievs the number of available
5586 * extents and their size for a particular extent type.
5587 *
5588 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005589 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005590int
James Smart6d368e52011-05-24 11:44:12 -04005591lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5592 uint16_t *extnt_count, uint16_t *extnt_size)
5593{
5594 int rc = 0;
5595 uint32_t length;
5596 uint32_t mbox_tmo;
5597 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5598 LPFC_MBOXQ_t *mbox;
5599
5600 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5601 if (!mbox)
5602 return -ENOMEM;
5603
5604 /* Find out how many extents are available for this resource type */
5605 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5606 sizeof(struct lpfc_sli4_cfg_mhdr));
5607 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5608 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5609 length, LPFC_SLI4_MBX_EMBED);
5610
5611 /* Send an extents count of 0 - the GET doesn't use it. */
5612 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5613 LPFC_SLI4_MBX_EMBED);
5614 if (unlikely(rc)) {
5615 rc = -EIO;
5616 goto err_exit;
5617 }
5618
5619 if (!phba->sli4_hba.intr_enable)
5620 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5621 else {
James Smarta183a152011-10-10 21:32:43 -04005622 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005623 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5624 }
5625 if (unlikely(rc)) {
5626 rc = -EIO;
5627 goto err_exit;
5628 }
5629
5630 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5631 if (bf_get(lpfc_mbox_hdr_status,
5632 &rsrc_info->header.cfg_shdr.response)) {
5633 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5634 "2930 Failed to get resource extents "
5635 "Status 0x%x Add'l Status 0x%x\n",
5636 bf_get(lpfc_mbox_hdr_status,
5637 &rsrc_info->header.cfg_shdr.response),
5638 bf_get(lpfc_mbox_hdr_add_status,
5639 &rsrc_info->header.cfg_shdr.response));
5640 rc = -EIO;
5641 goto err_exit;
5642 }
5643
5644 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5645 &rsrc_info->u.rsp);
5646 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5647 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005648
5649 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5650 "3162 Retrieved extents type-%d from port: count:%d, "
5651 "size:%d\n", type, *extnt_count, *extnt_size);
5652
5653err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005654 mempool_free(mbox, phba->mbox_mem_pool);
5655 return rc;
5656}
5657
5658/**
5659 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5660 * @phba: Pointer to HBA context object.
5661 * @type: The extent type to check.
5662 *
5663 * This function reads the current available extents from the port and checks
5664 * if the extent count or extent size has changed since the last access.
5665 * Callers use this routine post port reset to understand if there is a
5666 * extent reprovisioning requirement.
5667 *
5668 * Returns:
5669 * -Error: error indicates problem.
5670 * 1: Extent count or size has changed.
5671 * 0: No changes.
5672 **/
5673static int
5674lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5675{
5676 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5677 uint16_t size_diff, rsrc_ext_size;
5678 int rc = 0;
5679 struct lpfc_rsrc_blks *rsrc_entry;
5680 struct list_head *rsrc_blk_list = NULL;
5681
5682 size_diff = 0;
5683 curr_ext_cnt = 0;
5684 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5685 &rsrc_ext_cnt,
5686 &rsrc_ext_size);
5687 if (unlikely(rc))
5688 return -EIO;
5689
5690 switch (type) {
5691 case LPFC_RSC_TYPE_FCOE_RPI:
5692 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5693 break;
5694 case LPFC_RSC_TYPE_FCOE_VPI:
5695 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5696 break;
5697 case LPFC_RSC_TYPE_FCOE_XRI:
5698 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5699 break;
5700 case LPFC_RSC_TYPE_FCOE_VFI:
5701 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5702 break;
5703 default:
5704 break;
5705 }
5706
5707 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5708 curr_ext_cnt++;
5709 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5710 size_diff++;
5711 }
5712
5713 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5714 rc = 1;
5715
5716 return rc;
5717}
5718
5719/**
5720 * lpfc_sli4_cfg_post_extnts -
5721 * @phba: Pointer to HBA context object.
5722 * @extnt_cnt - number of available extents.
5723 * @type - the extent type (rpi, xri, vfi, vpi).
5724 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5725 * @mbox - pointer to the caller's allocated mailbox structure.
5726 *
5727 * This function executes the extents allocation request. It also
5728 * takes care of the amount of memory needed to allocate or get the
5729 * allocated extents. It is the caller's responsibility to evaluate
5730 * the response.
5731 *
5732 * Returns:
5733 * -Error: Error value describes the condition found.
5734 * 0: if successful
5735 **/
5736static int
James Smart8a9d2e82012-05-09 21:16:12 -04005737lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005738 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5739{
5740 int rc = 0;
5741 uint32_t req_len;
5742 uint32_t emb_len;
5743 uint32_t alloc_len, mbox_tmo;
5744
5745 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005746 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005747
5748 /*
5749 * Calculate the size of an embedded mailbox. The uint32_t
5750 * accounts for extents-specific word.
5751 */
5752 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5753 sizeof(uint32_t);
5754
5755 /*
5756 * Presume the allocation and response will fit into an embedded
5757 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5758 */
5759 *emb = LPFC_SLI4_MBX_EMBED;
5760 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005761 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005762 sizeof(union lpfc_sli4_cfg_shdr) +
5763 sizeof(uint32_t);
5764 *emb = LPFC_SLI4_MBX_NEMBED;
5765 }
5766
5767 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5768 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5769 req_len, *emb);
5770 if (alloc_len < req_len) {
5771 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005772 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005773 "less than the requested DMA memory "
5774 "size (x%x)\n", alloc_len, req_len);
5775 return -ENOMEM;
5776 }
James Smart8a9d2e82012-05-09 21:16:12 -04005777 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005778 if (unlikely(rc))
5779 return -EIO;
5780
5781 if (!phba->sli4_hba.intr_enable)
5782 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5783 else {
James Smarta183a152011-10-10 21:32:43 -04005784 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005785 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5786 }
5787
5788 if (unlikely(rc))
5789 rc = -EIO;
5790 return rc;
5791}
5792
5793/**
5794 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5795 * @phba: Pointer to HBA context object.
5796 * @type: The resource extent type to allocate.
5797 *
5798 * This function allocates the number of elements for the specified
5799 * resource type.
5800 **/
5801static int
5802lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5803{
5804 bool emb = false;
5805 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5806 uint16_t rsrc_id, rsrc_start, j, k;
5807 uint16_t *ids;
5808 int i, rc;
5809 unsigned long longs;
5810 unsigned long *bmask;
5811 struct lpfc_rsrc_blks *rsrc_blks;
5812 LPFC_MBOXQ_t *mbox;
5813 uint32_t length;
5814 struct lpfc_id_range *id_array = NULL;
5815 void *virtaddr = NULL;
5816 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5817 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5818 struct list_head *ext_blk_list;
5819
5820 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5821 &rsrc_cnt,
5822 &rsrc_size);
5823 if (unlikely(rc))
5824 return -EIO;
5825
5826 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5827 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5828 "3009 No available Resource Extents "
5829 "for resource type 0x%x: Count: 0x%x, "
5830 "Size 0x%x\n", type, rsrc_cnt,
5831 rsrc_size);
5832 return -ENOMEM;
5833 }
5834
James Smart8a9d2e82012-05-09 21:16:12 -04005835 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5836 "2903 Post resource extents type-0x%x: "
5837 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005838
5839 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5840 if (!mbox)
5841 return -ENOMEM;
5842
James Smart8a9d2e82012-05-09 21:16:12 -04005843 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005844 if (unlikely(rc)) {
5845 rc = -EIO;
5846 goto err_exit;
5847 }
5848
5849 /*
5850 * Figure out where the response is located. Then get local pointers
5851 * to the response data. The port does not guarantee to respond to
5852 * all extents counts request so update the local variable with the
5853 * allocated count from the port.
5854 */
5855 if (emb == LPFC_SLI4_MBX_EMBED) {
5856 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5857 id_array = &rsrc_ext->u.rsp.id[0];
5858 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5859 } else {
5860 virtaddr = mbox->sge_array->addr[0];
5861 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5862 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5863 id_array = &n_rsrc->id;
5864 }
5865
5866 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5867 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5868
5869 /*
5870 * Based on the resource size and count, correct the base and max
5871 * resource values.
5872 */
5873 length = sizeof(struct lpfc_rsrc_blks);
5874 switch (type) {
5875 case LPFC_RSC_TYPE_FCOE_RPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005876 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005877 sizeof(unsigned long),
5878 GFP_KERNEL);
5879 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5880 rc = -ENOMEM;
5881 goto err_exit;
5882 }
Kees Cook6396bb22018-06-12 14:03:40 -07005883 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005884 sizeof(uint16_t),
5885 GFP_KERNEL);
5886 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5887 kfree(phba->sli4_hba.rpi_bmask);
5888 rc = -ENOMEM;
5889 goto err_exit;
5890 }
5891
5892 /*
5893 * The next_rpi was initialized with the maximum available
5894 * count but the port may allocate a smaller number. Catch
5895 * that case and update the next_rpi.
5896 */
5897 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5898
5899 /* Initialize local ptrs for common extent processing later. */
5900 bmask = phba->sli4_hba.rpi_bmask;
5901 ids = phba->sli4_hba.rpi_ids;
5902 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5903 break;
5904 case LPFC_RSC_TYPE_FCOE_VPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005905 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005906 GFP_KERNEL);
5907 if (unlikely(!phba->vpi_bmask)) {
5908 rc = -ENOMEM;
5909 goto err_exit;
5910 }
Kees Cook6396bb22018-06-12 14:03:40 -07005911 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04005912 GFP_KERNEL);
5913 if (unlikely(!phba->vpi_ids)) {
5914 kfree(phba->vpi_bmask);
5915 rc = -ENOMEM;
5916 goto err_exit;
5917 }
5918
5919 /* Initialize local ptrs for common extent processing later. */
5920 bmask = phba->vpi_bmask;
5921 ids = phba->vpi_ids;
5922 ext_blk_list = &phba->lpfc_vpi_blk_list;
5923 break;
5924 case LPFC_RSC_TYPE_FCOE_XRI:
Kees Cook6396bb22018-06-12 14:03:40 -07005925 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005926 sizeof(unsigned long),
5927 GFP_KERNEL);
5928 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5929 rc = -ENOMEM;
5930 goto err_exit;
5931 }
James Smart8a9d2e82012-05-09 21:16:12 -04005932 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07005933 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005934 sizeof(uint16_t),
5935 GFP_KERNEL);
5936 if (unlikely(!phba->sli4_hba.xri_ids)) {
5937 kfree(phba->sli4_hba.xri_bmask);
5938 rc = -ENOMEM;
5939 goto err_exit;
5940 }
5941
5942 /* Initialize local ptrs for common extent processing later. */
5943 bmask = phba->sli4_hba.xri_bmask;
5944 ids = phba->sli4_hba.xri_ids;
5945 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5946 break;
5947 case LPFC_RSC_TYPE_FCOE_VFI:
Kees Cook6396bb22018-06-12 14:03:40 -07005948 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005949 sizeof(unsigned long),
5950 GFP_KERNEL);
5951 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5952 rc = -ENOMEM;
5953 goto err_exit;
5954 }
Kees Cook6396bb22018-06-12 14:03:40 -07005955 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005956 sizeof(uint16_t),
5957 GFP_KERNEL);
5958 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5959 kfree(phba->sli4_hba.vfi_bmask);
5960 rc = -ENOMEM;
5961 goto err_exit;
5962 }
5963
5964 /* Initialize local ptrs for common extent processing later. */
5965 bmask = phba->sli4_hba.vfi_bmask;
5966 ids = phba->sli4_hba.vfi_ids;
5967 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5968 break;
5969 default:
5970 /* Unsupported Opcode. Fail call. */
5971 id_array = NULL;
5972 bmask = NULL;
5973 ids = NULL;
5974 ext_blk_list = NULL;
5975 goto err_exit;
5976 }
5977
5978 /*
5979 * Complete initializing the extent configuration with the
5980 * allocated ids assigned to this function. The bitmask serves
5981 * as an index into the array and manages the available ids. The
5982 * array just stores the ids communicated to the port via the wqes.
5983 */
5984 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5985 if ((i % 2) == 0)
5986 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5987 &id_array[k]);
5988 else
5989 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5990 &id_array[k]);
5991
5992 rsrc_blks = kzalloc(length, GFP_KERNEL);
5993 if (unlikely(!rsrc_blks)) {
5994 rc = -ENOMEM;
5995 kfree(bmask);
5996 kfree(ids);
5997 goto err_exit;
5998 }
5999 rsrc_blks->rsrc_start = rsrc_id;
6000 rsrc_blks->rsrc_size = rsrc_size;
6001 list_add_tail(&rsrc_blks->list, ext_blk_list);
6002 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08006003 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart5e5b5112019-01-28 11:14:22 -08006004 phba->sli4_hba.io_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08006005 lpfc_sli4_get_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08006006 }
James Smart6d368e52011-05-24 11:44:12 -04006007
6008 while (rsrc_id < (rsrc_start + rsrc_size)) {
6009 ids[j] = rsrc_id;
6010 rsrc_id++;
6011 j++;
6012 }
6013 /* Entire word processed. Get next word.*/
6014 if ((i % 2) == 1)
6015 k++;
6016 }
6017 err_exit:
6018 lpfc_sli4_mbox_cmd_free(phba, mbox);
6019 return rc;
6020}
6021
James Smart895427b2017-02-12 13:52:30 -08006022
6023
James Smart6d368e52011-05-24 11:44:12 -04006024/**
6025 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6026 * @phba: Pointer to HBA context object.
6027 * @type: the extent's type.
6028 *
6029 * This function deallocates all extents of a particular resource type.
6030 * SLI4 does not allow for deallocating a particular extent range. It
6031 * is the caller's responsibility to release all kernel memory resources.
6032 **/
6033static int
6034lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6035{
6036 int rc;
6037 uint32_t length, mbox_tmo = 0;
6038 LPFC_MBOXQ_t *mbox;
6039 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6040 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6041
6042 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6043 if (!mbox)
6044 return -ENOMEM;
6045
6046 /*
6047 * This function sends an embedded mailbox because it only sends the
6048 * the resource type. All extents of this type are released by the
6049 * port.
6050 */
6051 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6052 sizeof(struct lpfc_sli4_cfg_mhdr));
6053 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6054 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6055 length, LPFC_SLI4_MBX_EMBED);
6056
6057 /* Send an extents count of 0 - the dealloc doesn't use it. */
6058 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6059 LPFC_SLI4_MBX_EMBED);
6060 if (unlikely(rc)) {
6061 rc = -EIO;
6062 goto out_free_mbox;
6063 }
6064 if (!phba->sli4_hba.intr_enable)
6065 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6066 else {
James Smarta183a152011-10-10 21:32:43 -04006067 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006068 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6069 }
6070 if (unlikely(rc)) {
6071 rc = -EIO;
6072 goto out_free_mbox;
6073 }
6074
6075 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6076 if (bf_get(lpfc_mbox_hdr_status,
6077 &dealloc_rsrc->header.cfg_shdr.response)) {
6078 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6079 "2919 Failed to release resource extents "
6080 "for type %d - Status 0x%x Add'l Status 0x%x. "
6081 "Resource memory not released.\n",
6082 type,
6083 bf_get(lpfc_mbox_hdr_status,
6084 &dealloc_rsrc->header.cfg_shdr.response),
6085 bf_get(lpfc_mbox_hdr_add_status,
6086 &dealloc_rsrc->header.cfg_shdr.response));
6087 rc = -EIO;
6088 goto out_free_mbox;
6089 }
6090
6091 /* Release kernel memory resources for the specific type. */
6092 switch (type) {
6093 case LPFC_RSC_TYPE_FCOE_VPI:
6094 kfree(phba->vpi_bmask);
6095 kfree(phba->vpi_ids);
6096 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6097 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6098 &phba->lpfc_vpi_blk_list, list) {
6099 list_del_init(&rsrc_blk->list);
6100 kfree(rsrc_blk);
6101 }
James Smart16a3a202013-04-17 20:14:38 -04006102 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006103 break;
6104 case LPFC_RSC_TYPE_FCOE_XRI:
6105 kfree(phba->sli4_hba.xri_bmask);
6106 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006107 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6108 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6109 list_del_init(&rsrc_blk->list);
6110 kfree(rsrc_blk);
6111 }
6112 break;
6113 case LPFC_RSC_TYPE_FCOE_VFI:
6114 kfree(phba->sli4_hba.vfi_bmask);
6115 kfree(phba->sli4_hba.vfi_ids);
6116 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6117 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6118 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6119 list_del_init(&rsrc_blk->list);
6120 kfree(rsrc_blk);
6121 }
6122 break;
6123 case LPFC_RSC_TYPE_FCOE_RPI:
6124 /* RPI bitmask and physical id array are cleaned up earlier. */
6125 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6126 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6127 list_del_init(&rsrc_blk->list);
6128 kfree(rsrc_blk);
6129 }
6130 break;
6131 default:
6132 break;
6133 }
6134
6135 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6136
6137 out_free_mbox:
6138 mempool_free(mbox, phba->mbox_mem_pool);
6139 return rc;
6140}
6141
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006142static void
James Smart7bdedb32016-07-06 12:36:00 -07006143lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6144 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006145{
James Smart65791f12016-07-06 12:35:56 -07006146 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07006147
James Smart65791f12016-07-06 12:35:56 -07006148 len = sizeof(struct lpfc_mbx_set_feature) -
6149 sizeof(struct lpfc_sli4_cfg_mhdr);
6150 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6151 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6152 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006153
James Smart7bdedb32016-07-06 12:36:00 -07006154 switch (feature) {
6155 case LPFC_SET_UE_RECOVERY:
6156 bf_set(lpfc_mbx_set_feature_UER,
6157 &mbox->u.mqe.un.set_feature, 1);
6158 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6159 mbox->u.mqe.un.set_feature.param_len = 8;
6160 break;
6161 case LPFC_SET_MDS_DIAGS:
6162 bf_set(lpfc_mbx_set_feature_mds,
6163 &mbox->u.mqe.un.set_feature, 1);
6164 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006165 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006166 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6167 mbox->u.mqe.un.set_feature.param_len = 8;
6168 break;
James Smart65791f12016-07-06 12:35:56 -07006169 }
James Smart7bdedb32016-07-06 12:36:00 -07006170
6171 return;
James Smart65791f12016-07-06 12:35:56 -07006172}
6173
James Smart6d368e52011-05-24 11:44:12 -04006174/**
James Smart1165a5c2018-11-29 16:09:39 -08006175 * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6176 * @phba: Pointer to HBA context object.
6177 *
6178 * Disable FW logging into host memory on the adapter. To
6179 * be done before reading logs from the host memory.
6180 **/
6181void
6182lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6183{
6184 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6185
6186 ras_fwlog->ras_active = false;
6187
6188 /* Disable FW logging to host memory */
6189 writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6190 phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
6191}
6192
6193/**
James Smartd2cc9bc2018-09-10 10:30:50 -07006194 * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6195 * @phba: Pointer to HBA context object.
6196 *
6197 * This function is called to free memory allocated for RAS FW logging
6198 * support in the driver.
6199 **/
6200void
6201lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6202{
6203 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6204 struct lpfc_dmabuf *dmabuf, *next;
6205
6206 if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6207 list_for_each_entry_safe(dmabuf, next,
6208 &ras_fwlog->fwlog_buff_list,
6209 list) {
6210 list_del(&dmabuf->list);
6211 dma_free_coherent(&phba->pcidev->dev,
6212 LPFC_RAS_MAX_ENTRY_SIZE,
6213 dmabuf->virt, dmabuf->phys);
6214 kfree(dmabuf);
6215 }
6216 }
6217
6218 if (ras_fwlog->lwpd.virt) {
6219 dma_free_coherent(&phba->pcidev->dev,
6220 sizeof(uint32_t) * 2,
6221 ras_fwlog->lwpd.virt,
6222 ras_fwlog->lwpd.phys);
6223 ras_fwlog->lwpd.virt = NULL;
6224 }
6225
6226 ras_fwlog->ras_active = false;
6227}
6228
6229/**
6230 * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6231 * @phba: Pointer to HBA context object.
6232 * @fwlog_buff_count: Count of buffers to be created.
6233 *
6234 * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6235 * to update FW log is posted to the adapter.
6236 * Buffer count is calculated based on module param ras_fwlog_buffsize
6237 * Size of each buffer posted to FW is 64K.
6238 **/
6239
6240static int
6241lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6242 uint32_t fwlog_buff_count)
6243{
6244 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6245 struct lpfc_dmabuf *dmabuf;
6246 int rc = 0, i = 0;
6247
6248 /* Initialize List */
6249 INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6250
6251 /* Allocate memory for the LWPD */
6252 ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6253 sizeof(uint32_t) * 2,
6254 &ras_fwlog->lwpd.phys,
6255 GFP_KERNEL);
6256 if (!ras_fwlog->lwpd.virt) {
James Smartcb349902018-11-29 16:09:27 -08006257 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006258 "6185 LWPD Memory Alloc Failed\n");
6259
6260 return -ENOMEM;
6261 }
6262
6263 ras_fwlog->fw_buffcount = fwlog_buff_count;
6264 for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6265 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6266 GFP_KERNEL);
6267 if (!dmabuf) {
6268 rc = -ENOMEM;
6269 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6270 "6186 Memory Alloc failed FW logging");
6271 goto free_mem;
6272 }
6273
Luis Chamberlain750afb02019-01-04 09:23:09 +01006274 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartd2cc9bc2018-09-10 10:30:50 -07006275 LPFC_RAS_MAX_ENTRY_SIZE,
Luis Chamberlain750afb02019-01-04 09:23:09 +01006276 &dmabuf->phys, GFP_KERNEL);
James Smartd2cc9bc2018-09-10 10:30:50 -07006277 if (!dmabuf->virt) {
6278 kfree(dmabuf);
6279 rc = -ENOMEM;
6280 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6281 "6187 DMA Alloc Failed FW logging");
6282 goto free_mem;
6283 }
James Smartd2cc9bc2018-09-10 10:30:50 -07006284 dmabuf->buffer_tag = i;
6285 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6286 }
6287
6288free_mem:
6289 if (rc)
6290 lpfc_sli4_ras_dma_free(phba);
6291
6292 return rc;
6293}
6294
6295/**
6296 * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6297 * @phba: pointer to lpfc hba data structure.
6298 * @pmboxq: pointer to the driver internal queue element for mailbox command.
6299 *
6300 * Completion handler for driver's RAS MBX command to the device.
6301 **/
6302static void
6303lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6304{
6305 MAILBOX_t *mb;
6306 union lpfc_sli4_cfg_shdr *shdr;
6307 uint32_t shdr_status, shdr_add_status;
6308 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6309
6310 mb = &pmb->u.mb;
6311
6312 shdr = (union lpfc_sli4_cfg_shdr *)
6313 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6314 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6315 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6316
6317 if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
James Smartcb349902018-11-29 16:09:27 -08006318 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smartd2cc9bc2018-09-10 10:30:50 -07006319 "6188 FW LOG mailbox "
6320 "completed with status x%x add_status x%x,"
6321 " mbx status x%x\n",
6322 shdr_status, shdr_add_status, mb->mbxStatus);
James Smartcb349902018-11-29 16:09:27 -08006323
6324 ras_fwlog->ras_hwsupport = false;
James Smartd2cc9bc2018-09-10 10:30:50 -07006325 goto disable_ras;
6326 }
6327
6328 ras_fwlog->ras_active = true;
6329 mempool_free(pmb, phba->mbox_mem_pool);
6330
6331 return;
6332
6333disable_ras:
6334 /* Free RAS DMA memory */
6335 lpfc_sli4_ras_dma_free(phba);
6336 mempool_free(pmb, phba->mbox_mem_pool);
6337}
6338
6339/**
6340 * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6341 * @phba: pointer to lpfc hba data structure.
6342 * @fwlog_level: Logging verbosity level.
6343 * @fwlog_enable: Enable/Disable logging.
6344 *
6345 * Initialize memory and post mailbox command to enable FW logging in host
6346 * memory.
6347 **/
6348int
6349lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6350 uint32_t fwlog_level,
6351 uint32_t fwlog_enable)
6352{
6353 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6354 struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6355 struct lpfc_dmabuf *dmabuf;
6356 LPFC_MBOXQ_t *mbox;
6357 uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6358 int rc = 0;
6359
6360 fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6361 phba->cfg_ras_fwlog_buffsize);
6362 fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6363
6364 /*
6365 * If re-enabling FW logging support use earlier allocated
6366 * DMA buffers while posting MBX command.
6367 **/
6368 if (!ras_fwlog->lwpd.virt) {
6369 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6370 if (rc) {
6371 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smartcb349902018-11-29 16:09:27 -08006372 "6189 FW Log Memory Allocation Failed");
James Smartd2cc9bc2018-09-10 10:30:50 -07006373 return rc;
6374 }
6375 }
6376
6377 /* Setup Mailbox command */
6378 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6379 if (!mbox) {
James Smartcb349902018-11-29 16:09:27 -08006380 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006381 "6190 RAS MBX Alloc Failed");
6382 rc = -ENOMEM;
6383 goto mem_free;
6384 }
6385
6386 ras_fwlog->fw_loglevel = fwlog_level;
6387 len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6388 sizeof(struct lpfc_sli4_cfg_mhdr));
6389
6390 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6391 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6392 len, LPFC_SLI4_MBX_EMBED);
6393
6394 mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6395 bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6396 fwlog_enable);
6397 bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6398 ras_fwlog->fw_loglevel);
6399 bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6400 ras_fwlog->fw_buffcount);
6401 bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6402 LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6403
6404 /* Update DMA buffer address */
6405 list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6406 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6407
6408 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6409 putPaddrLow(dmabuf->phys);
6410
6411 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
6412 putPaddrHigh(dmabuf->phys);
6413 }
6414
6415 /* Update LPWD address */
6416 mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
6417 mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
6418
6419 mbox->vport = phba->pport;
6420 mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
6421
6422 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6423
6424 if (rc == MBX_NOT_FINISHED) {
James Smartcb349902018-11-29 16:09:27 -08006425 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6426 "6191 FW-Log Mailbox failed. "
James Smartd2cc9bc2018-09-10 10:30:50 -07006427 "status %d mbxStatus : x%x", rc,
6428 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6429 mempool_free(mbox, phba->mbox_mem_pool);
6430 rc = -EIO;
6431 goto mem_free;
6432 } else
6433 rc = 0;
6434mem_free:
6435 if (rc)
6436 lpfc_sli4_ras_dma_free(phba);
6437
6438 return rc;
6439}
6440
6441/**
6442 * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
6443 * @phba: Pointer to HBA context object.
6444 *
6445 * Check if RAS is supported on the adapter and initialize it.
6446 **/
6447void
6448lpfc_sli4_ras_setup(struct lpfc_hba *phba)
6449{
6450 /* Check RAS FW Log needs to be enabled or not */
6451 if (lpfc_check_fwlog_support(phba))
6452 return;
6453
6454 lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
6455 LPFC_RAS_ENABLE_LOGGING);
6456}
6457
6458/**
James Smart6d368e52011-05-24 11:44:12 -04006459 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6460 * @phba: Pointer to HBA context object.
6461 *
6462 * This function allocates all SLI4 resource identifiers.
6463 **/
6464int
6465lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6466{
6467 int i, rc, error = 0;
6468 uint16_t count, base;
6469 unsigned long longs;
6470
James Smartff78d8f2011-12-13 13:21:35 -05006471 if (!phba->sli4_hba.rpi_hdrs_in_use)
6472 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04006473 if (phba->sli4_hba.extents_in_use) {
6474 /*
6475 * The port supports resource extents. The XRI, VPI, VFI, RPI
6476 * resource extent count must be read and allocated before
6477 * provisioning the resource id arrays.
6478 */
6479 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6480 LPFC_IDX_RSRC_RDY) {
6481 /*
6482 * Extent-based resources are set - the driver could
6483 * be in a port reset. Figure out if any corrective
6484 * actions need to be taken.
6485 */
6486 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6487 LPFC_RSC_TYPE_FCOE_VFI);
6488 if (rc != 0)
6489 error++;
6490 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6491 LPFC_RSC_TYPE_FCOE_VPI);
6492 if (rc != 0)
6493 error++;
6494 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6495 LPFC_RSC_TYPE_FCOE_XRI);
6496 if (rc != 0)
6497 error++;
6498 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6499 LPFC_RSC_TYPE_FCOE_RPI);
6500 if (rc != 0)
6501 error++;
6502
6503 /*
6504 * It's possible that the number of resources
6505 * provided to this port instance changed between
6506 * resets. Detect this condition and reallocate
6507 * resources. Otherwise, there is no action.
6508 */
6509 if (error) {
6510 lpfc_printf_log(phba, KERN_INFO,
6511 LOG_MBOX | LOG_INIT,
6512 "2931 Detected extent resource "
6513 "change. Reallocating all "
6514 "extents.\n");
6515 rc = lpfc_sli4_dealloc_extent(phba,
6516 LPFC_RSC_TYPE_FCOE_VFI);
6517 rc = lpfc_sli4_dealloc_extent(phba,
6518 LPFC_RSC_TYPE_FCOE_VPI);
6519 rc = lpfc_sli4_dealloc_extent(phba,
6520 LPFC_RSC_TYPE_FCOE_XRI);
6521 rc = lpfc_sli4_dealloc_extent(phba,
6522 LPFC_RSC_TYPE_FCOE_RPI);
6523 } else
6524 return 0;
6525 }
6526
6527 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6528 if (unlikely(rc))
6529 goto err_exit;
6530
6531 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6532 if (unlikely(rc))
6533 goto err_exit;
6534
6535 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6536 if (unlikely(rc))
6537 goto err_exit;
6538
6539 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6540 if (unlikely(rc))
6541 goto err_exit;
6542 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6543 LPFC_IDX_RSRC_RDY);
6544 return rc;
6545 } else {
6546 /*
6547 * The port does not support resource extents. The XRI, VPI,
6548 * VFI, RPI resource ids were determined from READ_CONFIG.
6549 * Just allocate the bitmasks and provision the resource id
6550 * arrays. If a port reset is active, the resources don't
6551 * need any action - just exit.
6552 */
6553 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006554 LPFC_IDX_RSRC_RDY) {
6555 lpfc_sli4_dealloc_resource_identifiers(phba);
6556 lpfc_sli4_remove_rpis(phba);
6557 }
James Smart6d368e52011-05-24 11:44:12 -04006558 /* RPIs. */
6559 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006560 if (count <= 0) {
6561 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6562 "3279 Invalid provisioning of "
6563 "rpi:%d\n", count);
6564 rc = -EINVAL;
6565 goto err_exit;
6566 }
James Smart6d368e52011-05-24 11:44:12 -04006567 base = phba->sli4_hba.max_cfg_param.rpi_base;
6568 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006569 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006570 sizeof(unsigned long),
6571 GFP_KERNEL);
6572 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6573 rc = -ENOMEM;
6574 goto err_exit;
6575 }
Kees Cook6396bb22018-06-12 14:03:40 -07006576 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006577 GFP_KERNEL);
6578 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6579 rc = -ENOMEM;
6580 goto free_rpi_bmask;
6581 }
6582
6583 for (i = 0; i < count; i++)
6584 phba->sli4_hba.rpi_ids[i] = base + i;
6585
6586 /* VPIs. */
6587 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006588 if (count <= 0) {
6589 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6590 "3280 Invalid provisioning of "
6591 "vpi:%d\n", count);
6592 rc = -EINVAL;
6593 goto free_rpi_ids;
6594 }
James Smart6d368e52011-05-24 11:44:12 -04006595 base = phba->sli4_hba.max_cfg_param.vpi_base;
6596 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006597 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006598 GFP_KERNEL);
6599 if (unlikely(!phba->vpi_bmask)) {
6600 rc = -ENOMEM;
6601 goto free_rpi_ids;
6602 }
Kees Cook6396bb22018-06-12 14:03:40 -07006603 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006604 GFP_KERNEL);
6605 if (unlikely(!phba->vpi_ids)) {
6606 rc = -ENOMEM;
6607 goto free_vpi_bmask;
6608 }
6609
6610 for (i = 0; i < count; i++)
6611 phba->vpi_ids[i] = base + i;
6612
6613 /* XRIs. */
6614 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006615 if (count <= 0) {
6616 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6617 "3281 Invalid provisioning of "
6618 "xri:%d\n", count);
6619 rc = -EINVAL;
6620 goto free_vpi_ids;
6621 }
James Smart6d368e52011-05-24 11:44:12 -04006622 base = phba->sli4_hba.max_cfg_param.xri_base;
6623 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006624 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006625 sizeof(unsigned long),
6626 GFP_KERNEL);
6627 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6628 rc = -ENOMEM;
6629 goto free_vpi_ids;
6630 }
James Smart41899be2012-03-01 22:34:19 -05006631 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006632 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006633 GFP_KERNEL);
6634 if (unlikely(!phba->sli4_hba.xri_ids)) {
6635 rc = -ENOMEM;
6636 goto free_xri_bmask;
6637 }
6638
6639 for (i = 0; i < count; i++)
6640 phba->sli4_hba.xri_ids[i] = base + i;
6641
6642 /* VFIs. */
6643 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006644 if (count <= 0) {
6645 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6646 "3282 Invalid provisioning of "
6647 "vfi:%d\n", count);
6648 rc = -EINVAL;
6649 goto free_xri_ids;
6650 }
James Smart6d368e52011-05-24 11:44:12 -04006651 base = phba->sli4_hba.max_cfg_param.vfi_base;
6652 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006653 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006654 sizeof(unsigned long),
6655 GFP_KERNEL);
6656 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6657 rc = -ENOMEM;
6658 goto free_xri_ids;
6659 }
Kees Cook6396bb22018-06-12 14:03:40 -07006660 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006661 GFP_KERNEL);
6662 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6663 rc = -ENOMEM;
6664 goto free_vfi_bmask;
6665 }
6666
6667 for (i = 0; i < count; i++)
6668 phba->sli4_hba.vfi_ids[i] = base + i;
6669
6670 /*
6671 * Mark all resources ready. An HBA reset doesn't need
6672 * to reset the initialization.
6673 */
6674 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6675 LPFC_IDX_RSRC_RDY);
6676 return 0;
6677 }
6678
6679 free_vfi_bmask:
6680 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006681 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006682 free_xri_ids:
6683 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006684 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006685 free_xri_bmask:
6686 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006687 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006688 free_vpi_ids:
6689 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006690 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006691 free_vpi_bmask:
6692 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006693 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006694 free_rpi_ids:
6695 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006696 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006697 free_rpi_bmask:
6698 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006699 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006700 err_exit:
6701 return rc;
6702}
6703
6704/**
6705 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6706 * @phba: Pointer to HBA context object.
6707 *
6708 * This function allocates the number of elements for the specified
6709 * resource type.
6710 **/
6711int
6712lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6713{
6714 if (phba->sli4_hba.extents_in_use) {
6715 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6716 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6717 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6718 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6719 } else {
6720 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006721 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006722 kfree(phba->vpi_ids);
6723 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6724 kfree(phba->sli4_hba.xri_bmask);
6725 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006726 kfree(phba->sli4_hba.vfi_bmask);
6727 kfree(phba->sli4_hba.vfi_ids);
6728 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6729 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6730 }
6731
6732 return 0;
6733}
6734
6735/**
James Smartb76f2dc2011-07-22 18:37:42 -04006736 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6737 * @phba: Pointer to HBA context object.
6738 * @type: The resource extent type.
6739 * @extnt_count: buffer to hold port extent count response
6740 * @extnt_size: buffer to hold port extent size response.
6741 *
6742 * This function calls the port to read the host allocated extents
6743 * for a particular type.
6744 **/
6745int
6746lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6747 uint16_t *extnt_cnt, uint16_t *extnt_size)
6748{
6749 bool emb;
6750 int rc = 0;
6751 uint16_t curr_blks = 0;
6752 uint32_t req_len, emb_len;
6753 uint32_t alloc_len, mbox_tmo;
6754 struct list_head *blk_list_head;
6755 struct lpfc_rsrc_blks *rsrc_blk;
6756 LPFC_MBOXQ_t *mbox;
6757 void *virtaddr = NULL;
6758 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6759 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6760 union lpfc_sli4_cfg_shdr *shdr;
6761
6762 switch (type) {
6763 case LPFC_RSC_TYPE_FCOE_VPI:
6764 blk_list_head = &phba->lpfc_vpi_blk_list;
6765 break;
6766 case LPFC_RSC_TYPE_FCOE_XRI:
6767 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6768 break;
6769 case LPFC_RSC_TYPE_FCOE_VFI:
6770 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6771 break;
6772 case LPFC_RSC_TYPE_FCOE_RPI:
6773 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6774 break;
6775 default:
6776 return -EIO;
6777 }
6778
6779 /* Count the number of extents currently allocatd for this type. */
6780 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6781 if (curr_blks == 0) {
6782 /*
6783 * The GET_ALLOCATED mailbox does not return the size,
6784 * just the count. The size should be just the size
6785 * stored in the current allocated block and all sizes
6786 * for an extent type are the same so set the return
6787 * value now.
6788 */
6789 *extnt_size = rsrc_blk->rsrc_size;
6790 }
6791 curr_blks++;
6792 }
6793
James Smartb76f2dc2011-07-22 18:37:42 -04006794 /*
6795 * Calculate the size of an embedded mailbox. The uint32_t
6796 * accounts for extents-specific word.
6797 */
6798 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6799 sizeof(uint32_t);
6800
6801 /*
6802 * Presume the allocation and response will fit into an embedded
6803 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6804 */
6805 emb = LPFC_SLI4_MBX_EMBED;
6806 req_len = emb_len;
6807 if (req_len > emb_len) {
6808 req_len = curr_blks * sizeof(uint16_t) +
6809 sizeof(union lpfc_sli4_cfg_shdr) +
6810 sizeof(uint32_t);
6811 emb = LPFC_SLI4_MBX_NEMBED;
6812 }
6813
6814 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6815 if (!mbox)
6816 return -ENOMEM;
6817 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6818
6819 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6820 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6821 req_len, emb);
6822 if (alloc_len < req_len) {
6823 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6824 "2983 Allocated DMA memory size (x%x) is "
6825 "less than the requested DMA memory "
6826 "size (x%x)\n", alloc_len, req_len);
6827 rc = -ENOMEM;
6828 goto err_exit;
6829 }
6830 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6831 if (unlikely(rc)) {
6832 rc = -EIO;
6833 goto err_exit;
6834 }
6835
6836 if (!phba->sli4_hba.intr_enable)
6837 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6838 else {
James Smarta183a152011-10-10 21:32:43 -04006839 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006840 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6841 }
6842
6843 if (unlikely(rc)) {
6844 rc = -EIO;
6845 goto err_exit;
6846 }
6847
6848 /*
6849 * Figure out where the response is located. Then get local pointers
6850 * to the response data. The port does not guarantee to respond to
6851 * all extents counts request so update the local variable with the
6852 * allocated count from the port.
6853 */
6854 if (emb == LPFC_SLI4_MBX_EMBED) {
6855 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6856 shdr = &rsrc_ext->header.cfg_shdr;
6857 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6858 } else {
6859 virtaddr = mbox->sge_array->addr[0];
6860 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6861 shdr = &n_rsrc->cfg_shdr;
6862 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6863 }
6864
6865 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6866 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6867 "2984 Failed to read allocated resources "
6868 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6869 type,
6870 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6871 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6872 rc = -EIO;
6873 goto err_exit;
6874 }
6875 err_exit:
6876 lpfc_sli4_mbox_cmd_free(phba, mbox);
6877 return rc;
6878}
6879
6880/**
James Smart0ef69962017-04-21 16:04:50 -07006881 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006882 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006883 * @pring: Pointer to driver SLI ring object.
6884 * @sgl_list: linked link of sgl buffers to post
6885 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006886 *
James Smart895427b2017-02-12 13:52:30 -08006887 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006888 * repost them to the port by using SGL block post. This is needed after a
6889 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006890 * of buffer sgls which contains contiguous xris and uses the non-embedded
6891 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006892 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6893 * mailbox command for posting.
6894 *
6895 * Returns: 0 = success, non-zero failure.
6896 **/
6897static int
James Smart895427b2017-02-12 13:52:30 -08006898lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6899 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006900{
6901 struct lpfc_sglq *sglq_entry = NULL;
6902 struct lpfc_sglq *sglq_entry_next = NULL;
6903 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006904 int status, total_cnt;
6905 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006906 int last_xritag = NO_XRI;
6907 LIST_HEAD(prep_sgl_list);
6908 LIST_HEAD(blck_sgl_list);
6909 LIST_HEAD(allc_sgl_list);
6910 LIST_HEAD(post_sgl_list);
6911 LIST_HEAD(free_sgl_list);
6912
James Smart38c20672013-03-01 16:37:44 -05006913 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006914 spin_lock(&phba->sli4_hba.sgl_list_lock);
6915 list_splice_init(sgl_list, &allc_sgl_list);
6916 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006917 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006918
James Smart895427b2017-02-12 13:52:30 -08006919 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006920 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6921 &allc_sgl_list, list) {
6922 list_del_init(&sglq_entry->list);
6923 block_cnt++;
6924 if ((last_xritag != NO_XRI) &&
6925 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6926 /* a hole in xri block, form a sgl posting block */
6927 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6928 post_cnt = block_cnt - 1;
6929 /* prepare list for next posting block */
6930 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6931 block_cnt = 1;
6932 } else {
6933 /* prepare list for next posting block */
6934 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6935 /* enough sgls for non-embed sgl mbox command */
6936 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6937 list_splice_init(&prep_sgl_list,
6938 &blck_sgl_list);
6939 post_cnt = block_cnt;
6940 block_cnt = 0;
6941 }
6942 }
6943 num_posted++;
6944
6945 /* keep track of last sgl's xritag */
6946 last_xritag = sglq_entry->sli4_xritag;
6947
James Smart895427b2017-02-12 13:52:30 -08006948 /* end of repost sgl list condition for buffers */
6949 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006950 if (post_cnt == 0) {
6951 list_splice_init(&prep_sgl_list,
6952 &blck_sgl_list);
6953 post_cnt = block_cnt;
6954 } else if (block_cnt == 1) {
6955 status = lpfc_sli4_post_sgl(phba,
6956 sglq_entry->phys, 0,
6957 sglq_entry->sli4_xritag);
6958 if (!status) {
6959 /* successful, put sgl to posted list */
6960 list_add_tail(&sglq_entry->list,
6961 &post_sgl_list);
6962 } else {
6963 /* Failure, put sgl to free list */
6964 lpfc_printf_log(phba, KERN_WARNING,
6965 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006966 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006967 "sgl, xritag:x%x\n",
6968 sglq_entry->sli4_xritag);
6969 list_add_tail(&sglq_entry->list,
6970 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006971 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006972 }
6973 }
6974 }
6975
6976 /* continue until a nembed page worth of sgls */
6977 if (post_cnt == 0)
6978 continue;
6979
James Smart895427b2017-02-12 13:52:30 -08006980 /* post the buffer list sgls as a block */
6981 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6982 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006983
6984 if (!status) {
6985 /* success, put sgl list to posted sgl list */
6986 list_splice_init(&blck_sgl_list, &post_sgl_list);
6987 } else {
6988 /* Failure, put sgl list to free sgl list */
6989 sglq_entry_first = list_first_entry(&blck_sgl_list,
6990 struct lpfc_sglq,
6991 list);
6992 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006993 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04006994 "xritag:x%x-x%x\n",
6995 sglq_entry_first->sli4_xritag,
6996 (sglq_entry_first->sli4_xritag +
6997 post_cnt - 1));
6998 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006999 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007000 }
7001
7002 /* don't reset xirtag due to hole in xri block */
7003 if (block_cnt == 0)
7004 last_xritag = NO_XRI;
7005
James Smart895427b2017-02-12 13:52:30 -08007006 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04007007 post_cnt = 0;
7008 }
7009
James Smart895427b2017-02-12 13:52:30 -08007010 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04007011 lpfc_free_sgl_list(phba, &free_sgl_list);
7012
James Smart895427b2017-02-12 13:52:30 -08007013 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04007014 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05007015 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007016 spin_lock(&phba->sli4_hba.sgl_list_lock);
7017 list_splice_init(&post_sgl_list, sgl_list);
7018 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05007019 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04007020 } else {
7021 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007022 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04007023 return -EIO;
7024 }
James Smart895427b2017-02-12 13:52:30 -08007025
7026 /* return the number of XRIs actually posted */
7027 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007028}
7029
James Smart0794d602019-01-28 11:14:19 -08007030/**
James Smart5e5b5112019-01-28 11:14:22 -08007031 * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
James Smart0794d602019-01-28 11:14:19 -08007032 * @phba: pointer to lpfc hba data structure.
7033 *
7034 * This routine walks the list of nvme buffers that have been allocated and
7035 * repost them to the port by using SGL block post. This is needed after a
7036 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7037 * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
James Smart5e5b5112019-01-28 11:14:22 -08007038 * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
James Smart0794d602019-01-28 11:14:19 -08007039 *
7040 * Returns: 0 = success, non-zero failure.
7041 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07007042static int
James Smart5e5b5112019-01-28 11:14:22 -08007043lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
James Smart0794d602019-01-28 11:14:19 -08007044{
7045 LIST_HEAD(post_nblist);
7046 int num_posted, rc = 0;
7047
7048 /* get all NVME buffers need to repost to a local list */
James Smart5e5b5112019-01-28 11:14:22 -08007049 lpfc_io_buf_flush(phba, &post_nblist);
James Smart0794d602019-01-28 11:14:19 -08007050
7051 /* post the list of nvme buffer sgls to port if available */
7052 if (!list_empty(&post_nblist)) {
James Smart5e5b5112019-01-28 11:14:22 -08007053 num_posted = lpfc_sli4_post_io_sgl_list(
7054 phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
James Smart0794d602019-01-28 11:14:19 -08007055 /* failed to post any nvme buffer, return error */
7056 if (num_posted == 0)
7057 rc = -EIO;
7058 }
7059 return rc;
7060}
7061
Bart Van Assche3999df72019-03-28 11:06:16 -07007062static void
James Smart61bda8f2016-10-13 15:06:05 -07007063lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7064{
7065 uint32_t len;
7066
7067 len = sizeof(struct lpfc_mbx_set_host_data) -
7068 sizeof(struct lpfc_sli4_cfg_mhdr);
7069 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7070 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7071 LPFC_SLI4_MBX_EMBED);
7072
7073 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08007074 mbox->u.mqe.un.set_host_data.param_len =
7075 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07007076 snprintf(mbox->u.mqe.un.set_host_data.data,
7077 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7078 "Linux %s v"LPFC_DRIVER_VERSION,
7079 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7080}
7081
James Smarta8cf5df2017-05-15 15:20:46 -07007082int
James Smart6c621a22017-05-15 15:20:45 -07007083lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07007084 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07007085{
7086 int rc, i;
7087 struct lpfc_rqe hrqe;
7088 struct lpfc_rqe drqe;
7089 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08007090 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07007091 struct rqb_dmabuf *rqb_buffer;
7092 LIST_HEAD(rqb_buf_list);
7093
James Smart411de512018-01-30 15:58:52 -08007094 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007095 rqbp = hrq->rqbp;
7096 for (i = 0; i < count; i++) {
7097 /* IF RQ is already full, don't bother */
7098 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
7099 break;
7100 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7101 if (!rqb_buffer)
7102 break;
7103 rqb_buffer->hrq = hrq;
7104 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07007105 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07007106 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7107 }
7108 while (!list_empty(&rqb_buf_list)) {
7109 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7110 hbuf.list);
7111
7112 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7113 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7114 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7115 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7116 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7117 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08007118 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7119 "6421 Cannot post to HRQ %d: %x %x %x "
7120 "DRQ %x %x\n",
7121 hrq->queue_id,
7122 hrq->host_index,
7123 hrq->hba_index,
7124 hrq->entry_count,
7125 drq->host_index,
7126 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07007127 rqbp->rqb_free_buffer(phba, rqb_buffer);
7128 } else {
7129 list_add_tail(&rqb_buffer->hbuf.list,
7130 &rqbp->rqb_buffer_list);
7131 rqbp->buffer_count++;
7132 }
7133 }
James Smart411de512018-01-30 15:58:52 -08007134 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007135 return 1;
7136}
7137
James Smart8a9d2e82012-05-09 21:16:12 -04007138/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08007139 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04007140 * @phba: Pointer to HBA context object.
7141 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08007142 * This function is the main SLI4 device initialization PCI function. This
7143 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04007144 * HBA error attention handler code. Caller is not required to hold any
7145 * locks.
7146 **/
7147int
7148lpfc_sli4_hba_setup(struct lpfc_hba *phba)
7149{
James Smartc4908502019-01-28 11:14:28 -08007150 int rc, i, cnt, len;
James Smartda0436e2009-05-22 14:51:39 -04007151 LPFC_MBOXQ_t *mboxq;
7152 struct lpfc_mqe *mqe;
7153 uint8_t *vpd;
7154 uint32_t vpd_size;
7155 uint32_t ftr_rsp = 0;
7156 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
7157 struct lpfc_vport *vport = phba->pport;
7158 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08007159 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04007160
7161 /* Perform a PCI function reset to start from clean */
7162 rc = lpfc_pci_function_reset(phba);
7163 if (unlikely(rc))
7164 return -ENODEV;
7165
7166 /* Check the HBA Host Status Register for readyness */
7167 rc = lpfc_sli4_post_status_check(phba);
7168 if (unlikely(rc))
7169 return -ENODEV;
7170 else {
7171 spin_lock_irq(&phba->hbalock);
7172 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
7173 spin_unlock_irq(&phba->hbalock);
7174 }
7175
7176 /*
7177 * Allocate a single mailbox container for initializing the
7178 * port.
7179 */
7180 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7181 if (!mboxq)
7182 return -ENOMEM;
7183
James Smartda0436e2009-05-22 14:51:39 -04007184 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04007185 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04007186 vpd = kzalloc(vpd_size, GFP_KERNEL);
7187 if (!vpd) {
7188 rc = -ENOMEM;
7189 goto out_free_mbox;
7190 }
7191
7192 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05007193 if (unlikely(rc)) {
7194 kfree(vpd);
7195 goto out_free_mbox;
7196 }
James Smart572709e2013-07-15 18:32:43 -04007197
James Smartda0436e2009-05-22 14:51:39 -04007198 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04007199 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07007200 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05007201 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007202 phba->fcp_embed_io = 0; /* SLI4 FC support only */
7203 } else {
James Smart76a95d72010-11-20 23:11:48 -05007204 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007205 }
James Smart45ed1192009-10-02 15:17:02 -04007206
7207 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
7208 LPFC_DCBX_CEE_MODE)
7209 phba->hba_flag |= HBA_FIP_SUPPORT;
7210 else
7211 phba->hba_flag &= ~HBA_FIP_SUPPORT;
7212
James Smartc00f62e2019-08-14 16:57:11 -07007213 phba->hba_flag &= ~HBA_IOQ_FLUSH;
James Smart4f2e66c2012-05-09 21:17:07 -04007214
James Smartc31098c2011-04-16 11:03:33 -04007215 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007216 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7217 "0376 READ_REV Error. SLI Level %d "
7218 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05007219 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04007220 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007221 kfree(vpd);
7222 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007223 }
James Smartcd1c8302011-10-10 21:33:25 -04007224
7225 /*
James Smartff78d8f2011-12-13 13:21:35 -05007226 * Continue initialization with default values even if driver failed
7227 * to read FCoE param config regions, only read parameters if the
7228 * board is FCoE
7229 */
7230 if (phba->hba_flag & HBA_FCOE_MODE &&
7231 lpfc_sli4_read_fcoe_params(phba))
7232 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
7233 "2570 Failed to read FCoE parameters\n");
7234
7235 /*
James Smartcd1c8302011-10-10 21:33:25 -04007236 * Retrieve sli4 device physical port name, failure of doing it
7237 * is considered as non-fatal.
7238 */
7239 rc = lpfc_sli4_retrieve_pport_name(phba);
7240 if (!rc)
7241 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7242 "3080 Successful retrieving SLI4 device "
7243 "physical port name: %s.\n", phba->Port);
7244
James Smartb3b4f3e2019-03-12 16:30:23 -07007245 rc = lpfc_sli4_get_ctl_attr(phba);
7246 if (!rc)
7247 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7248 "8351 Successful retrieving SLI4 device "
7249 "CTL ATTR\n");
7250
James Smartda0436e2009-05-22 14:51:39 -04007251 /*
7252 * Evaluate the read rev and vpd data. Populate the driver
7253 * state with the results. If this routine fails, the failure
7254 * is not fatal as the driver will use generic values.
7255 */
7256 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
7257 if (unlikely(!rc)) {
7258 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7259 "0377 Error %d parsing vpd. "
7260 "Using defaults.\n", rc);
7261 rc = 0;
7262 }
James Smart76a95d72010-11-20 23:11:48 -05007263 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04007264
James Smartf1126682009-06-10 17:22:44 -04007265 /* Save information as VPD data */
7266 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
7267 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
James Smart4e565cf2018-02-22 08:18:50 -08007268
7269 /*
7270 * This is because first G7 ASIC doesn't support the standard
7271 * 0x5a NVME cmd descriptor type/subtype
7272 */
7273 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7274 LPFC_SLI_INTF_IF_TYPE_6) &&
7275 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
7276 (phba->vpd.rev.smRev == 0) &&
7277 (phba->cfg_nvme_embed_cmd == 1))
7278 phba->cfg_nvme_embed_cmd = 0;
7279
James Smartf1126682009-06-10 17:22:44 -04007280 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
7281 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
7282 &mqe->un.read_rev);
7283 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
7284 &mqe->un.read_rev);
7285 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
7286 &mqe->un.read_rev);
7287 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
7288 &mqe->un.read_rev);
7289 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
7290 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
7291 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
7292 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
7293 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
7294 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
7295 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7296 "(%d):0380 READ_REV Status x%x "
7297 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
7298 mboxq->vport ? mboxq->vport->vpi : 0,
7299 bf_get(lpfc_mqe_status, mqe),
7300 phba->vpd.rev.opFwName,
7301 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
7302 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04007303
James Smart572709e2013-07-15 18:32:43 -04007304 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
7305 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
7306 if (phba->pport->cfg_lun_queue_depth > rc) {
7307 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7308 "3362 LUN queue depth changed from %d to %d\n",
7309 phba->pport->cfg_lun_queue_depth, rc);
7310 phba->pport->cfg_lun_queue_depth = rc;
7311 }
7312
James Smart65791f12016-07-06 12:35:56 -07007313 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07007314 LPFC_SLI_INTF_IF_TYPE_0) {
7315 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
7316 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7317 if (rc == MBX_SUCCESS) {
7318 phba->hba_flag |= HBA_RECOVERABLE_UE;
7319 /* Set 1Sec interval to detect UE */
7320 phba->eratt_poll_interval = 1;
7321 phba->sli4_hba.ue_to_sr = bf_get(
7322 lpfc_mbx_set_feature_UESR,
7323 &mboxq->u.mqe.un.set_feature);
7324 phba->sli4_hba.ue_to_rp = bf_get(
7325 lpfc_mbx_set_feature_UERP,
7326 &mboxq->u.mqe.un.set_feature);
7327 }
7328 }
7329
7330 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
7331 /* Enable MDS Diagnostics only if the SLI Port supports it */
7332 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
7333 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7334 if (rc != MBX_SUCCESS)
7335 phba->mds_diags_support = 0;
7336 }
James Smart572709e2013-07-15 18:32:43 -04007337
James Smartda0436e2009-05-22 14:51:39 -04007338 /*
7339 * Discover the port's supported feature set and match it against the
7340 * hosts requests.
7341 */
7342 lpfc_request_features(phba, mboxq);
7343 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7344 if (unlikely(rc)) {
7345 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007346 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007347 }
7348
7349 /*
7350 * The port must support FCP initiator mode as this is the
7351 * only mode running in the host.
7352 */
7353 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7354 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7355 "0378 No support for fcpi mode.\n");
7356 ftr_rsp++;
7357 }
James Smart0bc2b7c2018-02-22 08:18:48 -08007358
7359 /* Performance Hints are ONLY for FCoE */
7360 if (phba->hba_flag & HBA_FCOE_MODE) {
7361 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7362 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7363 else
7364 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7365 }
7366
James Smartda0436e2009-05-22 14:51:39 -04007367 /*
7368 * If the port cannot support the host's requested features
7369 * then turn off the global config parameters to disable the
7370 * feature in the driver. This is not a fatal error.
7371 */
James Smartf44ac122018-03-05 12:04:08 -08007372 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7373 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7374 phba->cfg_enable_bg = 0;
7375 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smartbf086112011-08-21 21:48:13 -04007376 ftr_rsp++;
James Smartf44ac122018-03-05 12:04:08 -08007377 }
James Smartbf086112011-08-21 21:48:13 -04007378 }
James Smartda0436e2009-05-22 14:51:39 -04007379
7380 if (phba->max_vpi && phba->cfg_enable_npiv &&
7381 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7382 ftr_rsp++;
7383
7384 if (ftr_rsp) {
7385 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7386 "0379 Feature Mismatch Data: x%08x %08x "
7387 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7388 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7389 phba->cfg_enable_npiv, phba->max_vpi);
7390 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7391 phba->cfg_enable_bg = 0;
7392 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7393 phba->cfg_enable_npiv = 0;
7394 }
7395
7396 /* These SLI3 features are assumed in SLI4 */
7397 spin_lock_irq(&phba->hbalock);
7398 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7399 spin_unlock_irq(&phba->hbalock);
7400
James Smart6d368e52011-05-24 11:44:12 -04007401 /*
7402 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
7403 * calls depends on these resources to complete port setup.
7404 */
7405 rc = lpfc_sli4_alloc_resource_identifiers(phba);
7406 if (rc) {
7407 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7408 "2920 Failed to alloc Resource IDs "
7409 "rc = x%x\n", rc);
7410 goto out_free_mbox;
7411 }
7412
James Smart61bda8f2016-10-13 15:06:05 -07007413 lpfc_set_host_data(phba, mboxq);
7414
7415 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7416 if (rc) {
7417 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7418 "2134 Failed to set host os driver version %x",
7419 rc);
7420 }
7421
James Smartda0436e2009-05-22 14:51:39 -04007422 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05007423 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7424 if (rc) {
7425 phba->link_state = LPFC_HBA_ERROR;
7426 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05007427 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05007428 }
7429
James Smartda0436e2009-05-22 14:51:39 -04007430 mboxq->vport = vport;
7431 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart3e1f0712018-11-29 16:09:29 -08007432 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04007433 if (rc == MBX_SUCCESS) {
7434 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7435 rc = 0;
7436 }
7437
7438 /*
7439 * This memory was allocated by the lpfc_read_sparam routine. Release
7440 * it to the mbuf pool.
7441 */
7442 lpfc_mbuf_free(phba, mp->virt, mp->phys);
7443 kfree(mp);
James Smart3e1f0712018-11-29 16:09:29 -08007444 mboxq->ctx_buf = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007445 if (unlikely(rc)) {
7446 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7447 "0382 READ_SPARAM command failed "
7448 "status %d, mbxStatus x%x\n",
7449 rc, bf_get(lpfc_mqe_status, mqe));
7450 phba->link_state = LPFC_HBA_ERROR;
7451 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007452 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007453 }
7454
James Smart05580562011-05-24 11:40:48 -04007455 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04007456
7457 /* Update the fc_host data structures with new wwn. */
7458 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7459 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7460
James Smart895427b2017-02-12 13:52:30 -08007461 /* Create all the SLI4 queues */
7462 rc = lpfc_sli4_queue_create(phba);
7463 if (rc) {
7464 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7465 "3089 Failed to allocate queues\n");
7466 rc = -ENODEV;
7467 goto out_free_mbox;
7468 }
7469 /* Set up all the queues to the device */
7470 rc = lpfc_sli4_queue_setup(phba);
7471 if (unlikely(rc)) {
7472 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7473 "0381 Error %d during queue setup.\n ", rc);
7474 goto out_stop_timers;
7475 }
7476 /* Initialize the driver internal SLI layer lists. */
7477 lpfc_sli4_setup(phba);
7478 lpfc_sli4_queue_init(phba);
7479
7480 /* update host els xri-sgl sizes and mappings */
7481 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007482 if (unlikely(rc)) {
7483 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7484 "1400 Failed to update xri-sgl size and "
7485 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08007486 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007487 }
7488
James Smart8a9d2e82012-05-09 21:16:12 -04007489 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08007490 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7491 phba->sli4_hba.els_xri_cnt);
7492 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04007493 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7494 "0582 Error %d during els sgl post "
7495 "operation\n", rc);
7496 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007497 goto out_destroy_queue;
7498 }
7499 phba->sli4_hba.els_xri_cnt = rc;
7500
James Smartf358dd02017-02-12 13:52:34 -08007501 if (phba->nvmet_support) {
7502 /* update host nvmet xri-sgl sizes and mappings */
7503 rc = lpfc_sli4_nvmet_sgl_update(phba);
7504 if (unlikely(rc)) {
7505 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7506 "6308 Failed to update nvmet-sgl size "
7507 "and mapping: %d\n", rc);
7508 goto out_destroy_queue;
7509 }
7510
7511 /* register the nvmet sgl pool to the port */
7512 rc = lpfc_sli4_repost_sgl_list(
7513 phba,
7514 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7515 phba->sli4_hba.nvmet_xri_cnt);
7516 if (unlikely(rc < 0)) {
7517 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7518 "3117 Error %d during nvmet "
7519 "sgl post\n", rc);
7520 rc = -ENODEV;
7521 goto out_destroy_queue;
7522 }
7523 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07007524
7525 cnt = phba->cfg_iocb_cnt * 1024;
7526 /* We need 1 iocbq for every SGL, for IO processing */
7527 cnt += phba->sli4_hba.nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08007528 } else {
James Smart0794d602019-01-28 11:14:19 -08007529 /* update host common xri-sgl sizes and mappings */
James Smart5e5b5112019-01-28 11:14:22 -08007530 rc = lpfc_sli4_io_sgl_update(phba);
James Smart895427b2017-02-12 13:52:30 -08007531 if (unlikely(rc)) {
7532 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7533 "6082 Failed to update nvme-sgl size "
7534 "and mapping: %d\n", rc);
7535 goto out_destroy_queue;
7536 }
James Smart6c621a22017-05-15 15:20:45 -07007537
James Smart0794d602019-01-28 11:14:19 -08007538 /* register the allocated common sgl pool to the port */
James Smart5e5b5112019-01-28 11:14:22 -08007539 rc = lpfc_sli4_repost_io_sgl_list(phba);
James Smart0794d602019-01-28 11:14:19 -08007540 if (unlikely(rc)) {
7541 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7542 "6116 Error %d during nvme sgl post "
7543 "operation\n", rc);
7544 /* Some NVME buffers were moved to abort nvme list */
7545 /* A pci function reset will repost them */
7546 rc = -ENODEV;
7547 goto out_destroy_queue;
7548 }
James Smart6c621a22017-05-15 15:20:45 -07007549 cnt = phba->cfg_iocb_cnt * 1024;
James Smart11e644e2017-06-15 22:56:48 -07007550 }
7551
7552 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07007553 /* Initialize and populate the iocb list per host */
7554 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007555 "2821 initialize iocb list %d total %d\n",
James Smart6c621a22017-05-15 15:20:45 -07007556 phba->cfg_iocb_cnt, cnt);
7557 rc = lpfc_init_iocb_list(phba, cnt);
7558 if (rc) {
7559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007560 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07007561 goto out_destroy_queue;
7562 }
James Smart8a9d2e82012-05-09 21:16:12 -04007563 }
7564
James Smart11e644e2017-06-15 22:56:48 -07007565 if (phba->nvmet_support)
7566 lpfc_nvmet_create_targetport(phba);
7567
James Smart2d7dbc42017-02-12 13:52:35 -08007568 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007569 /* Post initial buffers to all RQs created */
7570 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7571 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7572 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7573 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7574 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007575 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007576 rqbp->buffer_count = 0;
7577
James Smart2d7dbc42017-02-12 13:52:35 -08007578 lpfc_post_rq_buffer(
7579 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7580 phba->sli4_hba.nvmet_mrq_data[i],
James Smart2448e482018-04-09 14:24:24 -07007581 phba->cfg_nvmet_mrq_post, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007582 }
7583 }
7584
James Smartda0436e2009-05-22 14:51:39 -04007585 /* Post the rpi header region to the device. */
7586 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7587 if (unlikely(rc)) {
7588 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7589 "0393 Error %d during rpi post operation\n",
7590 rc);
7591 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007592 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007593 }
James Smart97f2ecf2012-03-01 22:35:23 -05007594 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007595
James Smart895427b2017-02-12 13:52:30 -08007596 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007597 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007598 /*
7599 * The FC Port needs to register FCFI (index 0)
7600 */
7601 lpfc_reg_fcfi(phba, mboxq);
7602 mboxq->vport = phba->pport;
7603 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7604 if (rc != MBX_SUCCESS)
7605 goto out_unset_queue;
7606 rc = 0;
7607 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7608 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007609 } else {
7610 /* We are a NVME Target mode with MRQ > 1 */
7611
7612 /* First register the FCFI */
7613 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7614 mboxq->vport = phba->pport;
7615 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7616 if (rc != MBX_SUCCESS)
7617 goto out_unset_queue;
7618 rc = 0;
7619 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7620 &mboxq->u.mqe.un.reg_fcfi_mrq);
7621
7622 /* Next register the MRQs */
7623 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7624 mboxq->vport = phba->pport;
7625 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7626 if (rc != MBX_SUCCESS)
7627 goto out_unset_queue;
7628 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007629 }
7630 /* Check if the port is configured to be disabled */
7631 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007632 }
7633
James Smartc4908502019-01-28 11:14:28 -08007634 /* Don't post more new bufs if repost already recovered
7635 * the nvme sgls.
7636 */
7637 if (phba->nvmet_support == 0) {
7638 if (phba->sli4_hba.io_xri_cnt == 0) {
7639 len = lpfc_new_io_buf(
7640 phba, phba->sli4_hba.io_xri_max);
7641 if (len == 0) {
7642 rc = -ENOMEM;
7643 goto out_unset_queue;
7644 }
7645
7646 if (phba->cfg_xri_rebalancing)
7647 lpfc_create_multixri_pools(phba);
7648 }
7649 } else {
7650 phba->cfg_xri_rebalancing = 0;
7651 }
7652
James Smartda0436e2009-05-22 14:51:39 -04007653 /* Allow asynchronous mailbox command to go through */
7654 spin_lock_irq(&phba->hbalock);
7655 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7656 spin_unlock_irq(&phba->hbalock);
7657
7658 /* Post receive buffers to the device */
7659 lpfc_sli4_rb_setup(phba);
7660
James Smartfc2b9892010-02-26 14:15:29 -05007661 /* Reset HBA FCF states after HBA reset */
7662 phba->fcf.fcf_flag = 0;
7663 phba->fcf.current_rec.flag = 0;
7664
James Smartda0436e2009-05-22 14:51:39 -04007665 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007666 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007667 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007668
7669 /* Start heart beat timer */
7670 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007671 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007672 phba->hb_outstanding = 0;
7673 phba->last_completion_time = jiffies;
7674
James Smart32517fc2019-01-28 11:14:33 -08007675 /* start eq_delay heartbeat */
7676 if (phba->cfg_auto_imax)
7677 queue_delayed_work(phba->wq, &phba->eq_delay_work,
7678 msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
7679
James Smartda0436e2009-05-22 14:51:39 -04007680 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007681 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007682 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007683
James Smart75baf692010-06-08 18:31:21 -04007684 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7685 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7686 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7687 if (!rc) {
7688 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7689 "2829 This device supports "
7690 "Advanced Error Reporting (AER)\n");
7691 spin_lock_irq(&phba->hbalock);
7692 phba->hba_flag |= HBA_AER_ENABLED;
7693 spin_unlock_irq(&phba->hbalock);
7694 } else {
7695 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7696 "2830 This device does not support "
7697 "Advanced Error Reporting (AER)\n");
7698 phba->cfg_aer_support = 0;
7699 }
James Smart0a96e972011-07-22 18:37:28 -04007700 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007701 }
7702
James Smartda0436e2009-05-22 14:51:39 -04007703 /*
7704 * The port is ready, set the host's link state to LINK_DOWN
7705 * in preparation for link interrupts.
7706 */
James Smartda0436e2009-05-22 14:51:39 -04007707 spin_lock_irq(&phba->hbalock);
7708 phba->link_state = LPFC_LINK_DOWN;
James Smart1dc5ec22018-10-23 13:41:11 -07007709
7710 /* Check if physical ports are trunked */
7711 if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
7712 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
7713 if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
7714 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
7715 if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
7716 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
7717 if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
7718 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04007719 spin_unlock_irq(&phba->hbalock);
James Smart1dc5ec22018-10-23 13:41:11 -07007720
James Smarte8869f52019-03-12 16:30:18 -07007721 /* Arm the CQs and then EQs on device */
7722 lpfc_sli4_arm_cqeq_intr(phba);
7723
7724 /* Indicate device interrupt mode */
7725 phba->sli4_hba.intr_enable = 1;
7726
James Smart026abb82011-12-13 13:20:45 -05007727 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7728 (phba->hba_flag & LINK_DISABLED)) {
7729 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7730 "3103 Adapter Link is disabled.\n");
7731 lpfc_down_link(phba, mboxq);
7732 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7733 if (rc != MBX_SUCCESS) {
7734 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7735 "3104 Adapter failed to issue "
7736 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
James Smartc4908502019-01-28 11:14:28 -08007737 goto out_io_buff_free;
James Smart026abb82011-12-13 13:20:45 -05007738 }
7739 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007740 /* don't perform init_link on SLI4 FC port loopback test */
7741 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7742 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7743 if (rc)
James Smartc4908502019-01-28 11:14:28 -08007744 goto out_io_buff_free;
James Smart1b511972011-12-13 13:23:09 -05007745 }
James Smart5350d872011-10-10 21:33:49 -04007746 }
7747 mempool_free(mboxq, phba->mbox_mem_pool);
7748 return rc;
James Smartc4908502019-01-28 11:14:28 -08007749out_io_buff_free:
7750 /* Free allocated IO Buffers */
7751 lpfc_io_free(phba);
James Smart76a95d72010-11-20 23:11:48 -05007752out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007753 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007754 lpfc_sli4_queue_unset(phba);
7755out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007756 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007757 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007758out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007759 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007760out_free_mbox:
7761 mempool_free(mboxq, phba->mbox_mem_pool);
7762 return rc;
7763}
James Smarte59058c2008-08-24 21:49:00 -04007764
7765/**
James Smart3621a712009-04-06 18:47:14 -04007766 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007767 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007768 *
James Smarte59058c2008-08-24 21:49:00 -04007769 * This is the callback function for mailbox timer. The mailbox
7770 * timer is armed when a new mailbox command is issued and the timer
7771 * is deleted when the mailbox complete. The function is called by
7772 * the kernel timer code when a mailbox does not complete within
7773 * expected time. This function wakes up the worker thread to
7774 * process the mailbox timeout and returns. All the processing is
7775 * done by the worker thread function lpfc_mbox_timeout_handler.
7776 **/
dea31012005-04-17 16:05:31 -05007777void
Kees Cookf22eb4d2017-09-06 20:24:26 -07007778lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05007779{
Kees Cookf22eb4d2017-09-06 20:24:26 -07007780 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05007781 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007782 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007783
James Smart2e0fef82007-06-17 19:56:36 -05007784 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007785 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007786 if (!tmo_posted)
7787 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7788 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7789
James Smart5e9d9b82008-06-14 22:52:53 -04007790 if (!tmo_posted)
7791 lpfc_worker_wake_up(phba);
7792 return;
dea31012005-04-17 16:05:31 -05007793}
7794
James Smarte8d3c3b2013-10-10 12:21:30 -04007795/**
7796 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7797 * are pending
7798 * @phba: Pointer to HBA context object.
7799 *
7800 * This function checks if any mailbox completions are present on the mailbox
7801 * completion queue.
7802 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007803static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007804lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7805{
7806
7807 uint32_t idx;
7808 struct lpfc_queue *mcq;
7809 struct lpfc_mcqe *mcqe;
7810 bool pending_completions = false;
James Smart7365f6f2018-02-22 08:18:46 -08007811 uint8_t qe_valid;
James Smarte8d3c3b2013-10-10 12:21:30 -04007812
7813 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7814 return false;
7815
7816 /* Check for completions on mailbox completion queue */
7817
7818 mcq = phba->sli4_hba.mbx_cq;
7819 idx = mcq->hba_index;
James Smart7365f6f2018-02-22 08:18:46 -08007820 qe_valid = mcq->qe_valid;
James Smart9afbee32019-03-12 16:30:28 -07007821 while (bf_get_le32(lpfc_cqe_valid,
7822 (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
7823 mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
James Smarte8d3c3b2013-10-10 12:21:30 -04007824 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7825 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7826 pending_completions = true;
7827 break;
7828 }
7829 idx = (idx + 1) % mcq->entry_count;
7830 if (mcq->hba_index == idx)
7831 break;
James Smart7365f6f2018-02-22 08:18:46 -08007832
7833 /* if the index wrapped around, toggle the valid bit */
7834 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7835 qe_valid = (qe_valid) ? 0 : 1;
James Smarte8d3c3b2013-10-10 12:21:30 -04007836 }
7837 return pending_completions;
7838
7839}
7840
7841/**
7842 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7843 * that were missed.
7844 * @phba: Pointer to HBA context object.
7845 *
7846 * For sli4, it is possible to miss an interrupt. As such mbox completions
7847 * maybe missed causing erroneous mailbox timeouts to occur. This function
7848 * checks to see if mbox completions are on the mailbox completion queue
7849 * and will process all the completions associated with the eq for the
7850 * mailbox completion queue.
7851 **/
YueHaibingd7b761b2019-05-31 23:28:41 +08007852static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007853lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7854{
James Smartb71413d2018-02-22 08:18:40 -08007855 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04007856 uint32_t eqidx;
7857 struct lpfc_queue *fpeq = NULL;
James Smart657add42019-05-21 17:49:06 -07007858 struct lpfc_queue *eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04007859 bool mbox_pending;
7860
7861 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7862 return false;
7863
James Smart657add42019-05-21 17:49:06 -07007864 /* Find the EQ associated with the mbox CQ */
7865 if (sli4_hba->hdwq) {
7866 for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
7867 eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
7868 if (eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
7869 fpeq = eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04007870 break;
7871 }
James Smart657add42019-05-21 17:49:06 -07007872 }
7873 }
James Smarte8d3c3b2013-10-10 12:21:30 -04007874 if (!fpeq)
7875 return false;
7876
7877 /* Turn off interrupts from this EQ */
7878
James Smartb71413d2018-02-22 08:18:40 -08007879 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04007880
7881 /* Check to see if a mbox completion is pending */
7882
7883 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7884
7885 /*
7886 * If a mbox completion is pending, process all the events on EQ
7887 * associated with the mbox completion queue (this could include
7888 * mailbox commands, async events, els commands, receive queue data
7889 * and fcp commands)
7890 */
7891
7892 if (mbox_pending)
James Smart32517fc2019-01-28 11:14:33 -08007893 /* process and rearm the EQ */
7894 lpfc_sli4_process_eq(phba, fpeq);
7895 else
7896 /* Always clear and re-arm the EQ */
7897 sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04007898
7899 return mbox_pending;
7900
7901}
James Smarte59058c2008-08-24 21:49:00 -04007902
7903/**
James Smart3621a712009-04-06 18:47:14 -04007904 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007905 * @phba: Pointer to HBA context object.
7906 *
7907 * This function is called from worker thread when a mailbox command times out.
7908 * The caller is not required to hold any locks. This function will reset the
7909 * HBA and recover all the pending commands.
7910 **/
dea31012005-04-17 16:05:31 -05007911void
7912lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7913{
James Smart2e0fef82007-06-17 19:56:36 -05007914 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007915 MAILBOX_t *mb = NULL;
7916
James Smart1dcb58e2007-04-25 09:51:30 -04007917 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007918
James Smarte8d3c3b2013-10-10 12:21:30 -04007919 /* If the mailbox completed, process the completion and return */
7920 if (lpfc_sli4_process_missed_mbox_completions(phba))
7921 return;
7922
James Smarteb016562014-09-03 12:58:06 -04007923 if (pmbox != NULL)
7924 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007925 /* Check the pmbox pointer first. There is a race condition
7926 * between the mbox timeout handler getting executed in the
7927 * worklist and the mailbox actually completing. When this
7928 * race condition occurs, the mbox_active will be NULL.
7929 */
7930 spin_lock_irq(&phba->hbalock);
7931 if (pmbox == NULL) {
7932 lpfc_printf_log(phba, KERN_WARNING,
7933 LOG_MBOX | LOG_SLI,
7934 "0353 Active Mailbox cleared - mailbox timeout "
7935 "exiting\n");
7936 spin_unlock_irq(&phba->hbalock);
7937 return;
7938 }
7939
dea31012005-04-17 16:05:31 -05007940 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007941 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart32350662019-08-14 16:57:06 -07007942 "0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007943 mb->mbxCommand,
7944 phba->pport->port_state,
7945 phba->sli.sli_flag,
7946 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007947 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007948
James Smart1dcb58e2007-04-25 09:51:30 -04007949 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7950 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007951 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007952 */
James Smart2e0fef82007-06-17 19:56:36 -05007953 spin_lock_irq(&phba->pport->work_port_lock);
7954 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7955 spin_unlock_irq(&phba->pport->work_port_lock);
7956 spin_lock_irq(&phba->hbalock);
7957 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007958 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007959 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007960
James Smartdb55fba2014-04-04 13:52:02 -04007961 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007962
7963 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007964 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007965
7966 /* Reset the HBA device */
7967 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007968}
7969
James Smarte59058c2008-08-24 21:49:00 -04007970/**
James Smart3772a992009-05-22 14:50:54 -04007971 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007972 * @phba: Pointer to HBA context object.
7973 * @pmbox: Pointer to mailbox object.
7974 * @flag: Flag indicating how the mailbox need to be processed.
7975 *
7976 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007977 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7978 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007979 * The mailbox command can be submitted in polling mode, in which case
7980 * this function will wait in a polling loop for the completion of the
7981 * mailbox.
7982 * If the mailbox is submitted in no_wait mode (not polling) the
7983 * function will submit the command and returns immediately without waiting
7984 * for the mailbox completion. The no_wait is supported only when HBA
7985 * is in SLI2/SLI3 mode - interrupts are enabled.
7986 * The SLI interface allows only one mailbox pending at a time. If the
7987 * mailbox is issued in polling mode and there is already a mailbox
7988 * pending, then the function will return an error. If the mailbox is issued
7989 * in NO_WAIT mode and there is a mailbox pending already, the function
7990 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7991 * The sli layer owns the mailbox object until the completion of mailbox
7992 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7993 * return codes the caller owns the mailbox command after the return of
7994 * the function.
7995 **/
James Smart3772a992009-05-22 14:50:54 -04007996static int
7997lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7998 uint32_t flag)
dea31012005-04-17 16:05:31 -05007999{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008000 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05008001 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008002 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05008003 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008004 int i;
James Smart09372822008-01-11 01:52:54 -05008005 unsigned long timeout;
dea31012005-04-17 16:05:31 -05008006 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04008007 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05008008 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04008009 int processing_queue = 0;
8010
8011 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8012 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04008013 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04008014 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04008015 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8016 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8017 return MBX_SUCCESS;
8018 }
James Smart58da1ff2008-04-07 10:15:56 -04008019 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04008020 pmbox = lpfc_mbox_get(phba);
8021 if (!pmbox) {
8022 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8023 return MBX_SUCCESS;
8024 }
8025 }
dea31012005-04-17 16:05:31 -05008026
James Smarted957682007-06-17 19:56:37 -05008027 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05008028 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05008029 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04008030 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05008031 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05008032 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008033 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04008034 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05008035 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04008036 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05008037 }
8038 }
8039
Linas Vepstas8d63f372007-02-14 14:28:36 -06008040 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04008041 if (unlikely(pci_channel_offline(phba->pcidev))) {
8042 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8043 goto out_not_finished;
8044 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06008045
James Smarta257bf92009-04-06 18:48:10 -04008046 /* If HBA has a deferred error attention, fail the iocb. */
8047 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8048 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8049 goto out_not_finished;
8050 }
8051
dea31012005-04-17 16:05:31 -05008052 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05008053
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008054 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05008055 status = MBX_SUCCESS;
8056
James Smart2e0fef82007-06-17 19:56:36 -05008057 if (phba->link_state == LPFC_HBA_ERROR) {
8058 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05008059
8060 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008061 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8062 "(%d):0311 Mailbox command x%x cannot "
8063 "issue Data: x%x x%x\n",
8064 pmbox->vport ? pmbox->vport->vpi : 0,
8065 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008066 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05008067 }
8068
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008069 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05008070 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
8071 !(hc_copy & HC_MBINT_ENA)) {
8072 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8073 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04008074 "(%d):2528 Mailbox command x%x cannot "
8075 "issue Data: x%x x%x\n",
8076 pmbox->vport ? pmbox->vport->vpi : 0,
8077 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05008078 goto out_not_finished;
8079 }
James Smart92908312006-03-07 15:04:13 -05008080 }
8081
dea31012005-04-17 16:05:31 -05008082 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8083 /* Polling for a mbox command when another one is already active
8084 * is not allowed in SLI. Also, the driver must have established
8085 * SLI2 mode to queue and process multiple mbox commands.
8086 */
8087
8088 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05008089 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008090
8091 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008092 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8093 "(%d):2529 Mailbox command x%x "
8094 "cannot issue Data: x%x x%x\n",
8095 pmbox->vport ? pmbox->vport->vpi : 0,
8096 pmbox->u.mb.mbxCommand,
8097 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008098 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008099 }
8100
James Smart3772a992009-05-22 14:50:54 -04008101 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05008102 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008103 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008104 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8105 "(%d):2530 Mailbox command x%x "
8106 "cannot issue Data: x%x x%x\n",
8107 pmbox->vport ? pmbox->vport->vpi : 0,
8108 pmbox->u.mb.mbxCommand,
8109 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008110 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008111 }
8112
dea31012005-04-17 16:05:31 -05008113 /* Another mailbox command is still being processed, queue this
8114 * command to be processed later.
8115 */
8116 lpfc_mbox_put(phba, pmbox);
8117
8118 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05008119 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008120 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05008121 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008122 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07008123 mbx->mbxCommand,
8124 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008125 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008126
8127 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05008128 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008129
James Smart858c9f62007-06-17 19:56:39 -05008130 if (pmbox->vport) {
8131 lpfc_debugfs_disc_trc(pmbox->vport,
8132 LPFC_DISC_TRC_MBOX_VPORT,
8133 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008134 (uint32_t)mbx->mbxCommand,
8135 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008136 }
8137 else {
8138 lpfc_debugfs_disc_trc(phba->pport,
8139 LPFC_DISC_TRC_MBOX,
8140 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008141 (uint32_t)mbx->mbxCommand,
8142 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008143 }
8144
James Smart2e0fef82007-06-17 19:56:36 -05008145 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05008146 }
8147
dea31012005-04-17 16:05:31 -05008148 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8149
8150 /* If we are not polling, we MUST be in SLI2 mode */
8151 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04008152 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008153 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05008154 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008155 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008156 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008157 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8158 "(%d):2531 Mailbox command x%x "
8159 "cannot issue Data: x%x x%x\n",
8160 pmbox->vport ? pmbox->vport->vpi : 0,
8161 pmbox->u.mb.mbxCommand,
8162 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008163 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008164 }
8165 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04008166 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8167 1000);
8168 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05008169 }
8170
8171 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05008172 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008173 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05008174 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008175 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07008176 mbx->mbxCommand,
8177 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008178 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008179
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008180 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05008181 if (pmbox->vport) {
8182 lpfc_debugfs_disc_trc(pmbox->vport,
8183 LPFC_DISC_TRC_MBOX_VPORT,
8184 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008185 (uint32_t)mbx->mbxCommand,
8186 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008187 }
8188 else {
8189 lpfc_debugfs_disc_trc(phba->pport,
8190 LPFC_DISC_TRC_MBOX,
8191 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008192 (uint32_t)mbx->mbxCommand,
8193 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008194 }
8195 }
8196
dea31012005-04-17 16:05:31 -05008197 psli->slistat.mbox_cmd++;
8198 evtctr = psli->slistat.mbox_event;
8199
8200 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008201 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05008202
James Smart3772a992009-05-22 14:50:54 -04008203 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04008204 /* Populate mbox extension offset word. */
8205 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008206 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008207 = (uint8_t *)phba->mbox_ext
8208 - (uint8_t *)phba->mbox;
8209 }
8210
8211 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008212 if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
8213 lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
8214 (uint8_t *)phba->mbox_ext,
8215 pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008216 }
8217 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008218 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008219 } else {
James Smart7a470272010-03-15 11:25:20 -04008220 /* Populate mbox extension offset word. */
8221 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008222 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008223 = MAILBOX_HBA_EXT_OFFSET;
8224
8225 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008226 if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -04008227 lpfc_memcpy_to_slim(phba->MBslimaddr +
8228 MAILBOX_HBA_EXT_OFFSET,
James Smart3e1f0712018-11-29 16:09:29 -08008229 pmbox->ctx_buf, pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008230
James Smart895427b2017-02-12 13:52:30 -08008231 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008232 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08008233 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
8234 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008235
8236 /* First copy mbox command data to HBA SLIM, skip past first
8237 word */
8238 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008239 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05008240 MAILBOX_CMD_SIZE - sizeof (uint32_t));
8241
8242 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008243 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05008244 to_slim = phba->MBslimaddr;
8245 writel(ldata, to_slim);
8246 readl(to_slim); /* flush */
8247
James Smart895427b2017-02-12 13:52:30 -08008248 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008249 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04008250 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008251 }
8252
8253 wmb();
dea31012005-04-17 16:05:31 -05008254
8255 switch (flag) {
8256 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05008257 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05008258 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05008259 /* Interrupt board to do it */
8260 writel(CA_MBATT, phba->CAregaddr);
8261 readl(phba->CAregaddr); /* flush */
8262 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05008263 break;
8264
8265 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05008266 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05008267 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05008268 /* Interrupt board to do it */
8269 writel(CA_MBATT, phba->CAregaddr);
8270 readl(phba->CAregaddr); /* flush */
8271
James Smart3772a992009-05-22 14:50:54 -04008272 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008273 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04008274 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008275 word0 = le32_to_cpu(word0);
8276 } else {
8277 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05008278 if (lpfc_readl(phba->MBslimaddr, &word0)) {
8279 spin_unlock_irqrestore(&phba->hbalock,
8280 drvr_flag);
8281 goto out_not_finished;
8282 }
dea31012005-04-17 16:05:31 -05008283 }
8284
8285 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008286 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8287 spin_unlock_irqrestore(&phba->hbalock,
8288 drvr_flag);
8289 goto out_not_finished;
8290 }
James Smarta183a152011-10-10 21:32:43 -04008291 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8292 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05008293 i = 0;
dea31012005-04-17 16:05:31 -05008294 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05008295 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
8296 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05008297 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05008298 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05008299 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008300 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05008301 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04008302 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008303 }
8304
8305 /* Check if we took a mbox interrupt while we were
8306 polling */
8307 if (((word0 & OWN_CHIP) != OWN_CHIP)
8308 && (evtctr != psli->slistat.mbox_event))
8309 break;
8310
James Smart09372822008-01-11 01:52:54 -05008311 if (i++ > 10) {
8312 spin_unlock_irqrestore(&phba->hbalock,
8313 drvr_flag);
8314 msleep(1);
8315 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8316 }
dea31012005-04-17 16:05:31 -05008317
James Smart3772a992009-05-22 14:50:54 -04008318 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008319 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04008320 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008321 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008322 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05008323 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04008324 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05008325 /* Check real SLIM for any errors */
8326 slimword0 = readl(phba->MBslimaddr);
8327 slimmb = (MAILBOX_t *) & slimword0;
8328 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
8329 && slimmb->mbxStatus) {
8330 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04008331 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008332 word0 = slimword0;
8333 }
8334 }
8335 } else {
8336 /* First copy command data */
8337 word0 = readl(phba->MBslimaddr);
8338 }
8339 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008340 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8341 spin_unlock_irqrestore(&phba->hbalock,
8342 drvr_flag);
8343 goto out_not_finished;
8344 }
dea31012005-04-17 16:05:31 -05008345 }
8346
James Smart3772a992009-05-22 14:50:54 -04008347 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008348 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08008349 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
8350 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008351 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008352 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
James Smart7a470272010-03-15 11:25:20 -04008353 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -08008354 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008355 pmbox->out_ext_byte_len);
8356 }
dea31012005-04-17 16:05:31 -05008357 } else {
8358 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008359 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08008360 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008361 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008362 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
8363 lpfc_memcpy_from_slim(
8364 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008365 phba->MBslimaddr +
8366 MAILBOX_HBA_EXT_OFFSET,
8367 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05008368 }
8369 }
8370
8371 writel(HA_MBATT, phba->HAregaddr);
8372 readl(phba->HAregaddr); /* flush */
8373
8374 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008375 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05008376 }
8377
James Smart2e0fef82007-06-17 19:56:36 -05008378 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8379 return status;
James Smart58da1ff2008-04-07 10:15:56 -04008380
8381out_not_finished:
8382 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04008383 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04008384 lpfc_mbox_cmpl_put(phba, pmbox);
8385 }
8386 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008387}
8388
James Smarte59058c2008-08-24 21:49:00 -04008389/**
James Smartf1126682009-06-10 17:22:44 -04008390 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8391 * @phba: Pointer to HBA context object.
8392 *
8393 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8394 * the driver internal pending mailbox queue. It will then try to wait out the
8395 * possible outstanding mailbox command before return.
8396 *
8397 * Returns:
8398 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8399 * the outstanding mailbox command timed out.
8400 **/
8401static int
8402lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8403{
8404 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04008405 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04008406 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04008407
8408 /* Mark the asynchronous mailbox command posting as blocked */
8409 spin_lock_irq(&phba->hbalock);
8410 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04008411 /* Determine how long we might wait for the active mailbox
8412 * command to be gracefully completed by firmware.
8413 */
James Smarta183a152011-10-10 21:32:43 -04008414 if (phba->sli.mbox_active)
8415 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8416 phba->sli.mbox_active) *
8417 1000) + jiffies;
8418 spin_unlock_irq(&phba->hbalock);
8419
James Smarte8d3c3b2013-10-10 12:21:30 -04008420 /* Make sure the mailbox is really active */
8421 if (timeout)
8422 lpfc_sli4_process_missed_mbox_completions(phba);
8423
James Smartf1126682009-06-10 17:22:44 -04008424 /* Wait for the outstnading mailbox command to complete */
8425 while (phba->sli.mbox_active) {
8426 /* Check active mailbox complete status every 2ms */
8427 msleep(2);
8428 if (time_after(jiffies, timeout)) {
8429 /* Timeout, marked the outstanding cmd not complete */
8430 rc = 1;
8431 break;
8432 }
8433 }
8434
8435 /* Can not cleanly block async mailbox command, fails it */
8436 if (rc) {
8437 spin_lock_irq(&phba->hbalock);
8438 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8439 spin_unlock_irq(&phba->hbalock);
8440 }
8441 return rc;
8442}
8443
8444/**
8445 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8446 * @phba: Pointer to HBA context object.
8447 *
8448 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8449 * commands from the driver internal pending mailbox queue. It makes sure
8450 * that there is no outstanding mailbox command before resuming posting
8451 * asynchronous mailbox commands. If, for any reason, there is outstanding
8452 * mailbox command, it will try to wait it out before resuming asynchronous
8453 * mailbox command posting.
8454 **/
8455static void
8456lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8457{
8458 struct lpfc_sli *psli = &phba->sli;
8459
8460 spin_lock_irq(&phba->hbalock);
8461 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8462 /* Asynchronous mailbox posting is not blocked, do nothing */
8463 spin_unlock_irq(&phba->hbalock);
8464 return;
8465 }
8466
8467 /* Outstanding synchronous mailbox command is guaranteed to be done,
8468 * successful or timeout, after timing-out the outstanding mailbox
8469 * command shall always be removed, so just unblock posting async
8470 * mailbox command and resume
8471 */
8472 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8473 spin_unlock_irq(&phba->hbalock);
8474
8475 /* wake up worker thread to post asynchronlous mailbox command */
8476 lpfc_worker_wake_up(phba);
8477}
8478
8479/**
James Smart2d843ed2012-09-29 11:29:06 -04008480 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8481 * @phba: Pointer to HBA context object.
8482 * @mboxq: Pointer to mailbox object.
8483 *
8484 * The function waits for the bootstrap mailbox register ready bit from
8485 * port for twice the regular mailbox command timeout value.
8486 *
8487 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8488 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8489 **/
8490static int
8491lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8492{
8493 uint32_t db_ready;
8494 unsigned long timeout;
8495 struct lpfc_register bmbx_reg;
8496
8497 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8498 * 1000) + jiffies;
8499
8500 do {
8501 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8502 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8503 if (!db_ready)
James Smarte2ffe4d2019-03-12 16:30:15 -07008504 mdelay(2);
James Smart2d843ed2012-09-29 11:29:06 -04008505
8506 if (time_after(jiffies, timeout))
8507 return MBXERR_ERROR;
8508 } while (!db_ready);
8509
8510 return 0;
8511}
8512
8513/**
James Smartda0436e2009-05-22 14:51:39 -04008514 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8515 * @phba: Pointer to HBA context object.
8516 * @mboxq: Pointer to mailbox object.
8517 *
8518 * The function posts a mailbox to the port. The mailbox is expected
8519 * to be comletely filled in and ready for the port to operate on it.
8520 * This routine executes a synchronous completion operation on the
8521 * mailbox by polling for its completion.
8522 *
8523 * The caller must not be holding any locks when calling this routine.
8524 *
8525 * Returns:
8526 * MBX_SUCCESS - mailbox posted successfully
8527 * Any of the MBX error values.
8528 **/
8529static int
8530lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8531{
8532 int rc = MBX_SUCCESS;
8533 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04008534 uint32_t mcqe_status;
8535 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04008536 struct lpfc_sli *psli = &phba->sli;
8537 struct lpfc_mqe *mb = &mboxq->u.mqe;
8538 struct lpfc_bmbx_create *mbox_rgn;
8539 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04008540
8541 /*
8542 * Only one mailbox can be active to the bootstrap mailbox region
8543 * at a time and there is no queueing provided.
8544 */
8545 spin_lock_irqsave(&phba->hbalock, iflag);
8546 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8547 spin_unlock_irqrestore(&phba->hbalock, iflag);
8548 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008549 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008550 "cannot issue Data: x%x x%x\n",
8551 mboxq->vport ? mboxq->vport->vpi : 0,
8552 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008553 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8554 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008555 psli->sli_flag, MBX_POLL);
8556 return MBXERR_ERROR;
8557 }
8558 /* The server grabs the token and owns it until release */
8559 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8560 phba->sli.mbox_active = mboxq;
8561 spin_unlock_irqrestore(&phba->hbalock, iflag);
8562
James Smart2d843ed2012-09-29 11:29:06 -04008563 /* wait for bootstrap mbox register for readyness */
8564 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8565 if (rc)
8566 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008567 /*
8568 * Initialize the bootstrap memory region to avoid stale data areas
8569 * in the mailbox post. Then copy the caller's mailbox contents to
8570 * the bmbx mailbox region.
8571 */
8572 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8573 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
James Smart48f8fdb2018-05-04 20:37:51 -07008574 lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8575 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008576
8577 /* Post the high mailbox dma address to the port and wait for ready. */
8578 dma_address = &phba->sli4_hba.bmbx.dma_address;
8579 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8580
James Smart2d843ed2012-09-29 11:29:06 -04008581 /* wait for bootstrap mbox register for hi-address write done */
8582 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8583 if (rc)
8584 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008585
8586 /* Post the low mailbox dma address to the port. */
8587 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008588
James Smart2d843ed2012-09-29 11:29:06 -04008589 /* wait for bootstrap mbox register for low address write done */
8590 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8591 if (rc)
8592 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008593
8594 /*
8595 * Read the CQ to ensure the mailbox has completed.
8596 * If so, update the mailbox status so that the upper layers
8597 * can complete the request normally.
8598 */
James Smart48f8fdb2018-05-04 20:37:51 -07008599 lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8600 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008601 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
James Smart48f8fdb2018-05-04 20:37:51 -07008602 lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8603 sizeof(struct lpfc_mcqe));
James Smartda0436e2009-05-22 14:51:39 -04008604 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008605 /*
8606 * When the CQE status indicates a failure and the mailbox status
8607 * indicates success then copy the CQE status into the mailbox status
8608 * (and prefix it with x4000).
8609 */
James Smartda0436e2009-05-22 14:51:39 -04008610 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008611 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8612 bf_set(lpfc_mqe_status, mb,
8613 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008614 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008615 } else
8616 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008617
8618 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008619 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008620 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8621 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008622 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8623 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8624 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008625 bf_get(lpfc_mqe_status, mb),
8626 mb->un.mb_words[0], mb->un.mb_words[1],
8627 mb->un.mb_words[2], mb->un.mb_words[3],
8628 mb->un.mb_words[4], mb->un.mb_words[5],
8629 mb->un.mb_words[6], mb->un.mb_words[7],
8630 mb->un.mb_words[8], mb->un.mb_words[9],
8631 mb->un.mb_words[10], mb->un.mb_words[11],
8632 mb->un.mb_words[12], mboxq->mcqe.word0,
8633 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8634 mboxq->mcqe.trailer);
8635exit:
8636 /* We are holding the token, no needed for lock when release */
8637 spin_lock_irqsave(&phba->hbalock, iflag);
8638 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8639 phba->sli.mbox_active = NULL;
8640 spin_unlock_irqrestore(&phba->hbalock, iflag);
8641 return rc;
8642}
8643
8644/**
8645 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8646 * @phba: Pointer to HBA context object.
8647 * @pmbox: Pointer to mailbox object.
8648 * @flag: Flag indicating how the mailbox need to be processed.
8649 *
8650 * This function is called by discovery code and HBA management code to submit
8651 * a mailbox command to firmware with SLI-4 interface spec.
8652 *
8653 * Return codes the caller owns the mailbox command after the return of the
8654 * function.
8655 **/
8656static int
8657lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8658 uint32_t flag)
8659{
8660 struct lpfc_sli *psli = &phba->sli;
8661 unsigned long iflags;
8662 int rc;
8663
James Smartb76f2dc2011-07-22 18:37:42 -04008664 /* dump from issue mailbox command if setup */
8665 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8666
James Smart8fa38512009-07-19 10:01:03 -04008667 rc = lpfc_mbox_dev_check(phba);
8668 if (unlikely(rc)) {
8669 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008670 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008671 "cannot issue Data: x%x x%x\n",
8672 mboxq->vport ? mboxq->vport->vpi : 0,
8673 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008674 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8675 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008676 psli->sli_flag, flag);
8677 goto out_not_finished;
8678 }
8679
James Smartda0436e2009-05-22 14:51:39 -04008680 /* Detect polling mode and jump to a handler */
8681 if (!phba->sli4_hba.intr_enable) {
8682 if (flag == MBX_POLL)
8683 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8684 else
8685 rc = -EIO;
8686 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008687 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008688 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008689 "(x%x/x%x) failure: "
8690 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8691 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008692 mboxq->vport ? mboxq->vport->vpi : 0,
8693 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008694 lpfc_sli_config_mbox_subsys_get(phba,
8695 mboxq),
8696 lpfc_sli_config_mbox_opcode_get(phba,
8697 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008698 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8699 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8700 bf_get(lpfc_mcqe_ext_status,
8701 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008702 psli->sli_flag, flag);
8703 return rc;
8704 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008705 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8706 "(%d):2542 Try to issue mailbox command "
James Smart7365f6f2018-02-22 08:18:46 -08008707 "x%x (x%x/x%x) synchronously ahead of async "
James Smartf1126682009-06-10 17:22:44 -04008708 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008709 mboxq->vport ? mboxq->vport->vpi : 0,
8710 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008711 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8712 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008713 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008714 /* Try to block the asynchronous mailbox posting */
8715 rc = lpfc_sli4_async_mbox_block(phba);
8716 if (!rc) {
8717 /* Successfully blocked, now issue sync mbox cmd */
8718 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8719 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008720 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008721 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008722 "(%d):2597 Sync Mailbox command "
8723 "x%x (x%x/x%x) failure: "
8724 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8725 "Data: x%x x%x\n,",
8726 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008727 mboxq->u.mb.mbxCommand,
8728 lpfc_sli_config_mbox_subsys_get(phba,
8729 mboxq),
8730 lpfc_sli_config_mbox_opcode_get(phba,
8731 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008732 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8733 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8734 bf_get(lpfc_mcqe_ext_status,
8735 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008736 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008737 /* Unblock the async mailbox posting afterward */
8738 lpfc_sli4_async_mbox_unblock(phba);
8739 }
8740 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008741 }
8742
8743 /* Now, interrupt mode asynchrous mailbox command */
8744 rc = lpfc_mbox_cmd_check(phba, mboxq);
8745 if (rc) {
8746 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008747 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008748 "cannot issue Data: x%x x%x\n",
8749 mboxq->vport ? mboxq->vport->vpi : 0,
8750 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008751 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8752 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008753 psli->sli_flag, flag);
8754 goto out_not_finished;
8755 }
James Smartda0436e2009-05-22 14:51:39 -04008756
8757 /* Put the mailbox command to the driver internal FIFO */
8758 psli->slistat.mbox_busy++;
8759 spin_lock_irqsave(&phba->hbalock, iflags);
8760 lpfc_mbox_put(phba, mboxq);
8761 spin_unlock_irqrestore(&phba->hbalock, iflags);
8762 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8763 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008764 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008765 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8766 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008767 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8768 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008769 phba->pport->port_state,
8770 psli->sli_flag, MBX_NOWAIT);
8771 /* Wake up worker thread to transport mailbox command from head */
8772 lpfc_worker_wake_up(phba);
8773
8774 return MBX_BUSY;
8775
8776out_not_finished:
8777 return MBX_NOT_FINISHED;
8778}
8779
8780/**
8781 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8782 * @phba: Pointer to HBA context object.
8783 *
8784 * This function is called by worker thread to send a mailbox command to
8785 * SLI4 HBA firmware.
8786 *
8787 **/
8788int
8789lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8790{
8791 struct lpfc_sli *psli = &phba->sli;
8792 LPFC_MBOXQ_t *mboxq;
8793 int rc = MBX_SUCCESS;
8794 unsigned long iflags;
8795 struct lpfc_mqe *mqe;
8796 uint32_t mbx_cmnd;
8797
8798 /* Check interrupt mode before post async mailbox command */
8799 if (unlikely(!phba->sli4_hba.intr_enable))
8800 return MBX_NOT_FINISHED;
8801
8802 /* Check for mailbox command service token */
8803 spin_lock_irqsave(&phba->hbalock, iflags);
8804 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8805 spin_unlock_irqrestore(&phba->hbalock, iflags);
8806 return MBX_NOT_FINISHED;
8807 }
8808 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8809 spin_unlock_irqrestore(&phba->hbalock, iflags);
8810 return MBX_NOT_FINISHED;
8811 }
8812 if (unlikely(phba->sli.mbox_active)) {
8813 spin_unlock_irqrestore(&phba->hbalock, iflags);
8814 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8815 "0384 There is pending active mailbox cmd\n");
8816 return MBX_NOT_FINISHED;
8817 }
8818 /* Take the mailbox command service token */
8819 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8820
8821 /* Get the next mailbox command from head of queue */
8822 mboxq = lpfc_mbox_get(phba);
8823
8824 /* If no more mailbox command waiting for post, we're done */
8825 if (!mboxq) {
8826 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8827 spin_unlock_irqrestore(&phba->hbalock, iflags);
8828 return MBX_SUCCESS;
8829 }
8830 phba->sli.mbox_active = mboxq;
8831 spin_unlock_irqrestore(&phba->hbalock, iflags);
8832
8833 /* Check device readiness for posting mailbox command */
8834 rc = lpfc_mbox_dev_check(phba);
8835 if (unlikely(rc))
8836 /* Driver clean routine will clean up pending mailbox */
8837 goto out_not_finished;
8838
8839 /* Prepare the mbox command to be posted */
8840 mqe = &mboxq->u.mqe;
8841 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8842
8843 /* Start timer for the mbox_tmo and log some mailbox post messages */
8844 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008845 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008846
8847 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008848 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008849 "x%x x%x\n",
8850 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008851 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8852 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008853 phba->pport->port_state, psli->sli_flag);
8854
8855 if (mbx_cmnd != MBX_HEARTBEAT) {
8856 if (mboxq->vport) {
8857 lpfc_debugfs_disc_trc(mboxq->vport,
8858 LPFC_DISC_TRC_MBOX_VPORT,
8859 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8860 mbx_cmnd, mqe->un.mb_words[0],
8861 mqe->un.mb_words[1]);
8862 } else {
8863 lpfc_debugfs_disc_trc(phba->pport,
8864 LPFC_DISC_TRC_MBOX,
8865 "MBOX Send: cmd:x%x mb:x%x x%x",
8866 mbx_cmnd, mqe->un.mb_words[0],
8867 mqe->un.mb_words[1]);
8868 }
8869 }
8870 psli->slistat.mbox_cmd++;
8871
8872 /* Post the mailbox command to the port */
8873 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8874 if (rc != MBX_SUCCESS) {
8875 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008876 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008877 "cannot issue Data: x%x x%x\n",
8878 mboxq->vport ? mboxq->vport->vpi : 0,
8879 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008880 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8881 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008882 psli->sli_flag, MBX_NOWAIT);
8883 goto out_not_finished;
8884 }
8885
8886 return rc;
8887
8888out_not_finished:
8889 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008890 if (phba->sli.mbox_active) {
8891 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8892 __lpfc_mbox_cmpl_put(phba, mboxq);
8893 /* Release the token */
8894 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8895 phba->sli.mbox_active = NULL;
8896 }
James Smartda0436e2009-05-22 14:51:39 -04008897 spin_unlock_irqrestore(&phba->hbalock, iflags);
8898
8899 return MBX_NOT_FINISHED;
8900}
8901
8902/**
8903 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8904 * @phba: Pointer to HBA context object.
8905 * @pmbox: Pointer to mailbox object.
8906 * @flag: Flag indicating how the mailbox need to be processed.
8907 *
8908 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8909 * the API jump table function pointer from the lpfc_hba struct.
8910 *
8911 * Return codes the caller owns the mailbox command after the return of the
8912 * function.
8913 **/
8914int
8915lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8916{
8917 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8918}
8919
8920/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008921 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008922 * @phba: The hba struct for which this call is being executed.
8923 * @dev_grp: The HBA PCI-Device group number.
8924 *
8925 * This routine sets up the mbox interface API function jump table in @phba
8926 * struct.
8927 * Returns: 0 - success, -ENODEV - failure.
8928 **/
8929int
8930lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8931{
8932
8933 switch (dev_grp) {
8934 case LPFC_PCI_DEV_LP:
8935 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8936 phba->lpfc_sli_handle_slow_ring_event =
8937 lpfc_sli_handle_slow_ring_event_s3;
8938 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8939 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8940 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8941 break;
8942 case LPFC_PCI_DEV_OC:
8943 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8944 phba->lpfc_sli_handle_slow_ring_event =
8945 lpfc_sli_handle_slow_ring_event_s4;
8946 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8947 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8948 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8949 break;
8950 default:
8951 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8952 "1420 Invalid HBA PCI-device group: 0x%x\n",
8953 dev_grp);
8954 return -ENODEV;
8955 break;
8956 }
8957 return 0;
8958}
8959
8960/**
James Smart3621a712009-04-06 18:47:14 -04008961 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008962 * @phba: Pointer to HBA context object.
8963 * @pring: Pointer to driver SLI ring object.
8964 * @piocb: Pointer to address of newly added command iocb.
8965 *
8966 * This function is called with hbalock held to add a command
8967 * iocb to the txq when SLI layer cannot submit the command iocb
8968 * to the ring.
8969 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008970void
James Smart92d7f7b2007-06-17 19:56:38 -05008971__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008972 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008973{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008974 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008975 /* Insert the caller's iocb in the txq tail for later processing. */
8976 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008977}
8978
James Smarte59058c2008-08-24 21:49:00 -04008979/**
James Smart3621a712009-04-06 18:47:14 -04008980 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008981 * @phba: Pointer to HBA context object.
8982 * @pring: Pointer to driver SLI ring object.
8983 * @piocb: Pointer to address of newly added command iocb.
8984 *
8985 * This function is called with hbalock held before a new
8986 * iocb is submitted to the firmware. This function checks
8987 * txq to flush the iocbs in txq to Firmware before
8988 * submitting new iocbs to the Firmware.
8989 * If there are iocbs in the txq which need to be submitted
8990 * to firmware, lpfc_sli_next_iocb returns the first element
8991 * of the txq after dequeuing it from txq.
8992 * If there is no iocb in the txq then the function will return
8993 * *piocb and *piocb is set to NULL. Caller needs to check
8994 * *piocb to find if there are more commands in the txq.
8995 **/
dea31012005-04-17 16:05:31 -05008996static struct lpfc_iocbq *
8997lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008998 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008999{
9000 struct lpfc_iocbq * nextiocb;
9001
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009002 lockdep_assert_held(&phba->hbalock);
9003
dea31012005-04-17 16:05:31 -05009004 nextiocb = lpfc_sli_ringtx_get(phba, pring);
9005 if (!nextiocb) {
9006 nextiocb = *piocb;
9007 *piocb = NULL;
9008 }
9009
9010 return nextiocb;
9011}
9012
James Smarte59058c2008-08-24 21:49:00 -04009013/**
James Smart3772a992009-05-22 14:50:54 -04009014 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009015 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04009016 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04009017 * @piocb: Pointer to command iocb.
9018 * @flag: Flag indicating if this command can be put into txq.
9019 *
James Smart3772a992009-05-22 14:50:54 -04009020 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
9021 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
9022 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
9023 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
9024 * this function allows only iocbs for posting buffers. This function finds
9025 * next available slot in the command ring and posts the command to the
9026 * available slot and writes the port attention register to request HBA start
9027 * processing new iocb. If there is no slot available in the ring and
9028 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
9029 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04009030 *
James Smart3772a992009-05-22 14:50:54 -04009031 * This function is called with hbalock held. The function will return success
9032 * after it successfully submit the iocb to firmware or after adding to the
9033 * txq.
James Smarte59058c2008-08-24 21:49:00 -04009034 **/
James Smart98c9ea52007-10-27 13:37:33 -04009035static int
James Smart3772a992009-05-22 14:50:54 -04009036__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05009037 struct lpfc_iocbq *piocb, uint32_t flag)
9038{
9039 struct lpfc_iocbq *nextiocb;
9040 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08009041 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05009042
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009043 lockdep_assert_held(&phba->hbalock);
9044
James Smart92d7f7b2007-06-17 19:56:38 -05009045 if (piocb->iocb_cmpl && (!piocb->vport) &&
9046 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
9047 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
9048 lpfc_printf_log(phba, KERN_ERR,
9049 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04009050 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05009051 piocb->iocb.ulpCommand);
9052 dump_stack();
9053 return IOCB_ERROR;
9054 }
9055
9056
Linas Vepstas8d63f372007-02-14 14:28:36 -06009057 /* If the PCI channel is in offline state, do not post iocbs. */
9058 if (unlikely(pci_channel_offline(phba->pcidev)))
9059 return IOCB_ERROR;
9060
James Smarta257bf92009-04-06 18:48:10 -04009061 /* If HBA has a deferred error attention, fail the iocb. */
9062 if (unlikely(phba->hba_flag & DEFER_ERATT))
9063 return IOCB_ERROR;
9064
dea31012005-04-17 16:05:31 -05009065 /*
9066 * We should never get an IOCB if we are in a < LINK_DOWN state
9067 */
James Smart2e0fef82007-06-17 19:56:36 -05009068 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05009069 return IOCB_ERROR;
9070
9071 /*
9072 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04009073 * outstanding event.
dea31012005-04-17 16:05:31 -05009074 */
James Smart0b727fe2007-10-27 13:37:25 -04009075 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05009076 goto iocb_busy;
9077
James Smart2e0fef82007-06-17 19:56:36 -05009078 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05009079 /*
James Smart2680eea2007-04-25 09:52:55 -04009080 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05009081 * can be issued if the link is not up.
9082 */
9083 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04009084 case CMD_GEN_REQUEST64_CR:
9085 case CMD_GEN_REQUEST64_CX:
9086 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
9087 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04009088 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04009089 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
9090 MENLO_TRANSPORT_TYPE))
9091
9092 goto iocb_busy;
9093 break;
dea31012005-04-17 16:05:31 -05009094 case CMD_QUE_RING_BUF_CN:
9095 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05009096 /*
9097 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
9098 * completion, iocb_cmpl MUST be 0.
9099 */
9100 if (piocb->iocb_cmpl)
9101 piocb->iocb_cmpl = NULL;
9102 /*FALLTHROUGH*/
9103 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04009104 case CMD_CLOSE_XRI_CN:
9105 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05009106 break;
9107 default:
9108 goto iocb_busy;
9109 }
9110
9111 /*
9112 * For FCP commands, we must be in a state where we can process link
9113 * attention events.
9114 */
James Smart895427b2017-02-12 13:52:30 -08009115 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05009116 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05009117 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05009118 }
dea31012005-04-17 16:05:31 -05009119
dea31012005-04-17 16:05:31 -05009120 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
9121 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
9122 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
9123
9124 if (iocb)
9125 lpfc_sli_update_ring(phba, pring);
9126 else
9127 lpfc_sli_update_full_ring(phba, pring);
9128
9129 if (!piocb)
9130 return IOCB_SUCCESS;
9131
9132 goto out_busy;
9133
9134 iocb_busy:
9135 pring->stats.iocb_cmd_delay++;
9136
9137 out_busy:
9138
9139 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05009140 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05009141 return IOCB_SUCCESS;
9142 }
9143
9144 return IOCB_BUSY;
9145}
9146
James Smart3772a992009-05-22 14:50:54 -04009147/**
James Smart4f774512009-05-22 14:52:35 -04009148 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
9149 * @phba: Pointer to HBA context object.
9150 * @piocb: Pointer to command iocb.
9151 * @sglq: Pointer to the scatter gather queue object.
9152 *
9153 * This routine converts the bpl or bde that is in the IOCB
9154 * to a sgl list for the sli4 hardware. The physical address
9155 * of the bpl/bde is converted back to a virtual address.
9156 * If the IOCB contains a BPL then the list of BDE's is
9157 * converted to sli4_sge's. If the IOCB contains a single
9158 * BDE then it is converted to a single sli_sge.
9159 * The IOCB is still in cpu endianess so the contents of
9160 * the bpl can be used without byte swapping.
9161 *
9162 * Returns valid XRI = Success, NO_XRI = Failure.
9163**/
9164static uint16_t
9165lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
9166 struct lpfc_sglq *sglq)
9167{
9168 uint16_t xritag = NO_XRI;
9169 struct ulp_bde64 *bpl = NULL;
9170 struct ulp_bde64 bde;
9171 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05009172 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04009173 IOCB_t *icmd;
9174 int numBdes = 0;
9175 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05009176 uint32_t offset = 0; /* accumulated offset in the sg request list */
9177 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04009178
9179 if (!piocbq || !sglq)
9180 return xritag;
9181
9182 sgl = (struct sli4_sge *)sglq->sgl;
9183 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05009184 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
9185 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009186 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9187 numBdes = icmd->un.genreq64.bdl.bdeSize /
9188 sizeof(struct ulp_bde64);
9189 /* The addrHigh and addrLow fields within the IOCB
9190 * have not been byteswapped yet so there is no
9191 * need to swap them back.
9192 */
James Smart1b511972011-12-13 13:23:09 -05009193 if (piocbq->context3)
9194 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
9195 else
9196 return xritag;
James Smart4f774512009-05-22 14:52:35 -04009197
James Smart1b511972011-12-13 13:23:09 -05009198 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04009199 if (!bpl)
9200 return xritag;
9201
9202 for (i = 0; i < numBdes; i++) {
9203 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05009204 sgl->addr_hi = bpl->addrHigh;
9205 sgl->addr_lo = bpl->addrLow;
9206
James Smart05580562011-05-24 11:40:48 -04009207 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009208 if ((i+1) == numBdes)
9209 bf_set(lpfc_sli4_sge_last, sgl, 1);
9210 else
9211 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05009212 /* swap the size field back to the cpu so we
9213 * can assign it to the sgl.
9214 */
9215 bde.tus.w = le32_to_cpu(bpl->tus.w);
9216 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05009217 /* The offsets in the sgl need to be accumulated
9218 * separately for the request and reply lists.
9219 * The request is always first, the reply follows.
9220 */
9221 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
9222 /* add up the reply sg entries */
9223 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
9224 inbound++;
9225 /* first inbound? reset the offset */
9226 if (inbound == 1)
9227 offset = 0;
9228 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04009229 bf_set(lpfc_sli4_sge_type, sgl,
9230 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05009231 offset += bde.tus.f.bdeSize;
9232 }
James Smart546fc852011-03-11 16:06:29 -05009233 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009234 bpl++;
9235 sgl++;
9236 }
9237 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
9238 /* The addrHigh and addrLow fields of the BDE have not
9239 * been byteswapped yet so they need to be swapped
9240 * before putting them in the sgl.
9241 */
9242 sgl->addr_hi =
9243 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
9244 sgl->addr_lo =
9245 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04009246 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009247 bf_set(lpfc_sli4_sge_last, sgl, 1);
9248 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05009249 sgl->sge_len =
9250 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04009251 }
9252 return sglq->sli4_xritag;
9253}
9254
9255/**
James Smart4f774512009-05-22 14:52:35 -04009256 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
9257 * @phba: Pointer to HBA context object.
9258 * @piocb: Pointer to command iocb.
9259 * @wqe: Pointer to the work queue entry.
9260 *
9261 * This routine converts the iocb command to its Work Queue Entry
9262 * equivalent. The wqe pointer should not have any fields set when
9263 * this routine is called because it will memcpy over them.
9264 * This routine does not set the CQ_ID or the WQEC bits in the
9265 * wqe.
9266 *
9267 * Returns: 0 = Success, IOCB_ERROR = Failure.
9268 **/
9269static int
9270lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
James Smart205e8242018-03-05 12:04:03 -08009271 union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -04009272{
James Smart5ffc2662009-11-18 15:39:44 -05009273 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04009274 uint8_t ct = 0;
9275 uint32_t fip;
9276 uint32_t abort_tag;
9277 uint8_t command_type = ELS_COMMAND_NON_FIP;
9278 uint8_t cmnd;
9279 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04009280 uint16_t abrt_iotag;
9281 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04009282 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04009283 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05009284 int numBdes, i;
9285 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04009286 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05009287 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05009288 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04009289
James Smart45ed1192009-10-02 15:17:02 -04009290 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04009291 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04009292 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04009293 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05009294 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04009295 command_type = ELS_COMMAND_FIP;
9296 else
9297 command_type = ELS_COMMAND_NON_FIP;
9298
James Smartb5c53952016-03-31 14:12:30 -07009299 if (phba->fcp_embed_io)
9300 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04009301 /* Some of the fields are in the right position already */
9302 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smarte62245d2019-08-14 16:57:08 -07009303 /* The ct field has moved so reset */
9304 wqe->generic.wqe_com.word7 = 0;
9305 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07009306
9307 abort_tag = (uint32_t) iocbq->iotag;
9308 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009309 /* words0-2 bpl convert bde */
9310 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05009311 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9312 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04009313 bpl = (struct ulp_bde64 *)
9314 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
9315 if (!bpl)
9316 return IOCB_ERROR;
9317
9318 /* Should already be byte swapped. */
9319 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
9320 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
9321 /* swap the size field back to the cpu so we
9322 * can assign it to the sgl.
9323 */
9324 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05009325 xmit_len = wqe->generic.bde.tus.f.bdeSize;
9326 total_len = 0;
9327 for (i = 0; i < numBdes; i++) {
9328 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
9329 total_len += bde.tus.f.bdeSize;
9330 }
James Smart4f774512009-05-22 14:52:35 -04009331 } else
James Smart5ffc2662009-11-18 15:39:44 -05009332 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04009333
9334 iocbq->iocb.ulpIoTag = iocbq->iotag;
9335 cmnd = iocbq->iocb.ulpCommand;
9336
9337 switch (iocbq->iocb.ulpCommand) {
9338 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04009339 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
9340 ndlp = iocbq->context_un.ndlp;
9341 else
9342 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009343 if (!iocbq->iocb.ulpLe) {
9344 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9345 "2007 Only Limited Edition cmd Format"
9346 " supported 0x%x\n",
9347 iocbq->iocb.ulpCommand);
9348 return IOCB_ERROR;
9349 }
James Smartff78d8f2011-12-13 13:21:35 -05009350
James Smart5ffc2662009-11-18 15:39:44 -05009351 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009352 /* Els_reguest64 has a TMO */
9353 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
9354 iocbq->iocb.ulpTimeout);
9355 /* Need a VF for word 4 set the vf bit*/
9356 bf_set(els_req64_vf, &wqe->els_req, 0);
9357 /* And a VFID for word 12 */
9358 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04009359 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04009360 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9361 iocbq->iocb.ulpContext);
9362 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9363 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04009364 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05009365 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05009366 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9367 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05009368 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9369 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05009370 if_type = bf_get(lpfc_sli_intf_if_type,
9371 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009372 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05009373 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05009374 *pcmd == ELS_CMD_SCR ||
James Smartf60cb932019-05-14 14:58:05 -07009375 *pcmd == ELS_CMD_RSCN_XMT ||
James Smart6b5151f2012-01-18 16:24:06 -05009376 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05009377 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05009378 *pcmd == ELS_CMD_PLOGI)) {
9379 bf_set(els_req64_sp, &wqe->els_req, 1);
9380 bf_set(els_req64_sid, &wqe->els_req,
9381 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009382 if ((*pcmd == ELS_CMD_FLOGI) &&
9383 !(phba->fc_topology ==
9384 LPFC_TOPOLOGY_LOOP))
9385 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05009386 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9387 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04009388 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05009389 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05009390 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9391 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9392 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9393 }
James Smartc8685952009-11-18 15:39:16 -05009394 }
James Smart6d368e52011-05-24 11:44:12 -04009395 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9396 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009397 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9398 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9399 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9400 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9401 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9402 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009403 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04009404 break;
James Smart5ffc2662009-11-18 15:39:44 -05009405 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04009406 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9407 iocbq->iocb.un.ulpWord[3]);
9408 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009409 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05009410 /* The entire sequence is transmitted for this IOCB */
9411 xmit_len = total_len;
9412 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05009413 if (phba->link_flag & LS_LOOPBACK_MODE)
9414 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -06009415 /* fall through */
James Smart4f774512009-05-22 14:52:35 -04009416 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04009417 /* word3 iocb=io_tag32 wqe=reserved */
9418 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04009419 /* word4 relative_offset memcpy */
9420 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009421 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9422 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9423 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9424 LPFC_WQE_IOD_WRITE);
9425 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9426 LPFC_WQE_LENLOC_WORD12);
9427 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05009428 wqe->xmit_sequence.xmit_len = xmit_len;
9429 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009430 break;
James Smart4f774512009-05-22 14:52:35 -04009431 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04009432 /* word3 iocb=iotag32 wqe=seq_payload_len */
9433 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009434 /* word4 iocb=rsvd wqe=rsvd */
9435 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9436 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04009437 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009438 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04009439 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9440 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9441 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9442 LPFC_WQE_LENLOC_WORD3);
9443 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04009444 break;
James Smart4f774512009-05-22 14:52:35 -04009445 case CMD_FCP_IWRITE64_CR:
9446 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04009447 /* word3 iocb=iotag wqe=payload_offset_len */
9448 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009449 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9450 xmit_len + sizeof(struct fcp_rsp));
9451 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9452 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009453 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9454 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9455 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9456 iocbq->iocb.ulpFCP2Rcvy);
9457 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9458 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009459 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9460 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9461 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009462 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009463 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009464 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9465 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009466 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9467 if (iocbq->priority) {
9468 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9469 (iocbq->priority << 1));
9470 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009471 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9472 (phba->cfg_XLanePriority << 1));
9473 }
9474 }
James Smartb5c53952016-03-31 14:12:30 -07009475 /* Note, word 10 is already initialized to 0 */
9476
James Smart414abe02018-06-26 08:24:26 -07009477 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9478 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009479 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9480 else
9481 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9482
James Smartb5c53952016-03-31 14:12:30 -07009483 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009484 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009485 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009486 struct fcp_cmnd *fcp_cmnd;
9487 uint32_t *ptr;
9488
9489 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009490
9491 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009492 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009493 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9494
9495 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009496 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009497 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009498 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9499 wqe->generic.bde.addrHigh = 0;
9500 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009501
James Smart205e8242018-03-05 12:04:03 -08009502 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9503 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009504
9505 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009506 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009507 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9508 }
James Smart7851fe22011-07-22 18:36:52 -04009509 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04009510 case CMD_FCP_IREAD64_CR:
9511 /* word3 iocb=iotag wqe=payload_offset_len */
9512 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009513 bf_set(payload_offset_len, &wqe->fcp_iread,
9514 xmit_len + sizeof(struct fcp_rsp));
9515 bf_set(cmd_buff_len, &wqe->fcp_iread,
9516 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009517 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9518 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9519 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9520 iocbq->iocb.ulpFCP2Rcvy);
9521 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04009522 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009523 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9524 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9525 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009526 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009527 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009528 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9529 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009530 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9531 if (iocbq->priority) {
9532 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9533 (iocbq->priority << 1));
9534 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009535 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9536 (phba->cfg_XLanePriority << 1));
9537 }
9538 }
James Smartb5c53952016-03-31 14:12:30 -07009539 /* Note, word 10 is already initialized to 0 */
9540
James Smart414abe02018-06-26 08:24:26 -07009541 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9542 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009543 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9544 else
9545 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9546
James Smartb5c53952016-03-31 14:12:30 -07009547 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009548 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009549 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009550 struct fcp_cmnd *fcp_cmnd;
9551 uint32_t *ptr;
9552
9553 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009554
9555 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009556 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009557 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9558
9559 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009560 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009561 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009562 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9563 wqe->generic.bde.addrHigh = 0;
9564 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009565
James Smart205e8242018-03-05 12:04:03 -08009566 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9567 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009568
9569 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009570 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009571 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9572 }
James Smart7851fe22011-07-22 18:36:52 -04009573 break;
James Smartf1126682009-06-10 17:22:44 -04009574 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04009575 /* word3 iocb=iotag wqe=payload_offset_len */
9576 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9577 bf_set(payload_offset_len, &wqe->fcp_icmd,
9578 xmit_len + sizeof(struct fcp_rsp));
9579 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9580 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009581 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04009582 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04009583 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009584 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9585 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9586 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9587 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9588 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04009589 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9590 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009591 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9592 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009593 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9594 if (iocbq->priority) {
9595 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9596 (iocbq->priority << 1));
9597 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009598 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9599 (phba->cfg_XLanePriority << 1));
9600 }
9601 }
James Smartb5c53952016-03-31 14:12:30 -07009602 /* Note, word 10 is already initialized to 0 */
9603
9604 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009605 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009606 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009607 struct fcp_cmnd *fcp_cmnd;
9608 uint32_t *ptr;
9609
9610 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009611
9612 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009613 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009614 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9615
9616 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009617 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009618 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009619 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9620 wqe->generic.bde.addrHigh = 0;
9621 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009622
James Smart205e8242018-03-05 12:04:03 -08009623 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9624 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009625
9626 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009627 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009628 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9629 }
James Smart7851fe22011-07-22 18:36:52 -04009630 break;
James Smart4f774512009-05-22 14:52:35 -04009631 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009632 /* For this command calculate the xmit length of the
9633 * request bde.
9634 */
9635 xmit_len = 0;
9636 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9637 sizeof(struct ulp_bde64);
9638 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009639 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009640 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9641 break;
James Smart63e801c2010-11-20 23:14:19 -05009642 xmit_len += bde.tus.f.bdeSize;
9643 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009644 /* word3 iocb=IO_TAG wqe=request_payload_len */
9645 wqe->gen_req.request_payload_len = xmit_len;
9646 /* word4 iocb=parameter wqe=relative_offset memcpy */
9647 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009648 /* word6 context tag copied in memcpy */
9649 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9650 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9651 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9652 "2015 Invalid CT %x command 0x%x\n",
9653 ct, iocbq->iocb.ulpCommand);
9654 return IOCB_ERROR;
9655 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009656 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9657 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9658 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9659 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9660 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9661 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9662 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9663 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009664 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009665 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009666 break;
James Smart4f774512009-05-22 14:52:35 -04009667 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009668 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009669 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009670 /* word3 iocb=iotag32 wqe=response_payload_len */
9671 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009672 /* word4 */
9673 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009674 /* word5 iocb=rsvd wge=did */
9675 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009676 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9677
9678 if_type = bf_get(lpfc_sli_intf_if_type,
9679 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009680 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -04009681 if (iocbq->vport->fc_flag & FC_PT2PT) {
9682 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9683 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9684 iocbq->vport->fc_myDID);
9685 if (iocbq->vport->fc_myDID == Fabric_DID) {
9686 bf_set(wqe_els_did,
9687 &wqe->xmit_els_rsp.wqe_dest, 0);
9688 }
9689 }
9690 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009691 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9692 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9693 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9694 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009695 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009696 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009697 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009698 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009699 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9700 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9701 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9702 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9703 LPFC_WQE_LENLOC_WORD3);
9704 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009705 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9706 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009707 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9708 iocbq->context2)->virt);
9709 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009710 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9711 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009712 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009713 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9714 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009715 phba->vpi_ids[phba->pport->vpi]);
9716 }
James Smart4f774512009-05-22 14:52:35 -04009717 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009718 break;
James Smart4f774512009-05-22 14:52:35 -04009719 case CMD_CLOSE_XRI_CN:
9720 case CMD_ABORT_XRI_CN:
9721 case CMD_ABORT_XRI_CX:
9722 /* words 0-2 memcpy should be 0 rserved */
9723 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009724 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9725 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9726 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9727 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9728 } else
9729 fip = 0;
9730
9731 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009732 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009733 * The link is down, or the command was ELS_FIP
9734 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009735 * on the wire.
9736 */
9737 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9738 else
9739 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9740 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009741 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9742 wqe->abort_cmd.rsrvd5 = 0;
9743 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009744 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9745 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009746 /*
9747 * The abort handler will send us CMD_ABORT_XRI_CN or
9748 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9749 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009750 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9751 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9752 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9753 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009754 cmnd = CMD_ABORT_XRI_CX;
9755 command_type = OTHER_COMMAND;
9756 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009757 break;
James Smart6669f9b2009-10-02 15:16:45 -04009758 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009759 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009760 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009761 * we re-construct this WQE here based on information in
9762 * iocbq from scratch.
9763 */
James Smartd9f492a2019-08-14 16:57:04 -07009764 memset(wqe, 0, sizeof(*wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009765 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009766 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009767 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9768 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009769 LPFC_ABTS_UNSOL_INT) {
9770 /* ABTS sent by initiator to CT exchange, the
9771 * RX_ID field will be filled with the newly
9772 * allocated responder XRI.
9773 */
9774 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9775 iocbq->sli4_xritag);
9776 } else {
9777 /* ABTS sent by responder to CT exchange, the
9778 * RX_ID field will be filled with the responder
9779 * RX_ID from ABTS.
9780 */
9781 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009782 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009783 }
James Smart6669f9b2009-10-02 15:16:45 -04009784 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9785 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009786
9787 /* Use CT=VPI */
9788 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9789 ndlp->nlp_DID);
9790 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9791 iocbq->iocb.ulpContext);
9792 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009793 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009794 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009795 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9796 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9797 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009798 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9799 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009800 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9801 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9802 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9803 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9804 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9805 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9806 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9807 }
9808
James Smart7851fe22011-07-22 18:36:52 -04009809 break;
James Smartae9e28f2017-05-15 15:20:51 -07009810 case CMD_SEND_FRAME:
James Smarte62245d2019-08-14 16:57:08 -07009811 bf_set(wqe_cmnd, &wqe->generic.wqe_com, CMD_SEND_FRAME);
9812 bf_set(wqe_sof, &wqe->generic.wqe_com, 0x2E); /* SOF byte */
9813 bf_set(wqe_eof, &wqe->generic.wqe_com, 0x41); /* EOF byte */
9814 bf_set(wqe_lenloc, &wqe->generic.wqe_com, 1);
9815 bf_set(wqe_xbl, &wqe->generic.wqe_com, 1);
9816 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
9817 bf_set(wqe_xc, &wqe->generic.wqe_com, 1);
9818 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, 0xA);
9819 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smartae9e28f2017-05-15 15:20:51 -07009820 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9821 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9822 return 0;
James Smart4f774512009-05-22 14:52:35 -04009823 case CMD_XRI_ABORTED_CX:
9824 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009825 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9826 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9827 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9828 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9829 default:
9830 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9831 "2014 Invalid command 0x%x\n",
9832 iocbq->iocb.ulpCommand);
9833 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009834 break;
James Smart4f774512009-05-22 14:52:35 -04009835 }
James Smart6d368e52011-05-24 11:44:12 -04009836
James Smart8012cc32012-10-31 14:44:49 -04009837 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9838 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9839 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9840 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9841 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9842 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9843 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9844 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009845 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9846 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9847 wqe->generic.wqe_com.abort_tag = abort_tag;
9848 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9849 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9850 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9851 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009852 return 0;
9853}
9854
9855/**
9856 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9857 * @phba: Pointer to HBA context object.
9858 * @ring_number: SLI ring number to issue iocb on.
9859 * @piocb: Pointer to command iocb.
9860 * @flag: Flag indicating if this command can be put into txq.
9861 *
9862 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9863 * an iocb command to an HBA with SLI-4 interface spec.
9864 *
9865 * This function is called with hbalock held. The function will return success
9866 * after it successfully submit the iocb to firmware or after adding to the
9867 * txq.
9868 **/
9869static int
9870__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9871 struct lpfc_iocbq *piocb, uint32_t flag)
9872{
9873 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -08009874 union lpfc_wqe128 wqe;
James Smart1ba981f2014-02-20 09:56:45 -05009875 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009876 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009877
James Smart895427b2017-02-12 13:52:30 -08009878 /* Get the WQ */
9879 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9880 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc00f62e2019-08-14 16:57:11 -07009881 wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;
James Smart895427b2017-02-12 13:52:30 -08009882 } else {
9883 wq = phba->sli4_hba.els_wq;
9884 }
9885
9886 /* Get corresponding ring */
9887 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009888
James Smartb5c53952016-03-31 14:12:30 -07009889 /*
9890 * The WQE can be either 64 or 128 bytes,
James Smartb5c53952016-03-31 14:12:30 -07009891 */
James Smartb5c53952016-03-31 14:12:30 -07009892
James Smartcda7fa182019-03-04 15:15:43 -08009893 lockdep_assert_held(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -08009894
James Smart4f774512009-05-22 14:52:35 -04009895 if (piocb->sli4_xritag == NO_XRI) {
9896 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009897 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009898 sglq = NULL;
9899 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009900 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009901 if (!(flag & SLI_IOCB_RET_IOCB)) {
9902 __lpfc_sli_ringtx_put(phba,
9903 pring, piocb);
9904 return IOCB_SUCCESS;
9905 } else {
9906 return IOCB_BUSY;
9907 }
9908 } else {
James Smart895427b2017-02-12 13:52:30 -08009909 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009910 if (!sglq) {
9911 if (!(flag & SLI_IOCB_RET_IOCB)) {
9912 __lpfc_sli_ringtx_put(phba,
9913 pring,
9914 piocb);
9915 return IOCB_SUCCESS;
9916 } else
9917 return IOCB_BUSY;
9918 }
9919 }
James Smart4f774512009-05-22 14:52:35 -04009920 }
James Smart2ea259e2017-02-12 13:52:27 -08009921 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009922 /* These IO's already have an XRI and a mapped sgl. */
9923 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009924 else {
James Smart6d368e52011-05-24 11:44:12 -04009925 /*
9926 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009927 * sglq is on the active list
9928 */
James Smartedccdc12013-01-03 15:43:45 -05009929 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009930 if (!sglq)
9931 return IOCB_ERROR;
9932 }
9933
9934 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009935 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009936 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009937 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009938 return IOCB_ERROR;
9939 }
9940
James Smart205e8242018-03-05 12:04:03 -08009941 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
James Smart4f774512009-05-22 14:52:35 -04009942 return IOCB_ERROR;
9943
James Smart205e8242018-03-05 12:04:03 -08009944 if (lpfc_sli4_wq_put(wq, &wqe))
James Smart895427b2017-02-12 13:52:30 -08009945 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009946 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9947
9948 return 0;
9949}
9950
9951/**
James Smart3772a992009-05-22 14:50:54 -04009952 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9953 *
9954 * This routine wraps the actual lockless version for issusing IOCB function
9955 * pointer from the lpfc_hba struct.
9956 *
9957 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009958 * IOCB_ERROR - Error
9959 * IOCB_SUCCESS - Success
9960 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009961 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009962int
James Smart3772a992009-05-22 14:50:54 -04009963__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9964 struct lpfc_iocbq *piocb, uint32_t flag)
9965{
9966 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9967}
9968
9969/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009970 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009971 * @phba: The hba struct for which this call is being executed.
9972 * @dev_grp: The HBA PCI-Device group number.
9973 *
9974 * This routine sets up the SLI interface API function jump table in @phba
9975 * struct.
9976 * Returns: 0 - success, -ENODEV - failure.
9977 **/
9978int
9979lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9980{
9981
9982 switch (dev_grp) {
9983 case LPFC_PCI_DEV_LP:
9984 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9985 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9986 break;
James Smart4f774512009-05-22 14:52:35 -04009987 case LPFC_PCI_DEV_OC:
9988 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9989 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9990 break;
James Smart3772a992009-05-22 14:50:54 -04009991 default:
9992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9993 "1419 Invalid HBA PCI-device group: 0x%x\n",
9994 dev_grp);
9995 return -ENODEV;
9996 break;
9997 }
9998 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9999 return 0;
10000}
James Smart92d7f7b2007-06-17 19:56:38 -050010001
James Smarta1efe162015-05-21 13:55:20 -040010002/**
James Smart895427b2017-02-12 13:52:30 -080010003 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -040010004 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -040010005 * @piocb: Pointer to command iocb.
10006 *
James Smart895427b2017-02-12 13:52:30 -080010007 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
10008 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -040010009 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -080010010 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -040010011 */
James Smart895427b2017-02-12 13:52:30 -080010012struct lpfc_sli_ring *
10013lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -040010014{
James Smartc4908502019-01-28 11:14:28 -080010015 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -080010016
James Smart895427b2017-02-12 13:52:30 -080010017 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smartcdb42be2019-01-28 11:14:21 -080010018 if (unlikely(!phba->sli4_hba.hdwq))
James Smart7370d102019-01-28 11:14:20 -080010019 return NULL;
10020 /*
10021 * for abort iocb hba_wqidx should already
10022 * be setup based on what work queue we used.
10023 */
10024 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc4908502019-01-28 11:14:28 -080010025 lpfc_cmd = (struct lpfc_io_buf *)piocb->context1;
James Smart1fbf9742019-01-28 11:14:26 -080010026 piocb->hba_wqidx = lpfc_cmd->hdwq_no;
James Smart9bd2bff52014-09-03 12:57:30 -040010027 }
James Smartc00f62e2019-08-14 16:57:11 -070010028 return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010029 } else {
10030 if (unlikely(!phba->sli4_hba.els_wq))
10031 return NULL;
10032 piocb->hba_wqidx = 0;
10033 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -040010034 }
James Smart9bd2bff52014-09-03 12:57:30 -040010035}
10036
James Smarte59058c2008-08-24 21:49:00 -040010037/**
James Smart3621a712009-04-06 18:47:14 -040010038 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -040010039 * @phba: Pointer to HBA context object.
10040 * @pring: Pointer to driver SLI ring object.
10041 * @piocb: Pointer to command iocb.
10042 * @flag: Flag indicating if this command can be put into txq.
10043 *
10044 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
10045 * function. This function gets the hbalock and calls
10046 * __lpfc_sli_issue_iocb function and will return the error returned
10047 * by __lpfc_sli_issue_iocb function. This wrapper is used by
10048 * functions which do not hold hbalock.
10049 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010050int
James Smart3772a992009-05-22 14:50:54 -040010051lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -050010052 struct lpfc_iocbq *piocb, uint32_t flag)
10053{
James Smart2a76a282012-08-03 12:35:54 -040010054 struct lpfc_sli_ring *pring;
James Smart92d7f7b2007-06-17 19:56:38 -050010055 unsigned long iflags;
James Smart6a828b02019-01-28 11:14:31 -080010056 int rc;
James Smart92d7f7b2007-06-17 19:56:38 -050010057
James Smart7e56aa22012-08-03 12:35:34 -040010058 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010059 pring = lpfc_sli4_calc_ring(phba, piocb);
10060 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010061 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -040010062
James Smart9bd2bff52014-09-03 12:57:30 -040010063 spin_lock_irqsave(&pring->ring_lock, iflags);
10064 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10065 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart7e56aa22012-08-03 12:35:34 -040010066 } else {
10067 /* For now, SLI2/3 will still use hbalock */
10068 spin_lock_irqsave(&phba->hbalock, iflags);
10069 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10070 spin_unlock_irqrestore(&phba->hbalock, iflags);
10071 }
James Smart92d7f7b2007-06-17 19:56:38 -050010072 return rc;
10073}
10074
James Smarte59058c2008-08-24 21:49:00 -040010075/**
James Smart3621a712009-04-06 18:47:14 -040010076 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010077 * @phba: Pointer to HBA context object.
10078 *
10079 * This function is called while driver attaches with the
10080 * HBA to setup the extra ring. The extra ring is used
10081 * only when driver needs to support target mode functionality
10082 * or IP over FC functionalities.
10083 *
James Smart895427b2017-02-12 13:52:30 -080010084 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -040010085 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010086static int
10087lpfc_extra_ring_setup( struct lpfc_hba *phba)
10088{
10089 struct lpfc_sli *psli;
10090 struct lpfc_sli_ring *pring;
10091
10092 psli = &phba->sli;
10093
10094 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -050010095
10096 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -080010097 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -040010098 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10099 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10100 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10101 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010102
James Smarta4bc3372006-12-02 13:34:16 -050010103 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -080010104 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -050010105
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
10111 /* Setup default profile for this ring */
10112 pring->iotag_max = 4096;
10113 pring->num_mask = 1;
10114 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -050010115 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
10116 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010117 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
10118 return 0;
10119}
10120
James Smartcb69f7d2011-12-13 13:21:57 -050010121/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
10122 * @phba: Pointer to HBA context object.
10123 * @iocbq: Pointer to iocb object.
10124 *
10125 * The async_event handler calls this routine when it receives
10126 * an ASYNC_STATUS_CN event from the port. The port generates
10127 * this event when an Abort Sequence request to an rport fails
10128 * twice in succession. The abort could be originated by the
10129 * driver or by the port. The ABTS could have been for an ELS
10130 * or FCP IO. The port only generates this event when an ABTS
10131 * fails to complete after one retry.
10132 */
10133static void
10134lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
10135 struct lpfc_iocbq *iocbq)
10136{
10137 struct lpfc_nodelist *ndlp = NULL;
10138 uint16_t rpi = 0, vpi = 0;
10139 struct lpfc_vport *vport = NULL;
10140
10141 /* The rpi in the ulpContext is vport-sensitive. */
10142 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
10143 rpi = iocbq->iocb.ulpContext;
10144
10145 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10146 "3092 Port generated ABTS async event "
10147 "on vpi %d rpi %d status 0x%x\n",
10148 vpi, rpi, iocbq->iocb.ulpStatus);
10149
10150 vport = lpfc_find_vport_by_vpid(phba, vpi);
10151 if (!vport)
10152 goto err_exit;
10153 ndlp = lpfc_findnode_rpi(vport, rpi);
10154 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
10155 goto err_exit;
10156
10157 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
10158 lpfc_sli_abts_recover_port(vport, ndlp);
10159 return;
10160
10161 err_exit:
10162 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10163 "3095 Event Context not found, no "
10164 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
10165 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
10166 vpi, rpi);
10167}
10168
10169/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
10170 * @phba: pointer to HBA context object.
10171 * @ndlp: nodelist pointer for the impacted rport.
10172 * @axri: pointer to the wcqe containing the failed exchange.
10173 *
10174 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
10175 * port. The port generates this event when an abort exchange request to an
10176 * rport fails twice in succession with no reply. The abort could be originated
10177 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
10178 */
10179void
10180lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
10181 struct lpfc_nodelist *ndlp,
10182 struct sli4_wcqe_xri_aborted *axri)
10183{
10184 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -050010185 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -050010186
James Smart6b5151f2012-01-18 16:24:06 -050010187 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -050010188 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10189 "3115 Node Context not found, driver "
10190 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -050010191 return;
10192 }
10193
James Smartcb69f7d2011-12-13 13:21:57 -050010194 vport = ndlp->vport;
10195 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10196 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -050010197 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -040010198 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -050010199 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -050010200 bf_get(lpfc_wcqe_xa_status, axri),
10201 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -050010202
James Smart5c1db2a2012-03-01 22:34:36 -050010203 /*
10204 * Catch the ABTS protocol failure case. Older OCe FW releases returned
10205 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
10206 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
10207 */
James Smarte3d2b802012-08-14 14:25:43 -040010208 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -050010209 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
10210 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -050010211 lpfc_sli_abts_recover_port(vport, ndlp);
10212}
10213
James Smarte59058c2008-08-24 21:49:00 -040010214/**
James Smart3621a712009-04-06 18:47:14 -040010215 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -040010216 * @phba: Pointer to HBA context object.
10217 * @pring: Pointer to driver SLI ring object.
10218 * @iocbq: Pointer to iocb object.
10219 *
10220 * This function is called by the slow ring event handler
10221 * function when there is an ASYNC event iocb in the ring.
10222 * This function is called with no lock held.
10223 * Currently this function handles only temperature related
10224 * ASYNC events. The function decodes the temperature sensor
10225 * event message and posts events for the management applications.
10226 **/
James Smart98c9ea52007-10-27 13:37:33 -040010227static void
James Smart57127f12007-10-27 13:37:05 -040010228lpfc_sli_async_event_handler(struct lpfc_hba * phba,
10229 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
10230{
10231 IOCB_t *icmd;
10232 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -040010233 struct temp_event temp_event_data;
10234 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -040010235 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -040010236
10237 icmd = &iocbq->iocb;
10238 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -040010239
James Smartcb69f7d2011-12-13 13:21:57 -050010240 switch (evt_code) {
10241 case ASYNC_TEMP_WARN:
10242 case ASYNC_TEMP_SAFE:
10243 temp_event_data.data = (uint32_t) icmd->ulpContext;
10244 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
10245 if (evt_code == ASYNC_TEMP_WARN) {
10246 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
10247 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10248 "0347 Adapter is very hot, please take "
10249 "corrective action. temperature : %d Celsius\n",
10250 (uint32_t) icmd->ulpContext);
10251 } else {
10252 temp_event_data.event_code = LPFC_NORMAL_TEMP;
10253 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10254 "0340 Adapter temperature is OK now. "
10255 "temperature : %d Celsius\n",
10256 (uint32_t) icmd->ulpContext);
10257 }
10258
10259 /* Send temperature change event to applications */
10260 shost = lpfc_shost_from_vport(phba->pport);
10261 fc_host_post_vendor_event(shost, fc_get_event_number(),
10262 sizeof(temp_event_data), (char *) &temp_event_data,
10263 LPFC_NL_VENDOR_ID);
10264 break;
10265 case ASYNC_STATUS_CN:
10266 lpfc_sli_abts_err_handler(phba, iocbq);
10267 break;
10268 default:
James Smarta257bf92009-04-06 18:48:10 -040010269 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -050010270 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -040010271 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -040010272 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -040010273 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
10274 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
10275 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
10276 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -050010277 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -040010278 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
10279 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
10280 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
10281 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
10282
James Smartcb69f7d2011-12-13 13:21:57 -050010283 break;
James Smart57127f12007-10-27 13:37:05 -040010284 }
James Smart57127f12007-10-27 13:37:05 -040010285}
10286
10287
James Smarte59058c2008-08-24 21:49:00 -040010288/**
James Smart895427b2017-02-12 13:52:30 -080010289 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010290 * @phba: Pointer to HBA context object.
10291 *
10292 * lpfc_sli_setup sets up rings of the SLI interface with
10293 * number of iocbs per ring and iotags. This function is
10294 * called while driver attach to the HBA and before the
10295 * interrupts are enabled. So there is no need for locking.
10296 *
10297 * This function always returns 0.
10298 **/
dea31012005-04-17 16:05:31 -050010299int
James Smart895427b2017-02-12 13:52:30 -080010300lpfc_sli4_setup(struct lpfc_hba *phba)
10301{
10302 struct lpfc_sli_ring *pring;
10303
10304 pring = phba->sli4_hba.els_wq->pring;
10305 pring->num_mask = LPFC_MAX_RING_MASK;
10306 pring->prt[0].profile = 0; /* Mask 0 */
10307 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10308 pring->prt[0].type = FC_TYPE_ELS;
10309 pring->prt[0].lpfc_sli_rcv_unsol_event =
10310 lpfc_els_unsol_event;
10311 pring->prt[1].profile = 0; /* Mask 1 */
10312 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10313 pring->prt[1].type = FC_TYPE_ELS;
10314 pring->prt[1].lpfc_sli_rcv_unsol_event =
10315 lpfc_els_unsol_event;
10316 pring->prt[2].profile = 0; /* Mask 2 */
10317 /* NameServer Inquiry */
10318 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10319 /* NameServer */
10320 pring->prt[2].type = FC_TYPE_CT;
10321 pring->prt[2].lpfc_sli_rcv_unsol_event =
10322 lpfc_ct_unsol_event;
10323 pring->prt[3].profile = 0; /* Mask 3 */
10324 /* NameServer response */
10325 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10326 /* NameServer */
10327 pring->prt[3].type = FC_TYPE_CT;
10328 pring->prt[3].lpfc_sli_rcv_unsol_event =
10329 lpfc_ct_unsol_event;
10330 return 0;
10331}
10332
10333/**
10334 * lpfc_sli_setup - SLI ring setup function
10335 * @phba: Pointer to HBA context object.
10336 *
10337 * lpfc_sli_setup sets up rings of the SLI interface with
10338 * number of iocbs per ring and iotags. This function is
10339 * called while driver attach to the HBA and before the
10340 * interrupts are enabled. So there is no need for locking.
10341 *
10342 * This function always returns 0. SLI3 only.
10343 **/
10344int
dea31012005-04-17 16:05:31 -050010345lpfc_sli_setup(struct lpfc_hba *phba)
10346{
James Smarted957682007-06-17 19:56:37 -050010347 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -050010348 struct lpfc_sli *psli = &phba->sli;
10349 struct lpfc_sli_ring *pring;
10350
James Smart2a76a282012-08-03 12:35:54 -040010351 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -050010352 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -050010353
James Bottomley604a3e32005-10-29 10:28:33 -050010354 psli->iocbq_lookup = NULL;
10355 psli->iocbq_lookup_len = 0;
10356 psli->last_iotag = 0;
10357
dea31012005-04-17 16:05:31 -050010358 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010359 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010360 switch (i) {
10361 case LPFC_FCP_RING: /* ring 0 - FCP */
10362 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010363 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10364 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10365 pring->sli.sli3.numCiocb +=
10366 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10367 pring->sli.sli3.numRiocb +=
10368 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10369 pring->sli.sli3.numCiocb +=
10370 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10371 pring->sli.sli3.numRiocb +=
10372 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10373 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010374 SLI3_IOCB_CMD_SIZE :
10375 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010376 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010377 SLI3_IOCB_RSP_SIZE :
10378 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010379 pring->iotag_ctr = 0;
10380 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -050010381 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -050010382 pring->fast_iotag = pring->iotag_max;
10383 pring->num_mask = 0;
10384 break;
James Smarta4bc3372006-12-02 13:34:16 -050010385 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -050010386 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010387 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10388 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10389 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010390 SLI3_IOCB_CMD_SIZE :
10391 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010392 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010393 SLI3_IOCB_RSP_SIZE :
10394 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -050010395 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -050010396 pring->num_mask = 0;
10397 break;
10398 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10399 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010400 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10401 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10402 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010403 SLI3_IOCB_CMD_SIZE :
10404 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010405 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010406 SLI3_IOCB_RSP_SIZE :
10407 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010408 pring->fast_iotag = 0;
10409 pring->iotag_ctr = 0;
10410 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -040010411 pring->lpfc_sli_rcv_async_status =
10412 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -040010413 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -050010414 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -040010415 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10416 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010417 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010418 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010419 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -040010420 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10421 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010422 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010423 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010424 pring->prt[2].profile = 0; /* Mask 2 */
10425 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -040010426 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -050010427 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010428 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010429 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010430 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010431 pring->prt[3].profile = 0; /* Mask 3 */
10432 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -040010433 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -050010434 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010435 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010436 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010437 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010438 break;
10439 }
James Smart7e56aa22012-08-03 12:35:34 -040010440 totiocbsize += (pring->sli.sli3.numCiocb *
10441 pring->sli.sli3.sizeCiocb) +
10442 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050010443 }
James Smarted957682007-06-17 19:56:37 -050010444 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050010445 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040010446 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10447 "SLI2 SLIM Data: x%x x%lx\n",
10448 phba->brd_no, totiocbsize,
10449 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050010450 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010451 if (phba->cfg_multi_ring_support == 2)
10452 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050010453
10454 return 0;
10455}
10456
James Smarte59058c2008-08-24 21:49:00 -040010457/**
James Smart895427b2017-02-12 13:52:30 -080010458 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040010459 * @phba: Pointer to HBA context object.
10460 *
James Smart895427b2017-02-12 13:52:30 -080010461 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040010462 * ring. This function also initializes ring indices of each ring.
10463 * This function is called during the initialization of the SLI
10464 * interface of an HBA.
10465 * This function is called with no lock held and always returns
10466 * 1.
10467 **/
James Smart895427b2017-02-12 13:52:30 -080010468void
10469lpfc_sli4_queue_init(struct lpfc_hba *phba)
10470{
10471 struct lpfc_sli *psli;
10472 struct lpfc_sli_ring *pring;
10473 int i;
10474
10475 psli = &phba->sli;
10476 spin_lock_irq(&phba->hbalock);
10477 INIT_LIST_HEAD(&psli->mboxq);
10478 INIT_LIST_HEAD(&psli->mboxq_cmpl);
10479 /* Initialize list headers for txq and txcmplq as double linked lists */
James Smartcdb42be2019-01-28 11:14:21 -080010480 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -070010481 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010482 pring->flag = 0;
10483 pring->ringno = LPFC_FCP_RING;
James Smartc4908502019-01-28 11:14:28 -080010484 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010485 INIT_LIST_HEAD(&pring->txq);
10486 INIT_LIST_HEAD(&pring->txcmplq);
10487 INIT_LIST_HEAD(&pring->iocb_continueq);
10488 spin_lock_init(&pring->ring_lock);
10489 }
10490 pring = phba->sli4_hba.els_wq->pring;
10491 pring->flag = 0;
10492 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080010493 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010494 INIT_LIST_HEAD(&pring->txq);
10495 INIT_LIST_HEAD(&pring->txcmplq);
10496 INIT_LIST_HEAD(&pring->iocb_continueq);
10497 spin_lock_init(&pring->ring_lock);
10498
James Smartcdb42be2019-01-28 11:14:21 -080010499 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080010500 pring = phba->sli4_hba.nvmels_wq->pring;
10501 pring->flag = 0;
10502 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080010503 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010504 INIT_LIST_HEAD(&pring->txq);
10505 INIT_LIST_HEAD(&pring->txcmplq);
10506 INIT_LIST_HEAD(&pring->iocb_continueq);
10507 spin_lock_init(&pring->ring_lock);
10508 }
10509
10510 spin_unlock_irq(&phba->hbalock);
10511}
10512
10513/**
10514 * lpfc_sli_queue_init - Queue initialization function
10515 * @phba: Pointer to HBA context object.
10516 *
10517 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10518 * ring. This function also initializes ring indices of each ring.
10519 * This function is called during the initialization of the SLI
10520 * interface of an HBA.
10521 * This function is called with no lock held and always returns
10522 * 1.
10523 **/
10524void
10525lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010526{
10527 struct lpfc_sli *psli;
10528 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050010529 int i;
dea31012005-04-17 16:05:31 -050010530
10531 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050010532 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010533 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050010534 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050010535 /* Initialize list headers for txq and txcmplq as double linked lists */
10536 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010537 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010538 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040010539 pring->sli.sli3.next_cmdidx = 0;
10540 pring->sli.sli3.local_getidx = 0;
10541 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050010542 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050010543 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050010544 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080010545 pring->flag = 0;
10546 INIT_LIST_HEAD(&pring->txq);
10547 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010548 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010549 }
James Smart2e0fef82007-06-17 19:56:36 -050010550 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010551}
10552
James Smarte59058c2008-08-24 21:49:00 -040010553/**
James Smart04c68492009-05-22 14:52:52 -040010554 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10555 * @phba: Pointer to HBA context object.
10556 *
10557 * This routine flushes the mailbox command subsystem. It will unconditionally
10558 * flush all the mailbox commands in the three possible stages in the mailbox
10559 * command sub-system: pending mailbox command queue; the outstanding mailbox
10560 * command; and completed mailbox command queue. It is caller's responsibility
10561 * to make sure that the driver is in the proper state to flush the mailbox
10562 * command sub-system. Namely, the posting of mailbox commands into the
10563 * pending mailbox command queue from the various clients must be stopped;
10564 * either the HBA is in a state that it will never works on the outstanding
10565 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10566 * mailbox command has been completed.
10567 **/
10568static void
10569lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10570{
10571 LIST_HEAD(completions);
10572 struct lpfc_sli *psli = &phba->sli;
10573 LPFC_MBOXQ_t *pmb;
10574 unsigned long iflag;
10575
James Smart523128e2018-09-10 10:30:46 -070010576 /* Disable softirqs, including timers from obtaining phba->hbalock */
10577 local_bh_disable();
10578
James Smart04c68492009-05-22 14:52:52 -040010579 /* Flush all the mailbox commands in the mbox system */
10580 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart523128e2018-09-10 10:30:46 -070010581
James Smart04c68492009-05-22 14:52:52 -040010582 /* The pending mailbox command queue */
10583 list_splice_init(&phba->sli.mboxq, &completions);
10584 /* The outstanding active mailbox command */
10585 if (psli->mbox_active) {
10586 list_add_tail(&psli->mbox_active->list, &completions);
10587 psli->mbox_active = NULL;
10588 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10589 }
10590 /* The completed mailbox command queue */
10591 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10592 spin_unlock_irqrestore(&phba->hbalock, iflag);
10593
James Smart523128e2018-09-10 10:30:46 -070010594 /* Enable softirqs again, done with phba->hbalock */
10595 local_bh_enable();
10596
James Smart04c68492009-05-22 14:52:52 -040010597 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10598 while (!list_empty(&completions)) {
10599 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10600 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10601 if (pmb->mbox_cmpl)
10602 pmb->mbox_cmpl(phba, pmb);
10603 }
10604}
10605
10606/**
James Smart3621a712009-04-06 18:47:14 -040010607 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010608 * @vport: Pointer to virtual port object.
10609 *
10610 * lpfc_sli_host_down is called to clean up the resources
10611 * associated with a vport before destroying virtual
10612 * port data structures.
10613 * This function does following operations:
10614 * - Free discovery resources associated with this virtual
10615 * port.
10616 * - Free iocbs associated with this virtual port in
10617 * the txq.
10618 * - Send abort for all iocb commands associated with this
10619 * vport in txcmplq.
10620 *
10621 * This function is called with no lock held and always returns 1.
10622 **/
dea31012005-04-17 16:05:31 -050010623int
James Smart92d7f7b2007-06-17 19:56:38 -050010624lpfc_sli_host_down(struct lpfc_vport *vport)
10625{
James Smart858c9f62007-06-17 19:56:39 -050010626 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010627 struct lpfc_hba *phba = vport->phba;
10628 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010629 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010630 struct lpfc_sli_ring *pring;
10631 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010632 int i;
10633 unsigned long flags = 0;
10634 uint16_t prev_pring_flag;
10635
10636 lpfc_cleanup_discovery_resources(vport);
10637
10638 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010639
James Smart895427b2017-02-12 13:52:30 -080010640 /*
10641 * Error everything on the txq since these iocbs
10642 * have not been given to the FW yet.
10643 * Also issue ABTS for everything on the txcmplq
10644 */
10645 if (phba->sli_rev != LPFC_SLI_REV4) {
10646 for (i = 0; i < psli->num_rings; i++) {
10647 pring = &psli->sli3_ring[i];
10648 prev_pring_flag = pring->flag;
10649 /* Only slow rings */
10650 if (pring->ringno == LPFC_ELS_RING) {
10651 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10652 /* Set the lpfc data pending flag */
10653 set_bit(LPFC_DATA_READY, &phba->data_flags);
10654 }
10655 list_for_each_entry_safe(iocb, next_iocb,
10656 &pring->txq, list) {
10657 if (iocb->vport != vport)
10658 continue;
10659 list_move_tail(&iocb->list, &completions);
10660 }
10661 list_for_each_entry_safe(iocb, next_iocb,
10662 &pring->txcmplq, list) {
10663 if (iocb->vport != vport)
10664 continue;
10665 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10666 }
10667 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010668 }
James Smart895427b2017-02-12 13:52:30 -080010669 } else {
10670 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10671 pring = qp->pring;
10672 if (!pring)
10673 continue;
10674 if (pring == phba->sli4_hba.els_wq->pring) {
10675 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10676 /* Set the lpfc data pending flag */
10677 set_bit(LPFC_DATA_READY, &phba->data_flags);
10678 }
10679 prev_pring_flag = pring->flag;
10680 spin_lock_irq(&pring->ring_lock);
10681 list_for_each_entry_safe(iocb, next_iocb,
10682 &pring->txq, list) {
10683 if (iocb->vport != vport)
10684 continue;
10685 list_move_tail(&iocb->list, &completions);
10686 }
10687 spin_unlock_irq(&pring->ring_lock);
10688 list_for_each_entry_safe(iocb, next_iocb,
10689 &pring->txcmplq, list) {
10690 if (iocb->vport != vport)
10691 continue;
10692 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10693 }
10694 pring->flag = prev_pring_flag;
10695 }
James Smart92d7f7b2007-06-17 19:56:38 -050010696 }
James Smart92d7f7b2007-06-17 19:56:38 -050010697 spin_unlock_irqrestore(&phba->hbalock, flags);
10698
James Smarta257bf92009-04-06 18:48:10 -040010699 /* Cancel all the IOCBs from the completions list */
10700 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10701 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010702 return 1;
10703}
10704
James Smarte59058c2008-08-24 21:49:00 -040010705/**
James Smart3621a712009-04-06 18:47:14 -040010706 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010707 * @phba: Pointer to HBA context object.
10708 *
10709 * This function cleans up all iocb, buffers, mailbox commands
10710 * while shutting down the HBA. This function is called with no
10711 * lock held and always returns 1.
10712 * This function does the following to cleanup driver resources:
10713 * - Free discovery resources for each virtual port
10714 * - Cleanup any pending fabric iocbs
10715 * - Iterate through the iocb txq and free each entry
10716 * in the list.
10717 * - Free up any buffer posted to the HBA
10718 * - Free mailbox commands in the mailbox queue.
10719 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010720int
James Smart2e0fef82007-06-17 19:56:36 -050010721lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010722{
James Smart2534ba72007-04-25 09:52:20 -040010723 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010724 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010725 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010726 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010727 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010728 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010729 int i;
10730
10731 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010732 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010733
dea31012005-04-17 16:05:31 -050010734 lpfc_hba_down_prep(phba);
10735
James Smart523128e2018-09-10 10:30:46 -070010736 /* Disable softirqs, including timers from obtaining phba->hbalock */
10737 local_bh_disable();
10738
James Smart92d7f7b2007-06-17 19:56:38 -050010739 lpfc_fabric_abort_hba(phba);
10740
James Smart2e0fef82007-06-17 19:56:36 -050010741 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010742
James Smart895427b2017-02-12 13:52:30 -080010743 /*
10744 * Error everything on the txq since these iocbs
10745 * have not been given to the FW yet.
10746 */
10747 if (phba->sli_rev != LPFC_SLI_REV4) {
10748 for (i = 0; i < psli->num_rings; i++) {
10749 pring = &psli->sli3_ring[i];
10750 /* Only slow rings */
10751 if (pring->ringno == LPFC_ELS_RING) {
10752 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10753 /* Set the lpfc data pending flag */
10754 set_bit(LPFC_DATA_READY, &phba->data_flags);
10755 }
10756 list_splice_init(&pring->txq, &completions);
10757 }
10758 } else {
10759 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10760 pring = qp->pring;
10761 if (!pring)
10762 continue;
James Smart4b0a42b2019-08-14 16:56:42 -070010763 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080010764 list_splice_init(&pring->txq, &completions);
James Smart4b0a42b2019-08-14 16:56:42 -070010765 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080010766 if (pring == phba->sli4_hba.els_wq->pring) {
10767 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10768 /* Set the lpfc data pending flag */
10769 set_bit(LPFC_DATA_READY, &phba->data_flags);
10770 }
10771 }
dea31012005-04-17 16:05:31 -050010772 }
James Smart2e0fef82007-06-17 19:56:36 -050010773 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010774
James Smarta257bf92009-04-06 18:48:10 -040010775 /* Cancel all the IOCBs from the completions list */
10776 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10777 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010778
James Smart0ff10d42008-01-11 01:52:36 -050010779 spin_lock_irqsave(&phba->hbalock, flags);
10780 list_splice_init(&phba->elsbuf, &completions);
10781 phba->elsbuf_cnt = 0;
10782 phba->elsbuf_prev_cnt = 0;
10783 spin_unlock_irqrestore(&phba->hbalock, flags);
10784
10785 while (!list_empty(&completions)) {
10786 list_remove_head(&completions, buf_ptr,
10787 struct lpfc_dmabuf, list);
10788 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10789 kfree(buf_ptr);
10790 }
10791
James Smart523128e2018-09-10 10:30:46 -070010792 /* Enable softirqs again, done with phba->hbalock */
10793 local_bh_enable();
10794
dea31012005-04-17 16:05:31 -050010795 /* Return any active mbox cmds */
10796 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010797
James Smartda0436e2009-05-22 14:51:39 -040010798 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010799 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010800 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010801
James Smartda0436e2009-05-22 14:51:39 -040010802 return 1;
10803}
James Smart92d7f7b2007-06-17 19:56:38 -050010804
James Smartda0436e2009-05-22 14:51:39 -040010805/**
James Smart3621a712009-04-06 18:47:14 -040010806 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010807 * @srcp: Source memory pointer.
10808 * @destp: Destination memory pointer.
10809 * @cnt: Number of words required to be copied.
10810 *
10811 * This function is used for copying data between driver memory
10812 * and the SLI memory. This function also changes the endianness
10813 * of each word if native endianness is different from SLI
10814 * endianness. This function can be called with or without
10815 * lock.
10816 **/
dea31012005-04-17 16:05:31 -050010817void
10818lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10819{
10820 uint32_t *src = srcp;
10821 uint32_t *dest = destp;
10822 uint32_t ldata;
10823 int i;
10824
10825 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10826 ldata = *src;
10827 ldata = le32_to_cpu(ldata);
10828 *dest = ldata;
10829 src++;
10830 dest++;
10831 }
10832}
10833
James Smarte59058c2008-08-24 21:49:00 -040010834
10835/**
James Smarta0c87cb2009-07-19 10:01:10 -040010836 * lpfc_sli_bemem_bcopy - SLI memory copy function
10837 * @srcp: Source memory pointer.
10838 * @destp: Destination memory pointer.
10839 * @cnt: Number of words required to be copied.
10840 *
10841 * This function is used for copying data between a data structure
10842 * with big endian representation to local endianness.
10843 * This function can be called with or without lock.
10844 **/
10845void
10846lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10847{
10848 uint32_t *src = srcp;
10849 uint32_t *dest = destp;
10850 uint32_t ldata;
10851 int i;
10852
10853 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10854 ldata = *src;
10855 ldata = be32_to_cpu(ldata);
10856 *dest = ldata;
10857 src++;
10858 dest++;
10859 }
10860}
10861
10862/**
James Smart3621a712009-04-06 18:47:14 -040010863 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010864 * @phba: Pointer to HBA context object.
10865 * @pring: Pointer to driver SLI ring object.
10866 * @mp: Pointer to driver buffer object.
10867 *
10868 * This function is called with no lock held.
10869 * It always return zero after adding the buffer to the postbufq
10870 * buffer list.
10871 **/
dea31012005-04-17 16:05:31 -050010872int
James Smart2e0fef82007-06-17 19:56:36 -050010873lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10874 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010875{
10876 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10877 later */
James Smart2e0fef82007-06-17 19:56:36 -050010878 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010879 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010880 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010881 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010882 return 0;
10883}
10884
James Smarte59058c2008-08-24 21:49:00 -040010885/**
James Smart3621a712009-04-06 18:47:14 -040010886 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010887 * @phba: Pointer to HBA context object.
10888 *
10889 * When HBQ is enabled, buffers are searched based on tags. This function
10890 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10891 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10892 * does not conflict with tags of buffer posted for unsolicited events.
10893 * The function returns the allocated tag. The function is called with
10894 * no locks held.
10895 **/
James Smart76bb24e2007-10-27 13:38:00 -040010896uint32_t
10897lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10898{
10899 spin_lock_irq(&phba->hbalock);
10900 phba->buffer_tag_count++;
10901 /*
10902 * Always set the QUE_BUFTAG_BIT to distiguish between
10903 * a tag assigned by HBQ.
10904 */
10905 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10906 spin_unlock_irq(&phba->hbalock);
10907 return phba->buffer_tag_count;
10908}
10909
James Smarte59058c2008-08-24 21:49:00 -040010910/**
James Smart3621a712009-04-06 18:47:14 -040010911 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010912 * @phba: Pointer to HBA context object.
10913 * @pring: Pointer to driver SLI ring object.
10914 * @tag: Buffer tag.
10915 *
10916 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10917 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10918 * iocb is posted to the response ring with the tag of the buffer.
10919 * This function searches the pring->postbufq list using the tag
10920 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10921 * iocb. If the buffer is found then lpfc_dmabuf object of the
10922 * buffer is returned to the caller else NULL is returned.
10923 * This function is called with no lock held.
10924 **/
James Smart76bb24e2007-10-27 13:38:00 -040010925struct lpfc_dmabuf *
10926lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10927 uint32_t tag)
10928{
10929 struct lpfc_dmabuf *mp, *next_mp;
10930 struct list_head *slp = &pring->postbufq;
10931
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010932 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010933 spin_lock_irq(&phba->hbalock);
10934 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10935 if (mp->buffer_tag == tag) {
10936 list_del_init(&mp->list);
10937 pring->postbufq_cnt--;
10938 spin_unlock_irq(&phba->hbalock);
10939 return mp;
10940 }
10941 }
10942
10943 spin_unlock_irq(&phba->hbalock);
10944 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010945 "0402 Cannot find virtual addr for buffer tag on "
James Smart32350662019-08-14 16:57:06 -070010946 "ring %d Data x%lx x%px x%px x%x\n",
James Smart76bb24e2007-10-27 13:38:00 -040010947 pring->ringno, (unsigned long) tag,
10948 slp->next, slp->prev, pring->postbufq_cnt);
10949
10950 return NULL;
10951}
dea31012005-04-17 16:05:31 -050010952
James Smarte59058c2008-08-24 21:49:00 -040010953/**
James Smart3621a712009-04-06 18:47:14 -040010954 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010955 * @phba: Pointer to HBA context object.
10956 * @pring: Pointer to driver SLI ring object.
10957 * @phys: DMA address of the buffer.
10958 *
10959 * This function searches the buffer list using the dma_address
10960 * of unsolicited event to find the driver's lpfc_dmabuf object
10961 * corresponding to the dma_address. The function returns the
10962 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10963 * This function is called by the ct and els unsolicited event
10964 * handlers to get the buffer associated with the unsolicited
10965 * event.
10966 *
10967 * This function is called with no lock held.
10968 **/
dea31012005-04-17 16:05:31 -050010969struct lpfc_dmabuf *
10970lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10971 dma_addr_t phys)
10972{
10973 struct lpfc_dmabuf *mp, *next_mp;
10974 struct list_head *slp = &pring->postbufq;
10975
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010976 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010977 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010978 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10979 if (mp->phys == phys) {
10980 list_del_init(&mp->list);
10981 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010982 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010983 return mp;
10984 }
10985 }
10986
James Smart2e0fef82007-06-17 19:56:36 -050010987 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010989 "0410 Cannot find virtual addr for mapped buf on "
James Smart32350662019-08-14 16:57:06 -070010990 "ring %d Data x%llx x%px x%px x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010991 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010992 slp->next, slp->prev, pring->postbufq_cnt);
10993 return NULL;
10994}
10995
James Smarte59058c2008-08-24 21:49:00 -040010996/**
James Smart3621a712009-04-06 18:47:14 -040010997 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010998 * @phba: Pointer to HBA context object.
10999 * @cmdiocb: Pointer to driver command iocb object.
11000 * @rspiocb: Pointer to driver response iocb object.
11001 *
11002 * This function is the completion handler for the abort iocbs for
11003 * ELS commands. This function is called from the ELS ring event
11004 * handler with no lock held. This function frees memory resources
11005 * associated with the abort iocb.
11006 **/
dea31012005-04-17 16:05:31 -050011007static void
James Smart2e0fef82007-06-17 19:56:36 -050011008lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11009 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050011010{
James Smart2e0fef82007-06-17 19:56:36 -050011011 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040011012 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050011013 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040011014
11015 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050011016
11017 /*
11018 * Assume that the port already completed and returned, or
11019 * will return the iocb. Just Log the message.
11020 */
James Smart2680eea2007-04-25 09:52:55 -040011021 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
11022 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
11023
James Smart2e0fef82007-06-17 19:56:36 -050011024 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040011025 if (phba->sli_rev < LPFC_SLI_REV4) {
James Smartfaa832e2018-07-31 17:23:18 -070011026 if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
11027 irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
11028 irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
11029 spin_unlock_irq(&phba->hbalock);
11030 goto release_iocb;
11031 }
James Smart45ed1192009-10-02 15:17:02 -040011032 if (abort_iotag != 0 &&
11033 abort_iotag <= phba->sli.last_iotag)
11034 abort_iocb =
11035 phba->sli.iocbq_lookup[abort_iotag];
11036 } else
11037 /* For sli4 the abort_tag is the XRI,
11038 * so the abort routine puts the iotag of the iocb
11039 * being aborted in the context field of the abort
11040 * IOCB.
11041 */
11042 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040011043
James Smart2a9bf3d2010-06-07 15:24:45 -040011044 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
James Smart32350662019-08-14 16:57:06 -070011045 "0327 Cannot abort els iocb x%px "
James Smart2a9bf3d2010-06-07 15:24:45 -040011046 "with tag %x context %x, abort status %x, "
11047 "abort code %x\n",
11048 abort_iocb, abort_iotag, abort_context,
11049 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040011050
James Smartff78d8f2011-12-13 13:21:35 -050011051 spin_unlock_irq(&phba->hbalock);
James Smart29601222019-08-14 16:56:36 -070011052 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
11053 irsp->un.ulpWord[4] == IOERR_SLI_ABORTED)
11054 lpfc_sli_release_iocbq(phba, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -040011055 }
James Smartfaa832e2018-07-31 17:23:18 -070011056release_iocb:
James Bottomley604a3e32005-10-29 10:28:33 -050011057 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050011058 return;
11059}
11060
James Smarte59058c2008-08-24 21:49:00 -040011061/**
James Smart3621a712009-04-06 18:47:14 -040011062 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040011063 * @phba: Pointer to HBA context object.
11064 * @cmdiocb: Pointer to driver command iocb object.
11065 * @rspiocb: Pointer to driver response iocb object.
11066 *
11067 * The function is called from SLI ring event handler with no
11068 * lock held. This function is the completion handler for ELS commands
11069 * which are aborted. The function frees memory resources used for
11070 * the aborted ELS commands.
11071 **/
James Smart92d7f7b2007-06-17 19:56:38 -050011072static void
11073lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11074 struct lpfc_iocbq *rspiocb)
11075{
11076 IOCB_t *irsp = &rspiocb->iocb;
11077
11078 /* ELS cmd tag <ulpIoTag> completes */
11079 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040011080 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050011081 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011082 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050011083 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050011084 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
11085 lpfc_ct_free_iocb(phba, cmdiocb);
11086 else
11087 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050011088 return;
11089}
11090
James Smarte59058c2008-08-24 21:49:00 -040011091/**
James Smart5af5eee2010-10-22 11:06:38 -040011092 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040011093 * @phba: Pointer to HBA context object.
11094 * @pring: Pointer to driver SLI ring object.
11095 * @cmdiocb: Pointer to driver command iocb object.
11096 *
James Smart5af5eee2010-10-22 11:06:38 -040011097 * This function issues an abort iocb for the provided command iocb down to
11098 * the port. Other than the case the outstanding command iocb is an abort
11099 * request, this function issues abort out unconditionally. This function is
11100 * called with hbalock held. The function returns 0 when it fails due to
11101 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040011102 **/
James Smart5af5eee2010-10-22 11:06:38 -040011103static int
11104lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050011105 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050011106{
James Smart2e0fef82007-06-17 19:56:36 -050011107 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011108 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050011109 IOCB_t *icmd = NULL;
11110 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011111 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040011112 unsigned long iflags;
James Smartfaa832e2018-07-31 17:23:18 -070011113 struct lpfc_nodelist *ndlp;
James Smart07951072007-04-25 09:51:38 -040011114
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011115 lockdep_assert_held(&phba->hbalock);
11116
James Smart92d7f7b2007-06-17 19:56:38 -050011117 /*
11118 * There are certain command types we don't want to abort. And we
11119 * don't want to abort commands that are already in the process of
11120 * being aborted.
James Smart07951072007-04-25 09:51:38 -040011121 */
11122 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050011123 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050011124 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11125 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040011126 return 0;
11127
dea31012005-04-17 16:05:31 -050011128 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050011129 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011130 if (abtsiocbp == NULL)
11131 return 0;
dea31012005-04-17 16:05:31 -050011132
James Smart07951072007-04-25 09:51:38 -040011133 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050011134 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040011135 */
11136 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11137
dea31012005-04-17 16:05:31 -050011138 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040011139 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
11140 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040011141 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040011142 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040011143 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
James Smartfaa832e2018-07-31 17:23:18 -070011144 } else {
James Smartda0436e2009-05-22 14:51:39 -040011145 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
James Smartfaa832e2018-07-31 17:23:18 -070011146 if (pring->ringno == LPFC_ELS_RING) {
11147 ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
11148 iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
11149 }
11150 }
dea31012005-04-17 16:05:31 -050011151 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040011152 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050011153
James Smart5ffc2662009-11-18 15:39:44 -050011154 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011155 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011156 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
11157 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011158 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
11159 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011160
James Smart2e0fef82007-06-17 19:56:36 -050011161 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040011162 iabt->ulpCommand = CMD_ABORT_XRI_CN;
11163 else
11164 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
11165
11166 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080011167 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040011168
James Smarte8b62012007-08-02 11:10:09 -040011169 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
11170 "0339 Abort xri x%x, original iotag x%x, "
11171 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040011172 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040011173 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040011174 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040011175
11176 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011177 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
11178 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040011179 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040011180 /* Note: both hbalock and ring_lock need to be set here */
11181 spin_lock_irqsave(&pring->ring_lock, iflags);
11182 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11183 abtsiocbp, 0);
11184 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11185 } else {
11186 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11187 abtsiocbp, 0);
11188 }
James Smart07951072007-04-25 09:51:38 -040011189
James Smartd7c255b2008-08-24 21:50:00 -040011190 if (retval)
11191 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040011192
11193 /*
11194 * Caller to this routine should check for IOCB_ERROR
11195 * and handle it properly. This routine no longer removes
11196 * iocb off txcmplq and call compl in case of IOCB_ERROR.
11197 */
11198 return retval;
11199}
11200
11201/**
11202 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
11203 * @phba: Pointer to HBA context object.
11204 * @pring: Pointer to driver SLI ring object.
11205 * @cmdiocb: Pointer to driver command iocb object.
11206 *
11207 * This function issues an abort iocb for the provided command iocb. In case
11208 * of unloading, the abort iocb will not be issued to commands on the ELS
11209 * ring. Instead, the callback function shall be changed to those commands
11210 * so that nothing happens when them finishes. This function is called with
11211 * hbalock held. The function returns 0 when the command iocb is an abort
11212 * request.
11213 **/
11214int
11215lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11216 struct lpfc_iocbq *cmdiocb)
11217{
11218 struct lpfc_vport *vport = cmdiocb->vport;
11219 int retval = IOCB_ERROR;
11220 IOCB_t *icmd = NULL;
11221
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011222 lockdep_assert_held(&phba->hbalock);
11223
James Smart5af5eee2010-10-22 11:06:38 -040011224 /*
11225 * There are certain command types we don't want to abort. And we
11226 * don't want to abort commands that are already in the process of
11227 * being aborted.
11228 */
11229 icmd = &cmdiocb->iocb;
11230 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
11231 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11232 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11233 return 0;
11234
Dick Kennedy1234a6d2017-09-29 17:34:29 -070011235 if (!pring) {
11236 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11237 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11238 else
11239 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11240 goto abort_iotag_exit;
11241 }
11242
James Smart5af5eee2010-10-22 11:06:38 -040011243 /*
11244 * If we're unloading, don't abort iocb on the ELS ring, but change
11245 * the callback so that nothing happens when it finishes.
11246 */
11247 if ((vport->load_flag & FC_UNLOADING) &&
11248 (pring->ringno == LPFC_ELS_RING)) {
11249 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11250 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11251 else
11252 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11253 goto abort_iotag_exit;
11254 }
11255
11256 /* Now, we try to issue the abort to the cmdiocb out */
11257 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
11258
James Smart07951072007-04-25 09:51:38 -040011259abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050011260 /*
11261 * Caller to this routine should check for IOCB_ERROR
11262 * and handle it properly. This routine no longer removes
11263 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040011264 */
James Smart2e0fef82007-06-17 19:56:36 -050011265 return retval;
dea31012005-04-17 16:05:31 -050011266}
11267
James Smarte59058c2008-08-24 21:49:00 -040011268/**
James Smart5af5eee2010-10-22 11:06:38 -040011269 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11270 * @phba: pointer to lpfc HBA data structure.
11271 *
11272 * This routine will abort all pending and outstanding iocbs to an HBA.
11273 **/
11274void
11275lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11276{
11277 struct lpfc_sli *psli = &phba->sli;
11278 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080011279 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011280 int i;
11281
James Smart895427b2017-02-12 13:52:30 -080011282 if (phba->sli_rev != LPFC_SLI_REV4) {
11283 for (i = 0; i < psli->num_rings; i++) {
11284 pring = &psli->sli3_ring[i];
11285 lpfc_sli_abort_iocb_ring(phba, pring);
11286 }
11287 return;
11288 }
11289 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11290 pring = qp->pring;
11291 if (!pring)
11292 continue;
James Smartdb55fba2014-04-04 13:52:02 -040011293 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040011294 }
11295}
11296
11297/**
James Smart3621a712009-04-06 18:47:14 -040011298 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040011299 * @iocbq: Pointer to driver iocb object.
11300 * @vport: Pointer to driver virtual port object.
11301 * @tgt_id: SCSI ID of the target.
11302 * @lun_id: LUN ID of the scsi device.
11303 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11304 *
James Smart3621a712009-04-06 18:47:14 -040011305 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040011306 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11307 * 0 if the filtering criteria is met for the given iocb and will return
11308 * 1 if the filtering criteria is not met.
11309 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11310 * given iocb is for the SCSI device specified by vport, tgt_id and
11311 * lun_id parameter.
11312 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11313 * given iocb is for the SCSI target specified by vport and tgt_id
11314 * parameters.
11315 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11316 * given iocb is for the SCSI host associated with the given vport.
11317 * This function is called with no locks held.
11318 **/
dea31012005-04-17 16:05:31 -050011319static int
James Smart51ef4c22007-08-02 11:10:31 -040011320lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11321 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011322 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011323{
James Smartc4908502019-01-28 11:14:28 -080011324 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011325 int rc = 1;
11326
James Smartb0e83012018-06-26 08:24:29 -070011327 if (iocbq->vport != vport)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011328 return rc;
11329
James Smartb0e83012018-06-26 08:24:29 -070011330 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
11331 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
James Smart51ef4c22007-08-02 11:10:31 -040011332 return rc;
11333
James Smartc4908502019-01-28 11:14:28 -080011334 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011335
James Smart495a7142008-06-14 22:52:59 -040011336 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050011337 return rc;
11338
11339 switch (ctx_cmd) {
11340 case LPFC_CTX_LUN:
James Smartb0e83012018-06-26 08:24:29 -070011341 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011342 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11343 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050011344 rc = 0;
11345 break;
11346 case LPFC_CTX_TGT:
James Smartb0e83012018-06-26 08:24:29 -070011347 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011348 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050011349 rc = 0;
11350 break;
dea31012005-04-17 16:05:31 -050011351 case LPFC_CTX_HOST:
11352 rc = 0;
11353 break;
11354 default:
11355 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070011356 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050011357 break;
11358 }
11359
11360 return rc;
11361}
11362
James Smarte59058c2008-08-24 21:49:00 -040011363/**
James Smart3621a712009-04-06 18:47:14 -040011364 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040011365 * @vport: Pointer to virtual port.
11366 * @tgt_id: SCSI ID of the target.
11367 * @lun_id: LUN ID of the scsi device.
11368 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11369 *
11370 * This function returns number of FCP commands pending for the vport.
11371 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11372 * commands pending on the vport associated with SCSI device specified
11373 * by tgt_id and lun_id parameters.
11374 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11375 * commands pending on the vport associated with SCSI target specified
11376 * by tgt_id parameter.
11377 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11378 * commands pending on the vport.
11379 * This function returns the number of iocbs which satisfy the filter.
11380 * This function is called without any lock held.
11381 **/
dea31012005-04-17 16:05:31 -050011382int
James Smart51ef4c22007-08-02 11:10:31 -040011383lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11384 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011385{
James Smart51ef4c22007-08-02 11:10:31 -040011386 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011387 struct lpfc_iocbq *iocbq;
11388 int sum, i;
dea31012005-04-17 16:05:31 -050011389
Johannes Thumshirn31979002016-07-18 16:06:03 +020011390 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011391 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11392 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011393
James Smart51ef4c22007-08-02 11:10:31 -040011394 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11395 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011396 sum++;
dea31012005-04-17 16:05:31 -050011397 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020011398 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011399
dea31012005-04-17 16:05:31 -050011400 return sum;
11401}
11402
James Smarte59058c2008-08-24 21:49:00 -040011403/**
James Smart3621a712009-04-06 18:47:14 -040011404 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040011405 * @phba: Pointer to HBA context object
11406 * @cmdiocb: Pointer to command iocb object.
11407 * @rspiocb: Pointer to response iocb object.
11408 *
11409 * This function is called when an aborted FCP iocb completes. This
11410 * function is called by the ring event handler with no lock held.
11411 * This function frees the iocb.
11412 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011413void
James Smart2e0fef82007-06-17 19:56:36 -050011414lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11415 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011416{
James Smartcb69f7d2011-12-13 13:21:57 -050011417 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040011418 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050011419 "original iotag x%x, abort cmd iotag x%x "
11420 "status 0x%x, reason 0x%x\n",
11421 cmdiocb->iocb.un.acxri.abortContextTag,
11422 cmdiocb->iocb.un.acxri.abortIoTag,
11423 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11424 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050011425 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011426 return;
11427}
11428
James Smarte59058c2008-08-24 21:49:00 -040011429/**
James Smart3621a712009-04-06 18:47:14 -040011430 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040011431 * @vport: Pointer to virtual port.
11432 * @pring: Pointer to driver SLI ring object.
11433 * @tgt_id: SCSI ID of the target.
11434 * @lun_id: LUN ID of the scsi device.
11435 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11436 *
11437 * This function sends an abort command for every SCSI command
11438 * associated with the given virtual port pending on the ring
11439 * filtered by lpfc_sli_validate_fcp_iocb function.
11440 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11441 * FCP iocbs associated with lun specified by tgt_id and lun_id
11442 * parameters
11443 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11444 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11445 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11446 * FCP iocbs associated with virtual port.
11447 * This function returns number of iocbs it failed to abort.
11448 * This function is called with no locks held.
11449 **/
dea31012005-04-17 16:05:31 -050011450int
James Smart51ef4c22007-08-02 11:10:31 -040011451lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11452 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050011453{
James Smart51ef4c22007-08-02 11:10:31 -040011454 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011455 struct lpfc_iocbq *iocbq;
11456 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070011457 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050011458 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050011459 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011460 int i;
dea31012005-04-17 16:05:31 -050011461
James Smartb0e83012018-06-26 08:24:29 -070011462 /* all I/Os are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -070011463 if (phba->hba_flag & HBA_IOQ_FLUSH)
James Smartb0e83012018-06-26 08:24:29 -070011464 return errcnt;
11465
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011466 for (i = 1; i <= phba->sli.last_iotag; i++) {
11467 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011468
James Smart51ef4c22007-08-02 11:10:31 -040011469 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011470 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011471 continue;
11472
James Smartafbd8d82013-09-06 12:22:13 -040011473 /*
11474 * If the iocbq is already being aborted, don't take a second
11475 * action, but do count it.
11476 */
11477 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11478 continue;
11479
dea31012005-04-17 16:05:31 -050011480 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011481 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011482 if (abtsiocb == NULL) {
11483 errcnt++;
11484 continue;
11485 }
dea31012005-04-17 16:05:31 -050011486
James Smartafbd8d82013-09-06 12:22:13 -040011487 /* indicate the IO is being aborted by the driver. */
11488 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11489
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011490 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011491 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11492 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011493 if (phba->sli_rev == LPFC_SLI_REV4)
11494 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11495 else
11496 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011497 abtsiocb->iocb.ulpLe = 1;
11498 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011499 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011500
James Smart5ffc2662009-11-18 15:39:44 -050011501 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011502 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011503 if (iocbq->iocb_flag & LPFC_IO_FCP)
11504 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011505 if (iocbq->iocb_flag & LPFC_IO_FOF)
11506 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011507
James Smart2e0fef82007-06-17 19:56:36 -050011508 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011509 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11510 else
11511 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11512
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011513 /* Setup callback routine and issue the command. */
11514 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011515 if (phba->sli_rev == LPFC_SLI_REV4) {
11516 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11517 if (!pring_s4)
11518 continue;
11519 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11520 abtsiocb, 0);
11521 } else
11522 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11523 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011524 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011525 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011526 errcnt++;
11527 continue;
11528 }
11529 }
11530
11531 return errcnt;
11532}
11533
James Smarte59058c2008-08-24 21:49:00 -040011534/**
James Smart98912dda2014-04-04 13:52:31 -040011535 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11536 * @vport: Pointer to virtual port.
11537 * @pring: Pointer to driver SLI ring object.
11538 * @tgt_id: SCSI ID of the target.
11539 * @lun_id: LUN ID of the scsi device.
11540 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11541 *
11542 * This function sends an abort command for every SCSI command
11543 * associated with the given virtual port pending on the ring
11544 * filtered by lpfc_sli_validate_fcp_iocb function.
11545 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11546 * FCP iocbs associated with lun specified by tgt_id and lun_id
11547 * parameters
11548 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11549 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11550 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11551 * FCP iocbs associated with virtual port.
11552 * This function returns number of iocbs it aborted .
11553 * This function is called with no locks held right after a taskmgmt
11554 * command is sent.
11555 **/
11556int
11557lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11558 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11559{
11560 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -080011561 struct lpfc_io_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011562 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011563 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011564 struct lpfc_iocbq *iocbq;
11565 IOCB_t *icmd;
11566 int sum, i, ret_val;
11567 unsigned long iflags;
James Smartc2017262019-01-28 11:14:37 -080011568 struct lpfc_sli_ring *pring_s4 = NULL;
James Smart98912dda2014-04-04 13:52:31 -040011569
James Smart59c68ea2018-04-09 14:24:25 -070011570 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011571
11572 /* all I/Os are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -070011573 if (phba->hba_flag & HBA_IOQ_FLUSH) {
James Smart59c68ea2018-04-09 14:24:25 -070011574 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011575 return 0;
11576 }
11577 sum = 0;
11578
11579 for (i = 1; i <= phba->sli.last_iotag; i++) {
11580 iocbq = phba->sli.iocbq_lookup[i];
11581
11582 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11583 cmd) != 0)
11584 continue;
11585
James Smartc2017262019-01-28 11:14:37 -080011586 /* Guard against IO completion being called at same time */
11587 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
11588 spin_lock(&lpfc_cmd->buf_lock);
11589
11590 if (!lpfc_cmd->pCmd) {
11591 spin_unlock(&lpfc_cmd->buf_lock);
11592 continue;
11593 }
11594
11595 if (phba->sli_rev == LPFC_SLI_REV4) {
11596 pring_s4 =
James Smartc00f62e2019-08-14 16:57:11 -070011597 phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;
James Smartc2017262019-01-28 11:14:37 -080011598 if (!pring_s4) {
11599 spin_unlock(&lpfc_cmd->buf_lock);
11600 continue;
11601 }
11602 /* Note: both hbalock and ring_lock must be set here */
11603 spin_lock(&pring_s4->ring_lock);
11604 }
11605
James Smart98912dda2014-04-04 13:52:31 -040011606 /*
11607 * If the iocbq is already being aborted, don't take a second
11608 * action, but do count it.
11609 */
James Smartc2017262019-01-28 11:14:37 -080011610 if ((iocbq->iocb_flag & LPFC_DRIVER_ABORTED) ||
11611 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
11612 if (phba->sli_rev == LPFC_SLI_REV4)
11613 spin_unlock(&pring_s4->ring_lock);
11614 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011615 continue;
James Smartc2017262019-01-28 11:14:37 -080011616 }
James Smart98912dda2014-04-04 13:52:31 -040011617
11618 /* issue ABTS for this IOCB based on iotag */
11619 abtsiocbq = __lpfc_sli_get_iocbq(phba);
James Smartc2017262019-01-28 11:14:37 -080011620 if (!abtsiocbq) {
11621 if (phba->sli_rev == LPFC_SLI_REV4)
11622 spin_unlock(&pring_s4->ring_lock);
11623 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011624 continue;
James Smartc2017262019-01-28 11:14:37 -080011625 }
James Smart98912dda2014-04-04 13:52:31 -040011626
11627 icmd = &iocbq->iocb;
11628 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11629 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11630 if (phba->sli_rev == LPFC_SLI_REV4)
11631 abtsiocbq->iocb.un.acxri.abortIoTag =
11632 iocbq->sli4_xritag;
11633 else
11634 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11635 abtsiocbq->iocb.ulpLe = 1;
11636 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11637 abtsiocbq->vport = vport;
11638
11639 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011640 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011641 if (iocbq->iocb_flag & LPFC_IO_FCP)
11642 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011643 if (iocbq->iocb_flag & LPFC_IO_FOF)
11644 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011645
James Smart8c50d252014-09-03 12:58:16 -040011646 ndlp = lpfc_cmd->rdata->pnode;
11647
11648 if (lpfc_is_link_up(phba) &&
11649 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011650 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11651 else
11652 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11653
11654 /* Setup callback routine and issue the command. */
11655 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11656
11657 /*
11658 * Indicate the IO is being aborted by the driver and set
11659 * the caller's flag into the aborted IO.
11660 */
11661 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11662
11663 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart98912dda2014-04-04 13:52:31 -040011664 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11665 abtsiocbq, 0);
James Smart59c68ea2018-04-09 14:24:25 -070011666 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011667 } else {
11668 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11669 abtsiocbq, 0);
11670 }
11671
James Smartc2017262019-01-28 11:14:37 -080011672 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011673
11674 if (ret_val == IOCB_ERROR)
11675 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11676 else
11677 sum++;
11678 }
James Smart59c68ea2018-04-09 14:24:25 -070011679 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011680 return sum;
11681}
11682
11683/**
James Smart3621a712009-04-06 18:47:14 -040011684 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011685 * @phba: Pointer to HBA context object.
11686 * @cmdiocbq: Pointer to command iocb.
11687 * @rspiocbq: Pointer to response iocb.
11688 *
11689 * This function is the completion handler for iocbs issued using
11690 * lpfc_sli_issue_iocb_wait function. This function is called by the
11691 * ring event handler function without any lock held. This function
11692 * can be called from both worker thread context and interrupt
11693 * context. This function also can be called from other thread which
11694 * cleans up the SLI layer objects.
11695 * This function copy the contents of the response iocb to the
11696 * response iocb memory object provided by the caller of
11697 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11698 * sleeps for the iocb completion.
11699 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011700static void
11701lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11702 struct lpfc_iocbq *cmdiocbq,
11703 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011704{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011705 wait_queue_head_t *pdone_q;
11706 unsigned long iflags;
James Smartc4908502019-01-28 11:14:28 -080011707 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011708
James Smart2e0fef82007-06-17 19:56:36 -050011709 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011710 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11711
11712 /*
11713 * A time out has occurred for the iocb. If a time out
11714 * completion handler has been supplied, call it. Otherwise,
11715 * just free the iocbq.
11716 */
11717
11718 spin_unlock_irqrestore(&phba->hbalock, iflags);
11719 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11720 cmdiocbq->wait_iocb_cmpl = NULL;
11721 if (cmdiocbq->iocb_cmpl)
11722 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11723 else
11724 lpfc_sli_release_iocbq(phba, cmdiocbq);
11725 return;
11726 }
11727
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011728 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11729 if (cmdiocbq->context2 && rspiocbq)
11730 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11731 &rspiocbq->iocb, sizeof(IOCB_t));
11732
James Smart0f65ff62010-02-26 14:14:23 -050011733 /* Set the exchange busy flag for task management commands */
11734 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11735 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smartc4908502019-01-28 11:14:28 -080011736 lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
James Smart0f65ff62010-02-26 14:14:23 -050011737 cur_iocbq);
11738 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11739 }
11740
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011741 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011742 if (pdone_q)
11743 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011744 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011745 return;
11746}
11747
James Smarte59058c2008-08-24 21:49:00 -040011748/**
James Smartd11e31d2009-06-10 17:23:06 -040011749 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11750 * @phba: Pointer to HBA context object..
11751 * @piocbq: Pointer to command iocb.
11752 * @flag: Flag to test.
11753 *
11754 * This routine grabs the hbalock and then test the iocb_flag to
11755 * see if the passed in flag is set.
11756 * Returns:
11757 * 1 if flag is set.
11758 * 0 if flag is not set.
11759 **/
11760static int
11761lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11762 struct lpfc_iocbq *piocbq, uint32_t flag)
11763{
11764 unsigned long iflags;
11765 int ret;
11766
11767 spin_lock_irqsave(&phba->hbalock, iflags);
11768 ret = piocbq->iocb_flag & flag;
11769 spin_unlock_irqrestore(&phba->hbalock, iflags);
11770 return ret;
11771
11772}
11773
11774/**
James Smart3621a712009-04-06 18:47:14 -040011775 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011776 * @phba: Pointer to HBA context object..
11777 * @pring: Pointer to sli ring.
11778 * @piocb: Pointer to command iocb.
11779 * @prspiocbq: Pointer to response iocb.
11780 * @timeout: Timeout in number of seconds.
11781 *
11782 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011783 * iocb to complete. The iocb_cmpl field of the shall be used
11784 * to handle iocbs which time out. If the field is NULL, the
11785 * function shall free the iocbq structure. If more clean up is
11786 * needed, the caller is expected to provide a completion function
11787 * that will provide the needed clean up. If the iocb command is
11788 * not completed within timeout seconds, the function will either
11789 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11790 * completion function set in the iocb_cmpl field and then return
11791 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11792 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011793 * The function waits for the iocb completion using an
11794 * non-interruptible wait.
11795 * This function will sleep while waiting for iocb completion.
11796 * So, this function should not be called from any context which
11797 * does not allow sleeping. Due to the same reason, this function
11798 * cannot be called with interrupt disabled.
11799 * This function assumes that the iocb completions occur while
11800 * this function sleep. So, this function cannot be called from
11801 * the thread which process iocb completion for this ring.
11802 * This function clears the iocb_flag of the iocb object before
11803 * issuing the iocb and the iocb completion handler sets this
11804 * flag and wakes this thread when the iocb completes.
11805 * The contents of the response iocb will be copied to prspiocbq
11806 * by the completion handler when the command completes.
11807 * This function returns IOCB_SUCCESS when success.
11808 * This function is called with no lock held.
11809 **/
dea31012005-04-17 16:05:31 -050011810int
James Smart2e0fef82007-06-17 19:56:36 -050011811lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011812 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011813 struct lpfc_iocbq *piocb,
11814 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011815 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011816{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011817 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011818 long timeleft, timeout_req = 0;
11819 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011820 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011821 struct lpfc_iocbq *iocb;
11822 int txq_cnt = 0;
11823 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011824 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011825 unsigned long iflags;
11826 bool iocb_completed = true;
11827
James Smart895427b2017-02-12 13:52:30 -080011828 if (phba->sli_rev >= LPFC_SLI_REV4)
11829 pring = lpfc_sli4_calc_ring(phba, piocb);
11830 else
11831 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011832 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011833 * If the caller has provided a response iocbq buffer, then context2
11834 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011835 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011836 if (prspiocbq) {
11837 if (piocb->context2)
11838 return IOCB_ERROR;
11839 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011840 }
11841
James Smart5a0916b2013-07-15 18:31:42 -040011842 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011843 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11844 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011845 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011846
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011847 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011848 if (lpfc_readl(phba->HCregaddr, &creg_val))
11849 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011850 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11851 writel(creg_val, phba->HCregaddr);
11852 readl(phba->HCregaddr); /* flush */
11853 }
11854
James Smart2a9bf3d2010-06-07 15:24:45 -040011855 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11856 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011857 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011858 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011859 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011860 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011861 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011862 spin_lock_irqsave(&phba->hbalock, iflags);
11863 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011864
James Smart5a0916b2013-07-15 18:31:42 -040011865 /*
11866 * IOCB timed out. Inform the wake iocb wait
11867 * completion function and set local status
11868 */
11869
11870 iocb_completed = false;
11871 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11872 }
11873 spin_unlock_irqrestore(&phba->hbalock, iflags);
11874 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011875 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011876 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011877 /* Note: we are not indicating if the IOCB has a success
11878 * status or not - that's for the caller to check.
11879 * IOCB_SUCCESS means just that the command was sent and
11880 * completed. Not that it completed successfully.
11881 * */
James Smart7054a602007-04-25 09:52:34 -040011882 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011883 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011884 "0338 IOCB wait timeout error - no "
11885 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011886 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011887 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011888 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011889 "0330 IOCB wake NOT set, "
11890 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011891 timeout, (timeleft / jiffies));
11892 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011893 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011894 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011895 if (phba->cfg_log_verbose & LOG_SLI) {
11896 list_for_each_entry(iocb, &pring->txq, list) {
11897 txq_cnt++;
11898 }
11899 list_for_each_entry(iocb, &pring->txcmplq, list) {
11900 txcmplq_cnt++;
11901 }
11902 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11903 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11904 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11905 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011906 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011907 } else {
11908 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011909 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011910 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011911 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011912 }
11913
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011914 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011915 if (lpfc_readl(phba->HCregaddr, &creg_val))
11916 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011917 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11918 writel(creg_val, phba->HCregaddr);
11919 readl(phba->HCregaddr); /* flush */
11920 }
11921
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011922 if (prspiocbq)
11923 piocb->context2 = NULL;
11924
11925 piocb->context_un.wait_queue = NULL;
11926 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011927 return retval;
11928}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011929
James Smarte59058c2008-08-24 21:49:00 -040011930/**
James Smart3621a712009-04-06 18:47:14 -040011931 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011932 * @phba: Pointer to HBA context object.
11933 * @pmboxq: Pointer to driver mailbox object.
11934 * @timeout: Timeout in number of seconds.
11935 *
11936 * This function issues the mailbox to firmware and waits for the
11937 * mailbox command to complete. If the mailbox command is not
11938 * completed within timeout seconds, it returns MBX_TIMEOUT.
11939 * The function waits for the mailbox completion using an
11940 * interruptible wait. If the thread is woken up due to a
11941 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11942 * should not free the mailbox resources, if this function returns
11943 * MBX_TIMEOUT.
11944 * This function will sleep while waiting for mailbox completion.
11945 * So, this function should not be called from any context which
11946 * does not allow sleeping. Due to the same reason, this function
11947 * cannot be called with interrupt disabled.
11948 * This function assumes that the mailbox completion occurs while
11949 * this function sleep. So, this function cannot be called from
11950 * the worker thread which processes mailbox completion.
11951 * This function is called in the context of HBA management
11952 * applications.
11953 * This function returns MBX_SUCCESS when successful.
11954 * This function is called with no lock held.
11955 **/
dea31012005-04-17 16:05:31 -050011956int
James Smart2e0fef82007-06-17 19:56:36 -050011957lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011958 uint32_t timeout)
11959{
James Smarte29d74f2018-03-05 12:04:07 -080011960 struct completion mbox_done;
dea31012005-04-17 16:05:31 -050011961 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011962 unsigned long flag;
dea31012005-04-17 16:05:31 -050011963
James Smart495a7142008-06-14 22:52:59 -040011964 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011965 /* setup wake call as IOCB callback */
11966 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea31012005-04-17 16:05:31 -050011967
James Smarte29d74f2018-03-05 12:04:07 -080011968 /* setup context3 field to pass wait_queue pointer to wake function */
11969 init_completion(&mbox_done);
11970 pmboxq->context3 = &mbox_done;
dea31012005-04-17 16:05:31 -050011971 /* now issue the command */
11972 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011973 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smarte29d74f2018-03-05 12:04:07 -080011974 wait_for_completion_timeout(&mbox_done,
11975 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011976
James Smart858c9f62007-06-17 19:56:39 -050011977 spin_lock_irqsave(&phba->hbalock, flag);
James Smarte29d74f2018-03-05 12:04:07 -080011978 pmboxq->context3 = NULL;
James Smart7054a602007-04-25 09:52:34 -040011979 /*
11980 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11981 * else do not free the resources.
11982 */
James Smartd7c47992010-06-08 18:31:54 -040011983 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011984 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011985 } else {
James Smart7054a602007-04-25 09:52:34 -040011986 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011987 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11988 }
11989 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050011990 }
dea31012005-04-17 16:05:31 -050011991 return retval;
11992}
11993
James Smarte59058c2008-08-24 21:49:00 -040011994/**
James Smart3772a992009-05-22 14:50:54 -040011995 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011996 * @phba: Pointer to HBA context.
11997 *
James Smart3772a992009-05-22 14:50:54 -040011998 * This function is called to shutdown the driver's mailbox sub-system.
11999 * It first marks the mailbox sub-system is in a block state to prevent
12000 * the asynchronous mailbox command from issued off the pending mailbox
12001 * command queue. If the mailbox command sub-system shutdown is due to
12002 * HBA error conditions such as EEH or ERATT, this routine shall invoke
12003 * the mailbox sub-system flush routine to forcefully bring down the
12004 * mailbox sub-system. Otherwise, if it is due to normal condition (such
12005 * as with offline or HBA function reset), this routine will wait for the
12006 * outstanding mailbox command to complete before invoking the mailbox
12007 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040012008 **/
James Smart3772a992009-05-22 14:50:54 -040012009void
James Smart618a5232012-06-12 13:54:36 -040012010lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040012011{
James Smart3772a992009-05-22 14:50:54 -040012012 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040012013 unsigned long timeout;
12014
James Smart618a5232012-06-12 13:54:36 -040012015 if (mbx_action == LPFC_MBX_NO_WAIT) {
12016 /* delay 100ms for port state */
12017 msleep(100);
12018 lpfc_sli_mbox_sys_flush(phba);
12019 return;
12020 }
James Smarta183a152011-10-10 21:32:43 -040012021 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050012022
James Smart523128e2018-09-10 10:30:46 -070012023 /* Disable softirqs, including timers from obtaining phba->hbalock */
12024 local_bh_disable();
12025
James Smart3772a992009-05-22 14:50:54 -040012026 spin_lock_irq(&phba->hbalock);
12027 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040012028
12029 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040012030 /* Determine how long we might wait for the active mailbox
12031 * command to be gracefully completed by firmware.
12032 */
James Smarta183a152011-10-10 21:32:43 -040012033 if (phba->sli.mbox_active)
12034 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
12035 phba->sli.mbox_active) *
12036 1000) + jiffies;
12037 spin_unlock_irq(&phba->hbalock);
12038
James Smart523128e2018-09-10 10:30:46 -070012039 /* Enable softirqs again, done with phba->hbalock */
12040 local_bh_enable();
12041
James Smart3772a992009-05-22 14:50:54 -040012042 while (phba->sli.mbox_active) {
12043 /* Check active mailbox complete status every 2ms */
12044 msleep(2);
12045 if (time_after(jiffies, timeout))
12046 /* Timeout, let the mailbox flush routine to
12047 * forcefully release active mailbox command
12048 */
12049 break;
12050 }
James Smart523128e2018-09-10 10:30:46 -070012051 } else {
James Smartd7069f02012-03-01 22:36:29 -050012052 spin_unlock_irq(&phba->hbalock);
12053
James Smart523128e2018-09-10 10:30:46 -070012054 /* Enable softirqs again, done with phba->hbalock */
12055 local_bh_enable();
12056 }
12057
James Smart3772a992009-05-22 14:50:54 -040012058 lpfc_sli_mbox_sys_flush(phba);
12059}
12060
12061/**
12062 * lpfc_sli_eratt_read - read sli-3 error attention events
12063 * @phba: Pointer to HBA context.
12064 *
12065 * This function is called to read the SLI3 device error attention registers
12066 * for possible error attention events. The caller must hold the hostlock
12067 * with spin_lock_irq().
12068 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012069 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040012070 * Register and returns 0 otherwise.
12071 **/
12072static int
12073lpfc_sli_eratt_read(struct lpfc_hba *phba)
12074{
James Smarted957682007-06-17 19:56:37 -050012075 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040012076
James Smart3772a992009-05-22 14:50:54 -040012077 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050012078 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12079 goto unplug_err;
12080
James Smart3772a992009-05-22 14:50:54 -040012081 if (ha_copy & HA_ERATT) {
12082 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050012083 if (lpfc_sli_read_hs(phba))
12084 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040012085
James Smart3772a992009-05-22 14:50:54 -040012086 /* Check if there is a deferred error condition is active */
12087 if ((HS_FFER1 & phba->work_hs) &&
12088 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012089 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040012090 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040012091 /* Clear all interrupt enable conditions */
12092 writel(0, phba->HCregaddr);
12093 readl(phba->HCregaddr);
12094 }
12095
12096 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040012097 phba->work_ha |= HA_ERATT;
12098 /* Indicate polling handles this ERATT */
12099 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012100 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012101 }
James Smart3772a992009-05-22 14:50:54 -040012102 return 0;
James Smart9940b972011-03-11 16:06:12 -050012103
12104unplug_err:
12105 /* Set the driver HS work bitmap */
12106 phba->work_hs |= UNPLUG_ERR;
12107 /* Set the driver HA work bitmap */
12108 phba->work_ha |= HA_ERATT;
12109 /* Indicate polling handles this ERATT */
12110 phba->hba_flag |= HBA_ERATT_HANDLED;
12111 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012112}
12113
James Smarte59058c2008-08-24 21:49:00 -040012114/**
James Smartda0436e2009-05-22 14:51:39 -040012115 * lpfc_sli4_eratt_read - read sli-4 error attention events
12116 * @phba: Pointer to HBA context.
12117 *
12118 * This function is called to read the SLI4 device error attention registers
12119 * for possible error attention events. The caller must hold the hostlock
12120 * with spin_lock_irq().
12121 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012122 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040012123 * Register and returns 0 otherwise.
12124 **/
12125static int
12126lpfc_sli4_eratt_read(struct lpfc_hba *phba)
12127{
12128 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050012129 uint32_t if_type, portsmphr;
12130 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040012131
James Smart2fcee4b2010-12-15 17:57:46 -050012132 /*
12133 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040012134 * registers for error attention. This can be changed later.
12135 */
James Smart2fcee4b2010-12-15 17:57:46 -050012136 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
12137 switch (if_type) {
12138 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050012139 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
12140 &uerr_sta_lo) ||
12141 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
12142 &uerr_sta_hi)) {
12143 phba->work_hs |= UNPLUG_ERR;
12144 phba->work_ha |= HA_ERATT;
12145 phba->hba_flag |= HBA_ERATT_HANDLED;
12146 return 1;
12147 }
James Smart2fcee4b2010-12-15 17:57:46 -050012148 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
12149 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
12150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12151 "1423 HBA Unrecoverable error: "
12152 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
12153 "ue_mask_lo_reg=0x%x, "
12154 "ue_mask_hi_reg=0x%x\n",
12155 uerr_sta_lo, uerr_sta_hi,
12156 phba->sli4_hba.ue_mask_lo,
12157 phba->sli4_hba.ue_mask_hi);
12158 phba->work_status[0] = uerr_sta_lo;
12159 phba->work_status[1] = uerr_sta_hi;
12160 phba->work_ha |= HA_ERATT;
12161 phba->hba_flag |= HBA_ERATT_HANDLED;
12162 return 1;
12163 }
12164 break;
12165 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080012166 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050012167 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
12168 &portstat_reg.word0) ||
12169 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
12170 &portsmphr)){
12171 phba->work_hs |= UNPLUG_ERR;
12172 phba->work_ha |= HA_ERATT;
12173 phba->hba_flag |= HBA_ERATT_HANDLED;
12174 return 1;
12175 }
James Smart2fcee4b2010-12-15 17:57:46 -050012176 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
12177 phba->work_status[0] =
12178 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
12179 phba->work_status[1] =
12180 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
12181 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050012182 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050012183 "port status reg 0x%x, "
12184 "port smphr reg 0x%x, "
12185 "error 1=0x%x, error 2=0x%x\n",
12186 portstat_reg.word0,
12187 portsmphr,
12188 phba->work_status[0],
12189 phba->work_status[1]);
12190 phba->work_ha |= HA_ERATT;
12191 phba->hba_flag |= HBA_ERATT_HANDLED;
12192 return 1;
12193 }
12194 break;
12195 case LPFC_SLI_INTF_IF_TYPE_1:
12196 default:
James Smarta747c9c2009-11-18 15:41:10 -050012197 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050012198 "2886 HBA Error Attention on unsupported "
12199 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050012200 return 1;
James Smartda0436e2009-05-22 14:51:39 -040012201 }
James Smart2fcee4b2010-12-15 17:57:46 -050012202
James Smartda0436e2009-05-22 14:51:39 -040012203 return 0;
12204}
12205
12206/**
James Smart3621a712009-04-06 18:47:14 -040012207 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040012208 * @phba: Pointer to HBA context.
12209 *
James Smart3772a992009-05-22 14:50:54 -040012210 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040012211 * error attention register bit for error attention events.
12212 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012213 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040012214 * Register and returns 0 otherwise.
12215 **/
12216int
12217lpfc_sli_check_eratt(struct lpfc_hba *phba)
12218{
12219 uint32_t ha_copy;
12220
12221 /* If somebody is waiting to handle an eratt, don't process it
12222 * here. The brdkill function will do this.
12223 */
12224 if (phba->link_flag & LS_IGNORE_ERATT)
12225 return 0;
12226
12227 /* Check if interrupt handler handles this ERATT */
12228 spin_lock_irq(&phba->hbalock);
12229 if (phba->hba_flag & HBA_ERATT_HANDLED) {
12230 /* Interrupt handler has handled ERATT */
12231 spin_unlock_irq(&phba->hbalock);
12232 return 0;
12233 }
12234
James Smarta257bf92009-04-06 18:48:10 -040012235 /*
12236 * If there is deferred error attention, do not check for error
12237 * attention
12238 */
12239 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12240 spin_unlock_irq(&phba->hbalock);
12241 return 0;
12242 }
12243
James Smart3772a992009-05-22 14:50:54 -040012244 /* If PCI channel is offline, don't process it */
12245 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040012246 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012247 return 0;
12248 }
12249
12250 switch (phba->sli_rev) {
12251 case LPFC_SLI_REV2:
12252 case LPFC_SLI_REV3:
12253 /* Read chip Host Attention (HA) register */
12254 ha_copy = lpfc_sli_eratt_read(phba);
12255 break;
James Smartda0436e2009-05-22 14:51:39 -040012256 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050012257 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040012258 ha_copy = lpfc_sli4_eratt_read(phba);
12259 break;
James Smart3772a992009-05-22 14:50:54 -040012260 default:
12261 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12262 "0299 Invalid SLI revision (%d)\n",
12263 phba->sli_rev);
12264 ha_copy = 0;
12265 break;
James Smart93996272008-08-24 21:50:30 -040012266 }
12267 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012268
12269 return ha_copy;
12270}
12271
12272/**
12273 * lpfc_intr_state_check - Check device state for interrupt handling
12274 * @phba: Pointer to HBA context.
12275 *
12276 * This inline routine checks whether a device or its PCI slot is in a state
12277 * that the interrupt should be handled.
12278 *
12279 * This function returns 0 if the device or the PCI slot is in a state that
12280 * interrupt should be handled, otherwise -EIO.
12281 */
12282static inline int
12283lpfc_intr_state_check(struct lpfc_hba *phba)
12284{
12285 /* If the pci channel is offline, ignore all the interrupts */
12286 if (unlikely(pci_channel_offline(phba->pcidev)))
12287 return -EIO;
12288
12289 /* Update device level interrupt statistics */
12290 phba->sli.slistat.sli_intr++;
12291
12292 /* Ignore all interrupts during initialization. */
12293 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12294 return -EIO;
12295
James Smart93996272008-08-24 21:50:30 -040012296 return 0;
12297}
12298
12299/**
James Smart3772a992009-05-22 14:50:54 -040012300 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040012301 * @irq: Interrupt number.
12302 * @dev_id: The device context pointer.
12303 *
James Smart93996272008-08-24 21:50:30 -040012304 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012305 * service routine when device with SLI-3 interface spec is enabled with
12306 * MSI-X multi-message interrupt mode and there are slow-path events in
12307 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12308 * interrupt mode, this function is called as part of the device-level
12309 * interrupt handler. When the PCI slot is in error recovery or the HBA
12310 * is undergoing initialization, the interrupt handler will not process
12311 * the interrupt. The link attention and ELS ring attention events are
12312 * handled by the worker thread. The interrupt handler signals the worker
12313 * thread and returns for these events. This function is called without
12314 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040012315 * structures.
12316 *
12317 * This function returns IRQ_HANDLED when interrupt is handled else it
12318 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040012319 **/
dea31012005-04-17 16:05:31 -050012320irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012321lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050012322{
James Smart2e0fef82007-06-17 19:56:36 -050012323 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050012324 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050012325 uint32_t work_ha_copy;
12326 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012327 unsigned long iflag;
dea31012005-04-17 16:05:31 -050012328 uint32_t control;
12329
James Smart92d7f7b2007-06-17 19:56:38 -050012330 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050012331 struct lpfc_vport *vport;
12332 struct lpfc_nodelist *ndlp;
12333 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050012334 LPFC_MBOXQ_t *pmb;
12335 int rc;
12336
dea31012005-04-17 16:05:31 -050012337 /*
12338 * Get the driver's phba structure from the dev_id and
12339 * assume the HBA is not interrupting.
12340 */
James Smart93996272008-08-24 21:50:30 -040012341 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050012342
12343 if (unlikely(!phba))
12344 return IRQ_NONE;
12345
dea31012005-04-17 16:05:31 -050012346 /*
James Smart93996272008-08-24 21:50:30 -040012347 * Stuff needs to be attented to when this function is invoked as an
12348 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012349 */
James Smart93996272008-08-24 21:50:30 -040012350 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012351 /* Check device state for handling interrupt */
12352 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012353 return IRQ_NONE;
12354 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050012355 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012356 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12357 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040012358 /* If somebody is waiting to handle an eratt don't process it
12359 * here. The brdkill function will do this.
12360 */
12361 if (phba->link_flag & LS_IGNORE_ERATT)
12362 ha_copy &= ~HA_ERATT;
12363 /* Check the need for handling ERATT in interrupt handler */
12364 if (ha_copy & HA_ERATT) {
12365 if (phba->hba_flag & HBA_ERATT_HANDLED)
12366 /* ERATT polling has handled ERATT */
12367 ha_copy &= ~HA_ERATT;
12368 else
12369 /* Indicate interrupt handler handles ERATT */
12370 phba->hba_flag |= HBA_ERATT_HANDLED;
12371 }
James Smarta257bf92009-04-06 18:48:10 -040012372
12373 /*
12374 * If there is deferred error attention, do not check for any
12375 * interrupt.
12376 */
12377 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012378 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012379 return IRQ_NONE;
12380 }
12381
James Smart93996272008-08-24 21:50:30 -040012382 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050012383 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12384 goto unplug_error;
12385
James Smarta747c9c2009-11-18 15:41:10 -050012386 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12387 HC_LAINT_ENA | HC_ERINT_ENA),
12388 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012389 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12390 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012391 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012392 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012393 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012394 } else
12395 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050012396
dea31012005-04-17 16:05:31 -050012397 work_ha_copy = ha_copy & phba->work_ha_mask;
12398
James Smart93996272008-08-24 21:50:30 -040012399 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050012400 if (work_ha_copy & HA_LATT) {
12401 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12402 /*
12403 * Turn off Link Attention interrupts
12404 * until CLEAR_LA done
12405 */
James Smart5b75da22008-12-04 22:39:35 -050012406 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012407 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050012408 if (lpfc_readl(phba->HCregaddr, &control))
12409 goto unplug_error;
dea31012005-04-17 16:05:31 -050012410 control &= ~HC_LAINT_ENA;
12411 writel(control, phba->HCregaddr);
12412 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012413 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012414 }
12415 else
12416 work_ha_copy &= ~HA_LATT;
12417 }
12418
James Smart93996272008-08-24 21:50:30 -040012419 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050012420 /*
12421 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12422 * the only slow ring.
12423 */
12424 status = (work_ha_copy &
12425 (HA_RXMASK << (4*LPFC_ELS_RING)));
12426 status >>= (4*LPFC_ELS_RING);
12427 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050012428 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012429 if (lpfc_readl(phba->HCregaddr, &control))
12430 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040012431
12432 lpfc_debugfs_slow_ring_trc(phba,
12433 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12434 control, status,
12435 (uint32_t)phba->sli.slistat.sli_intr);
12436
James Smart858c9f62007-06-17 19:56:39 -050012437 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040012438 lpfc_debugfs_slow_ring_trc(phba,
12439 "ISR Disable ring:"
12440 "pwork:x%x hawork:x%x wait:x%x",
12441 phba->work_ha, work_ha_copy,
12442 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012443 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012444
James Smart858c9f62007-06-17 19:56:39 -050012445 control &=
12446 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050012447 writel(control, phba->HCregaddr);
12448 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050012449 }
James Smarta58cbd52007-08-02 11:09:43 -040012450 else {
12451 lpfc_debugfs_slow_ring_trc(phba,
12452 "ISR slow ring: pwork:"
12453 "x%x hawork:x%x wait:x%x",
12454 phba->work_ha, work_ha_copy,
12455 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012456 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012457 }
James Smart5b75da22008-12-04 22:39:35 -050012458 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012459 }
12460 }
James Smart5b75da22008-12-04 22:39:35 -050012461 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012462 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050012463 if (lpfc_sli_read_hs(phba))
12464 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040012465 /*
12466 * Check if there is a deferred error condition
12467 * is active
12468 */
12469 if ((HS_FFER1 & phba->work_hs) &&
12470 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012471 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12472 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040012473 phba->hba_flag |= DEFER_ERATT;
12474 /* Clear all interrupt enable conditions */
12475 writel(0, phba->HCregaddr);
12476 readl(phba->HCregaddr);
12477 }
12478 }
12479
James Smart93996272008-08-24 21:50:30 -040012480 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050012481 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040012482 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040012483 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050012484 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050012485
12486 /* First check out the status word */
12487 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12488 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050012489 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050012490 /*
12491 * Stray Mailbox Interrupt, mbxCommand <cmd>
12492 * mbxStatus <status>
12493 */
James Smart09372822008-01-11 01:52:54 -050012494 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012495 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012496 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012497 "Interrupt mbxCommand x%x "
12498 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012499 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012500 pmbox->mbxCommand,
12501 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012502 /* clear mailbox attention bit */
12503 work_ha_copy &= ~HA_MBATT;
12504 } else {
James Smart97eab632008-04-07 10:16:05 -040012505 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012506 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012507 phba->last_completion_time = jiffies;
12508 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012509 if (pmb->mbox_cmpl) {
12510 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12511 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012512 if (pmb->out_ext_byte_len &&
James Smart3e1f0712018-11-29 16:09:29 -080012513 pmb->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -040012514 lpfc_sli_pcimem_bcopy(
12515 phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -080012516 pmb->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -040012517 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012518 }
James Smart09372822008-01-11 01:52:54 -050012519 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12520 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12521
12522 lpfc_debugfs_disc_trc(vport,
12523 LPFC_DISC_TRC_MBOX_VPORT,
12524 "MBOX dflt rpi: : "
12525 "status:x%x rpi:x%x",
12526 (uint32_t)pmbox->mbxStatus,
12527 pmbox->un.varWords[0], 0);
12528
12529 if (!pmbox->mbxStatus) {
12530 mp = (struct lpfc_dmabuf *)
James Smart3e1f0712018-11-29 16:09:29 -080012531 (pmb->ctx_buf);
James Smart09372822008-01-11 01:52:54 -050012532 ndlp = (struct lpfc_nodelist *)
James Smart3e1f0712018-11-29 16:09:29 -080012533 pmb->ctx_ndlp;
James Smart09372822008-01-11 01:52:54 -050012534
12535 /* Reg_LOGIN of dflt RPI was
12536 * successful. new lets get
12537 * rid of the RPI using the
12538 * same mbox buffer.
12539 */
12540 lpfc_unreg_login(phba,
12541 vport->vpi,
12542 pmbox->un.varWords[0],
12543 pmb);
12544 pmb->mbox_cmpl =
12545 lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080012546 pmb->ctx_buf = mp;
12547 pmb->ctx_ndlp = ndlp;
James Smart09372822008-01-11 01:52:54 -050012548 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012549 rc = lpfc_sli_issue_mbox(phba,
12550 pmb,
12551 MBX_NOWAIT);
12552 if (rc != MBX_BUSY)
12553 lpfc_printf_log(phba,
12554 KERN_ERR,
12555 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012556 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012557 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012558 if (rc != MBX_NOT_FINISHED)
12559 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012560 }
12561 }
James Smart5b75da22008-12-04 22:39:35 -050012562 spin_lock_irqsave(
12563 &phba->pport->work_port_lock,
12564 iflag);
James Smart09372822008-01-11 01:52:54 -050012565 phba->pport->work_port_events &=
12566 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012567 spin_unlock_irqrestore(
12568 &phba->pport->work_port_lock,
12569 iflag);
James Smart09372822008-01-11 01:52:54 -050012570 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012571 }
James Smart97eab632008-04-07 10:16:05 -040012572 } else
James Smart5b75da22008-12-04 22:39:35 -050012573 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012574
James Smart92d7f7b2007-06-17 19:56:38 -050012575 if ((work_ha_copy & HA_MBATT) &&
12576 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012577send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012578 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012579 do {
12580 rc = lpfc_sli_issue_mbox(phba, NULL,
12581 MBX_NOWAIT);
12582 } while (rc == MBX_NOT_FINISHED);
12583 if (rc != MBX_SUCCESS)
12584 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12585 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012586 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012587 }
12588
James Smart5b75da22008-12-04 22:39:35 -050012589 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012590 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012591 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012592 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012593 }
James Smart93996272008-08-24 21:50:30 -040012594 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012595unplug_error:
12596 spin_unlock_irqrestore(&phba->hbalock, iflag);
12597 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012598
James Smart3772a992009-05-22 14:50:54 -040012599} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012600
12601/**
James Smart3772a992009-05-22 14:50:54 -040012602 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012603 * @irq: Interrupt number.
12604 * @dev_id: The device context pointer.
12605 *
12606 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012607 * service routine when device with SLI-3 interface spec is enabled with
12608 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12609 * ring event in the HBA. However, when the device is enabled with either
12610 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12611 * device-level interrupt handler. When the PCI slot is in error recovery
12612 * or the HBA is undergoing initialization, the interrupt handler will not
12613 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12614 * the intrrupt context. This function is called without any lock held.
12615 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012616 *
12617 * This function returns IRQ_HANDLED when interrupt is handled else it
12618 * returns IRQ_NONE.
12619 **/
12620irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012621lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012622{
12623 struct lpfc_hba *phba;
12624 uint32_t ha_copy;
12625 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012626 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012627 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012628
12629 /* Get the driver's phba structure from the dev_id and
12630 * assume the HBA is not interrupting.
12631 */
12632 phba = (struct lpfc_hba *) dev_id;
12633
12634 if (unlikely(!phba))
12635 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012636
12637 /*
James Smart93996272008-08-24 21:50:30 -040012638 * Stuff needs to be attented to when this function is invoked as an
12639 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012640 */
James Smart93996272008-08-24 21:50:30 -040012641 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012642 /* Check device state for handling interrupt */
12643 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012644 return IRQ_NONE;
12645 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012646 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12647 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012648 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012649 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012650 /*
12651 * If there is deferred error attention, do not check for
12652 * any interrupt.
12653 */
12654 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012655 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012656 return IRQ_NONE;
12657 }
James Smart93996272008-08-24 21:50:30 -040012658 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12659 phba->HAregaddr);
12660 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012661 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012662 } else
12663 ha_copy = phba->ha_copy;
12664
12665 /*
12666 * Process all events on FCP ring. Take the optimized path for FCP IO.
12667 */
12668 ha_copy &= ~(phba->work_ha_mask);
12669
12670 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012671 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012672 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012673 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012674 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012675
12676 if (phba->cfg_multi_ring_support == 2) {
12677 /*
James Smart93996272008-08-24 21:50:30 -040012678 * Process all events on extra ring. Take the optimized path
12679 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012680 */
James Smart93996272008-08-24 21:50:30 -040012681 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012682 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012683 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012684 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012685 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012686 status);
12687 }
12688 }
dea31012005-04-17 16:05:31 -050012689 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012690} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012691
James Smart93996272008-08-24 21:50:30 -040012692/**
James Smart3772a992009-05-22 14:50:54 -040012693 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012694 * @irq: Interrupt number.
12695 * @dev_id: The device context pointer.
12696 *
James Smart3772a992009-05-22 14:50:54 -040012697 * This function is the HBA device-level interrupt handler to device with
12698 * SLI-3 interface spec, called from the PCI layer when either MSI or
12699 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12700 * requires driver attention. This function invokes the slow-path interrupt
12701 * attention handling function and fast-path interrupt attention handling
12702 * function in turn to process the relevant HBA attention events. This
12703 * function is called without any lock held. It gets the hbalock to access
12704 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012705 *
12706 * This function returns IRQ_HANDLED when interrupt is handled, else it
12707 * returns IRQ_NONE.
12708 **/
12709irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012710lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012711{
12712 struct lpfc_hba *phba;
12713 irqreturn_t sp_irq_rc, fp_irq_rc;
12714 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012715 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012716
12717 /*
12718 * Get the driver's phba structure from the dev_id and
12719 * assume the HBA is not interrupting.
12720 */
12721 phba = (struct lpfc_hba *) dev_id;
12722
12723 if (unlikely(!phba))
12724 return IRQ_NONE;
12725
James Smart3772a992009-05-22 14:50:54 -040012726 /* Check device state for handling interrupt */
12727 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012728 return IRQ_NONE;
12729
12730 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012731 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12732 spin_unlock(&phba->hbalock);
12733 return IRQ_HANDLED;
12734 }
12735
James Smart93996272008-08-24 21:50:30 -040012736 if (unlikely(!phba->ha_copy)) {
12737 spin_unlock(&phba->hbalock);
12738 return IRQ_NONE;
12739 } else if (phba->ha_copy & HA_ERATT) {
12740 if (phba->hba_flag & HBA_ERATT_HANDLED)
12741 /* ERATT polling has handled ERATT */
12742 phba->ha_copy &= ~HA_ERATT;
12743 else
12744 /* Indicate interrupt handler handles ERATT */
12745 phba->hba_flag |= HBA_ERATT_HANDLED;
12746 }
12747
James Smarta257bf92009-04-06 18:48:10 -040012748 /*
12749 * If there is deferred error attention, do not check for any interrupt.
12750 */
12751 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012752 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012753 return IRQ_NONE;
12754 }
12755
James Smart93996272008-08-24 21:50:30 -040012756 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012757 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12758 spin_unlock(&phba->hbalock);
12759 return IRQ_HANDLED;
12760 }
James Smarta747c9c2009-11-18 15:41:10 -050012761 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12762 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12763 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012764 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012765 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012766 readl(phba->HAregaddr); /* flush */
12767 spin_unlock(&phba->hbalock);
12768
12769 /*
12770 * Invokes slow-path host attention interrupt handling as appropriate.
12771 */
12772
12773 /* status of events with mailbox and link attention */
12774 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12775
12776 /* status of events with ELS ring */
12777 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12778 status2 >>= (4*LPFC_ELS_RING);
12779
12780 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012781 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012782 else
12783 sp_irq_rc = IRQ_NONE;
12784
12785 /*
12786 * Invoke fast-path host attention interrupt handling as appropriate.
12787 */
12788
12789 /* status of events with FCP ring */
12790 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12791 status1 >>= (4*LPFC_FCP_RING);
12792
12793 /* status of events with extra ring */
12794 if (phba->cfg_multi_ring_support == 2) {
12795 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12796 status2 >>= (4*LPFC_EXTRA_RING);
12797 } else
12798 status2 = 0;
12799
12800 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012801 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012802 else
12803 fp_irq_rc = IRQ_NONE;
12804
12805 /* Return device-level interrupt handling status */
12806 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012807} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012808
12809/**
James Smart4f774512009-05-22 14:52:35 -040012810 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12811 * @phba: pointer to lpfc hba data structure.
12812 *
12813 * This routine is invoked by the worker thread to process all the pending
12814 * SLI4 els abort xri events.
12815 **/
12816void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12817{
12818 struct lpfc_cq_event *cq_event;
12819
12820 /* First, declare the els xri abort event has been handled */
12821 spin_lock_irq(&phba->hbalock);
12822 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12823 spin_unlock_irq(&phba->hbalock);
12824 /* Now, handle all the els xri abort events */
12825 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12826 /* Get the first event from the head of the event queue */
12827 spin_lock_irq(&phba->hbalock);
12828 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12829 cq_event, struct lpfc_cq_event, list);
12830 spin_unlock_irq(&phba->hbalock);
12831 /* Notify aborted XRI for ELS work queue */
12832 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12833 /* Free the event processed back to the free pool */
12834 lpfc_sli4_cq_event_release(phba, cq_event);
12835 }
12836}
12837
James Smart341af102010-01-26 23:07:37 -050012838/**
12839 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12840 * @phba: pointer to lpfc hba data structure
12841 * @pIocbIn: pointer to the rspiocbq
12842 * @pIocbOut: pointer to the cmdiocbq
12843 * @wcqe: pointer to the complete wcqe
12844 *
12845 * This routine transfers the fields of a command iocbq to a response iocbq
12846 * by copying all the IOCB fields from command iocbq and transferring the
12847 * completion status information from the complete wcqe.
12848 **/
James Smart4f774512009-05-22 14:52:35 -040012849static void
James Smart341af102010-01-26 23:07:37 -050012850lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12851 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012852 struct lpfc_iocbq *pIocbOut,
12853 struct lpfc_wcqe_complete *wcqe)
12854{
James Smartaf227412013-10-10 12:23:10 -040012855 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012856 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012857 uint32_t status, max_response;
12858 struct lpfc_dmabuf *dmabuf;
12859 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012860 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12861
12862 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12863 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012864 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012865 status = bf_get(lpfc_wcqe_c_status, wcqe);
12866 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012867 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12868 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12869 pIocbIn->iocb.un.fcpi.fcpi_parm =
12870 pIocbOut->iocb.un.fcpi.fcpi_parm -
12871 wcqe->total_data_placed;
12872 else
12873 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012874 else {
James Smart4f774512009-05-22 14:52:35 -040012875 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012876 switch (pIocbOut->iocb.ulpCommand) {
12877 case CMD_ELS_REQUEST64_CR:
12878 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12879 bpl = (struct ulp_bde64 *)dmabuf->virt;
12880 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12881 max_response = bde.tus.f.bdeSize;
12882 break;
12883 case CMD_GEN_REQUEST64_CR:
12884 max_response = 0;
12885 if (!pIocbOut->context3)
12886 break;
12887 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12888 sizeof(struct ulp_bde64);
12889 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12890 bpl = (struct ulp_bde64 *)dmabuf->virt;
12891 for (i = 0; i < numBdes; i++) {
12892 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12893 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12894 max_response += bde.tus.f.bdeSize;
12895 }
12896 break;
12897 default:
12898 max_response = wcqe->total_data_placed;
12899 break;
12900 }
12901 if (max_response < wcqe->total_data_placed)
12902 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12903 else
12904 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12905 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012906 }
James Smart341af102010-01-26 23:07:37 -050012907
James Smartacd68592012-01-18 16:25:09 -050012908 /* Convert BG errors for completion status */
12909 if (status == CQE_STATUS_DI_ERROR) {
12910 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12911
12912 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12913 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12914 else
12915 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12916
12917 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12918 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12919 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12920 BGS_GUARD_ERR_MASK;
12921 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12922 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12923 BGS_APPTAG_ERR_MASK;
12924 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12925 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12926 BGS_REFTAG_ERR_MASK;
12927
12928 /* Check to see if there was any good data before the error */
12929 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12930 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12931 BGS_HI_WATER_MARK_PRESENT_MASK;
12932 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12933 wcqe->total_data_placed;
12934 }
12935
12936 /*
12937 * Set ALL the error bits to indicate we don't know what
12938 * type of error it is.
12939 */
12940 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12941 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12942 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12943 BGS_GUARD_ERR_MASK);
12944 }
12945
James Smart341af102010-01-26 23:07:37 -050012946 /* Pick up HBA exchange busy condition */
12947 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12948 spin_lock_irqsave(&phba->hbalock, iflags);
12949 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12950 spin_unlock_irqrestore(&phba->hbalock, iflags);
12951 }
James Smart4f774512009-05-22 14:52:35 -040012952}
12953
12954/**
James Smart45ed1192009-10-02 15:17:02 -040012955 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12956 * @phba: Pointer to HBA context object.
12957 * @wcqe: Pointer to work-queue completion queue entry.
12958 *
12959 * This routine handles an ELS work-queue completion event and construct
12960 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12961 * discovery engine to handle.
12962 *
12963 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12964 **/
12965static struct lpfc_iocbq *
12966lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12967 struct lpfc_iocbq *irspiocbq)
12968{
James Smart895427b2017-02-12 13:52:30 -080012969 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012970 struct lpfc_iocbq *cmdiocbq;
12971 struct lpfc_wcqe_complete *wcqe;
12972 unsigned long iflags;
12973
James Smart895427b2017-02-12 13:52:30 -080012974 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070012975 if (unlikely(!pring))
12976 return NULL;
James Smart895427b2017-02-12 13:52:30 -080012977
James Smart45ed1192009-10-02 15:17:02 -040012978 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart45ed1192009-10-02 15:17:02 -040012979 pring->stats.iocb_event++;
12980 /* Look up the ELS command IOCB and create pseudo response IOCB */
12981 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12982 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040012983 if (unlikely(!cmdiocbq)) {
12984 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12985 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070012986 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12987 wcqe->word0, wcqe->total_data_placed,
12988 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040012989 lpfc_sli_release_iocbq(phba, irspiocbq);
12990 return NULL;
12991 }
12992
James Smarte2a8be52019-05-06 17:26:47 -070012993 spin_lock_irqsave(&pring->ring_lock, iflags);
Dick Kennedy401bb412017-09-29 17:34:28 -070012994 /* Put the iocb back on the txcmplq */
12995 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12996 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12997
James Smart45ed1192009-10-02 15:17:02 -040012998 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012999 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040013000
13001 return irspiocbq;
13002}
13003
James Smart8a5ca102017-11-20 16:00:30 -080013004inline struct lpfc_cq_event *
13005lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
13006{
13007 struct lpfc_cq_event *cq_event;
13008
13009 /* Allocate a new internal CQ_EVENT entry */
13010 cq_event = lpfc_sli4_cq_event_alloc(phba);
13011 if (!cq_event) {
13012 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13013 "0602 Failed to alloc CQ_EVENT entry\n");
13014 return NULL;
13015 }
13016
13017 /* Move the CQE into the event */
13018 memcpy(&cq_event->cqe, entry, size);
13019 return cq_event;
13020}
13021
James Smart45ed1192009-10-02 15:17:02 -040013022/**
James Smart04c68492009-05-22 14:52:52 -040013023 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
13024 * @phba: Pointer to HBA context object.
13025 * @cqe: Pointer to mailbox completion queue entry.
13026 *
13027 * This routine process a mailbox completion queue entry with asynchrous
13028 * event.
13029 *
13030 * Return: true if work posted to worker thread, otherwise false.
13031 **/
13032static bool
13033lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13034{
13035 struct lpfc_cq_event *cq_event;
13036 unsigned long iflags;
13037
13038 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13039 "0392 Async Event: word0:x%x, word1:x%x, "
13040 "word2:x%x, word3:x%x\n", mcqe->word0,
13041 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
13042
James Smart8a5ca102017-11-20 16:00:30 -080013043 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
13044 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040013045 return false;
James Smart04c68492009-05-22 14:52:52 -040013046 spin_lock_irqsave(&phba->hbalock, iflags);
13047 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
13048 /* Set the async event flag */
13049 phba->hba_flag |= ASYNC_EVENT;
13050 spin_unlock_irqrestore(&phba->hbalock, iflags);
13051
13052 return true;
13053}
13054
13055/**
13056 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
13057 * @phba: Pointer to HBA context object.
13058 * @cqe: Pointer to mailbox completion queue entry.
13059 *
13060 * This routine process a mailbox completion queue entry with mailbox
13061 * completion event.
13062 *
13063 * Return: true if work posted to worker thread, otherwise false.
13064 **/
13065static bool
13066lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13067{
13068 uint32_t mcqe_status;
13069 MAILBOX_t *mbox, *pmbox;
13070 struct lpfc_mqe *mqe;
13071 struct lpfc_vport *vport;
13072 struct lpfc_nodelist *ndlp;
13073 struct lpfc_dmabuf *mp;
13074 unsigned long iflags;
13075 LPFC_MBOXQ_t *pmb;
13076 bool workposted = false;
13077 int rc;
13078
13079 /* If not a mailbox complete MCQE, out by checking mailbox consume */
13080 if (!bf_get(lpfc_trailer_completed, mcqe))
13081 goto out_no_mqe_complete;
13082
13083 /* Get the reference to the active mbox command */
13084 spin_lock_irqsave(&phba->hbalock, iflags);
13085 pmb = phba->sli.mbox_active;
13086 if (unlikely(!pmb)) {
13087 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
13088 "1832 No pending MBOX command to handle\n");
13089 spin_unlock_irqrestore(&phba->hbalock, iflags);
13090 goto out_no_mqe_complete;
13091 }
13092 spin_unlock_irqrestore(&phba->hbalock, iflags);
13093 mqe = &pmb->u.mqe;
13094 pmbox = (MAILBOX_t *)&pmb->u.mqe;
13095 mbox = phba->mbox;
13096 vport = pmb->vport;
13097
13098 /* Reset heartbeat timer */
13099 phba->last_completion_time = jiffies;
13100 del_timer(&phba->sli.mbox_tmo);
13101
13102 /* Move mbox data to caller's mailbox region, do endian swapping */
13103 if (pmb->mbox_cmpl && mbox)
James Smart48f8fdb2018-05-04 20:37:51 -070013104 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040013105
James Smart73d91e52011-10-10 21:32:10 -040013106 /*
13107 * For mcqe errors, conditionally move a modified error code to
13108 * the mbox so that the error will not be missed.
13109 */
13110 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
13111 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
13112 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
13113 bf_set(lpfc_mqe_status, mqe,
13114 (LPFC_MBX_ERROR_RANGE | mcqe_status));
13115 }
James Smart04c68492009-05-22 14:52:52 -040013116 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13117 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13118 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
13119 "MBOX dflt rpi: status:x%x rpi:x%x",
13120 mcqe_status,
13121 pmbox->un.varWords[0], 0);
13122 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
James Smart3e1f0712018-11-29 16:09:29 -080013123 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
13124 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smart04c68492009-05-22 14:52:52 -040013125 /* Reg_LOGIN of dflt RPI was successful. Now lets get
13126 * RID of the PPI using the same mbox buffer.
13127 */
13128 lpfc_unreg_login(phba, vport->vpi,
13129 pmbox->un.varWords[0], pmb);
13130 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080013131 pmb->ctx_buf = mp;
13132 pmb->ctx_ndlp = ndlp;
James Smart04c68492009-05-22 14:52:52 -040013133 pmb->vport = vport;
13134 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
13135 if (rc != MBX_BUSY)
13136 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
13137 LOG_SLI, "0385 rc should "
13138 "have been MBX_BUSY\n");
13139 if (rc != MBX_NOT_FINISHED)
13140 goto send_current_mbox;
13141 }
13142 }
13143 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
13144 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
13145 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
13146
13147 /* There is mailbox completion work to do */
13148 spin_lock_irqsave(&phba->hbalock, iflags);
13149 __lpfc_mbox_cmpl_put(phba, pmb);
13150 phba->work_ha |= HA_MBATT;
13151 spin_unlock_irqrestore(&phba->hbalock, iflags);
13152 workposted = true;
13153
13154send_current_mbox:
13155 spin_lock_irqsave(&phba->hbalock, iflags);
13156 /* Release the mailbox command posting token */
13157 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13158 /* Setting active mailbox pointer need to be in sync to flag clear */
13159 phba->sli.mbox_active = NULL;
13160 spin_unlock_irqrestore(&phba->hbalock, iflags);
13161 /* Wake up worker thread to post the next pending mailbox command */
13162 lpfc_worker_wake_up(phba);
13163out_no_mqe_complete:
13164 if (bf_get(lpfc_trailer_consumed, mcqe))
13165 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
13166 return workposted;
13167}
13168
13169/**
13170 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
13171 * @phba: Pointer to HBA context object.
13172 * @cqe: Pointer to mailbox completion queue entry.
13173 *
13174 * This routine process a mailbox completion queue entry, it invokes the
13175 * proper mailbox complete handling or asynchrous event handling routine
13176 * according to the MCQE's async bit.
13177 *
13178 * Return: true if work posted to worker thread, otherwise false.
13179 **/
13180static bool
James Smart32517fc2019-01-28 11:14:33 -080013181lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13182 struct lpfc_cqe *cqe)
James Smart04c68492009-05-22 14:52:52 -040013183{
13184 struct lpfc_mcqe mcqe;
13185 bool workposted;
13186
James Smart32517fc2019-01-28 11:14:33 -080013187 cq->CQ_mbox++;
13188
James Smart04c68492009-05-22 14:52:52 -040013189 /* Copy the mailbox MCQE and convert endian order as needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013190 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
James Smart04c68492009-05-22 14:52:52 -040013191
13192 /* Invoke the proper event handling routine */
13193 if (!bf_get(lpfc_trailer_async, &mcqe))
13194 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
13195 else
13196 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
13197 return workposted;
13198}
13199
13200/**
James Smart4f774512009-05-22 14:52:35 -040013201 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
13202 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040013203 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040013204 * @wcqe: Pointer to work-queue completion queue entry.
13205 *
13206 * This routine handles an ELS work-queue completion event.
13207 *
13208 * Return: true if work posted to worker thread, otherwise false.
13209 **/
13210static bool
James Smart2a76a282012-08-03 12:35:54 -040013211lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013212 struct lpfc_wcqe_complete *wcqe)
13213{
James Smart4f774512009-05-22 14:52:35 -040013214 struct lpfc_iocbq *irspiocbq;
13215 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040013216 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050013217 int txq_cnt = 0;
13218 int txcmplq_cnt = 0;
13219 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040013220
James Smart11f0e342018-05-04 20:37:57 -070013221 /* Check for response status */
13222 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13223 /* Log the error status */
13224 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13225 "0357 ELS CQE error: status=x%x: "
13226 "CQE: %08x %08x %08x %08x\n",
13227 bf_get(lpfc_wcqe_c_status, wcqe),
13228 wcqe->word0, wcqe->total_data_placed,
13229 wcqe->parameter, wcqe->word3);
13230 }
13231
James Smart45ed1192009-10-02 15:17:02 -040013232 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040013233 irspiocbq = lpfc_sli_get_iocbq(phba);
13234 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050013235 if (!list_empty(&pring->txq))
13236 txq_cnt++;
13237 if (!list_empty(&pring->txcmplq))
13238 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040013239 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040013240 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
13241 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050013242 txq_cnt, phba->iocb_cnt,
13243 fcp_txcmplq_cnt,
13244 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040013245 return false;
James Smart4f774512009-05-22 14:52:35 -040013246 }
James Smart4f774512009-05-22 14:52:35 -040013247
James Smart45ed1192009-10-02 15:17:02 -040013248 /* Save off the slow-path queue event for work thread to process */
13249 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040013250 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040013251 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013252 &phba->sli4_hba.sp_queue_event);
13253 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040013254 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013255
James Smart45ed1192009-10-02 15:17:02 -040013256 return true;
James Smart4f774512009-05-22 14:52:35 -040013257}
13258
13259/**
13260 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13261 * @phba: Pointer to HBA context object.
13262 * @wcqe: Pointer to work-queue completion queue entry.
13263 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013264 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013265 * proper WQ release routine to the slow-path WQ.
13266 **/
13267static void
13268lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13269 struct lpfc_wcqe_release *wcqe)
13270{
James Smart2e90f4b2011-12-13 13:22:37 -050013271 /* sanity check on queue memory */
13272 if (unlikely(!phba->sli4_hba.els_wq))
13273 return;
James Smart4f774512009-05-22 14:52:35 -040013274 /* Check for the slow-path ELS work queue */
13275 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13276 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13277 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13278 else
13279 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13280 "2579 Slow-path wqe consume event carries "
13281 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13282 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13283 phba->sli4_hba.els_wq->queue_id);
13284}
13285
13286/**
13287 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13288 * @phba: Pointer to HBA context object.
13289 * @cq: Pointer to a WQ completion queue.
13290 * @wcqe: Pointer to work-queue completion queue entry.
13291 *
13292 * This routine handles an XRI abort event.
13293 *
13294 * Return: true if work posted to worker thread, otherwise false.
13295 **/
13296static bool
13297lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13298 struct lpfc_queue *cq,
13299 struct sli4_wcqe_xri_aborted *wcqe)
13300{
13301 bool workposted = false;
13302 struct lpfc_cq_event *cq_event;
13303 unsigned long iflags;
13304
James Smart4f774512009-05-22 14:52:35 -040013305 switch (cq->subtype) {
James Smartc00f62e2019-08-14 16:57:11 -070013306 case LPFC_IO:
13307 lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);
13308 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
13309 /* Notify aborted XRI for NVME work queue */
13310 if (phba->nvmet_support)
13311 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13312 }
James Smart5e5b5112019-01-28 11:14:22 -080013313 workposted = false;
James Smart4f774512009-05-22 14:52:35 -040013314 break;
James Smart422c4cb2017-11-20 16:00:32 -080013315 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040013316 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080013317 cq_event = lpfc_cq_event_setup(
13318 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13319 if (!cq_event)
13320 return false;
James Smart5e5b5112019-01-28 11:14:22 -080013321 cq_event->hdwq = cq->hdwq;
James Smart4f774512009-05-22 14:52:35 -040013322 spin_lock_irqsave(&phba->hbalock, iflags);
13323 list_add_tail(&cq_event->list,
13324 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13325 /* Set the els xri abort event flag */
13326 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13327 spin_unlock_irqrestore(&phba->hbalock, iflags);
13328 workposted = true;
13329 break;
13330 default:
13331 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080013332 "0603 Invalid CQ subtype %d: "
13333 "%08x %08x %08x %08x\n",
13334 cq->subtype, wcqe->word0, wcqe->parameter,
13335 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013336 workposted = false;
13337 break;
13338 }
13339 return workposted;
13340}
13341
James Smarte817e5d2018-12-13 15:17:53 -080013342#define FC_RCTL_MDS_DIAGS 0xF4
13343
James Smart4f774512009-05-22 14:52:35 -040013344/**
James Smart4d9ab992009-10-02 15:16:39 -040013345 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040013346 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040013347 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013348 *
James Smart4d9ab992009-10-02 15:16:39 -040013349 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013350 *
13351 * Return: true if work posted to worker thread, otherwise false.
13352 **/
13353static bool
James Smart4d9ab992009-10-02 15:16:39 -040013354lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13355{
13356 bool workposted = false;
James Smarte817e5d2018-12-13 15:17:53 -080013357 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040013358 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13359 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070013360 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040013361 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040013362 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040013363 unsigned long iflags;
13364
James Smart2e90f4b2011-12-13 13:22:37 -050013365 /* sanity check on queue memory */
13366 if (unlikely(!hrq) || unlikely(!drq))
13367 return workposted;
13368
James Smart7851fe22011-07-22 18:36:52 -040013369 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13370 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13371 else
13372 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13373 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040013374 goto out;
13375
13376 status = bf_get(lpfc_rcqe_status, rcqe);
13377 switch (status) {
13378 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13379 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13380 "2537 Receive Frame Truncated!!\n");
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060013381 /* fall through */
James Smart4d9ab992009-10-02 15:16:39 -040013382 case FC_STATUS_RQ_SUCCESS:
13383 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013384 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040013385 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13386 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040013387 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040013388 spin_unlock_irqrestore(&phba->hbalock, iflags);
13389 goto out;
13390 }
James Smartb84daac2012-08-03 12:35:13 -040013391 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013392 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040013393 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080013394
James Smarte817e5d2018-12-13 15:17:53 -080013395 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13396
13397 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
13398 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
13399 spin_unlock_irqrestore(&phba->hbalock, iflags);
13400 /* Handle MDS Loopback frames */
13401 lpfc_sli4_handle_mds_loopback(phba->pport, dma_buf);
13402 break;
13403 }
13404
13405 /* save off the frame for the work thread to process */
James Smart4d9ab992009-10-02 15:16:39 -040013406 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013407 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040013408 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040013409 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040013410 spin_unlock_irqrestore(&phba->hbalock, iflags);
13411 workposted = true;
13412 break;
James Smart4d9ab992009-10-02 15:16:39 -040013413 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013414 if (phba->nvmet_support) {
13415 tgtp = phba->targetport->private;
13416 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13417 "6402 RQE Error x%x, posted %d err_cnt "
13418 "%d: %x %x %x\n",
13419 status, hrq->RQ_buf_posted,
13420 hrq->RQ_no_posted_buf,
13421 atomic_read(&tgtp->rcv_fcp_cmd_in),
13422 atomic_read(&tgtp->rcv_fcp_cmd_out),
13423 atomic_read(&tgtp->xmt_fcp_release));
13424 }
13425 /* fallthrough */
13426
13427 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040013428 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040013429 /* Post more buffers if possible */
13430 spin_lock_irqsave(&phba->hbalock, iflags);
13431 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13432 spin_unlock_irqrestore(&phba->hbalock, iflags);
13433 workposted = true;
13434 break;
13435 }
13436out:
13437 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040013438}
13439
13440/**
13441 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13442 * @phba: Pointer to HBA context object.
13443 * @cq: Pointer to the completion queue.
James Smart32517fc2019-01-28 11:14:33 -080013444 * @cqe: Pointer to a completion queue entry.
James Smart4d9ab992009-10-02 15:16:39 -040013445 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013446 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040013447 * entry.
13448 *
13449 * Return: true if work posted to worker thread, otherwise false.
13450 **/
13451static bool
13452lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013453 struct lpfc_cqe *cqe)
13454{
James Smart45ed1192009-10-02 15:17:02 -040013455 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040013456 bool workposted = false;
13457
13458 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013459 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013460
13461 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040013462 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040013463 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040013464 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040013465 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040013466 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013467 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013468 break;
13469 case CQE_CODE_RELEASE_WQE:
13470 /* Process the WQ release event */
13471 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013472 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013473 break;
13474 case CQE_CODE_XRI_ABORTED:
13475 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013476 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013477 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013478 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013479 break;
James Smart4d9ab992009-10-02 15:16:39 -040013480 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013481 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013482 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013483 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013484 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013485 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013486 break;
James Smart4f774512009-05-22 14:52:35 -040013487 default:
13488 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13489 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013490 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013491 break;
13492 }
13493 return workposted;
13494}
13495
13496/**
James Smart4f774512009-05-22 14:52:35 -040013497 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13498 * @phba: Pointer to HBA context object.
13499 * @eqe: Pointer to fast-path event queue entry.
13500 *
13501 * This routine process a event queue entry from the slow-path event queue.
13502 * It will check the MajorCode and MinorCode to determine this is for a
13503 * completion event on a completion queue, if not, an error shall be logged
13504 * and just return. Otherwise, it will get to the corresponding completion
13505 * queue and process all the entries on that completion queue, rearm the
13506 * completion queue, and then return.
13507 *
13508 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013509static void
James Smart67d12732012-08-03 12:36:13 -040013510lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13511 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013512{
James Smart67d12732012-08-03 12:36:13 -040013513 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013514 uint16_t cqid;
13515
James Smart4f774512009-05-22 14:52:35 -040013516 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013517 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013518
James Smart4f774512009-05-22 14:52:35 -040013519 list_for_each_entry(childq, &speq->child_list, list) {
13520 if (childq->queue_id == cqid) {
13521 cq = childq;
13522 break;
13523 }
13524 }
13525 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013526 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13527 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13528 "0365 Slow-path CQ identifier "
13529 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013530 return;
James Smart4f774512009-05-22 14:52:35 -040013531 }
13532
James Smart895427b2017-02-12 13:52:30 -080013533 /* Save EQ associated with this CQ */
13534 cq->assoc_qp = speq;
13535
James Smart6a828b02019-01-28 11:14:31 -080013536 if (!queue_work_on(cq->chann, phba->wq, &cq->spwork))
Dick Kennedyf485c182017-09-29 17:34:34 -070013537 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13538 "0390 Cannot schedule soft IRQ "
13539 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
Bart Van Assched6d189c2019-03-28 11:06:22 -070013540 cqid, cq->queue_id, raw_smp_processor_id());
Dick Kennedyf485c182017-09-29 17:34:34 -070013541}
13542
13543/**
James Smart32517fc2019-01-28 11:14:33 -080013544 * __lpfc_sli4_process_cq - Process elements of a CQ
Dick Kennedyf485c182017-09-29 17:34:34 -070013545 * @phba: Pointer to HBA context object.
James Smart32517fc2019-01-28 11:14:33 -080013546 * @cq: Pointer to CQ to be processed
13547 * @handler: Routine to process each cqe
13548 * @delay: Pointer to usdelay to set in case of rescheduling of the handler
Dick Kennedyf485c182017-09-29 17:34:34 -070013549 *
James Smart32517fc2019-01-28 11:14:33 -080013550 * This routine processes completion queue entries in a CQ. While a valid
13551 * queue element is found, the handler is called. During processing checks
13552 * are made for periodic doorbell writes to let the hardware know of
13553 * element consumption.
Dick Kennedyf485c182017-09-29 17:34:34 -070013554 *
James Smart32517fc2019-01-28 11:14:33 -080013555 * If the max limit on cqes to process is hit, or there are no more valid
13556 * entries, the loop stops. If we processed a sufficient number of elements,
13557 * meaning there is sufficient load, rather than rearming and generating
13558 * another interrupt, a cq rescheduling delay will be set. A delay of 0
13559 * indicates no rescheduling.
13560 *
13561 * Returns True if work scheduled, False otherwise.
Dick Kennedyf485c182017-09-29 17:34:34 -070013562 **/
James Smart32517fc2019-01-28 11:14:33 -080013563static bool
13564__lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
13565 bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
13566 struct lpfc_cqe *), unsigned long *delay)
Dick Kennedyf485c182017-09-29 17:34:34 -070013567{
Dick Kennedyf485c182017-09-29 17:34:34 -070013568 struct lpfc_cqe *cqe;
13569 bool workposted = false;
James Smart32517fc2019-01-28 11:14:33 -080013570 int count = 0, consumed = 0;
13571 bool arm = true;
13572
13573 /* default - no reschedule */
13574 *delay = 0;
13575
13576 if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
13577 goto rearm_and_exit;
Dick Kennedyf485c182017-09-29 17:34:34 -070013578
James Smart4f774512009-05-22 14:52:35 -040013579 /* Process all the entries to the CQ */
James Smartd74a89a2019-05-21 17:48:55 -070013580 cq->q_flag = 0;
James Smart32517fc2019-01-28 11:14:33 -080013581 cqe = lpfc_sli4_cq_get(cq);
13582 while (cqe) {
James Smart32517fc2019-01-28 11:14:33 -080013583 workposted |= handler(phba, cq, cqe);
13584 __lpfc_sli4_consume_cqe(phba, cq, cqe);
13585
13586 consumed++;
13587 if (!(++count % cq->max_proc_limit))
13588 break;
13589
13590 if (!(count % cq->notify_interval)) {
13591 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
13592 LPFC_QUEUE_NOARM);
13593 consumed = 0;
James Smart4f774512009-05-22 14:52:35 -040013594 }
James Smartb84daac2012-08-03 12:35:13 -040013595
James Smartd74a89a2019-05-21 17:48:55 -070013596 if (count == LPFC_NVMET_CQ_NOTIFY)
13597 cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
13598
James Smart32517fc2019-01-28 11:14:33 -080013599 cqe = lpfc_sli4_cq_get(cq);
13600 }
13601 if (count >= phba->cfg_cq_poll_threshold) {
13602 *delay = 1;
13603 arm = false;
13604 }
13605
13606 /* Track the max number of CQEs processed in 1 EQ */
13607 if (count > cq->CQ_max_cqe)
13608 cq->CQ_max_cqe = count;
13609
13610 cq->assoc_qp->EQ_cqe_cnt += count;
13611
13612 /* Catch the no cq entry condition */
13613 if (unlikely(count == 0))
13614 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13615 "0369 No entry from completion queue "
13616 "qid=%d\n", cq->queue_id);
13617
13618 cq->queue_claimed = 0;
13619
13620rearm_and_exit:
13621 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
13622 arm ? LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
13623
13624 return workposted;
13625}
13626
13627/**
13628 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13629 * @cq: pointer to CQ to process
13630 *
13631 * This routine calls the cq processing routine with a handler specific
13632 * to the type of queue bound to it.
13633 *
13634 * The CQ routine returns two values: the first is the calling status,
13635 * which indicates whether work was queued to the background discovery
13636 * thread. If true, the routine should wakeup the discovery thread;
13637 * the second is the delay parameter. If non-zero, rather than rearming
13638 * the CQ and yet another interrupt, the CQ handler should be queued so
13639 * that it is processed in a subsequent polling action. The value of
13640 * the delay indicates when to reschedule it.
13641 **/
13642static void
13643__lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
13644{
13645 struct lpfc_hba *phba = cq->phba;
13646 unsigned long delay;
13647 bool workposted = false;
13648
13649 /* Process and rearm the CQ */
13650 switch (cq->type) {
13651 case LPFC_MCQ:
13652 workposted |= __lpfc_sli4_process_cq(phba, cq,
13653 lpfc_sli4_sp_handle_mcqe,
13654 &delay);
13655 break;
13656 case LPFC_WCQ:
James Smartc00f62e2019-08-14 16:57:11 -070013657 if (cq->subtype == LPFC_IO)
James Smart32517fc2019-01-28 11:14:33 -080013658 workposted |= __lpfc_sli4_process_cq(phba, cq,
13659 lpfc_sli4_fp_handle_cqe,
13660 &delay);
13661 else
13662 workposted |= __lpfc_sli4_process_cq(phba, cq,
13663 lpfc_sli4_sp_handle_cqe,
13664 &delay);
James Smart4f774512009-05-22 14:52:35 -040013665 break;
13666 default:
13667 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13668 "0370 Invalid completion queue type (%d)\n",
13669 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013670 return;
James Smart4f774512009-05-22 14:52:35 -040013671 }
13672
James Smart32517fc2019-01-28 11:14:33 -080013673 if (delay) {
13674 if (!queue_delayed_work_on(cq->chann, phba->wq,
13675 &cq->sched_spwork, delay))
13676 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13677 "0394 Cannot schedule soft IRQ "
13678 "for cqid=%d on CPU %d\n",
13679 cq->queue_id, cq->chann);
13680 }
James Smart4f774512009-05-22 14:52:35 -040013681
13682 /* wake up worker thread if there are works to be done */
13683 if (workposted)
13684 lpfc_worker_wake_up(phba);
13685}
13686
13687/**
James Smart32517fc2019-01-28 11:14:33 -080013688 * lpfc_sli4_sp_process_cq - slow-path work handler when started by
13689 * interrupt
13690 * @work: pointer to work element
13691 *
13692 * translates from the work handler and calls the slow-path handler.
13693 **/
13694static void
13695lpfc_sli4_sp_process_cq(struct work_struct *work)
13696{
13697 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
13698
13699 __lpfc_sli4_sp_process_cq(cq);
13700}
13701
13702/**
13703 * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
13704 * @work: pointer to work element
13705 *
13706 * translates from the work handler and calls the slow-path handler.
13707 **/
13708static void
13709lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
13710{
13711 struct lpfc_queue *cq = container_of(to_delayed_work(work),
13712 struct lpfc_queue, sched_spwork);
13713
13714 __lpfc_sli4_sp_process_cq(cq);
13715}
13716
13717/**
James Smart4f774512009-05-22 14:52:35 -040013718 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013719 * @phba: Pointer to HBA context object.
13720 * @cq: Pointer to associated CQ
13721 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013722 *
13723 * This routine process a fast-path work queue completion entry from fast-path
13724 * event queue for FCP command response completion.
13725 **/
13726static void
James Smart2a76a282012-08-03 12:35:54 -040013727lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013728 struct lpfc_wcqe_complete *wcqe)
13729{
James Smart2a76a282012-08-03 12:35:54 -040013730 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013731 struct lpfc_iocbq *cmdiocbq;
13732 struct lpfc_iocbq irspiocbq;
13733 unsigned long iflags;
13734
James Smart4f774512009-05-22 14:52:35 -040013735 /* Check for response status */
13736 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13737 /* If resource errors reported from HBA, reduce queue
13738 * depth of the SCSI device.
13739 */
James Smarte3d2b802012-08-14 14:25:43 -040013740 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13741 IOSTAT_LOCAL_REJECT)) &&
13742 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13743 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013744 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013745
James Smart4f774512009-05-22 14:52:35 -040013746 /* Log the error status */
James Smart11f0e342018-05-04 20:37:57 -070013747 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13748 "0373 FCP CQE error: status=x%x: "
13749 "CQE: %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013750 bf_get(lpfc_wcqe_c_status, wcqe),
James Smart11f0e342018-05-04 20:37:57 -070013751 wcqe->word0, wcqe->total_data_placed,
13752 wcqe->parameter, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013753 }
13754
13755 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013756 spin_lock_irqsave(&pring->ring_lock, iflags);
13757 pring->stats.iocb_event++;
James Smarte2a8be52019-05-06 17:26:47 -070013758 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013759 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13760 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart4f774512009-05-22 14:52:35 -040013761 if (unlikely(!cmdiocbq)) {
13762 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13763 "0374 FCP complete with no corresponding "
13764 "cmdiocb: iotag (%d)\n",
13765 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13766 return;
13767 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013768#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13769 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13770#endif
James Smart895427b2017-02-12 13:52:30 -080013771 if (cmdiocbq->iocb_cmpl == NULL) {
13772 if (cmdiocbq->wqe_cmpl) {
13773 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13774 spin_lock_irqsave(&phba->hbalock, iflags);
13775 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13776 spin_unlock_irqrestore(&phba->hbalock, iflags);
13777 }
13778
13779 /* Pass the cmd_iocb and the wcqe to the upper layer */
13780 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13781 return;
13782 }
James Smart4f774512009-05-22 14:52:35 -040013783 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13784 "0375 FCP cmdiocb not callback function "
13785 "iotag: (%d)\n",
13786 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13787 return;
13788 }
13789
13790 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013791 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013792
James Smart0f65ff62010-02-26 14:14:23 -050013793 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13794 spin_lock_irqsave(&phba->hbalock, iflags);
13795 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13796 spin_unlock_irqrestore(&phba->hbalock, iflags);
13797 }
13798
James Smart4f774512009-05-22 14:52:35 -040013799 /* Pass the cmd_iocb and the rsp state to the upper layer */
13800 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13801}
13802
13803/**
13804 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13805 * @phba: Pointer to HBA context object.
13806 * @cq: Pointer to completion queue.
13807 * @wcqe: Pointer to work-queue completion queue entry.
13808 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013809 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013810 * proper WQ release routine to the slow-path WQ.
13811 **/
13812static void
13813lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13814 struct lpfc_wcqe_release *wcqe)
13815{
13816 struct lpfc_queue *childwq;
13817 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013818 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013819
13820 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013821 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013822 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013823 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013824 lpfc_sli4_wq_release(childwq,
13825 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080013826 if (childwq->q_flag & HBA_NVMET_WQFULL)
13827 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040013828 wqid_matched = true;
13829 break;
13830 }
13831 }
13832 /* Report warning log message if no match found */
13833 if (wqid_matched != true)
13834 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13835 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013836 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013837}
13838
13839/**
James Smart2d7dbc42017-02-12 13:52:35 -080013840 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13841 * @phba: Pointer to HBA context object.
13842 * @rcqe: Pointer to receive-queue completion queue entry.
13843 *
13844 * This routine process a receive-queue completion queue entry.
13845 *
13846 * Return: true if work posted to worker thread, otherwise false.
13847 **/
13848static bool
13849lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13850 struct lpfc_rcqe *rcqe)
13851{
13852 bool workposted = false;
13853 struct lpfc_queue *hrq;
13854 struct lpfc_queue *drq;
13855 struct rqb_dmabuf *dma_buf;
13856 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013857 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013858 uint32_t status, rq_id;
13859 unsigned long iflags;
13860 uint32_t fctl, idx;
13861
13862 if ((phba->nvmet_support == 0) ||
13863 (phba->sli4_hba.nvmet_cqset == NULL))
13864 return workposted;
13865
13866 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13867 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13868 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13869
13870 /* sanity check on queue memory */
13871 if (unlikely(!hrq) || unlikely(!drq))
13872 return workposted;
13873
13874 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13875 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13876 else
13877 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13878
13879 if ((phba->nvmet_support == 0) ||
13880 (rq_id != hrq->queue_id))
13881 return workposted;
13882
13883 status = bf_get(lpfc_rcqe_status, rcqe);
13884 switch (status) {
13885 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13886 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13887 "6126 Receive Frame Truncated!!\n");
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060013888 /* fall through */
James Smart2d7dbc42017-02-12 13:52:35 -080013889 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080013890 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013891 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080013892 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13893 if (!dma_buf) {
13894 hrq->RQ_no_buf_found++;
13895 spin_unlock_irqrestore(&phba->hbalock, iflags);
13896 goto out;
13897 }
13898 spin_unlock_irqrestore(&phba->hbalock, iflags);
13899 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013900 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013901 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13902
13903 /* Just some basic sanity checks on FCP Command frame */
13904 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13905 fc_hdr->fh_f_ctl[1] << 8 |
13906 fc_hdr->fh_f_ctl[2]);
13907 if (((fctl &
13908 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13909 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13910 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13911 goto drop;
13912
13913 if (fc_hdr->fh_type == FC_TYPE_FCP) {
James Smartd74a89a2019-05-21 17:48:55 -070013914 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013915 lpfc_nvmet_unsol_fcp_event(
James Smartd74a89a2019-05-21 17:48:55 -070013916 phba, idx, dma_buf, cq->isr_timestamp,
13917 cq->q_flag & HBA_NVMET_CQ_NOTIFY);
James Smart2d7dbc42017-02-12 13:52:35 -080013918 return false;
13919 }
13920drop:
James Smart22b738a2019-03-12 16:30:11 -070013921 lpfc_rq_buf_free(phba, &dma_buf->hbuf);
James Smart2d7dbc42017-02-12 13:52:35 -080013922 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013923 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013924 if (phba->nvmet_support) {
13925 tgtp = phba->targetport->private;
13926 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13927 "6401 RQE Error x%x, posted %d err_cnt "
13928 "%d: %x %x %x\n",
13929 status, hrq->RQ_buf_posted,
13930 hrq->RQ_no_posted_buf,
13931 atomic_read(&tgtp->rcv_fcp_cmd_in),
13932 atomic_read(&tgtp->rcv_fcp_cmd_out),
13933 atomic_read(&tgtp->xmt_fcp_release));
13934 }
13935 /* fallthrough */
13936
13937 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013938 hrq->RQ_no_posted_buf++;
13939 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013940 break;
13941 }
13942out:
13943 return workposted;
13944}
13945
13946/**
James Smart895427b2017-02-12 13:52:30 -080013947 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart32517fc2019-01-28 11:14:33 -080013948 * @phba: adapter with cq
James Smart4f774512009-05-22 14:52:35 -040013949 * @cq: Pointer to the completion queue.
13950 * @eqe: Pointer to fast-path completion queue entry.
13951 *
13952 * This routine process a fast-path work queue completion entry from fast-path
13953 * event queue for FCP command response completion.
James Smart32517fc2019-01-28 11:14:33 -080013954 *
13955 * Return: true if work posted to worker thread, otherwise false.
James Smart4f774512009-05-22 14:52:35 -040013956 **/
James Smart32517fc2019-01-28 11:14:33 -080013957static bool
James Smart895427b2017-02-12 13:52:30 -080013958lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013959 struct lpfc_cqe *cqe)
13960{
13961 struct lpfc_wcqe_release wcqe;
13962 bool workposted = false;
13963
13964 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013965 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013966
13967 /* Check and process for different type of WCQE and dispatch */
13968 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13969 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013970 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013971 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013972 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013973 phba->last_completion_time = jiffies;
James Smartc00f62e2019-08-14 16:57:11 -070013974 if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)
James Smart895427b2017-02-12 13:52:30 -080013975 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013976 (struct lpfc_wcqe_complete *)&wcqe);
13977 break;
13978 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013979 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013980 /* Process the WQ release event */
13981 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13982 (struct lpfc_wcqe_release *)&wcqe);
13983 break;
13984 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013985 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013986 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013987 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013988 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13989 (struct sli4_wcqe_xri_aborted *)&wcqe);
13990 break;
James Smart895427b2017-02-12 13:52:30 -080013991 case CQE_CODE_RECEIVE_V1:
13992 case CQE_CODE_RECEIVE:
13993 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013994 if (cq->subtype == LPFC_NVMET) {
13995 workposted = lpfc_sli4_nvmet_handle_rcqe(
13996 phba, cq, (struct lpfc_rcqe *)&wcqe);
13997 }
James Smart895427b2017-02-12 13:52:30 -080013998 break;
James Smart4f774512009-05-22 14:52:35 -040013999 default:
14000 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080014001 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040014002 bf_get(lpfc_wcqe_c_code, &wcqe));
14003 break;
14004 }
14005 return workposted;
14006}
14007
14008/**
James Smart67d12732012-08-03 12:36:13 -040014009 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040014010 * @phba: Pointer to HBA context object.
14011 * @eqe: Pointer to fast-path event queue entry.
14012 *
14013 * This routine process a event queue entry from the fast-path event queue.
14014 * It will check the MajorCode and MinorCode to determine this is for a
14015 * completion event on a completion queue, if not, an error shall be logged
14016 * and just return. Otherwise, it will get to the corresponding completion
14017 * queue and process all the entries on the completion queue, rearm the
14018 * completion queue, and then return.
14019 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070014020static void
James Smart32517fc2019-01-28 11:14:33 -080014021lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
14022 struct lpfc_eqe *eqe)
James Smart4f774512009-05-22 14:52:35 -040014023{
James Smart895427b2017-02-12 13:52:30 -080014024 struct lpfc_queue *cq = NULL;
James Smart32517fc2019-01-28 11:14:33 -080014025 uint32_t qidx = eq->hdwq;
James Smart2d7dbc42017-02-12 13:52:35 -080014026 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040014027
James Smartcb5172e2010-03-15 11:25:07 -040014028 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040014029 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040014030 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040014031 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040014032 bf_get_le32(lpfc_eqe_major_code, eqe),
14033 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070014034 return;
James Smart4f774512009-05-22 14:52:35 -040014035 }
14036
James Smart67d12732012-08-03 12:36:13 -040014037 /* Get the reference to the corresponding CQ */
14038 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14039
James Smart6a828b02019-01-28 11:14:31 -080014040 /* Use the fast lookup method first */
14041 if (cqid <= phba->sli4_hba.cq_max) {
14042 cq = phba->sli4_hba.cq_lookup[cqid];
14043 if (cq)
14044 goto work_cq;
James Smartcdb42be2019-01-28 11:14:21 -080014045 }
14046
14047 /* Next check for NVMET completion */
James Smart2d7dbc42017-02-12 13:52:35 -080014048 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
14049 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
14050 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
14051 /* Process NVMET unsol rcv */
14052 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
14053 goto process_cq;
14054 }
14055 }
14056
James Smart895427b2017-02-12 13:52:30 -080014057 if (phba->sli4_hba.nvmels_cq &&
14058 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
14059 /* Process NVME unsol rcv */
14060 cq = phba->sli4_hba.nvmels_cq;
14061 }
14062
14063 /* Otherwise this is a Slow path event */
14064 if (cq == NULL) {
James Smartcdb42be2019-01-28 11:14:21 -080014065 lpfc_sli4_sp_handle_eqe(phba, eqe,
14066 phba->sli4_hba.hdwq[qidx].hba_eq);
Dick Kennedyf485c182017-09-29 17:34:34 -070014067 return;
James Smart67d12732012-08-03 12:36:13 -040014068 }
14069
James Smart895427b2017-02-12 13:52:30 -080014070process_cq:
James Smart4f774512009-05-22 14:52:35 -040014071 if (unlikely(cqid != cq->queue_id)) {
14072 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14073 "0368 Miss-matched fast-path completion "
14074 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
14075 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070014076 return;
James Smart4f774512009-05-22 14:52:35 -040014077 }
14078
James Smart6a828b02019-01-28 11:14:31 -080014079work_cq:
James Smartd74a89a2019-05-21 17:48:55 -070014080#if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
14081 if (phba->ktime_on)
14082 cq->isr_timestamp = ktime_get_ns();
14083 else
14084 cq->isr_timestamp = 0;
14085#endif
James Smart45aa3122019-01-28 11:14:29 -080014086 if (!queue_work_on(cq->chann, phba->wq, &cq->irqwork))
Dick Kennedyf485c182017-09-29 17:34:34 -070014087 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14088 "0363 Cannot schedule soft IRQ "
14089 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
Bart Van Assched6d189c2019-03-28 11:06:22 -070014090 cqid, cq->queue_id, raw_smp_processor_id());
Dick Kennedyf485c182017-09-29 17:34:34 -070014091}
14092
14093/**
James Smart32517fc2019-01-28 11:14:33 -080014094 * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
14095 * @cq: Pointer to CQ to be processed
Dick Kennedyf485c182017-09-29 17:34:34 -070014096 *
James Smart32517fc2019-01-28 11:14:33 -080014097 * This routine calls the cq processing routine with the handler for
14098 * fast path CQEs.
14099 *
14100 * The CQ routine returns two values: the first is the calling status,
14101 * which indicates whether work was queued to the background discovery
14102 * thread. If true, the routine should wakeup the discovery thread;
14103 * the second is the delay parameter. If non-zero, rather than rearming
14104 * the CQ and yet another interrupt, the CQ handler should be queued so
14105 * that it is processed in a subsequent polling action. The value of
14106 * the delay indicates when to reschedule it.
Dick Kennedyf485c182017-09-29 17:34:34 -070014107 **/
14108static void
James Smart32517fc2019-01-28 11:14:33 -080014109__lpfc_sli4_hba_process_cq(struct lpfc_queue *cq)
Dick Kennedyf485c182017-09-29 17:34:34 -070014110{
Dick Kennedyf485c182017-09-29 17:34:34 -070014111 struct lpfc_hba *phba = cq->phba;
James Smart32517fc2019-01-28 11:14:33 -080014112 unsigned long delay;
Dick Kennedyf485c182017-09-29 17:34:34 -070014113 bool workposted = false;
Dick Kennedyf485c182017-09-29 17:34:34 -070014114
James Smart32517fc2019-01-28 11:14:33 -080014115 /* process and rearm the CQ */
14116 workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
14117 &delay);
14118
14119 if (delay) {
14120 if (!queue_delayed_work_on(cq->chann, phba->wq,
14121 &cq->sched_irqwork, delay))
14122 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14123 "0367 Cannot schedule soft IRQ "
14124 "for cqid=%d on CPU %d\n",
14125 cq->queue_id, cq->chann);
James Smart4f774512009-05-22 14:52:35 -040014126 }
14127
James Smart4f774512009-05-22 14:52:35 -040014128 /* wake up worker thread if there are works to be done */
14129 if (workposted)
14130 lpfc_worker_wake_up(phba);
14131}
14132
James Smart1ba981f2014-02-20 09:56:45 -050014133/**
James Smart32517fc2019-01-28 11:14:33 -080014134 * lpfc_sli4_hba_process_cq - fast-path work handler when started by
14135 * interrupt
14136 * @work: pointer to work element
James Smart1ba981f2014-02-20 09:56:45 -050014137 *
James Smart32517fc2019-01-28 11:14:33 -080014138 * translates from the work handler and calls the fast-path handler.
James Smart1ba981f2014-02-20 09:56:45 -050014139 **/
14140static void
James Smart32517fc2019-01-28 11:14:33 -080014141lpfc_sli4_hba_process_cq(struct work_struct *work)
James Smart1ba981f2014-02-20 09:56:45 -050014142{
James Smart32517fc2019-01-28 11:14:33 -080014143 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
James Smart1ba981f2014-02-20 09:56:45 -050014144
James Smart32517fc2019-01-28 11:14:33 -080014145 __lpfc_sli4_hba_process_cq(cq);
James Smart1ba981f2014-02-20 09:56:45 -050014146}
14147
14148/**
James Smart32517fc2019-01-28 11:14:33 -080014149 * lpfc_sli4_hba_process_cq - fast-path work handler when started by timer
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 **/
James Smart32517fc2019-01-28 11:14:33 -080014154static void
14155lpfc_sli4_dly_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(to_delayed_work(work),
14158 struct lpfc_queue, sched_irqwork);
James Smart1ba981f2014-02-20 09:56:45 -050014159
James Smart32517fc2019-01-28 11:14:33 -080014160 __lpfc_sli4_hba_process_cq(cq);
James Smart1ba981f2014-02-20 09:56:45 -050014161}
14162
James Smart4f774512009-05-22 14:52:35 -040014163/**
James Smart67d12732012-08-03 12:36:13 -040014164 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040014165 * @irq: Interrupt number.
14166 * @dev_id: The device context pointer.
14167 *
14168 * This function is directly called from the PCI layer as an interrupt
14169 * service routine when device with SLI-4 interface spec is enabled with
14170 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
14171 * ring event in the HBA. However, when the device is enabled with either
14172 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14173 * device-level interrupt handler. When the PCI slot is in error recovery
14174 * or the HBA is undergoing initialization, the interrupt handler will not
14175 * process the interrupt. The SCSI FCP fast-path ring event are handled in
14176 * the intrrupt context. This function is called without any lock held.
14177 * It gets the hbalock to access and update SLI data structures. Note that,
14178 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14179 * equal to that of FCP CQ index.
14180 *
James Smart67d12732012-08-03 12:36:13 -040014181 * The link attention and ELS ring attention events are handled
14182 * by the worker thread. The interrupt handler signals the worker thread
14183 * and returns for these events. This function is called without any lock
14184 * held. It gets the hbalock to access and update SLI data structures.
14185 *
James Smart4f774512009-05-22 14:52:35 -040014186 * This function returns IRQ_HANDLED when interrupt is handled else it
14187 * returns IRQ_NONE.
14188 **/
14189irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040014190lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040014191{
14192 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080014193 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040014194 struct lpfc_queue *fpeq;
James Smart4f774512009-05-22 14:52:35 -040014195 unsigned long iflag;
14196 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080014197 int hba_eqidx;
James Smart32517fc2019-01-28 11:14:33 -080014198 struct lpfc_eq_intr_info *eqi;
14199 uint32_t icnt;
James Smart4f774512009-05-22 14:52:35 -040014200
14201 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080014202 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14203 phba = hba_eq_hdl->phba;
14204 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040014205
14206 if (unlikely(!phba))
14207 return IRQ_NONE;
James Smartcdb42be2019-01-28 11:14:21 -080014208 if (unlikely(!phba->sli4_hba.hdwq))
James Smart5350d872011-10-10 21:33:49 -040014209 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014210
14211 /* Get to the EQ struct associated with this vector */
James Smart657add42019-05-21 17:49:06 -070014212 fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
James Smart2e90f4b2011-12-13 13:22:37 -050014213 if (unlikely(!fpeq))
14214 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014215
14216 /* Check device state for handling interrupt */
14217 if (unlikely(lpfc_intr_state_check(phba))) {
14218 /* Check again for link_state with lock held */
14219 spin_lock_irqsave(&phba->hbalock, iflag);
14220 if (phba->link_state < LPFC_LINK_DOWN)
14221 /* Flush, clear interrupt, and rearm the EQ */
14222 lpfc_sli4_eq_flush(phba, fpeq);
14223 spin_unlock_irqrestore(&phba->hbalock, iflag);
14224 return IRQ_NONE;
14225 }
14226
James Smart32517fc2019-01-28 11:14:33 -080014227 eqi = phba->sli4_hba.eq_info;
14228 icnt = this_cpu_inc_return(eqi->icnt);
Bart Van Assched6d189c2019-03-28 11:06:22 -070014229 fpeq->last_cpu = raw_smp_processor_id();
James Smart4f774512009-05-22 14:52:35 -040014230
James Smart32517fc2019-01-28 11:14:33 -080014231 if (icnt > LPFC_EQD_ISR_TRIGGER &&
14232 phba->cfg_irq_chann == 1 &&
14233 phba->cfg_auto_imax &&
14234 fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
14235 phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
14236 lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
James Smartb84daac2012-08-03 12:35:13 -040014237
James Smart32517fc2019-01-28 11:14:33 -080014238 /* process and rearm the EQ */
14239 ecount = lpfc_sli4_process_eq(phba, fpeq);
James Smart4f774512009-05-22 14:52:35 -040014240
14241 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040014242 fpeq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040014243 if (phba->intr_type == MSIX)
14244 /* MSI-X treated interrupt served as no EQ share INT */
14245 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14246 "0358 MSI-X interrupt with no EQE\n");
14247 else
14248 /* Non MSI-X treated on interrupt as EQ share INT */
14249 return IRQ_NONE;
14250 }
14251
14252 return IRQ_HANDLED;
14253} /* lpfc_sli4_fp_intr_handler */
14254
14255/**
14256 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14257 * @irq: Interrupt number.
14258 * @dev_id: The device context pointer.
14259 *
14260 * This function is the device-level interrupt handler to device with SLI-4
14261 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14262 * interrupt mode is enabled and there is an event in the HBA which requires
14263 * driver attention. This function invokes the slow-path interrupt attention
14264 * handling function and fast-path interrupt attention handling function in
14265 * turn to process the relevant HBA attention events. This function is called
14266 * without any lock held. It gets the hbalock to access and update SLI data
14267 * structures.
14268 *
14269 * This function returns IRQ_HANDLED when interrupt is handled, else it
14270 * returns IRQ_NONE.
14271 **/
14272irqreturn_t
14273lpfc_sli4_intr_handler(int irq, void *dev_id)
14274{
14275 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040014276 irqreturn_t hba_irq_rc;
14277 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080014278 int qidx;
James Smart4f774512009-05-22 14:52:35 -040014279
14280 /* Get the driver's phba structure from the dev_id */
14281 phba = (struct lpfc_hba *)dev_id;
14282
14283 if (unlikely(!phba))
14284 return IRQ_NONE;
14285
14286 /*
James Smart4f774512009-05-22 14:52:35 -040014287 * Invoke fast-path host attention interrupt handling as appropriate.
14288 */
James Smart6a828b02019-01-28 11:14:31 -080014289 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040014290 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014291 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040014292 if (hba_irq_rc == IRQ_HANDLED)
14293 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040014294 }
14295
James Smart67d12732012-08-03 12:36:13 -040014296 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014297} /* lpfc_sli4_intr_handler */
14298
14299/**
14300 * lpfc_sli4_queue_free - free a queue structure and associated memory
14301 * @queue: The queue structure to free.
14302 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040014303 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040014304 * the host resident queue. This function must be called after destroying the
14305 * queue on the HBA.
14306 **/
14307void
14308lpfc_sli4_queue_free(struct lpfc_queue *queue)
14309{
14310 struct lpfc_dmabuf *dmabuf;
14311
14312 if (!queue)
14313 return;
14314
James Smart4645f7b2019-03-12 16:30:14 -070014315 if (!list_empty(&queue->wq_list))
14316 list_del(&queue->wq_list);
14317
James Smart4f774512009-05-22 14:52:35 -040014318 while (!list_empty(&queue->page_list)) {
14319 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14320 list);
James Smart81b96ed2017-11-20 16:00:29 -080014321 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040014322 dmabuf->virt, dmabuf->phys);
14323 kfree(dmabuf);
14324 }
James Smart895427b2017-02-12 13:52:30 -080014325 if (queue->rqbp) {
14326 lpfc_free_rq_buffer(queue->phba, queue);
14327 kfree(queue->rqbp);
14328 }
James Smartd1f525a2017-04-21 16:04:55 -070014329
James Smart32517fc2019-01-28 11:14:33 -080014330 if (!list_empty(&queue->cpu_list))
14331 list_del(&queue->cpu_list);
14332
James Smart4f774512009-05-22 14:52:35 -040014333 kfree(queue);
14334 return;
14335}
14336
14337/**
14338 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14339 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080014340 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040014341 * @entry_size: The size of each queue entry for this queue.
14342 * @entry count: The number of entries that this queue will handle.
James Smartc1a21eb2019-03-12 16:30:29 -070014343 * @cpu: The cpu that will primarily utilize this queue.
James Smart4f774512009-05-22 14:52:35 -040014344 *
14345 * This function allocates a queue structure and the DMAable memory used for
14346 * the host resident queue. This function must be called before creating the
14347 * queue on the HBA.
14348 **/
14349struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080014350lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
James Smartc1a21eb2019-03-12 16:30:29 -070014351 uint32_t entry_size, uint32_t entry_count, int cpu)
James Smart4f774512009-05-22 14:52:35 -040014352{
14353 struct lpfc_queue *queue;
14354 struct lpfc_dmabuf *dmabuf;
James Smartcb5172e2010-03-15 11:25:07 -040014355 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart9afbee32019-03-12 16:30:28 -070014356 uint16_t x, pgcnt;
James Smart4f774512009-05-22 14:52:35 -040014357
James Smartcb5172e2010-03-15 11:25:07 -040014358 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014359 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040014360
James Smart9afbee32019-03-12 16:30:28 -070014361 pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080014362
14363 /* If needed, Adjust page count to match the max the adapter supports */
James Smart9afbee32019-03-12 16:30:28 -070014364 if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
14365 pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
14366
James Smartc1a21eb2019-03-12 16:30:29 -070014367 queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),
14368 GFP_KERNEL, cpu_to_node(cpu));
James Smart9afbee32019-03-12 16:30:28 -070014369 if (!queue)
14370 return NULL;
James Smart895427b2017-02-12 13:52:30 -080014371
James Smart4f774512009-05-22 14:52:35 -040014372 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080014373 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080014374 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040014375 INIT_LIST_HEAD(&queue->page_list);
14376 INIT_LIST_HEAD(&queue->child_list);
James Smart32517fc2019-01-28 11:14:33 -080014377 INIT_LIST_HEAD(&queue->cpu_list);
James Smart81b96ed2017-11-20 16:00:29 -080014378
14379 /* Set queue parameters now. If the system cannot provide memory
14380 * resources, the free routine needs to know what was allocated.
14381 */
James Smart9afbee32019-03-12 16:30:28 -070014382 queue->page_count = pgcnt;
14383 queue->q_pgs = (void **)&queue[1];
14384 queue->entry_cnt_per_pg = hw_page_size / entry_size;
James Smart81b96ed2017-11-20 16:00:29 -080014385 queue->entry_size = entry_size;
14386 queue->entry_count = entry_count;
14387 queue->page_size = hw_page_size;
14388 queue->phba = phba;
14389
James Smart9afbee32019-03-12 16:30:28 -070014390 for (x = 0; x < queue->page_count; x++) {
James Smartc1a21eb2019-03-12 16:30:29 -070014391 dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
14392 dev_to_node(&phba->pcidev->dev));
James Smart4f774512009-05-22 14:52:35 -040014393 if (!dmabuf)
14394 goto out_fail;
Luis Chamberlain750afb02019-01-04 09:23:09 +010014395 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
14396 hw_page_size, &dmabuf->phys,
14397 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040014398 if (!dmabuf->virt) {
14399 kfree(dmabuf);
14400 goto out_fail;
14401 }
14402 dmabuf->buffer_tag = x;
14403 list_add_tail(&dmabuf->list, &queue->page_list);
James Smart9afbee32019-03-12 16:30:28 -070014404 /* use lpfc_sli4_qe to index a paritcular entry in this page */
14405 queue->q_pgs[x] = dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -040014406 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014407 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14408 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart32517fc2019-01-28 11:14:33 -080014409 INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
14410 INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014411
James Smart32517fc2019-01-28 11:14:33 -080014412 /* notify_interval will be set during q creation */
James Smart64eb4dc2017-05-15 15:20:49 -070014413
James Smart4f774512009-05-22 14:52:35 -040014414 return queue;
14415out_fail:
14416 lpfc_sli4_queue_free(queue);
14417 return NULL;
14418}
14419
14420/**
James Smart962bc512013-01-03 15:44:00 -050014421 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14422 * @phba: HBA structure that indicates port to create a queue on.
14423 * @pci_barset: PCI BAR set flag.
14424 *
14425 * This function shall perform iomap of the specified PCI BAR address to host
14426 * memory address if not already done so and return it. The returned host
14427 * memory address can be NULL.
14428 */
14429static void __iomem *
14430lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14431{
James Smart962bc512013-01-03 15:44:00 -050014432 if (!phba->pcidev)
14433 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014434
14435 switch (pci_barset) {
14436 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014437 return phba->pci_bar0_memmap_p;
14438 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014439 return phba->pci_bar2_memmap_p;
14440 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014441 return phba->pci_bar4_memmap_p;
14442 default:
14443 break;
14444 }
14445 return NULL;
14446}
14447
14448/**
James Smartcb733e32019-01-28 11:14:32 -080014449 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
14450 * @phba: HBA structure that EQs are on.
14451 * @startq: The starting EQ index to modify
14452 * @numq: The number of EQs (consecutive indexes) to modify
14453 * @usdelay: amount of delay
James Smart173edbb2012-06-12 13:54:50 -040014454 *
James Smartcb733e32019-01-28 11:14:32 -080014455 * This function revises the EQ delay on 1 or more EQs. The EQ delay
14456 * is set either by writing to a register (if supported by the SLI Port)
14457 * or by mailbox command. The mailbox command allows several EQs to be
14458 * updated at once.
James Smart173edbb2012-06-12 13:54:50 -040014459 *
James Smartcb733e32019-01-28 11:14:32 -080014460 * The @phba struct is used to send a mailbox command to HBA. The @startq
14461 * is used to get the starting EQ index to change. The @numq value is
14462 * used to specify how many consecutive EQ indexes, starting at EQ index,
14463 * are to be changed. This function is asynchronous and will wait for any
14464 * mailbox commands to finish before returning.
James Smart173edbb2012-06-12 13:54:50 -040014465 *
James Smartcb733e32019-01-28 11:14:32 -080014466 * On success this function will return a zero. If unable to allocate
14467 * enough memory this function will return -ENOMEM. If a mailbox command
14468 * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
14469 * have had their delay multipler changed.
James Smart173edbb2012-06-12 13:54:50 -040014470 **/
James Smartcb733e32019-01-28 11:14:32 -080014471void
James Smart0cf07f842017-06-01 21:07:10 -070014472lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
James Smartcb733e32019-01-28 11:14:32 -080014473 uint32_t numq, uint32_t usdelay)
James Smart173edbb2012-06-12 13:54:50 -040014474{
14475 struct lpfc_mbx_modify_eq_delay *eq_delay;
14476 LPFC_MBOXQ_t *mbox;
14477 struct lpfc_queue *eq;
James Smartcb733e32019-01-28 11:14:32 -080014478 int cnt = 0, rc, length;
James Smart173edbb2012-06-12 13:54:50 -040014479 uint32_t shdr_status, shdr_add_status;
James Smartcb733e32019-01-28 11:14:32 -080014480 uint32_t dmult;
James Smart895427b2017-02-12 13:52:30 -080014481 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014482 union lpfc_sli4_cfg_shdr *shdr;
James Smart173edbb2012-06-12 13:54:50 -040014483
James Smart6a828b02019-01-28 11:14:31 -080014484 if (startq >= phba->cfg_irq_chann)
James Smartcb733e32019-01-28 11:14:32 -080014485 return;
14486
14487 if (usdelay > 0xFFFF) {
14488 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
14489 "6429 usdelay %d too large. Scaled down to "
14490 "0xFFFF.\n", usdelay);
14491 usdelay = 0xFFFF;
14492 }
14493
14494 /* set values by EQ_DELAY register if supported */
14495 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14496 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070014497 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smartcb733e32019-01-28 11:14:32 -080014498 if (!eq)
14499 continue;
14500
James Smart32517fc2019-01-28 11:14:33 -080014501 lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
James Smartcb733e32019-01-28 11:14:32 -080014502
14503 if (++cnt >= numq)
14504 break;
14505 }
James Smartcb733e32019-01-28 11:14:32 -080014506 return;
14507 }
14508
14509 /* Otherwise, set values by mailbox cmd */
James Smart173edbb2012-06-12 13:54:50 -040014510
14511 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smartcb733e32019-01-28 11:14:32 -080014512 if (!mbox) {
14513 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_FCP | LOG_NVME,
14514 "6428 Failed allocating mailbox cmd buffer."
14515 " EQ delay was not set.\n");
14516 return;
14517 }
James Smart173edbb2012-06-12 13:54:50 -040014518 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14519 sizeof(struct lpfc_sli4_cfg_mhdr));
14520 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14521 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14522 length, LPFC_SLI4_MBX_EMBED);
14523 eq_delay = &mbox->u.mqe.un.eq_delay;
14524
14525 /* Calculate delay multiper from maximum interrupt per second */
James Smartcb733e32019-01-28 11:14:32 -080014526 dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
14527 if (dmult)
14528 dmult--;
James Smart0cf07f842017-06-01 21:07:10 -070014529 if (dmult > LPFC_DMULT_MAX)
14530 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014531
James Smart6a828b02019-01-28 11:14:31 -080014532 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070014533 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smart173edbb2012-06-12 13:54:50 -040014534 if (!eq)
14535 continue;
James Smartcb733e32019-01-28 11:14:32 -080014536 eq->q_mode = usdelay;
James Smart173edbb2012-06-12 13:54:50 -040014537 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14538 eq_delay->u.request.eq[cnt].phase = 0;
14539 eq_delay->u.request.eq[cnt].delay_multi = dmult;
James Smart0cf07f842017-06-01 21:07:10 -070014540
James Smartcb733e32019-01-28 11:14:32 -080014541 if (++cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014542 break;
14543 }
14544 eq_delay->u.request.num_eq = cnt;
14545
14546 mbox->vport = phba->pport;
14547 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080014548 mbox->ctx_buf = NULL;
14549 mbox->ctx_ndlp = NULL;
James Smart173edbb2012-06-12 13:54:50 -040014550 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14551 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14552 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14553 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14554 if (shdr_status || shdr_add_status || rc) {
14555 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14556 "2512 MODIFY_EQ_DELAY mailbox failed with "
14557 "status x%x add_status x%x, mbx status x%x\n",
14558 shdr_status, shdr_add_status, rc);
James Smart173edbb2012-06-12 13:54:50 -040014559 }
14560 mempool_free(mbox, phba->mbox_mem_pool);
James Smartcb733e32019-01-28 11:14:32 -080014561 return;
James Smart173edbb2012-06-12 13:54:50 -040014562}
14563
14564/**
James Smart4f774512009-05-22 14:52:35 -040014565 * lpfc_eq_create - Create an Event Queue on the HBA
14566 * @phba: HBA structure that indicates port to create a queue on.
14567 * @eq: The queue structure to use to create the event queue.
14568 * @imax: The maximum interrupt per second limit.
14569 *
14570 * This function creates an event queue, as detailed in @eq, on a port,
14571 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14572 *
14573 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14574 * is used to get the entry count and entry size that are necessary to
14575 * determine the number of pages to allocate and use for this queue. This
14576 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14577 * event queue. This function is asynchronous and will wait for the mailbox
14578 * command to finish before continuing.
14579 *
14580 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014581 * memory this function will return -ENOMEM. If the queue create mailbox command
14582 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014583 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014584int
James Smartee020062012-09-29 11:28:52 -040014585lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014586{
14587 struct lpfc_mbx_eq_create *eq_create;
14588 LPFC_MBOXQ_t *mbox;
14589 int rc, length, status = 0;
14590 struct lpfc_dmabuf *dmabuf;
14591 uint32_t shdr_status, shdr_add_status;
14592 union lpfc_sli4_cfg_shdr *shdr;
14593 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014594 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14595
James Smart2e90f4b2011-12-13 13:22:37 -050014596 /* sanity check on queue memory */
14597 if (!eq)
14598 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014599 if (!phba->sli4_hba.pc_sli4_params.supported)
14600 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014601
14602 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14603 if (!mbox)
14604 return -ENOMEM;
14605 length = (sizeof(struct lpfc_mbx_eq_create) -
14606 sizeof(struct lpfc_sli4_cfg_mhdr));
14607 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14608 LPFC_MBOX_OPCODE_EQ_CREATE,
14609 length, LPFC_SLI4_MBX_EMBED);
14610 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080014611 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014612 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14613 eq->page_count);
14614 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14615 LPFC_EQE_SIZE);
14616 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080014617
14618 /* Use version 2 of CREATE_EQ if eqav is set */
14619 if (phba->sli4_hba.pc_sli4_params.eqav) {
14620 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14621 LPFC_Q_CREATE_VERSION_2);
14622 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14623 phba->sli4_hba.pc_sli4_params.eqav);
14624 }
14625
James Smart2c9c5a02015-04-07 15:07:15 -040014626 /* don't setup delay multiplier using EQ_CREATE */
14627 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014628 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14629 dmult);
14630 switch (eq->entry_count) {
14631 default:
14632 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14633 "0360 Unsupported EQ count. (%d)\n",
14634 eq->entry_count);
James Smart04d210c2019-05-21 17:49:03 -070014635 if (eq->entry_count < 256) {
14636 status = -EINVAL;
14637 goto out;
14638 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014639 /* fall through - otherwise default to smallest count */
James Smart4f774512009-05-22 14:52:35 -040014640 case 256:
14641 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14642 LPFC_EQ_CNT_256);
14643 break;
14644 case 512:
14645 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14646 LPFC_EQ_CNT_512);
14647 break;
14648 case 1024:
14649 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14650 LPFC_EQ_CNT_1024);
14651 break;
14652 case 2048:
14653 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14654 LPFC_EQ_CNT_2048);
14655 break;
14656 case 4096:
14657 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14658 LPFC_EQ_CNT_4096);
14659 break;
14660 }
14661 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014662 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014663 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14664 putPaddrLow(dmabuf->phys);
14665 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14666 putPaddrHigh(dmabuf->phys);
14667 }
14668 mbox->vport = phba->pport;
14669 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080014670 mbox->ctx_buf = NULL;
14671 mbox->ctx_ndlp = NULL;
James Smart4f774512009-05-22 14:52:35 -040014672 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040014673 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14674 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14675 if (shdr_status || shdr_add_status || rc) {
14676 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14677 "2500 EQ_CREATE mailbox failed with "
14678 "status x%x add_status x%x, mbx status x%x\n",
14679 shdr_status, shdr_add_status, rc);
14680 status = -ENXIO;
14681 }
14682 eq->type = LPFC_EQ;
14683 eq->subtype = LPFC_NONE;
14684 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14685 if (eq->queue_id == 0xFFFF)
14686 status = -ENXIO;
14687 eq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080014688 eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
14689 eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
James Smart04d210c2019-05-21 17:49:03 -070014690out:
James Smart8fa38512009-07-19 10:01:03 -040014691 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014692 return status;
14693}
14694
14695/**
14696 * lpfc_cq_create - Create a Completion Queue on the HBA
14697 * @phba: HBA structure that indicates port to create a queue on.
14698 * @cq: The queue structure to use to create the completion queue.
14699 * @eq: The event queue to bind this completion queue to.
14700 *
14701 * This function creates a completion queue, as detailed in @wq, on a port,
14702 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14703 *
14704 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14705 * is used to get the entry count and entry size that are necessary to
14706 * determine the number of pages to allocate and use for this queue. The @eq
14707 * is used to indicate which event queue to bind this completion queue to. This
14708 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14709 * completion queue. This function is asynchronous and will wait for the mailbox
14710 * command to finish before continuing.
14711 *
14712 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014713 * memory this function will return -ENOMEM. If the queue create mailbox command
14714 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014715 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014716int
James Smart4f774512009-05-22 14:52:35 -040014717lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14718 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14719{
14720 struct lpfc_mbx_cq_create *cq_create;
14721 struct lpfc_dmabuf *dmabuf;
14722 LPFC_MBOXQ_t *mbox;
14723 int rc, length, status = 0;
14724 uint32_t shdr_status, shdr_add_status;
14725 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014726
James Smart2e90f4b2011-12-13 13:22:37 -050014727 /* sanity check on queue memory */
14728 if (!cq || !eq)
14729 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014730
James Smart4f774512009-05-22 14:52:35 -040014731 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14732 if (!mbox)
14733 return -ENOMEM;
14734 length = (sizeof(struct lpfc_mbx_cq_create) -
14735 sizeof(struct lpfc_sli4_cfg_mhdr));
14736 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14737 LPFC_MBOX_OPCODE_CQ_CREATE,
14738 length, LPFC_SLI4_MBX_EMBED);
14739 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014740 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014741 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14742 cq->page_count);
14743 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14744 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014745 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14746 phba->sli4_hba.pc_sli4_params.cqv);
14747 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080014748 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14749 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014750 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14751 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080014752 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14753 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050014754 } else {
14755 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14756 eq->queue_id);
14757 }
James Smart4f774512009-05-22 14:52:35 -040014758 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014759 case 2048:
14760 case 4096:
14761 if (phba->sli4_hba.pc_sli4_params.cqv ==
14762 LPFC_Q_CREATE_VERSION_2) {
14763 cq_create->u.request.context.lpfc_cq_context_count =
14764 cq->entry_count;
14765 bf_set(lpfc_cq_context_count,
14766 &cq_create->u.request.context,
14767 LPFC_CQ_CNT_WORD7);
14768 break;
14769 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014770 /* fall through */
James Smart4f774512009-05-22 14:52:35 -040014771 default:
14772 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014773 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014774 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014775 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014776 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014777 if (cq->entry_count < 256) {
14778 status = -EINVAL;
14779 goto out;
14780 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014781 /* fall through - otherwise default to smallest count */
James Smart4f774512009-05-22 14:52:35 -040014782 case 256:
14783 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14784 LPFC_CQ_CNT_256);
14785 break;
14786 case 512:
14787 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14788 LPFC_CQ_CNT_512);
14789 break;
14790 case 1024:
14791 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14792 LPFC_CQ_CNT_1024);
14793 break;
14794 }
14795 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080014796 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040014797 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14798 putPaddrLow(dmabuf->phys);
14799 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14800 putPaddrHigh(dmabuf->phys);
14801 }
14802 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14803
14804 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014805 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14806 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14807 if (shdr_status || shdr_add_status || rc) {
14808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14809 "2501 CQ_CREATE mailbox failed with "
14810 "status x%x add_status x%x, mbx status x%x\n",
14811 shdr_status, shdr_add_status, rc);
14812 status = -ENXIO;
14813 goto out;
14814 }
14815 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14816 if (cq->queue_id == 0xFFFF) {
14817 status = -ENXIO;
14818 goto out;
14819 }
14820 /* link the cq onto the parent eq child list */
14821 list_add_tail(&cq->list, &eq->child_list);
14822 /* Set up completion queue's type and subtype */
14823 cq->type = type;
14824 cq->subtype = subtype;
14825 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014826 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080014827 cq->assoc_qp = eq;
James Smart4f774512009-05-22 14:52:35 -040014828 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080014829 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
14830 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
James Smart4f774512009-05-22 14:52:35 -040014831
James Smart6a828b02019-01-28 11:14:31 -080014832 if (cq->queue_id > phba->sli4_hba.cq_max)
14833 phba->sli4_hba.cq_max = cq->queue_id;
James Smart8fa38512009-07-19 10:01:03 -040014834out:
14835 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014836 return status;
14837}
14838
14839/**
James Smart2d7dbc42017-02-12 13:52:35 -080014840 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14841 * @phba: HBA structure that indicates port to create a queue on.
14842 * @cqp: The queue structure array to use to create the completion queues.
James Smartcdb42be2019-01-28 11:14:21 -080014843 * @hdwq: The hardware queue array with the EQ to bind completion queues to.
James Smart2d7dbc42017-02-12 13:52:35 -080014844 *
14845 * This function creates a set of completion queue, s to support MRQ
14846 * as detailed in @cqp, on a port,
14847 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14848 *
14849 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14850 * is used to get the entry count and entry size that are necessary to
14851 * determine the number of pages to allocate and use for this queue. The @eq
14852 * is used to indicate which event queue to bind this completion queue to. This
14853 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14854 * completion queue. This function is asynchronous and will wait for the mailbox
14855 * command to finish before continuing.
14856 *
14857 * On success this function will return a zero. If unable to allocate enough
14858 * memory this function will return -ENOMEM. If the queue create mailbox command
14859 * fails this function will return -ENXIO.
14860 **/
14861int
14862lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
James Smartcdb42be2019-01-28 11:14:21 -080014863 struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
14864 uint32_t subtype)
James Smart2d7dbc42017-02-12 13:52:35 -080014865{
14866 struct lpfc_queue *cq;
14867 struct lpfc_queue *eq;
14868 struct lpfc_mbx_cq_create_set *cq_set;
14869 struct lpfc_dmabuf *dmabuf;
14870 LPFC_MBOXQ_t *mbox;
14871 int rc, length, alloclen, status = 0;
14872 int cnt, idx, numcq, page_idx = 0;
14873 uint32_t shdr_status, shdr_add_status;
14874 union lpfc_sli4_cfg_shdr *shdr;
14875 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14876
14877 /* sanity check on queue memory */
14878 numcq = phba->cfg_nvmet_mrq;
James Smartcdb42be2019-01-28 11:14:21 -080014879 if (!cqp || !hdwq || !numcq)
James Smart2d7dbc42017-02-12 13:52:35 -080014880 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080014881
14882 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14883 if (!mbox)
14884 return -ENOMEM;
14885
14886 length = sizeof(struct lpfc_mbx_cq_create_set);
14887 length += ((numcq * cqp[0]->page_count) *
14888 sizeof(struct dma_address));
14889 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14890 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14891 LPFC_SLI4_MBX_NEMBED);
14892 if (alloclen < length) {
14893 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14894 "3098 Allocated DMA memory size (%d) is "
14895 "less than the requested DMA memory size "
14896 "(%d)\n", alloclen, length);
14897 status = -ENOMEM;
14898 goto out;
14899 }
14900 cq_set = mbox->sge_array->addr[0];
14901 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14902 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14903
14904 for (idx = 0; idx < numcq; idx++) {
14905 cq = cqp[idx];
James Smartcdb42be2019-01-28 11:14:21 -080014906 eq = hdwq[idx].hba_eq;
James Smart2d7dbc42017-02-12 13:52:35 -080014907 if (!cq || !eq) {
14908 status = -ENOMEM;
14909 goto out;
14910 }
James Smart81b96ed2017-11-20 16:00:29 -080014911 if (!phba->sli4_hba.pc_sli4_params.supported)
14912 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080014913
14914 switch (idx) {
14915 case 0:
14916 bf_set(lpfc_mbx_cq_create_set_page_size,
14917 &cq_set->u.request,
14918 (hw_page_size / SLI4_PAGE_SIZE));
14919 bf_set(lpfc_mbx_cq_create_set_num_pages,
14920 &cq_set->u.request, cq->page_count);
14921 bf_set(lpfc_mbx_cq_create_set_evt,
14922 &cq_set->u.request, 1);
14923 bf_set(lpfc_mbx_cq_create_set_valid,
14924 &cq_set->u.request, 1);
14925 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14926 &cq_set->u.request, 0);
14927 bf_set(lpfc_mbx_cq_create_set_num_cq,
14928 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080014929 bf_set(lpfc_mbx_cq_create_set_autovalid,
14930 &cq_set->u.request,
14931 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080014932 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014933 case 2048:
14934 case 4096:
14935 if (phba->sli4_hba.pc_sli4_params.cqv ==
14936 LPFC_Q_CREATE_VERSION_2) {
14937 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14938 &cq_set->u.request,
14939 cq->entry_count);
14940 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14941 &cq_set->u.request,
14942 LPFC_CQ_CNT_WORD7);
14943 break;
14944 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014945 /* fall through */
James Smart2d7dbc42017-02-12 13:52:35 -080014946 default:
14947 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14948 "3118 Bad CQ count. (%d)\n",
14949 cq->entry_count);
14950 if (cq->entry_count < 256) {
14951 status = -EINVAL;
14952 goto out;
14953 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014954 /* fall through - otherwise default to smallest */
James Smart2d7dbc42017-02-12 13:52:35 -080014955 case 256:
14956 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14957 &cq_set->u.request, LPFC_CQ_CNT_256);
14958 break;
14959 case 512:
14960 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14961 &cq_set->u.request, LPFC_CQ_CNT_512);
14962 break;
14963 case 1024:
14964 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14965 &cq_set->u.request, LPFC_CQ_CNT_1024);
14966 break;
14967 }
14968 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14969 &cq_set->u.request, eq->queue_id);
14970 break;
14971 case 1:
14972 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14973 &cq_set->u.request, eq->queue_id);
14974 break;
14975 case 2:
14976 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14977 &cq_set->u.request, eq->queue_id);
14978 break;
14979 case 3:
14980 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14981 &cq_set->u.request, eq->queue_id);
14982 break;
14983 case 4:
14984 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14985 &cq_set->u.request, eq->queue_id);
14986 break;
14987 case 5:
14988 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14989 &cq_set->u.request, eq->queue_id);
14990 break;
14991 case 6:
14992 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14993 &cq_set->u.request, eq->queue_id);
14994 break;
14995 case 7:
14996 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14997 &cq_set->u.request, eq->queue_id);
14998 break;
14999 case 8:
15000 bf_set(lpfc_mbx_cq_create_set_eq_id8,
15001 &cq_set->u.request, eq->queue_id);
15002 break;
15003 case 9:
15004 bf_set(lpfc_mbx_cq_create_set_eq_id9,
15005 &cq_set->u.request, eq->queue_id);
15006 break;
15007 case 10:
15008 bf_set(lpfc_mbx_cq_create_set_eq_id10,
15009 &cq_set->u.request, eq->queue_id);
15010 break;
15011 case 11:
15012 bf_set(lpfc_mbx_cq_create_set_eq_id11,
15013 &cq_set->u.request, eq->queue_id);
15014 break;
15015 case 12:
15016 bf_set(lpfc_mbx_cq_create_set_eq_id12,
15017 &cq_set->u.request, eq->queue_id);
15018 break;
15019 case 13:
15020 bf_set(lpfc_mbx_cq_create_set_eq_id13,
15021 &cq_set->u.request, eq->queue_id);
15022 break;
15023 case 14:
15024 bf_set(lpfc_mbx_cq_create_set_eq_id14,
15025 &cq_set->u.request, eq->queue_id);
15026 break;
15027 case 15:
15028 bf_set(lpfc_mbx_cq_create_set_eq_id15,
15029 &cq_set->u.request, eq->queue_id);
15030 break;
15031 }
15032
15033 /* link the cq onto the parent eq child list */
15034 list_add_tail(&cq->list, &eq->child_list);
15035 /* Set up completion queue's type and subtype */
15036 cq->type = type;
15037 cq->subtype = subtype;
15038 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080015039 cq->assoc_qp = eq;
James Smart2d7dbc42017-02-12 13:52:35 -080015040 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015041 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
15042 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
15043 cq->entry_count);
James Smart81b96ed2017-11-20 16:00:29 -080015044 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080015045
15046 rc = 0;
15047 list_for_each_entry(dmabuf, &cq->page_list, list) {
15048 memset(dmabuf->virt, 0, hw_page_size);
15049 cnt = page_idx + dmabuf->buffer_tag;
15050 cq_set->u.request.page[cnt].addr_lo =
15051 putPaddrLow(dmabuf->phys);
15052 cq_set->u.request.page[cnt].addr_hi =
15053 putPaddrHigh(dmabuf->phys);
15054 rc++;
15055 }
15056 page_idx += rc;
15057 }
15058
15059 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15060
15061 /* The IOCTL status is embedded in the mailbox subheader. */
15062 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15063 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15064 if (shdr_status || shdr_add_status || rc) {
15065 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15066 "3119 CQ_CREATE_SET mailbox failed with "
15067 "status x%x add_status x%x, mbx status x%x\n",
15068 shdr_status, shdr_add_status, rc);
15069 status = -ENXIO;
15070 goto out;
15071 }
15072 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
15073 if (rc == 0xFFFF) {
15074 status = -ENXIO;
15075 goto out;
15076 }
15077
15078 for (idx = 0; idx < numcq; idx++) {
15079 cq = cqp[idx];
15080 cq->queue_id = rc + idx;
James Smart6a828b02019-01-28 11:14:31 -080015081 if (cq->queue_id > phba->sli4_hba.cq_max)
15082 phba->sli4_hba.cq_max = cq->queue_id;
James Smart2d7dbc42017-02-12 13:52:35 -080015083 }
15084
15085out:
15086 lpfc_sli4_mbox_cmd_free(phba, mbox);
15087 return status;
15088}
15089
15090/**
James Smartb19a0612010-04-06 14:48:51 -040015091 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040015092 * @phba: HBA structure that indicates port to create a queue on.
15093 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040015094 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
15095 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040015096 *
James Smartb19a0612010-04-06 14:48:51 -040015097 * This function provides failback (fb) functionality when the
15098 * mq_create_ext fails on older FW generations. It's purpose is identical
15099 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040015100 *
James Smartb19a0612010-04-06 14:48:51 -040015101 * This routine cannot fail as all attributes were previously accessed and
15102 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040015103 **/
James Smartb19a0612010-04-06 14:48:51 -040015104static void
15105lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
15106 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040015107{
15108 struct lpfc_mbx_mq_create *mq_create;
15109 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040015110 int length;
James Smart04c68492009-05-22 14:52:52 -040015111
James Smart04c68492009-05-22 14:52:52 -040015112 length = (sizeof(struct lpfc_mbx_mq_create) -
15113 sizeof(struct lpfc_sli4_cfg_mhdr));
15114 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15115 LPFC_MBOX_OPCODE_MQ_CREATE,
15116 length, LPFC_SLI4_MBX_EMBED);
15117 mq_create = &mbox->u.mqe.un.mq_create;
15118 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040015119 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040015120 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040015121 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040015122 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
15123 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040015124 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015125 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15126 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040015127 break;
15128 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015129 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15130 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040015131 break;
15132 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015133 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15134 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040015135 break;
15136 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015137 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15138 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040015139 break;
15140 }
15141 list_for_each_entry(dmabuf, &mq->page_list, list) {
15142 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040015143 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040015144 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040015145 putPaddrHigh(dmabuf->phys);
15146 }
15147}
15148
15149/**
15150 * lpfc_mq_create - Create a mailbox Queue on the HBA
15151 * @phba: HBA structure that indicates port to create a queue on.
15152 * @mq: The queue structure to use to create the mailbox queue.
15153 * @cq: The completion queue to associate with this cq.
15154 * @subtype: The queue's subtype.
15155 *
15156 * This function creates a mailbox queue, as detailed in @mq, on a port,
15157 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
15158 *
15159 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15160 * is used to get the entry count and entry size that are necessary to
15161 * determine the number of pages to allocate and use for this queue. This
15162 * function will send the MQ_CREATE mailbox command to the HBA to setup the
15163 * mailbox queue. This function is asynchronous and will wait for the mailbox
15164 * command to finish before continuing.
15165 *
15166 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015167 * memory this function will return -ENOMEM. If the queue create mailbox command
15168 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040015169 **/
15170int32_t
15171lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15172 struct lpfc_queue *cq, uint32_t subtype)
15173{
15174 struct lpfc_mbx_mq_create *mq_create;
15175 struct lpfc_mbx_mq_create_ext *mq_create_ext;
15176 struct lpfc_dmabuf *dmabuf;
15177 LPFC_MBOXQ_t *mbox;
15178 int rc, length, status = 0;
15179 uint32_t shdr_status, shdr_add_status;
15180 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015181 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040015182
James Smart2e90f4b2011-12-13 13:22:37 -050015183 /* sanity check on queue memory */
15184 if (!mq || !cq)
15185 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015186 if (!phba->sli4_hba.pc_sli4_params.supported)
15187 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040015188
15189 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15190 if (!mbox)
15191 return -ENOMEM;
15192 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15193 sizeof(struct lpfc_sli4_cfg_mhdr));
15194 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15195 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15196 length, LPFC_SLI4_MBX_EMBED);
15197
15198 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050015199 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050015200 bf_set(lpfc_mbx_mq_create_ext_num_pages,
15201 &mq_create_ext->u.request, mq->page_count);
15202 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15203 &mq_create_ext->u.request, 1);
15204 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040015205 &mq_create_ext->u.request, 1);
15206 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15207 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050015208 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15209 &mq_create_ext->u.request, 1);
15210 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15211 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040015212 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050015213 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15214 phba->sli4_hba.pc_sli4_params.mqv);
15215 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15216 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15217 cq->queue_id);
15218 else
15219 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15220 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040015221 switch (mq->entry_count) {
15222 default:
15223 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15224 "0362 Unsupported MQ count. (%d)\n",
15225 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015226 if (mq->entry_count < 16) {
15227 status = -EINVAL;
15228 goto out;
15229 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015230 /* fall through - otherwise default to smallest count */
James Smartb19a0612010-04-06 14:48:51 -040015231 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015232 bf_set(lpfc_mq_context_ring_size,
15233 &mq_create_ext->u.request.context,
15234 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040015235 break;
15236 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015237 bf_set(lpfc_mq_context_ring_size,
15238 &mq_create_ext->u.request.context,
15239 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040015240 break;
15241 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015242 bf_set(lpfc_mq_context_ring_size,
15243 &mq_create_ext->u.request.context,
15244 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040015245 break;
15246 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015247 bf_set(lpfc_mq_context_ring_size,
15248 &mq_create_ext->u.request.context,
15249 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040015250 break;
15251 }
15252 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015253 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040015254 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15255 putPaddrLow(dmabuf->phys);
15256 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040015257 putPaddrHigh(dmabuf->phys);
15258 }
15259 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040015260 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15261 &mq_create_ext->u.response);
15262 if (rc != MBX_SUCCESS) {
15263 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15264 "2795 MQ_CREATE_EXT failed with "
15265 "status x%x. Failback to MQ_CREATE.\n",
15266 rc);
15267 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15268 mq_create = &mbox->u.mqe.un.mq_create;
15269 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15270 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15271 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15272 &mq_create->u.response);
15273 }
15274
James Smart04c68492009-05-22 14:52:52 -040015275 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040015276 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15277 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15278 if (shdr_status || shdr_add_status || rc) {
15279 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15280 "2502 MQ_CREATE mailbox failed with "
15281 "status x%x add_status x%x, mbx status x%x\n",
15282 shdr_status, shdr_add_status, rc);
15283 status = -ENXIO;
15284 goto out;
15285 }
James Smart04c68492009-05-22 14:52:52 -040015286 if (mq->queue_id == 0xFFFF) {
15287 status = -ENXIO;
15288 goto out;
15289 }
15290 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050015291 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040015292 mq->subtype = subtype;
15293 mq->host_index = 0;
15294 mq->hba_index = 0;
15295
15296 /* link the mq onto the parent cq child list */
15297 list_add_tail(&mq->list, &cq->child_list);
15298out:
James Smart8fa38512009-07-19 10:01:03 -040015299 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015300 return status;
15301}
15302
15303/**
James Smart4f774512009-05-22 14:52:35 -040015304 * lpfc_wq_create - Create a Work Queue on the HBA
15305 * @phba: HBA structure that indicates port to create a queue on.
15306 * @wq: The queue structure to use to create the work queue.
15307 * @cq: The completion queue to bind this work queue to.
15308 * @subtype: The subtype of the work queue indicating its functionality.
15309 *
15310 * This function creates a work queue, as detailed in @wq, on a port, described
15311 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15312 *
15313 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15314 * is used to get the entry count and entry size that are necessary to
15315 * determine the number of pages to allocate and use for this queue. The @cq
15316 * is used to indicate which completion queue to bind this work queue to. This
15317 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15318 * work queue. This function is asynchronous and will wait for the mailbox
15319 * command to finish before continuing.
15320 *
15321 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015322 * memory this function will return -ENOMEM. If the queue create mailbox command
15323 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015324 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015325int
James Smart4f774512009-05-22 14:52:35 -040015326lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15327 struct lpfc_queue *cq, uint32_t subtype)
15328{
15329 struct lpfc_mbx_wq_create *wq_create;
15330 struct lpfc_dmabuf *dmabuf;
15331 LPFC_MBOXQ_t *mbox;
15332 int rc, length, status = 0;
15333 uint32_t shdr_status, shdr_add_status;
15334 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015335 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050015336 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050015337 void __iomem *bar_memmap_p;
15338 uint32_t db_offset;
15339 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080015340 uint8_t dpp_barset;
15341 uint32_t dpp_offset;
15342 unsigned long pg_addr;
James Smart81b96ed2017-11-20 16:00:29 -080015343 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040015344
James Smart2e90f4b2011-12-13 13:22:37 -050015345 /* sanity check on queue memory */
15346 if (!wq || !cq)
15347 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015348 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015349 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040015350
15351 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15352 if (!mbox)
15353 return -ENOMEM;
15354 length = (sizeof(struct lpfc_mbx_wq_create) -
15355 sizeof(struct lpfc_sli4_cfg_mhdr));
15356 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15357 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15358 length, LPFC_SLI4_MBX_EMBED);
15359 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015360 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015361 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15362 wq->page_count);
15363 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15364 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040015365
15366 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050015367 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15368 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050015369
James Smartc176ffa2018-01-30 15:58:46 -080015370 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15371 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080015372 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15373 else
15374 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15375
James Smart0c651872013-07-15 18:33:23 -040015376
James Smart1351e692018-02-22 08:18:43 -080015377 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15378 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15379 else
15380 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15381
15382 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040015383 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050015384 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15385 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070015386 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15387 LPFC_Q_CREATE_VERSION_1);
15388
James Smart5a6f1332011-03-11 16:05:35 -050015389 switch (wq->entry_size) {
15390 default:
15391 case 64:
15392 bf_set(lpfc_mbx_wq_create_wqe_size,
15393 &wq_create->u.request_1,
15394 LPFC_WQ_WQE_SIZE_64);
15395 break;
15396 case 128:
15397 bf_set(lpfc_mbx_wq_create_wqe_size,
15398 &wq_create->u.request_1,
15399 LPFC_WQ_WQE_SIZE_128);
15400 break;
15401 }
James Smart1351e692018-02-22 08:18:43 -080015402 /* Request DPP by default */
15403 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080015404 bf_set(lpfc_mbx_wq_create_page_size,
15405 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015406 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015407 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015408 break;
15409 default:
James Smart1351e692018-02-22 08:18:43 -080015410 page = wq_create->u.request.page;
15411 break;
James Smart5a6f1332011-03-11 16:05:35 -050015412 }
James Smart0c651872013-07-15 18:33:23 -040015413
James Smart4f774512009-05-22 14:52:35 -040015414 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015415 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015416 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15417 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015418 }
James Smart962bc512013-01-03 15:44:00 -050015419
15420 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15421 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15422
James Smart4f774512009-05-22 14:52:35 -040015423 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15424 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015425 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15426 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15427 if (shdr_status || shdr_add_status || rc) {
15428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15429 "2503 WQ_CREATE mailbox failed with "
15430 "status x%x add_status x%x, mbx status x%x\n",
15431 shdr_status, shdr_add_status, rc);
15432 status = -ENXIO;
15433 goto out;
15434 }
James Smart1351e692018-02-22 08:18:43 -080015435
15436 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15437 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15438 &wq_create->u.response);
15439 else
15440 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15441 &wq_create->u.response_1);
15442
James Smart4f774512009-05-22 14:52:35 -040015443 if (wq->queue_id == 0xFFFF) {
15444 status = -ENXIO;
15445 goto out;
15446 }
James Smart1351e692018-02-22 08:18:43 -080015447
15448 wq->db_format = LPFC_DB_LIST_FORMAT;
15449 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15450 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15451 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15452 &wq_create->u.response);
15453 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15454 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15456 "3265 WQ[%d] doorbell format "
15457 "not supported: x%x\n",
15458 wq->queue_id, wq->db_format);
15459 status = -EINVAL;
15460 goto out;
15461 }
15462 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15463 &wq_create->u.response);
15464 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15465 pci_barset);
15466 if (!bar_memmap_p) {
15467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15468 "3263 WQ[%d] failed to memmap "
15469 "pci barset:x%x\n",
15470 wq->queue_id, pci_barset);
15471 status = -ENOMEM;
15472 goto out;
15473 }
15474 db_offset = wq_create->u.response.doorbell_offset;
15475 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15476 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15477 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15478 "3252 WQ[%d] doorbell offset "
15479 "not supported: x%x\n",
15480 wq->queue_id, db_offset);
15481 status = -EINVAL;
15482 goto out;
15483 }
15484 wq->db_regaddr = bar_memmap_p + db_offset;
15485 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15486 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15487 "format:x%x\n", wq->queue_id,
15488 pci_barset, db_offset, wq->db_format);
15489 } else
15490 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015491 } else {
James Smart1351e692018-02-22 08:18:43 -080015492 /* Check if DPP was honored by the firmware */
15493 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15494 &wq_create->u.response_1);
15495 if (wq->dpp_enable) {
15496 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15497 &wq_create->u.response_1);
15498 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15499 pci_barset);
15500 if (!bar_memmap_p) {
15501 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15502 "3267 WQ[%d] failed to memmap "
15503 "pci barset:x%x\n",
15504 wq->queue_id, pci_barset);
15505 status = -ENOMEM;
15506 goto out;
15507 }
15508 db_offset = wq_create->u.response_1.doorbell_offset;
15509 wq->db_regaddr = bar_memmap_p + db_offset;
15510 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15511 &wq_create->u.response_1);
15512 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15513 &wq_create->u.response_1);
15514 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15515 dpp_barset);
15516 if (!bar_memmap_p) {
15517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15518 "3268 WQ[%d] failed to memmap "
15519 "pci barset:x%x\n",
15520 wq->queue_id, dpp_barset);
15521 status = -ENOMEM;
15522 goto out;
15523 }
15524 dpp_offset = wq_create->u.response_1.dpp_offset;
15525 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15526 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15527 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15528 "dpp_id:x%x dpp_barset:x%x "
15529 "dpp_offset:x%x\n",
15530 wq->queue_id, pci_barset, db_offset,
15531 wq->dpp_id, dpp_barset, dpp_offset);
15532
15533 /* Enable combined writes for DPP aperture */
15534 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15535#ifdef CONFIG_X86
15536 rc = set_memory_wc(pg_addr, 1);
15537 if (rc) {
15538 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15539 "3272 Cannot setup Combined "
15540 "Write on WQ[%d] - disable DPP\n",
15541 wq->queue_id);
15542 phba->cfg_enable_dpp = 0;
15543 }
15544#else
15545 phba->cfg_enable_dpp = 0;
15546#endif
15547 } else
15548 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015549 }
James Smart895427b2017-02-12 13:52:30 -080015550 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15551 if (wq->pring == NULL) {
15552 status = -ENOMEM;
15553 goto out;
15554 }
James Smart4f774512009-05-22 14:52:35 -040015555 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015556 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015557 wq->subtype = subtype;
15558 wq->host_index = 0;
15559 wq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015560 wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040015561
15562 /* link the wq onto the parent cq child list */
15563 list_add_tail(&wq->list, &cq->child_list);
15564out:
James Smart8fa38512009-07-19 10:01:03 -040015565 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015566 return status;
15567}
15568
15569/**
15570 * lpfc_rq_create - Create a Receive Queue on the HBA
15571 * @phba: HBA structure that indicates port to create a queue on.
15572 * @hrq: The queue structure to use to create the header receive queue.
15573 * @drq: The queue structure to use to create the data receive queue.
15574 * @cq: The completion queue to bind this work queue to.
15575 *
15576 * This function creates a receive buffer queue pair , as detailed in @hrq and
15577 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15578 * to the HBA.
15579 *
15580 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15581 * struct is used to get the entry count that is necessary to determine the
15582 * number of pages to use for this queue. The @cq is used to indicate which
15583 * completion queue to bind received buffers that are posted to these queues to.
15584 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15585 * receive queue pair. This function is asynchronous and will wait for the
15586 * mailbox command to finish before continuing.
15587 *
15588 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015589 * memory this function will return -ENOMEM. If the queue create mailbox command
15590 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015591 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015592int
James Smart4f774512009-05-22 14:52:35 -040015593lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15594 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15595{
15596 struct lpfc_mbx_rq_create *rq_create;
15597 struct lpfc_dmabuf *dmabuf;
15598 LPFC_MBOXQ_t *mbox;
15599 int rc, length, status = 0;
15600 uint32_t shdr_status, shdr_add_status;
15601 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015602 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015603 void __iomem *bar_memmap_p;
15604 uint32_t db_offset;
15605 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015606
James Smart2e90f4b2011-12-13 13:22:37 -050015607 /* sanity check on queue memory */
15608 if (!hrq || !drq || !cq)
15609 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015610 if (!phba->sli4_hba.pc_sli4_params.supported)
15611 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015612
15613 if (hrq->entry_count != drq->entry_count)
15614 return -EINVAL;
15615 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15616 if (!mbox)
15617 return -ENOMEM;
15618 length = (sizeof(struct lpfc_mbx_rq_create) -
15619 sizeof(struct lpfc_sli4_cfg_mhdr));
15620 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15621 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15622 length, LPFC_SLI4_MBX_EMBED);
15623 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015624 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15625 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15626 phba->sli4_hba.pc_sli4_params.rqv);
15627 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15628 bf_set(lpfc_rq_context_rqe_count_1,
15629 &rq_create->u.request.context,
15630 hrq->entry_count);
15631 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015632 bf_set(lpfc_rq_context_rqe_size,
15633 &rq_create->u.request.context,
15634 LPFC_RQE_SIZE_8);
15635 bf_set(lpfc_rq_context_page_size,
15636 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015637 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015638 } else {
15639 switch (hrq->entry_count) {
15640 default:
15641 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15642 "2535 Unsupported RQ count. (%d)\n",
15643 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015644 if (hrq->entry_count < 512) {
15645 status = -EINVAL;
15646 goto out;
15647 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015648 /* fall through - otherwise default to smallest count */
James Smart5a6f1332011-03-11 16:05:35 -050015649 case 512:
15650 bf_set(lpfc_rq_context_rqe_count,
15651 &rq_create->u.request.context,
15652 LPFC_RQ_RING_SIZE_512);
15653 break;
15654 case 1024:
15655 bf_set(lpfc_rq_context_rqe_count,
15656 &rq_create->u.request.context,
15657 LPFC_RQ_RING_SIZE_1024);
15658 break;
15659 case 2048:
15660 bf_set(lpfc_rq_context_rqe_count,
15661 &rq_create->u.request.context,
15662 LPFC_RQ_RING_SIZE_2048);
15663 break;
15664 case 4096:
15665 bf_set(lpfc_rq_context_rqe_count,
15666 &rq_create->u.request.context,
15667 LPFC_RQ_RING_SIZE_4096);
15668 break;
15669 }
15670 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15671 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015672 }
15673 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15674 cq->queue_id);
15675 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15676 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015677 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015678 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015679 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15680 putPaddrLow(dmabuf->phys);
15681 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15682 putPaddrHigh(dmabuf->phys);
15683 }
James Smart962bc512013-01-03 15:44:00 -050015684 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15685 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15686
James Smart4f774512009-05-22 14:52:35 -040015687 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15688 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015689 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15690 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15691 if (shdr_status || shdr_add_status || rc) {
15692 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15693 "2504 RQ_CREATE mailbox failed with "
15694 "status x%x add_status x%x, mbx status x%x\n",
15695 shdr_status, shdr_add_status, rc);
15696 status = -ENXIO;
15697 goto out;
15698 }
15699 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15700 if (hrq->queue_id == 0xFFFF) {
15701 status = -ENXIO;
15702 goto out;
15703 }
James Smart962bc512013-01-03 15:44:00 -050015704
15705 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15706 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15707 &rq_create->u.response);
15708 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15709 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15710 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15711 "3262 RQ [%d] doorbell format not "
15712 "supported: x%x\n", hrq->queue_id,
15713 hrq->db_format);
15714 status = -EINVAL;
15715 goto out;
15716 }
15717
15718 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15719 &rq_create->u.response);
15720 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15721 if (!bar_memmap_p) {
15722 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15723 "3269 RQ[%d] failed to memmap pci "
15724 "barset:x%x\n", hrq->queue_id,
15725 pci_barset);
15726 status = -ENOMEM;
15727 goto out;
15728 }
15729
15730 db_offset = rq_create->u.response.doorbell_offset;
15731 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15732 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15733 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15734 "3270 RQ[%d] doorbell offset not "
15735 "supported: x%x\n", hrq->queue_id,
15736 db_offset);
15737 status = -EINVAL;
15738 goto out;
15739 }
15740 hrq->db_regaddr = bar_memmap_p + db_offset;
15741 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015742 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15743 "format:x%x\n", hrq->queue_id, pci_barset,
15744 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015745 } else {
15746 hrq->db_format = LPFC_DB_RING_FORMAT;
15747 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15748 }
James Smart4f774512009-05-22 14:52:35 -040015749 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015750 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015751 hrq->subtype = subtype;
15752 hrq->host_index = 0;
15753 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015754 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040015755
15756 /* now create the data queue */
15757 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15758 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15759 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015760 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15761 phba->sli4_hba.pc_sli4_params.rqv);
15762 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15763 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015764 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015765 if (subtype == LPFC_NVMET)
15766 rq_create->u.request.context.buffer_size =
15767 LPFC_NVMET_DATA_BUF_SIZE;
15768 else
15769 rq_create->u.request.context.buffer_size =
15770 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015771 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15772 LPFC_RQE_SIZE_8);
15773 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15774 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015775 } else {
15776 switch (drq->entry_count) {
15777 default:
15778 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15779 "2536 Unsupported RQ count. (%d)\n",
15780 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015781 if (drq->entry_count < 512) {
15782 status = -EINVAL;
15783 goto out;
15784 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015785 /* fall through - otherwise default to smallest count */
James Smart5a6f1332011-03-11 16:05:35 -050015786 case 512:
15787 bf_set(lpfc_rq_context_rqe_count,
15788 &rq_create->u.request.context,
15789 LPFC_RQ_RING_SIZE_512);
15790 break;
15791 case 1024:
15792 bf_set(lpfc_rq_context_rqe_count,
15793 &rq_create->u.request.context,
15794 LPFC_RQ_RING_SIZE_1024);
15795 break;
15796 case 2048:
15797 bf_set(lpfc_rq_context_rqe_count,
15798 &rq_create->u.request.context,
15799 LPFC_RQ_RING_SIZE_2048);
15800 break;
15801 case 4096:
15802 bf_set(lpfc_rq_context_rqe_count,
15803 &rq_create->u.request.context,
15804 LPFC_RQ_RING_SIZE_4096);
15805 break;
15806 }
James Smart3c603be2017-05-15 15:20:44 -070015807 if (subtype == LPFC_NVMET)
15808 bf_set(lpfc_rq_context_buf_size,
15809 &rq_create->u.request.context,
15810 LPFC_NVMET_DATA_BUF_SIZE);
15811 else
15812 bf_set(lpfc_rq_context_buf_size,
15813 &rq_create->u.request.context,
15814 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015815 }
15816 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15817 cq->queue_id);
15818 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15819 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015820 list_for_each_entry(dmabuf, &drq->page_list, list) {
15821 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15822 putPaddrLow(dmabuf->phys);
15823 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15824 putPaddrHigh(dmabuf->phys);
15825 }
James Smart962bc512013-01-03 15:44:00 -050015826 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15827 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015828 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15829 /* The IOCTL status is embedded in the mailbox subheader. */
15830 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15831 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15832 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15833 if (shdr_status || shdr_add_status || rc) {
15834 status = -ENXIO;
15835 goto out;
15836 }
15837 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15838 if (drq->queue_id == 0xFFFF) {
15839 status = -ENXIO;
15840 goto out;
15841 }
15842 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015843 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015844 drq->subtype = subtype;
15845 drq->host_index = 0;
15846 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015847 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040015848
15849 /* link the header and data RQs onto the parent cq child list */
15850 list_add_tail(&hrq->list, &cq->child_list);
15851 list_add_tail(&drq->list, &cq->child_list);
15852
15853out:
James Smart8fa38512009-07-19 10:01:03 -040015854 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015855 return status;
15856}
15857
15858/**
James Smart2d7dbc42017-02-12 13:52:35 -080015859 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15860 * @phba: HBA structure that indicates port to create a queue on.
15861 * @hrqp: The queue structure array to use to create the header receive queues.
15862 * @drqp: The queue structure array to use to create the data receive queues.
15863 * @cqp: The completion queue array to bind these receive queues to.
15864 *
15865 * This function creates a receive buffer queue pair , as detailed in @hrq and
15866 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15867 * to the HBA.
15868 *
15869 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15870 * struct is used to get the entry count that is necessary to determine the
15871 * number of pages to use for this queue. The @cq is used to indicate which
15872 * completion queue to bind received buffers that are posted to these queues to.
15873 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15874 * receive queue pair. This function is asynchronous and will wait for the
15875 * mailbox command to finish before continuing.
15876 *
15877 * On success this function will return a zero. If unable to allocate enough
15878 * memory this function will return -ENOMEM. If the queue create mailbox command
15879 * fails this function will return -ENXIO.
15880 **/
15881int
15882lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15883 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15884 uint32_t subtype)
15885{
15886 struct lpfc_queue *hrq, *drq, *cq;
15887 struct lpfc_mbx_rq_create_v2 *rq_create;
15888 struct lpfc_dmabuf *dmabuf;
15889 LPFC_MBOXQ_t *mbox;
15890 int rc, length, alloclen, status = 0;
15891 int cnt, idx, numrq, page_idx = 0;
15892 uint32_t shdr_status, shdr_add_status;
15893 union lpfc_sli4_cfg_shdr *shdr;
15894 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15895
15896 numrq = phba->cfg_nvmet_mrq;
15897 /* sanity check on array memory */
15898 if (!hrqp || !drqp || !cqp || !numrq)
15899 return -ENODEV;
15900 if (!phba->sli4_hba.pc_sli4_params.supported)
15901 hw_page_size = SLI4_PAGE_SIZE;
15902
15903 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15904 if (!mbox)
15905 return -ENOMEM;
15906
15907 length = sizeof(struct lpfc_mbx_rq_create_v2);
15908 length += ((2 * numrq * hrqp[0]->page_count) *
15909 sizeof(struct dma_address));
15910
15911 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15912 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15913 LPFC_SLI4_MBX_NEMBED);
15914 if (alloclen < length) {
15915 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15916 "3099 Allocated DMA memory size (%d) is "
15917 "less than the requested DMA memory size "
15918 "(%d)\n", alloclen, length);
15919 status = -ENOMEM;
15920 goto out;
15921 }
15922
15923
15924
15925 rq_create = mbox->sge_array->addr[0];
15926 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15927
15928 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15929 cnt = 0;
15930
15931 for (idx = 0; idx < numrq; idx++) {
15932 hrq = hrqp[idx];
15933 drq = drqp[idx];
15934 cq = cqp[idx];
15935
James Smart2d7dbc42017-02-12 13:52:35 -080015936 /* sanity check on queue memory */
15937 if (!hrq || !drq || !cq) {
15938 status = -ENODEV;
15939 goto out;
15940 }
15941
James Smart7aabe842017-03-04 09:30:22 -080015942 if (hrq->entry_count != drq->entry_count) {
15943 status = -EINVAL;
15944 goto out;
15945 }
15946
James Smart2d7dbc42017-02-12 13:52:35 -080015947 if (idx == 0) {
15948 bf_set(lpfc_mbx_rq_create_num_pages,
15949 &rq_create->u.request,
15950 hrq->page_count);
15951 bf_set(lpfc_mbx_rq_create_rq_cnt,
15952 &rq_create->u.request, (numrq * 2));
15953 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15954 1);
15955 bf_set(lpfc_rq_context_base_cq,
15956 &rq_create->u.request.context,
15957 cq->queue_id);
15958 bf_set(lpfc_rq_context_data_size,
15959 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015960 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015961 bf_set(lpfc_rq_context_hdr_size,
15962 &rq_create->u.request.context,
15963 LPFC_HDR_BUF_SIZE);
15964 bf_set(lpfc_rq_context_rqe_count_1,
15965 &rq_create->u.request.context,
15966 hrq->entry_count);
15967 bf_set(lpfc_rq_context_rqe_size,
15968 &rq_create->u.request.context,
15969 LPFC_RQE_SIZE_8);
15970 bf_set(lpfc_rq_context_page_size,
15971 &rq_create->u.request.context,
15972 (PAGE_SIZE/SLI4_PAGE_SIZE));
15973 }
15974 rc = 0;
15975 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15976 memset(dmabuf->virt, 0, hw_page_size);
15977 cnt = page_idx + dmabuf->buffer_tag;
15978 rq_create->u.request.page[cnt].addr_lo =
15979 putPaddrLow(dmabuf->phys);
15980 rq_create->u.request.page[cnt].addr_hi =
15981 putPaddrHigh(dmabuf->phys);
15982 rc++;
15983 }
15984 page_idx += rc;
15985
15986 rc = 0;
15987 list_for_each_entry(dmabuf, &drq->page_list, list) {
15988 memset(dmabuf->virt, 0, hw_page_size);
15989 cnt = page_idx + dmabuf->buffer_tag;
15990 rq_create->u.request.page[cnt].addr_lo =
15991 putPaddrLow(dmabuf->phys);
15992 rq_create->u.request.page[cnt].addr_hi =
15993 putPaddrHigh(dmabuf->phys);
15994 rc++;
15995 }
15996 page_idx += rc;
15997
15998 hrq->db_format = LPFC_DB_RING_FORMAT;
15999 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16000 hrq->type = LPFC_HRQ;
16001 hrq->assoc_qid = cq->queue_id;
16002 hrq->subtype = subtype;
16003 hrq->host_index = 0;
16004 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016005 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080016006
16007 drq->db_format = LPFC_DB_RING_FORMAT;
16008 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16009 drq->type = LPFC_DRQ;
16010 drq->assoc_qid = cq->queue_id;
16011 drq->subtype = subtype;
16012 drq->host_index = 0;
16013 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016014 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080016015
16016 list_add_tail(&hrq->list, &cq->child_list);
16017 list_add_tail(&drq->list, &cq->child_list);
16018 }
16019
16020 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16021 /* The IOCTL status is embedded in the mailbox subheader. */
16022 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16023 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16024 if (shdr_status || shdr_add_status || rc) {
16025 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16026 "3120 RQ_CREATE mailbox failed with "
16027 "status x%x add_status x%x, mbx status x%x\n",
16028 shdr_status, shdr_add_status, rc);
16029 status = -ENXIO;
16030 goto out;
16031 }
16032 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16033 if (rc == 0xFFFF) {
16034 status = -ENXIO;
16035 goto out;
16036 }
16037
16038 /* Initialize all RQs with associated queue id */
16039 for (idx = 0; idx < numrq; idx++) {
16040 hrq = hrqp[idx];
16041 hrq->queue_id = rc + (2 * idx);
16042 drq = drqp[idx];
16043 drq->queue_id = rc + (2 * idx) + 1;
16044 }
16045
16046out:
16047 lpfc_sli4_mbox_cmd_free(phba, mbox);
16048 return status;
16049}
16050
16051/**
James Smart4f774512009-05-22 14:52:35 -040016052 * lpfc_eq_destroy - Destroy an event Queue on the HBA
16053 * @eq: The queue structure associated with the queue to destroy.
16054 *
16055 * This function destroys a queue, as detailed in @eq by sending an mailbox
16056 * command, specific to the type of queue, to the HBA.
16057 *
16058 * The @eq struct is used to get the queue ID of the queue to destroy.
16059 *
16060 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016061 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016062 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016063int
James Smart4f774512009-05-22 14:52:35 -040016064lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
16065{
16066 LPFC_MBOXQ_t *mbox;
16067 int rc, length, status = 0;
16068 uint32_t shdr_status, shdr_add_status;
16069 union lpfc_sli4_cfg_shdr *shdr;
16070
James Smart2e90f4b2011-12-13 13:22:37 -050016071 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016072 if (!eq)
16073 return -ENODEV;
James Smart32517fc2019-01-28 11:14:33 -080016074
James Smart4f774512009-05-22 14:52:35 -040016075 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
16076 if (!mbox)
16077 return -ENOMEM;
16078 length = (sizeof(struct lpfc_mbx_eq_destroy) -
16079 sizeof(struct lpfc_sli4_cfg_mhdr));
16080 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16081 LPFC_MBOX_OPCODE_EQ_DESTROY,
16082 length, LPFC_SLI4_MBX_EMBED);
16083 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
16084 eq->queue_id);
16085 mbox->vport = eq->phba->pport;
16086 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16087
16088 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
16089 /* The IOCTL status is embedded in the mailbox subheader. */
16090 shdr = (union lpfc_sli4_cfg_shdr *)
16091 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
16092 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16093 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16094 if (shdr_status || shdr_add_status || rc) {
16095 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16096 "2505 EQ_DESTROY mailbox failed with "
16097 "status x%x add_status x%x, mbx status x%x\n",
16098 shdr_status, shdr_add_status, rc);
16099 status = -ENXIO;
16100 }
16101
16102 /* Remove eq from any list */
16103 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040016104 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016105 return status;
16106}
16107
16108/**
16109 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
16110 * @cq: The queue structure associated with the queue to destroy.
16111 *
16112 * This function destroys a queue, as detailed in @cq by sending an mailbox
16113 * command, specific to the type of queue, to the HBA.
16114 *
16115 * The @cq struct is used to get the queue ID of the queue to destroy.
16116 *
16117 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016118 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016119 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016120int
James Smart4f774512009-05-22 14:52:35 -040016121lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
16122{
16123 LPFC_MBOXQ_t *mbox;
16124 int rc, length, status = 0;
16125 uint32_t shdr_status, shdr_add_status;
16126 union lpfc_sli4_cfg_shdr *shdr;
16127
James Smart2e90f4b2011-12-13 13:22:37 -050016128 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016129 if (!cq)
16130 return -ENODEV;
16131 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
16132 if (!mbox)
16133 return -ENOMEM;
16134 length = (sizeof(struct lpfc_mbx_cq_destroy) -
16135 sizeof(struct lpfc_sli4_cfg_mhdr));
16136 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16137 LPFC_MBOX_OPCODE_CQ_DESTROY,
16138 length, LPFC_SLI4_MBX_EMBED);
16139 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
16140 cq->queue_id);
16141 mbox->vport = cq->phba->pport;
16142 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16143 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
16144 /* The IOCTL status is embedded in the mailbox subheader. */
16145 shdr = (union lpfc_sli4_cfg_shdr *)
16146 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
16147 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16148 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16149 if (shdr_status || shdr_add_status || rc) {
16150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16151 "2506 CQ_DESTROY mailbox failed with "
16152 "status x%x add_status x%x, mbx status x%x\n",
16153 shdr_status, shdr_add_status, rc);
16154 status = -ENXIO;
16155 }
16156 /* Remove cq from any list */
16157 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040016158 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016159 return status;
16160}
16161
16162/**
James Smart04c68492009-05-22 14:52:52 -040016163 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
16164 * @qm: The queue structure associated with the queue to destroy.
16165 *
16166 * This function destroys a queue, as detailed in @mq by sending an mailbox
16167 * command, specific to the type of queue, to the HBA.
16168 *
16169 * The @mq struct is used to get the queue ID of the queue to destroy.
16170 *
16171 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016172 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040016173 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016174int
James Smart04c68492009-05-22 14:52:52 -040016175lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16176{
16177 LPFC_MBOXQ_t *mbox;
16178 int rc, length, status = 0;
16179 uint32_t shdr_status, shdr_add_status;
16180 union lpfc_sli4_cfg_shdr *shdr;
16181
James Smart2e90f4b2011-12-13 13:22:37 -050016182 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040016183 if (!mq)
16184 return -ENODEV;
16185 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16186 if (!mbox)
16187 return -ENOMEM;
16188 length = (sizeof(struct lpfc_mbx_mq_destroy) -
16189 sizeof(struct lpfc_sli4_cfg_mhdr));
16190 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16191 LPFC_MBOX_OPCODE_MQ_DESTROY,
16192 length, LPFC_SLI4_MBX_EMBED);
16193 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16194 mq->queue_id);
16195 mbox->vport = mq->phba->pport;
16196 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16197 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16198 /* The IOCTL status is embedded in the mailbox subheader. */
16199 shdr = (union lpfc_sli4_cfg_shdr *)
16200 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16201 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16202 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16203 if (shdr_status || shdr_add_status || rc) {
16204 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16205 "2507 MQ_DESTROY mailbox failed with "
16206 "status x%x add_status x%x, mbx status x%x\n",
16207 shdr_status, shdr_add_status, rc);
16208 status = -ENXIO;
16209 }
16210 /* Remove mq from any list */
16211 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040016212 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040016213 return status;
16214}
16215
16216/**
James Smart4f774512009-05-22 14:52:35 -040016217 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16218 * @wq: The queue structure associated with the queue to destroy.
16219 *
16220 * This function destroys a queue, as detailed in @wq by sending an mailbox
16221 * command, specific to the type of queue, to the HBA.
16222 *
16223 * The @wq struct is used to get the queue ID of the queue to destroy.
16224 *
16225 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016226 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016227 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016228int
James Smart4f774512009-05-22 14:52:35 -040016229lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16230{
16231 LPFC_MBOXQ_t *mbox;
16232 int rc, length, status = 0;
16233 uint32_t shdr_status, shdr_add_status;
16234 union lpfc_sli4_cfg_shdr *shdr;
16235
James Smart2e90f4b2011-12-13 13:22:37 -050016236 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016237 if (!wq)
16238 return -ENODEV;
16239 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16240 if (!mbox)
16241 return -ENOMEM;
16242 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16243 sizeof(struct lpfc_sli4_cfg_mhdr));
16244 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16245 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16246 length, LPFC_SLI4_MBX_EMBED);
16247 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16248 wq->queue_id);
16249 mbox->vport = wq->phba->pport;
16250 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16251 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16252 shdr = (union lpfc_sli4_cfg_shdr *)
16253 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16254 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16255 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16256 if (shdr_status || shdr_add_status || rc) {
16257 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16258 "2508 WQ_DESTROY mailbox failed with "
16259 "status x%x add_status x%x, mbx status x%x\n",
16260 shdr_status, shdr_add_status, rc);
16261 status = -ENXIO;
16262 }
16263 /* Remove wq from any list */
16264 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070016265 kfree(wq->pring);
16266 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040016267 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016268 return status;
16269}
16270
16271/**
16272 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16273 * @rq: The queue structure associated with the queue to destroy.
16274 *
16275 * This function destroys a queue, as detailed in @rq by sending an mailbox
16276 * command, specific to the type of queue, to the HBA.
16277 *
16278 * The @rq struct is used to get the queue ID of the queue to destroy.
16279 *
16280 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016281 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016282 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016283int
James Smart4f774512009-05-22 14:52:35 -040016284lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16285 struct lpfc_queue *drq)
16286{
16287 LPFC_MBOXQ_t *mbox;
16288 int rc, length, status = 0;
16289 uint32_t shdr_status, shdr_add_status;
16290 union lpfc_sli4_cfg_shdr *shdr;
16291
James Smart2e90f4b2011-12-13 13:22:37 -050016292 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016293 if (!hrq || !drq)
16294 return -ENODEV;
16295 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16296 if (!mbox)
16297 return -ENOMEM;
16298 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050016299 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040016300 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16301 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16302 length, LPFC_SLI4_MBX_EMBED);
16303 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16304 hrq->queue_id);
16305 mbox->vport = hrq->phba->pport;
16306 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16307 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16308 /* The IOCTL status is embedded in the mailbox subheader. */
16309 shdr = (union lpfc_sli4_cfg_shdr *)
16310 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16311 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16312 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16313 if (shdr_status || shdr_add_status || rc) {
16314 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16315 "2509 RQ_DESTROY mailbox failed with "
16316 "status x%x add_status x%x, mbx status x%x\n",
16317 shdr_status, shdr_add_status, rc);
16318 if (rc != MBX_TIMEOUT)
16319 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16320 return -ENXIO;
16321 }
16322 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16323 drq->queue_id);
16324 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16325 shdr = (union lpfc_sli4_cfg_shdr *)
16326 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16327 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16328 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16329 if (shdr_status || shdr_add_status || rc) {
16330 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16331 "2510 RQ_DESTROY mailbox failed with "
16332 "status x%x add_status x%x, mbx status x%x\n",
16333 shdr_status, shdr_add_status, rc);
16334 status = -ENXIO;
16335 }
16336 list_del_init(&hrq->list);
16337 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040016338 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016339 return status;
16340}
16341
16342/**
16343 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16344 * @phba: The virtual port for which this call being executed.
16345 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16346 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16347 * @xritag: the xritag that ties this io to the SGL pages.
16348 *
16349 * This routine will post the sgl pages for the IO that has the xritag
16350 * that is in the iocbq structure. The xritag is assigned during iocbq
16351 * creation and persists for as long as the driver is loaded.
16352 * if the caller has fewer than 256 scatter gather segments to map then
16353 * pdma_phys_addr1 should be 0.
16354 * If the caller needs to map more than 256 scatter gather segment then
16355 * pdma_phys_addr1 should be a valid physical address.
16356 * physical address for SGLs must be 64 byte aligned.
16357 * If you are going to map 2 SGL's then the first one must have 256 entries
16358 * the second sgl can have between 1 and 256 entries.
16359 *
16360 * Return codes:
16361 * 0 - Success
16362 * -ENXIO, -ENOMEM - Failure
16363 **/
16364int
16365lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16366 dma_addr_t pdma_phys_addr0,
16367 dma_addr_t pdma_phys_addr1,
16368 uint16_t xritag)
16369{
16370 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16371 LPFC_MBOXQ_t *mbox;
16372 int rc;
16373 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040016374 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040016375 union lpfc_sli4_cfg_shdr *shdr;
16376
16377 if (xritag == NO_XRI) {
16378 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16379 "0364 Invalid param:\n");
16380 return -EINVAL;
16381 }
16382
16383 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16384 if (!mbox)
16385 return -ENOMEM;
16386
16387 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16388 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16389 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050016390 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040016391
16392 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16393 &mbox->u.mqe.un.post_sgl_pages;
16394 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16395 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16396
16397 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16398 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16399 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16400 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16401
16402 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16403 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16404 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16405 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16406 if (!phba->sli4_hba.intr_enable)
16407 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040016408 else {
James Smarta183a152011-10-10 21:32:43 -040016409 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040016410 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16411 }
James Smart4f774512009-05-22 14:52:35 -040016412 /* The IOCTL status is embedded in the mailbox subheader. */
16413 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16414 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16415 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16416 if (rc != MBX_TIMEOUT)
16417 mempool_free(mbox, phba->mbox_mem_pool);
16418 if (shdr_status || shdr_add_status || rc) {
16419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16420 "2511 POST_SGL mailbox failed with "
16421 "status x%x add_status x%x, mbx status x%x\n",
16422 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040016423 }
16424 return 0;
16425}
James Smart4f774512009-05-22 14:52:35 -040016426
16427/**
James Smart88a2cfb2011-07-22 18:36:33 -040016428 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040016429 * @phba: pointer to lpfc hba data structure.
16430 *
16431 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040016432 * HBA consistent with the SLI-4 interface spec. This routine
16433 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16434 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040016435 *
James Smart88a2cfb2011-07-22 18:36:33 -040016436 * Returns
16437 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16438 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16439 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016440static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016441lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16442{
16443 unsigned long xri;
16444
16445 /*
16446 * Fetch the next logical xri. Because this index is logical,
16447 * the driver starts at 0 each time.
16448 */
16449 spin_lock_irq(&phba->hbalock);
16450 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16451 phba->sli4_hba.max_cfg_param.max_xri, 0);
16452 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16453 spin_unlock_irq(&phba->hbalock);
16454 return NO_XRI;
16455 } else {
16456 set_bit(xri, phba->sli4_hba.xri_bmask);
16457 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040016458 }
James Smart6d368e52011-05-24 11:44:12 -040016459 spin_unlock_irq(&phba->hbalock);
16460 return xri;
16461}
16462
16463/**
16464 * lpfc_sli4_free_xri - Release an xri for reuse.
16465 * @phba: pointer to lpfc hba data structure.
16466 *
16467 * This routine is invoked to release an xri to the pool of
16468 * available rpis maintained by the driver.
16469 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016470static void
James Smart6d368e52011-05-24 11:44:12 -040016471__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16472{
16473 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016474 phba->sli4_hba.max_cfg_param.xri_used--;
16475 }
16476}
16477
16478/**
16479 * lpfc_sli4_free_xri - Release an xri for reuse.
16480 * @phba: pointer to lpfc hba data structure.
16481 *
16482 * This routine is invoked to release an xri to the pool of
16483 * available rpis maintained by the driver.
16484 **/
16485void
16486lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16487{
16488 spin_lock_irq(&phba->hbalock);
16489 __lpfc_sli4_free_xri(phba, xri);
16490 spin_unlock_irq(&phba->hbalock);
16491}
16492
16493/**
James Smart4f774512009-05-22 14:52:35 -040016494 * lpfc_sli4_next_xritag - Get an xritag for the io
16495 * @phba: Pointer to HBA context object.
16496 *
16497 * This function gets an xritag for the iocb. If there is no unused xritag
16498 * it will return 0xffff.
16499 * The function returns the allocated xritag if successful, else returns zero.
16500 * Zero is not a valid xritag.
16501 * The caller is not required to hold any lock.
16502 **/
16503uint16_t
16504lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16505{
James Smart6d368e52011-05-24 11:44:12 -040016506 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016507
James Smart6d368e52011-05-24 11:44:12 -040016508 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016509 if (xri_index == NO_XRI)
16510 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16511 "2004 Failed to allocate XRI.last XRITAG is %d"
16512 " Max XRI is %d, Used XRI is %d\n",
16513 xri_index,
16514 phba->sli4_hba.max_cfg_param.max_xri,
16515 phba->sli4_hba.max_cfg_param.xri_used);
16516 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016517}
16518
16519/**
James Smart895427b2017-02-12 13:52:30 -080016520 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016521 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016522 * @post_sgl_list: pointer to els sgl entry list.
16523 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016524 *
16525 * This routine is invoked to post a block of driver's sgl pages to the
16526 * HBA using non-embedded mailbox command. No Lock is held. This routine
16527 * is only called when the driver is loading and after all IO has been
16528 * stopped.
16529 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016530static int
James Smart895427b2017-02-12 13:52:30 -080016531lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016532 struct list_head *post_sgl_list,
16533 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016534{
James Smart8a9d2e82012-05-09 21:16:12 -040016535 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016536 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16537 struct sgl_page_pairs *sgl_pg_pairs;
16538 void *viraddr;
16539 LPFC_MBOXQ_t *mbox;
16540 uint32_t reqlen, alloclen, pg_pairs;
16541 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016542 uint16_t xritag_start = 0;
16543 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016544 uint32_t shdr_status, shdr_add_status;
16545 union lpfc_sli4_cfg_shdr *shdr;
16546
James Smart895427b2017-02-12 13:52:30 -080016547 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016548 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016549 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016550 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016551 "2559 Block sgl registration required DMA "
16552 "size (%d) great than a page\n", reqlen);
16553 return -ENOMEM;
16554 }
James Smart895427b2017-02-12 13:52:30 -080016555
James Smart4f774512009-05-22 14:52:35 -040016556 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016557 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016558 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016559
16560 /* Allocate DMA memory and set up the non-embedded mailbox command */
16561 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16562 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16563 LPFC_SLI4_MBX_NEMBED);
16564
16565 if (alloclen < reqlen) {
16566 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16567 "0285 Allocated DMA memory size (%d) is "
16568 "less than the requested DMA memory "
16569 "size (%d)\n", alloclen, reqlen);
16570 lpfc_sli4_mbox_cmd_free(phba, mbox);
16571 return -ENOMEM;
16572 }
James Smart4f774512009-05-22 14:52:35 -040016573 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016574 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016575 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16576 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16577
James Smart8a9d2e82012-05-09 21:16:12 -040016578 pg_pairs = 0;
16579 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016580 /* Set up the sge entry */
16581 sgl_pg_pairs->sgl_pg0_addr_lo =
16582 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16583 sgl_pg_pairs->sgl_pg0_addr_hi =
16584 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16585 sgl_pg_pairs->sgl_pg1_addr_lo =
16586 cpu_to_le32(putPaddrLow(0));
16587 sgl_pg_pairs->sgl_pg1_addr_hi =
16588 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016589
James Smart4f774512009-05-22 14:52:35 -040016590 /* Keep the first xritag on the list */
16591 if (pg_pairs == 0)
16592 xritag_start = sglq_entry->sli4_xritag;
16593 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016594 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016595 }
James Smart6d368e52011-05-24 11:44:12 -040016596
16597 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016598 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016599 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016600 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016601
James Smart4f774512009-05-22 14:52:35 -040016602 if (!phba->sli4_hba.intr_enable)
16603 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16604 else {
James Smarta183a152011-10-10 21:32:43 -040016605 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016606 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16607 }
16608 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16609 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16610 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16611 if (rc != MBX_TIMEOUT)
16612 lpfc_sli4_mbox_cmd_free(phba, mbox);
16613 if (shdr_status || shdr_add_status || rc) {
16614 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16615 "2513 POST_SGL_BLOCK mailbox command failed "
16616 "status x%x add_status x%x mbx status x%x\n",
16617 shdr_status, shdr_add_status, rc);
16618 rc = -ENXIO;
16619 }
16620 return rc;
16621}
16622
16623/**
James Smart5e5b5112019-01-28 11:14:22 -080016624 * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
James Smart4f774512009-05-22 14:52:35 -040016625 * @phba: pointer to lpfc hba data structure.
James Smart0794d602019-01-28 11:14:19 -080016626 * @nblist: pointer to nvme buffer list.
James Smart4f774512009-05-22 14:52:35 -040016627 * @count: number of scsi buffers on the list.
16628 *
16629 * This routine is invoked to post a block of @count scsi sgl pages from a
James Smart0794d602019-01-28 11:14:19 -080016630 * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
James Smart4f774512009-05-22 14:52:35 -040016631 * No Lock is held.
16632 *
16633 **/
James Smart0794d602019-01-28 11:14:19 -080016634static int
James Smart5e5b5112019-01-28 11:14:22 -080016635lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
16636 int count)
James Smart4f774512009-05-22 14:52:35 -040016637{
James Smartc4908502019-01-28 11:14:28 -080016638 struct lpfc_io_buf *lpfc_ncmd;
James Smart4f774512009-05-22 14:52:35 -040016639 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16640 struct sgl_page_pairs *sgl_pg_pairs;
16641 void *viraddr;
16642 LPFC_MBOXQ_t *mbox;
16643 uint32_t reqlen, alloclen, pg_pairs;
16644 uint32_t mbox_tmo;
16645 uint16_t xritag_start = 0;
16646 int rc = 0;
16647 uint32_t shdr_status, shdr_add_status;
16648 dma_addr_t pdma_phys_bpl1;
16649 union lpfc_sli4_cfg_shdr *shdr;
16650
16651 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016652 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016653 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016654 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016655 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016656 "6118 Block sgl registration required DMA "
James Smart4f774512009-05-22 14:52:35 -040016657 "size (%d) great than a page\n", reqlen);
16658 return -ENOMEM;
16659 }
16660 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16661 if (!mbox) {
16662 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016663 "6119 Failed to allocate mbox cmd memory\n");
James Smart4f774512009-05-22 14:52:35 -040016664 return -ENOMEM;
16665 }
16666
16667 /* Allocate DMA memory and set up the non-embedded mailbox command */
16668 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
James Smart0794d602019-01-28 11:14:19 -080016669 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16670 reqlen, LPFC_SLI4_MBX_NEMBED);
James Smart4f774512009-05-22 14:52:35 -040016671
16672 if (alloclen < reqlen) {
16673 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016674 "6120 Allocated DMA memory size (%d) is "
James Smart4f774512009-05-22 14:52:35 -040016675 "less than the requested DMA memory "
16676 "size (%d)\n", alloclen, reqlen);
16677 lpfc_sli4_mbox_cmd_free(phba, mbox);
16678 return -ENOMEM;
16679 }
James Smart6d368e52011-05-24 11:44:12 -040016680
James Smart4f774512009-05-22 14:52:35 -040016681 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016682 viraddr = mbox->sge_array->addr[0];
16683
16684 /* Set up the SGL pages in the non-embedded DMA pages */
16685 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16686 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16687
16688 pg_pairs = 0;
James Smart0794d602019-01-28 11:14:19 -080016689 list_for_each_entry(lpfc_ncmd, nblist, list) {
James Smart4f774512009-05-22 14:52:35 -040016690 /* Set up the sge entry */
16691 sgl_pg_pairs->sgl_pg0_addr_lo =
James Smart0794d602019-01-28 11:14:19 -080016692 cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040016693 sgl_pg_pairs->sgl_pg0_addr_hi =
James Smart0794d602019-01-28 11:14:19 -080016694 cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040016695 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
James Smart0794d602019-01-28 11:14:19 -080016696 pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
16697 SGL_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040016698 else
16699 pdma_phys_bpl1 = 0;
16700 sgl_pg_pairs->sgl_pg1_addr_lo =
16701 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16702 sgl_pg_pairs->sgl_pg1_addr_hi =
16703 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16704 /* Keep the first xritag on the list */
16705 if (pg_pairs == 0)
James Smart0794d602019-01-28 11:14:19 -080016706 xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -040016707 sgl_pg_pairs++;
16708 pg_pairs++;
16709 }
16710 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16711 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16712 /* Perform endian conversion if necessary */
16713 sgl->word0 = cpu_to_le32(sgl->word0);
16714
James Smart0794d602019-01-28 11:14:19 -080016715 if (!phba->sli4_hba.intr_enable) {
James Smart4f774512009-05-22 14:52:35 -040016716 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart0794d602019-01-28 11:14:19 -080016717 } else {
James Smarta183a152011-10-10 21:32:43 -040016718 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016719 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16720 }
James Smart0794d602019-01-28 11:14:19 -080016721 shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040016722 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16723 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16724 if (rc != MBX_TIMEOUT)
16725 lpfc_sli4_mbox_cmd_free(phba, mbox);
16726 if (shdr_status || shdr_add_status || rc) {
16727 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -080016728 "6125 POST_SGL_BLOCK mailbox command failed "
James Smart4f774512009-05-22 14:52:35 -040016729 "status x%x add_status x%x mbx status x%x\n",
16730 shdr_status, shdr_add_status, rc);
16731 rc = -ENXIO;
16732 }
16733 return rc;
16734}
16735
16736/**
James Smart5e5b5112019-01-28 11:14:22 -080016737 * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
James Smart0794d602019-01-28 11:14:19 -080016738 * @phba: pointer to lpfc hba data structure.
16739 * @post_nblist: pointer to the nvme buffer list.
16740 *
16741 * This routine walks a list of nvme buffers that was passed in. It attempts
16742 * to construct blocks of nvme buffer sgls which contains contiguous xris and
16743 * uses the non-embedded SGL block post mailbox commands to post to the port.
16744 * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
16745 * embedded SGL post mailbox command for posting. The @post_nblist passed in
16746 * must be local list, thus no lock is needed when manipulate the list.
16747 *
16748 * Returns: 0 = failure, non-zero number of successfully posted buffers.
16749 **/
16750int
James Smart5e5b5112019-01-28 11:14:22 -080016751lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
16752 struct list_head *post_nblist, int sb_count)
James Smart0794d602019-01-28 11:14:19 -080016753{
James Smartc4908502019-01-28 11:14:28 -080016754 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart0794d602019-01-28 11:14:19 -080016755 int status, sgl_size;
16756 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
16757 dma_addr_t pdma_phys_sgl1;
16758 int last_xritag = NO_XRI;
16759 int cur_xritag;
James Smart0794d602019-01-28 11:14:19 -080016760 LIST_HEAD(prep_nblist);
16761 LIST_HEAD(blck_nblist);
16762 LIST_HEAD(nvme_nblist);
16763
16764 /* sanity check */
16765 if (sb_count <= 0)
16766 return -EINVAL;
16767
16768 sgl_size = phba->cfg_sg_dma_buf_size;
16769 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
16770 list_del_init(&lpfc_ncmd->list);
16771 block_cnt++;
16772 if ((last_xritag != NO_XRI) &&
16773 (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
16774 /* a hole in xri block, form a sgl posting block */
16775 list_splice_init(&prep_nblist, &blck_nblist);
16776 post_cnt = block_cnt - 1;
16777 /* prepare list for next posting block */
16778 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
16779 block_cnt = 1;
16780 } else {
16781 /* prepare list for next posting block */
16782 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
16783 /* enough sgls for non-embed sgl mbox command */
16784 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
16785 list_splice_init(&prep_nblist, &blck_nblist);
16786 post_cnt = block_cnt;
16787 block_cnt = 0;
16788 }
16789 }
16790 num_posting++;
16791 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
16792
16793 /* end of repost sgl list condition for NVME buffers */
16794 if (num_posting == sb_count) {
16795 if (post_cnt == 0) {
16796 /* last sgl posting block */
16797 list_splice_init(&prep_nblist, &blck_nblist);
16798 post_cnt = block_cnt;
16799 } else if (block_cnt == 1) {
16800 /* last single sgl with non-contiguous xri */
16801 if (sgl_size > SGL_PAGE_SIZE)
16802 pdma_phys_sgl1 =
16803 lpfc_ncmd->dma_phys_sgl +
16804 SGL_PAGE_SIZE;
16805 else
16806 pdma_phys_sgl1 = 0;
16807 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
16808 status = lpfc_sli4_post_sgl(
16809 phba, lpfc_ncmd->dma_phys_sgl,
16810 pdma_phys_sgl1, cur_xritag);
16811 if (status) {
James Smartc4908502019-01-28 11:14:28 -080016812 /* Post error. Buffer unavailable. */
16813 lpfc_ncmd->flags |=
16814 LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016815 } else {
James Smartc4908502019-01-28 11:14:28 -080016816 /* Post success. Bffer available. */
16817 lpfc_ncmd->flags &=
16818 ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016819 lpfc_ncmd->status = IOSTAT_SUCCESS;
16820 num_posted++;
16821 }
16822 /* success, put on NVME buffer sgl list */
16823 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
16824 }
16825 }
16826
16827 /* continue until a nembed page worth of sgls */
16828 if (post_cnt == 0)
16829 continue;
16830
16831 /* post block of NVME buffer list sgls */
James Smart5e5b5112019-01-28 11:14:22 -080016832 status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
16833 post_cnt);
James Smart0794d602019-01-28 11:14:19 -080016834
16835 /* don't reset xirtag due to hole in xri block */
16836 if (block_cnt == 0)
16837 last_xritag = NO_XRI;
16838
16839 /* reset NVME buffer post count for next round of posting */
16840 post_cnt = 0;
16841
16842 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
16843 while (!list_empty(&blck_nblist)) {
16844 list_remove_head(&blck_nblist, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -080016845 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -080016846 if (status) {
James Smartc4908502019-01-28 11:14:28 -080016847 /* Post error. Mark buffer unavailable. */
16848 lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016849 } else {
James Smartc4908502019-01-28 11:14:28 -080016850 /* Post success, Mark buffer available. */
16851 lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016852 lpfc_ncmd->status = IOSTAT_SUCCESS;
16853 num_posted++;
16854 }
16855 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
16856 }
16857 }
16858 /* Push NVME buffers with sgl posted to the available list */
James Smart5e5b5112019-01-28 11:14:22 -080016859 lpfc_io_buf_replenish(phba, &nvme_nblist);
16860
James Smart0794d602019-01-28 11:14:19 -080016861 return num_posted;
16862}
16863
16864/**
James Smart4f774512009-05-22 14:52:35 -040016865 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16866 * @phba: pointer to lpfc_hba struct that the frame was received on
16867 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16868 *
16869 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16870 * valid type of frame that the LPFC driver will handle. This function will
16871 * return a zero if the frame is a valid frame or a non zero value when the
16872 * frame does not pass the check.
16873 **/
16874static int
16875lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16876{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016877 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016878 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016879 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016880
James Smarte62245d2019-08-14 16:57:08 -070016881#define FC_RCTL_MDS_DIAGS 0xF4
16882
James Smart4f774512009-05-22 14:52:35 -040016883 switch (fc_hdr->fh_r_ctl) {
16884 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16885 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16886 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16887 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16888 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16889 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16890 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16891 case FC_RCTL_DD_CMD_STATUS: /* command status */
16892 case FC_RCTL_ELS_REQ: /* extended link services request */
16893 case FC_RCTL_ELS_REP: /* extended link services reply */
16894 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16895 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16896 case FC_RCTL_BA_NOP: /* basic link service NOP */
16897 case FC_RCTL_BA_ABTS: /* basic link service abort */
16898 case FC_RCTL_BA_RMC: /* remove connection */
16899 case FC_RCTL_BA_ACC: /* basic accept */
16900 case FC_RCTL_BA_RJT: /* basic reject */
16901 case FC_RCTL_BA_PRMT:
16902 case FC_RCTL_ACK_1: /* acknowledge_1 */
16903 case FC_RCTL_ACK_0: /* acknowledge_0 */
16904 case FC_RCTL_P_RJT: /* port reject */
16905 case FC_RCTL_F_RJT: /* fabric reject */
16906 case FC_RCTL_P_BSY: /* port busy */
16907 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16908 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16909 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016910 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016911 case FC_RCTL_END: /* end */
16912 break;
16913 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16914 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16915 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16916 return lpfc_fc_frame_check(phba, fc_hdr);
16917 default:
16918 goto drop;
16919 }
James Smartae9e28f2017-05-15 15:20:51 -070016920
James Smart4f774512009-05-22 14:52:35 -040016921 switch (fc_hdr->fh_type) {
16922 case FC_TYPE_BLS:
16923 case FC_TYPE_ELS:
16924 case FC_TYPE_FCP:
16925 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016926 case FC_TYPE_NVME:
James Smart4f774512009-05-22 14:52:35 -040016927 break;
16928 case FC_TYPE_IP:
16929 case FC_TYPE_ILS:
16930 default:
16931 goto drop;
16932 }
James Smart546fc852011-03-11 16:06:29 -050016933
James Smart4f774512009-05-22 14:52:35 -040016934 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016935 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040016936 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070016937 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16938 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16939 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16940 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16941 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016942 return 0;
16943drop:
16944 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016945 "2539 Dropped frame rctl:x%x type:x%x\n",
16946 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040016947 return 1;
16948}
16949
16950/**
16951 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16952 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16953 *
16954 * This function processes the FC header to retrieve the VFI from the VF
16955 * header, if one exists. This function will return the VFI if one exists
16956 * or 0 if no VSAN Header exists.
16957 **/
16958static uint32_t
16959lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16960{
16961 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16962
16963 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16964 return 0;
16965 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16966}
16967
16968/**
16969 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16970 * @phba: Pointer to the HBA structure to search for the vport on
16971 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16972 * @fcfi: The FC Fabric ID that the frame came from
16973 *
16974 * This function searches the @phba for a vport that matches the content of the
16975 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16976 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16977 * returns the matching vport pointer or NULL if unable to match frame to a
16978 * vport.
16979 **/
16980static struct lpfc_vport *
16981lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016982 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016983{
16984 struct lpfc_vport **vports;
16985 struct lpfc_vport *vport = NULL;
16986 int i;
James Smart939723a2012-05-09 21:19:03 -040016987
James Smartbf086112011-08-21 21:48:13 -040016988 if (did == Fabric_DID)
16989 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016990 if ((phba->pport->fc_flag & FC_PT2PT) &&
16991 !(phba->link_state == LPFC_HBA_READY))
16992 return phba->pport;
16993
James Smart4f774512009-05-22 14:52:35 -040016994 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016995 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016996 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16997 if (phba->fcf.fcfi == fcfi &&
16998 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16999 vports[i]->fc_myDID == did) {
17000 vport = vports[i];
17001 break;
17002 }
17003 }
James Smart895427b2017-02-12 13:52:30 -080017004 }
James Smart4f774512009-05-22 14:52:35 -040017005 lpfc_destroy_vport_work_array(phba, vports);
17006 return vport;
17007}
17008
17009/**
James Smart45ed1192009-10-02 15:17:02 -040017010 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
17011 * @vport: The vport to work on.
17012 *
17013 * This function updates the receive sequence time stamp for this vport. The
17014 * receive sequence time stamp indicates the time that the last frame of the
17015 * the sequence that has been idle for the longest amount of time was received.
17016 * the driver uses this time stamp to indicate if any received sequences have
17017 * timed out.
17018 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017019static void
James Smart45ed1192009-10-02 15:17:02 -040017020lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
17021{
17022 struct lpfc_dmabuf *h_buf;
17023 struct hbq_dmabuf *dmabuf = NULL;
17024
17025 /* get the oldest sequence on the rcv list */
17026 h_buf = list_get_first(&vport->rcv_buffer_list,
17027 struct lpfc_dmabuf, list);
17028 if (!h_buf)
17029 return;
17030 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17031 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
17032}
17033
17034/**
17035 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
17036 * @vport: The vport that the received sequences were sent to.
17037 *
17038 * This function cleans up all outstanding received sequences. This is called
17039 * by the driver when a link event or user action invalidates all the received
17040 * sequences.
17041 **/
17042void
17043lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
17044{
17045 struct lpfc_dmabuf *h_buf, *hnext;
17046 struct lpfc_dmabuf *d_buf, *dnext;
17047 struct hbq_dmabuf *dmabuf = NULL;
17048
17049 /* start with the oldest sequence on the rcv list */
17050 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17051 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17052 list_del_init(&dmabuf->hbuf.list);
17053 list_for_each_entry_safe(d_buf, dnext,
17054 &dmabuf->dbuf.list, list) {
17055 list_del_init(&d_buf->list);
17056 lpfc_in_buf_free(vport->phba, d_buf);
17057 }
17058 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17059 }
17060}
17061
17062/**
17063 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
17064 * @vport: The vport that the received sequences were sent to.
17065 *
17066 * This function determines whether any received sequences have timed out by
17067 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
17068 * indicates that there is at least one timed out sequence this routine will
17069 * go through the received sequences one at a time from most inactive to most
17070 * active to determine which ones need to be cleaned up. Once it has determined
17071 * that a sequence needs to be cleaned up it will simply free up the resources
17072 * without sending an abort.
17073 **/
17074void
17075lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
17076{
17077 struct lpfc_dmabuf *h_buf, *hnext;
17078 struct lpfc_dmabuf *d_buf, *dnext;
17079 struct hbq_dmabuf *dmabuf = NULL;
17080 unsigned long timeout;
17081 int abort_count = 0;
17082
17083 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17084 vport->rcv_buffer_time_stamp);
17085 if (list_empty(&vport->rcv_buffer_list) ||
17086 time_before(jiffies, timeout))
17087 return;
17088 /* start with the oldest sequence on the rcv list */
17089 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17090 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17091 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17092 dmabuf->time_stamp);
17093 if (time_before(jiffies, timeout))
17094 break;
17095 abort_count++;
17096 list_del_init(&dmabuf->hbuf.list);
17097 list_for_each_entry_safe(d_buf, dnext,
17098 &dmabuf->dbuf.list, list) {
17099 list_del_init(&d_buf->list);
17100 lpfc_in_buf_free(vport->phba, d_buf);
17101 }
17102 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17103 }
17104 if (abort_count)
17105 lpfc_update_rcv_time_stamp(vport);
17106}
17107
17108/**
James Smart4f774512009-05-22 14:52:35 -040017109 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
17110 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
17111 *
17112 * This function searches through the existing incomplete sequences that have
17113 * been sent to this @vport. If the frame matches one of the incomplete
17114 * sequences then the dbuf in the @dmabuf is added to the list of frames that
17115 * make up that sequence. If no sequence is found that matches this frame then
17116 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
17117 * This function returns a pointer to the first dmabuf in the sequence list that
17118 * the frame was linked to.
17119 **/
17120static struct hbq_dmabuf *
17121lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17122{
17123 struct fc_frame_header *new_hdr;
17124 struct fc_frame_header *temp_hdr;
17125 struct lpfc_dmabuf *d_buf;
17126 struct lpfc_dmabuf *h_buf;
17127 struct hbq_dmabuf *seq_dmabuf = NULL;
17128 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050017129 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040017130
James Smart4d9ab992009-10-02 15:16:39 -040017131 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017132 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040017133 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050017134
James Smart4f774512009-05-22 14:52:35 -040017135 /* Use the hdr_buf to find the sequence that this frame belongs to */
17136 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17137 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17138 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17139 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17140 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17141 continue;
17142 /* found a pending sequence that matches this frame */
17143 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17144 break;
17145 }
17146 if (!seq_dmabuf) {
17147 /*
17148 * This indicates first frame received for this sequence.
17149 * Queue the buffer on the vport's rcv_buffer_list.
17150 */
17151 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040017152 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017153 return dmabuf;
17154 }
17155 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050017156 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
17157 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040017158 list_del_init(&seq_dmabuf->hbuf.list);
17159 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17160 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017161 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017162 return dmabuf;
17163 }
James Smart45ed1192009-10-02 15:17:02 -040017164 /* move this sequence to the tail to indicate a young sequence */
17165 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
17166 seq_dmabuf->time_stamp = jiffies;
17167 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050017168 if (list_empty(&seq_dmabuf->dbuf.list)) {
17169 temp_hdr = dmabuf->hbuf.virt;
17170 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17171 return seq_dmabuf;
17172 }
James Smart4f774512009-05-22 14:52:35 -040017173 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050017174 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
17175 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040017176 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17177 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
17178 /*
17179 * If the frame's sequence count is greater than the frame on
17180 * the list then insert the frame right after this frame
17181 */
James Smarteeead812009-12-21 17:01:23 -050017182 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
17183 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040017184 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050017185 found = 1;
17186 break;
James Smart4f774512009-05-22 14:52:35 -040017187 }
James Smart4360ca92015-12-16 18:12:04 -050017188
17189 if (&d_buf->list == &seq_dmabuf->dbuf.list)
17190 break;
17191 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040017192 }
James Smart4360ca92015-12-16 18:12:04 -050017193
17194 if (found)
17195 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017196 return NULL;
17197}
17198
17199/**
James Smart6669f9b2009-10-02 15:16:45 -040017200 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
17201 * @vport: pointer to a vitural port
17202 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17203 *
17204 * This function tries to abort from the partially assembed sequence, described
17205 * by the information from basic abbort @dmabuf. It checks to see whether such
17206 * partially assembled sequence held by the driver. If so, it shall free up all
17207 * the frames from the partially assembled sequence.
17208 *
17209 * Return
17210 * true -- if there is matching partially assembled sequence present and all
17211 * the frames freed with the sequence;
17212 * false -- if there is no matching partially assembled sequence present so
17213 * nothing got aborted in the lower layer driver
17214 **/
17215static bool
17216lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
17217 struct hbq_dmabuf *dmabuf)
17218{
17219 struct fc_frame_header *new_hdr;
17220 struct fc_frame_header *temp_hdr;
17221 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
17222 struct hbq_dmabuf *seq_dmabuf = NULL;
17223
17224 /* Use the hdr_buf to find the sequence that matches this frame */
17225 INIT_LIST_HEAD(&dmabuf->dbuf.list);
17226 INIT_LIST_HEAD(&dmabuf->hbuf.list);
17227 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17228 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17229 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17230 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17231 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17232 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17233 continue;
17234 /* found a pending sequence that matches this frame */
17235 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17236 break;
17237 }
17238
17239 /* Free up all the frames from the partially assembled sequence */
17240 if (seq_dmabuf) {
17241 list_for_each_entry_safe(d_buf, n_buf,
17242 &seq_dmabuf->dbuf.list, list) {
17243 list_del_init(&d_buf->list);
17244 lpfc_in_buf_free(vport->phba, d_buf);
17245 }
17246 return true;
17247 }
17248 return false;
17249}
17250
17251/**
James Smart6dd9e312013-01-03 15:43:37 -050017252 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
17253 * @vport: pointer to a vitural port
17254 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17255 *
17256 * This function tries to abort from the assembed sequence from upper level
17257 * protocol, described by the information from basic abbort @dmabuf. It
17258 * checks to see whether such pending context exists at upper level protocol.
17259 * If so, it shall clean up the pending context.
17260 *
17261 * Return
17262 * true -- if there is matching pending context of the sequence cleaned
17263 * at ulp;
17264 * false -- if there is no matching pending context of the sequence present
17265 * at ulp.
17266 **/
17267static bool
17268lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17269{
17270 struct lpfc_hba *phba = vport->phba;
17271 int handled;
17272
17273 /* Accepting abort at ulp with SLI4 only */
17274 if (phba->sli_rev < LPFC_SLI_REV4)
17275 return false;
17276
17277 /* Register all caring upper level protocols to attend abort */
17278 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
17279 if (handled)
17280 return true;
17281
17282 return false;
17283}
17284
17285/**
James Smart546fc852011-03-11 16:06:29 -050017286 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040017287 * @phba: Pointer to HBA context object.
17288 * @cmd_iocbq: pointer to the command iocbq structure.
17289 * @rsp_iocbq: pointer to the response iocbq structure.
17290 *
James Smart546fc852011-03-11 16:06:29 -050017291 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040017292 * event. It properly releases the memory allocated to the sequence abort
17293 * accept iocb.
17294 **/
17295static void
James Smart546fc852011-03-11 16:06:29 -050017296lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040017297 struct lpfc_iocbq *cmd_iocbq,
17298 struct lpfc_iocbq *rsp_iocbq)
17299{
James Smart6dd9e312013-01-03 15:43:37 -050017300 struct lpfc_nodelist *ndlp;
17301
17302 if (cmd_iocbq) {
17303 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
17304 lpfc_nlp_put(ndlp);
17305 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017306 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050017307 }
James Smart6b5151f2012-01-18 16:24:06 -050017308
17309 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
17310 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
17311 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17312 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
17313 rsp_iocbq->iocb.ulpStatus,
17314 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040017315}
17316
17317/**
James Smart6d368e52011-05-24 11:44:12 -040017318 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
17319 * @phba: Pointer to HBA context object.
17320 * @xri: xri id in transaction.
17321 *
17322 * This function validates the xri maps to the known range of XRIs allocated an
17323 * used by the driver.
17324 **/
James Smart7851fe22011-07-22 18:36:52 -040017325uint16_t
James Smart6d368e52011-05-24 11:44:12 -040017326lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
17327 uint16_t xri)
17328{
James Smarta2fc4aef2014-09-03 12:57:55 -040017329 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040017330
17331 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
17332 if (xri == phba->sli4_hba.xri_ids[i])
17333 return i;
17334 }
17335 return NO_XRI;
17336}
17337
James Smart6d368e52011-05-24 11:44:12 -040017338/**
James Smart546fc852011-03-11 16:06:29 -050017339 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017340 * @phba: Pointer to HBA context object.
17341 * @fc_hdr: pointer to a FC frame header.
17342 *
James Smart546fc852011-03-11 16:06:29 -050017343 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017344 * event after aborting the sequence handling.
17345 **/
James Smart86c67372017-04-21 16:05:04 -070017346void
James Smart6dd9e312013-01-03 15:43:37 -050017347lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17348 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040017349{
James Smart6dd9e312013-01-03 15:43:37 -050017350 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040017351 struct lpfc_iocbq *ctiocb = NULL;
17352 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040017353 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050017354 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040017355 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050017356 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040017357
17358 if (!lpfc_is_link_up(phba))
17359 return;
17360
17361 sid = sli4_sid_from_fc_hdr(fc_hdr);
17362 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050017363 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040017364
James Smart6dd9e312013-01-03 15:43:37 -050017365 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040017366 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070017367 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050017368 if (!ndlp) {
17369 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17370 "1268 Failed to allocate ndlp for "
17371 "oxid:x%x SID:x%x\n", oxid, sid);
17372 return;
17373 }
James Smart6dd9e312013-01-03 15:43:37 -050017374 /* Put ndlp onto pport node list */
17375 lpfc_enqueue_node(vport, ndlp);
17376 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17377 /* re-setup ndlp without removing from node list */
17378 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17379 if (!ndlp) {
17380 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17381 "3275 Failed to active ndlp found "
17382 "for oxid:x%x SID:x%x\n", oxid, sid);
17383 return;
17384 }
James Smart6669f9b2009-10-02 15:16:45 -040017385 }
17386
James Smart546fc852011-03-11 16:06:29 -050017387 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040017388 ctiocb = lpfc_sli_get_iocbq(phba);
17389 if (!ctiocb)
17390 return;
17391
James Smart5ffc2662009-11-18 15:39:44 -050017392 /* Extract the F_CTL field from FC_HDR */
17393 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17394
James Smart6669f9b2009-10-02 15:16:45 -040017395 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040017396 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050017397 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040017398 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17399 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17400 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17401
17402 /* Fill in the rest of iocb fields */
17403 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17404 icmd->ulpBdeCount = 0;
17405 icmd->ulpLe = 1;
17406 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040017407 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050017408 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017409
James Smart6669f9b2009-10-02 15:16:45 -040017410 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050017411 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040017412 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050017413 ctiocb->sli4_xritag = NO_XRI;
17414
James Smartee0f4fe2012-05-09 21:19:14 -040017415 if (fctl & FC_FC_EX_CTX)
17416 /* Exchange responder sent the abort so we
17417 * own the oxid.
17418 */
17419 xri = oxid;
17420 else
17421 xri = rxid;
17422 lxri = lpfc_sli4_xri_inrange(phba, xri);
17423 if (lxri != NO_XRI)
17424 lpfc_set_rrq_active(phba, ndlp, lxri,
17425 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050017426 /* For BA_ABTS from exchange responder, if the logical xri with
17427 * the oxid maps to the FCP XRI range, the port no longer has
17428 * that exchange context, send a BLS_RJT. Override the IOCB for
17429 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050017430 */
James Smart6dd9e312013-01-03 15:43:37 -050017431 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080017432 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050017433 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17434 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17435 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17436 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17437 }
17438
17439 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17440 * the driver no longer has that exchange, send a BLS_RJT. Override
17441 * the IOCB for a BA_RJT.
17442 */
17443 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050017444 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17445 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17446 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17447 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17448 }
James Smart6669f9b2009-10-02 15:16:45 -040017449
James Smart5ffc2662009-11-18 15:39:44 -050017450 if (fctl & FC_FC_EX_CTX) {
17451 /* ABTS sent by responder to CT exchange, construction
17452 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17453 * field and RX_ID from ABTS for RX_ID field.
17454 */
James Smart546fc852011-03-11 16:06:29 -050017455 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050017456 } else {
17457 /* ABTS sent by initiator to CT exchange, construction
17458 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050017459 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050017460 */
James Smart546fc852011-03-11 16:06:29 -050017461 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050017462 }
James Smartf09c3ac2012-03-01 22:33:29 -050017463 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050017464 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050017465
James Smart546fc852011-03-11 16:06:29 -050017466 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050017467 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17468 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17469 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050017470
17471 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17472 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050017473 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17474 "2925 Failed to issue CT ABTS RSP x%x on "
17475 "xri x%x, Data x%x\n",
17476 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17477 phba->link_state);
17478 lpfc_nlp_put(ndlp);
17479 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050017480 lpfc_sli_release_iocbq(phba, ctiocb);
17481 }
James Smart6669f9b2009-10-02 15:16:45 -040017482}
17483
17484/**
17485 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17486 * @vport: Pointer to the vport on which this sequence was received
17487 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17488 *
17489 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17490 * receive sequence is only partially assembed by the driver, it shall abort
17491 * the partially assembled frames for the sequence. Otherwise, if the
17492 * unsolicited receive sequence has been completely assembled and passed to
17493 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17494 * unsolicited sequence has been aborted. After that, it will issue a basic
17495 * accept to accept the abort.
17496 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017497static void
James Smart6669f9b2009-10-02 15:16:45 -040017498lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17499 struct hbq_dmabuf *dmabuf)
17500{
17501 struct lpfc_hba *phba = vport->phba;
17502 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050017503 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050017504 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040017505
James Smart6669f9b2009-10-02 15:16:45 -040017506 /* Make a copy of fc_hdr before the dmabuf being released */
17507 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050017508 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040017509
James Smart5ffc2662009-11-18 15:39:44 -050017510 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050017511 /* ABTS by responder to exchange, no cleanup needed */
17512 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050017513 } else {
James Smart6dd9e312013-01-03 15:43:37 -050017514 /* ABTS by initiator to exchange, need to do cleanup */
17515 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17516 if (aborted == false)
17517 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050017518 }
James Smart6dd9e312013-01-03 15:43:37 -050017519 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17520
James Smart86c67372017-04-21 16:05:04 -070017521 if (phba->nvmet_support) {
17522 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17523 return;
17524 }
17525
James Smart6dd9e312013-01-03 15:43:37 -050017526 /* Respond with BA_ACC or BA_RJT accordingly */
17527 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040017528}
17529
17530/**
James Smart4f774512009-05-22 14:52:35 -040017531 * lpfc_seq_complete - Indicates if a sequence is complete
17532 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17533 *
17534 * This function checks the sequence, starting with the frame described by
17535 * @dmabuf, to see if all the frames associated with this sequence are present.
17536 * the frames associated with this sequence are linked to the @dmabuf using the
17537 * dbuf list. This function looks for two major things. 1) That the first frame
17538 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17539 * set. 3) That there are no holes in the sequence count. The function will
17540 * return 1 when the sequence is complete, otherwise it will return 0.
17541 **/
17542static int
17543lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17544{
17545 struct fc_frame_header *hdr;
17546 struct lpfc_dmabuf *d_buf;
17547 struct hbq_dmabuf *seq_dmabuf;
17548 uint32_t fctl;
17549 int seq_count = 0;
17550
17551 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17552 /* make sure first fame of sequence has a sequence count of zero */
17553 if (hdr->fh_seq_cnt != seq_count)
17554 return 0;
17555 fctl = (hdr->fh_f_ctl[0] << 16 |
17556 hdr->fh_f_ctl[1] << 8 |
17557 hdr->fh_f_ctl[2]);
17558 /* If last frame of sequence we can return success. */
17559 if (fctl & FC_FC_END_SEQ)
17560 return 1;
17561 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17562 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17563 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17564 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050017565 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040017566 return 0;
17567 fctl = (hdr->fh_f_ctl[0] << 16 |
17568 hdr->fh_f_ctl[1] << 8 |
17569 hdr->fh_f_ctl[2]);
17570 /* If last frame of sequence we can return success. */
17571 if (fctl & FC_FC_END_SEQ)
17572 return 1;
17573 }
17574 return 0;
17575}
17576
17577/**
17578 * lpfc_prep_seq - Prep sequence for ULP processing
17579 * @vport: Pointer to the vport on which this sequence was received
17580 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17581 *
17582 * This function takes a sequence, described by a list of frames, and creates
17583 * a list of iocbq structures to describe the sequence. This iocbq list will be
17584 * used to issue to the generic unsolicited sequence handler. This routine
17585 * returns a pointer to the first iocbq in the list. If the function is unable
17586 * to allocate an iocbq then it throw out the received frames that were not
17587 * able to be described and return a pointer to the first iocbq. If unable to
17588 * allocate any iocbqs (including the first) this function will return NULL.
17589 **/
17590static struct lpfc_iocbq *
17591lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17592{
James Smart7851fe22011-07-22 18:36:52 -040017593 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040017594 struct lpfc_dmabuf *d_buf, *n_buf;
17595 struct lpfc_iocbq *first_iocbq, *iocbq;
17596 struct fc_frame_header *fc_hdr;
17597 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040017598 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050017599 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040017600
17601 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17602 /* remove from receive buffer list */
17603 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017604 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017605 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040017606 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040017607 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017608 /* Get an iocbq struct to fill in. */
17609 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17610 if (first_iocbq) {
17611 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040017612 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017613 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080017614 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040017615
17616 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17617 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17618 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17619 first_iocbq->iocb.un.rcvels.parmRo =
17620 sli4_did_from_fc_hdr(fc_hdr);
17621 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17622 } else
17623 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040017624 first_iocbq->iocb.ulpContext = NO_XRI;
17625 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17626 be16_to_cpu(fc_hdr->fh_ox_id);
17627 /* iocbq is prepped for internal consumption. Physical vpi. */
17628 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17629 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040017630 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040017631 tot_len = bf_get(lpfc_rcqe_length,
17632 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17633
James Smart4f774512009-05-22 14:52:35 -040017634 first_iocbq->context2 = &seq_dmabuf->dbuf;
17635 first_iocbq->context3 = NULL;
17636 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040017637 if (tot_len > LPFC_DATA_BUF_SIZE)
17638 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040017639 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040017640 else
17641 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17642
James Smart4f774512009-05-22 14:52:35 -040017643 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040017644
James Smart7851fe22011-07-22 18:36:52 -040017645 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040017646 }
17647 iocbq = first_iocbq;
17648 /*
17649 * Each IOCBq can have two Buffers assigned, so go through the list
17650 * of buffers for this sequence and save two buffers in each IOCBq
17651 */
17652 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17653 if (!iocbq) {
17654 lpfc_in_buf_free(vport->phba, d_buf);
17655 continue;
17656 }
17657 if (!iocbq->context3) {
17658 iocbq->context3 = d_buf;
17659 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040017660 /* We need to get the size out of the right CQE */
17661 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17662 len = bf_get(lpfc_rcqe_length,
17663 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017664 pbde = (struct ulp_bde64 *)
17665 &iocbq->iocb.unsli3.sli3Words[4];
17666 if (len > LPFC_DATA_BUF_SIZE)
17667 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17668 else
17669 pbde->tus.f.bdeSize = len;
17670
James Smart7851fe22011-07-22 18:36:52 -040017671 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17672 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040017673 } else {
17674 iocbq = lpfc_sli_get_iocbq(vport->phba);
17675 if (!iocbq) {
17676 if (first_iocbq) {
17677 first_iocbq->iocb.ulpStatus =
17678 IOSTAT_FCP_RSP_ERROR;
17679 first_iocbq->iocb.un.ulpWord[4] =
17680 IOERR_NO_RESOURCES;
17681 }
17682 lpfc_in_buf_free(vport->phba, d_buf);
17683 continue;
17684 }
James Smart7851fe22011-07-22 18:36:52 -040017685 /* We need to get the size out of the right CQE */
17686 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17687 len = bf_get(lpfc_rcqe_length,
17688 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017689 iocbq->context2 = d_buf;
17690 iocbq->context3 = NULL;
17691 iocbq->iocb.ulpBdeCount = 1;
17692 if (len > LPFC_DATA_BUF_SIZE)
17693 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17694 LPFC_DATA_BUF_SIZE;
17695 else
17696 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17697
James Smart7851fe22011-07-22 18:36:52 -040017698 tot_len += len;
17699 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17700
James Smart4f774512009-05-22 14:52:35 -040017701 iocbq->iocb.un.rcvels.remoteID = sid;
17702 list_add_tail(&iocbq->list, &first_iocbq->list);
17703 }
17704 }
17705 return first_iocbq;
17706}
17707
James Smart6669f9b2009-10-02 15:16:45 -040017708static void
17709lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17710 struct hbq_dmabuf *seq_dmabuf)
17711{
17712 struct fc_frame_header *fc_hdr;
17713 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17714 struct lpfc_hba *phba = vport->phba;
17715
17716 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17717 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17718 if (!iocbq) {
17719 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17720 "2707 Ring %d handler: Failed to allocate "
17721 "iocb Rctl x%x Type x%x received\n",
17722 LPFC_ELS_RING,
17723 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17724 return;
17725 }
17726 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017727 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017728 iocbq, fc_hdr->fh_r_ctl,
17729 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017730 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017731 "2540 Ring %d handler: unexpected Rctl "
17732 "x%x Type x%x received\n",
17733 LPFC_ELS_RING,
17734 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17735
17736 /* Free iocb created in lpfc_prep_seq */
17737 list_for_each_entry_safe(curr_iocb, next_iocb,
17738 &iocbq->list, list) {
17739 list_del_init(&curr_iocb->list);
17740 lpfc_sli_release_iocbq(phba, curr_iocb);
17741 }
17742 lpfc_sli_release_iocbq(phba, iocbq);
17743}
17744
James Smartae9e28f2017-05-15 15:20:51 -070017745static void
17746lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17747 struct lpfc_iocbq *rspiocb)
17748{
17749 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17750
17751 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017752 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017753 kfree(pcmd);
17754 lpfc_sli_release_iocbq(phba, cmdiocb);
James Smarte817e5d2018-12-13 15:17:53 -080017755 lpfc_drain_txq(phba);
James Smartae9e28f2017-05-15 15:20:51 -070017756}
17757
17758static void
17759lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17760 struct hbq_dmabuf *dmabuf)
17761{
17762 struct fc_frame_header *fc_hdr;
17763 struct lpfc_hba *phba = vport->phba;
17764 struct lpfc_iocbq *iocbq = NULL;
17765 union lpfc_wqe *wqe;
17766 struct lpfc_dmabuf *pcmd = NULL;
17767 uint32_t frame_len;
17768 int rc;
James Smarte817e5d2018-12-13 15:17:53 -080017769 unsigned long iflags;
James Smartae9e28f2017-05-15 15:20:51 -070017770
17771 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17772 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17773
17774 /* Send the received frame back */
17775 iocbq = lpfc_sli_get_iocbq(phba);
James Smarte817e5d2018-12-13 15:17:53 -080017776 if (!iocbq) {
17777 /* Queue cq event and wakeup worker thread to process it */
17778 spin_lock_irqsave(&phba->hbalock, iflags);
17779 list_add_tail(&dmabuf->cq_event.list,
17780 &phba->sli4_hba.sp_queue_event);
17781 phba->hba_flag |= HBA_SP_QUEUE_EVT;
17782 spin_unlock_irqrestore(&phba->hbalock, iflags);
17783 lpfc_worker_wake_up(phba);
17784 return;
17785 }
James Smartae9e28f2017-05-15 15:20:51 -070017786
17787 /* Allocate buffer for command payload */
17788 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17789 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020017790 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070017791 &pcmd->phys);
17792 if (!pcmd || !pcmd->virt)
17793 goto exit;
17794
17795 INIT_LIST_HEAD(&pcmd->list);
17796
17797 /* copyin the payload */
17798 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17799
17800 /* fill in BDE's for command */
17801 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17802 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17803 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17804 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17805
17806 iocbq->context2 = pcmd;
17807 iocbq->vport = vport;
17808 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17809 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17810
17811 /*
17812 * Setup rest of the iocb as though it were a WQE
17813 * Build the SEND_FRAME WQE
17814 */
17815 wqe = (union lpfc_wqe *)&iocbq->iocb;
17816
17817 wqe->send_frame.frame_len = frame_len;
17818 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17819 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17820 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17821 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17822 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17823 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17824
17825 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17826 iocbq->iocb.ulpLe = 1;
17827 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17828 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17829 if (rc == IOCB_ERROR)
17830 goto exit;
17831
17832 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17833 return;
17834
17835exit:
17836 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17837 "2023 Unable to process MDS loopback frame\n");
17838 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017839 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017840 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070017841 if (iocbq)
17842 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070017843 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17844}
17845
James Smart4f774512009-05-22 14:52:35 -040017846/**
17847 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17848 * @phba: Pointer to HBA context object.
17849 *
17850 * This function is called with no lock held. This function processes all
17851 * the received buffers and gives it to upper layers when a received buffer
17852 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017853 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017854 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17855 * appropriate receive function when the final frame in a sequence is received.
17856 **/
James Smart4d9ab992009-10-02 15:16:39 -040017857void
17858lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17859 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017860{
James Smart4d9ab992009-10-02 15:16:39 -040017861 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017862 struct fc_frame_header *fc_hdr;
17863 struct lpfc_vport *vport;
17864 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017865 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017866
James Smart4f774512009-05-22 14:52:35 -040017867 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017868 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017869
James Smarte817e5d2018-12-13 15:17:53 -080017870 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
17871 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
17872 vport = phba->pport;
17873 /* Handle MDS Loopback frames */
17874 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17875 return;
17876 }
17877
James Smart4d9ab992009-10-02 15:16:39 -040017878 /* check to see if this a valid type of frame */
17879 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17880 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17881 return;
17882 }
James Smart2ea259e2017-02-12 13:52:27 -080017883
James Smart7851fe22011-07-22 18:36:52 -040017884 if ((bf_get(lpfc_cqe_code,
17885 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17886 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17887 &dmabuf->cq_event.cqe.rcqe_cmpl);
17888 else
17889 fcfi = bf_get(lpfc_rcqe_fcf_id,
17890 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017891
James Smarte62245d2019-08-14 16:57:08 -070017892 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17893 vport = phba->pport;
17894 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17895 "2023 MDS Loopback %d bytes\n",
17896 bf_get(lpfc_rcqe_length,
17897 &dmabuf->cq_event.cqe.rcqe_cmpl));
17898 /* Handle MDS Loopback frames */
17899 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17900 return;
17901 }
17902
James Smart895427b2017-02-12 13:52:30 -080017903 /* d_id this frame is directed to */
17904 did = sli4_did_from_fc_hdr(fc_hdr);
17905
17906 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017907 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017908 /* throw out the frame */
17909 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17910 return;
17911 }
James Smart939723a2012-05-09 21:19:03 -040017912
James Smart939723a2012-05-09 21:19:03 -040017913 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17914 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17915 (did != Fabric_DID)) {
17916 /*
17917 * Throw out the frame if we are not pt2pt.
17918 * The pt2pt protocol allows for discovery frames
17919 * to be received without a registered VPI.
17920 */
17921 if (!(vport->fc_flag & FC_PT2PT) ||
17922 (phba->link_state == LPFC_HBA_READY)) {
17923 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17924 return;
17925 }
17926 }
17927
James Smart6669f9b2009-10-02 15:16:45 -040017928 /* Handle the basic abort sequence (BA_ABTS) event */
17929 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17930 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17931 return;
17932 }
17933
James Smart4d9ab992009-10-02 15:16:39 -040017934 /* Link this frame */
17935 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17936 if (!seq_dmabuf) {
17937 /* unable to add frame to vport - throw it out */
17938 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17939 return;
17940 }
17941 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017942 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017943 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017944
James Smart6669f9b2009-10-02 15:16:45 -040017945 /* Send the complete sequence to the upper layer protocol */
17946 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017947}
James Smart6fb120a2009-05-22 14:52:59 -040017948
17949/**
17950 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17951 * @phba: pointer to lpfc hba data structure.
17952 *
17953 * This routine is invoked to post rpi header templates to the
17954 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017955 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17956 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017957 *
17958 * This routine does not require any locks. It's usage is expected
17959 * to be driver load or reset recovery when the driver is
17960 * sequential.
17961 *
17962 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017963 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017964 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017965 * When this error occurs, the driver is not guaranteed
17966 * to have any rpi regions posted to the device and
17967 * must either attempt to repost the regions or take a
17968 * fatal error.
17969 **/
17970int
17971lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17972{
17973 struct lpfc_rpi_hdr *rpi_page;
17974 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017975 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017976
James Smart6d368e52011-05-24 11:44:12 -040017977 /* SLI4 ports that support extents do not require RPI headers. */
17978 if (!phba->sli4_hba.rpi_hdrs_in_use)
17979 goto exit;
17980 if (phba->sli4_hba.extents_in_use)
17981 return -EIO;
17982
James Smart6fb120a2009-05-22 14:52:59 -040017983 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017984 /*
17985 * Assign the rpi headers a physical rpi only if the driver
17986 * has not initialized those resources. A port reset only
17987 * needs the headers posted.
17988 */
17989 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17990 LPFC_RPI_RSRC_RDY)
17991 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17992
James Smart6fb120a2009-05-22 14:52:59 -040017993 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17994 if (rc != MBX_SUCCESS) {
17995 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17996 "2008 Error %d posting all rpi "
17997 "headers\n", rc);
17998 rc = -EIO;
17999 break;
18000 }
18001 }
18002
James Smart6d368e52011-05-24 11:44:12 -040018003 exit:
18004 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
18005 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040018006 return rc;
18007}
18008
18009/**
18010 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
18011 * @phba: pointer to lpfc hba data structure.
18012 * @rpi_page: pointer to the rpi memory region.
18013 *
18014 * This routine is invoked to post a single rpi header to the
18015 * HBA consistent with the SLI-4 interface spec. This memory region
18016 * maps up to 64 rpi context regions.
18017 *
18018 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018019 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040018020 * -ENOMEM - No available memory
18021 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040018022 **/
18023int
18024lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
18025{
18026 LPFC_MBOXQ_t *mboxq;
18027 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
18028 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018029 uint32_t shdr_status, shdr_add_status;
18030 union lpfc_sli4_cfg_shdr *shdr;
18031
James Smart6d368e52011-05-24 11:44:12 -040018032 /* SLI4 ports that support extents do not require RPI headers. */
18033 if (!phba->sli4_hba.rpi_hdrs_in_use)
18034 return rc;
18035 if (phba->sli4_hba.extents_in_use)
18036 return -EIO;
18037
James Smart6fb120a2009-05-22 14:52:59 -040018038 /* The port is notified of the header region via a mailbox command. */
18039 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18040 if (!mboxq) {
18041 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18042 "2001 Unable to allocate memory for issuing "
18043 "SLI_CONFIG_SPECIAL mailbox command\n");
18044 return -ENOMEM;
18045 }
18046
18047 /* Post all rpi memory regions to the port. */
18048 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040018049 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18050 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
18051 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050018052 sizeof(struct lpfc_sli4_cfg_mhdr),
18053 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040018054
18055
18056 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040018057 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
18058 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040018059 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
18060 hdr_tmpl, rpi_page->page_count);
18061
James Smart6fb120a2009-05-22 14:52:59 -040018062 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
18063 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040018064 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040018065 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
18066 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18067 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18068 if (rc != MBX_TIMEOUT)
18069 mempool_free(mboxq, phba->mbox_mem_pool);
18070 if (shdr_status || shdr_add_status || rc) {
18071 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18072 "2514 POST_RPI_HDR mailbox failed with "
18073 "status x%x add_status x%x, mbx status x%x\n",
18074 shdr_status, shdr_add_status, rc);
18075 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070018076 } else {
18077 /*
18078 * The next_rpi stores the next logical module-64 rpi value used
18079 * to post physical rpis in subsequent rpi postings.
18080 */
18081 spin_lock_irq(&phba->hbalock);
18082 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
18083 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040018084 }
18085 return rc;
18086}
18087
18088/**
18089 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
18090 * @phba: pointer to lpfc hba data structure.
18091 *
18092 * This routine is invoked to post rpi header templates to the
18093 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040018094 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18095 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040018096 *
18097 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018098 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040018099 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
18100 **/
18101int
18102lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
18103{
James Smart6d368e52011-05-24 11:44:12 -040018104 unsigned long rpi;
18105 uint16_t max_rpi, rpi_limit;
18106 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018107 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040018108 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040018109
James Smart6fb120a2009-05-22 14:52:59 -040018110 /*
James Smart6d368e52011-05-24 11:44:12 -040018111 * Fetch the next logical rpi. Because this index is logical,
18112 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040018113 */
James Smart4902b382013-10-10 12:20:35 -040018114 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040018115 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
18116 rpi_limit = phba->sli4_hba.next_rpi;
18117
James Smart6d368e52011-05-24 11:44:12 -040018118 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
18119 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040018120 rpi = LPFC_RPI_ALLOC_ERROR;
18121 else {
18122 set_bit(rpi, phba->sli4_hba.rpi_bmask);
18123 phba->sli4_hba.max_cfg_param.rpi_used++;
18124 phba->sli4_hba.rpi_count++;
18125 }
James Smartbe6bb942015-04-07 15:07:22 -040018126 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
18127 "0001 rpi:%x max:%x lim:%x\n",
18128 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040018129
18130 /*
18131 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040018132 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040018133 */
18134 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
18135 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040018136 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018137 return rpi;
18138 }
18139
18140 /*
James Smart6d368e52011-05-24 11:44:12 -040018141 * RPI header postings are not required for SLI4 ports capable of
18142 * extents.
18143 */
18144 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040018145 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040018146 return rpi;
18147 }
18148
18149 /*
James Smart6fb120a2009-05-22 14:52:59 -040018150 * If the driver is running low on rpi resources, allocate another
18151 * page now. Note that the next_rpi value is used because
18152 * it represents how many are actually in use whereas max_rpi notes
18153 * how many are supported max by the device.
18154 */
James Smart6d368e52011-05-24 11:44:12 -040018155 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040018156 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018157 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
18158 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
18159 if (!rpi_hdr) {
18160 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18161 "2002 Error Could not grow rpi "
18162 "count\n");
18163 } else {
James Smart6d368e52011-05-24 11:44:12 -040018164 lrpi = rpi_hdr->start_rpi;
18165 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040018166 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
18167 }
18168 }
18169
18170 return rpi;
18171}
18172
18173/**
18174 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18175 * @phba: pointer to lpfc hba data structure.
18176 *
18177 * This routine is invoked to release an rpi to the pool of
18178 * available rpis maintained by the driver.
18179 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018180static void
James Smartd7c47992010-06-08 18:31:54 -040018181__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18182{
18183 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
18184 phba->sli4_hba.rpi_count--;
18185 phba->sli4_hba.max_cfg_param.rpi_used--;
James Smartb95b2112019-08-14 16:56:47 -070018186 } else {
18187 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
18188 "2016 rpi %x not inuse\n",
18189 rpi);
James Smartd7c47992010-06-08 18:31:54 -040018190 }
18191}
18192
18193/**
18194 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18195 * @phba: pointer to lpfc hba data structure.
18196 *
18197 * This routine is invoked to release an rpi to the pool of
18198 * available rpis maintained by the driver.
18199 **/
18200void
James Smart6fb120a2009-05-22 14:52:59 -040018201lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18202{
18203 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040018204 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040018205 spin_unlock_irq(&phba->hbalock);
18206}
18207
18208/**
18209 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
18210 * @phba: pointer to lpfc hba data structure.
18211 *
18212 * This routine is invoked to remove the memory region that
18213 * provided rpi via a bitmask.
18214 **/
18215void
18216lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
18217{
18218 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040018219 kfree(phba->sli4_hba.rpi_ids);
18220 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040018221}
18222
18223/**
18224 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
18225 * @phba: pointer to lpfc hba data structure.
18226 *
18227 * This routine is invoked to remove the memory region that
18228 * provided rpi via a bitmask.
18229 **/
18230int
James Smart6b5151f2012-01-18 16:24:06 -050018231lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
18232 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040018233{
18234 LPFC_MBOXQ_t *mboxq;
18235 struct lpfc_hba *phba = ndlp->phba;
18236 int rc;
18237
18238 /* The port is notified of the header region via a mailbox command. */
18239 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18240 if (!mboxq)
18241 return -ENOMEM;
18242
18243 /* Post all rpi memory regions to the port. */
18244 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050018245 if (cmpl) {
18246 mboxq->mbox_cmpl = cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080018247 mboxq->ctx_buf = arg;
18248 mboxq->ctx_ndlp = ndlp;
James Smart72859902012-01-18 16:25:38 -050018249 } else
18250 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050018251 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040018252 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18253 if (rc == MBX_NOT_FINISHED) {
18254 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18255 "2010 Resume RPI Mailbox failed "
18256 "status %d, mbxStatus x%x\n", rc,
18257 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
18258 mempool_free(mboxq, phba->mbox_mem_pool);
18259 return -EIO;
18260 }
18261 return 0;
18262}
18263
18264/**
18265 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050018266 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040018267 *
James Smart76a95d72010-11-20 23:11:48 -050018268 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040018269 *
18270 * Returns:
18271 * 0 success
18272 * -Evalue otherwise
18273 **/
18274int
James Smart76a95d72010-11-20 23:11:48 -050018275lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040018276{
18277 LPFC_MBOXQ_t *mboxq;
18278 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040018279 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040018280 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050018281 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040018282 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18283 if (!mboxq)
18284 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050018285 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040018286 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040018287 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040018288 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050018289 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040018290 "2022 INIT VPI Mailbox failed "
18291 "status %d, mbxStatus x%x\n", rc,
18292 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040018293 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040018294 }
James Smart6a9c52c2009-10-02 15:16:51 -040018295 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050018296 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040018297
18298 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040018299}
18300
18301/**
18302 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
18303 * @phba: pointer to lpfc hba data structure.
18304 * @mboxq: Pointer to mailbox object.
18305 *
18306 * This routine is invoked to manually add a single FCF record. The caller
18307 * must pass a completely initialized FCF_Record. This routine takes
18308 * care of the nonembedded mailbox operations.
18309 **/
18310static void
18311lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
18312{
18313 void *virt_addr;
18314 union lpfc_sli4_cfg_shdr *shdr;
18315 uint32_t shdr_status, shdr_add_status;
18316
18317 virt_addr = mboxq->sge_array->addr[0];
18318 /* The IOCTL status is embedded in the mailbox subheader. */
18319 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
18320 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18321 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18322
18323 if ((shdr_status || shdr_add_status) &&
18324 (shdr_status != STATUS_FCF_IN_USE))
18325 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18326 "2558 ADD_FCF_RECORD mailbox failed with "
18327 "status x%x add_status x%x\n",
18328 shdr_status, shdr_add_status);
18329
18330 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18331}
18332
18333/**
18334 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
18335 * @phba: pointer to lpfc hba data structure.
18336 * @fcf_record: pointer to the initialized fcf record to add.
18337 *
18338 * This routine is invoked to manually add a single FCF record. The caller
18339 * must pass a completely initialized FCF_Record. This routine takes
18340 * care of the nonembedded mailbox operations.
18341 **/
18342int
18343lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
18344{
18345 int rc = 0;
18346 LPFC_MBOXQ_t *mboxq;
18347 uint8_t *bytep;
18348 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040018349 struct lpfc_mbx_sge sge;
18350 uint32_t alloc_len, req_len;
18351 uint32_t fcfindex;
18352
18353 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18354 if (!mboxq) {
18355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18356 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18357 return -ENOMEM;
18358 }
18359
18360 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18361 sizeof(uint32_t);
18362
18363 /* Allocate DMA memory and set up the non-embedded mailbox command */
18364 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18365 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18366 req_len, LPFC_SLI4_MBX_NEMBED);
18367 if (alloc_len < req_len) {
18368 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18369 "2523 Allocated DMA memory size (x%x) is "
18370 "less than the requested DMA memory "
18371 "size (x%x)\n", alloc_len, req_len);
18372 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18373 return -ENOMEM;
18374 }
18375
18376 /*
18377 * Get the first SGE entry from the non-embedded DMA memory. This
18378 * routine only uses a single SGE.
18379 */
18380 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040018381 virt_addr = mboxq->sge_array->addr[0];
18382 /*
18383 * Configure the FCF record for FCFI 0. This is the driver's
18384 * hardcoded default and gets used in nonFIP mode.
18385 */
18386 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18387 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18388 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18389
18390 /*
18391 * Copy the fcf_index and the FCF Record Data. The data starts after
18392 * the FCoE header plus word10. The data copy needs to be endian
18393 * correct.
18394 */
18395 bytep += sizeof(uint32_t);
18396 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18397 mboxq->vport = phba->pport;
18398 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18399 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18400 if (rc == MBX_NOT_FINISHED) {
18401 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18402 "2515 ADD_FCF_RECORD mailbox failed with "
18403 "status 0x%x\n", rc);
18404 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18405 rc = -EIO;
18406 } else
18407 rc = 0;
18408
18409 return rc;
18410}
18411
18412/**
18413 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18414 * @phba: pointer to lpfc hba data structure.
18415 * @fcf_record: pointer to the fcf record to write the default data.
18416 * @fcf_index: FCF table entry index.
18417 *
18418 * This routine is invoked to build the driver's default FCF record. The
18419 * values used are hardcoded. This routine handles memory initialization.
18420 *
18421 **/
18422void
18423lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18424 struct fcf_record *fcf_record,
18425 uint16_t fcf_index)
18426{
18427 memset(fcf_record, 0, sizeof(struct fcf_record));
18428 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18429 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18430 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18431 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18432 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18433 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18434 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18435 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18436 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18437 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18438 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18439 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18440 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040018441 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040018442 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18443 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18444 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18445 /* Set the VLAN bit map */
18446 if (phba->valid_vlan) {
18447 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18448 = 1 << (phba->vlan_id % 8);
18449 }
18450}
18451
18452/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018453 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040018454 * @phba: pointer to lpfc hba data structure.
18455 * @fcf_index: FCF table entry offset.
18456 *
James Smart0c9ab6f2010-02-26 14:15:57 -050018457 * This routine is invoked to scan the entire FCF table by reading FCF
18458 * record and processing it one at a time starting from the @fcf_index
18459 * for initial FCF discovery or fast FCF failover rediscovery.
18460 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018461 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018462 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040018463 **/
18464int
James Smart0c9ab6f2010-02-26 14:15:57 -050018465lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040018466{
18467 int rc = 0, error;
18468 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040018469
James Smart32b97932009-07-19 10:01:21 -040018470 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050018471 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040018472 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18473 if (!mboxq) {
18474 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18475 "2000 Failed to allocate mbox for "
18476 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040018477 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050018478 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018479 }
James Smartecfd03c2010-02-12 14:41:27 -050018480 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050018481 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050018482 if (rc) {
18483 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050018484 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018485 }
James Smartecfd03c2010-02-12 14:41:27 -050018486 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040018487 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050018488 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040018489
18490 spin_lock_irq(&phba->hbalock);
18491 phba->hba_flag |= FCF_TS_INPROG;
18492 spin_unlock_irq(&phba->hbalock);
18493
James Smart6fb120a2009-05-22 14:52:59 -040018494 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050018495 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040018496 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050018497 else {
James Smart38b92ef2010-08-04 16:11:39 -040018498 /* Reset eligible FCF count for new scan */
18499 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040018500 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018501 error = 0;
James Smart32b97932009-07-19 10:01:21 -040018502 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018503fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040018504 if (error) {
18505 if (mboxq)
18506 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040018507 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040018508 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040018509 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040018510 spin_unlock_irq(&phba->hbalock);
18511 }
James Smart6fb120a2009-05-22 14:52:59 -040018512 return error;
18513}
James Smarta0c87cb2009-07-19 10:01:10 -040018514
18515/**
James Smarta93ff372010-10-22 11:06:08 -040018516 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050018517 * @phba: pointer to lpfc hba data structure.
18518 * @fcf_index: FCF table entry offset.
18519 *
18520 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040018521 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050018522 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018523 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018524 * otherwise.
18525 **/
18526int
18527lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18528{
18529 int rc = 0, error;
18530 LPFC_MBOXQ_t *mboxq;
18531
18532 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18533 if (!mboxq) {
18534 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18535 "2763 Failed to allocate mbox for "
18536 "READ_FCF cmd\n");
18537 error = -ENOMEM;
18538 goto fail_fcf_read;
18539 }
18540 /* Construct the read FCF record mailbox command */
18541 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18542 if (rc) {
18543 error = -EINVAL;
18544 goto fail_fcf_read;
18545 }
18546 /* Issue the mailbox command asynchronously */
18547 mboxq->vport = phba->pport;
18548 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18549 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18550 if (rc == MBX_NOT_FINISHED)
18551 error = -EIO;
18552 else
18553 error = 0;
18554
18555fail_fcf_read:
18556 if (error && mboxq)
18557 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18558 return error;
18559}
18560
18561/**
18562 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18563 * @phba: pointer to lpfc hba data structure.
18564 * @fcf_index: FCF table entry offset.
18565 *
18566 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040018567 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050018568 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018569 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018570 * otherwise.
18571 **/
18572int
18573lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18574{
18575 int rc = 0, error;
18576 LPFC_MBOXQ_t *mboxq;
18577
18578 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18579 if (!mboxq) {
18580 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18581 "2758 Failed to allocate mbox for "
18582 "READ_FCF cmd\n");
18583 error = -ENOMEM;
18584 goto fail_fcf_read;
18585 }
18586 /* Construct the read FCF record mailbox command */
18587 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18588 if (rc) {
18589 error = -EINVAL;
18590 goto fail_fcf_read;
18591 }
18592 /* Issue the mailbox command asynchronously */
18593 mboxq->vport = phba->pport;
18594 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18595 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18596 if (rc == MBX_NOT_FINISHED)
18597 error = -EIO;
18598 else
18599 error = 0;
18600
18601fail_fcf_read:
18602 if (error && mboxq)
18603 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18604 return error;
18605}
18606
18607/**
James Smartf5cb5302015-12-16 18:11:52 -050018608 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040018609 * phba pointer to the lpfc_hba struct for this port.
18610 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18611 * routine when the rr_bmask is empty. The FCF indecies are put into the
18612 * rr_bmask based on their priority level. Starting from the highest priority
18613 * to the lowest. The most likely FCF candidate will be in the highest
18614 * priority group. When this routine is called it searches the fcf_pri list for
18615 * next lowest priority group and repopulates the rr_bmask with only those
18616 * fcf_indexes.
18617 * returns:
18618 * 1=success 0=failure
18619 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018620static int
James Smart7d791df2011-07-22 18:37:52 -040018621lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18622{
18623 uint16_t next_fcf_pri;
18624 uint16_t last_index;
18625 struct lpfc_fcf_pri *fcf_pri;
18626 int rc;
18627 int ret = 0;
18628
18629 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18630 LPFC_SLI4_FCF_TBL_INDX_MAX);
18631 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18632 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050018633
18634 /* Verify the priority list has 2 or more entries */
18635 spin_lock_irq(&phba->hbalock);
18636 if (list_empty(&phba->fcf.fcf_pri_list) ||
18637 list_is_singular(&phba->fcf.fcf_pri_list)) {
18638 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040018639 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18640 "3061 Last IDX %d\n", last_index);
18641 return 0; /* Empty rr list */
18642 }
James Smart25626692013-03-01 16:36:54 -050018643 spin_unlock_irq(&phba->hbalock);
18644
James Smart7d791df2011-07-22 18:37:52 -040018645 next_fcf_pri = 0;
18646 /*
18647 * Clear the rr_bmask and set all of the bits that are at this
18648 * priority.
18649 */
18650 memset(phba->fcf.fcf_rr_bmask, 0,
18651 sizeof(*phba->fcf.fcf_rr_bmask));
18652 spin_lock_irq(&phba->hbalock);
18653 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18654 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18655 continue;
18656 /*
18657 * the 1st priority that has not FLOGI failed
18658 * will be the highest.
18659 */
18660 if (!next_fcf_pri)
18661 next_fcf_pri = fcf_pri->fcf_rec.priority;
18662 spin_unlock_irq(&phba->hbalock);
18663 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18664 rc = lpfc_sli4_fcf_rr_index_set(phba,
18665 fcf_pri->fcf_rec.fcf_index);
18666 if (rc)
18667 return 0;
18668 }
18669 spin_lock_irq(&phba->hbalock);
18670 }
18671 /*
18672 * if next_fcf_pri was not set above and the list is not empty then
18673 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070018674 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040018675 */
18676 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18677 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18678 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18679 /*
18680 * the 1st priority that has not FLOGI failed
18681 * will be the highest.
18682 */
18683 if (!next_fcf_pri)
18684 next_fcf_pri = fcf_pri->fcf_rec.priority;
18685 spin_unlock_irq(&phba->hbalock);
18686 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18687 rc = lpfc_sli4_fcf_rr_index_set(phba,
18688 fcf_pri->fcf_rec.fcf_index);
18689 if (rc)
18690 return 0;
18691 }
18692 spin_lock_irq(&phba->hbalock);
18693 }
18694 } else
18695 ret = 1;
18696 spin_unlock_irq(&phba->hbalock);
18697
18698 return ret;
18699}
18700/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018701 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18702 * @phba: pointer to lpfc hba data structure.
18703 *
18704 * This routine is to get the next eligible FCF record index in a round
18705 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040018706 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050018707 * shall be returned, otherwise, the next eligible FCF record's index
18708 * shall be returned.
18709 **/
18710uint16_t
18711lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18712{
18713 uint16_t next_fcf_index;
18714
James Smart421c6622013-01-03 15:44:16 -050018715initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040018716 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050018717 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18718
James Smart7d791df2011-07-22 18:37:52 -040018719next_priority:
James Smart421c6622013-01-03 15:44:16 -050018720 /* Determine the next fcf index to check */
18721 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050018722 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18723 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040018724 next_fcf_index);
18725
James Smart0c9ab6f2010-02-26 14:15:57 -050018726 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018727 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18728 /*
18729 * If we have wrapped then we need to clear the bits that
18730 * have been tested so that we can detect when we should
18731 * change the priority level.
18732 */
James Smart0c9ab6f2010-02-26 14:15:57 -050018733 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18734 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040018735 }
18736
James Smart0c9ab6f2010-02-26 14:15:57 -050018737
James Smart3804dc82010-07-14 15:31:37 -040018738 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018739 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18740 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18741 /*
18742 * If next fcf index is not found check if there are lower
18743 * Priority level fcf's in the fcf_priority list.
18744 * Set up the rr_bmask with all of the avaiable fcf bits
18745 * at that level and continue the selection process.
18746 */
18747 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018748 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018749 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18750 "2844 No roundrobin failover FCF available\n");
James Smart036cad12018-10-23 13:41:06 -070018751
18752 return LPFC_FCOE_FCF_NEXT_NONE;
James Smart3804dc82010-07-14 15:31:37 -040018753 }
18754
James Smart7d791df2011-07-22 18:37:52 -040018755 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18756 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018757 LPFC_FCF_FLOGI_FAILED) {
18758 if (list_is_singular(&phba->fcf.fcf_pri_list))
18759 return LPFC_FCOE_FCF_NEXT_NONE;
18760
James Smart7d791df2011-07-22 18:37:52 -040018761 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018762 }
James Smart7d791df2011-07-22 18:37:52 -040018763
James Smart3804dc82010-07-14 15:31:37 -040018764 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018765 "2845 Get next roundrobin failover FCF (x%x)\n",
18766 next_fcf_index);
18767
James Smart0c9ab6f2010-02-26 14:15:57 -050018768 return next_fcf_index;
18769}
18770
18771/**
18772 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18773 * @phba: pointer to lpfc hba data structure.
18774 *
18775 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018776 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018777 * does not go beyond the range of the driver allocated bmask dimension
18778 * before setting the bit.
18779 *
18780 * Returns 0 if the index bit successfully set, otherwise, it returns
18781 * -EINVAL.
18782 **/
18783int
18784lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18785{
18786 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18787 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018788 "2610 FCF (x%x) reached driver's book "
18789 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018790 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18791 return -EINVAL;
18792 }
18793 /* Set the eligible FCF record index bmask */
18794 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18795
James Smart3804dc82010-07-14 15:31:37 -040018796 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018797 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018798 "bmask\n", fcf_index);
18799
James Smart0c9ab6f2010-02-26 14:15:57 -050018800 return 0;
18801}
18802
18803/**
James Smart3804dc82010-07-14 15:31:37 -040018804 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018805 * @phba: pointer to lpfc hba data structure.
18806 *
18807 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018808 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018809 * does not go beyond the range of the driver allocated bmask dimension
18810 * before clearing the bit.
18811 **/
18812void
18813lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18814{
James Smart9a803a72013-09-06 12:17:56 -040018815 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018816 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18817 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018818 "2762 FCF (x%x) reached driver's book "
18819 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018820 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18821 return;
18822 }
18823 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018824 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018825 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18826 list) {
James Smart7d791df2011-07-22 18:37:52 -040018827 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18828 list_del_init(&fcf_pri->list);
18829 break;
18830 }
18831 }
18832 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018833 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018834
18835 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018836 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018837 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018838}
18839
18840/**
James Smartecfd03c2010-02-12 14:41:27 -050018841 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18842 * @phba: pointer to lpfc hba data structure.
18843 *
18844 * This routine is the completion routine for the rediscover FCF table mailbox
18845 * command. If the mailbox command returned failure, it will try to stop the
18846 * FCF rediscover wait timer.
18847 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018848static void
James Smartecfd03c2010-02-12 14:41:27 -050018849lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18850{
18851 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18852 uint32_t shdr_status, shdr_add_status;
18853
18854 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18855
18856 shdr_status = bf_get(lpfc_mbox_hdr_status,
18857 &redisc_fcf->header.cfg_shdr.response);
18858 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18859 &redisc_fcf->header.cfg_shdr.response);
18860 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018861 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018862 "2746 Requesting for FCF rediscovery failed "
18863 "status x%x add_status x%x\n",
18864 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018865 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018866 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018867 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018868 spin_unlock_irq(&phba->hbalock);
18869 /*
18870 * CVL event triggered FCF rediscover request failed,
18871 * last resort to re-try current registered FCF entry.
18872 */
18873 lpfc_retry_pport_discovery(phba);
18874 } else {
18875 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018876 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018877 spin_unlock_irq(&phba->hbalock);
18878 /*
18879 * DEAD FCF event triggered FCF rediscover request
18880 * failed, last resort to fail over as a link down
18881 * to FCF registration.
18882 */
18883 lpfc_sli4_fcf_dead_failthrough(phba);
18884 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018885 } else {
18886 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018887 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018888 /*
18889 * Start FCF rediscovery wait timer for pending FCF
18890 * before rescan FCF record table.
18891 */
18892 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018893 }
James Smartecfd03c2010-02-12 14:41:27 -050018894
18895 mempool_free(mbox, phba->mbox_mem_pool);
18896}
18897
18898/**
James Smart3804dc82010-07-14 15:31:37 -040018899 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018900 * @phba: pointer to lpfc hba data structure.
18901 *
18902 * This routine is invoked to request for rediscovery of the entire FCF table
18903 * by the port.
18904 **/
18905int
18906lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18907{
18908 LPFC_MBOXQ_t *mbox;
18909 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18910 int rc, length;
18911
James Smart0c9ab6f2010-02-26 14:15:57 -050018912 /* Cancel retry delay timers to all vports before FCF rediscover */
18913 lpfc_cancel_all_vport_retry_delay_timer(phba);
18914
James Smartecfd03c2010-02-12 14:41:27 -050018915 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18916 if (!mbox) {
18917 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18918 "2745 Failed to allocate mbox for "
18919 "requesting FCF rediscover.\n");
18920 return -ENOMEM;
18921 }
18922
18923 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18924 sizeof(struct lpfc_sli4_cfg_mhdr));
18925 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18926 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18927 length, LPFC_SLI4_MBX_EMBED);
18928
18929 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18930 /* Set count to 0 for invalidating the entire FCF database */
18931 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18932
18933 /* Issue the mailbox command asynchronously */
18934 mbox->vport = phba->pport;
18935 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18936 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18937
18938 if (rc == MBX_NOT_FINISHED) {
18939 mempool_free(mbox, phba->mbox_mem_pool);
18940 return -EIO;
18941 }
18942 return 0;
18943}
18944
18945/**
James Smartfc2b9892010-02-26 14:15:29 -050018946 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18947 * @phba: pointer to lpfc hba data structure.
18948 *
18949 * This function is the failover routine as a last resort to the FCF DEAD
18950 * event when driver failed to perform fast FCF failover.
18951 **/
18952void
18953lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18954{
18955 uint32_t link_state;
18956
18957 /*
18958 * Last resort as FCF DEAD event failover will treat this as
18959 * a link down, but save the link state because we don't want
18960 * it to be changed to Link Down unless it is already down.
18961 */
18962 link_state = phba->link_state;
18963 lpfc_linkdown(phba);
18964 phba->link_state = link_state;
18965
18966 /* Unregister FCF if no devices connected to it */
18967 lpfc_unregister_unused_fcf(phba);
18968}
18969
18970/**
James Smart026abb82011-12-13 13:20:45 -050018971 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018972 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018973 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018974 *
James Smart026abb82011-12-13 13:20:45 -050018975 * This function gets SLI3 port configure region 23 data through memory dump
18976 * mailbox command. When it successfully retrieves data, the size of the data
18977 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018978 **/
James Smart026abb82011-12-13 13:20:45 -050018979static uint32_t
18980lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018981{
18982 LPFC_MBOXQ_t *pmb = NULL;
18983 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018984 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018985 int rc;
18986
James Smart026abb82011-12-13 13:20:45 -050018987 if (!rgn23_data)
18988 return 0;
18989
James Smarta0c87cb2009-07-19 10:01:10 -040018990 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18991 if (!pmb) {
18992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018993 "2600 failed to allocate mailbox memory\n");
18994 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018995 }
18996 mb = &pmb->u.mb;
18997
James Smarta0c87cb2009-07-19 10:01:10 -040018998 do {
18999 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
19000 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
19001
19002 if (rc != MBX_SUCCESS) {
19003 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050019004 "2601 failed to read config "
19005 "region 23, rc 0x%x Status 0x%x\n",
19006 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040019007 mb->un.varDmp.word_cnt = 0;
19008 }
19009 /*
19010 * dump mem may return a zero when finished or we got a
19011 * mailbox error, either way we are done.
19012 */
19013 if (mb->un.varDmp.word_cnt == 0)
19014 break;
19015 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
19016 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
19017
19018 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050019019 rgn23_data + offset,
19020 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040019021 offset += mb->un.varDmp.word_cnt;
19022 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
19023
James Smart026abb82011-12-13 13:20:45 -050019024 mempool_free(pmb, phba->mbox_mem_pool);
19025 return offset;
19026}
19027
19028/**
19029 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
19030 * @phba: pointer to lpfc hba data structure.
19031 * @rgn23_data: pointer to configure region 23 data.
19032 *
19033 * This function gets SLI4 port configure region 23 data through memory dump
19034 * mailbox command. When it successfully retrieves data, the size of the data
19035 * will be returned, otherwise, 0 will be returned.
19036 **/
19037static uint32_t
19038lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
19039{
19040 LPFC_MBOXQ_t *mboxq = NULL;
19041 struct lpfc_dmabuf *mp = NULL;
19042 struct lpfc_mqe *mqe;
19043 uint32_t data_length = 0;
19044 int rc;
19045
19046 if (!rgn23_data)
19047 return 0;
19048
19049 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19050 if (!mboxq) {
19051 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19052 "3105 failed to allocate mailbox memory\n");
19053 return 0;
19054 }
19055
19056 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
19057 goto out;
19058 mqe = &mboxq->u.mqe;
James Smart3e1f0712018-11-29 16:09:29 -080019059 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smart026abb82011-12-13 13:20:45 -050019060 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19061 if (rc)
19062 goto out;
19063 data_length = mqe->un.mb_words[5];
19064 if (data_length == 0)
19065 goto out;
19066 if (data_length > DMP_RGN23_SIZE) {
19067 data_length = 0;
19068 goto out;
19069 }
19070 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
19071out:
19072 mempool_free(mboxq, phba->mbox_mem_pool);
19073 if (mp) {
19074 lpfc_mbuf_free(phba, mp->virt, mp->phys);
19075 kfree(mp);
19076 }
19077 return data_length;
19078}
19079
19080/**
19081 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
19082 * @phba: pointer to lpfc hba data structure.
19083 *
19084 * This function read region 23 and parse TLV for port status to
19085 * decide if the user disaled the port. If the TLV indicates the
19086 * port is disabled, the hba_flag is set accordingly.
19087 **/
19088void
19089lpfc_sli_read_link_ste(struct lpfc_hba *phba)
19090{
19091 uint8_t *rgn23_data = NULL;
19092 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
19093 uint32_t offset = 0;
19094
19095 /* Get adapter Region 23 data */
19096 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
19097 if (!rgn23_data)
19098 goto out;
19099
19100 if (phba->sli_rev < LPFC_SLI_REV4)
19101 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
19102 else {
19103 if_type = bf_get(lpfc_sli_intf_if_type,
19104 &phba->sli4_hba.sli_intf);
19105 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
19106 goto out;
19107 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
19108 }
James Smarta0c87cb2009-07-19 10:01:10 -040019109
19110 if (!data_size)
19111 goto out;
19112
19113 /* Check the region signature first */
19114 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
19115 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19116 "2619 Config region 23 has bad signature\n");
19117 goto out;
19118 }
19119 offset += 4;
19120
19121 /* Check the data structure version */
19122 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
19123 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19124 "2620 Config region 23 has bad version\n");
19125 goto out;
19126 }
19127 offset += 4;
19128
19129 /* Parse TLV entries in the region */
19130 while (offset < data_size) {
19131 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
19132 break;
19133 /*
19134 * If the TLV is not driver specific TLV or driver id is
19135 * not linux driver id, skip the record.
19136 */
19137 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
19138 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
19139 (rgn23_data[offset + 3] != 0)) {
19140 offset += rgn23_data[offset + 1] * 4 + 4;
19141 continue;
19142 }
19143
19144 /* Driver found a driver specific TLV in the config region */
19145 sub_tlv_len = rgn23_data[offset + 1] * 4;
19146 offset += 4;
19147 tlv_offset = 0;
19148
19149 /*
19150 * Search for configured port state sub-TLV.
19151 */
19152 while ((offset < data_size) &&
19153 (tlv_offset < sub_tlv_len)) {
19154 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
19155 offset += 4;
19156 tlv_offset += 4;
19157 break;
19158 }
19159 if (rgn23_data[offset] != PORT_STE_TYPE) {
19160 offset += rgn23_data[offset + 1] * 4 + 4;
19161 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
19162 continue;
19163 }
19164
19165 /* This HBA contains PORT_STE configured */
19166 if (!rgn23_data[offset + 2])
19167 phba->hba_flag |= LINK_DISABLED;
19168
19169 goto out;
19170 }
19171 }
James Smart026abb82011-12-13 13:20:45 -050019172
James Smarta0c87cb2009-07-19 10:01:10 -040019173out:
James Smarta0c87cb2009-07-19 10:01:10 -040019174 kfree(rgn23_data);
19175 return;
19176}
James Smart695a8142010-01-26 23:08:03 -050019177
19178/**
James Smart52d52442011-05-24 11:42:45 -040019179 * lpfc_wr_object - write an object to the firmware
19180 * @phba: HBA structure that indicates port to create a queue on.
19181 * @dmabuf_list: list of dmabufs to write to the port.
19182 * @size: the total byte value of the objects to write to the port.
19183 * @offset: the current offset to be used to start the transfer.
19184 *
19185 * This routine will create a wr_object mailbox command to send to the port.
19186 * the mailbox command will be constructed using the dma buffers described in
19187 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
19188 * BDEs that the imbedded mailbox can support. The @offset variable will be
19189 * used to indicate the starting offset of the transfer and will also return
19190 * the offset after the write object mailbox has completed. @size is used to
19191 * determine the end of the object and whether the eof bit should be set.
19192 *
19193 * Return 0 is successful and offset will contain the the new offset to use
19194 * for the next write.
19195 * Return negative value for error cases.
19196 **/
19197int
19198lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
19199 uint32_t size, uint32_t *offset)
19200{
19201 struct lpfc_mbx_wr_object *wr_object;
19202 LPFC_MBOXQ_t *mbox;
19203 int rc = 0, i = 0;
James Smart50212672018-12-13 15:17:57 -080019204 uint32_t shdr_status, shdr_add_status, shdr_change_status;
James Smart52d52442011-05-24 11:42:45 -040019205 uint32_t mbox_tmo;
James Smart52d52442011-05-24 11:42:45 -040019206 struct lpfc_dmabuf *dmabuf;
19207 uint32_t written = 0;
James Smart50212672018-12-13 15:17:57 -080019208 bool check_change_status = false;
James Smart52d52442011-05-24 11:42:45 -040019209
19210 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19211 if (!mbox)
19212 return -ENOMEM;
19213
19214 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
19215 LPFC_MBOX_OPCODE_WRITE_OBJECT,
19216 sizeof(struct lpfc_mbx_wr_object) -
19217 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
19218
19219 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
19220 wr_object->u.request.write_offset = *offset;
19221 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
19222 wr_object->u.request.object_name[0] =
19223 cpu_to_le32(wr_object->u.request.object_name[0]);
19224 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
19225 list_for_each_entry(dmabuf, dmabuf_list, list) {
19226 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
19227 break;
19228 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
19229 wr_object->u.request.bde[i].addrHigh =
19230 putPaddrHigh(dmabuf->phys);
19231 if (written + SLI4_PAGE_SIZE >= size) {
19232 wr_object->u.request.bde[i].tus.f.bdeSize =
19233 (size - written);
19234 written += (size - written);
19235 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
James Smart50212672018-12-13 15:17:57 -080019236 bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
19237 check_change_status = true;
James Smart52d52442011-05-24 11:42:45 -040019238 } else {
19239 wr_object->u.request.bde[i].tus.f.bdeSize =
19240 SLI4_PAGE_SIZE;
19241 written += SLI4_PAGE_SIZE;
19242 }
19243 i++;
19244 }
19245 wr_object->u.request.bde_count = i;
19246 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
19247 if (!phba->sli4_hba.intr_enable)
19248 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
19249 else {
James Smarta183a152011-10-10 21:32:43 -040019250 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040019251 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
19252 }
19253 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart50212672018-12-13 15:17:57 -080019254 shdr_status = bf_get(lpfc_mbox_hdr_status,
19255 &wr_object->header.cfg_shdr.response);
19256 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
19257 &wr_object->header.cfg_shdr.response);
19258 if (check_change_status) {
19259 shdr_change_status = bf_get(lpfc_wr_object_change_status,
19260 &wr_object->u.response);
19261 switch (shdr_change_status) {
19262 case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
19263 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19264 "3198 Firmware write complete: System "
19265 "reboot required to instantiate\n");
19266 break;
19267 case (LPFC_CHANGE_STATUS_FW_RESET):
19268 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19269 "3199 Firmware write complete: Firmware"
19270 " reset required to instantiate\n");
19271 break;
19272 case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
19273 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19274 "3200 Firmware write complete: Port "
19275 "Migration or PCI Reset required to "
19276 "instantiate\n");
19277 break;
19278 case (LPFC_CHANGE_STATUS_PCI_RESET):
19279 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19280 "3201 Firmware write complete: PCI "
19281 "Reset required to instantiate\n");
19282 break;
19283 default:
19284 break;
19285 }
19286 }
James Smart52d52442011-05-24 11:42:45 -040019287 if (rc != MBX_TIMEOUT)
19288 mempool_free(mbox, phba->mbox_mem_pool);
19289 if (shdr_status || shdr_add_status || rc) {
19290 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19291 "3025 Write Object mailbox failed with "
19292 "status x%x add_status x%x, mbx status x%x\n",
19293 shdr_status, shdr_add_status, rc);
19294 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080019295 *offset = shdr_add_status;
James Smart52d52442011-05-24 11:42:45 -040019296 } else
19297 *offset += wr_object->u.response.actual_write_length;
19298 return rc;
19299}
19300
19301/**
James Smart695a8142010-01-26 23:08:03 -050019302 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
19303 * @vport: pointer to vport data structure.
19304 *
19305 * This function iterate through the mailboxq and clean up all REG_LOGIN
19306 * and REG_VPI mailbox commands associated with the vport. This function
19307 * is called when driver want to restart discovery of the vport due to
19308 * a Clear Virtual Link event.
19309 **/
19310void
19311lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
19312{
19313 struct lpfc_hba *phba = vport->phba;
19314 LPFC_MBOXQ_t *mb, *nextmb;
19315 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040019316 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040019317 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040019318 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040019319 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050019320 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050019321
James Smartd439d282010-09-29 11:18:45 -040019322 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050019323 spin_lock_irq(&phba->hbalock);
19324 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
19325 if (mb->vport != vport)
19326 continue;
19327
19328 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19329 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19330 continue;
19331
James Smartd439d282010-09-29 11:18:45 -040019332 list_del(&mb->list);
19333 list_add_tail(&mb->list, &mbox_cmd_list);
19334 }
19335 /* Clean up active mailbox command with the vport */
19336 mb = phba->sli.mbox_active;
19337 if (mb && (mb->vport == vport)) {
19338 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
19339 (mb->u.mb.mbxCommand == MBX_REG_VPI))
19340 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19341 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019342 act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -040019343 /* Put reference count for delayed processing */
19344 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
19345 /* Unregister the RPI when mailbox complete */
19346 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19347 }
19348 }
James Smart63e801c2010-11-20 23:14:19 -050019349 /* Cleanup any mailbox completions which are not yet processed */
19350 do {
19351 restart_loop = 0;
19352 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
19353 /*
19354 * If this mailox is already processed or it is
19355 * for another vport ignore it.
19356 */
19357 if ((mb->vport != vport) ||
19358 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
19359 continue;
19360
19361 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19362 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19363 continue;
19364
19365 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19366 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019367 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smart63e801c2010-11-20 23:14:19 -050019368 /* Unregister the RPI when mailbox complete */
19369 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19370 restart_loop = 1;
19371 spin_unlock_irq(&phba->hbalock);
19372 spin_lock(shost->host_lock);
19373 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19374 spin_unlock(shost->host_lock);
19375 spin_lock_irq(&phba->hbalock);
19376 break;
19377 }
19378 }
19379 } while (restart_loop);
19380
James Smartd439d282010-09-29 11:18:45 -040019381 spin_unlock_irq(&phba->hbalock);
19382
19383 /* Release the cleaned-up mailbox commands */
19384 while (!list_empty(&mbox_cmd_list)) {
19385 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050019386 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019387 mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
James Smart695a8142010-01-26 23:08:03 -050019388 if (mp) {
19389 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19390 kfree(mp);
19391 }
James Smart3e1f0712018-11-29 16:09:29 -080019392 mb->ctx_buf = NULL;
19393 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
19394 mb->ctx_ndlp = NULL;
James Smart78730cf2010-04-06 15:06:30 -040019395 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019396 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040019397 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019398 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040019399 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040019400 }
James Smart695a8142010-01-26 23:08:03 -050019401 }
James Smart695a8142010-01-26 23:08:03 -050019402 mempool_free(mb, phba->mbox_mem_pool);
19403 }
James Smartd439d282010-09-29 11:18:45 -040019404
19405 /* Release the ndlp with the cleaned-up active mailbox command */
19406 if (act_mbx_ndlp) {
19407 spin_lock(shost->host_lock);
19408 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19409 spin_unlock(shost->host_lock);
19410 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050019411 }
James Smart695a8142010-01-26 23:08:03 -050019412}
19413
James Smart2a9bf3d2010-06-07 15:24:45 -040019414/**
19415 * lpfc_drain_txq - Drain the txq
19416 * @phba: Pointer to HBA context object.
19417 *
19418 * This function attempt to submit IOCBs on the txq
19419 * to the adapter. For SLI4 adapters, the txq contains
19420 * ELS IOCBs that have been deferred because the there
19421 * are no SGLs. This congestion can occur with large
19422 * vport counts during node discovery.
19423 **/
19424
19425uint32_t
19426lpfc_drain_txq(struct lpfc_hba *phba)
19427{
19428 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080019429 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090019430 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040019431 unsigned long iflags = 0;
19432 char *fail_msg = NULL;
19433 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080019434 union lpfc_wqe128 wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040019435 uint32_t txq_cnt = 0;
James Smartdc19e3b2018-05-24 21:08:57 -070019436 struct lpfc_queue *wq;
James Smart2a9bf3d2010-06-07 15:24:45 -040019437
James Smartdc19e3b2018-05-24 21:08:57 -070019438 if (phba->link_flag & LS_MDS_LOOPBACK) {
19439 /* MDS WQE are posted only to first WQ*/
James Smartc00f62e2019-08-14 16:57:11 -070019440 wq = phba->sli4_hba.hdwq[0].io_wq;
James Smartdc19e3b2018-05-24 21:08:57 -070019441 if (unlikely(!wq))
19442 return 0;
19443 pring = wq->pring;
19444 } else {
19445 wq = phba->sli4_hba.els_wq;
19446 if (unlikely(!wq))
19447 return 0;
19448 pring = lpfc_phba_elsring(phba);
19449 }
19450
19451 if (unlikely(!pring) || list_empty(&pring->txq))
Dick Kennedy1234a6d2017-09-29 17:34:29 -070019452 return 0;
James Smart895427b2017-02-12 13:52:30 -080019453
James Smart398d81c2013-05-31 17:04:19 -040019454 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050019455 list_for_each_entry(piocbq, &pring->txq, list) {
19456 txq_cnt++;
19457 }
19458
19459 if (txq_cnt > pring->txq_max)
19460 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019461
James Smart398d81c2013-05-31 17:04:19 -040019462 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019463
James Smart0e9bb8d2013-03-01 16:35:12 -050019464 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040019465 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019466
James Smart19ca7602010-11-20 23:11:55 -050019467 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040019468 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040019469 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040019470 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19471 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050019472 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040019473 break;
19474 }
James Smart895427b2017-02-12 13:52:30 -080019475 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040019476 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050019477 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040019478 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019479 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040019480 }
James Smart0e9bb8d2013-03-01 16:35:12 -050019481 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040019482
19483 /* The xri and iocb resources secured,
19484 * attempt to issue request
19485 */
James Smart6d368e52011-05-24 11:44:12 -040019486 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040019487 piocbq->sli4_xritag = sglq->sli4_xritag;
19488 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19489 fail_msg = "to convert bpl to sgl";
James Smart205e8242018-03-05 12:04:03 -080019490 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019491 fail_msg = "to convert iocb to wqe";
James Smartdc19e3b2018-05-24 21:08:57 -070019492 else if (lpfc_sli4_wq_put(wq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019493 fail_msg = " - Wq is full";
19494 else
19495 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19496
19497 if (fail_msg) {
19498 /* Failed means we can't issue and need to cancel */
19499 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19500 "2822 IOCB failed %s iotag 0x%x "
19501 "xri 0x%x\n",
19502 fail_msg,
19503 piocbq->iotag, piocbq->sli4_xritag);
19504 list_add_tail(&piocbq->list, &completions);
19505 }
James Smart398d81c2013-05-31 17:04:19 -040019506 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019507 }
19508
James Smart2a9bf3d2010-06-07 15:24:45 -040019509 /* Cancel all the IOCBs that cannot be issued */
19510 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19511 IOERR_SLI_ABORTED);
19512
James Smart0e9bb8d2013-03-01 16:35:12 -050019513 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019514}
James Smart895427b2017-02-12 13:52:30 -080019515
19516/**
19517 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19518 * @phba: Pointer to HBA context object.
19519 * @pwqe: Pointer to command WQE.
19520 * @sglq: Pointer to the scatter gather queue object.
19521 *
19522 * This routine converts the bpl or bde that is in the WQE
19523 * to a sgl list for the sli4 hardware. The physical address
19524 * of the bpl/bde is converted back to a virtual address.
19525 * If the WQE contains a BPL then the list of BDE's is
19526 * converted to sli4_sge's. If the WQE contains a single
19527 * BDE then it is converted to a single sli_sge.
19528 * The WQE is still in cpu endianness so the contents of
19529 * the bpl can be used without byte swapping.
19530 *
19531 * Returns valid XRI = Success, NO_XRI = Failure.
19532 */
19533static uint16_t
19534lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19535 struct lpfc_sglq *sglq)
19536{
19537 uint16_t xritag = NO_XRI;
19538 struct ulp_bde64 *bpl = NULL;
19539 struct ulp_bde64 bde;
19540 struct sli4_sge *sgl = NULL;
19541 struct lpfc_dmabuf *dmabuf;
James Smart205e8242018-03-05 12:04:03 -080019542 union lpfc_wqe128 *wqe;
James Smart895427b2017-02-12 13:52:30 -080019543 int numBdes = 0;
19544 int i = 0;
19545 uint32_t offset = 0; /* accumulated offset in the sg request list */
19546 int inbound = 0; /* number of sg reply entries inbound from firmware */
19547 uint32_t cmd;
19548
19549 if (!pwqeq || !sglq)
19550 return xritag;
19551
19552 sgl = (struct sli4_sge *)sglq->sgl;
19553 wqe = &pwqeq->wqe;
19554 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19555
19556 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19557 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19558 return sglq->sli4_xritag;
19559 numBdes = pwqeq->rsvd2;
19560 if (numBdes) {
19561 /* The addrHigh and addrLow fields within the WQE
19562 * have not been byteswapped yet so there is no
19563 * need to swap them back.
19564 */
19565 if (pwqeq->context3)
19566 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19567 else
19568 return xritag;
19569
19570 bpl = (struct ulp_bde64 *)dmabuf->virt;
19571 if (!bpl)
19572 return xritag;
19573
19574 for (i = 0; i < numBdes; i++) {
19575 /* Should already be byte swapped. */
19576 sgl->addr_hi = bpl->addrHigh;
19577 sgl->addr_lo = bpl->addrLow;
19578
19579 sgl->word2 = le32_to_cpu(sgl->word2);
19580 if ((i+1) == numBdes)
19581 bf_set(lpfc_sli4_sge_last, sgl, 1);
19582 else
19583 bf_set(lpfc_sli4_sge_last, sgl, 0);
19584 /* swap the size field back to the cpu so we
19585 * can assign it to the sgl.
19586 */
19587 bde.tus.w = le32_to_cpu(bpl->tus.w);
19588 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19589 /* The offsets in the sgl need to be accumulated
19590 * separately for the request and reply lists.
19591 * The request is always first, the reply follows.
19592 */
19593 switch (cmd) {
19594 case CMD_GEN_REQUEST64_WQE:
19595 /* add up the reply sg entries */
19596 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19597 inbound++;
19598 /* first inbound? reset the offset */
19599 if (inbound == 1)
19600 offset = 0;
19601 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19602 bf_set(lpfc_sli4_sge_type, sgl,
19603 LPFC_SGE_TYPE_DATA);
19604 offset += bde.tus.f.bdeSize;
19605 break;
19606 case CMD_FCP_TRSP64_WQE:
19607 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19608 bf_set(lpfc_sli4_sge_type, sgl,
19609 LPFC_SGE_TYPE_DATA);
19610 break;
19611 case CMD_FCP_TSEND64_WQE:
19612 case CMD_FCP_TRECEIVE64_WQE:
19613 bf_set(lpfc_sli4_sge_type, sgl,
19614 bpl->tus.f.bdeFlags);
19615 if (i < 3)
19616 offset = 0;
19617 else
19618 offset += bde.tus.f.bdeSize;
19619 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19620 break;
19621 }
19622 sgl->word2 = cpu_to_le32(sgl->word2);
19623 bpl++;
19624 sgl++;
19625 }
19626 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19627 /* The addrHigh and addrLow fields of the BDE have not
19628 * been byteswapped yet so they need to be swapped
19629 * before putting them in the sgl.
19630 */
19631 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19632 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19633 sgl->word2 = le32_to_cpu(sgl->word2);
19634 bf_set(lpfc_sli4_sge_last, sgl, 1);
19635 sgl->word2 = cpu_to_le32(sgl->word2);
19636 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19637 }
19638 return sglq->sli4_xritag;
19639}
19640
19641/**
19642 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19643 * @phba: Pointer to HBA context object.
19644 * @ring_number: Base sli ring number
19645 * @pwqe: Pointer to command WQE.
19646 **/
19647int
James Smart1fbf9742019-01-28 11:14:26 -080019648lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
James Smart895427b2017-02-12 13:52:30 -080019649 struct lpfc_iocbq *pwqe)
19650{
James Smart205e8242018-03-05 12:04:03 -080019651 union lpfc_wqe128 *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080019652 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080019653 struct lpfc_queue *wq;
19654 struct lpfc_sglq *sglq;
19655 struct lpfc_sli_ring *pring;
19656 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070019657 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080019658
19659 /* NVME_LS and NVME_LS ABTS requests. */
19660 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19661 pring = phba->sli4_hba.nvmels_wq->pring;
James Smart6a828b02019-01-28 11:14:31 -080019662 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
19663 qp, wq_access);
James Smart895427b2017-02-12 13:52:30 -080019664 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19665 if (!sglq) {
19666 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19667 return WQE_BUSY;
19668 }
19669 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19670 pwqe->sli4_xritag = sglq->sli4_xritag;
19671 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19672 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19673 return WQE_ERROR;
19674 }
19675 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19676 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070019677 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19678 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019679 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019680 return ret;
James Smart895427b2017-02-12 13:52:30 -080019681 }
Dick Kennedycd22d602017-08-23 16:55:35 -070019682
James Smart895427b2017-02-12 13:52:30 -080019683 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19684 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19685 return 0;
19686 }
19687
19688 /* NVME_FCREQ and NVME_ABTS requests */
19689 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19690 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smartc00f62e2019-08-14 16:57:11 -070019691 wq = qp->io_wq;
James Smart1fbf9742019-01-28 11:14:26 -080019692 pring = wq->pring;
James Smart895427b2017-02-12 13:52:30 -080019693
James Smartc00f62e2019-08-14 16:57:11 -070019694 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
James Smart895427b2017-02-12 13:52:30 -080019695
James Smart6a828b02019-01-28 11:14:31 -080019696 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
19697 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070019698 ret = lpfc_sli4_wq_put(wq, wqe);
19699 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019700 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019701 return ret;
James Smart895427b2017-02-12 13:52:30 -080019702 }
19703 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19704 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19705 return 0;
19706 }
19707
James Smartf358dd02017-02-12 13:52:34 -080019708 /* NVMET requests */
19709 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19710 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smartc00f62e2019-08-14 16:57:11 -070019711 wq = qp->io_wq;
James Smart1fbf9742019-01-28 11:14:26 -080019712 pring = wq->pring;
James Smartf358dd02017-02-12 13:52:34 -080019713
James Smartf358dd02017-02-12 13:52:34 -080019714 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070019715 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080019716 if (pwqe->sli4_xritag == NO_XRI) {
19717 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19718 pwqe->sli4_xritag = sglq->sli4_xritag;
19719 }
19720 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19721 pwqe->sli4_xritag);
James Smartc00f62e2019-08-14 16:57:11 -070019722 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
James Smart1fbf9742019-01-28 11:14:26 -080019723
James Smart6a828b02019-01-28 11:14:31 -080019724 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
19725 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070019726 ret = lpfc_sli4_wq_put(wq, wqe);
19727 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080019728 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019729 return ret;
James Smartf358dd02017-02-12 13:52:34 -080019730 }
19731 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19732 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19733 return 0;
19734 }
James Smart895427b2017-02-12 13:52:30 -080019735 return WQE_ERROR;
19736}
James Smartc4908502019-01-28 11:14:28 -080019737
19738#ifdef LPFC_MXP_STAT
19739/**
19740 * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
19741 * @phba: pointer to lpfc hba data structure.
19742 * @hwqid: belong to which HWQ.
19743 *
19744 * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
19745 * 15 seconds after a test case is running.
19746 *
19747 * The user should call lpfc_debugfs_multixripools_write before running a test
19748 * case to clear stat_snapshot_taken. Then the user starts a test case. During
19749 * test case is running, stat_snapshot_taken is incremented by 1 every time when
19750 * this routine is called from heartbeat timer. When stat_snapshot_taken is
19751 * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
19752 **/
19753void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
19754{
19755 struct lpfc_sli4_hdw_queue *qp;
19756 struct lpfc_multixri_pool *multixri_pool;
19757 struct lpfc_pvt_pool *pvt_pool;
19758 struct lpfc_pbl_pool *pbl_pool;
19759 u32 txcmplq_cnt;
19760
19761 qp = &phba->sli4_hba.hdwq[hwqid];
19762 multixri_pool = qp->p_multixri_pool;
19763 if (!multixri_pool)
19764 return;
19765
19766 if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
19767 pvt_pool = &qp->p_multixri_pool->pvt_pool;
19768 pbl_pool = &qp->p_multixri_pool->pbl_pool;
James Smartc00f62e2019-08-14 16:57:11 -070019769 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080019770
19771 multixri_pool->stat_pbl_count = pbl_pool->count;
19772 multixri_pool->stat_pvt_count = pvt_pool->count;
19773 multixri_pool->stat_busy_count = txcmplq_cnt;
19774 }
19775
19776 multixri_pool->stat_snapshot_taken++;
19777}
19778#endif
19779
19780/**
19781 * lpfc_adjust_pvt_pool_count - Adjust private pool count
19782 * @phba: pointer to lpfc hba data structure.
19783 * @hwqid: belong to which HWQ.
19784 *
19785 * This routine moves some XRIs from private to public pool when private pool
19786 * is not busy.
19787 **/
19788void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
19789{
19790 struct lpfc_multixri_pool *multixri_pool;
19791 u32 io_req_count;
19792 u32 prev_io_req_count;
19793
19794 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
19795 if (!multixri_pool)
19796 return;
19797 io_req_count = multixri_pool->io_req_count;
19798 prev_io_req_count = multixri_pool->prev_io_req_count;
19799
19800 if (prev_io_req_count != io_req_count) {
19801 /* Private pool is busy */
19802 multixri_pool->prev_io_req_count = io_req_count;
19803 } else {
19804 /* Private pool is not busy.
19805 * Move XRIs from private to public pool.
19806 */
19807 lpfc_move_xri_pvt_to_pbl(phba, hwqid);
19808 }
19809}
19810
19811/**
19812 * lpfc_adjust_high_watermark - Adjust high watermark
19813 * @phba: pointer to lpfc hba data structure.
19814 * @hwqid: belong to which HWQ.
19815 *
19816 * This routine sets high watermark as number of outstanding XRIs,
19817 * but make sure the new value is between xri_limit/2 and xri_limit.
19818 **/
19819void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
19820{
19821 u32 new_watermark;
19822 u32 watermark_max;
19823 u32 watermark_min;
19824 u32 xri_limit;
19825 u32 txcmplq_cnt;
19826 u32 abts_io_bufs;
19827 struct lpfc_multixri_pool *multixri_pool;
19828 struct lpfc_sli4_hdw_queue *qp;
19829
19830 qp = &phba->sli4_hba.hdwq[hwqid];
19831 multixri_pool = qp->p_multixri_pool;
19832 if (!multixri_pool)
19833 return;
19834 xri_limit = multixri_pool->xri_limit;
19835
19836 watermark_max = xri_limit;
19837 watermark_min = xri_limit / 2;
19838
James Smartc00f62e2019-08-14 16:57:11 -070019839 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080019840 abts_io_bufs = qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -070019841 abts_io_bufs += qp->abts_nvme_io_bufs;
James Smartc4908502019-01-28 11:14:28 -080019842
19843 new_watermark = txcmplq_cnt + abts_io_bufs;
19844 new_watermark = min(watermark_max, new_watermark);
19845 new_watermark = max(watermark_min, new_watermark);
19846 multixri_pool->pvt_pool.high_watermark = new_watermark;
19847
19848#ifdef LPFC_MXP_STAT
19849 multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
19850 new_watermark);
19851#endif
19852}
19853
19854/**
19855 * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
19856 * @phba: pointer to lpfc hba data structure.
19857 * @hwqid: belong to which HWQ.
19858 *
19859 * This routine is called from hearbeat timer when pvt_pool is idle.
19860 * All free XRIs are moved from private to public pool on hwqid with 2 steps.
19861 * The first step moves (all - low_watermark) amount of XRIs.
19862 * The second step moves the rest of XRIs.
19863 **/
19864void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
19865{
19866 struct lpfc_pbl_pool *pbl_pool;
19867 struct lpfc_pvt_pool *pvt_pool;
James Smart6a828b02019-01-28 11:14:31 -080019868 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080019869 struct lpfc_io_buf *lpfc_ncmd;
19870 struct lpfc_io_buf *lpfc_ncmd_next;
19871 unsigned long iflag;
19872 struct list_head tmp_list;
19873 u32 tmp_count;
19874
James Smart6a828b02019-01-28 11:14:31 -080019875 qp = &phba->sli4_hba.hdwq[hwqid];
19876 pbl_pool = &qp->p_multixri_pool->pbl_pool;
19877 pvt_pool = &qp->p_multixri_pool->pvt_pool;
James Smartc4908502019-01-28 11:14:28 -080019878 tmp_count = 0;
19879
James Smart6a828b02019-01-28 11:14:31 -080019880 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
19881 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080019882
19883 if (pvt_pool->count > pvt_pool->low_watermark) {
19884 /* Step 1: move (all - low_watermark) from pvt_pool
19885 * to pbl_pool
19886 */
19887
19888 /* Move low watermark of bufs from pvt_pool to tmp_list */
19889 INIT_LIST_HEAD(&tmp_list);
19890 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
19891 &pvt_pool->list, list) {
19892 list_move_tail(&lpfc_ncmd->list, &tmp_list);
19893 tmp_count++;
19894 if (tmp_count >= pvt_pool->low_watermark)
19895 break;
19896 }
19897
19898 /* Move all bufs from pvt_pool to pbl_pool */
19899 list_splice_init(&pvt_pool->list, &pbl_pool->list);
19900
19901 /* Move all bufs from tmp_list to pvt_pool */
19902 list_splice(&tmp_list, &pvt_pool->list);
19903
19904 pbl_pool->count += (pvt_pool->count - tmp_count);
19905 pvt_pool->count = tmp_count;
19906 } else {
19907 /* Step 2: move the rest from pvt_pool to pbl_pool */
19908 list_splice_init(&pvt_pool->list, &pbl_pool->list);
19909 pbl_pool->count += pvt_pool->count;
19910 pvt_pool->count = 0;
19911 }
19912
19913 spin_unlock(&pvt_pool->lock);
19914 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
19915}
19916
19917/**
19918 * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
19919 * @phba: pointer to lpfc hba data structure
19920 * @pbl_pool: specified public free XRI pool
19921 * @pvt_pool: specified private free XRI pool
19922 * @count: number of XRIs to move
19923 *
19924 * This routine tries to move some free common bufs from the specified pbl_pool
19925 * to the specified pvt_pool. It might move less than count XRIs if there's not
19926 * enough in public pool.
19927 *
19928 * Return:
19929 * true - if XRIs are successfully moved from the specified pbl_pool to the
19930 * specified pvt_pool
19931 * false - if the specified pbl_pool is empty or locked by someone else
19932 **/
19933static bool
James Smart6a828b02019-01-28 11:14:31 -080019934_lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
19935 struct lpfc_pbl_pool *pbl_pool,
James Smartc4908502019-01-28 11:14:28 -080019936 struct lpfc_pvt_pool *pvt_pool, u32 count)
19937{
19938 struct lpfc_io_buf *lpfc_ncmd;
19939 struct lpfc_io_buf *lpfc_ncmd_next;
19940 unsigned long iflag;
19941 int ret;
19942
19943 ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
19944 if (ret) {
19945 if (pbl_pool->count) {
19946 /* Move a batch of XRIs from public to private pool */
James Smart6a828b02019-01-28 11:14:31 -080019947 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080019948 list_for_each_entry_safe(lpfc_ncmd,
19949 lpfc_ncmd_next,
19950 &pbl_pool->list,
19951 list) {
19952 list_move_tail(&lpfc_ncmd->list,
19953 &pvt_pool->list);
19954 pvt_pool->count++;
19955 pbl_pool->count--;
19956 count--;
19957 if (count == 0)
19958 break;
19959 }
19960
19961 spin_unlock(&pvt_pool->lock);
19962 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
19963 return true;
19964 }
19965 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
19966 }
19967
19968 return false;
19969}
19970
19971/**
19972 * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
19973 * @phba: pointer to lpfc hba data structure.
19974 * @hwqid: belong to which HWQ.
19975 * @count: number of XRIs to move
19976 *
19977 * This routine tries to find some free common bufs in one of public pools with
19978 * Round Robin method. The search always starts from local hwqid, then the next
19979 * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
19980 * a batch of free common bufs are moved to private pool on hwqid.
19981 * It might move less than count XRIs if there's not enough in public pool.
19982 **/
19983void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
19984{
19985 struct lpfc_multixri_pool *multixri_pool;
19986 struct lpfc_multixri_pool *next_multixri_pool;
19987 struct lpfc_pvt_pool *pvt_pool;
19988 struct lpfc_pbl_pool *pbl_pool;
James Smart6a828b02019-01-28 11:14:31 -080019989 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080019990 u32 next_hwqid;
19991 u32 hwq_count;
19992 int ret;
19993
James Smart6a828b02019-01-28 11:14:31 -080019994 qp = &phba->sli4_hba.hdwq[hwqid];
19995 multixri_pool = qp->p_multixri_pool;
James Smartc4908502019-01-28 11:14:28 -080019996 pvt_pool = &multixri_pool->pvt_pool;
19997 pbl_pool = &multixri_pool->pbl_pool;
19998
19999 /* Check if local pbl_pool is available */
James Smart6a828b02019-01-28 11:14:31 -080020000 ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080020001 if (ret) {
20002#ifdef LPFC_MXP_STAT
20003 multixri_pool->local_pbl_hit_count++;
20004#endif
20005 return;
20006 }
20007
20008 hwq_count = phba->cfg_hdw_queue;
20009
20010 /* Get the next hwqid which was found last time */
20011 next_hwqid = multixri_pool->rrb_next_hwqid;
20012
20013 do {
20014 /* Go to next hwq */
20015 next_hwqid = (next_hwqid + 1) % hwq_count;
20016
20017 next_multixri_pool =
20018 phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
20019 pbl_pool = &next_multixri_pool->pbl_pool;
20020
20021 /* Check if the public free xri pool is available */
20022 ret = _lpfc_move_xri_pbl_to_pvt(
James Smart6a828b02019-01-28 11:14:31 -080020023 phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080020024
20025 /* Exit while-loop if success or all hwqid are checked */
20026 } while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
20027
20028 /* Starting point for the next time */
20029 multixri_pool->rrb_next_hwqid = next_hwqid;
20030
20031 if (!ret) {
20032 /* stats: all public pools are empty*/
20033 multixri_pool->pbl_empty_count++;
20034 }
20035
20036#ifdef LPFC_MXP_STAT
20037 if (ret) {
20038 if (next_hwqid == hwqid)
20039 multixri_pool->local_pbl_hit_count++;
20040 else
20041 multixri_pool->other_pbl_hit_count++;
20042 }
20043#endif
20044}
20045
20046/**
20047 * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
20048 * @phba: pointer to lpfc hba data structure.
20049 * @qp: belong to which HWQ.
20050 *
20051 * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
20052 * low watermark.
20053 **/
20054void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
20055{
20056 struct lpfc_multixri_pool *multixri_pool;
20057 struct lpfc_pvt_pool *pvt_pool;
20058
20059 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
20060 pvt_pool = &multixri_pool->pvt_pool;
20061
20062 if (pvt_pool->count < pvt_pool->low_watermark)
20063 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20064}
20065
20066/**
20067 * lpfc_release_io_buf - Return one IO buf back to free pool
20068 * @phba: pointer to lpfc hba data structure.
20069 * @lpfc_ncmd: IO buf to be returned.
20070 * @qp: belong to which HWQ.
20071 *
20072 * This routine returns one IO buf back to free pool. If this is an urgent IO,
20073 * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
20074 * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
20075 * xri_limit. If cfg_xri_rebalancing==0, the IO buf is returned to
20076 * lpfc_io_buf_list_put.
20077 **/
20078void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
20079 struct lpfc_sli4_hdw_queue *qp)
20080{
20081 unsigned long iflag;
20082 struct lpfc_pbl_pool *pbl_pool;
20083 struct lpfc_pvt_pool *pvt_pool;
20084 struct lpfc_epd_pool *epd_pool;
20085 u32 txcmplq_cnt;
20086 u32 xri_owned;
20087 u32 xri_limit;
20088 u32 abts_io_bufs;
20089
20090 /* MUST zero fields if buffer is reused by another protocol */
20091 lpfc_ncmd->nvmeCmd = NULL;
20092 lpfc_ncmd->cur_iocbq.wqe_cmpl = NULL;
20093 lpfc_ncmd->cur_iocbq.iocb_cmpl = NULL;
20094
20095 if (phba->cfg_xri_rebalancing) {
20096 if (lpfc_ncmd->expedite) {
20097 /* Return to expedite pool */
20098 epd_pool = &phba->epd_pool;
20099 spin_lock_irqsave(&epd_pool->lock, iflag);
20100 list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
20101 epd_pool->count++;
20102 spin_unlock_irqrestore(&epd_pool->lock, iflag);
20103 return;
20104 }
20105
20106 /* Avoid invalid access if an IO sneaks in and is being rejected
20107 * just _after_ xri pools are destroyed in lpfc_offline.
20108 * Nothing much can be done at this point.
20109 */
20110 if (!qp->p_multixri_pool)
20111 return;
20112
20113 pbl_pool = &qp->p_multixri_pool->pbl_pool;
20114 pvt_pool = &qp->p_multixri_pool->pvt_pool;
20115
James Smartc00f62e2019-08-14 16:57:11 -070020116 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080020117 abts_io_bufs = qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -070020118 abts_io_bufs += qp->abts_nvme_io_bufs;
James Smartc4908502019-01-28 11:14:28 -080020119
20120 xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
20121 xri_limit = qp->p_multixri_pool->xri_limit;
20122
20123#ifdef LPFC_MXP_STAT
20124 if (xri_owned <= xri_limit)
20125 qp->p_multixri_pool->below_limit_count++;
20126 else
20127 qp->p_multixri_pool->above_limit_count++;
20128#endif
20129
20130 /* XRI goes to either public or private free xri pool
20131 * based on watermark and xri_limit
20132 */
20133 if ((pvt_pool->count < pvt_pool->low_watermark) ||
20134 (xri_owned < xri_limit &&
20135 pvt_pool->count < pvt_pool->high_watermark)) {
James Smart6a828b02019-01-28 11:14:31 -080020136 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
20137 qp, free_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020138 list_add_tail(&lpfc_ncmd->list,
20139 &pvt_pool->list);
20140 pvt_pool->count++;
20141 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20142 } else {
James Smart6a828b02019-01-28 11:14:31 -080020143 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
20144 qp, free_pub_pool);
James Smartc4908502019-01-28 11:14:28 -080020145 list_add_tail(&lpfc_ncmd->list,
20146 &pbl_pool->list);
20147 pbl_pool->count++;
20148 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20149 }
20150 } else {
James Smart6a828b02019-01-28 11:14:31 -080020151 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
20152 qp, free_xri);
James Smartc4908502019-01-28 11:14:28 -080020153 list_add_tail(&lpfc_ncmd->list,
20154 &qp->lpfc_io_buf_list_put);
20155 qp->put_io_bufs++;
20156 spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
20157 iflag);
20158 }
James Smartd79c9e92019-08-14 16:57:09 -070020159
20160 if (phba->cfg_xpsgl && !phba->nvmet_support &&
20161 !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))
20162 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
20163
20164 if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))
20165 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
James Smartc4908502019-01-28 11:14:28 -080020166}
20167
20168/**
20169 * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
20170 * @phba: pointer to lpfc hba data structure.
20171 * @pvt_pool: pointer to private pool data structure.
20172 * @ndlp: pointer to lpfc nodelist data structure.
20173 *
20174 * This routine tries to get one free IO buf from private pool.
20175 *
20176 * Return:
20177 * pointer to one free IO buf - if private pool is not empty
20178 * NULL - if private pool is empty
20179 **/
20180static struct lpfc_io_buf *
20181lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
James Smart6a828b02019-01-28 11:14:31 -080020182 struct lpfc_sli4_hdw_queue *qp,
James Smartc4908502019-01-28 11:14:28 -080020183 struct lpfc_pvt_pool *pvt_pool,
20184 struct lpfc_nodelist *ndlp)
20185{
20186 struct lpfc_io_buf *lpfc_ncmd;
20187 struct lpfc_io_buf *lpfc_ncmd_next;
20188 unsigned long iflag;
20189
James Smart6a828b02019-01-28 11:14:31 -080020190 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020191 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20192 &pvt_pool->list, list) {
20193 if (lpfc_test_rrq_active(
20194 phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
20195 continue;
20196 list_del(&lpfc_ncmd->list);
20197 pvt_pool->count--;
20198 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20199 return lpfc_ncmd;
20200 }
20201 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20202
20203 return NULL;
20204}
20205
20206/**
20207 * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
20208 * @phba: pointer to lpfc hba data structure.
20209 *
20210 * This routine tries to get one free IO buf from expedite pool.
20211 *
20212 * Return:
20213 * pointer to one free IO buf - if expedite pool is not empty
20214 * NULL - if expedite pool is empty
20215 **/
20216static struct lpfc_io_buf *
20217lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
20218{
20219 struct lpfc_io_buf *lpfc_ncmd;
20220 struct lpfc_io_buf *lpfc_ncmd_next;
20221 unsigned long iflag;
20222 struct lpfc_epd_pool *epd_pool;
20223
20224 epd_pool = &phba->epd_pool;
20225 lpfc_ncmd = NULL;
20226
20227 spin_lock_irqsave(&epd_pool->lock, iflag);
20228 if (epd_pool->count > 0) {
20229 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20230 &epd_pool->list, list) {
20231 list_del(&lpfc_ncmd->list);
20232 epd_pool->count--;
20233 break;
20234 }
20235 }
20236 spin_unlock_irqrestore(&epd_pool->lock, iflag);
20237
20238 return lpfc_ncmd;
20239}
20240
20241/**
20242 * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
20243 * @phba: pointer to lpfc hba data structure.
20244 * @ndlp: pointer to lpfc nodelist data structure.
20245 * @hwqid: belong to which HWQ
20246 * @expedite: 1 means this request is urgent.
20247 *
20248 * This routine will do the following actions and then return a pointer to
20249 * one free IO buf.
20250 *
20251 * 1. If private free xri count is empty, move some XRIs from public to
20252 * private pool.
20253 * 2. Get one XRI from private free xri pool.
20254 * 3. If we fail to get one from pvt_pool and this is an expedite request,
20255 * get one free xri from expedite pool.
20256 *
20257 * Note: ndlp is only used on SCSI side for RRQ testing.
20258 * The caller should pass NULL for ndlp on NVME side.
20259 *
20260 * Return:
20261 * pointer to one free IO buf - if private pool is not empty
20262 * NULL - if private pool is empty
20263 **/
20264static struct lpfc_io_buf *
20265lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
20266 struct lpfc_nodelist *ndlp,
20267 int hwqid, int expedite)
20268{
20269 struct lpfc_sli4_hdw_queue *qp;
20270 struct lpfc_multixri_pool *multixri_pool;
20271 struct lpfc_pvt_pool *pvt_pool;
20272 struct lpfc_io_buf *lpfc_ncmd;
20273
20274 qp = &phba->sli4_hba.hdwq[hwqid];
20275 lpfc_ncmd = NULL;
20276 multixri_pool = qp->p_multixri_pool;
20277 pvt_pool = &multixri_pool->pvt_pool;
20278 multixri_pool->io_req_count++;
20279
20280 /* If pvt_pool is empty, move some XRIs from public to private pool */
20281 if (pvt_pool->count == 0)
20282 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20283
20284 /* Get one XRI from private free xri pool */
James Smart6a828b02019-01-28 11:14:31 -080020285 lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
James Smartc4908502019-01-28 11:14:28 -080020286
20287 if (lpfc_ncmd) {
20288 lpfc_ncmd->hdwq = qp;
20289 lpfc_ncmd->hdwq_no = hwqid;
20290 } else if (expedite) {
20291 /* If we fail to get one from pvt_pool and this is an expedite
20292 * request, get one free xri from expedite pool.
20293 */
20294 lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
20295 }
20296
20297 return lpfc_ncmd;
20298}
20299
20300static inline struct lpfc_io_buf *
20301lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
20302{
20303 struct lpfc_sli4_hdw_queue *qp;
20304 struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
20305
20306 qp = &phba->sli4_hba.hdwq[idx];
20307 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
20308 &qp->lpfc_io_buf_list_get, list) {
20309 if (lpfc_test_rrq_active(phba, ndlp,
20310 lpfc_cmd->cur_iocbq.sli4_lxritag))
20311 continue;
20312
20313 if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
20314 continue;
20315
20316 list_del_init(&lpfc_cmd->list);
20317 qp->get_io_bufs--;
20318 lpfc_cmd->hdwq = qp;
20319 lpfc_cmd->hdwq_no = idx;
20320 return lpfc_cmd;
20321 }
20322 return NULL;
20323}
20324
20325/**
20326 * lpfc_get_io_buf - Get one IO buffer from free pool
20327 * @phba: The HBA for which this call is being executed.
20328 * @ndlp: pointer to lpfc nodelist data structure.
20329 * @hwqid: belong to which HWQ
20330 * @expedite: 1 means this request is urgent.
20331 *
20332 * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
20333 * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
20334 * a IO buffer from head of @hdwq io_buf_list and returns to caller.
20335 *
20336 * Note: ndlp is only used on SCSI side for RRQ testing.
20337 * The caller should pass NULL for ndlp on NVME side.
20338 *
20339 * Return codes:
20340 * NULL - Error
20341 * Pointer to lpfc_io_buf - Success
20342 **/
20343struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
20344 struct lpfc_nodelist *ndlp,
20345 u32 hwqid, int expedite)
20346{
20347 struct lpfc_sli4_hdw_queue *qp;
20348 unsigned long iflag;
20349 struct lpfc_io_buf *lpfc_cmd;
20350
20351 qp = &phba->sli4_hba.hdwq[hwqid];
20352 lpfc_cmd = NULL;
20353
20354 if (phba->cfg_xri_rebalancing)
20355 lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
20356 phba, ndlp, hwqid, expedite);
20357 else {
James Smart6a828b02019-01-28 11:14:31 -080020358 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
20359 qp, alloc_xri_get);
James Smartc4908502019-01-28 11:14:28 -080020360 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
20361 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
20362 if (!lpfc_cmd) {
James Smart6a828b02019-01-28 11:14:31 -080020363 lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
20364 qp, alloc_xri_put);
James Smartc4908502019-01-28 11:14:28 -080020365 list_splice(&qp->lpfc_io_buf_list_put,
20366 &qp->lpfc_io_buf_list_get);
20367 qp->get_io_bufs += qp->put_io_bufs;
20368 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
20369 qp->put_io_bufs = 0;
20370 spin_unlock(&qp->io_buf_list_put_lock);
20371 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
20372 expedite)
20373 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
20374 }
20375 spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
20376 }
20377
20378 return lpfc_cmd;
20379}
James Smartd79c9e92019-08-14 16:57:09 -070020380
20381/**
20382 * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool
20383 * @phba: The HBA for which this call is being executed.
20384 * @lpfc_buf: IO buf structure to append the SGL chunk
20385 *
20386 * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,
20387 * and will allocate an SGL chunk if the pool is empty.
20388 *
20389 * Return codes:
20390 * NULL - Error
20391 * Pointer to sli4_hybrid_sgl - Success
20392 **/
20393struct sli4_hybrid_sgl *
20394lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
20395{
20396 struct sli4_hybrid_sgl *list_entry = NULL;
20397 struct sli4_hybrid_sgl *tmp = NULL;
20398 struct sli4_hybrid_sgl *allocated_sgl = NULL;
20399 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
20400 struct list_head *buf_list = &hdwq->sgl_list;
20401
20402 spin_lock_irq(&hdwq->hdwq_lock);
20403
20404 if (likely(!list_empty(buf_list))) {
20405 /* break off 1 chunk from the sgl_list */
20406 list_for_each_entry_safe(list_entry, tmp,
20407 buf_list, list_node) {
20408 list_move_tail(&list_entry->list_node,
20409 &lpfc_buf->dma_sgl_xtra_list);
20410 break;
20411 }
20412 } else {
20413 /* allocate more */
20414 spin_unlock_irq(&hdwq->hdwq_lock);
20415 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
20416 cpu_to_node(smp_processor_id()));
20417 if (!tmp) {
20418 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
20419 "8353 error kmalloc memory for HDWQ "
20420 "%d %s\n",
20421 lpfc_buf->hdwq_no, __func__);
20422 return NULL;
20423 }
20424
20425 tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
20426 GFP_ATOMIC, &tmp->dma_phys_sgl);
20427 if (!tmp->dma_sgl) {
20428 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
20429 "8354 error pool_alloc memory for HDWQ "
20430 "%d %s\n",
20431 lpfc_buf->hdwq_no, __func__);
20432 kfree(tmp);
20433 return NULL;
20434 }
20435
20436 spin_lock_irq(&hdwq->hdwq_lock);
20437 list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);
20438 }
20439
20440 allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,
20441 struct sli4_hybrid_sgl,
20442 list_node);
20443
20444 spin_unlock_irq(&hdwq->hdwq_lock);
20445
20446 return allocated_sgl;
20447}
20448
20449/**
20450 * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool
20451 * @phba: The HBA for which this call is being executed.
20452 * @lpfc_buf: IO buf structure with the SGL chunk
20453 *
20454 * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.
20455 *
20456 * Return codes:
20457 * 0 - Success
20458 * -EINVAL - Error
20459 **/
20460int
20461lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
20462{
20463 int rc = 0;
20464 struct sli4_hybrid_sgl *list_entry = NULL;
20465 struct sli4_hybrid_sgl *tmp = NULL;
20466 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
20467 struct list_head *buf_list = &hdwq->sgl_list;
20468
20469 spin_lock_irq(&hdwq->hdwq_lock);
20470
20471 if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {
20472 list_for_each_entry_safe(list_entry, tmp,
20473 &lpfc_buf->dma_sgl_xtra_list,
20474 list_node) {
20475 list_move_tail(&list_entry->list_node,
20476 buf_list);
20477 }
20478 } else {
20479 rc = -EINVAL;
20480 }
20481
20482 spin_unlock_irq(&hdwq->hdwq_lock);
20483 return rc;
20484}
20485
20486/**
20487 * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool
20488 * @phba: phba object
20489 * @hdwq: hdwq to cleanup sgl buff resources on
20490 *
20491 * This routine frees all SGL chunks of hdwq SGL chunk pool.
20492 *
20493 * Return codes:
20494 * None
20495 **/
20496void
20497lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,
20498 struct lpfc_sli4_hdw_queue *hdwq)
20499{
20500 struct list_head *buf_list = &hdwq->sgl_list;
20501 struct sli4_hybrid_sgl *list_entry = NULL;
20502 struct sli4_hybrid_sgl *tmp = NULL;
20503
20504 spin_lock_irq(&hdwq->hdwq_lock);
20505
20506 /* Free sgl pool */
20507 list_for_each_entry_safe(list_entry, tmp,
20508 buf_list, list_node) {
20509 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
20510 list_entry->dma_sgl,
20511 list_entry->dma_phys_sgl);
20512 list_del(&list_entry->list_node);
20513 kfree(list_entry);
20514 }
20515
20516 spin_unlock_irq(&hdwq->hdwq_lock);
20517}
20518
20519/**
20520 * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq
20521 * @phba: The HBA for which this call is being executed.
20522 * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer
20523 *
20524 * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,
20525 * and will allocate an CMD/RSP buffer if the pool is empty.
20526 *
20527 * Return codes:
20528 * NULL - Error
20529 * Pointer to fcp_cmd_rsp_buf - Success
20530 **/
20531struct fcp_cmd_rsp_buf *
20532lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
20533 struct lpfc_io_buf *lpfc_buf)
20534{
20535 struct fcp_cmd_rsp_buf *list_entry = NULL;
20536 struct fcp_cmd_rsp_buf *tmp = NULL;
20537 struct fcp_cmd_rsp_buf *allocated_buf = NULL;
20538 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
20539 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
20540
20541 spin_lock_irq(&hdwq->hdwq_lock);
20542
20543 if (likely(!list_empty(buf_list))) {
20544 /* break off 1 chunk from the list */
20545 list_for_each_entry_safe(list_entry, tmp,
20546 buf_list,
20547 list_node) {
20548 list_move_tail(&list_entry->list_node,
20549 &lpfc_buf->dma_cmd_rsp_list);
20550 break;
20551 }
20552 } else {
20553 /* allocate more */
20554 spin_unlock_irq(&hdwq->hdwq_lock);
20555 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
20556 cpu_to_node(smp_processor_id()));
20557 if (!tmp) {
20558 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
20559 "8355 error kmalloc memory for HDWQ "
20560 "%d %s\n",
20561 lpfc_buf->hdwq_no, __func__);
20562 return NULL;
20563 }
20564
20565 tmp->fcp_cmnd = dma_pool_alloc(phba->lpfc_cmd_rsp_buf_pool,
20566 GFP_ATOMIC,
20567 &tmp->fcp_cmd_rsp_dma_handle);
20568
20569 if (!tmp->fcp_cmnd) {
20570 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
20571 "8356 error pool_alloc memory for HDWQ "
20572 "%d %s\n",
20573 lpfc_buf->hdwq_no, __func__);
20574 kfree(tmp);
20575 return NULL;
20576 }
20577
20578 tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +
20579 sizeof(struct fcp_cmnd));
20580
20581 spin_lock_irq(&hdwq->hdwq_lock);
20582 list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);
20583 }
20584
20585 allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,
20586 struct fcp_cmd_rsp_buf,
20587 list_node);
20588
20589 spin_unlock_irq(&hdwq->hdwq_lock);
20590
20591 return allocated_buf;
20592}
20593
20594/**
20595 * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool
20596 * @phba: The HBA for which this call is being executed.
20597 * @lpfc_buf: IO buf structure with the CMD/RSP buf
20598 *
20599 * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.
20600 *
20601 * Return codes:
20602 * 0 - Success
20603 * -EINVAL - Error
20604 **/
20605int
20606lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
20607 struct lpfc_io_buf *lpfc_buf)
20608{
20609 int rc = 0;
20610 struct fcp_cmd_rsp_buf *list_entry = NULL;
20611 struct fcp_cmd_rsp_buf *tmp = NULL;
20612 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
20613 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
20614
20615 spin_lock_irq(&hdwq->hdwq_lock);
20616
20617 if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {
20618 list_for_each_entry_safe(list_entry, tmp,
20619 &lpfc_buf->dma_cmd_rsp_list,
20620 list_node) {
20621 list_move_tail(&list_entry->list_node,
20622 buf_list);
20623 }
20624 } else {
20625 rc = -EINVAL;
20626 }
20627
20628 spin_unlock_irq(&hdwq->hdwq_lock);
20629 return rc;
20630}
20631
20632/**
20633 * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool
20634 * @phba: phba object
20635 * @hdwq: hdwq to cleanup cmd rsp buff resources on
20636 *
20637 * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.
20638 *
20639 * Return codes:
20640 * None
20641 **/
20642void
20643lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
20644 struct lpfc_sli4_hdw_queue *hdwq)
20645{
20646 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
20647 struct fcp_cmd_rsp_buf *list_entry = NULL;
20648 struct fcp_cmd_rsp_buf *tmp = NULL;
20649
20650 spin_lock_irq(&hdwq->hdwq_lock);
20651
20652 /* Free cmd_rsp buf pool */
20653 list_for_each_entry_safe(list_entry, tmp,
20654 buf_list,
20655 list_node) {
20656 dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,
20657 list_entry->fcp_cmnd,
20658 list_entry->fcp_cmd_rsp_dma_handle);
20659 list_del(&list_entry->list_node);
20660 kfree(list_entry);
20661 }
20662
20663 spin_unlock_irq(&hdwq->hdwq_lock);
20664}