blob: 337e4f105f23d85b1939ef57986bec5029ec9b75 [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 *
994 * This function is called with hbalock held. This function
995 * returns 0 = rrq not active for this xri
996 * 1 = rrq is valid for this xri.
997 **/
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{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001002 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -05001003 if (!ndlp)
1004 return 0;
James Smartcff261f2013-12-17 20:29:47 -05001005 if (!ndlp->active_rrqs_xri_bitmap)
1006 return 0;
1007 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
Colin Ian King258f84f2019-02-12 15:29:45 +00001008 return 1;
James Smart19ca7602010-11-20 23:11:55 -05001009 else
1010 return 0;
1011}
1012
1013/**
1014 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1015 * @phba: Pointer to HBA context object.
1016 * @ndlp: nodelist pointer for this target.
1017 * @xritag: xri used in this exchange.
1018 * @rxid: Remote Exchange ID.
1019 * @send_rrq: Flag used to determine if we should send rrq els cmd.
1020 *
1021 * This function takes the hbalock.
1022 * The active bit is always set in the active rrq xri_bitmap even
1023 * if there is no slot avaiable for the other rrq information.
1024 *
1025 * returns 0 rrq actived for this xri
1026 * < 0 No memory or invalid ndlp.
1027 **/
1028int
1029lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -05001030 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -05001031{
James Smart19ca7602010-11-20 23:11:55 -05001032 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -05001033 struct lpfc_node_rrq *rrq;
1034 int empty;
1035
1036 if (!ndlp)
1037 return -EINVAL;
1038
1039 if (!phba->cfg_enable_rrq)
1040 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001041
1042 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001043 if (phba->pport->load_flag & FC_UNLOADING) {
1044 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1045 goto out;
1046 }
1047
1048 /*
1049 * set the active bit even if there is no mem available.
1050 */
1051 if (NLP_CHK_FREE_REQ(ndlp))
1052 goto out;
1053
1054 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1055 goto out;
1056
James Smartcff261f2013-12-17 20:29:47 -05001057 if (!ndlp->active_rrqs_xri_bitmap)
1058 goto out;
1059
1060 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -05001061 goto out;
1062
James Smart19ca7602010-11-20 23:11:55 -05001063 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001064 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
1065 if (!rrq) {
1066 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1067 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1068 " DID:0x%x Send:%d\n",
1069 xritag, rxid, ndlp->nlp_DID, send_rrq);
1070 return -EINVAL;
1071 }
James Smarte5771b42013-03-01 16:37:14 -05001072 if (phba->cfg_enable_rrq == 1)
1073 rrq->send_rrq = send_rrq;
1074 else
1075 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -05001076 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -04001077 rrq->rrq_stop_time = jiffies +
1078 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -05001079 rrq->ndlp = ndlp;
1080 rrq->nlp_DID = ndlp->nlp_DID;
1081 rrq->vport = ndlp->vport;
1082 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -05001083 spin_lock_irqsave(&phba->hbalock, iflags);
1084 empty = list_empty(&phba->active_rrq_list);
1085 list_add_tail(&rrq->list, &phba->active_rrq_list);
1086 phba->hba_flag |= HBA_RRQ_ACTIVE;
1087 if (empty)
1088 lpfc_worker_wake_up(phba);
1089 spin_unlock_irqrestore(&phba->hbalock, iflags);
1090 return 0;
1091out:
1092 spin_unlock_irqrestore(&phba->hbalock, iflags);
1093 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1094 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1095 " DID:0x%x Send:%d\n",
1096 xritag, rxid, ndlp->nlp_DID, send_rrq);
1097 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001098}
1099
1100/**
James Smart895427b2017-02-12 13:52:30 -08001101 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -04001102 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -05001103 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -04001104 *
James Smartdafe8ce2014-09-03 12:56:40 -04001105 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -04001106 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -04001107 * list is not empty then it is successful, it returns pointer to the newly
1108 * allocated sglq object else it returns NULL.
1109 **/
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;
1118 int found = 0;
1119
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001120 lockdep_assert_held(&phba->hbalock);
1121
James Smart19ca7602010-11-20 23:11:55 -05001122 if (piocbq->iocb_flag & LPFC_IO_FCP) {
James Smartc4908502019-01-28 11:14:28 -08001123 lpfc_cmd = (struct lpfc_io_buf *) piocbq->context1;
James Smart19ca7602010-11-20 23:11:55 -05001124 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -05001125 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -04001126 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -05001127 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -04001128 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1129 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1130 ndlp = NULL;
1131 else
1132 ndlp = piocbq->context_un.ndlp;
1133 } else {
James Smart19ca7602010-11-20 23:11:55 -05001134 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -04001135 }
James Smart19ca7602010-11-20 23:11:55 -05001136
James Smart895427b2017-02-12 13:52:30 -08001137 spin_lock(&phba->sli4_hba.sgl_list_lock);
1138 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -05001139 start_sglq = sglq;
1140 while (!found) {
1141 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -08001142 break;
James Smart895427b2017-02-12 13:52:30 -08001143 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1144 test_bit(sglq->sli4_lxritag,
1145 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -05001146 /* This xri has an rrq outstanding for this DID.
1147 * put it back in the list and get another xri.
1148 */
James Smart895427b2017-02-12 13:52:30 -08001149 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001150 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -08001151 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -05001152 struct lpfc_sglq, list);
1153 if (sglq == start_sglq) {
James Smart14041bd2017-06-01 21:07:01 -07001154 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001155 sglq = NULL;
1156 break;
1157 } else
1158 continue;
1159 }
1160 sglq->ndlp = ndlp;
1161 found = 1;
James Smart6d368e52011-05-24 11:44:12 -04001162 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -05001163 sglq->state = SGL_ALLOCATED;
1164 }
James Smart895427b2017-02-12 13:52:30 -08001165 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001166 return sglq;
1167}
1168
1169/**
James Smartf358dd02017-02-12 13:52:34 -08001170 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1171 * @phba: Pointer to HBA context object.
1172 * @piocb: Pointer to the iocbq.
1173 *
1174 * This function is called with the sgl_list lock held. This function
1175 * gets a new driver sglq object from the sglq list. If the
1176 * list is not empty then it is successful, it returns pointer to the newly
1177 * allocated sglq object else it returns NULL.
1178 **/
1179struct lpfc_sglq *
1180__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1181{
1182 struct list_head *lpfc_nvmet_sgl_list;
1183 struct lpfc_sglq *sglq = NULL;
1184
1185 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1186
1187 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1188
1189 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1190 if (!sglq)
1191 return NULL;
1192 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1193 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001194 return sglq;
1195}
1196
James Smarte59058c2008-08-24 21:49:00 -04001197/**
James Smart3621a712009-04-06 18:47:14 -04001198 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001199 * @phba: Pointer to HBA context object.
1200 *
1201 * This function is called with no lock held. This function
1202 * allocates a new driver iocb object from the iocb pool. If the
1203 * allocation is successful, it returns pointer to the newly
1204 * allocated iocb object else it returns NULL.
1205 **/
James Smart2e0fef82007-06-17 19:56:36 -05001206struct lpfc_iocbq *
1207lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001208{
James Smart2e0fef82007-06-17 19:56:36 -05001209 struct lpfc_iocbq * iocbq = NULL;
1210 unsigned long iflags;
1211
1212 spin_lock_irqsave(&phba->hbalock, iflags);
1213 iocbq = __lpfc_sli_get_iocbq(phba);
1214 spin_unlock_irqrestore(&phba->hbalock, iflags);
1215 return iocbq;
1216}
1217
James Smarte59058c2008-08-24 21:49:00 -04001218/**
James Smart4f774512009-05-22 14:52:35 -04001219 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1220 * @phba: Pointer to HBA context object.
1221 * @iocbq: Pointer to driver iocb object.
1222 *
1223 * This function is called with hbalock held to release driver
1224 * iocb object to the iocb pool. The iotag in the iocb object
1225 * does not change for each use of the iocb object. This function
1226 * clears all other fields of the iocb object when it is freed.
1227 * The sqlq structure that holds the xritag and phys and virtual
1228 * mappings for the scatter gather list is retrieved from the
1229 * active array of sglq. The get of the sglq pointer also clears
1230 * the entry in the array. If the status of the IO indiactes that
1231 * this IO was aborted then the sglq entry it put on the
1232 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1233 * IO has good status or fails for any other reason then the sglq
James Smart895427b2017-02-12 13:52:30 -08001234 * entry is added to the free list (lpfc_els_sgl_list).
James Smart4f774512009-05-22 14:52:35 -04001235 **/
1236static void
1237__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1238{
1239 struct lpfc_sglq *sglq;
1240 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001241 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001242 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001243
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001244 lockdep_assert_held(&phba->hbalock);
1245
James Smart4f774512009-05-22 14:52:35 -04001246 if (iocbq->sli4_xritag == NO_XRI)
1247 sglq = NULL;
1248 else
James Smart6d368e52011-05-24 11:44:12 -04001249 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1250
James Smart0e9bb8d2013-03-01 16:35:12 -05001251
James Smart4f774512009-05-22 14:52:35 -04001252 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001253 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1254 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1255 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001256 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001257 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001258 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001259 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1260 spin_unlock_irqrestore(
1261 &phba->sli4_hba.sgl_list_lock, iflag);
1262 goto out;
1263 }
1264
James Smart895427b2017-02-12 13:52:30 -08001265 pring = phba->sli4_hba.els_wq->pring;
James Smart4f774512009-05-22 14:52:35 -04001266 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1267 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001268 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1269 iflag);
James Smart341af102010-01-26 23:07:37 -05001270 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001271 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001272 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001273 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001274 } else {
James Smart895427b2017-02-12 13:52:30 -08001275 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1276 iflag);
James Smart4f774512009-05-22 14:52:35 -04001277 sglq->state = SGL_FREED;
1278 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001279 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001280 &phba->sli4_hba.lpfc_els_sgl_list);
1281 spin_unlock_irqrestore(
1282 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001283
1284 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001285 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001286 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001287 }
James Smart4f774512009-05-22 14:52:35 -04001288 }
1289
James Smartf358dd02017-02-12 13:52:34 -08001290out:
James Smart4f774512009-05-22 14:52:35 -04001291 /*
1292 * Clean all volatile data fields, preserve iotag and node struct.
1293 */
1294 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001295 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001296 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001297 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1298 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001299 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1300}
1301
James Smart2a9bf3d2010-06-07 15:24:45 -04001302
James Smart4f774512009-05-22 14:52:35 -04001303/**
James Smart3772a992009-05-22 14:50:54 -04001304 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1305 * @phba: Pointer to HBA context object.
1306 * @iocbq: Pointer to driver iocb object.
1307 *
1308 * This function is called with hbalock held to release driver
1309 * iocb object to the iocb pool. The iotag in the iocb object
1310 * does not change for each use of the iocb object. This function
1311 * clears all other fields of the iocb object when it is freed.
1312 **/
1313static void
1314__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1315{
1316 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1317
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001318 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001319
1320 /*
James Smart3772a992009-05-22 14:50:54 -04001321 * Clean all volatile data fields, preserve iotag and node struct.
1322 */
1323 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1324 iocbq->sli4_xritag = NO_XRI;
1325 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1326}
1327
1328/**
James Smart3621a712009-04-06 18:47:14 -04001329 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001330 * @phba: Pointer to HBA context object.
1331 * @iocbq: Pointer to driver iocb object.
1332 *
1333 * This function is called with hbalock held to release driver
1334 * iocb object to the iocb pool. The iotag in the iocb object
1335 * does not change for each use of the iocb object. This function
1336 * clears all other fields of the iocb object when it is freed.
1337 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001338static void
James Smart2e0fef82007-06-17 19:56:36 -05001339__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1340{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001341 lockdep_assert_held(&phba->hbalock);
1342
James Smart3772a992009-05-22 14:50:54 -04001343 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001344 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001345}
1346
James Smarte59058c2008-08-24 21:49:00 -04001347/**
James Smart3621a712009-04-06 18:47:14 -04001348 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001349 * @phba: Pointer to HBA context object.
1350 * @iocbq: Pointer to driver iocb object.
1351 *
1352 * This function is called with no lock held to release the iocb to
1353 * iocb pool.
1354 **/
James Smart2e0fef82007-06-17 19:56:36 -05001355void
1356lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1357{
1358 unsigned long iflags;
1359
1360 /*
1361 * Clean all volatile data fields, preserve iotag and node struct.
1362 */
1363 spin_lock_irqsave(&phba->hbalock, iflags);
1364 __lpfc_sli_release_iocbq(phba, iocbq);
1365 spin_unlock_irqrestore(&phba->hbalock, iflags);
1366}
1367
James Smarte59058c2008-08-24 21:49:00 -04001368/**
James Smarta257bf92009-04-06 18:48:10 -04001369 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1370 * @phba: Pointer to HBA context object.
1371 * @iocblist: List of IOCBs.
1372 * @ulpstatus: ULP status in IOCB command field.
1373 * @ulpWord4: ULP word-4 in IOCB command field.
1374 *
1375 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1376 * on the list by invoking the complete callback function associated with the
1377 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1378 * fields.
1379 **/
1380void
1381lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1382 uint32_t ulpstatus, uint32_t ulpWord4)
1383{
1384 struct lpfc_iocbq *piocb;
1385
1386 while (!list_empty(iocblist)) {
1387 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001388 if (!piocb->iocb_cmpl)
1389 lpfc_sli_release_iocbq(phba, piocb);
1390 else {
1391 piocb->iocb.ulpStatus = ulpstatus;
1392 piocb->iocb.un.ulpWord[4] = ulpWord4;
1393 (piocb->iocb_cmpl) (phba, piocb, piocb);
1394 }
1395 }
1396 return;
1397}
1398
1399/**
James Smart3621a712009-04-06 18:47:14 -04001400 * lpfc_sli_iocb_cmd_type - Get the iocb type
1401 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001402 *
1403 * This function is called by ring event handler function to get the iocb type.
1404 * This function translates the iocb command to an iocb command type used to
1405 * decide the final disposition of each completed IOCB.
1406 * The function returns
1407 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1408 * LPFC_SOL_IOCB if it is a solicited iocb completion
1409 * LPFC_ABORT_IOCB if it is an abort iocb
1410 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1411 *
1412 * The caller is not required to hold any lock.
1413 **/
dea31012005-04-17 16:05:31 -05001414static lpfc_iocb_type
1415lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1416{
1417 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1418
1419 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1420 return 0;
1421
1422 switch (iocb_cmnd) {
1423 case CMD_XMIT_SEQUENCE_CR:
1424 case CMD_XMIT_SEQUENCE_CX:
1425 case CMD_XMIT_BCAST_CN:
1426 case CMD_XMIT_BCAST_CX:
1427 case CMD_ELS_REQUEST_CR:
1428 case CMD_ELS_REQUEST_CX:
1429 case CMD_CREATE_XRI_CR:
1430 case CMD_CREATE_XRI_CX:
1431 case CMD_GET_RPI_CN:
1432 case CMD_XMIT_ELS_RSP_CX:
1433 case CMD_GET_RPI_CR:
1434 case CMD_FCP_IWRITE_CR:
1435 case CMD_FCP_IWRITE_CX:
1436 case CMD_FCP_IREAD_CR:
1437 case CMD_FCP_IREAD_CX:
1438 case CMD_FCP_ICMND_CR:
1439 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001440 case CMD_FCP_TSEND_CX:
1441 case CMD_FCP_TRSP_CX:
1442 case CMD_FCP_TRECEIVE_CX:
1443 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001444 case CMD_ADAPTER_MSG:
1445 case CMD_ADAPTER_DUMP:
1446 case CMD_XMIT_SEQUENCE64_CR:
1447 case CMD_XMIT_SEQUENCE64_CX:
1448 case CMD_XMIT_BCAST64_CN:
1449 case CMD_XMIT_BCAST64_CX:
1450 case CMD_ELS_REQUEST64_CR:
1451 case CMD_ELS_REQUEST64_CX:
1452 case CMD_FCP_IWRITE64_CR:
1453 case CMD_FCP_IWRITE64_CX:
1454 case CMD_FCP_IREAD64_CR:
1455 case CMD_FCP_IREAD64_CX:
1456 case CMD_FCP_ICMND64_CR:
1457 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001458 case CMD_FCP_TSEND64_CX:
1459 case CMD_FCP_TRSP64_CX:
1460 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001461 case CMD_GEN_REQUEST64_CR:
1462 case CMD_GEN_REQUEST64_CX:
1463 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001464 case DSSCMD_IWRITE64_CR:
1465 case DSSCMD_IWRITE64_CX:
1466 case DSSCMD_IREAD64_CR:
1467 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001468 type = LPFC_SOL_IOCB;
1469 break;
1470 case CMD_ABORT_XRI_CN:
1471 case CMD_ABORT_XRI_CX:
1472 case CMD_CLOSE_XRI_CN:
1473 case CMD_CLOSE_XRI_CX:
1474 case CMD_XRI_ABORTED_CX:
1475 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001476 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001477 type = LPFC_ABORT_IOCB;
1478 break;
1479 case CMD_RCV_SEQUENCE_CX:
1480 case CMD_RCV_ELS_REQ_CX:
1481 case CMD_RCV_SEQUENCE64_CX:
1482 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001483 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001484 case CMD_IOCB_RCV_SEQ64_CX:
1485 case CMD_IOCB_RCV_ELS64_CX:
1486 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001487 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001488 type = LPFC_UNSOL_IOCB;
1489 break;
James Smart3163f722008-02-08 18:50:25 -05001490 case CMD_IOCB_XMIT_MSEQ64_CR:
1491 case CMD_IOCB_XMIT_MSEQ64_CX:
1492 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1493 case CMD_IOCB_RCV_ELS_LIST64_CX:
1494 case CMD_IOCB_CLOSE_EXTENDED_CN:
1495 case CMD_IOCB_ABORT_EXTENDED_CN:
1496 case CMD_IOCB_RET_HBQE64_CN:
1497 case CMD_IOCB_FCP_IBIDIR64_CR:
1498 case CMD_IOCB_FCP_IBIDIR64_CX:
1499 case CMD_IOCB_FCP_ITASKMGT64_CX:
1500 case CMD_IOCB_LOGENTRY_CN:
1501 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1502 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001503 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001504 type = LPFC_UNKNOWN_IOCB;
1505 break;
dea31012005-04-17 16:05:31 -05001506 default:
1507 type = LPFC_UNKNOWN_IOCB;
1508 break;
1509 }
1510
1511 return type;
1512}
1513
James Smarte59058c2008-08-24 21:49:00 -04001514/**
James Smart3621a712009-04-06 18:47:14 -04001515 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001516 * @phba: Pointer to HBA context object.
1517 *
1518 * This function is called from SLI initialization code
1519 * to configure every ring of the HBA's SLI interface. The
1520 * caller is not required to hold any lock. This function issues
1521 * a config_ring mailbox command for each ring.
1522 * This function returns zero if successful else returns a negative
1523 * error code.
1524 **/
dea31012005-04-17 16:05:31 -05001525static int
James Smarted957682007-06-17 19:56:37 -05001526lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001527{
1528 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001529 LPFC_MBOXQ_t *pmb;
1530 MAILBOX_t *pmbox;
1531 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001532
James Smarted957682007-06-17 19:56:37 -05001533 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1534 if (!pmb)
1535 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001536 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001537 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001538 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001539 lpfc_config_ring(phba, i, pmb);
1540 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1541 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001542 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001543 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001544 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1545 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001546 rc, pmbox->mbxCommand,
1547 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001548 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001549 ret = -ENXIO;
1550 break;
dea31012005-04-17 16:05:31 -05001551 }
1552 }
James Smarted957682007-06-17 19:56:37 -05001553 mempool_free(pmb, phba->mbox_mem_pool);
1554 return ret;
dea31012005-04-17 16:05:31 -05001555}
1556
James Smarte59058c2008-08-24 21:49:00 -04001557/**
James Smart3621a712009-04-06 18:47:14 -04001558 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001559 * @phba: Pointer to HBA context object.
1560 * @pring: Pointer to driver SLI ring object.
1561 * @piocb: Pointer to the driver iocb object.
1562 *
1563 * This function is called with hbalock held. The function adds the
1564 * new iocb to txcmplq of the given ring. This function always returns
1565 * 0. If this function is called for ELS ring, this function checks if
1566 * there is a vport associated with the ELS command. This function also
1567 * starts els_tmofunc timer if this is an ELS command.
1568 **/
dea31012005-04-17 16:05:31 -05001569static int
James Smart2e0fef82007-06-17 19:56:36 -05001570lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1571 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001572{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001573 lockdep_assert_held(&phba->hbalock);
1574
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001575 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001576
dea31012005-04-17 16:05:31 -05001577 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001578 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08001579 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001580
James Smart92d7f7b2007-06-17 19:56:38 -05001581 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1582 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001583 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1584 BUG_ON(!piocb->vport);
1585 if (!(piocb->vport->load_flag & FC_UNLOADING))
1586 mod_timer(&piocb->vport->els_tmofunc,
1587 jiffies +
1588 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1589 }
dea31012005-04-17 16:05:31 -05001590
James Smart2e0fef82007-06-17 19:56:36 -05001591 return 0;
dea31012005-04-17 16:05:31 -05001592}
1593
James Smarte59058c2008-08-24 21:49:00 -04001594/**
James Smart3621a712009-04-06 18:47:14 -04001595 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001596 * @phba: Pointer to HBA context object.
1597 * @pring: Pointer to driver SLI ring object.
1598 *
1599 * This function is called with hbalock held to get next
1600 * iocb in txq of the given ring. If there is any iocb in
1601 * the txq, the function returns first iocb in the list after
1602 * removing the iocb from the list, else it returns NULL.
1603 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001604struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001605lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001606{
dea31012005-04-17 16:05:31 -05001607 struct lpfc_iocbq *cmd_iocb;
1608
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001609 lockdep_assert_held(&phba->hbalock);
1610
James Smart858c9f62007-06-17 19:56:39 -05001611 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001612 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001613}
1614
James Smarte59058c2008-08-24 21:49:00 -04001615/**
James Smart3621a712009-04-06 18:47:14 -04001616 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001617 * @phba: Pointer to HBA context object.
1618 * @pring: Pointer to driver SLI ring object.
1619 *
1620 * This function is called with hbalock held and the caller must post the
1621 * iocb without releasing the lock. If the caller releases the lock,
1622 * iocb slot returned by the function is not guaranteed to be available.
1623 * The function returns pointer to the next available iocb slot if there
1624 * is available slot in the ring, else it returns NULL.
1625 * If the get index of the ring is ahead of the put index, the function
1626 * will post an error attention event to the worker thread to take the
1627 * HBA to offline state.
1628 **/
dea31012005-04-17 16:05:31 -05001629static IOCB_t *
1630lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1631{
James Smart34b02dc2008-08-24 21:49:55 -04001632 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001633 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001634
1635 lockdep_assert_held(&phba->hbalock);
1636
James Smart7e56aa22012-08-03 12:35:34 -04001637 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1638 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1639 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001640
James Smart7e56aa22012-08-03 12:35:34 -04001641 if (unlikely(pring->sli.sli3.local_getidx ==
1642 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001643
James Smart7e56aa22012-08-03 12:35:34 -04001644 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001645
James Smart7e56aa22012-08-03 12:35:34 -04001646 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001647 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001648 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001649 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001650 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001651 pring->sli.sli3.local_getidx,
1652 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001653
James Smart2e0fef82007-06-17 19:56:36 -05001654 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001655 /*
1656 * All error attention handlers are posted to
1657 * worker thread
1658 */
1659 phba->work_ha |= HA_ERATT;
1660 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001661
James Smart5e9d9b82008-06-14 22:52:53 -04001662 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001663
1664 return NULL;
1665 }
1666
James Smart7e56aa22012-08-03 12:35:34 -04001667 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001668 return NULL;
1669 }
1670
James Smarted957682007-06-17 19:56:37 -05001671 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001672}
1673
James Smarte59058c2008-08-24 21:49:00 -04001674/**
James Smart3621a712009-04-06 18:47:14 -04001675 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001676 * @phba: Pointer to HBA context object.
1677 * @iocbq: Pointer to driver iocb object.
1678 *
1679 * This function gets an iotag for the iocb. If there is no unused iotag and
1680 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1681 * array and assigns a new iotag.
1682 * The function returns the allocated iotag if successful, else returns zero.
1683 * Zero is not a valid iotag.
1684 * The caller is not required to hold any lock.
1685 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001686uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001687lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001688{
James Smart2e0fef82007-06-17 19:56:36 -05001689 struct lpfc_iocbq **new_arr;
1690 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001691 size_t new_len;
1692 struct lpfc_sli *psli = &phba->sli;
1693 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001694
James Smart2e0fef82007-06-17 19:56:36 -05001695 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001696 iotag = psli->last_iotag;
1697 if(++iotag < psli->iocbq_lookup_len) {
1698 psli->last_iotag = iotag;
1699 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001700 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001701 iocbq->iotag = iotag;
1702 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001703 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001704 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1705 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001706 spin_unlock_irq(&phba->hbalock);
Kees Cook6396bb22018-06-12 14:03:40 -07001707 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001708 GFP_KERNEL);
1709 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001710 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001711 old_arr = psli->iocbq_lookup;
1712 if (new_len <= psli->iocbq_lookup_len) {
1713 /* highly unprobable case */
1714 kfree(new_arr);
1715 iotag = psli->last_iotag;
1716 if(++iotag < psli->iocbq_lookup_len) {
1717 psli->last_iotag = iotag;
1718 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001719 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001720 iocbq->iotag = iotag;
1721 return iotag;
1722 }
James Smart2e0fef82007-06-17 19:56:36 -05001723 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001724 return 0;
1725 }
1726 if (psli->iocbq_lookup)
1727 memcpy(new_arr, old_arr,
1728 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001729 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001730 psli->iocbq_lookup = new_arr;
1731 psli->iocbq_lookup_len = new_len;
1732 psli->last_iotag = iotag;
1733 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001734 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001735 iocbq->iotag = iotag;
1736 kfree(old_arr);
1737 return iotag;
1738 }
James Smart8f6d98d2006-08-01 07:34:00 -04001739 } else
James Smart2e0fef82007-06-17 19:56:36 -05001740 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001741
James Smartbc739052010-08-04 16:11:18 -04001742 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001743 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1744 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001745
James Bottomley604a3e32005-10-29 10:28:33 -05001746 return 0;
dea31012005-04-17 16:05:31 -05001747}
1748
James Smarte59058c2008-08-24 21:49:00 -04001749/**
James Smart3621a712009-04-06 18:47:14 -04001750 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001751 * @phba: Pointer to HBA context object.
1752 * @pring: Pointer to driver SLI ring object.
1753 * @iocb: Pointer to iocb slot in the ring.
1754 * @nextiocb: Pointer to driver iocb object which need to be
1755 * posted to firmware.
1756 *
1757 * This function is called with hbalock held to post a new iocb to
1758 * the firmware. This function copies the new iocb to ring iocb slot and
1759 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1760 * a completion call back for this iocb else the function will free the
1761 * iocb object.
1762 **/
dea31012005-04-17 16:05:31 -05001763static void
1764lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1765 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1766{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001767 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001768 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001769 * Set up an iotag
dea31012005-04-17 16:05:31 -05001770 */
James Bottomley604a3e32005-10-29 10:28:33 -05001771 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001772
James Smarte2a0a9d2008-12-04 22:40:02 -05001773
James Smarta58cbd52007-08-02 11:09:43 -04001774 if (pring->ringno == LPFC_ELS_RING) {
1775 lpfc_debugfs_slow_ring_trc(phba,
1776 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1777 *(((uint32_t *) &nextiocb->iocb) + 4),
1778 *(((uint32_t *) &nextiocb->iocb) + 6),
1779 *(((uint32_t *) &nextiocb->iocb) + 7));
1780 }
1781
dea31012005-04-17 16:05:31 -05001782 /*
1783 * Issue iocb command to adapter
1784 */
James Smart92d7f7b2007-06-17 19:56:38 -05001785 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001786 wmb();
1787 pring->stats.iocb_cmd++;
1788
1789 /*
1790 * If there is no completion routine to call, we can release the
1791 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1792 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1793 */
1794 if (nextiocb->iocb_cmpl)
1795 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001796 else
James Smart2e0fef82007-06-17 19:56:36 -05001797 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001798
1799 /*
1800 * Let the HBA know what IOCB slot will be the next one the
1801 * driver will put a command into.
1802 */
James Smart7e56aa22012-08-03 12:35:34 -04001803 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1804 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001805}
1806
James Smarte59058c2008-08-24 21:49:00 -04001807/**
James Smart3621a712009-04-06 18:47:14 -04001808 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001809 * @phba: Pointer to HBA context object.
1810 * @pring: Pointer to driver SLI ring object.
1811 *
1812 * The caller is not required to hold any lock for calling this function.
1813 * This function updates the chip attention bits for the ring to inform firmware
1814 * that there are pending work to be done for this ring and requests an
1815 * interrupt when there is space available in the ring. This function is
1816 * called when the driver is unable to post more iocbs to the ring due
1817 * to unavailability of space in the ring.
1818 **/
dea31012005-04-17 16:05:31 -05001819static void
James Smart2e0fef82007-06-17 19:56:36 -05001820lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001821{
1822 int ringno = pring->ringno;
1823
1824 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1825
1826 wmb();
1827
1828 /*
1829 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1830 * The HBA will tell us when an IOCB entry is available.
1831 */
1832 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1833 readl(phba->CAregaddr); /* flush */
1834
1835 pring->stats.iocb_cmd_full++;
1836}
1837
James Smarte59058c2008-08-24 21:49:00 -04001838/**
James Smart3621a712009-04-06 18:47:14 -04001839 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001840 * @phba: Pointer to HBA context object.
1841 * @pring: Pointer to driver SLI ring object.
1842 *
1843 * This function updates the chip attention register bit for the
1844 * given ring to inform HBA that there is more work to be done
1845 * in this ring. The caller is not required to hold any lock.
1846 **/
dea31012005-04-17 16:05:31 -05001847static void
James Smart2e0fef82007-06-17 19:56:36 -05001848lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001849{
1850 int ringno = pring->ringno;
1851
1852 /*
1853 * Tell the HBA that there is work to do in this ring.
1854 */
James Smart34b02dc2008-08-24 21:49:55 -04001855 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1856 wmb();
1857 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1858 readl(phba->CAregaddr); /* flush */
1859 }
dea31012005-04-17 16:05:31 -05001860}
1861
James Smarte59058c2008-08-24 21:49:00 -04001862/**
James Smart3621a712009-04-06 18:47:14 -04001863 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001864 * @phba: Pointer to HBA context object.
1865 * @pring: Pointer to driver SLI ring object.
1866 *
1867 * This function is called with hbalock held to post pending iocbs
1868 * in the txq to the firmware. This function is called when driver
1869 * detects space available in the ring.
1870 **/
dea31012005-04-17 16:05:31 -05001871static void
James Smart2e0fef82007-06-17 19:56:36 -05001872lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001873{
1874 IOCB_t *iocb;
1875 struct lpfc_iocbq *nextiocb;
1876
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001877 lockdep_assert_held(&phba->hbalock);
1878
dea31012005-04-17 16:05:31 -05001879 /*
1880 * Check to see if:
1881 * (a) there is anything on the txq to send
1882 * (b) link is up
1883 * (c) link attention events can be processed (fcp ring only)
1884 * (d) IOCB processing is not blocked by the outstanding mbox command.
1885 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001886
1887 if (lpfc_is_link_up(phba) &&
1888 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001889 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001890 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001891
1892 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1893 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1894 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1895
1896 if (iocb)
1897 lpfc_sli_update_ring(phba, pring);
1898 else
1899 lpfc_sli_update_full_ring(phba, pring);
1900 }
1901
1902 return;
1903}
1904
James Smarte59058c2008-08-24 21:49:00 -04001905/**
James Smart3621a712009-04-06 18:47:14 -04001906 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001907 * @phba: Pointer to HBA context object.
1908 * @hbqno: HBQ number.
1909 *
1910 * This function is called with hbalock held to get the next
1911 * available slot for the given HBQ. If there is free slot
1912 * available for the HBQ it will return pointer to the next available
1913 * HBQ entry else it will return NULL.
1914 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001915static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001916lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1917{
1918 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1919
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001920 lockdep_assert_held(&phba->hbalock);
1921
James Smarted957682007-06-17 19:56:37 -05001922 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1923 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1924 hbqp->next_hbqPutIdx = 0;
1925
1926 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001927 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001928 uint32_t getidx = le32_to_cpu(raw_index);
1929
1930 hbqp->local_hbqGetIdx = getidx;
1931
1932 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1933 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001934 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001935 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001936 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001937 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001938 hbqp->entry_count);
1939
1940 phba->link_state = LPFC_HBA_ERROR;
1941 return NULL;
1942 }
1943
1944 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1945 return NULL;
1946 }
1947
James Smart51ef4c22007-08-02 11:10:31 -04001948 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1949 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001950}
1951
James Smarte59058c2008-08-24 21:49:00 -04001952/**
James Smart3621a712009-04-06 18:47:14 -04001953 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001954 * @phba: Pointer to HBA context object.
1955 *
1956 * This function is called with no lock held to free all the
1957 * hbq buffers while uninitializing the SLI interface. It also
1958 * frees the HBQ buffers returned by the firmware but not yet
1959 * processed by the upper layers.
1960 **/
James Smarted957682007-06-17 19:56:37 -05001961void
1962lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1963{
James Smart92d7f7b2007-06-17 19:56:38 -05001964 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1965 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001966 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001967 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001968
James Smart51ef4c22007-08-02 11:10:31 -04001969 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001970 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001971 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001972 for (i = 0; i < hbq_count; ++i) {
1973 list_for_each_entry_safe(dmabuf, next_dmabuf,
1974 &phba->hbqs[i].hbq_buffer_list, list) {
1975 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1976 list_del(&hbq_buf->dbuf.list);
1977 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1978 }
James Smarta8adb832007-10-27 13:37:53 -04001979 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001980 }
James Smart3163f722008-02-08 18:50:25 -05001981
1982 /* Mark the HBQs not in use */
1983 phba->hbq_in_use = 0;
1984 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001985}
1986
James Smarte59058c2008-08-24 21:49:00 -04001987/**
James Smart3621a712009-04-06 18:47:14 -04001988 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001989 * @phba: Pointer to HBA context object.
1990 * @hbqno: HBQ number.
1991 * @hbq_buf: Pointer to HBQ buffer.
1992 *
1993 * This function is called with the hbalock held to post a
1994 * hbq buffer to the firmware. If the function finds an empty
1995 * slot in the HBQ, it will post the buffer. The function will return
1996 * pointer to the hbq entry if it successfully post the buffer
1997 * else it will return NULL.
1998 **/
James Smart3772a992009-05-22 14:50:54 -04001999static int
James Smarted957682007-06-17 19:56:37 -05002000lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05002001 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05002002{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002003 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002004 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2005}
2006
2007/**
2008 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2009 * @phba: Pointer to HBA context object.
2010 * @hbqno: HBQ number.
2011 * @hbq_buf: Pointer to HBQ buffer.
2012 *
2013 * This function is called with the hbalock held to post a hbq buffer to the
2014 * firmware. If the function finds an empty slot in the HBQ, it will post the
2015 * buffer and place it on the hbq_buffer_list. The function will return zero if
2016 * it successfully post the buffer else it will return an error.
2017 **/
2018static int
2019lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2020 struct hbq_dmabuf *hbq_buf)
2021{
James Smarted957682007-06-17 19:56:37 -05002022 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05002023 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05002024
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002025 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05002026 /* Get next HBQ entry slot to use */
2027 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2028 if (hbqe) {
2029 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2030
James Smart92d7f7b2007-06-17 19:56:38 -05002031 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2032 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08002033 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05002034 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002035 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2036 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2037 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05002038 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2039 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05002040 /* flush */
James Smarted957682007-06-17 19:56:37 -05002041 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04002042 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04002043 return 0;
2044 } else
2045 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05002046}
2047
James Smart4f774512009-05-22 14:52:35 -04002048/**
2049 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2050 * @phba: Pointer to HBA context object.
2051 * @hbqno: HBQ number.
2052 * @hbq_buf: Pointer to HBQ buffer.
2053 *
2054 * This function is called with the hbalock held to post an RQE to the SLI4
2055 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2056 * the hbq_buffer_list and return zero, otherwise it will return an error.
2057 **/
2058static int
2059lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2060 struct hbq_dmabuf *hbq_buf)
2061{
2062 int rc;
2063 struct lpfc_rqe hrqe;
2064 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08002065 struct lpfc_queue *hrq;
2066 struct lpfc_queue *drq;
2067
2068 if (hbqno != LPFC_ELS_HBQ)
2069 return 1;
2070 hrq = phba->sli4_hba.hdr_rq;
2071 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04002072
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002073 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04002074 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2075 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2076 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2077 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08002078 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04002079 if (rc < 0)
2080 return rc;
James Smart895427b2017-02-12 13:52:30 -08002081 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04002082 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2083 return 0;
2084}
2085
James Smarte59058c2008-08-24 21:49:00 -04002086/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05002087static struct lpfc_hbq_init lpfc_els_hbq = {
2088 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05002089 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05002090 .mask_count = 0,
2091 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04002092 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05002093 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04002094 .init_count = 40,
2095 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05002096};
James Smarted957682007-06-17 19:56:37 -05002097
James Smarte59058c2008-08-24 21:49:00 -04002098/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04002099struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05002100 &lpfc_els_hbq,
2101};
2102
James Smarte59058c2008-08-24 21:49:00 -04002103/**
James Smart3621a712009-04-06 18:47:14 -04002104 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04002105 * @phba: Pointer to HBA context object.
2106 * @hbqno: HBQ number.
2107 * @count: Number of HBQ buffers to be posted.
2108 *
James Smartd7c255b2008-08-24 21:50:00 -04002109 * This function is called with no lock held to post more hbq buffers to the
2110 * given HBQ. The function returns the number of HBQ buffers successfully
2111 * posted.
James Smarte59058c2008-08-24 21:49:00 -04002112 **/
James Smart311464e2007-08-02 11:10:37 -04002113static int
James Smart92d7f7b2007-06-17 19:56:38 -05002114lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2115{
James Smartd7c255b2008-08-24 21:50:00 -04002116 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05002117 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05002118 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04002119 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002120 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04002121 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04002122
James Smartd7c255b2008-08-24 21:50:00 -04002123 if ((phba->hbqs[hbqno].buffer_count + count) >
2124 lpfc_hbq_defs[hbqno]->entry_count)
2125 count = lpfc_hbq_defs[hbqno]->entry_count -
2126 phba->hbqs[hbqno].buffer_count;
2127 if (!count)
2128 return 0;
2129 /* Allocate HBQ entries */
2130 for (i = 0; i < count; i++) {
2131 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2132 if (!hbq_buffer)
2133 break;
2134 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2135 }
James Smart3163f722008-02-08 18:50:25 -05002136 /* Check whether HBQ is still in use */
2137 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002138 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04002139 goto err;
2140 while (!list_empty(&hbq_buf_list)) {
2141 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2142 dbuf.list);
2143 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2144 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04002145 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04002146 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04002147 posted++;
2148 } else
James Smart51ef4c22007-08-02 11:10:31 -04002149 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05002150 }
James Smart3163f722008-02-08 18:50:25 -05002151 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04002152 return posted;
2153err:
2154 spin_unlock_irqrestore(&phba->hbalock, flags);
2155 while (!list_empty(&hbq_buf_list)) {
2156 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2157 dbuf.list);
2158 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2159 }
James Smart92d7f7b2007-06-17 19:56:38 -05002160 return 0;
James Smarted957682007-06-17 19:56:37 -05002161}
2162
James Smarte59058c2008-08-24 21:49:00 -04002163/**
James Smart3621a712009-04-06 18:47:14 -04002164 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04002165 * @phba: Pointer to HBA context object.
2166 * @qno: HBQ number.
2167 *
2168 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04002169 * is called with no lock held. The function returns the number of HBQ entries
2170 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002171 **/
James Smarted957682007-06-17 19:56:37 -05002172int
James Smart92d7f7b2007-06-17 19:56:38 -05002173lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002174{
James Smartdef9c7a2009-12-21 17:02:28 -05002175 if (phba->sli_rev == LPFC_SLI_REV4)
2176 return 0;
2177 else
2178 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2179 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05002180}
2181
James Smarte59058c2008-08-24 21:49:00 -04002182/**
James Smart3621a712009-04-06 18:47:14 -04002183 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002184 * @phba: Pointer to HBA context object.
2185 * @qno: HBQ queue number.
2186 *
2187 * This function is called from SLI initialization code path with
2188 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002189 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002190 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002191static int
James Smart92d7f7b2007-06-17 19:56:38 -05002192lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002193{
James Smartdef9c7a2009-12-21 17:02:28 -05002194 if (phba->sli_rev == LPFC_SLI_REV4)
2195 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002196 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002197 else
2198 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2199 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002200}
2201
James Smarte59058c2008-08-24 21:49:00 -04002202/**
James Smart3772a992009-05-22 14:50:54 -04002203 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2204 * @phba: Pointer to HBA context object.
2205 * @hbqno: HBQ number.
2206 *
2207 * This function removes the first hbq buffer on an hbq list and returns a
2208 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2209 **/
2210static struct hbq_dmabuf *
2211lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2212{
2213 struct lpfc_dmabuf *d_buf;
2214
2215 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2216 if (!d_buf)
2217 return NULL;
2218 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2219}
2220
2221/**
James Smart2d7dbc42017-02-12 13:52:35 -08002222 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2223 * @phba: Pointer to HBA context object.
2224 * @hbqno: HBQ number.
2225 *
2226 * This function removes the first RQ buffer on an RQ buffer list and returns a
2227 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2228 **/
2229static struct rqb_dmabuf *
2230lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2231{
2232 struct lpfc_dmabuf *h_buf;
2233 struct lpfc_rqb *rqbp;
2234
2235 rqbp = hrq->rqbp;
2236 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2237 struct lpfc_dmabuf, list);
2238 if (!h_buf)
2239 return NULL;
2240 rqbp->buffer_count--;
2241 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2242}
2243
2244/**
James Smart3621a712009-04-06 18:47:14 -04002245 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002246 * @phba: Pointer to HBA context object.
2247 * @tag: Tag of the hbq buffer.
2248 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002249 * This function searches for the hbq buffer associated with the given tag in
2250 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2251 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002252 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002253static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002254lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2255{
James Smart92d7f7b2007-06-17 19:56:38 -05002256 struct lpfc_dmabuf *d_buf;
2257 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002258 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002259
James Smart51ef4c22007-08-02 11:10:31 -04002260 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002261 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002262 return NULL;
2263
James Smart3772a992009-05-22 14:50:54 -04002264 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002265 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002266 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002267 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002268 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002269 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002270 }
2271 }
James Smart3772a992009-05-22 14:50:54 -04002272 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002273 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002274 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002275 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002276 return NULL;
James Smarted957682007-06-17 19:56:37 -05002277}
2278
James Smarte59058c2008-08-24 21:49:00 -04002279/**
James Smart3621a712009-04-06 18:47:14 -04002280 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002281 * @phba: Pointer to HBA context object.
2282 * @hbq_buffer: Pointer to HBQ buffer.
2283 *
2284 * This function is called with hbalock. This function gives back
2285 * the hbq buffer to firmware. If the HBQ does not have space to
2286 * post the buffer, it will free the buffer.
2287 **/
James Smarted957682007-06-17 19:56:37 -05002288void
James Smart51ef4c22007-08-02 11:10:31 -04002289lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002290{
2291 uint32_t hbqno;
2292
James Smart51ef4c22007-08-02 11:10:31 -04002293 if (hbq_buffer) {
2294 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002295 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002296 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002297 }
2298}
2299
James Smarte59058c2008-08-24 21:49:00 -04002300/**
James Smart3621a712009-04-06 18:47:14 -04002301 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002302 * @mbxCommand: mailbox command code.
2303 *
2304 * This function is called by the mailbox event handler function to verify
2305 * that the completed mailbox command is a legitimate mailbox command. If the
2306 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2307 * and the mailbox event handler will take the HBA offline.
2308 **/
dea31012005-04-17 16:05:31 -05002309static int
2310lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2311{
2312 uint8_t ret;
2313
2314 switch (mbxCommand) {
2315 case MBX_LOAD_SM:
2316 case MBX_READ_NV:
2317 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002318 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002319 case MBX_RUN_BIU_DIAG:
2320 case MBX_INIT_LINK:
2321 case MBX_DOWN_LINK:
2322 case MBX_CONFIG_LINK:
2323 case MBX_CONFIG_RING:
2324 case MBX_RESET_RING:
2325 case MBX_READ_CONFIG:
2326 case MBX_READ_RCONFIG:
2327 case MBX_READ_SPARM:
2328 case MBX_READ_STATUS:
2329 case MBX_READ_RPI:
2330 case MBX_READ_XRI:
2331 case MBX_READ_REV:
2332 case MBX_READ_LNK_STAT:
2333 case MBX_REG_LOGIN:
2334 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002335 case MBX_CLEAR_LA:
2336 case MBX_DUMP_MEMORY:
2337 case MBX_DUMP_CONTEXT:
2338 case MBX_RUN_DIAGS:
2339 case MBX_RESTART:
2340 case MBX_UPDATE_CFG:
2341 case MBX_DOWN_LOAD:
2342 case MBX_DEL_LD_ENTRY:
2343 case MBX_RUN_PROGRAM:
2344 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002345 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002346 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002347 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002348 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002349 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002350 case MBX_LOAD_AREA:
2351 case MBX_RUN_BIU_DIAG64:
2352 case MBX_CONFIG_PORT:
2353 case MBX_READ_SPARM64:
2354 case MBX_READ_RPI64:
2355 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002356 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002357 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002358 case MBX_SET_DEBUG:
2359 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002360 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002361 case MBX_REG_VPI:
2362 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002363 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002364 case MBX_PORT_CAPABILITIES:
2365 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002366 case MBX_SLI4_CONFIG:
2367 case MBX_SLI4_REQ_FTRS:
2368 case MBX_REG_FCFI:
2369 case MBX_UNREG_FCFI:
2370 case MBX_REG_VFI:
2371 case MBX_UNREG_VFI:
2372 case MBX_INIT_VPI:
2373 case MBX_INIT_VFI:
2374 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002375 case MBX_READ_EVENT_LOG_STATUS:
2376 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002377 case MBX_SECURITY_MGMT:
2378 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002379 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002380 ret = mbxCommand;
2381 break;
2382 default:
2383 ret = MBX_SHUTDOWN;
2384 break;
2385 }
James Smart2e0fef82007-06-17 19:56:36 -05002386 return ret;
dea31012005-04-17 16:05:31 -05002387}
James Smarte59058c2008-08-24 21:49:00 -04002388
2389/**
James Smart3621a712009-04-06 18:47:14 -04002390 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002391 * @phba: Pointer to HBA context object.
2392 * @pmboxq: Pointer to mailbox command.
2393 *
2394 * This is completion handler function for mailbox commands issued from
2395 * lpfc_sli_issue_mbox_wait function. This function is called by the
2396 * mailbox event handler function with no lock held. This function
2397 * will wake up thread waiting on the wait queue pointed by context1
2398 * of the mailbox.
2399 **/
James Smart04c68492009-05-22 14:52:52 -04002400void
James Smart2e0fef82007-06-17 19:56:36 -05002401lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002402{
James Smart858c9f62007-06-17 19:56:39 -05002403 unsigned long drvr_flag;
James Smarte29d74f2018-03-05 12:04:07 -08002404 struct completion *pmbox_done;
dea31012005-04-17 16:05:31 -05002405
2406 /*
James Smarte29d74f2018-03-05 12:04:07 -08002407 * If pmbox_done is empty, the driver thread gave up waiting and
dea31012005-04-17 16:05:31 -05002408 * continued running.
2409 */
James Smart7054a602007-04-25 09:52:34 -04002410 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002411 spin_lock_irqsave(&phba->hbalock, drvr_flag);
James Smarte29d74f2018-03-05 12:04:07 -08002412 pmbox_done = (struct completion *)pmboxq->context3;
2413 if (pmbox_done)
2414 complete(pmbox_done);
James Smart858c9f62007-06-17 19:56:39 -05002415 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002416 return;
2417}
2418
James Smarte59058c2008-08-24 21:49:00 -04002419
2420/**
James Smart3621a712009-04-06 18:47:14 -04002421 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002422 * @phba: Pointer to HBA context object.
2423 * @pmb: Pointer to mailbox object.
2424 *
2425 * This function is the default mailbox completion handler. It
2426 * frees the memory resources associated with the completed mailbox
2427 * command. If the completed command is a REG_LOGIN mailbox command,
2428 * this function will issue a UREG_LOGIN to re-claim the RPI.
2429 **/
dea31012005-04-17 16:05:31 -05002430void
James Smart2e0fef82007-06-17 19:56:36 -05002431lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002432{
James Smartd439d282010-09-29 11:18:45 -04002433 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002434 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002435 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002436 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002437 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002438 int rc;
2439
James Smart3e1f0712018-11-29 16:09:29 -08002440 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
James Smart7054a602007-04-25 09:52:34 -04002441
dea31012005-04-17 16:05:31 -05002442 if (mp) {
2443 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2444 kfree(mp);
2445 }
James Smart7054a602007-04-25 09:52:34 -04002446
2447 /*
2448 * If a REG_LOGIN succeeded after node is destroyed or node
2449 * is in re-discovery driver need to cleanup the RPI.
2450 */
James Smart2e0fef82007-06-17 19:56:36 -05002451 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002452 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2453 !pmb->u.mb.mbxStatus) {
2454 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002455 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002456 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002457 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002458 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002459 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2460 if (rc != MBX_NOT_FINISHED)
2461 return;
2462 }
2463
James Smart695a8142010-01-26 23:08:03 -05002464 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2465 !(phba->pport->load_flag & FC_UNLOADING) &&
2466 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002467 shost = lpfc_shost_from_vport(vport);
2468 spin_lock_irq(shost->host_lock);
2469 vport->vpi_state |= LPFC_VPI_REGISTERED;
2470 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2471 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002472 }
2473
James Smartd439d282010-09-29 11:18:45 -04002474 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -08002475 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -04002476 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002477 pmb->ctx_buf = NULL;
2478 pmb->ctx_ndlp = NULL;
2479 }
2480
2481 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2482 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2483
2484 /* Check to see if there are any deferred events to process */
2485 if (ndlp) {
2486 lpfc_printf_vlog(
2487 vport,
2488 KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2489 "1438 UNREG cmpl deferred mbox x%x "
2490 "on NPort x%x Data: x%x x%x %p\n",
2491 ndlp->nlp_rpi, ndlp->nlp_DID,
2492 ndlp->nlp_flag, ndlp->nlp_defer_did, ndlp);
2493
2494 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2495 (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
James Smart00292e02018-12-13 15:17:55 -08002496 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002497 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2498 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002499 } else {
2500 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002501 }
James Smart9b164062019-03-12 16:30:06 -07002502 pmb->ctx_ndlp = NULL;
James Smartdea16bd2018-11-29 16:09:30 -08002503 }
James Smartd439d282010-09-29 11:18:45 -04002504 }
2505
James Smartdcf2a4e2010-09-29 11:18:53 -04002506 /* Check security permission status on INIT_LINK mailbox command */
2507 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2508 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2509 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2510 "2860 SLI authentication is required "
2511 "for INIT_LINK but has not done yet\n");
2512
James Smart04c68492009-05-22 14:52:52 -04002513 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2514 lpfc_sli4_mbox_cmd_free(phba, pmb);
2515 else
2516 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002517}
James Smartbe6bb942015-04-07 15:07:22 -04002518 /**
2519 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2520 * @phba: Pointer to HBA context object.
2521 * @pmb: Pointer to mailbox object.
2522 *
2523 * This function is the unreg rpi mailbox completion handler. It
2524 * frees the memory resources associated with the completed mailbox
2525 * command. An additional refrenece is put on the ndlp to prevent
2526 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2527 * the unreg mailbox command completes, this routine puts the
2528 * reference back.
2529 *
2530 **/
2531void
2532lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2533{
2534 struct lpfc_vport *vport = pmb->vport;
2535 struct lpfc_nodelist *ndlp;
2536
James Smart3e1f0712018-11-29 16:09:29 -08002537 ndlp = pmb->ctx_ndlp;
James Smartbe6bb942015-04-07 15:07:22 -04002538 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2539 if (phba->sli_rev == LPFC_SLI_REV4 &&
2540 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002541 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002542 LPFC_SLI_INTF_IF_TYPE_2)) {
2543 if (ndlp) {
James Smartdea16bd2018-11-29 16:09:30 -08002544 lpfc_printf_vlog(
2545 vport, KERN_INFO, LOG_MBOX | LOG_SLI,
2546 "0010 UNREG_LOGIN vpi:%x "
2547 "rpi:%x DID:%x defer x%x flg x%x "
2548 "map:%x %p\n",
2549 vport->vpi, ndlp->nlp_rpi,
2550 ndlp->nlp_DID, ndlp->nlp_defer_did,
2551 ndlp->nlp_flag,
2552 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002553 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002554 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002555
2556 /* Check to see if there are any deferred
2557 * events to process
2558 */
2559 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2560 (ndlp->nlp_defer_did !=
2561 NLP_EVT_NOTHING_PENDING)) {
2562 lpfc_printf_vlog(
2563 vport, KERN_INFO, LOG_DISCOVERY,
2564 "4111 UNREG cmpl deferred "
2565 "clr x%x on "
2566 "NPort x%x Data: x%x %p\n",
2567 ndlp->nlp_rpi, ndlp->nlp_DID,
2568 ndlp->nlp_defer_did, ndlp);
James Smart00292e02018-12-13 15:17:55 -08002569 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002570 ndlp->nlp_defer_did =
2571 NLP_EVT_NOTHING_PENDING;
2572 lpfc_issue_els_plogi(
2573 vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002574 } else {
2575 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002576 }
James Smartbe6bb942015-04-07 15:07:22 -04002577 }
2578 }
2579 }
2580
2581 mempool_free(pmb, phba->mbox_mem_pool);
2582}
dea31012005-04-17 16:05:31 -05002583
James Smarte59058c2008-08-24 21:49:00 -04002584/**
James Smart3621a712009-04-06 18:47:14 -04002585 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002586 * @phba: Pointer to HBA context object.
2587 *
2588 * This function is called with no lock held. This function processes all
2589 * the completed mailbox commands and gives it to upper layers. The interrupt
2590 * service routine processes mailbox completion interrupt and adds completed
2591 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2592 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2593 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2594 * function returns the mailbox commands to the upper layer by calling the
2595 * completion handler function of each mailbox.
2596 **/
dea31012005-04-17 16:05:31 -05002597int
James Smart2e0fef82007-06-17 19:56:36 -05002598lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002599{
James Smart92d7f7b2007-06-17 19:56:38 -05002600 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002601 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002602 int rc;
2603 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002604
2605 phba->sli.slistat.mbox_event++;
2606
James Smart92d7f7b2007-06-17 19:56:38 -05002607 /* Get all completed mailboxe buffers into the cmplq */
2608 spin_lock_irq(&phba->hbalock);
2609 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2610 spin_unlock_irq(&phba->hbalock);
2611
dea31012005-04-17 16:05:31 -05002612 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002613 do {
2614 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2615 if (pmb == NULL)
2616 break;
2617
James Smart04c68492009-05-22 14:52:52 -04002618 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002619
James Smart858c9f62007-06-17 19:56:39 -05002620 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2621 if (pmb->vport) {
2622 lpfc_debugfs_disc_trc(pmb->vport,
2623 LPFC_DISC_TRC_MBOX_VPORT,
2624 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2625 (uint32_t)pmbox->mbxCommand,
2626 pmbox->un.varWords[0],
2627 pmbox->un.varWords[1]);
2628 }
2629 else {
2630 lpfc_debugfs_disc_trc(phba->pport,
2631 LPFC_DISC_TRC_MBOX,
2632 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2633 (uint32_t)pmbox->mbxCommand,
2634 pmbox->un.varWords[0],
2635 pmbox->un.varWords[1]);
2636 }
2637 }
2638
dea31012005-04-17 16:05:31 -05002639 /*
2640 * It is a fatal error if unknown mbox command completion.
2641 */
2642 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2643 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002644 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002645 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002646 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002647 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002648 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002649 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002650 lpfc_sli_config_mbox_subsys_get(phba,
2651 pmb),
2652 lpfc_sli_config_mbox_opcode_get(phba,
2653 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002654 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002655 phba->work_hs = HS_FFER3;
2656 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002657 continue;
dea31012005-04-17 16:05:31 -05002658 }
2659
dea31012005-04-17 16:05:31 -05002660 if (pmbox->mbxStatus) {
2661 phba->sli.slistat.mbox_stat_err++;
2662 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2663 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002664 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002665 LOG_MBOX | LOG_SLI,
2666 "(%d):0305 Mbox cmd cmpl "
2667 "error - RETRYing Data: x%x "
2668 "(x%x/x%x) x%x x%x x%x\n",
2669 pmb->vport ? pmb->vport->vpi : 0,
2670 pmbox->mbxCommand,
2671 lpfc_sli_config_mbox_subsys_get(phba,
2672 pmb),
2673 lpfc_sli_config_mbox_opcode_get(phba,
2674 pmb),
2675 pmbox->mbxStatus,
2676 pmbox->un.varWords[0],
2677 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002678 pmbox->mbxStatus = 0;
2679 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002680 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002681 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002682 continue;
dea31012005-04-17 16:05:31 -05002683 }
2684 }
2685
2686 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002687 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002688 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002689 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2690 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002691 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002692 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002693 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2694 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002695 pmb->mbox_cmpl,
2696 *((uint32_t *) pmbox),
2697 pmbox->un.varWords[0],
2698 pmbox->un.varWords[1],
2699 pmbox->un.varWords[2],
2700 pmbox->un.varWords[3],
2701 pmbox->un.varWords[4],
2702 pmbox->un.varWords[5],
2703 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002704 pmbox->un.varWords[7],
2705 pmbox->un.varWords[8],
2706 pmbox->un.varWords[9],
2707 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002708
James Smart92d7f7b2007-06-17 19:56:38 -05002709 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002710 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002711 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002712 return 0;
dea31012005-04-17 16:05:31 -05002713}
James Smart92d7f7b2007-06-17 19:56:38 -05002714
James Smarte59058c2008-08-24 21:49:00 -04002715/**
James Smart3621a712009-04-06 18:47:14 -04002716 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002717 * @phba: Pointer to HBA context object.
2718 * @pring: Pointer to driver SLI ring object.
2719 * @tag: buffer tag.
2720 *
2721 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2722 * is set in the tag the buffer is posted for a particular exchange,
2723 * the function will return the buffer without replacing the buffer.
2724 * If the buffer is for unsolicited ELS or CT traffic, this function
2725 * returns the buffer and also posts another buffer to the firmware.
2726 **/
James Smart76bb24e2007-10-27 13:38:00 -04002727static struct lpfc_dmabuf *
2728lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002729 struct lpfc_sli_ring *pring,
2730 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002731{
James Smart9f1e1b52008-12-04 22:39:40 -05002732 struct hbq_dmabuf *hbq_entry;
2733
James Smart76bb24e2007-10-27 13:38:00 -04002734 if (tag & QUE_BUFTAG_BIT)
2735 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002736 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2737 if (!hbq_entry)
2738 return NULL;
2739 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002740}
James Smart57127f12007-10-27 13:37:05 -04002741
James Smart3772a992009-05-22 14:50:54 -04002742/**
2743 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2744 * @phba: Pointer to HBA context object.
2745 * @pring: Pointer to driver SLI ring object.
2746 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2747 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2748 * @fch_type: the type for the first frame of the sequence.
2749 *
2750 * This function is called with no lock held. This function uses the r_ctl and
2751 * type of the received sequence to find the correct callback function to call
2752 * to process the sequence.
2753 **/
2754static int
2755lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2756 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2757 uint32_t fch_type)
2758{
2759 int i;
2760
James Smartf358dd02017-02-12 13:52:34 -08002761 switch (fch_type) {
2762 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002763 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002764 return 1;
2765 default:
2766 break;
2767 }
2768
James Smart3772a992009-05-22 14:50:54 -04002769 /* unSolicited Responses */
2770 if (pring->prt[0].profile) {
2771 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2772 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2773 saveq);
2774 return 1;
2775 }
2776 /* We must search, based on rctl / type
2777 for the right routine */
2778 for (i = 0; i < pring->num_mask; i++) {
2779 if ((pring->prt[i].rctl == fch_r_ctl) &&
2780 (pring->prt[i].type == fch_type)) {
2781 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2782 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2783 (phba, pring, saveq);
2784 return 1;
2785 }
2786 }
2787 return 0;
2788}
James Smarte59058c2008-08-24 21:49:00 -04002789
2790/**
James Smart3621a712009-04-06 18:47:14 -04002791 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002792 * @phba: Pointer to HBA context object.
2793 * @pring: Pointer to driver SLI ring object.
2794 * @saveq: Pointer to the unsolicited iocb.
2795 *
2796 * This function is called with no lock held by the ring event handler
2797 * when there is an unsolicited iocb posted to the response ring by the
2798 * firmware. This function gets the buffer associated with the iocbs
2799 * and calls the event handler for the ring. This function handles both
2800 * qring buffers and hbq buffers.
2801 * When the function returns 1 the caller can free the iocb object otherwise
2802 * upper layer functions will free the iocb objects.
2803 **/
dea31012005-04-17 16:05:31 -05002804static int
2805lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2806 struct lpfc_iocbq *saveq)
2807{
2808 IOCB_t * irsp;
2809 WORD5 * w5p;
2810 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002811 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002812 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002813
dea31012005-04-17 16:05:31 -05002814 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002815
2816 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2817 if (pring->lpfc_sli_rcv_async_status)
2818 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2819 else
2820 lpfc_printf_log(phba,
2821 KERN_WARNING,
2822 LOG_SLI,
2823 "0316 Ring %d handler: unexpected "
2824 "ASYNC_STATUS iocb received evt_code "
2825 "0x%x\n",
2826 pring->ringno,
2827 irsp->un.asyncstat.evt_code);
2828 return 1;
2829 }
2830
James Smart3163f722008-02-08 18:50:25 -05002831 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2832 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2833 if (irsp->ulpBdeCount > 0) {
2834 dmzbuf = lpfc_sli_get_buff(phba, pring,
2835 irsp->un.ulpWord[3]);
2836 lpfc_in_buf_free(phba, dmzbuf);
2837 }
2838
2839 if (irsp->ulpBdeCount > 1) {
2840 dmzbuf = lpfc_sli_get_buff(phba, pring,
2841 irsp->unsli3.sli3Words[3]);
2842 lpfc_in_buf_free(phba, dmzbuf);
2843 }
2844
2845 if (irsp->ulpBdeCount > 2) {
2846 dmzbuf = lpfc_sli_get_buff(phba, pring,
2847 irsp->unsli3.sli3Words[7]);
2848 lpfc_in_buf_free(phba, dmzbuf);
2849 }
2850
2851 return 1;
2852 }
2853
James Smart92d7f7b2007-06-17 19:56:38 -05002854 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002855 if (irsp->ulpBdeCount != 0) {
2856 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002857 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002858 if (!saveq->context2)
2859 lpfc_printf_log(phba,
2860 KERN_ERR,
2861 LOG_SLI,
2862 "0341 Ring %d Cannot find buffer for "
2863 "an unsolicited iocb. tag 0x%x\n",
2864 pring->ringno,
2865 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002866 }
2867 if (irsp->ulpBdeCount == 2) {
2868 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002869 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002870 if (!saveq->context3)
2871 lpfc_printf_log(phba,
2872 KERN_ERR,
2873 LOG_SLI,
2874 "0342 Ring %d Cannot find buffer for an"
2875 " unsolicited iocb. tag 0x%x\n",
2876 pring->ringno,
2877 irsp->unsli3.sli3Words[7]);
2878 }
2879 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002880 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002881 if (irsp->ulpBdeCount != 0) {
2882 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2883 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002884 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002885 lpfc_printf_log(phba,
2886 KERN_ERR,
2887 LOG_SLI,
2888 "0343 Ring %d Cannot find "
2889 "buffer for an unsolicited iocb"
2890 ". tag 0x%x\n", pring->ringno,
2891 irsp->un.ulpWord[3]);
2892 }
2893 if (irsp->ulpBdeCount == 2) {
2894 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2895 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002896 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002897 lpfc_printf_log(phba,
2898 KERN_ERR,
2899 LOG_SLI,
2900 "0344 Ring %d Cannot find "
2901 "buffer for an unsolicited "
2902 "iocb. tag 0x%x\n",
2903 pring->ringno,
2904 irsp->unsli3.sli3Words[7]);
2905 }
2906 }
James Smart92d7f7b2007-06-17 19:56:38 -05002907 }
James Smart9c2face2008-01-11 01:53:18 -05002908 if (irsp->ulpBdeCount != 0 &&
2909 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2910 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2911 int found = 0;
2912
2913 /* search continue save q for same XRI */
2914 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002915 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2916 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002917 list_add_tail(&saveq->list, &iocbq->list);
2918 found = 1;
2919 break;
2920 }
2921 }
2922 if (!found)
2923 list_add_tail(&saveq->clist,
2924 &pring->iocb_continue_saveq);
2925 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2926 list_del_init(&iocbq->clist);
2927 saveq = iocbq;
2928 irsp = &(saveq->iocb);
2929 } else
2930 return 0;
2931 }
2932 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2933 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2934 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002935 Rctl = FC_RCTL_ELS_REQ;
2936 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002937 } else {
2938 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2939 Rctl = w5p->hcsw.Rctl;
2940 Type = w5p->hcsw.Type;
2941
2942 /* Firmware Workaround */
2943 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2944 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2945 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002946 Rctl = FC_RCTL_ELS_REQ;
2947 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002948 w5p->hcsw.Rctl = Rctl;
2949 w5p->hcsw.Type = Type;
2950 }
2951 }
James Smart92d7f7b2007-06-17 19:56:38 -05002952
James Smart3772a992009-05-22 14:50:54 -04002953 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002954 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002955 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002956 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002957 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002958
James Smart92d7f7b2007-06-17 19:56:38 -05002959 return 1;
dea31012005-04-17 16:05:31 -05002960}
2961
James Smarte59058c2008-08-24 21:49:00 -04002962/**
James Smart3621a712009-04-06 18:47:14 -04002963 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002964 * @phba: Pointer to HBA context object.
2965 * @pring: Pointer to driver SLI ring object.
2966 * @prspiocb: Pointer to response iocb object.
2967 *
2968 * This function looks up the iocb_lookup table to get the command iocb
2969 * corresponding to the given response iocb using the iotag of the
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002970 * response iocb. This function is called with the hbalock held
2971 * for sli3 devices or the ring_lock for sli4 devices.
James Smarte59058c2008-08-24 21:49:00 -04002972 * This function returns the command iocb object if it finds the command
2973 * iocb else returns NULL.
2974 **/
dea31012005-04-17 16:05:31 -05002975static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002976lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2977 struct lpfc_sli_ring *pring,
2978 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002979{
dea31012005-04-17 16:05:31 -05002980 struct lpfc_iocbq *cmd_iocb = NULL;
2981 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002982 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002983
James Bottomley604a3e32005-10-29 10:28:33 -05002984 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002985
James Bottomley604a3e32005-10-29 10:28:33 -05002986 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2987 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002988 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002989 /* remove from txcmpl queue list */
2990 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002991 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08002992 pring->txcmplq_cnt--;
James Smart89533e92016-10-13 15:06:15 -07002993 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002994 }
dea31012005-04-17 16:05:31 -05002995 }
2996
dea31012005-04-17 16:05:31 -05002997 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002998 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05002999 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003000 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05003001 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05003002 return NULL;
3003}
3004
James Smarte59058c2008-08-24 21:49:00 -04003005/**
James Smart3772a992009-05-22 14:50:54 -04003006 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3007 * @phba: Pointer to HBA context object.
3008 * @pring: Pointer to driver SLI ring object.
3009 * @iotag: IOCB tag.
3010 *
3011 * This function looks up the iocb_lookup table to get the command iocb
3012 * corresponding to the given iotag. This function is called with the
3013 * hbalock held.
3014 * This function returns the command iocb object if it finds the command
3015 * iocb else returns NULL.
3016 **/
3017static struct lpfc_iocbq *
3018lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3019 struct lpfc_sli_ring *pring, uint16_t iotag)
3020{
James Smart895427b2017-02-12 13:52:30 -08003021 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04003022
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01003023 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04003024 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3025 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003026 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3027 /* remove from txcmpl queue list */
3028 list_del_init(&cmd_iocb->list);
3029 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003030 pring->txcmplq_cnt--;
James Smart4f2e66c2012-05-09 21:17:07 -04003031 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003032 }
James Smart3772a992009-05-22 14:50:54 -04003033 }
James Smart89533e92016-10-13 15:06:15 -07003034
James Smart3772a992009-05-22 14:50:54 -04003035 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08003036 "0372 iotag x%x lookup error: max iotag (x%x) "
3037 "iocb_flag x%x\n",
3038 iotag, phba->sli.last_iotag,
3039 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04003040 return NULL;
3041}
3042
3043/**
James Smart3621a712009-04-06 18:47:14 -04003044 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04003045 * @phba: Pointer to HBA context object.
3046 * @pring: Pointer to driver SLI ring object.
3047 * @saveq: Pointer to the response iocb to be processed.
3048 *
3049 * This function is called by the ring event handler for non-fcp
3050 * rings when there is a new response iocb in the response ring.
3051 * The caller is not required to hold any locks. This function
3052 * gets the command iocb associated with the response iocb and
3053 * calls the completion handler for the command iocb. If there
3054 * is no completion handler, the function will free the resources
3055 * associated with command iocb. If the response iocb is for
3056 * an already aborted command iocb, the status of the completion
3057 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3058 * This function always returns 1.
3059 **/
dea31012005-04-17 16:05:31 -05003060static int
James Smart2e0fef82007-06-17 19:56:36 -05003061lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003062 struct lpfc_iocbq *saveq)
3063{
James Smart2e0fef82007-06-17 19:56:36 -05003064 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05003065 int rc = 1;
3066 unsigned long iflag;
3067
3068 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003069 if (phba->sli_rev == LPFC_SLI_REV4)
3070 spin_lock_irqsave(&pring->ring_lock, iflag);
3071 else
3072 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003073 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003074 if (phba->sli_rev == LPFC_SLI_REV4)
3075 spin_unlock_irqrestore(&pring->ring_lock, iflag);
3076 else
3077 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart2e0fef82007-06-17 19:56:36 -05003078
dea31012005-04-17 16:05:31 -05003079 if (cmdiocbp) {
3080 if (cmdiocbp->iocb_cmpl) {
3081 /*
James Smartea2151b2008-09-07 11:52:10 -04003082 * If an ELS command failed send an event to mgmt
3083 * application.
3084 */
3085 if (saveq->iocb.ulpStatus &&
3086 (pring->ringno == LPFC_ELS_RING) &&
3087 (cmdiocbp->iocb.ulpCommand ==
3088 CMD_ELS_REQUEST64_CR))
3089 lpfc_send_els_failure_event(phba,
3090 cmdiocbp, saveq);
3091
3092 /*
dea31012005-04-17 16:05:31 -05003093 * Post all ELS completions to the worker thread.
3094 * All other are passed to the completion callback.
3095 */
3096 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05003097 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3098 (cmdiocbp->iocb_flag &
3099 LPFC_DRIVER_ABORTED)) {
3100 spin_lock_irqsave(&phba->hbalock,
3101 iflag);
James Smart07951072007-04-25 09:51:38 -04003102 cmdiocbp->iocb_flag &=
3103 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05003104 spin_unlock_irqrestore(&phba->hbalock,
3105 iflag);
James Smart07951072007-04-25 09:51:38 -04003106 saveq->iocb.ulpStatus =
3107 IOSTAT_LOCAL_REJECT;
3108 saveq->iocb.un.ulpWord[4] =
3109 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05003110
3111 /* Firmware could still be in progress
3112 * of DMAing payload, so don't free data
3113 * buffer till after a hbeat.
3114 */
James Smart341af102010-01-26 23:07:37 -05003115 spin_lock_irqsave(&phba->hbalock,
3116 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05003117 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05003118 spin_unlock_irqrestore(&phba->hbalock,
3119 iflag);
3120 }
James Smart0f65ff62010-02-26 14:14:23 -05003121 if (phba->sli_rev == LPFC_SLI_REV4) {
3122 if (saveq->iocb_flag &
3123 LPFC_EXCHANGE_BUSY) {
3124 /* Set cmdiocb flag for the
3125 * exchange busy so sgl (xri)
3126 * will not be released until
3127 * the abort xri is received
3128 * from hba.
3129 */
3130 spin_lock_irqsave(
3131 &phba->hbalock, iflag);
3132 cmdiocbp->iocb_flag |=
3133 LPFC_EXCHANGE_BUSY;
3134 spin_unlock_irqrestore(
3135 &phba->hbalock, iflag);
3136 }
3137 if (cmdiocbp->iocb_flag &
3138 LPFC_DRIVER_ABORTED) {
3139 /*
3140 * Clear LPFC_DRIVER_ABORTED
3141 * bit in case it was driver
3142 * initiated abort.
3143 */
3144 spin_lock_irqsave(
3145 &phba->hbalock, iflag);
3146 cmdiocbp->iocb_flag &=
3147 ~LPFC_DRIVER_ABORTED;
3148 spin_unlock_irqrestore(
3149 &phba->hbalock, iflag);
3150 cmdiocbp->iocb.ulpStatus =
3151 IOSTAT_LOCAL_REJECT;
3152 cmdiocbp->iocb.un.ulpWord[4] =
3153 IOERR_ABORT_REQUESTED;
3154 /*
3155 * For SLI4, irsiocb contains
3156 * NO_XRI in sli_xritag, it
3157 * shall not affect releasing
3158 * sgl (xri) process.
3159 */
3160 saveq->iocb.ulpStatus =
3161 IOSTAT_LOCAL_REJECT;
3162 saveq->iocb.un.ulpWord[4] =
3163 IOERR_SLI_ABORTED;
3164 spin_lock_irqsave(
3165 &phba->hbalock, iflag);
3166 saveq->iocb_flag |=
3167 LPFC_DELAY_MEM_FREE;
3168 spin_unlock_irqrestore(
3169 &phba->hbalock, iflag);
3170 }
James Smart07951072007-04-25 09:51:38 -04003171 }
dea31012005-04-17 16:05:31 -05003172 }
James Smart2e0fef82007-06-17 19:56:36 -05003173 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003174 } else
3175 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003176 } else {
3177 /*
3178 * Unknown initiating command based on the response iotag.
3179 * This could be the case on the ELS ring because of
3180 * lpfc_els_abort().
3181 */
3182 if (pring->ringno != LPFC_ELS_RING) {
3183 /*
3184 * Ring <ringno> handler: unexpected completion IoTag
3185 * <IoTag>
3186 */
James Smarta257bf92009-04-06 18:48:10 -04003187 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003188 "0322 Ring %d handler: "
3189 "unexpected completion IoTag x%x "
3190 "Data: x%x x%x x%x x%x\n",
3191 pring->ringno,
3192 saveq->iocb.ulpIoTag,
3193 saveq->iocb.ulpStatus,
3194 saveq->iocb.un.ulpWord[4],
3195 saveq->iocb.ulpCommand,
3196 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003197 }
3198 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003199
dea31012005-04-17 16:05:31 -05003200 return rc;
3201}
3202
James Smarte59058c2008-08-24 21:49:00 -04003203/**
James Smart3621a712009-04-06 18:47:14 -04003204 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003205 * @phba: Pointer to HBA context object.
3206 * @pring: Pointer to driver SLI ring object.
3207 *
3208 * This function is called from the iocb ring event handlers when
3209 * put pointer is ahead of the get pointer for a ring. This function signal
3210 * an error attention condition to the worker thread and the worker
3211 * thread will transition the HBA to offline state.
3212 **/
James Smart2e0fef82007-06-17 19:56:36 -05003213static void
3214lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003215{
James Smart34b02dc2008-08-24 21:49:55 -04003216 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003217 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003218 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003219 * rsp ring <portRspMax>
3220 */
3221 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003222 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003223 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003224 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003225 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003226
James Smart2e0fef82007-06-17 19:56:36 -05003227 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003228
3229 /*
3230 * All error attention handlers are posted to
3231 * worker thread
3232 */
3233 phba->work_ha |= HA_ERATT;
3234 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003235
James Smart5e9d9b82008-06-14 22:52:53 -04003236 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003237
3238 return;
3239}
3240
James Smarte59058c2008-08-24 21:49:00 -04003241/**
James Smart3621a712009-04-06 18:47:14 -04003242 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04003243 * @ptr: Pointer to address of HBA context object.
3244 *
3245 * This function is invoked by the Error Attention polling timer when the
3246 * timer times out. It will check the SLI Error Attention register for
3247 * possible attention events. If so, it will post an Error Attention event
3248 * and wake up worker thread to process it. Otherwise, it will set up the
3249 * Error Attention polling timer for the next poll.
3250 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003251void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003252{
3253 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003254 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003255 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003256
Kees Cookf22eb4d2017-09-06 20:24:26 -07003257 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003258
James Smartaa6fbb72012-08-03 12:36:03 -04003259 /* Here we will also keep track of interrupts per sec of the hba */
3260 sli_intr = phba->sli.slistat.sli_intr;
3261
3262 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3263 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3264 sli_intr);
3265 else
3266 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3267
James Smart65791f12016-07-06 12:35:56 -07003268 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3269 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003270 phba->sli.slistat.sli_ips = cnt;
3271
3272 phba->sli.slistat.sli_prev_intr = sli_intr;
3273
James Smart93996272008-08-24 21:50:30 -04003274 /* Check chip HA register for error event */
3275 eratt = lpfc_sli_check_eratt(phba);
3276
3277 if (eratt)
3278 /* Tell the worker thread there is work to do */
3279 lpfc_worker_wake_up(phba);
3280 else
3281 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003282 mod_timer(&phba->eratt_poll,
3283 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003284 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003285 return;
3286}
3287
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003288
James Smarte59058c2008-08-24 21:49:00 -04003289/**
James Smart3621a712009-04-06 18:47:14 -04003290 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003291 * @phba: Pointer to HBA context object.
3292 * @pring: Pointer to driver SLI ring object.
3293 * @mask: Host attention register mask for this ring.
3294 *
3295 * This function is called from the interrupt context when there is a ring
3296 * event for the fcp ring. The caller does not hold any lock.
3297 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003298 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003299 * LE bit set. The function will call the completion handler of the command iocb
3300 * if the response iocb indicates a completion for a command iocb or it is
3301 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3302 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003303 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003304 * to check it explicitly.
3305 */
3306int
James Smart2e0fef82007-06-17 19:56:36 -05003307lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3308 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003309{
James Smart34b02dc2008-08-24 21:49:55 -04003310 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003311 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003312 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003313 struct lpfc_iocbq *cmdiocbq = NULL;
3314 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003315 uint32_t status;
3316 uint32_t portRspPut, portRspMax;
3317 int rc = 1;
3318 lpfc_iocb_type type;
3319 unsigned long iflag;
3320 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003321
James Smart2e0fef82007-06-17 19:56:36 -05003322 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003323 pring->stats.iocb_event++;
3324
dea31012005-04-17 16:05:31 -05003325 /*
3326 * The next available response entry should never exceed the maximum
3327 * entries. If it does, treat it as an adapter hardware error.
3328 */
James Smart7e56aa22012-08-03 12:35:34 -04003329 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003330 portRspPut = le32_to_cpu(pgp->rspPutInx);
3331 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003332 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003333 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003334 return 1;
3335 }
James Smart45ed1192009-10-02 15:17:02 -04003336 if (phba->fcp_ring_in_use) {
3337 spin_unlock_irqrestore(&phba->hbalock, iflag);
3338 return 1;
3339 } else
3340 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003341
3342 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003343 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003344 /*
3345 * Fetch an entry off the ring and copy it into a local data
3346 * structure. The copy involves a byte-swap since the
3347 * network byte order and pci byte orders are different.
3348 */
James Smarted957682007-06-17 19:56:37 -05003349 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003350 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003351
James Smart7e56aa22012-08-03 12:35:34 -04003352 if (++pring->sli.sli3.rspidx >= portRspMax)
3353 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003354
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003355 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3356 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003357 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003358 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003359 irsp = &rspiocbq.iocb;
3360
dea31012005-04-17 16:05:31 -05003361 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3362 pring->stats.iocb_rsp++;
3363 rsp_cmpl++;
3364
3365 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003366 /*
3367 * If resource errors reported from HBA, reduce
3368 * queuedepths of the SCSI device.
3369 */
3370 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003371 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3372 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003373 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003374 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003375 spin_lock_irqsave(&phba->hbalock, iflag);
3376 }
3377
dea31012005-04-17 16:05:31 -05003378 /* Rsp ring <ringno> error: IOCB */
3379 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003380 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003381 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003382 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003383 irsp->un.ulpWord[0],
3384 irsp->un.ulpWord[1],
3385 irsp->un.ulpWord[2],
3386 irsp->un.ulpWord[3],
3387 irsp->un.ulpWord[4],
3388 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003389 *(uint32_t *)&irsp->un1,
3390 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003391 }
3392
3393 switch (type) {
3394 case LPFC_ABORT_IOCB:
3395 case LPFC_SOL_IOCB:
3396 /*
3397 * Idle exchange closed via ABTS from port. No iocb
3398 * resources need to be recovered.
3399 */
3400 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003401 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003402 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003403 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003404 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003405 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003406 break;
3407 }
3408
James Bottomley604a3e32005-10-29 10:28:33 -05003409 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3410 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003411 if (unlikely(!cmdiocbq))
3412 break;
3413 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3414 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3415 if (cmdiocbq->iocb_cmpl) {
3416 spin_unlock_irqrestore(&phba->hbalock, iflag);
3417 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3418 &rspiocbq);
3419 spin_lock_irqsave(&phba->hbalock, iflag);
3420 }
dea31012005-04-17 16:05:31 -05003421 break;
James Smarta4bc3372006-12-02 13:34:16 -05003422 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003423 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003424 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003425 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003426 break;
dea31012005-04-17 16:05:31 -05003427 default:
3428 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3429 char adaptermsg[LPFC_MAX_ADPTMSG];
3430 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3431 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3432 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003433 dev_warn(&((phba->pcidev)->dev),
3434 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003435 phba->brd_no, adaptermsg);
3436 } else {
3437 /* Unknown IOCB command */
3438 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003439 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003440 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003441 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003442 irsp->ulpStatus,
3443 irsp->ulpIoTag,
3444 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003445 }
3446 break;
3447 }
3448
3449 /*
3450 * The response IOCB has been processed. Update the ring
3451 * pointer in SLIM. If the port response put pointer has not
3452 * been updated, sync the pgp->rspPutInx and fetch the new port
3453 * response put pointer.
3454 */
James Smart7e56aa22012-08-03 12:35:34 -04003455 writel(pring->sli.sli3.rspidx,
3456 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003457
James Smart7e56aa22012-08-03 12:35:34 -04003458 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003459 portRspPut = le32_to_cpu(pgp->rspPutInx);
3460 }
3461
3462 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3463 pring->stats.iocb_rsp_full++;
3464 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3465 writel(status, phba->CAregaddr);
3466 readl(phba->CAregaddr);
3467 }
3468 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3469 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3470 pring->stats.iocb_cmd_empty++;
3471
3472 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003473 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003474 lpfc_sli_resume_iocb(phba, pring);
3475
3476 if ((pring->lpfc_sli_cmd_available))
3477 (pring->lpfc_sli_cmd_available) (phba, pring);
3478
3479 }
3480
James Smart45ed1192009-10-02 15:17:02 -04003481 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003482 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003483 return rc;
3484}
3485
James Smarte59058c2008-08-24 21:49:00 -04003486/**
James Smart3772a992009-05-22 14:50:54 -04003487 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3488 * @phba: Pointer to HBA context object.
3489 * @pring: Pointer to driver SLI ring object.
3490 * @rspiocbp: Pointer to driver response IOCB object.
3491 *
3492 * This function is called from the worker thread when there is a slow-path
3493 * response IOCB to process. This function chains all the response iocbs until
3494 * seeing the iocb with the LE bit set. The function will call
3495 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3496 * completion of a command iocb. The function will call the
3497 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3498 * The function frees the resources or calls the completion handler if this
3499 * iocb is an abort completion. The function returns NULL when the response
3500 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3501 * this function shall chain the iocb on to the iocb_continueq and return the
3502 * response iocb passed in.
3503 **/
3504static struct lpfc_iocbq *
3505lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3506 struct lpfc_iocbq *rspiocbp)
3507{
3508 struct lpfc_iocbq *saveq;
3509 struct lpfc_iocbq *cmdiocbp;
3510 struct lpfc_iocbq *next_iocb;
3511 IOCB_t *irsp = NULL;
3512 uint32_t free_saveq;
3513 uint8_t iocb_cmd_type;
3514 lpfc_iocb_type type;
3515 unsigned long iflag;
3516 int rc;
3517
3518 spin_lock_irqsave(&phba->hbalock, iflag);
3519 /* First add the response iocb to the countinueq list */
3520 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3521 pring->iocb_continueq_cnt++;
3522
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003523 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003524 irsp = &rspiocbp->iocb;
3525 if (irsp->ulpLe) {
3526 /*
3527 * By default, the driver expects to free all resources
3528 * associated with this iocb completion.
3529 */
3530 free_saveq = 1;
3531 saveq = list_get_first(&pring->iocb_continueq,
3532 struct lpfc_iocbq, list);
3533 irsp = &(saveq->iocb);
3534 list_del_init(&pring->iocb_continueq);
3535 pring->iocb_continueq_cnt = 0;
3536
3537 pring->stats.iocb_rsp++;
3538
3539 /*
3540 * If resource errors reported from HBA, reduce
3541 * queuedepths of the SCSI device.
3542 */
3543 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003544 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3545 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003546 spin_unlock_irqrestore(&phba->hbalock, iflag);
3547 phba->lpfc_rampdown_queue_depth(phba);
3548 spin_lock_irqsave(&phba->hbalock, iflag);
3549 }
3550
3551 if (irsp->ulpStatus) {
3552 /* Rsp ring <ringno> error: IOCB */
3553 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3554 "0328 Rsp Ring %d error: "
3555 "IOCB Data: "
3556 "x%x x%x x%x x%x "
3557 "x%x x%x x%x x%x "
3558 "x%x x%x x%x x%x "
3559 "x%x x%x x%x x%x\n",
3560 pring->ringno,
3561 irsp->un.ulpWord[0],
3562 irsp->un.ulpWord[1],
3563 irsp->un.ulpWord[2],
3564 irsp->un.ulpWord[3],
3565 irsp->un.ulpWord[4],
3566 irsp->un.ulpWord[5],
3567 *(((uint32_t *) irsp) + 6),
3568 *(((uint32_t *) irsp) + 7),
3569 *(((uint32_t *) irsp) + 8),
3570 *(((uint32_t *) irsp) + 9),
3571 *(((uint32_t *) irsp) + 10),
3572 *(((uint32_t *) irsp) + 11),
3573 *(((uint32_t *) irsp) + 12),
3574 *(((uint32_t *) irsp) + 13),
3575 *(((uint32_t *) irsp) + 14),
3576 *(((uint32_t *) irsp) + 15));
3577 }
3578
3579 /*
3580 * Fetch the IOCB command type and call the correct completion
3581 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3582 * get freed back to the lpfc_iocb_list by the discovery
3583 * kernel thread.
3584 */
3585 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3586 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3587 switch (type) {
3588 case LPFC_SOL_IOCB:
3589 spin_unlock_irqrestore(&phba->hbalock, iflag);
3590 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3591 spin_lock_irqsave(&phba->hbalock, iflag);
3592 break;
3593
3594 case LPFC_UNSOL_IOCB:
3595 spin_unlock_irqrestore(&phba->hbalock, iflag);
3596 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3597 spin_lock_irqsave(&phba->hbalock, iflag);
3598 if (!rc)
3599 free_saveq = 0;
3600 break;
3601
3602 case LPFC_ABORT_IOCB:
3603 cmdiocbp = NULL;
3604 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3605 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3606 saveq);
3607 if (cmdiocbp) {
3608 /* Call the specified completion routine */
3609 if (cmdiocbp->iocb_cmpl) {
3610 spin_unlock_irqrestore(&phba->hbalock,
3611 iflag);
3612 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3613 saveq);
3614 spin_lock_irqsave(&phba->hbalock,
3615 iflag);
3616 } else
3617 __lpfc_sli_release_iocbq(phba,
3618 cmdiocbp);
3619 }
3620 break;
3621
3622 case LPFC_UNKNOWN_IOCB:
3623 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3624 char adaptermsg[LPFC_MAX_ADPTMSG];
3625 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3626 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3627 MAX_MSG_DATA);
3628 dev_warn(&((phba->pcidev)->dev),
3629 "lpfc%d: %s\n",
3630 phba->brd_no, adaptermsg);
3631 } else {
3632 /* Unknown IOCB command */
3633 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3634 "0335 Unknown IOCB "
3635 "command Data: x%x "
3636 "x%x x%x x%x\n",
3637 irsp->ulpCommand,
3638 irsp->ulpStatus,
3639 irsp->ulpIoTag,
3640 irsp->ulpContext);
3641 }
3642 break;
3643 }
3644
3645 if (free_saveq) {
3646 list_for_each_entry_safe(rspiocbp, next_iocb,
3647 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003648 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003649 __lpfc_sli_release_iocbq(phba, rspiocbp);
3650 }
3651 __lpfc_sli_release_iocbq(phba, saveq);
3652 }
3653 rspiocbp = NULL;
3654 }
3655 spin_unlock_irqrestore(&phba->hbalock, iflag);
3656 return rspiocbp;
3657}
3658
3659/**
3660 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003661 * @phba: Pointer to HBA context object.
3662 * @pring: Pointer to driver SLI ring object.
3663 * @mask: Host attention register mask for this ring.
3664 *
James Smart3772a992009-05-22 14:50:54 -04003665 * This routine wraps the actual slow_ring event process routine from the
3666 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003667 **/
James Smart3772a992009-05-22 14:50:54 -04003668void
James Smart2e0fef82007-06-17 19:56:36 -05003669lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3670 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003671{
James Smart3772a992009-05-22 14:50:54 -04003672 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3673}
3674
3675/**
3676 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3677 * @phba: Pointer to HBA context object.
3678 * @pring: Pointer to driver SLI ring object.
3679 * @mask: Host attention register mask for this ring.
3680 *
3681 * This function is called from the worker thread when there is a ring event
3682 * for non-fcp rings. The caller does not hold any lock. The function will
3683 * remove each response iocb in the response ring and calls the handle
3684 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3685 **/
3686static void
3687lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3688 struct lpfc_sli_ring *pring, uint32_t mask)
3689{
James Smart34b02dc2008-08-24 21:49:55 -04003690 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003691 IOCB_t *entry;
3692 IOCB_t *irsp = NULL;
3693 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003694 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003695 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003696 uint32_t status;
dea31012005-04-17 16:05:31 -05003697
James Smart34b02dc2008-08-24 21:49:55 -04003698 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003699 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003700 pring->stats.iocb_event++;
3701
dea31012005-04-17 16:05:31 -05003702 /*
3703 * The next available response entry should never exceed the maximum
3704 * entries. If it does, treat it as an adapter hardware error.
3705 */
James Smart7e56aa22012-08-03 12:35:34 -04003706 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003707 portRspPut = le32_to_cpu(pgp->rspPutInx);
3708 if (portRspPut >= portRspMax) {
3709 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003710 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003711 * rsp ring <portRspMax>
3712 */
James Smarted957682007-06-17 19:56:37 -05003713 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003714 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003715 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003716 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003717
James Smart2e0fef82007-06-17 19:56:36 -05003718 phba->link_state = LPFC_HBA_ERROR;
3719 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003720
3721 phba->work_hs = HS_FFER3;
3722 lpfc_handle_eratt(phba);
3723
James Smart3772a992009-05-22 14:50:54 -04003724 return;
dea31012005-04-17 16:05:31 -05003725 }
3726
3727 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003728 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003729 /*
3730 * Build a completion list and call the appropriate handler.
3731 * The process is to get the next available response iocb, get
3732 * a free iocb from the list, copy the response data into the
3733 * free iocb, insert to the continuation list, and update the
3734 * next response index to slim. This process makes response
3735 * iocb's in the ring available to DMA as fast as possible but
3736 * pays a penalty for a copy operation. Since the iocb is
3737 * only 32 bytes, this penalty is considered small relative to
3738 * the PCI reads for register values and a slim write. When
3739 * the ulpLe field is set, the entire Command has been
3740 * received.
3741 */
James Smarted957682007-06-17 19:56:37 -05003742 entry = lpfc_resp_iocb(phba, pring);
3743
James Smart858c9f62007-06-17 19:56:39 -05003744 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003745 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003746 if (rspiocbp == NULL) {
3747 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003748 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003749 break;
3750 }
3751
James Smarted957682007-06-17 19:56:37 -05003752 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3753 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003754 irsp = &rspiocbp->iocb;
3755
James Smart7e56aa22012-08-03 12:35:34 -04003756 if (++pring->sli.sli3.rspidx >= portRspMax)
3757 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003758
James Smarta58cbd52007-08-02 11:09:43 -04003759 if (pring->ringno == LPFC_ELS_RING) {
3760 lpfc_debugfs_slow_ring_trc(phba,
3761 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3762 *(((uint32_t *) irsp) + 4),
3763 *(((uint32_t *) irsp) + 6),
3764 *(((uint32_t *) irsp) + 7));
3765 }
3766
James Smart7e56aa22012-08-03 12:35:34 -04003767 writel(pring->sli.sli3.rspidx,
3768 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003769
James Smart3772a992009-05-22 14:50:54 -04003770 spin_unlock_irqrestore(&phba->hbalock, iflag);
3771 /* Handle the response IOCB */
3772 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3773 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003774
3775 /*
3776 * If the port response put pointer has not been updated, sync
3777 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3778 * response put pointer.
3779 */
James Smart7e56aa22012-08-03 12:35:34 -04003780 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003781 portRspPut = le32_to_cpu(pgp->rspPutInx);
3782 }
James Smart7e56aa22012-08-03 12:35:34 -04003783 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003784
James Smart92d7f7b2007-06-17 19:56:38 -05003785 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003786 /* At least one response entry has been freed */
3787 pring->stats.iocb_rsp_full++;
3788 /* SET RxRE_RSP in Chip Att register */
3789 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3790 writel(status, phba->CAregaddr);
3791 readl(phba->CAregaddr); /* flush */
3792 }
3793 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3794 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3795 pring->stats.iocb_cmd_empty++;
3796
3797 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003798 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003799 lpfc_sli_resume_iocb(phba, pring);
3800
3801 if ((pring->lpfc_sli_cmd_available))
3802 (pring->lpfc_sli_cmd_available) (phba, pring);
3803
3804 }
3805
James Smart2e0fef82007-06-17 19:56:36 -05003806 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003807 return;
dea31012005-04-17 16:05:31 -05003808}
3809
James Smarte59058c2008-08-24 21:49:00 -04003810/**
James Smart4f774512009-05-22 14:52:35 -04003811 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3812 * @phba: Pointer to HBA context object.
3813 * @pring: Pointer to driver SLI ring object.
3814 * @mask: Host attention register mask for this ring.
3815 *
3816 * This function is called from the worker thread when there is a pending
3817 * ELS response iocb on the driver internal slow-path response iocb worker
3818 * queue. The caller does not hold any lock. The function will remove each
3819 * response iocb from the response worker queue and calls the handle
3820 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3821 **/
3822static void
3823lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3824 struct lpfc_sli_ring *pring, uint32_t mask)
3825{
3826 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003827 struct hbq_dmabuf *dmabuf;
3828 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003829 unsigned long iflag;
James Smart0ef01a22018-09-10 10:30:45 -07003830 int count = 0;
James Smart4f774512009-05-22 14:52:35 -04003831
James Smart45ed1192009-10-02 15:17:02 -04003832 spin_lock_irqsave(&phba->hbalock, iflag);
3833 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3834 spin_unlock_irqrestore(&phba->hbalock, iflag);
3835 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003836 /* Get the response iocb from the head of work queue */
3837 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003838 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003839 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003840 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003841
3842 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3843 case CQE_CODE_COMPL_WQE:
3844 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3845 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003846 /* Translate ELS WCQE to response IOCBQ */
3847 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3848 irspiocbq);
3849 if (irspiocbq)
3850 lpfc_sli_sp_handle_rspiocb(phba, pring,
3851 irspiocbq);
James Smart0ef01a22018-09-10 10:30:45 -07003852 count++;
James Smart4d9ab992009-10-02 15:16:39 -04003853 break;
3854 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003855 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003856 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3857 cq_event);
3858 lpfc_sli4_handle_received_buffer(phba, dmabuf);
James Smart0ef01a22018-09-10 10:30:45 -07003859 count++;
James Smart4d9ab992009-10-02 15:16:39 -04003860 break;
3861 default:
3862 break;
3863 }
James Smart0ef01a22018-09-10 10:30:45 -07003864
3865 /* Limit the number of events to 64 to avoid soft lockups */
3866 if (count == 64)
3867 break;
James Smart4f774512009-05-22 14:52:35 -04003868 }
3869}
3870
3871/**
James Smart3621a712009-04-06 18:47:14 -04003872 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003873 * @phba: Pointer to HBA context object.
3874 * @pring: Pointer to driver SLI ring object.
3875 *
3876 * This function aborts all iocbs in the given ring and frees all the iocb
3877 * objects in txq. This function issues an abort iocb for all the iocb commands
3878 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3879 * the return of this function. The caller is not required to hold any locks.
3880 **/
James Smart2e0fef82007-06-17 19:56:36 -05003881void
dea31012005-04-17 16:05:31 -05003882lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3883{
James Smart2534ba72007-04-25 09:52:20 -04003884 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003885 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003886
James Smart92d7f7b2007-06-17 19:56:38 -05003887 if (pring->ringno == LPFC_ELS_RING) {
3888 lpfc_fabric_abort_hba(phba);
3889 }
3890
dea31012005-04-17 16:05:31 -05003891 /* Error everything on txq and txcmplq
3892 * First do the txq.
3893 */
James Smartdb55fba2014-04-04 13:52:02 -04003894 if (phba->sli_rev >= LPFC_SLI_REV4) {
3895 spin_lock_irq(&pring->ring_lock);
3896 list_splice_init(&pring->txq, &completions);
3897 pring->txq_cnt = 0;
3898 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003899
James Smartdb55fba2014-04-04 13:52:02 -04003900 spin_lock_irq(&phba->hbalock);
3901 /* Next issue ABTS for everything on the txcmplq */
3902 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3903 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3904 spin_unlock_irq(&phba->hbalock);
3905 } else {
3906 spin_lock_irq(&phba->hbalock);
3907 list_splice_init(&pring->txq, &completions);
3908 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003909
James Smartdb55fba2014-04-04 13:52:02 -04003910 /* Next issue ABTS for everything on the txcmplq */
3911 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3912 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3913 spin_unlock_irq(&phba->hbalock);
3914 }
James Smart2534ba72007-04-25 09:52:20 -04003915
James Smarta257bf92009-04-06 18:48:10 -04003916 /* Cancel all the IOCBs from the completions list */
3917 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3918 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003919}
3920
James Smarte59058c2008-08-24 21:49:00 -04003921/**
James Smartdb55fba2014-04-04 13:52:02 -04003922 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3923 * @phba: Pointer to HBA context object.
3924 * @pring: Pointer to driver SLI ring object.
3925 *
3926 * This function aborts all iocbs in FCP rings and frees all the iocb
3927 * objects in txq. This function issues an abort iocb for all the iocb commands
3928 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3929 * the return of this function. The caller is not required to hold any locks.
3930 **/
3931void
3932lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3933{
3934 struct lpfc_sli *psli = &phba->sli;
3935 struct lpfc_sli_ring *pring;
3936 uint32_t i;
3937
3938 /* Look on all the FCP Rings for the iotag */
3939 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08003940 for (i = 0; i < phba->cfg_hdw_queue; i++) {
3941 pring = phba->sli4_hba.hdwq[i].fcp_wq->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003942 lpfc_sli_abort_iocb_ring(phba, pring);
3943 }
3944 } else {
James Smart895427b2017-02-12 13:52:30 -08003945 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003946 lpfc_sli_abort_iocb_ring(phba, pring);
3947 }
3948}
3949
James Smart895427b2017-02-12 13:52:30 -08003950/**
James Smart3621a712009-04-06 18:47:14 -04003951 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003952 * @phba: Pointer to HBA context object.
3953 *
3954 * This function flushes all iocbs in the fcp ring and frees all the iocb
3955 * objects in txq and txcmplq. This function will not issue abort iocbs
3956 * for all the iocb commands in txcmplq, they will just be returned with
3957 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3958 * slot has been permanently disabled.
3959 **/
3960void
3961lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3962{
3963 LIST_HEAD(txq);
3964 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003965 struct lpfc_sli *psli = &phba->sli;
3966 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003967 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08003968 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04003969
3970 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003971 /* Indicate the I/O queues are flushed */
3972 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003973 spin_unlock_irq(&phba->hbalock);
3974
James Smartdb55fba2014-04-04 13:52:02 -04003975 /* Look on all the FCP Rings for the iotag */
3976 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08003977 for (i = 0; i < phba->cfg_hdw_queue; i++) {
3978 pring = phba->sli4_hba.hdwq[i].fcp_wq->pring;
James Smarta8e497d2008-08-24 21:50:11 -04003979
James Smartdb55fba2014-04-04 13:52:02 -04003980 spin_lock_irq(&pring->ring_lock);
3981 /* Retrieve everything on txq */
3982 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08003983 list_for_each_entry_safe(piocb, next_iocb,
3984 &pring->txcmplq, list)
3985 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04003986 /* Retrieve everything on the txcmplq */
3987 list_splice_init(&pring->txcmplq, &txcmplq);
3988 pring->txq_cnt = 0;
3989 pring->txcmplq_cnt = 0;
3990 spin_unlock_irq(&pring->ring_lock);
3991
3992 /* Flush the txq */
3993 lpfc_sli_cancel_iocbs(phba, &txq,
3994 IOSTAT_LOCAL_REJECT,
3995 IOERR_SLI_DOWN);
3996 /* Flush the txcmpq */
3997 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3998 IOSTAT_LOCAL_REJECT,
3999 IOERR_SLI_DOWN);
4000 }
4001 } else {
James Smart895427b2017-02-12 13:52:30 -08004002 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004003
4004 spin_lock_irq(&phba->hbalock);
4005 /* Retrieve everything on txq */
4006 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004007 list_for_each_entry_safe(piocb, next_iocb,
4008 &pring->txcmplq, list)
4009 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004010 /* Retrieve everything on the txcmplq */
4011 list_splice_init(&pring->txcmplq, &txcmplq);
4012 pring->txq_cnt = 0;
4013 pring->txcmplq_cnt = 0;
4014 spin_unlock_irq(&phba->hbalock);
4015
4016 /* Flush the txq */
4017 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4018 IOERR_SLI_DOWN);
4019 /* Flush the txcmpq */
4020 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4021 IOERR_SLI_DOWN);
4022 }
James Smarta8e497d2008-08-24 21:50:11 -04004023}
4024
4025/**
James Smart895427b2017-02-12 13:52:30 -08004026 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
4027 * @phba: Pointer to HBA context object.
4028 *
4029 * This function flushes all wqes in the nvme rings and frees all resources
4030 * in the txcmplq. This function does not issue abort wqes for the IO
4031 * commands in txcmplq, they will just be returned with
4032 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4033 * slot has been permanently disabled.
4034 **/
4035void
4036lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
4037{
4038 LIST_HEAD(txcmplq);
4039 struct lpfc_sli_ring *pring;
4040 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004041 struct lpfc_iocbq *piocb, *next_iocb;
James Smart895427b2017-02-12 13:52:30 -08004042
James Smartcdb42be2019-01-28 11:14:21 -08004043 if ((phba->sli_rev < LPFC_SLI_REV4) ||
4044 !(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
James Smart895427b2017-02-12 13:52:30 -08004045 return;
4046
4047 /* Hint to other driver operations that a flush is in progress. */
4048 spin_lock_irq(&phba->hbalock);
4049 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
4050 spin_unlock_irq(&phba->hbalock);
4051
4052 /* Cycle through all NVME rings and complete each IO with
4053 * a local driver reason code. This is a flush so no
4054 * abort exchange to FW.
4055 */
James Smartcdb42be2019-01-28 11:14:21 -08004056 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4057 pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
James Smart895427b2017-02-12 13:52:30 -08004058
James Smart895427b2017-02-12 13:52:30 -08004059 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -08004060 list_for_each_entry_safe(piocb, next_iocb,
4061 &pring->txcmplq, list)
4062 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4063 /* Retrieve everything on the txcmplq */
James Smart895427b2017-02-12 13:52:30 -08004064 list_splice_init(&pring->txcmplq, &txcmplq);
4065 pring->txcmplq_cnt = 0;
4066 spin_unlock_irq(&pring->ring_lock);
4067
4068 /* Flush the txcmpq &&&PAE */
4069 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4070 IOSTAT_LOCAL_REJECT,
4071 IOERR_SLI_DOWN);
4072 }
4073}
4074
4075/**
James Smart3772a992009-05-22 14:50:54 -04004076 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004077 * @phba: Pointer to HBA context object.
4078 * @mask: Bit mask to be checked.
4079 *
4080 * This function reads the host status register and compares
4081 * with the provided bit mask to check if HBA completed
4082 * the restart. This function will wait in a loop for the
4083 * HBA to complete restart. If the HBA does not restart within
4084 * 15 iterations, the function will reset the HBA again. The
4085 * function returns 1 when HBA fail to restart otherwise returns
4086 * zero.
4087 **/
James Smart3772a992009-05-22 14:50:54 -04004088static int
4089lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004090{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004091 uint32_t status;
4092 int i = 0;
4093 int retval = 0;
dea31012005-04-17 16:05:31 -05004094
Jamie Wellnitz41415862006-02-28 19:25:27 -05004095 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004096 if (lpfc_readl(phba->HSregaddr, &status))
4097 return 1;
dea31012005-04-17 16:05:31 -05004098
Jamie Wellnitz41415862006-02-28 19:25:27 -05004099 /*
4100 * Check status register every 100ms for 5 retries, then every
4101 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4102 * every 2.5 sec for 4.
4103 * Break our of the loop if errors occurred during init.
4104 */
4105 while (((status & mask) != mask) &&
4106 !(status & HS_FFERM) &&
4107 i++ < 20) {
dea31012005-04-17 16:05:31 -05004108
Jamie Wellnitz41415862006-02-28 19:25:27 -05004109 if (i <= 5)
4110 msleep(10);
4111 else if (i <= 10)
4112 msleep(500);
4113 else
4114 msleep(2500);
dea31012005-04-17 16:05:31 -05004115
Jamie Wellnitz41415862006-02-28 19:25:27 -05004116 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004117 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004118 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004119 lpfc_sli_brdrestart(phba);
4120 }
4121 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004122 if (lpfc_readl(phba->HSregaddr, &status)) {
4123 retval = 1;
4124 break;
4125 }
dea31012005-04-17 16:05:31 -05004126 }
dea31012005-04-17 16:05:31 -05004127
Jamie Wellnitz41415862006-02-28 19:25:27 -05004128 /* Check to see if any errors occurred during init */
4129 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05004130 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4131 "2751 Adapter failed to restart, "
4132 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4133 status,
4134 readl(phba->MBslimaddr + 0xa8),
4135 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004136 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004137 retval = 1;
4138 }
dea31012005-04-17 16:05:31 -05004139
Jamie Wellnitz41415862006-02-28 19:25:27 -05004140 return retval;
dea31012005-04-17 16:05:31 -05004141}
4142
James Smartda0436e2009-05-22 14:51:39 -04004143/**
4144 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4145 * @phba: Pointer to HBA context object.
4146 * @mask: Bit mask to be checked.
4147 *
4148 * This function checks the host status register to check if HBA is
4149 * ready. This function will wait in a loop for the HBA to be ready
4150 * If the HBA is not ready , the function will will reset the HBA PCI
4151 * function again. The function returns 1 when HBA fail to be ready
4152 * otherwise returns zero.
4153 **/
4154static int
4155lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4156{
4157 uint32_t status;
4158 int retval = 0;
4159
4160 /* Read the HBA Host Status Register */
4161 status = lpfc_sli4_post_status_check(phba);
4162
4163 if (status) {
4164 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4165 lpfc_sli_brdrestart(phba);
4166 status = lpfc_sli4_post_status_check(phba);
4167 }
4168
4169 /* Check to see if any errors occurred during init */
4170 if (status) {
4171 phba->link_state = LPFC_HBA_ERROR;
4172 retval = 1;
4173 } else
4174 phba->sli4_hba.intr_enable = 0;
4175
4176 return retval;
4177}
4178
4179/**
4180 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4181 * @phba: Pointer to HBA context object.
4182 * @mask: Bit mask to be checked.
4183 *
4184 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4185 * from the API jump table function pointer from the lpfc_hba struct.
4186 **/
4187int
4188lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4189{
4190 return phba->lpfc_sli_brdready(phba, mask);
4191}
4192
James Smart92908312006-03-07 15:04:13 -05004193#define BARRIER_TEST_PATTERN (0xdeadbeef)
4194
James Smarte59058c2008-08-24 21:49:00 -04004195/**
James Smart3621a712009-04-06 18:47:14 -04004196 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004197 * @phba: Pointer to HBA context object.
4198 *
James Smart1b511972011-12-13 13:23:09 -05004199 * This function is called before resetting an HBA. This function is called
4200 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004201 **/
James Smart2e0fef82007-06-17 19:56:36 -05004202void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004203{
James Smart65a29c12006-07-06 15:50:50 -04004204 uint32_t __iomem *resp_buf;
4205 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004206 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004207 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004208 int i;
4209 uint8_t hdrtype;
4210
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004211 lockdep_assert_held(&phba->hbalock);
4212
James Smart92908312006-03-07 15:04:13 -05004213 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4214 if (hdrtype != 0x80 ||
4215 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4216 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4217 return;
4218
4219 /*
4220 * Tell the other part of the chip to suspend temporarily all
4221 * its DMA activity.
4222 */
James Smart65a29c12006-07-06 15:50:50 -04004223 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004224
4225 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004226 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4227 return;
James Smart92908312006-03-07 15:04:13 -05004228 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4229 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004230 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004231
James Smart9940b972011-03-11 16:06:12 -05004232 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4233 return;
4234 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004235 /* Clear Chip error bit */
4236 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004237 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004238 }
4239
4240 mbox = 0;
4241 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4242 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4243
4244 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004245 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004246 writel(mbox, mbox_buf);
4247
James Smart9940b972011-03-11 16:06:12 -05004248 for (i = 0; i < 50; i++) {
4249 if (lpfc_readl((resp_buf + 1), &resp_data))
4250 return;
4251 if (resp_data != ~(BARRIER_TEST_PATTERN))
4252 mdelay(1);
4253 else
4254 break;
4255 }
4256 resp_data = 0;
4257 if (lpfc_readl((resp_buf + 1), &resp_data))
4258 return;
4259 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004260 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004261 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004262 goto restore_hc;
4263 else
4264 goto clear_errat;
4265 }
4266
4267 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004268 resp_data = 0;
4269 for (i = 0; i < 500; i++) {
4270 if (lpfc_readl(resp_buf, &resp_data))
4271 return;
4272 if (resp_data != mbox)
4273 mdelay(1);
4274 else
4275 break;
4276 }
James Smart92908312006-03-07 15:04:13 -05004277
4278clear_errat:
4279
James Smart9940b972011-03-11 16:06:12 -05004280 while (++i < 500) {
4281 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4282 return;
4283 if (!(ha_copy & HA_ERATT))
4284 mdelay(1);
4285 else
4286 break;
4287 }
James Smart92908312006-03-07 15:04:13 -05004288
4289 if (readl(phba->HAregaddr) & HA_ERATT) {
4290 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004291 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004292 }
4293
4294restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004295 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004296 writel(hc_copy, phba->HCregaddr);
4297 readl(phba->HCregaddr); /* flush */
4298}
4299
James Smarte59058c2008-08-24 21:49:00 -04004300/**
James Smart3621a712009-04-06 18:47:14 -04004301 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004302 * @phba: Pointer to HBA context object.
4303 *
4304 * This function issues a kill_board mailbox command and waits for
4305 * the error attention interrupt. This function is called for stopping
4306 * the firmware processing. The caller is not required to hold any
4307 * locks. This function calls lpfc_hba_down_post function to free
4308 * any pending commands after the kill. The function will return 1 when it
4309 * fails to kill the board else will return 0.
4310 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004311int
James Smart2e0fef82007-06-17 19:56:36 -05004312lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004313{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004314 struct lpfc_sli *psli;
4315 LPFC_MBOXQ_t *pmb;
4316 uint32_t status;
4317 uint32_t ha_copy;
4318 int retval;
4319 int i = 0;
4320
4321 psli = &phba->sli;
4322
4323 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004324 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004325 "0329 Kill HBA Data: x%x x%x\n",
4326 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004327
James Smart98c9ea52007-10-27 13:37:33 -04004328 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4329 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004330 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004331
4332 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004333 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004334 if (lpfc_readl(phba->HCregaddr, &status)) {
4335 spin_unlock_irq(&phba->hbalock);
4336 mempool_free(pmb, phba->mbox_mem_pool);
4337 return 1;
4338 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004339 status &= ~HC_ERINT_ENA;
4340 writel(status, phba->HCregaddr);
4341 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004342 phba->link_flag |= LS_IGNORE_ERATT;
4343 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004344
4345 lpfc_kill_board(phba, pmb);
4346 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4347 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4348
4349 if (retval != MBX_SUCCESS) {
4350 if (retval != MBX_BUSY)
4351 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004352 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4353 "2752 KILL_BOARD command failed retval %d\n",
4354 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004355 spin_lock_irq(&phba->hbalock);
4356 phba->link_flag &= ~LS_IGNORE_ERATT;
4357 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004358 return 1;
4359 }
4360
James Smartf4b4c682009-05-22 14:53:12 -04004361 spin_lock_irq(&phba->hbalock);
4362 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4363 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004364
Jamie Wellnitz41415862006-02-28 19:25:27 -05004365 mempool_free(pmb, phba->mbox_mem_pool);
4366
4367 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4368 * attention every 100ms for 3 seconds. If we don't get ERATT after
4369 * 3 seconds we still set HBA_ERROR state because the status of the
4370 * board is now undefined.
4371 */
James Smart9940b972011-03-11 16:06:12 -05004372 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4373 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004374 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4375 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004376 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4377 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004378 }
4379
4380 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004381 if (ha_copy & HA_ERATT) {
4382 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004383 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004384 }
James Smart2e0fef82007-06-17 19:56:36 -05004385 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004386 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004387 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004388 phba->link_flag &= ~LS_IGNORE_ERATT;
4389 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004390
Jamie Wellnitz41415862006-02-28 19:25:27 -05004391 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004392 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004393
James Smart2e0fef82007-06-17 19:56:36 -05004394 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004395}
4396
James Smarte59058c2008-08-24 21:49:00 -04004397/**
James Smart3772a992009-05-22 14:50:54 -04004398 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004399 * @phba: Pointer to HBA context object.
4400 *
4401 * This function resets the HBA by writing HC_INITFF to the control
4402 * register. After the HBA resets, this function resets all the iocb ring
4403 * indices. This function disables PCI layer parity checking during
4404 * the reset.
4405 * This function returns 0 always.
4406 * The caller is not required to hold any locks.
4407 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004408int
James Smart2e0fef82007-06-17 19:56:36 -05004409lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004410{
4411 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004412 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004413 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004414 int i;
dea31012005-04-17 16:05:31 -05004415
Jamie Wellnitz41415862006-02-28 19:25:27 -05004416 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004417
Jamie Wellnitz41415862006-02-28 19:25:27 -05004418 /* Reset HBA */
4419 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004420 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004421 (phba->pport) ? phba->pport->port_state : 0,
4422 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004423
4424 /* perform board reset */
4425 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004426 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004427 if (phba->pport) {
4428 phba->pport->fc_myDID = 0;
4429 phba->pport->fc_prevDID = 0;
4430 }
dea31012005-04-17 16:05:31 -05004431
Jamie Wellnitz41415862006-02-28 19:25:27 -05004432 /* Turn off parity checking and serr during the physical reset */
James Smart32a93102019-03-12 16:30:13 -07004433 if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
4434 return -EIO;
4435
Jamie Wellnitz41415862006-02-28 19:25:27 -05004436 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4437 (cfg_value &
4438 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4439
James Smart3772a992009-05-22 14:50:54 -04004440 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4441
Jamie Wellnitz41415862006-02-28 19:25:27 -05004442 /* Now toggle INITFF bit in the Host Control Register */
4443 writel(HC_INITFF, phba->HCregaddr);
4444 mdelay(1);
4445 readl(phba->HCregaddr); /* flush */
4446 writel(0, phba->HCregaddr);
4447 readl(phba->HCregaddr); /* flush */
4448
4449 /* Restore PCI cmd register */
4450 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004451
4452 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004453 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004454 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004455 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004456 pring->sli.sli3.rspidx = 0;
4457 pring->sli.sli3.next_cmdidx = 0;
4458 pring->sli.sli3.local_getidx = 0;
4459 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004460 pring->missbufcnt = 0;
4461 }
dea31012005-04-17 16:05:31 -05004462
James Smart2e0fef82007-06-17 19:56:36 -05004463 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004464 return 0;
4465}
4466
James Smarte59058c2008-08-24 21:49:00 -04004467/**
James Smartda0436e2009-05-22 14:51:39 -04004468 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4469 * @phba: Pointer to HBA context object.
4470 *
4471 * This function resets a SLI4 HBA. This function disables PCI layer parity
4472 * checking during resets the device. The caller is not required to hold
4473 * any locks.
4474 *
4475 * This function returns 0 always.
4476 **/
4477int
4478lpfc_sli4_brdreset(struct lpfc_hba *phba)
4479{
4480 struct lpfc_sli *psli = &phba->sli;
4481 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004482 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004483
4484 /* Reset HBA */
4485 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004486 "0295 Reset HBA Data: x%x x%x x%x\n",
4487 phba->pport->port_state, psli->sli_flag,
4488 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004489
4490 /* perform board reset */
4491 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004492 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004493 phba->pport->fc_myDID = 0;
4494 phba->pport->fc_prevDID = 0;
4495
James Smartda0436e2009-05-22 14:51:39 -04004496 spin_lock_irq(&phba->hbalock);
4497 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4498 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004499 spin_unlock_irq(&phba->hbalock);
4500
James Smart02936352014-04-04 13:52:12 -04004501 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4502 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4503 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4504 return rc;
4505 }
4506
James Smartda0436e2009-05-22 14:51:39 -04004507 /* Now physically reset the device */
4508 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4509 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004510
4511 /* Turn off parity checking and serr during the physical reset */
James Smart32a93102019-03-12 16:30:13 -07004512 if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
4513 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4514 "3205 PCI read Config failed\n");
4515 return -EIO;
4516 }
4517
James Smartbe858b62010-12-15 17:57:20 -05004518 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4519 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4520
James Smart88318812012-09-29 11:29:29 -04004521 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004522 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004523
James Smartbe858b62010-12-15 17:57:20 -05004524 /* Restore PCI cmd register */
4525 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4526
James Smart27b01b82012-05-09 21:19:44 -04004527 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004528}
4529
4530/**
4531 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004532 * @phba: Pointer to HBA context object.
4533 *
4534 * This function is called in the SLI initialization code path to
4535 * restart the HBA. The caller is not required to hold any lock.
4536 * This function writes MBX_RESTART mailbox command to the SLIM and
4537 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4538 * function to free any pending commands. The function enables
4539 * POST only during the first initialization. The function returns zero.
4540 * The function does not guarantee completion of MBX_RESTART mailbox
4541 * command before the return of this function.
4542 **/
James Smartda0436e2009-05-22 14:51:39 -04004543static int
4544lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004545{
4546 MAILBOX_t *mb;
4547 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004548 volatile uint32_t word0;
4549 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004550 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004551
James Smart2e0fef82007-06-17 19:56:36 -05004552 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004553
James Smart0d878412009-10-02 15:16:56 -04004554 /* Take PCIe device Advanced Error Reporting (AER) state */
4555 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4556
Jamie Wellnitz41415862006-02-28 19:25:27 -05004557 psli = &phba->sli;
4558
4559 /* Restart HBA */
4560 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004561 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004562 (phba->pport) ? phba->pport->port_state : 0,
4563 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004564
4565 word0 = 0;
4566 mb = (MAILBOX_t *) &word0;
4567 mb->mbxCommand = MBX_RESTART;
4568 mb->mbxHc = 1;
4569
James Smart92908312006-03-07 15:04:13 -05004570 lpfc_reset_barrier(phba);
4571
Jamie Wellnitz41415862006-02-28 19:25:27 -05004572 to_slim = phba->MBslimaddr;
4573 writel(*(uint32_t *) mb, to_slim);
4574 readl(to_slim); /* flush */
4575
4576 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004577 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004578 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004579 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004580 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004581 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004582 writel(*(uint32_t *) mb, to_slim);
4583 readl(to_slim); /* flush */
4584
4585 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004586 if (phba->pport)
4587 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004588 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004589 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004590 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004591
James Smart64ba8812006-08-02 15:24:34 -04004592 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004593 psli->stats_start = ktime_get_seconds();
James Smart64ba8812006-08-02 15:24:34 -04004594
James Smarteaf15d52008-12-04 22:39:29 -05004595 /* Give the INITFF and Post time to settle. */
4596 mdelay(100);
dea31012005-04-17 16:05:31 -05004597
James Smart0d878412009-10-02 15:16:56 -04004598 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4599 if (hba_aer_enabled)
4600 pci_disable_pcie_error_reporting(phba->pcidev);
4601
Jamie Wellnitz41415862006-02-28 19:25:27 -05004602 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004603
4604 return 0;
4605}
4606
James Smarte59058c2008-08-24 21:49:00 -04004607/**
James Smartda0436e2009-05-22 14:51:39 -04004608 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4609 * @phba: Pointer to HBA context object.
4610 *
4611 * This function is called in the SLI initialization code path to restart
4612 * a SLI4 HBA. The caller is not required to hold any lock.
4613 * At the end of the function, it calls lpfc_hba_down_post function to
4614 * free any pending commands.
4615 **/
4616static int
4617lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4618{
4619 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004620 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004621 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004622
4623 /* Restart HBA */
4624 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4625 "0296 Restart HBA Data: x%x x%x\n",
4626 phba->pport->port_state, psli->sli_flag);
4627
James Smart75baf692010-06-08 18:31:21 -04004628 /* Take PCIe device Advanced Error Reporting (AER) state */
4629 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4630
James Smart27b01b82012-05-09 21:19:44 -04004631 rc = lpfc_sli4_brdreset(phba);
James Smart5a9eeff2018-11-29 16:09:32 -08004632 if (rc)
4633 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004634
4635 spin_lock_irq(&phba->hbalock);
4636 phba->pport->stopped = 0;
4637 phba->link_state = LPFC_INIT_START;
4638 phba->hba_flag = 0;
4639 spin_unlock_irq(&phba->hbalock);
4640
4641 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004642 psli->stats_start = ktime_get_seconds();
James Smartda0436e2009-05-22 14:51:39 -04004643
James Smart75baf692010-06-08 18:31:21 -04004644 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4645 if (hba_aer_enabled)
4646 pci_disable_pcie_error_reporting(phba->pcidev);
4647
James Smartda0436e2009-05-22 14:51:39 -04004648 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004649 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004650
James Smart27b01b82012-05-09 21:19:44 -04004651 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004652}
4653
4654/**
4655 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4656 * @phba: Pointer to HBA context object.
4657 *
4658 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4659 * API jump table function pointer from the lpfc_hba struct.
4660**/
4661int
4662lpfc_sli_brdrestart(struct lpfc_hba *phba)
4663{
4664 return phba->lpfc_sli_brdrestart(phba);
4665}
4666
4667/**
James Smart3621a712009-04-06 18:47:14 -04004668 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004669 * @phba: Pointer to HBA context object.
4670 *
4671 * This function is called after a HBA restart to wait for successful
4672 * restart of the HBA. Successful restart of the HBA is indicated by
4673 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4674 * iteration, the function will restart the HBA again. The function returns
4675 * zero if HBA successfully restarted else returns negative error code.
4676 **/
James Smart4492b732017-04-27 15:08:26 -07004677int
dea31012005-04-17 16:05:31 -05004678lpfc_sli_chipset_init(struct lpfc_hba *phba)
4679{
4680 uint32_t status, i = 0;
4681
4682 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004683 if (lpfc_readl(phba->HSregaddr, &status))
4684 return -EIO;
dea31012005-04-17 16:05:31 -05004685
4686 /* Check status register to see what current state is */
4687 i = 0;
4688 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4689
James Smartdcf2a4e2010-09-29 11:18:53 -04004690 /* Check every 10ms for 10 retries, then every 100ms for 90
4691 * retries, then every 1 sec for 50 retires for a total of
4692 * ~60 seconds before reset the board again and check every
4693 * 1 sec for 50 retries. The up to 60 seconds before the
4694 * board ready is required by the Falcon FIPS zeroization
4695 * complete, and any reset the board in between shall cause
4696 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004697 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004698 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004699 /* Adapter failed to init, timeout, status reg
4700 <status> */
James Smarted957682007-06-17 19:56:37 -05004701 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004702 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004703 "timeout, status reg x%x, "
4704 "FW Data: A8 x%x AC x%x\n", status,
4705 readl(phba->MBslimaddr + 0xa8),
4706 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004707 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004708 return -ETIMEDOUT;
4709 }
4710
4711 /* Check to see if any errors occurred during init */
4712 if (status & HS_FFERM) {
4713 /* ERROR: During chipset initialization */
4714 /* Adapter failed to init, chipset, status reg
4715 <status> */
James Smarted957682007-06-17 19:56:37 -05004716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004717 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004718 "chipset, status reg x%x, "
4719 "FW Data: A8 x%x AC x%x\n", status,
4720 readl(phba->MBslimaddr + 0xa8),
4721 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004722 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004723 return -EIO;
4724 }
4725
James Smartdcf2a4e2010-09-29 11:18:53 -04004726 if (i <= 10)
dea31012005-04-17 16:05:31 -05004727 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004728 else if (i <= 100)
4729 msleep(100);
4730 else
4731 msleep(1000);
dea31012005-04-17 16:05:31 -05004732
James Smartdcf2a4e2010-09-29 11:18:53 -04004733 if (i == 150) {
4734 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004735 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004736 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004737 }
4738 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004739 if (lpfc_readl(phba->HSregaddr, &status))
4740 return -EIO;
dea31012005-04-17 16:05:31 -05004741 }
4742
4743 /* Check to see if any errors occurred during init */
4744 if (status & HS_FFERM) {
4745 /* ERROR: During chipset initialization */
4746 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004747 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004748 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004749 "status reg x%x, "
4750 "FW Data: A8 x%x AC x%x\n", status,
4751 readl(phba->MBslimaddr + 0xa8),
4752 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004753 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004754 return -EIO;
4755 }
4756
4757 /* Clear all interrupt enable conditions */
4758 writel(0, phba->HCregaddr);
4759 readl(phba->HCregaddr); /* flush */
4760
4761 /* setup host attn register */
4762 writel(0xffffffff, phba->HAregaddr);
4763 readl(phba->HAregaddr); /* flush */
4764 return 0;
4765}
4766
James Smarte59058c2008-08-24 21:49:00 -04004767/**
James Smart3621a712009-04-06 18:47:14 -04004768 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004769 *
4770 * This function calculates and returns the number of HBQs required to be
4771 * configured.
4772 **/
James Smart78b2d852007-08-02 11:10:21 -04004773int
James Smarted957682007-06-17 19:56:37 -05004774lpfc_sli_hbq_count(void)
4775{
James Smart92d7f7b2007-06-17 19:56:38 -05004776 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004777}
4778
James Smarte59058c2008-08-24 21:49:00 -04004779/**
James Smart3621a712009-04-06 18:47:14 -04004780 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004781 *
4782 * This function adds the number of hbq entries in every HBQ to get
4783 * the total number of hbq entries required for the HBA and returns
4784 * the total count.
4785 **/
James Smarted957682007-06-17 19:56:37 -05004786static int
4787lpfc_sli_hbq_entry_count(void)
4788{
4789 int hbq_count = lpfc_sli_hbq_count();
4790 int count = 0;
4791 int i;
4792
4793 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004794 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004795 return count;
4796}
4797
James Smarte59058c2008-08-24 21:49:00 -04004798/**
James Smart3621a712009-04-06 18:47:14 -04004799 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004800 *
4801 * This function calculates amount of memory required for all hbq entries
4802 * to be configured and returns the total memory required.
4803 **/
dea31012005-04-17 16:05:31 -05004804int
James Smarted957682007-06-17 19:56:37 -05004805lpfc_sli_hbq_size(void)
4806{
4807 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4808}
4809
James Smarte59058c2008-08-24 21:49:00 -04004810/**
James Smart3621a712009-04-06 18:47:14 -04004811 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004812 * @phba: Pointer to HBA context object.
4813 *
4814 * This function is called during the SLI initialization to configure
4815 * all the HBQs and post buffers to the HBQ. The caller is not
4816 * required to hold any locks. This function will return zero if successful
4817 * else it will return negative error code.
4818 **/
James Smarted957682007-06-17 19:56:37 -05004819static int
4820lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4821{
4822 int hbq_count = lpfc_sli_hbq_count();
4823 LPFC_MBOXQ_t *pmb;
4824 MAILBOX_t *pmbox;
4825 uint32_t hbqno;
4826 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004827
James Smart92d7f7b2007-06-17 19:56:38 -05004828 /* Get a Mailbox buffer to setup mailbox
4829 * commands for HBA initialization
4830 */
James Smarted957682007-06-17 19:56:37 -05004831 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4832
4833 if (!pmb)
4834 return -ENOMEM;
4835
James Smart04c68492009-05-22 14:52:52 -04004836 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004837
4838 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4839 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004840 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004841
4842 hbq_entry_index = 0;
4843 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4844 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4845 phba->hbqs[hbqno].hbqPutIdx = 0;
4846 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4847 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004848 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004849 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4850 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004851 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4852
4853 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4854 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4855 mbxStatus <status>, ring <num> */
4856
4857 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004858 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004859 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004860 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004861 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004862 pmbox->mbxStatus, hbqno);
4863
4864 phba->link_state = LPFC_HBA_ERROR;
4865 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004866 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004867 }
4868 }
4869 phba->hbq_count = hbq_count;
4870
James Smarted957682007-06-17 19:56:37 -05004871 mempool_free(pmb, phba->mbox_mem_pool);
4872
James Smart92d7f7b2007-06-17 19:56:38 -05004873 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004874 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4875 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004876 return 0;
4877}
4878
James Smarte59058c2008-08-24 21:49:00 -04004879/**
James Smart4f774512009-05-22 14:52:35 -04004880 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4881 * @phba: Pointer to HBA context object.
4882 *
4883 * This function is called during the SLI initialization to configure
4884 * all the HBQs and post buffers to the HBQ. The caller is not
4885 * required to hold any locks. This function will return zero if successful
4886 * else it will return negative error code.
4887 **/
4888static int
4889lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4890{
4891 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004892 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4893 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004894 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004895 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004896 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004897 return 0;
4898}
4899
4900/**
James Smart3621a712009-04-06 18:47:14 -04004901 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004902 * @phba: Pointer to HBA context object.
4903 * @sli_mode: sli mode - 2/3
4904 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004905 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004906 * to issue config_port mailbox command. This function restarts the
4907 * HBA firmware and issues a config_port mailbox command to configure
4908 * the SLI interface in the sli mode specified by sli_mode
4909 * variable. The caller is not required to hold any locks.
4910 * The function returns 0 if successful, else returns negative error
4911 * code.
4912 **/
James Smart93996272008-08-24 21:50:30 -04004913int
4914lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004915{
4916 LPFC_MBOXQ_t *pmb;
4917 uint32_t resetcount = 0, rc = 0, done = 0;
4918
4919 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4920 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004921 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004922 return -ENOMEM;
4923 }
4924
James Smarted957682007-06-17 19:56:37 -05004925 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004926 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004927 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004928 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004929 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004930 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004931 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004932 rc = lpfc_sli_chipset_init(phba);
4933 if (rc)
4934 break;
4935
James Smart2e0fef82007-06-17 19:56:36 -05004936 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004937 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004938 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004939 resetcount++;
4940
James Smarted957682007-06-17 19:56:37 -05004941 /* Call pre CONFIG_PORT mailbox command initialization. A
4942 * value of 0 means the call was successful. Any other
4943 * nonzero value is a failure, but if ERESTART is returned,
4944 * the driver may reset the HBA and try again.
4945 */
dea31012005-04-17 16:05:31 -05004946 rc = lpfc_config_port_prep(phba);
4947 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004948 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004949 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004950 } else if (rc)
dea31012005-04-17 16:05:31 -05004951 break;
James Smart6d368e52011-05-24 11:44:12 -04004952
James Smart2e0fef82007-06-17 19:56:36 -05004953 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004954 lpfc_config_port(phba, pmb);
4955 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004956 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4957 LPFC_SLI3_HBQ_ENABLED |
4958 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004959 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004960 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004961 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004962 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004963 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004964 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004965 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004966 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004967 spin_unlock_irq(&phba->hbalock);
4968 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004969 } else {
4970 /* Allow asynchronous mailbox command to go through */
4971 spin_lock_irq(&phba->hbalock);
4972 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4973 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004974 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004975
4976 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4977 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4978 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4979 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004980 }
dea31012005-04-17 16:05:31 -05004981 }
James Smarted957682007-06-17 19:56:37 -05004982 if (!done) {
4983 rc = -EINVAL;
4984 goto do_prep_failed;
4985 }
James Smart04c68492009-05-22 14:52:52 -04004986 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4987 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004988 rc = -ENXIO;
4989 goto do_prep_failed;
4990 }
James Smart04c68492009-05-22 14:52:52 -04004991 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004992 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004993 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4994 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4995 phba->max_vpi : phba->max_vports;
4996
James Smart34b02dc2008-08-24 21:49:55 -04004997 } else
4998 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004999 phba->fips_level = 0;
5000 phba->fips_spec_rev = 0;
5001 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04005002 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04005003 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
5004 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
5005 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5006 "2850 Security Crypto Active. FIPS x%d "
5007 "(Spec Rev: x%d)",
5008 phba->fips_level, phba->fips_spec_rev);
5009 }
5010 if (pmb->u.mb.un.varCfgPort.sec_err) {
5011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5012 "2856 Config Port Security Crypto "
5013 "Error: x%x ",
5014 pmb->u.mb.un.varCfgPort.sec_err);
5015 }
James Smart04c68492009-05-22 14:52:52 -04005016 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04005017 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005018 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04005019 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04005020
5021 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5022 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05005023
James Smartf44ac122018-03-05 12:04:08 -08005024 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5025 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5026 phba->cfg_enable_bg = 0;
5027 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smarte2a0a9d2008-12-04 22:40:02 -05005028 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5029 "0443 Adapter did not grant "
5030 "BlockGuard\n");
James Smartf44ac122018-03-05 12:04:08 -08005031 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005032 }
James Smart34b02dc2008-08-24 21:49:55 -04005033 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05005034 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04005035 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04005036 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05005037 }
James Smart92d7f7b2007-06-17 19:56:38 -05005038do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05005039 mempool_free(pmb, phba->mbox_mem_pool);
5040 return rc;
5041}
5042
James Smarte59058c2008-08-24 21:49:00 -04005043
5044/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08005045 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04005046 * @phba: Pointer to HBA context object.
5047 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005048 * This function is the main SLI initialization function. This function
5049 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04005050 * error attention handler code. Caller is not required to hold any
5051 * locks. This function issues config_port mailbox command to configure
5052 * the SLI, setup iocb rings and HBQ rings. In the end the function
5053 * calls the config_port_post function to issue init_link mailbox
5054 * command and to start the discovery. The function will return zero
5055 * if successful, else it will return negative error code.
5056 **/
James Smarted957682007-06-17 19:56:37 -05005057int
5058lpfc_sli_hba_setup(struct lpfc_hba *phba)
5059{
5060 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04005061 int mode = 3, i;
5062 int longs;
James Smarted957682007-06-17 19:56:37 -05005063
James Smart12247e82016-07-06 12:36:09 -07005064 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05005065 case 2:
James Smart78b2d852007-08-02 11:10:21 -04005066 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05005067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005068 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05005069 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07005070 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05005071 break;
5072 }
James Smarted957682007-06-17 19:56:37 -05005073 mode = 2;
5074 break;
5075 case 0:
5076 case 3:
5077 break;
5078 default:
James Smart92d7f7b2007-06-17 19:56:38 -05005079 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005080 "1819 Unrecognized sli_mode parameter: %d.\n",
5081 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05005082
5083 break;
5084 }
James Smartb5c53952016-03-31 14:12:30 -07005085 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005086
James Smart93996272008-08-24 21:50:30 -04005087 rc = lpfc_sli_config_port(phba, mode);
5088
James Smart12247e82016-07-06 12:36:09 -07005089 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05005090 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005091 "1820 Unable to select SLI-3. "
5092 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05005093 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04005094 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07005095 else if (rc && mode == 2)
5096 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05005097 if (rc)
dea31012005-04-17 16:05:31 -05005098 goto lpfc_sli_hba_setup_error;
5099
James Smart0d878412009-10-02 15:16:56 -04005100 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5101 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5102 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5103 if (!rc) {
5104 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5105 "2709 This device supports "
5106 "Advanced Error Reporting (AER)\n");
5107 spin_lock_irq(&phba->hbalock);
5108 phba->hba_flag |= HBA_AER_ENABLED;
5109 spin_unlock_irq(&phba->hbalock);
5110 } else {
5111 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5112 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005113 "Advanced Error Reporting (AER): %d\n",
5114 rc);
James Smart0d878412009-10-02 15:16:56 -04005115 phba->cfg_aer_support = 0;
5116 }
5117 }
5118
James Smarted957682007-06-17 19:56:37 -05005119 if (phba->sli_rev == 3) {
5120 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5121 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005122 } else {
5123 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5124 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005125 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005126 }
5127
5128 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005129 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5130 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005131 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005132
5133 if (rc)
5134 goto lpfc_sli_hba_setup_error;
5135
James Smart6d368e52011-05-24 11:44:12 -04005136 /* Initialize VPIs. */
5137 if (phba->sli_rev == LPFC_SLI_REV3) {
5138 /*
5139 * The VPI bitmask and physical ID array are allocated
5140 * and initialized once only - at driver load. A port
5141 * reset doesn't need to reinitialize this memory.
5142 */
5143 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5144 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07005145 phba->vpi_bmask = kcalloc(longs,
5146 sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005147 GFP_KERNEL);
5148 if (!phba->vpi_bmask) {
5149 rc = -ENOMEM;
5150 goto lpfc_sli_hba_setup_error;
5151 }
5152
Kees Cook6396bb22018-06-12 14:03:40 -07005153 phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5154 sizeof(uint16_t),
5155 GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -04005156 if (!phba->vpi_ids) {
5157 kfree(phba->vpi_bmask);
5158 rc = -ENOMEM;
5159 goto lpfc_sli_hba_setup_error;
5160 }
5161 for (i = 0; i < phba->max_vpi; i++)
5162 phba->vpi_ids[i] = i;
5163 }
5164 }
5165
James Smart93996272008-08-24 21:50:30 -04005166 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005167 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5168 rc = lpfc_sli_hbq_setup(phba);
5169 if (rc)
5170 goto lpfc_sli_hba_setup_error;
5171 }
James Smart04c68492009-05-22 14:52:52 -04005172 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005173 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005174 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005175
5176 rc = lpfc_config_port_post(phba);
5177 if (rc)
5178 goto lpfc_sli_hba_setup_error;
5179
James Smarted957682007-06-17 19:56:37 -05005180 return rc;
5181
James Smart92d7f7b2007-06-17 19:56:38 -05005182lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005183 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05005184 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005185 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005186 return rc;
5187}
5188
James Smartda0436e2009-05-22 14:51:39 -04005189/**
5190 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5191 * @phba: Pointer to HBA context object.
5192 * @mboxq: mailbox pointer.
5193 * This function issue a dump mailbox command to read config region
5194 * 23 and parse the records in the region and populate driver
5195 * data structure.
5196 **/
5197static int
James Smartff78d8f2011-12-13 13:21:35 -05005198lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005199{
James Smartff78d8f2011-12-13 13:21:35 -05005200 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005201 struct lpfc_dmabuf *mp;
5202 struct lpfc_mqe *mqe;
5203 uint32_t data_length;
5204 int rc;
5205
5206 /* Program the default value of vlan_id and fc_map */
5207 phba->valid_vlan = 0;
5208 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5209 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5210 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5211
James Smartff78d8f2011-12-13 13:21:35 -05005212 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5213 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005214 return -ENOMEM;
5215
James Smartff78d8f2011-12-13 13:21:35 -05005216 mqe = &mboxq->u.mqe;
5217 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5218 rc = -ENOMEM;
5219 goto out_free_mboxq;
5220 }
5221
James Smart3e1f0712018-11-29 16:09:29 -08005222 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04005223 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5224
5225 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5226 "(%d):2571 Mailbox cmd x%x Status x%x "
5227 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5228 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5229 "CQ: x%x x%x x%x x%x\n",
5230 mboxq->vport ? mboxq->vport->vpi : 0,
5231 bf_get(lpfc_mqe_command, mqe),
5232 bf_get(lpfc_mqe_status, mqe),
5233 mqe->un.mb_words[0], mqe->un.mb_words[1],
5234 mqe->un.mb_words[2], mqe->un.mb_words[3],
5235 mqe->un.mb_words[4], mqe->un.mb_words[5],
5236 mqe->un.mb_words[6], mqe->un.mb_words[7],
5237 mqe->un.mb_words[8], mqe->un.mb_words[9],
5238 mqe->un.mb_words[10], mqe->un.mb_words[11],
5239 mqe->un.mb_words[12], mqe->un.mb_words[13],
5240 mqe->un.mb_words[14], mqe->un.mb_words[15],
5241 mqe->un.mb_words[16], mqe->un.mb_words[50],
5242 mboxq->mcqe.word0,
5243 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5244 mboxq->mcqe.trailer);
5245
5246 if (rc) {
5247 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5248 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005249 rc = -EIO;
5250 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005251 }
5252 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005253 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005254 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5255 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005256 rc = -EIO;
5257 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005258 }
James Smartda0436e2009-05-22 14:51:39 -04005259
5260 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5261 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5262 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005263 rc = 0;
5264
5265out_free_mboxq:
5266 mempool_free(mboxq, phba->mbox_mem_pool);
5267 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005268}
5269
5270/**
5271 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5272 * @phba: pointer to lpfc hba data structure.
5273 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5274 * @vpd: pointer to the memory to hold resulting port vpd data.
5275 * @vpd_size: On input, the number of bytes allocated to @vpd.
5276 * On output, the number of data bytes in @vpd.
5277 *
5278 * This routine executes a READ_REV SLI4 mailbox command. In
5279 * addition, this routine gets the port vpd data.
5280 *
5281 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005282 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005283 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005284 **/
5285static int
5286lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5287 uint8_t *vpd, uint32_t *vpd_size)
5288{
5289 int rc = 0;
5290 uint32_t dma_size;
5291 struct lpfc_dmabuf *dmabuf;
5292 struct lpfc_mqe *mqe;
5293
5294 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5295 if (!dmabuf)
5296 return -ENOMEM;
5297
5298 /*
5299 * Get a DMA buffer for the vpd data resulting from the READ_REV
5300 * mailbox command.
5301 */
5302 dma_size = *vpd_size;
Luis Chamberlain750afb02019-01-04 09:23:09 +01005303 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
5304 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005305 if (!dmabuf->virt) {
5306 kfree(dmabuf);
5307 return -ENOMEM;
5308 }
James Smartda0436e2009-05-22 14:51:39 -04005309
5310 /*
5311 * The SLI4 implementation of READ_REV conflicts at word1,
5312 * bits 31:16 and SLI4 adds vpd functionality not present
5313 * in SLI3. This code corrects the conflicts.
5314 */
5315 lpfc_read_rev(phba, mboxq);
5316 mqe = &mboxq->u.mqe;
5317 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5318 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5319 mqe->un.read_rev.word1 &= 0x0000FFFF;
5320 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5321 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5322
5323 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5324 if (rc) {
5325 dma_free_coherent(&phba->pcidev->dev, dma_size,
5326 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005327 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005328 return -EIO;
5329 }
5330
James Smartda0436e2009-05-22 14:51:39 -04005331 /*
5332 * The available vpd length cannot be bigger than the
5333 * DMA buffer passed to the port. Catch the less than
5334 * case and update the caller's size.
5335 */
5336 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5337 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5338
James Smartd7c47992010-06-08 18:31:54 -04005339 memcpy(vpd, dmabuf->virt, *vpd_size);
5340
James Smartda0436e2009-05-22 14:51:39 -04005341 dma_free_coherent(&phba->pcidev->dev, dma_size,
5342 dmabuf->virt, dmabuf->phys);
5343 kfree(dmabuf);
5344 return 0;
5345}
5346
5347/**
James Smartb3b4f3e2019-03-12 16:30:23 -07005348 * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
James Smartcd1c8302011-10-10 21:33:25 -04005349 * @phba: pointer to lpfc hba data structure.
5350 *
5351 * This routine retrieves SLI4 device physical port name this PCI function
5352 * is attached to.
5353 *
5354 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005355 * 0 - successful
James Smartb3b4f3e2019-03-12 16:30:23 -07005356 * otherwise - failed to retrieve controller attributes
James Smartcd1c8302011-10-10 21:33:25 -04005357 **/
5358static int
James Smartb3b4f3e2019-03-12 16:30:23 -07005359lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
James Smartcd1c8302011-10-10 21:33:25 -04005360{
5361 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005362 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5363 struct lpfc_controller_attribute *cntl_attr;
James Smartcd1c8302011-10-10 21:33:25 -04005364 void *virtaddr = NULL;
5365 uint32_t alloclen, reqlen;
5366 uint32_t shdr_status, shdr_add_status;
5367 union lpfc_sli4_cfg_shdr *shdr;
James Smartcd1c8302011-10-10 21:33:25 -04005368 int rc;
5369
James Smartcd1c8302011-10-10 21:33:25 -04005370 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5371 if (!mboxq)
5372 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005373
James Smartb3b4f3e2019-03-12 16:30:23 -07005374 /* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
James Smartcd1c8302011-10-10 21:33:25 -04005375 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5376 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5377 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5378 LPFC_SLI4_MBX_NEMBED);
James Smartb3b4f3e2019-03-12 16:30:23 -07005379
James Smartcd1c8302011-10-10 21:33:25 -04005380 if (alloclen < reqlen) {
5381 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5382 "3084 Allocated DMA memory size (%d) is "
5383 "less than the requested DMA memory size "
5384 "(%d)\n", alloclen, reqlen);
5385 rc = -ENOMEM;
5386 goto out_free_mboxq;
5387 }
5388 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5389 virtaddr = mboxq->sge_array->addr[0];
5390 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5391 shdr = &mbx_cntl_attr->cfg_shdr;
5392 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5393 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5394 if (shdr_status || shdr_add_status || rc) {
5395 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5396 "3085 Mailbox x%x (x%x/x%x) failed, "
5397 "rc:x%x, status:x%x, add_status:x%x\n",
5398 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5399 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5400 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5401 rc, shdr_status, shdr_add_status);
5402 rc = -ENXIO;
5403 goto out_free_mboxq;
5404 }
James Smartb3b4f3e2019-03-12 16:30:23 -07005405
James Smartcd1c8302011-10-10 21:33:25 -04005406 cntl_attr = &mbx_cntl_attr->cntl_attr;
5407 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5408 phba->sli4_hba.lnk_info.lnk_tp =
5409 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5410 phba->sli4_hba.lnk_info.lnk_no =
5411 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
James Smartb3b4f3e2019-03-12 16:30:23 -07005412
5413 memset(phba->BIOSVersion, 0, sizeof(phba->BIOSVersion));
5414 strlcat(phba->BIOSVersion, (char *)cntl_attr->bios_ver_str,
5415 sizeof(phba->BIOSVersion));
5416
James Smartcd1c8302011-10-10 21:33:25 -04005417 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartb3b4f3e2019-03-12 16:30:23 -07005418 "3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s\n",
James Smartcd1c8302011-10-10 21:33:25 -04005419 phba->sli4_hba.lnk_info.lnk_tp,
James Smartb3b4f3e2019-03-12 16:30:23 -07005420 phba->sli4_hba.lnk_info.lnk_no,
5421 phba->BIOSVersion);
5422out_free_mboxq:
5423 if (rc != MBX_TIMEOUT) {
5424 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5425 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5426 else
5427 mempool_free(mboxq, phba->mbox_mem_pool);
5428 }
5429 return rc;
5430}
5431
5432/**
5433 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5434 * @phba: pointer to lpfc hba data structure.
5435 *
5436 * This routine retrieves SLI4 device physical port name this PCI function
5437 * is attached to.
5438 *
5439 * Return codes
5440 * 0 - successful
5441 * otherwise - failed to retrieve physical port name
5442 **/
5443static int
5444lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5445{
5446 LPFC_MBOXQ_t *mboxq;
5447 struct lpfc_mbx_get_port_name *get_port_name;
5448 uint32_t shdr_status, shdr_add_status;
5449 union lpfc_sli4_cfg_shdr *shdr;
5450 char cport_name = 0;
5451 int rc;
5452
5453 /* We assume nothing at this point */
5454 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5455 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5456
5457 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5458 if (!mboxq)
5459 return -ENOMEM;
5460 /* obtain link type and link number via READ_CONFIG */
5461 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5462 lpfc_sli4_read_config(phba);
5463 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5464 goto retrieve_ppname;
5465
5466 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5467 rc = lpfc_sli4_get_ctl_attr(phba);
5468 if (rc)
5469 goto out_free_mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005470
5471retrieve_ppname:
5472 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5473 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5474 sizeof(struct lpfc_mbx_get_port_name) -
5475 sizeof(struct lpfc_sli4_cfg_mhdr),
5476 LPFC_SLI4_MBX_EMBED);
5477 get_port_name = &mboxq->u.mqe.un.get_port_name;
5478 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5479 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5480 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5481 phba->sli4_hba.lnk_info.lnk_tp);
5482 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5483 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5484 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5485 if (shdr_status || shdr_add_status || rc) {
5486 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5487 "3087 Mailbox x%x (x%x/x%x) failed: "
5488 "rc:x%x, status:x%x, add_status:x%x\n",
5489 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5490 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5491 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5492 rc, shdr_status, shdr_add_status);
5493 rc = -ENXIO;
5494 goto out_free_mboxq;
5495 }
5496 switch (phba->sli4_hba.lnk_info.lnk_no) {
5497 case LPFC_LINK_NUMBER_0:
5498 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5499 &get_port_name->u.response);
5500 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5501 break;
5502 case LPFC_LINK_NUMBER_1:
5503 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5504 &get_port_name->u.response);
5505 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5506 break;
5507 case LPFC_LINK_NUMBER_2:
5508 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5509 &get_port_name->u.response);
5510 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5511 break;
5512 case LPFC_LINK_NUMBER_3:
5513 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5514 &get_port_name->u.response);
5515 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5516 break;
5517 default:
5518 break;
5519 }
5520
5521 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5522 phba->Port[0] = cport_name;
5523 phba->Port[1] = '\0';
5524 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5525 "3091 SLI get port name: %s\n", phba->Port);
5526 }
5527
5528out_free_mboxq:
5529 if (rc != MBX_TIMEOUT) {
5530 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5531 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5532 else
5533 mempool_free(mboxq, phba->mbox_mem_pool);
5534 }
5535 return rc;
5536}
5537
5538/**
James Smartda0436e2009-05-22 14:51:39 -04005539 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5540 * @phba: pointer to lpfc hba data structure.
5541 *
5542 * This routine is called to explicitly arm the SLI4 device's completion and
5543 * event queues
5544 **/
5545static void
5546lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5547{
James Smart895427b2017-02-12 13:52:30 -08005548 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08005549 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartcdb42be2019-01-28 11:14:21 -08005550 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07005551 struct lpfc_queue *eq;
James Smartda0436e2009-05-22 14:51:39 -04005552
James Smart32517fc2019-01-28 11:14:33 -08005553 sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
5554 sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
James Smartb71413d2018-02-22 08:18:40 -08005555 if (sli4_hba->nvmels_cq)
James Smart32517fc2019-01-28 11:14:33 -08005556 sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
5557 LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08005558
James Smartcdb42be2019-01-28 11:14:21 -08005559 if (sli4_hba->hdwq) {
James Smart657add42019-05-21 17:49:06 -07005560 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -08005561 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07005562 qp = &sli4_hba->hdwq[qidx];
5563 /* ARM the corresponding CQ */
5564 sli4_hba->sli4_write_cq_db(phba, qp->fcp_cq, 0,
James Smart32517fc2019-01-28 11:14:33 -08005565 LPFC_QUEUE_REARM);
James Smart657add42019-05-21 17:49:06 -07005566 sli4_hba->sli4_write_cq_db(phba, qp->nvme_cq, 0,
James Smart32517fc2019-01-28 11:14:33 -08005567 LPFC_QUEUE_REARM);
James Smartcdb42be2019-01-28 11:14:21 -08005568 }
James Smart895427b2017-02-12 13:52:30 -08005569
James Smart657add42019-05-21 17:49:06 -07005570 /* Loop thru all IRQ vectors */
5571 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
5572 eq = sli4_hba->hba_eq_hdl[qidx].eq;
5573 /* ARM the corresponding EQ */
5574 sli4_hba->sli4_write_eq_db(phba, eq,
5575 0, LPFC_QUEUE_REARM);
5576 }
James Smartcdb42be2019-01-28 11:14:21 -08005577 }
James Smart1ba981f2014-02-20 09:56:45 -05005578
James Smart2d7dbc42017-02-12 13:52:35 -08005579 if (phba->nvmet_support) {
5580 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smart32517fc2019-01-28 11:14:33 -08005581 sli4_hba->sli4_write_cq_db(phba,
5582 sli4_hba->nvmet_cqset[qidx], 0,
James Smart2d7dbc42017-02-12 13:52:35 -08005583 LPFC_QUEUE_REARM);
5584 }
James Smart2e90f4b2011-12-13 13:22:37 -05005585 }
James Smartda0436e2009-05-22 14:51:39 -04005586}
5587
5588/**
James Smart6d368e52011-05-24 11:44:12 -04005589 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5590 * @phba: Pointer to HBA context object.
5591 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005592 * @extnt_count: buffer to hold port available extent count.
5593 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005594 *
James Smartb76f2dc2011-07-22 18:37:42 -04005595 * This function calls the port and retrievs the number of available
5596 * extents and their size for a particular extent type.
5597 *
5598 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005599 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005600int
James Smart6d368e52011-05-24 11:44:12 -04005601lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5602 uint16_t *extnt_count, uint16_t *extnt_size)
5603{
5604 int rc = 0;
5605 uint32_t length;
5606 uint32_t mbox_tmo;
5607 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5608 LPFC_MBOXQ_t *mbox;
5609
5610 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5611 if (!mbox)
5612 return -ENOMEM;
5613
5614 /* Find out how many extents are available for this resource type */
5615 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5616 sizeof(struct lpfc_sli4_cfg_mhdr));
5617 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5618 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5619 length, LPFC_SLI4_MBX_EMBED);
5620
5621 /* Send an extents count of 0 - the GET doesn't use it. */
5622 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5623 LPFC_SLI4_MBX_EMBED);
5624 if (unlikely(rc)) {
5625 rc = -EIO;
5626 goto err_exit;
5627 }
5628
5629 if (!phba->sli4_hba.intr_enable)
5630 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5631 else {
James Smarta183a152011-10-10 21:32:43 -04005632 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005633 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5634 }
5635 if (unlikely(rc)) {
5636 rc = -EIO;
5637 goto err_exit;
5638 }
5639
5640 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5641 if (bf_get(lpfc_mbox_hdr_status,
5642 &rsrc_info->header.cfg_shdr.response)) {
5643 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5644 "2930 Failed to get resource extents "
5645 "Status 0x%x Add'l Status 0x%x\n",
5646 bf_get(lpfc_mbox_hdr_status,
5647 &rsrc_info->header.cfg_shdr.response),
5648 bf_get(lpfc_mbox_hdr_add_status,
5649 &rsrc_info->header.cfg_shdr.response));
5650 rc = -EIO;
5651 goto err_exit;
5652 }
5653
5654 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5655 &rsrc_info->u.rsp);
5656 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5657 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005658
5659 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5660 "3162 Retrieved extents type-%d from port: count:%d, "
5661 "size:%d\n", type, *extnt_count, *extnt_size);
5662
5663err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005664 mempool_free(mbox, phba->mbox_mem_pool);
5665 return rc;
5666}
5667
5668/**
5669 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5670 * @phba: Pointer to HBA context object.
5671 * @type: The extent type to check.
5672 *
5673 * This function reads the current available extents from the port and checks
5674 * if the extent count or extent size has changed since the last access.
5675 * Callers use this routine post port reset to understand if there is a
5676 * extent reprovisioning requirement.
5677 *
5678 * Returns:
5679 * -Error: error indicates problem.
5680 * 1: Extent count or size has changed.
5681 * 0: No changes.
5682 **/
5683static int
5684lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5685{
5686 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5687 uint16_t size_diff, rsrc_ext_size;
5688 int rc = 0;
5689 struct lpfc_rsrc_blks *rsrc_entry;
5690 struct list_head *rsrc_blk_list = NULL;
5691
5692 size_diff = 0;
5693 curr_ext_cnt = 0;
5694 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5695 &rsrc_ext_cnt,
5696 &rsrc_ext_size);
5697 if (unlikely(rc))
5698 return -EIO;
5699
5700 switch (type) {
5701 case LPFC_RSC_TYPE_FCOE_RPI:
5702 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5703 break;
5704 case LPFC_RSC_TYPE_FCOE_VPI:
5705 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5706 break;
5707 case LPFC_RSC_TYPE_FCOE_XRI:
5708 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5709 break;
5710 case LPFC_RSC_TYPE_FCOE_VFI:
5711 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5712 break;
5713 default:
5714 break;
5715 }
5716
5717 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5718 curr_ext_cnt++;
5719 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5720 size_diff++;
5721 }
5722
5723 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5724 rc = 1;
5725
5726 return rc;
5727}
5728
5729/**
5730 * lpfc_sli4_cfg_post_extnts -
5731 * @phba: Pointer to HBA context object.
5732 * @extnt_cnt - number of available extents.
5733 * @type - the extent type (rpi, xri, vfi, vpi).
5734 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5735 * @mbox - pointer to the caller's allocated mailbox structure.
5736 *
5737 * This function executes the extents allocation request. It also
5738 * takes care of the amount of memory needed to allocate or get the
5739 * allocated extents. It is the caller's responsibility to evaluate
5740 * the response.
5741 *
5742 * Returns:
5743 * -Error: Error value describes the condition found.
5744 * 0: if successful
5745 **/
5746static int
James Smart8a9d2e82012-05-09 21:16:12 -04005747lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005748 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5749{
5750 int rc = 0;
5751 uint32_t req_len;
5752 uint32_t emb_len;
5753 uint32_t alloc_len, mbox_tmo;
5754
5755 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005756 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005757
5758 /*
5759 * Calculate the size of an embedded mailbox. The uint32_t
5760 * accounts for extents-specific word.
5761 */
5762 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5763 sizeof(uint32_t);
5764
5765 /*
5766 * Presume the allocation and response will fit into an embedded
5767 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5768 */
5769 *emb = LPFC_SLI4_MBX_EMBED;
5770 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005771 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005772 sizeof(union lpfc_sli4_cfg_shdr) +
5773 sizeof(uint32_t);
5774 *emb = LPFC_SLI4_MBX_NEMBED;
5775 }
5776
5777 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5778 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5779 req_len, *emb);
5780 if (alloc_len < req_len) {
5781 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005782 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005783 "less than the requested DMA memory "
5784 "size (x%x)\n", alloc_len, req_len);
5785 return -ENOMEM;
5786 }
James Smart8a9d2e82012-05-09 21:16:12 -04005787 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005788 if (unlikely(rc))
5789 return -EIO;
5790
5791 if (!phba->sli4_hba.intr_enable)
5792 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5793 else {
James Smarta183a152011-10-10 21:32:43 -04005794 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005795 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5796 }
5797
5798 if (unlikely(rc))
5799 rc = -EIO;
5800 return rc;
5801}
5802
5803/**
5804 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5805 * @phba: Pointer to HBA context object.
5806 * @type: The resource extent type to allocate.
5807 *
5808 * This function allocates the number of elements for the specified
5809 * resource type.
5810 **/
5811static int
5812lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5813{
5814 bool emb = false;
5815 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5816 uint16_t rsrc_id, rsrc_start, j, k;
5817 uint16_t *ids;
5818 int i, rc;
5819 unsigned long longs;
5820 unsigned long *bmask;
5821 struct lpfc_rsrc_blks *rsrc_blks;
5822 LPFC_MBOXQ_t *mbox;
5823 uint32_t length;
5824 struct lpfc_id_range *id_array = NULL;
5825 void *virtaddr = NULL;
5826 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5827 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5828 struct list_head *ext_blk_list;
5829
5830 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5831 &rsrc_cnt,
5832 &rsrc_size);
5833 if (unlikely(rc))
5834 return -EIO;
5835
5836 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5837 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5838 "3009 No available Resource Extents "
5839 "for resource type 0x%x: Count: 0x%x, "
5840 "Size 0x%x\n", type, rsrc_cnt,
5841 rsrc_size);
5842 return -ENOMEM;
5843 }
5844
James Smart8a9d2e82012-05-09 21:16:12 -04005845 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5846 "2903 Post resource extents type-0x%x: "
5847 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005848
5849 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5850 if (!mbox)
5851 return -ENOMEM;
5852
James Smart8a9d2e82012-05-09 21:16:12 -04005853 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005854 if (unlikely(rc)) {
5855 rc = -EIO;
5856 goto err_exit;
5857 }
5858
5859 /*
5860 * Figure out where the response is located. Then get local pointers
5861 * to the response data. The port does not guarantee to respond to
5862 * all extents counts request so update the local variable with the
5863 * allocated count from the port.
5864 */
5865 if (emb == LPFC_SLI4_MBX_EMBED) {
5866 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5867 id_array = &rsrc_ext->u.rsp.id[0];
5868 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5869 } else {
5870 virtaddr = mbox->sge_array->addr[0];
5871 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5872 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5873 id_array = &n_rsrc->id;
5874 }
5875
5876 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5877 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5878
5879 /*
5880 * Based on the resource size and count, correct the base and max
5881 * resource values.
5882 */
5883 length = sizeof(struct lpfc_rsrc_blks);
5884 switch (type) {
5885 case LPFC_RSC_TYPE_FCOE_RPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005886 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005887 sizeof(unsigned long),
5888 GFP_KERNEL);
5889 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5890 rc = -ENOMEM;
5891 goto err_exit;
5892 }
Kees Cook6396bb22018-06-12 14:03:40 -07005893 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005894 sizeof(uint16_t),
5895 GFP_KERNEL);
5896 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5897 kfree(phba->sli4_hba.rpi_bmask);
5898 rc = -ENOMEM;
5899 goto err_exit;
5900 }
5901
5902 /*
5903 * The next_rpi was initialized with the maximum available
5904 * count but the port may allocate a smaller number. Catch
5905 * that case and update the next_rpi.
5906 */
5907 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5908
5909 /* Initialize local ptrs for common extent processing later. */
5910 bmask = phba->sli4_hba.rpi_bmask;
5911 ids = phba->sli4_hba.rpi_ids;
5912 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5913 break;
5914 case LPFC_RSC_TYPE_FCOE_VPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005915 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005916 GFP_KERNEL);
5917 if (unlikely(!phba->vpi_bmask)) {
5918 rc = -ENOMEM;
5919 goto err_exit;
5920 }
Kees Cook6396bb22018-06-12 14:03:40 -07005921 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04005922 GFP_KERNEL);
5923 if (unlikely(!phba->vpi_ids)) {
5924 kfree(phba->vpi_bmask);
5925 rc = -ENOMEM;
5926 goto err_exit;
5927 }
5928
5929 /* Initialize local ptrs for common extent processing later. */
5930 bmask = phba->vpi_bmask;
5931 ids = phba->vpi_ids;
5932 ext_blk_list = &phba->lpfc_vpi_blk_list;
5933 break;
5934 case LPFC_RSC_TYPE_FCOE_XRI:
Kees Cook6396bb22018-06-12 14:03:40 -07005935 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005936 sizeof(unsigned long),
5937 GFP_KERNEL);
5938 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5939 rc = -ENOMEM;
5940 goto err_exit;
5941 }
James Smart8a9d2e82012-05-09 21:16:12 -04005942 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07005943 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005944 sizeof(uint16_t),
5945 GFP_KERNEL);
5946 if (unlikely(!phba->sli4_hba.xri_ids)) {
5947 kfree(phba->sli4_hba.xri_bmask);
5948 rc = -ENOMEM;
5949 goto err_exit;
5950 }
5951
5952 /* Initialize local ptrs for common extent processing later. */
5953 bmask = phba->sli4_hba.xri_bmask;
5954 ids = phba->sli4_hba.xri_ids;
5955 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5956 break;
5957 case LPFC_RSC_TYPE_FCOE_VFI:
Kees Cook6396bb22018-06-12 14:03:40 -07005958 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005959 sizeof(unsigned long),
5960 GFP_KERNEL);
5961 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5962 rc = -ENOMEM;
5963 goto err_exit;
5964 }
Kees Cook6396bb22018-06-12 14:03:40 -07005965 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005966 sizeof(uint16_t),
5967 GFP_KERNEL);
5968 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5969 kfree(phba->sli4_hba.vfi_bmask);
5970 rc = -ENOMEM;
5971 goto err_exit;
5972 }
5973
5974 /* Initialize local ptrs for common extent processing later. */
5975 bmask = phba->sli4_hba.vfi_bmask;
5976 ids = phba->sli4_hba.vfi_ids;
5977 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5978 break;
5979 default:
5980 /* Unsupported Opcode. Fail call. */
5981 id_array = NULL;
5982 bmask = NULL;
5983 ids = NULL;
5984 ext_blk_list = NULL;
5985 goto err_exit;
5986 }
5987
5988 /*
5989 * Complete initializing the extent configuration with the
5990 * allocated ids assigned to this function. The bitmask serves
5991 * as an index into the array and manages the available ids. The
5992 * array just stores the ids communicated to the port via the wqes.
5993 */
5994 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5995 if ((i % 2) == 0)
5996 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5997 &id_array[k]);
5998 else
5999 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6000 &id_array[k]);
6001
6002 rsrc_blks = kzalloc(length, GFP_KERNEL);
6003 if (unlikely(!rsrc_blks)) {
6004 rc = -ENOMEM;
6005 kfree(bmask);
6006 kfree(ids);
6007 goto err_exit;
6008 }
6009 rsrc_blks->rsrc_start = rsrc_id;
6010 rsrc_blks->rsrc_size = rsrc_size;
6011 list_add_tail(&rsrc_blks->list, ext_blk_list);
6012 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08006013 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart5e5b5112019-01-28 11:14:22 -08006014 phba->sli4_hba.io_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08006015 lpfc_sli4_get_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08006016 }
James Smart6d368e52011-05-24 11:44:12 -04006017
6018 while (rsrc_id < (rsrc_start + rsrc_size)) {
6019 ids[j] = rsrc_id;
6020 rsrc_id++;
6021 j++;
6022 }
6023 /* Entire word processed. Get next word.*/
6024 if ((i % 2) == 1)
6025 k++;
6026 }
6027 err_exit:
6028 lpfc_sli4_mbox_cmd_free(phba, mbox);
6029 return rc;
6030}
6031
James Smart895427b2017-02-12 13:52:30 -08006032
6033
James Smart6d368e52011-05-24 11:44:12 -04006034/**
6035 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6036 * @phba: Pointer to HBA context object.
6037 * @type: the extent's type.
6038 *
6039 * This function deallocates all extents of a particular resource type.
6040 * SLI4 does not allow for deallocating a particular extent range. It
6041 * is the caller's responsibility to release all kernel memory resources.
6042 **/
6043static int
6044lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6045{
6046 int rc;
6047 uint32_t length, mbox_tmo = 0;
6048 LPFC_MBOXQ_t *mbox;
6049 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6050 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6051
6052 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6053 if (!mbox)
6054 return -ENOMEM;
6055
6056 /*
6057 * This function sends an embedded mailbox because it only sends the
6058 * the resource type. All extents of this type are released by the
6059 * port.
6060 */
6061 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6062 sizeof(struct lpfc_sli4_cfg_mhdr));
6063 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6064 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6065 length, LPFC_SLI4_MBX_EMBED);
6066
6067 /* Send an extents count of 0 - the dealloc doesn't use it. */
6068 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6069 LPFC_SLI4_MBX_EMBED);
6070 if (unlikely(rc)) {
6071 rc = -EIO;
6072 goto out_free_mbox;
6073 }
6074 if (!phba->sli4_hba.intr_enable)
6075 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6076 else {
James Smarta183a152011-10-10 21:32:43 -04006077 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006078 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6079 }
6080 if (unlikely(rc)) {
6081 rc = -EIO;
6082 goto out_free_mbox;
6083 }
6084
6085 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6086 if (bf_get(lpfc_mbox_hdr_status,
6087 &dealloc_rsrc->header.cfg_shdr.response)) {
6088 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6089 "2919 Failed to release resource extents "
6090 "for type %d - Status 0x%x Add'l Status 0x%x. "
6091 "Resource memory not released.\n",
6092 type,
6093 bf_get(lpfc_mbox_hdr_status,
6094 &dealloc_rsrc->header.cfg_shdr.response),
6095 bf_get(lpfc_mbox_hdr_add_status,
6096 &dealloc_rsrc->header.cfg_shdr.response));
6097 rc = -EIO;
6098 goto out_free_mbox;
6099 }
6100
6101 /* Release kernel memory resources for the specific type. */
6102 switch (type) {
6103 case LPFC_RSC_TYPE_FCOE_VPI:
6104 kfree(phba->vpi_bmask);
6105 kfree(phba->vpi_ids);
6106 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6107 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6108 &phba->lpfc_vpi_blk_list, list) {
6109 list_del_init(&rsrc_blk->list);
6110 kfree(rsrc_blk);
6111 }
James Smart16a3a202013-04-17 20:14:38 -04006112 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006113 break;
6114 case LPFC_RSC_TYPE_FCOE_XRI:
6115 kfree(phba->sli4_hba.xri_bmask);
6116 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006117 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6118 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6119 list_del_init(&rsrc_blk->list);
6120 kfree(rsrc_blk);
6121 }
6122 break;
6123 case LPFC_RSC_TYPE_FCOE_VFI:
6124 kfree(phba->sli4_hba.vfi_bmask);
6125 kfree(phba->sli4_hba.vfi_ids);
6126 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6127 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6128 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6129 list_del_init(&rsrc_blk->list);
6130 kfree(rsrc_blk);
6131 }
6132 break;
6133 case LPFC_RSC_TYPE_FCOE_RPI:
6134 /* RPI bitmask and physical id array are cleaned up earlier. */
6135 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6136 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6137 list_del_init(&rsrc_blk->list);
6138 kfree(rsrc_blk);
6139 }
6140 break;
6141 default:
6142 break;
6143 }
6144
6145 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6146
6147 out_free_mbox:
6148 mempool_free(mbox, phba->mbox_mem_pool);
6149 return rc;
6150}
6151
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006152static void
James Smart7bdedb32016-07-06 12:36:00 -07006153lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6154 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006155{
James Smart65791f12016-07-06 12:35:56 -07006156 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07006157
James Smart65791f12016-07-06 12:35:56 -07006158 len = sizeof(struct lpfc_mbx_set_feature) -
6159 sizeof(struct lpfc_sli4_cfg_mhdr);
6160 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6161 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6162 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006163
James Smart7bdedb32016-07-06 12:36:00 -07006164 switch (feature) {
6165 case LPFC_SET_UE_RECOVERY:
6166 bf_set(lpfc_mbx_set_feature_UER,
6167 &mbox->u.mqe.un.set_feature, 1);
6168 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6169 mbox->u.mqe.un.set_feature.param_len = 8;
6170 break;
6171 case LPFC_SET_MDS_DIAGS:
6172 bf_set(lpfc_mbx_set_feature_mds,
6173 &mbox->u.mqe.un.set_feature, 1);
6174 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006175 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006176 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6177 mbox->u.mqe.un.set_feature.param_len = 8;
6178 break;
James Smart65791f12016-07-06 12:35:56 -07006179 }
James Smart7bdedb32016-07-06 12:36:00 -07006180
6181 return;
James Smart65791f12016-07-06 12:35:56 -07006182}
6183
James Smart6d368e52011-05-24 11:44:12 -04006184/**
James Smart1165a5c2018-11-29 16:09:39 -08006185 * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6186 * @phba: Pointer to HBA context object.
6187 *
6188 * Disable FW logging into host memory on the adapter. To
6189 * be done before reading logs from the host memory.
6190 **/
6191void
6192lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6193{
6194 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6195
6196 ras_fwlog->ras_active = false;
6197
6198 /* Disable FW logging to host memory */
6199 writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6200 phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
6201}
6202
6203/**
James Smartd2cc9bc2018-09-10 10:30:50 -07006204 * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6205 * @phba: Pointer to HBA context object.
6206 *
6207 * This function is called to free memory allocated for RAS FW logging
6208 * support in the driver.
6209 **/
6210void
6211lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6212{
6213 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6214 struct lpfc_dmabuf *dmabuf, *next;
6215
6216 if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6217 list_for_each_entry_safe(dmabuf, next,
6218 &ras_fwlog->fwlog_buff_list,
6219 list) {
6220 list_del(&dmabuf->list);
6221 dma_free_coherent(&phba->pcidev->dev,
6222 LPFC_RAS_MAX_ENTRY_SIZE,
6223 dmabuf->virt, dmabuf->phys);
6224 kfree(dmabuf);
6225 }
6226 }
6227
6228 if (ras_fwlog->lwpd.virt) {
6229 dma_free_coherent(&phba->pcidev->dev,
6230 sizeof(uint32_t) * 2,
6231 ras_fwlog->lwpd.virt,
6232 ras_fwlog->lwpd.phys);
6233 ras_fwlog->lwpd.virt = NULL;
6234 }
6235
6236 ras_fwlog->ras_active = false;
6237}
6238
6239/**
6240 * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6241 * @phba: Pointer to HBA context object.
6242 * @fwlog_buff_count: Count of buffers to be created.
6243 *
6244 * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6245 * to update FW log is posted to the adapter.
6246 * Buffer count is calculated based on module param ras_fwlog_buffsize
6247 * Size of each buffer posted to FW is 64K.
6248 **/
6249
6250static int
6251lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6252 uint32_t fwlog_buff_count)
6253{
6254 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6255 struct lpfc_dmabuf *dmabuf;
6256 int rc = 0, i = 0;
6257
6258 /* Initialize List */
6259 INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6260
6261 /* Allocate memory for the LWPD */
6262 ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6263 sizeof(uint32_t) * 2,
6264 &ras_fwlog->lwpd.phys,
6265 GFP_KERNEL);
6266 if (!ras_fwlog->lwpd.virt) {
James Smartcb349902018-11-29 16:09:27 -08006267 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006268 "6185 LWPD Memory Alloc Failed\n");
6269
6270 return -ENOMEM;
6271 }
6272
6273 ras_fwlog->fw_buffcount = fwlog_buff_count;
6274 for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6275 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6276 GFP_KERNEL);
6277 if (!dmabuf) {
6278 rc = -ENOMEM;
6279 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6280 "6186 Memory Alloc failed FW logging");
6281 goto free_mem;
6282 }
6283
Luis Chamberlain750afb02019-01-04 09:23:09 +01006284 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartd2cc9bc2018-09-10 10:30:50 -07006285 LPFC_RAS_MAX_ENTRY_SIZE,
Luis Chamberlain750afb02019-01-04 09:23:09 +01006286 &dmabuf->phys, GFP_KERNEL);
James Smartd2cc9bc2018-09-10 10:30:50 -07006287 if (!dmabuf->virt) {
6288 kfree(dmabuf);
6289 rc = -ENOMEM;
6290 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6291 "6187 DMA Alloc Failed FW logging");
6292 goto free_mem;
6293 }
James Smartd2cc9bc2018-09-10 10:30:50 -07006294 dmabuf->buffer_tag = i;
6295 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6296 }
6297
6298free_mem:
6299 if (rc)
6300 lpfc_sli4_ras_dma_free(phba);
6301
6302 return rc;
6303}
6304
6305/**
6306 * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6307 * @phba: pointer to lpfc hba data structure.
6308 * @pmboxq: pointer to the driver internal queue element for mailbox command.
6309 *
6310 * Completion handler for driver's RAS MBX command to the device.
6311 **/
6312static void
6313lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6314{
6315 MAILBOX_t *mb;
6316 union lpfc_sli4_cfg_shdr *shdr;
6317 uint32_t shdr_status, shdr_add_status;
6318 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6319
6320 mb = &pmb->u.mb;
6321
6322 shdr = (union lpfc_sli4_cfg_shdr *)
6323 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6324 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6325 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6326
6327 if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
James Smartcb349902018-11-29 16:09:27 -08006328 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smartd2cc9bc2018-09-10 10:30:50 -07006329 "6188 FW LOG mailbox "
6330 "completed with status x%x add_status x%x,"
6331 " mbx status x%x\n",
6332 shdr_status, shdr_add_status, mb->mbxStatus);
James Smartcb349902018-11-29 16:09:27 -08006333
6334 ras_fwlog->ras_hwsupport = false;
James Smartd2cc9bc2018-09-10 10:30:50 -07006335 goto disable_ras;
6336 }
6337
6338 ras_fwlog->ras_active = true;
6339 mempool_free(pmb, phba->mbox_mem_pool);
6340
6341 return;
6342
6343disable_ras:
6344 /* Free RAS DMA memory */
6345 lpfc_sli4_ras_dma_free(phba);
6346 mempool_free(pmb, phba->mbox_mem_pool);
6347}
6348
6349/**
6350 * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6351 * @phba: pointer to lpfc hba data structure.
6352 * @fwlog_level: Logging verbosity level.
6353 * @fwlog_enable: Enable/Disable logging.
6354 *
6355 * Initialize memory and post mailbox command to enable FW logging in host
6356 * memory.
6357 **/
6358int
6359lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6360 uint32_t fwlog_level,
6361 uint32_t fwlog_enable)
6362{
6363 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6364 struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6365 struct lpfc_dmabuf *dmabuf;
6366 LPFC_MBOXQ_t *mbox;
6367 uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6368 int rc = 0;
6369
6370 fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6371 phba->cfg_ras_fwlog_buffsize);
6372 fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6373
6374 /*
6375 * If re-enabling FW logging support use earlier allocated
6376 * DMA buffers while posting MBX command.
6377 **/
6378 if (!ras_fwlog->lwpd.virt) {
6379 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6380 if (rc) {
6381 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smartcb349902018-11-29 16:09:27 -08006382 "6189 FW Log Memory Allocation Failed");
James Smartd2cc9bc2018-09-10 10:30:50 -07006383 return rc;
6384 }
6385 }
6386
6387 /* Setup Mailbox command */
6388 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6389 if (!mbox) {
James Smartcb349902018-11-29 16:09:27 -08006390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006391 "6190 RAS MBX Alloc Failed");
6392 rc = -ENOMEM;
6393 goto mem_free;
6394 }
6395
6396 ras_fwlog->fw_loglevel = fwlog_level;
6397 len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6398 sizeof(struct lpfc_sli4_cfg_mhdr));
6399
6400 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6401 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6402 len, LPFC_SLI4_MBX_EMBED);
6403
6404 mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6405 bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6406 fwlog_enable);
6407 bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6408 ras_fwlog->fw_loglevel);
6409 bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6410 ras_fwlog->fw_buffcount);
6411 bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6412 LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6413
6414 /* Update DMA buffer address */
6415 list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6416 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6417
6418 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6419 putPaddrLow(dmabuf->phys);
6420
6421 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
6422 putPaddrHigh(dmabuf->phys);
6423 }
6424
6425 /* Update LPWD address */
6426 mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
6427 mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
6428
6429 mbox->vport = phba->pport;
6430 mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
6431
6432 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6433
6434 if (rc == MBX_NOT_FINISHED) {
James Smartcb349902018-11-29 16:09:27 -08006435 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6436 "6191 FW-Log Mailbox failed. "
James Smartd2cc9bc2018-09-10 10:30:50 -07006437 "status %d mbxStatus : x%x", rc,
6438 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6439 mempool_free(mbox, phba->mbox_mem_pool);
6440 rc = -EIO;
6441 goto mem_free;
6442 } else
6443 rc = 0;
6444mem_free:
6445 if (rc)
6446 lpfc_sli4_ras_dma_free(phba);
6447
6448 return rc;
6449}
6450
6451/**
6452 * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
6453 * @phba: Pointer to HBA context object.
6454 *
6455 * Check if RAS is supported on the adapter and initialize it.
6456 **/
6457void
6458lpfc_sli4_ras_setup(struct lpfc_hba *phba)
6459{
6460 /* Check RAS FW Log needs to be enabled or not */
6461 if (lpfc_check_fwlog_support(phba))
6462 return;
6463
6464 lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
6465 LPFC_RAS_ENABLE_LOGGING);
6466}
6467
6468/**
James Smart6d368e52011-05-24 11:44:12 -04006469 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6470 * @phba: Pointer to HBA context object.
6471 *
6472 * This function allocates all SLI4 resource identifiers.
6473 **/
6474int
6475lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6476{
6477 int i, rc, error = 0;
6478 uint16_t count, base;
6479 unsigned long longs;
6480
James Smartff78d8f2011-12-13 13:21:35 -05006481 if (!phba->sli4_hba.rpi_hdrs_in_use)
6482 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04006483 if (phba->sli4_hba.extents_in_use) {
6484 /*
6485 * The port supports resource extents. The XRI, VPI, VFI, RPI
6486 * resource extent count must be read and allocated before
6487 * provisioning the resource id arrays.
6488 */
6489 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6490 LPFC_IDX_RSRC_RDY) {
6491 /*
6492 * Extent-based resources are set - the driver could
6493 * be in a port reset. Figure out if any corrective
6494 * actions need to be taken.
6495 */
6496 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6497 LPFC_RSC_TYPE_FCOE_VFI);
6498 if (rc != 0)
6499 error++;
6500 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6501 LPFC_RSC_TYPE_FCOE_VPI);
6502 if (rc != 0)
6503 error++;
6504 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6505 LPFC_RSC_TYPE_FCOE_XRI);
6506 if (rc != 0)
6507 error++;
6508 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6509 LPFC_RSC_TYPE_FCOE_RPI);
6510 if (rc != 0)
6511 error++;
6512
6513 /*
6514 * It's possible that the number of resources
6515 * provided to this port instance changed between
6516 * resets. Detect this condition and reallocate
6517 * resources. Otherwise, there is no action.
6518 */
6519 if (error) {
6520 lpfc_printf_log(phba, KERN_INFO,
6521 LOG_MBOX | LOG_INIT,
6522 "2931 Detected extent resource "
6523 "change. Reallocating all "
6524 "extents.\n");
6525 rc = lpfc_sli4_dealloc_extent(phba,
6526 LPFC_RSC_TYPE_FCOE_VFI);
6527 rc = lpfc_sli4_dealloc_extent(phba,
6528 LPFC_RSC_TYPE_FCOE_VPI);
6529 rc = lpfc_sli4_dealloc_extent(phba,
6530 LPFC_RSC_TYPE_FCOE_XRI);
6531 rc = lpfc_sli4_dealloc_extent(phba,
6532 LPFC_RSC_TYPE_FCOE_RPI);
6533 } else
6534 return 0;
6535 }
6536
6537 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6538 if (unlikely(rc))
6539 goto err_exit;
6540
6541 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6542 if (unlikely(rc))
6543 goto err_exit;
6544
6545 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6546 if (unlikely(rc))
6547 goto err_exit;
6548
6549 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6550 if (unlikely(rc))
6551 goto err_exit;
6552 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6553 LPFC_IDX_RSRC_RDY);
6554 return rc;
6555 } else {
6556 /*
6557 * The port does not support resource extents. The XRI, VPI,
6558 * VFI, RPI resource ids were determined from READ_CONFIG.
6559 * Just allocate the bitmasks and provision the resource id
6560 * arrays. If a port reset is active, the resources don't
6561 * need any action - just exit.
6562 */
6563 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006564 LPFC_IDX_RSRC_RDY) {
6565 lpfc_sli4_dealloc_resource_identifiers(phba);
6566 lpfc_sli4_remove_rpis(phba);
6567 }
James Smart6d368e52011-05-24 11:44:12 -04006568 /* RPIs. */
6569 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006570 if (count <= 0) {
6571 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6572 "3279 Invalid provisioning of "
6573 "rpi:%d\n", count);
6574 rc = -EINVAL;
6575 goto err_exit;
6576 }
James Smart6d368e52011-05-24 11:44:12 -04006577 base = phba->sli4_hba.max_cfg_param.rpi_base;
6578 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006579 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006580 sizeof(unsigned long),
6581 GFP_KERNEL);
6582 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6583 rc = -ENOMEM;
6584 goto err_exit;
6585 }
Kees Cook6396bb22018-06-12 14:03:40 -07006586 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006587 GFP_KERNEL);
6588 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6589 rc = -ENOMEM;
6590 goto free_rpi_bmask;
6591 }
6592
6593 for (i = 0; i < count; i++)
6594 phba->sli4_hba.rpi_ids[i] = base + i;
6595
6596 /* VPIs. */
6597 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006598 if (count <= 0) {
6599 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6600 "3280 Invalid provisioning of "
6601 "vpi:%d\n", count);
6602 rc = -EINVAL;
6603 goto free_rpi_ids;
6604 }
James Smart6d368e52011-05-24 11:44:12 -04006605 base = phba->sli4_hba.max_cfg_param.vpi_base;
6606 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006607 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006608 GFP_KERNEL);
6609 if (unlikely(!phba->vpi_bmask)) {
6610 rc = -ENOMEM;
6611 goto free_rpi_ids;
6612 }
Kees Cook6396bb22018-06-12 14:03:40 -07006613 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006614 GFP_KERNEL);
6615 if (unlikely(!phba->vpi_ids)) {
6616 rc = -ENOMEM;
6617 goto free_vpi_bmask;
6618 }
6619
6620 for (i = 0; i < count; i++)
6621 phba->vpi_ids[i] = base + i;
6622
6623 /* XRIs. */
6624 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006625 if (count <= 0) {
6626 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6627 "3281 Invalid provisioning of "
6628 "xri:%d\n", count);
6629 rc = -EINVAL;
6630 goto free_vpi_ids;
6631 }
James Smart6d368e52011-05-24 11:44:12 -04006632 base = phba->sli4_hba.max_cfg_param.xri_base;
6633 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006634 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006635 sizeof(unsigned long),
6636 GFP_KERNEL);
6637 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6638 rc = -ENOMEM;
6639 goto free_vpi_ids;
6640 }
James Smart41899be2012-03-01 22:34:19 -05006641 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006642 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006643 GFP_KERNEL);
6644 if (unlikely(!phba->sli4_hba.xri_ids)) {
6645 rc = -ENOMEM;
6646 goto free_xri_bmask;
6647 }
6648
6649 for (i = 0; i < count; i++)
6650 phba->sli4_hba.xri_ids[i] = base + i;
6651
6652 /* VFIs. */
6653 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006654 if (count <= 0) {
6655 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6656 "3282 Invalid provisioning of "
6657 "vfi:%d\n", count);
6658 rc = -EINVAL;
6659 goto free_xri_ids;
6660 }
James Smart6d368e52011-05-24 11:44:12 -04006661 base = phba->sli4_hba.max_cfg_param.vfi_base;
6662 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006663 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006664 sizeof(unsigned long),
6665 GFP_KERNEL);
6666 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6667 rc = -ENOMEM;
6668 goto free_xri_ids;
6669 }
Kees Cook6396bb22018-06-12 14:03:40 -07006670 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006671 GFP_KERNEL);
6672 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6673 rc = -ENOMEM;
6674 goto free_vfi_bmask;
6675 }
6676
6677 for (i = 0; i < count; i++)
6678 phba->sli4_hba.vfi_ids[i] = base + i;
6679
6680 /*
6681 * Mark all resources ready. An HBA reset doesn't need
6682 * to reset the initialization.
6683 */
6684 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6685 LPFC_IDX_RSRC_RDY);
6686 return 0;
6687 }
6688
6689 free_vfi_bmask:
6690 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006691 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006692 free_xri_ids:
6693 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006694 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006695 free_xri_bmask:
6696 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006697 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006698 free_vpi_ids:
6699 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006700 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006701 free_vpi_bmask:
6702 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006703 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006704 free_rpi_ids:
6705 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006706 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006707 free_rpi_bmask:
6708 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006709 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006710 err_exit:
6711 return rc;
6712}
6713
6714/**
6715 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6716 * @phba: Pointer to HBA context object.
6717 *
6718 * This function allocates the number of elements for the specified
6719 * resource type.
6720 **/
6721int
6722lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6723{
6724 if (phba->sli4_hba.extents_in_use) {
6725 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6726 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6727 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6728 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6729 } else {
6730 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006731 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006732 kfree(phba->vpi_ids);
6733 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6734 kfree(phba->sli4_hba.xri_bmask);
6735 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006736 kfree(phba->sli4_hba.vfi_bmask);
6737 kfree(phba->sli4_hba.vfi_ids);
6738 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6739 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6740 }
6741
6742 return 0;
6743}
6744
6745/**
James Smartb76f2dc2011-07-22 18:37:42 -04006746 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6747 * @phba: Pointer to HBA context object.
6748 * @type: The resource extent type.
6749 * @extnt_count: buffer to hold port extent count response
6750 * @extnt_size: buffer to hold port extent size response.
6751 *
6752 * This function calls the port to read the host allocated extents
6753 * for a particular type.
6754 **/
6755int
6756lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6757 uint16_t *extnt_cnt, uint16_t *extnt_size)
6758{
6759 bool emb;
6760 int rc = 0;
6761 uint16_t curr_blks = 0;
6762 uint32_t req_len, emb_len;
6763 uint32_t alloc_len, mbox_tmo;
6764 struct list_head *blk_list_head;
6765 struct lpfc_rsrc_blks *rsrc_blk;
6766 LPFC_MBOXQ_t *mbox;
6767 void *virtaddr = NULL;
6768 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6769 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6770 union lpfc_sli4_cfg_shdr *shdr;
6771
6772 switch (type) {
6773 case LPFC_RSC_TYPE_FCOE_VPI:
6774 blk_list_head = &phba->lpfc_vpi_blk_list;
6775 break;
6776 case LPFC_RSC_TYPE_FCOE_XRI:
6777 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6778 break;
6779 case LPFC_RSC_TYPE_FCOE_VFI:
6780 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6781 break;
6782 case LPFC_RSC_TYPE_FCOE_RPI:
6783 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6784 break;
6785 default:
6786 return -EIO;
6787 }
6788
6789 /* Count the number of extents currently allocatd for this type. */
6790 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6791 if (curr_blks == 0) {
6792 /*
6793 * The GET_ALLOCATED mailbox does not return the size,
6794 * just the count. The size should be just the size
6795 * stored in the current allocated block and all sizes
6796 * for an extent type are the same so set the return
6797 * value now.
6798 */
6799 *extnt_size = rsrc_blk->rsrc_size;
6800 }
6801 curr_blks++;
6802 }
6803
James Smartb76f2dc2011-07-22 18:37:42 -04006804 /*
6805 * Calculate the size of an embedded mailbox. The uint32_t
6806 * accounts for extents-specific word.
6807 */
6808 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6809 sizeof(uint32_t);
6810
6811 /*
6812 * Presume the allocation and response will fit into an embedded
6813 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6814 */
6815 emb = LPFC_SLI4_MBX_EMBED;
6816 req_len = emb_len;
6817 if (req_len > emb_len) {
6818 req_len = curr_blks * sizeof(uint16_t) +
6819 sizeof(union lpfc_sli4_cfg_shdr) +
6820 sizeof(uint32_t);
6821 emb = LPFC_SLI4_MBX_NEMBED;
6822 }
6823
6824 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6825 if (!mbox)
6826 return -ENOMEM;
6827 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6828
6829 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6830 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6831 req_len, emb);
6832 if (alloc_len < req_len) {
6833 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6834 "2983 Allocated DMA memory size (x%x) is "
6835 "less than the requested DMA memory "
6836 "size (x%x)\n", alloc_len, req_len);
6837 rc = -ENOMEM;
6838 goto err_exit;
6839 }
6840 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6841 if (unlikely(rc)) {
6842 rc = -EIO;
6843 goto err_exit;
6844 }
6845
6846 if (!phba->sli4_hba.intr_enable)
6847 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6848 else {
James Smarta183a152011-10-10 21:32:43 -04006849 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006850 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6851 }
6852
6853 if (unlikely(rc)) {
6854 rc = -EIO;
6855 goto err_exit;
6856 }
6857
6858 /*
6859 * Figure out where the response is located. Then get local pointers
6860 * to the response data. The port does not guarantee to respond to
6861 * all extents counts request so update the local variable with the
6862 * allocated count from the port.
6863 */
6864 if (emb == LPFC_SLI4_MBX_EMBED) {
6865 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6866 shdr = &rsrc_ext->header.cfg_shdr;
6867 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6868 } else {
6869 virtaddr = mbox->sge_array->addr[0];
6870 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6871 shdr = &n_rsrc->cfg_shdr;
6872 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6873 }
6874
6875 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6876 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6877 "2984 Failed to read allocated resources "
6878 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6879 type,
6880 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6881 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6882 rc = -EIO;
6883 goto err_exit;
6884 }
6885 err_exit:
6886 lpfc_sli4_mbox_cmd_free(phba, mbox);
6887 return rc;
6888}
6889
6890/**
James Smart0ef69962017-04-21 16:04:50 -07006891 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006892 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006893 * @pring: Pointer to driver SLI ring object.
6894 * @sgl_list: linked link of sgl buffers to post
6895 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006896 *
James Smart895427b2017-02-12 13:52:30 -08006897 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006898 * repost them to the port by using SGL block post. This is needed after a
6899 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006900 * of buffer sgls which contains contiguous xris and uses the non-embedded
6901 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006902 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6903 * mailbox command for posting.
6904 *
6905 * Returns: 0 = success, non-zero failure.
6906 **/
6907static int
James Smart895427b2017-02-12 13:52:30 -08006908lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6909 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006910{
6911 struct lpfc_sglq *sglq_entry = NULL;
6912 struct lpfc_sglq *sglq_entry_next = NULL;
6913 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006914 int status, total_cnt;
6915 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006916 int last_xritag = NO_XRI;
6917 LIST_HEAD(prep_sgl_list);
6918 LIST_HEAD(blck_sgl_list);
6919 LIST_HEAD(allc_sgl_list);
6920 LIST_HEAD(post_sgl_list);
6921 LIST_HEAD(free_sgl_list);
6922
James Smart38c20672013-03-01 16:37:44 -05006923 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006924 spin_lock(&phba->sli4_hba.sgl_list_lock);
6925 list_splice_init(sgl_list, &allc_sgl_list);
6926 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006927 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006928
James Smart895427b2017-02-12 13:52:30 -08006929 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006930 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6931 &allc_sgl_list, list) {
6932 list_del_init(&sglq_entry->list);
6933 block_cnt++;
6934 if ((last_xritag != NO_XRI) &&
6935 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6936 /* a hole in xri block, form a sgl posting block */
6937 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6938 post_cnt = block_cnt - 1;
6939 /* prepare list for next posting block */
6940 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6941 block_cnt = 1;
6942 } else {
6943 /* prepare list for next posting block */
6944 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6945 /* enough sgls for non-embed sgl mbox command */
6946 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6947 list_splice_init(&prep_sgl_list,
6948 &blck_sgl_list);
6949 post_cnt = block_cnt;
6950 block_cnt = 0;
6951 }
6952 }
6953 num_posted++;
6954
6955 /* keep track of last sgl's xritag */
6956 last_xritag = sglq_entry->sli4_xritag;
6957
James Smart895427b2017-02-12 13:52:30 -08006958 /* end of repost sgl list condition for buffers */
6959 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006960 if (post_cnt == 0) {
6961 list_splice_init(&prep_sgl_list,
6962 &blck_sgl_list);
6963 post_cnt = block_cnt;
6964 } else if (block_cnt == 1) {
6965 status = lpfc_sli4_post_sgl(phba,
6966 sglq_entry->phys, 0,
6967 sglq_entry->sli4_xritag);
6968 if (!status) {
6969 /* successful, put sgl to posted list */
6970 list_add_tail(&sglq_entry->list,
6971 &post_sgl_list);
6972 } else {
6973 /* Failure, put sgl to free list */
6974 lpfc_printf_log(phba, KERN_WARNING,
6975 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006976 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006977 "sgl, xritag:x%x\n",
6978 sglq_entry->sli4_xritag);
6979 list_add_tail(&sglq_entry->list,
6980 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006981 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006982 }
6983 }
6984 }
6985
6986 /* continue until a nembed page worth of sgls */
6987 if (post_cnt == 0)
6988 continue;
6989
James Smart895427b2017-02-12 13:52:30 -08006990 /* post the buffer list sgls as a block */
6991 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6992 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006993
6994 if (!status) {
6995 /* success, put sgl list to posted sgl list */
6996 list_splice_init(&blck_sgl_list, &post_sgl_list);
6997 } else {
6998 /* Failure, put sgl list to free sgl list */
6999 sglq_entry_first = list_first_entry(&blck_sgl_list,
7000 struct lpfc_sglq,
7001 list);
7002 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007003 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04007004 "xritag:x%x-x%x\n",
7005 sglq_entry_first->sli4_xritag,
7006 (sglq_entry_first->sli4_xritag +
7007 post_cnt - 1));
7008 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04007009 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007010 }
7011
7012 /* don't reset xirtag due to hole in xri block */
7013 if (block_cnt == 0)
7014 last_xritag = NO_XRI;
7015
James Smart895427b2017-02-12 13:52:30 -08007016 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04007017 post_cnt = 0;
7018 }
7019
James Smart895427b2017-02-12 13:52:30 -08007020 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04007021 lpfc_free_sgl_list(phba, &free_sgl_list);
7022
James Smart895427b2017-02-12 13:52:30 -08007023 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04007024 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05007025 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007026 spin_lock(&phba->sli4_hba.sgl_list_lock);
7027 list_splice_init(&post_sgl_list, sgl_list);
7028 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05007029 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04007030 } else {
7031 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007032 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04007033 return -EIO;
7034 }
James Smart895427b2017-02-12 13:52:30 -08007035
7036 /* return the number of XRIs actually posted */
7037 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007038}
7039
James Smart0794d602019-01-28 11:14:19 -08007040/**
James Smart5e5b5112019-01-28 11:14:22 -08007041 * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
James Smart0794d602019-01-28 11:14:19 -08007042 * @phba: pointer to lpfc hba data structure.
7043 *
7044 * This routine walks the list of nvme buffers that have been allocated and
7045 * repost them to the port by using SGL block post. This is needed after a
7046 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7047 * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
James Smart5e5b5112019-01-28 11:14:22 -08007048 * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
James Smart0794d602019-01-28 11:14:19 -08007049 *
7050 * Returns: 0 = success, non-zero failure.
7051 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07007052static int
James Smart5e5b5112019-01-28 11:14:22 -08007053lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
James Smart0794d602019-01-28 11:14:19 -08007054{
7055 LIST_HEAD(post_nblist);
7056 int num_posted, rc = 0;
7057
7058 /* get all NVME buffers need to repost to a local list */
James Smart5e5b5112019-01-28 11:14:22 -08007059 lpfc_io_buf_flush(phba, &post_nblist);
James Smart0794d602019-01-28 11:14:19 -08007060
7061 /* post the list of nvme buffer sgls to port if available */
7062 if (!list_empty(&post_nblist)) {
James Smart5e5b5112019-01-28 11:14:22 -08007063 num_posted = lpfc_sli4_post_io_sgl_list(
7064 phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
James Smart0794d602019-01-28 11:14:19 -08007065 /* failed to post any nvme buffer, return error */
7066 if (num_posted == 0)
7067 rc = -EIO;
7068 }
7069 return rc;
7070}
7071
Bart Van Assche3999df72019-03-28 11:06:16 -07007072static void
James Smart61bda8f2016-10-13 15:06:05 -07007073lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7074{
7075 uint32_t len;
7076
7077 len = sizeof(struct lpfc_mbx_set_host_data) -
7078 sizeof(struct lpfc_sli4_cfg_mhdr);
7079 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7080 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7081 LPFC_SLI4_MBX_EMBED);
7082
7083 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08007084 mbox->u.mqe.un.set_host_data.param_len =
7085 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07007086 snprintf(mbox->u.mqe.un.set_host_data.data,
7087 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7088 "Linux %s v"LPFC_DRIVER_VERSION,
7089 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7090}
7091
James Smarta8cf5df2017-05-15 15:20:46 -07007092int
James Smart6c621a22017-05-15 15:20:45 -07007093lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07007094 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07007095{
7096 int rc, i;
7097 struct lpfc_rqe hrqe;
7098 struct lpfc_rqe drqe;
7099 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08007100 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07007101 struct rqb_dmabuf *rqb_buffer;
7102 LIST_HEAD(rqb_buf_list);
7103
James Smart411de512018-01-30 15:58:52 -08007104 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007105 rqbp = hrq->rqbp;
7106 for (i = 0; i < count; i++) {
7107 /* IF RQ is already full, don't bother */
7108 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
7109 break;
7110 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7111 if (!rqb_buffer)
7112 break;
7113 rqb_buffer->hrq = hrq;
7114 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07007115 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07007116 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7117 }
7118 while (!list_empty(&rqb_buf_list)) {
7119 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7120 hbuf.list);
7121
7122 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7123 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7124 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7125 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7126 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7127 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08007128 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7129 "6421 Cannot post to HRQ %d: %x %x %x "
7130 "DRQ %x %x\n",
7131 hrq->queue_id,
7132 hrq->host_index,
7133 hrq->hba_index,
7134 hrq->entry_count,
7135 drq->host_index,
7136 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07007137 rqbp->rqb_free_buffer(phba, rqb_buffer);
7138 } else {
7139 list_add_tail(&rqb_buffer->hbuf.list,
7140 &rqbp->rqb_buffer_list);
7141 rqbp->buffer_count++;
7142 }
7143 }
James Smart411de512018-01-30 15:58:52 -08007144 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007145 return 1;
7146}
7147
James Smart8a9d2e82012-05-09 21:16:12 -04007148/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08007149 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04007150 * @phba: Pointer to HBA context object.
7151 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08007152 * This function is the main SLI4 device initialization PCI function. This
7153 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04007154 * HBA error attention handler code. Caller is not required to hold any
7155 * locks.
7156 **/
7157int
7158lpfc_sli4_hba_setup(struct lpfc_hba *phba)
7159{
James Smartc4908502019-01-28 11:14:28 -08007160 int rc, i, cnt, len;
James Smartda0436e2009-05-22 14:51:39 -04007161 LPFC_MBOXQ_t *mboxq;
7162 struct lpfc_mqe *mqe;
7163 uint8_t *vpd;
7164 uint32_t vpd_size;
7165 uint32_t ftr_rsp = 0;
7166 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
7167 struct lpfc_vport *vport = phba->pport;
7168 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08007169 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04007170
7171 /* Perform a PCI function reset to start from clean */
7172 rc = lpfc_pci_function_reset(phba);
7173 if (unlikely(rc))
7174 return -ENODEV;
7175
7176 /* Check the HBA Host Status Register for readyness */
7177 rc = lpfc_sli4_post_status_check(phba);
7178 if (unlikely(rc))
7179 return -ENODEV;
7180 else {
7181 spin_lock_irq(&phba->hbalock);
7182 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
7183 spin_unlock_irq(&phba->hbalock);
7184 }
7185
7186 /*
7187 * Allocate a single mailbox container for initializing the
7188 * port.
7189 */
7190 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7191 if (!mboxq)
7192 return -ENOMEM;
7193
James Smartda0436e2009-05-22 14:51:39 -04007194 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04007195 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04007196 vpd = kzalloc(vpd_size, GFP_KERNEL);
7197 if (!vpd) {
7198 rc = -ENOMEM;
7199 goto out_free_mbox;
7200 }
7201
7202 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05007203 if (unlikely(rc)) {
7204 kfree(vpd);
7205 goto out_free_mbox;
7206 }
James Smart572709e2013-07-15 18:32:43 -04007207
James Smartda0436e2009-05-22 14:51:39 -04007208 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04007209 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07007210 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05007211 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007212 phba->fcp_embed_io = 0; /* SLI4 FC support only */
7213 } else {
James Smart76a95d72010-11-20 23:11:48 -05007214 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007215 }
James Smart45ed1192009-10-02 15:17:02 -04007216
7217 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
7218 LPFC_DCBX_CEE_MODE)
7219 phba->hba_flag |= HBA_FIP_SUPPORT;
7220 else
7221 phba->hba_flag &= ~HBA_FIP_SUPPORT;
7222
James Smart4f2e66c2012-05-09 21:17:07 -04007223 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
7224
James Smartc31098c2011-04-16 11:03:33 -04007225 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007226 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7227 "0376 READ_REV Error. SLI Level %d "
7228 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05007229 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04007230 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007231 kfree(vpd);
7232 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007233 }
James Smartcd1c8302011-10-10 21:33:25 -04007234
7235 /*
James Smartff78d8f2011-12-13 13:21:35 -05007236 * Continue initialization with default values even if driver failed
7237 * to read FCoE param config regions, only read parameters if the
7238 * board is FCoE
7239 */
7240 if (phba->hba_flag & HBA_FCOE_MODE &&
7241 lpfc_sli4_read_fcoe_params(phba))
7242 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
7243 "2570 Failed to read FCoE parameters\n");
7244
7245 /*
James Smartcd1c8302011-10-10 21:33:25 -04007246 * Retrieve sli4 device physical port name, failure of doing it
7247 * is considered as non-fatal.
7248 */
7249 rc = lpfc_sli4_retrieve_pport_name(phba);
7250 if (!rc)
7251 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7252 "3080 Successful retrieving SLI4 device "
7253 "physical port name: %s.\n", phba->Port);
7254
James Smartb3b4f3e2019-03-12 16:30:23 -07007255 rc = lpfc_sli4_get_ctl_attr(phba);
7256 if (!rc)
7257 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7258 "8351 Successful retrieving SLI4 device "
7259 "CTL ATTR\n");
7260
James Smartda0436e2009-05-22 14:51:39 -04007261 /*
7262 * Evaluate the read rev and vpd data. Populate the driver
7263 * state with the results. If this routine fails, the failure
7264 * is not fatal as the driver will use generic values.
7265 */
7266 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
7267 if (unlikely(!rc)) {
7268 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7269 "0377 Error %d parsing vpd. "
7270 "Using defaults.\n", rc);
7271 rc = 0;
7272 }
James Smart76a95d72010-11-20 23:11:48 -05007273 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04007274
James Smartf1126682009-06-10 17:22:44 -04007275 /* Save information as VPD data */
7276 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
7277 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
James Smart4e565cf2018-02-22 08:18:50 -08007278
7279 /*
7280 * This is because first G7 ASIC doesn't support the standard
7281 * 0x5a NVME cmd descriptor type/subtype
7282 */
7283 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7284 LPFC_SLI_INTF_IF_TYPE_6) &&
7285 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
7286 (phba->vpd.rev.smRev == 0) &&
7287 (phba->cfg_nvme_embed_cmd == 1))
7288 phba->cfg_nvme_embed_cmd = 0;
7289
James Smartf1126682009-06-10 17:22:44 -04007290 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
7291 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
7292 &mqe->un.read_rev);
7293 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
7294 &mqe->un.read_rev);
7295 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
7296 &mqe->un.read_rev);
7297 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
7298 &mqe->un.read_rev);
7299 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
7300 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
7301 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
7302 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
7303 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
7304 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
7305 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7306 "(%d):0380 READ_REV Status x%x "
7307 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
7308 mboxq->vport ? mboxq->vport->vpi : 0,
7309 bf_get(lpfc_mqe_status, mqe),
7310 phba->vpd.rev.opFwName,
7311 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
7312 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04007313
James Smart572709e2013-07-15 18:32:43 -04007314 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
7315 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
7316 if (phba->pport->cfg_lun_queue_depth > rc) {
7317 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7318 "3362 LUN queue depth changed from %d to %d\n",
7319 phba->pport->cfg_lun_queue_depth, rc);
7320 phba->pport->cfg_lun_queue_depth = rc;
7321 }
7322
James Smart65791f12016-07-06 12:35:56 -07007323 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07007324 LPFC_SLI_INTF_IF_TYPE_0) {
7325 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
7326 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7327 if (rc == MBX_SUCCESS) {
7328 phba->hba_flag |= HBA_RECOVERABLE_UE;
7329 /* Set 1Sec interval to detect UE */
7330 phba->eratt_poll_interval = 1;
7331 phba->sli4_hba.ue_to_sr = bf_get(
7332 lpfc_mbx_set_feature_UESR,
7333 &mboxq->u.mqe.un.set_feature);
7334 phba->sli4_hba.ue_to_rp = bf_get(
7335 lpfc_mbx_set_feature_UERP,
7336 &mboxq->u.mqe.un.set_feature);
7337 }
7338 }
7339
7340 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
7341 /* Enable MDS Diagnostics only if the SLI Port supports it */
7342 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
7343 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7344 if (rc != MBX_SUCCESS)
7345 phba->mds_diags_support = 0;
7346 }
James Smart572709e2013-07-15 18:32:43 -04007347
James Smartda0436e2009-05-22 14:51:39 -04007348 /*
7349 * Discover the port's supported feature set and match it against the
7350 * hosts requests.
7351 */
7352 lpfc_request_features(phba, mboxq);
7353 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7354 if (unlikely(rc)) {
7355 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007356 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007357 }
7358
7359 /*
7360 * The port must support FCP initiator mode as this is the
7361 * only mode running in the host.
7362 */
7363 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7364 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7365 "0378 No support for fcpi mode.\n");
7366 ftr_rsp++;
7367 }
James Smart0bc2b7c2018-02-22 08:18:48 -08007368
7369 /* Performance Hints are ONLY for FCoE */
7370 if (phba->hba_flag & HBA_FCOE_MODE) {
7371 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7372 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7373 else
7374 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7375 }
7376
James Smartda0436e2009-05-22 14:51:39 -04007377 /*
7378 * If the port cannot support the host's requested features
7379 * then turn off the global config parameters to disable the
7380 * feature in the driver. This is not a fatal error.
7381 */
James Smartf44ac122018-03-05 12:04:08 -08007382 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7383 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7384 phba->cfg_enable_bg = 0;
7385 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smartbf086112011-08-21 21:48:13 -04007386 ftr_rsp++;
James Smartf44ac122018-03-05 12:04:08 -08007387 }
James Smartbf086112011-08-21 21:48:13 -04007388 }
James Smartda0436e2009-05-22 14:51:39 -04007389
7390 if (phba->max_vpi && phba->cfg_enable_npiv &&
7391 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7392 ftr_rsp++;
7393
7394 if (ftr_rsp) {
7395 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7396 "0379 Feature Mismatch Data: x%08x %08x "
7397 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7398 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7399 phba->cfg_enable_npiv, phba->max_vpi);
7400 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7401 phba->cfg_enable_bg = 0;
7402 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7403 phba->cfg_enable_npiv = 0;
7404 }
7405
7406 /* These SLI3 features are assumed in SLI4 */
7407 spin_lock_irq(&phba->hbalock);
7408 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7409 spin_unlock_irq(&phba->hbalock);
7410
James Smart6d368e52011-05-24 11:44:12 -04007411 /*
7412 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
7413 * calls depends on these resources to complete port setup.
7414 */
7415 rc = lpfc_sli4_alloc_resource_identifiers(phba);
7416 if (rc) {
7417 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7418 "2920 Failed to alloc Resource IDs "
7419 "rc = x%x\n", rc);
7420 goto out_free_mbox;
7421 }
7422
James Smart61bda8f2016-10-13 15:06:05 -07007423 lpfc_set_host_data(phba, mboxq);
7424
7425 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7426 if (rc) {
7427 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7428 "2134 Failed to set host os driver version %x",
7429 rc);
7430 }
7431
James Smartda0436e2009-05-22 14:51:39 -04007432 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05007433 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7434 if (rc) {
7435 phba->link_state = LPFC_HBA_ERROR;
7436 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05007437 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05007438 }
7439
James Smartda0436e2009-05-22 14:51:39 -04007440 mboxq->vport = vport;
7441 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart3e1f0712018-11-29 16:09:29 -08007442 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04007443 if (rc == MBX_SUCCESS) {
7444 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7445 rc = 0;
7446 }
7447
7448 /*
7449 * This memory was allocated by the lpfc_read_sparam routine. Release
7450 * it to the mbuf pool.
7451 */
7452 lpfc_mbuf_free(phba, mp->virt, mp->phys);
7453 kfree(mp);
James Smart3e1f0712018-11-29 16:09:29 -08007454 mboxq->ctx_buf = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007455 if (unlikely(rc)) {
7456 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7457 "0382 READ_SPARAM command failed "
7458 "status %d, mbxStatus x%x\n",
7459 rc, bf_get(lpfc_mqe_status, mqe));
7460 phba->link_state = LPFC_HBA_ERROR;
7461 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007462 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007463 }
7464
James Smart05580562011-05-24 11:40:48 -04007465 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04007466
7467 /* Update the fc_host data structures with new wwn. */
7468 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7469 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7470
James Smart895427b2017-02-12 13:52:30 -08007471 /* Create all the SLI4 queues */
7472 rc = lpfc_sli4_queue_create(phba);
7473 if (rc) {
7474 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7475 "3089 Failed to allocate queues\n");
7476 rc = -ENODEV;
7477 goto out_free_mbox;
7478 }
7479 /* Set up all the queues to the device */
7480 rc = lpfc_sli4_queue_setup(phba);
7481 if (unlikely(rc)) {
7482 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7483 "0381 Error %d during queue setup.\n ", rc);
7484 goto out_stop_timers;
7485 }
7486 /* Initialize the driver internal SLI layer lists. */
7487 lpfc_sli4_setup(phba);
7488 lpfc_sli4_queue_init(phba);
7489
7490 /* update host els xri-sgl sizes and mappings */
7491 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007492 if (unlikely(rc)) {
7493 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7494 "1400 Failed to update xri-sgl size and "
7495 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08007496 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007497 }
7498
James Smart8a9d2e82012-05-09 21:16:12 -04007499 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08007500 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7501 phba->sli4_hba.els_xri_cnt);
7502 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04007503 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7504 "0582 Error %d during els sgl post "
7505 "operation\n", rc);
7506 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007507 goto out_destroy_queue;
7508 }
7509 phba->sli4_hba.els_xri_cnt = rc;
7510
James Smartf358dd02017-02-12 13:52:34 -08007511 if (phba->nvmet_support) {
7512 /* update host nvmet xri-sgl sizes and mappings */
7513 rc = lpfc_sli4_nvmet_sgl_update(phba);
7514 if (unlikely(rc)) {
7515 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7516 "6308 Failed to update nvmet-sgl size "
7517 "and mapping: %d\n", rc);
7518 goto out_destroy_queue;
7519 }
7520
7521 /* register the nvmet sgl pool to the port */
7522 rc = lpfc_sli4_repost_sgl_list(
7523 phba,
7524 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7525 phba->sli4_hba.nvmet_xri_cnt);
7526 if (unlikely(rc < 0)) {
7527 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7528 "3117 Error %d during nvmet "
7529 "sgl post\n", rc);
7530 rc = -ENODEV;
7531 goto out_destroy_queue;
7532 }
7533 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07007534
7535 cnt = phba->cfg_iocb_cnt * 1024;
7536 /* We need 1 iocbq for every SGL, for IO processing */
7537 cnt += phba->sli4_hba.nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08007538 } else {
James Smart0794d602019-01-28 11:14:19 -08007539 /* update host common xri-sgl sizes and mappings */
James Smart5e5b5112019-01-28 11:14:22 -08007540 rc = lpfc_sli4_io_sgl_update(phba);
James Smart895427b2017-02-12 13:52:30 -08007541 if (unlikely(rc)) {
7542 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7543 "6082 Failed to update nvme-sgl size "
7544 "and mapping: %d\n", rc);
7545 goto out_destroy_queue;
7546 }
James Smart6c621a22017-05-15 15:20:45 -07007547
James Smart0794d602019-01-28 11:14:19 -08007548 /* register the allocated common sgl pool to the port */
James Smart5e5b5112019-01-28 11:14:22 -08007549 rc = lpfc_sli4_repost_io_sgl_list(phba);
James Smart0794d602019-01-28 11:14:19 -08007550 if (unlikely(rc)) {
7551 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7552 "6116 Error %d during nvme sgl post "
7553 "operation\n", rc);
7554 /* Some NVME buffers were moved to abort nvme list */
7555 /* A pci function reset will repost them */
7556 rc = -ENODEV;
7557 goto out_destroy_queue;
7558 }
James Smart6c621a22017-05-15 15:20:45 -07007559 cnt = phba->cfg_iocb_cnt * 1024;
James Smart11e644e2017-06-15 22:56:48 -07007560 }
7561
7562 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07007563 /* Initialize and populate the iocb list per host */
7564 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007565 "2821 initialize iocb list %d total %d\n",
James Smart6c621a22017-05-15 15:20:45 -07007566 phba->cfg_iocb_cnt, cnt);
7567 rc = lpfc_init_iocb_list(phba, cnt);
7568 if (rc) {
7569 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007570 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07007571 goto out_destroy_queue;
7572 }
James Smart8a9d2e82012-05-09 21:16:12 -04007573 }
7574
James Smart11e644e2017-06-15 22:56:48 -07007575 if (phba->nvmet_support)
7576 lpfc_nvmet_create_targetport(phba);
7577
James Smart2d7dbc42017-02-12 13:52:35 -08007578 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007579 /* Post initial buffers to all RQs created */
7580 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7581 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7582 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7583 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7584 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007585 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007586 rqbp->buffer_count = 0;
7587
James Smart2d7dbc42017-02-12 13:52:35 -08007588 lpfc_post_rq_buffer(
7589 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7590 phba->sli4_hba.nvmet_mrq_data[i],
James Smart2448e482018-04-09 14:24:24 -07007591 phba->cfg_nvmet_mrq_post, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007592 }
7593 }
7594
James Smartda0436e2009-05-22 14:51:39 -04007595 /* Post the rpi header region to the device. */
7596 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7597 if (unlikely(rc)) {
7598 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7599 "0393 Error %d during rpi post operation\n",
7600 rc);
7601 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007602 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007603 }
James Smart97f2ecf2012-03-01 22:35:23 -05007604 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007605
James Smart895427b2017-02-12 13:52:30 -08007606 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007607 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007608 /*
7609 * The FC Port needs to register FCFI (index 0)
7610 */
7611 lpfc_reg_fcfi(phba, mboxq);
7612 mboxq->vport = phba->pport;
7613 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7614 if (rc != MBX_SUCCESS)
7615 goto out_unset_queue;
7616 rc = 0;
7617 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7618 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007619 } else {
7620 /* We are a NVME Target mode with MRQ > 1 */
7621
7622 /* First register the FCFI */
7623 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
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;
7629 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7630 &mboxq->u.mqe.un.reg_fcfi_mrq);
7631
7632 /* Next register the MRQs */
7633 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7634 mboxq->vport = phba->pport;
7635 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7636 if (rc != MBX_SUCCESS)
7637 goto out_unset_queue;
7638 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007639 }
7640 /* Check if the port is configured to be disabled */
7641 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007642 }
7643
James Smartc4908502019-01-28 11:14:28 -08007644 /* Don't post more new bufs if repost already recovered
7645 * the nvme sgls.
7646 */
7647 if (phba->nvmet_support == 0) {
7648 if (phba->sli4_hba.io_xri_cnt == 0) {
7649 len = lpfc_new_io_buf(
7650 phba, phba->sli4_hba.io_xri_max);
7651 if (len == 0) {
7652 rc = -ENOMEM;
7653 goto out_unset_queue;
7654 }
7655
7656 if (phba->cfg_xri_rebalancing)
7657 lpfc_create_multixri_pools(phba);
7658 }
7659 } else {
7660 phba->cfg_xri_rebalancing = 0;
7661 }
7662
James Smartda0436e2009-05-22 14:51:39 -04007663 /* Allow asynchronous mailbox command to go through */
7664 spin_lock_irq(&phba->hbalock);
7665 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7666 spin_unlock_irq(&phba->hbalock);
7667
7668 /* Post receive buffers to the device */
7669 lpfc_sli4_rb_setup(phba);
7670
James Smartfc2b9892010-02-26 14:15:29 -05007671 /* Reset HBA FCF states after HBA reset */
7672 phba->fcf.fcf_flag = 0;
7673 phba->fcf.current_rec.flag = 0;
7674
James Smartda0436e2009-05-22 14:51:39 -04007675 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007676 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007677 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007678
7679 /* Start heart beat timer */
7680 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007681 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007682 phba->hb_outstanding = 0;
7683 phba->last_completion_time = jiffies;
7684
James Smart32517fc2019-01-28 11:14:33 -08007685 /* start eq_delay heartbeat */
7686 if (phba->cfg_auto_imax)
7687 queue_delayed_work(phba->wq, &phba->eq_delay_work,
7688 msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
7689
James Smartda0436e2009-05-22 14:51:39 -04007690 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007691 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007692 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007693
James Smart75baf692010-06-08 18:31:21 -04007694 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7695 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7696 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7697 if (!rc) {
7698 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7699 "2829 This device supports "
7700 "Advanced Error Reporting (AER)\n");
7701 spin_lock_irq(&phba->hbalock);
7702 phba->hba_flag |= HBA_AER_ENABLED;
7703 spin_unlock_irq(&phba->hbalock);
7704 } else {
7705 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7706 "2830 This device does not support "
7707 "Advanced Error Reporting (AER)\n");
7708 phba->cfg_aer_support = 0;
7709 }
James Smart0a96e972011-07-22 18:37:28 -04007710 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007711 }
7712
James Smartda0436e2009-05-22 14:51:39 -04007713 /*
7714 * The port is ready, set the host's link state to LINK_DOWN
7715 * in preparation for link interrupts.
7716 */
James Smartda0436e2009-05-22 14:51:39 -04007717 spin_lock_irq(&phba->hbalock);
7718 phba->link_state = LPFC_LINK_DOWN;
James Smart1dc5ec22018-10-23 13:41:11 -07007719
7720 /* Check if physical ports are trunked */
7721 if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
7722 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
7723 if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
7724 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
7725 if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
7726 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
7727 if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
7728 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04007729 spin_unlock_irq(&phba->hbalock);
James Smart1dc5ec22018-10-23 13:41:11 -07007730
James Smarte8869f52019-03-12 16:30:18 -07007731 /* Arm the CQs and then EQs on device */
7732 lpfc_sli4_arm_cqeq_intr(phba);
7733
7734 /* Indicate device interrupt mode */
7735 phba->sli4_hba.intr_enable = 1;
7736
James Smart026abb82011-12-13 13:20:45 -05007737 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7738 (phba->hba_flag & LINK_DISABLED)) {
7739 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7740 "3103 Adapter Link is disabled.\n");
7741 lpfc_down_link(phba, mboxq);
7742 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7743 if (rc != MBX_SUCCESS) {
7744 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7745 "3104 Adapter failed to issue "
7746 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
James Smartc4908502019-01-28 11:14:28 -08007747 goto out_io_buff_free;
James Smart026abb82011-12-13 13:20:45 -05007748 }
7749 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007750 /* don't perform init_link on SLI4 FC port loopback test */
7751 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7752 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7753 if (rc)
James Smartc4908502019-01-28 11:14:28 -08007754 goto out_io_buff_free;
James Smart1b511972011-12-13 13:23:09 -05007755 }
James Smart5350d872011-10-10 21:33:49 -04007756 }
7757 mempool_free(mboxq, phba->mbox_mem_pool);
7758 return rc;
James Smartc4908502019-01-28 11:14:28 -08007759out_io_buff_free:
7760 /* Free allocated IO Buffers */
7761 lpfc_io_free(phba);
James Smart76a95d72010-11-20 23:11:48 -05007762out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007763 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007764 lpfc_sli4_queue_unset(phba);
7765out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007766 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007767 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007768out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007769 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007770out_free_mbox:
7771 mempool_free(mboxq, phba->mbox_mem_pool);
7772 return rc;
7773}
James Smarte59058c2008-08-24 21:49:00 -04007774
7775/**
James Smart3621a712009-04-06 18:47:14 -04007776 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007777 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007778 *
James Smarte59058c2008-08-24 21:49:00 -04007779 * This is the callback function for mailbox timer. The mailbox
7780 * timer is armed when a new mailbox command is issued and the timer
7781 * is deleted when the mailbox complete. The function is called by
7782 * the kernel timer code when a mailbox does not complete within
7783 * expected time. This function wakes up the worker thread to
7784 * process the mailbox timeout and returns. All the processing is
7785 * done by the worker thread function lpfc_mbox_timeout_handler.
7786 **/
dea31012005-04-17 16:05:31 -05007787void
Kees Cookf22eb4d2017-09-06 20:24:26 -07007788lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05007789{
Kees Cookf22eb4d2017-09-06 20:24:26 -07007790 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05007791 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007792 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007793
James Smart2e0fef82007-06-17 19:56:36 -05007794 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007795 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007796 if (!tmo_posted)
7797 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7798 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7799
James Smart5e9d9b82008-06-14 22:52:53 -04007800 if (!tmo_posted)
7801 lpfc_worker_wake_up(phba);
7802 return;
dea31012005-04-17 16:05:31 -05007803}
7804
James Smarte8d3c3b2013-10-10 12:21:30 -04007805/**
7806 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7807 * are pending
7808 * @phba: Pointer to HBA context object.
7809 *
7810 * This function checks if any mailbox completions are present on the mailbox
7811 * completion queue.
7812 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007813static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007814lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7815{
7816
7817 uint32_t idx;
7818 struct lpfc_queue *mcq;
7819 struct lpfc_mcqe *mcqe;
7820 bool pending_completions = false;
James Smart7365f6f2018-02-22 08:18:46 -08007821 uint8_t qe_valid;
James Smarte8d3c3b2013-10-10 12:21:30 -04007822
7823 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7824 return false;
7825
7826 /* Check for completions on mailbox completion queue */
7827
7828 mcq = phba->sli4_hba.mbx_cq;
7829 idx = mcq->hba_index;
James Smart7365f6f2018-02-22 08:18:46 -08007830 qe_valid = mcq->qe_valid;
James Smart9afbee32019-03-12 16:30:28 -07007831 while (bf_get_le32(lpfc_cqe_valid,
7832 (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
7833 mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
James Smarte8d3c3b2013-10-10 12:21:30 -04007834 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7835 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7836 pending_completions = true;
7837 break;
7838 }
7839 idx = (idx + 1) % mcq->entry_count;
7840 if (mcq->hba_index == idx)
7841 break;
James Smart7365f6f2018-02-22 08:18:46 -08007842
7843 /* if the index wrapped around, toggle the valid bit */
7844 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7845 qe_valid = (qe_valid) ? 0 : 1;
James Smarte8d3c3b2013-10-10 12:21:30 -04007846 }
7847 return pending_completions;
7848
7849}
7850
7851/**
7852 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7853 * that were missed.
7854 * @phba: Pointer to HBA context object.
7855 *
7856 * For sli4, it is possible to miss an interrupt. As such mbox completions
7857 * maybe missed causing erroneous mailbox timeouts to occur. This function
7858 * checks to see if mbox completions are on the mailbox completion queue
7859 * and will process all the completions associated with the eq for the
7860 * mailbox completion queue.
7861 **/
YueHaibingd7b761b2019-05-31 23:28:41 +08007862static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007863lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7864{
James Smartb71413d2018-02-22 08:18:40 -08007865 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04007866 uint32_t eqidx;
7867 struct lpfc_queue *fpeq = NULL;
James Smart657add42019-05-21 17:49:06 -07007868 struct lpfc_queue *eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04007869 bool mbox_pending;
7870
7871 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7872 return false;
7873
James Smart657add42019-05-21 17:49:06 -07007874 /* Find the EQ associated with the mbox CQ */
7875 if (sli4_hba->hdwq) {
7876 for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
7877 eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
7878 if (eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
7879 fpeq = eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04007880 break;
7881 }
James Smart657add42019-05-21 17:49:06 -07007882 }
7883 }
James Smarte8d3c3b2013-10-10 12:21:30 -04007884 if (!fpeq)
7885 return false;
7886
7887 /* Turn off interrupts from this EQ */
7888
James Smartb71413d2018-02-22 08:18:40 -08007889 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04007890
7891 /* Check to see if a mbox completion is pending */
7892
7893 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7894
7895 /*
7896 * If a mbox completion is pending, process all the events on EQ
7897 * associated with the mbox completion queue (this could include
7898 * mailbox commands, async events, els commands, receive queue data
7899 * and fcp commands)
7900 */
7901
7902 if (mbox_pending)
James Smart32517fc2019-01-28 11:14:33 -08007903 /* process and rearm the EQ */
7904 lpfc_sli4_process_eq(phba, fpeq);
7905 else
7906 /* Always clear and re-arm the EQ */
7907 sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04007908
7909 return mbox_pending;
7910
7911}
James Smarte59058c2008-08-24 21:49:00 -04007912
7913/**
James Smart3621a712009-04-06 18:47:14 -04007914 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007915 * @phba: Pointer to HBA context object.
7916 *
7917 * This function is called from worker thread when a mailbox command times out.
7918 * The caller is not required to hold any locks. This function will reset the
7919 * HBA and recover all the pending commands.
7920 **/
dea31012005-04-17 16:05:31 -05007921void
7922lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7923{
James Smart2e0fef82007-06-17 19:56:36 -05007924 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007925 MAILBOX_t *mb = NULL;
7926
James Smart1dcb58e2007-04-25 09:51:30 -04007927 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007928
James Smarte8d3c3b2013-10-10 12:21:30 -04007929 /* If the mailbox completed, process the completion and return */
7930 if (lpfc_sli4_process_missed_mbox_completions(phba))
7931 return;
7932
James Smarteb016562014-09-03 12:58:06 -04007933 if (pmbox != NULL)
7934 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007935 /* Check the pmbox pointer first. There is a race condition
7936 * between the mbox timeout handler getting executed in the
7937 * worklist and the mailbox actually completing. When this
7938 * race condition occurs, the mbox_active will be NULL.
7939 */
7940 spin_lock_irq(&phba->hbalock);
7941 if (pmbox == NULL) {
7942 lpfc_printf_log(phba, KERN_WARNING,
7943 LOG_MBOX | LOG_SLI,
7944 "0353 Active Mailbox cleared - mailbox timeout "
7945 "exiting\n");
7946 spin_unlock_irq(&phba->hbalock);
7947 return;
7948 }
7949
dea31012005-04-17 16:05:31 -05007950 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007951 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007952 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007953 mb->mbxCommand,
7954 phba->pport->port_state,
7955 phba->sli.sli_flag,
7956 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007957 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007958
James Smart1dcb58e2007-04-25 09:51:30 -04007959 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7960 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007961 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007962 */
James Smart2e0fef82007-06-17 19:56:36 -05007963 spin_lock_irq(&phba->pport->work_port_lock);
7964 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7965 spin_unlock_irq(&phba->pport->work_port_lock);
7966 spin_lock_irq(&phba->hbalock);
7967 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007968 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007969 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007970
James Smartdb55fba2014-04-04 13:52:02 -04007971 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007972
7973 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007974 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007975
7976 /* Reset the HBA device */
7977 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007978}
7979
James Smarte59058c2008-08-24 21:49:00 -04007980/**
James Smart3772a992009-05-22 14:50:54 -04007981 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007982 * @phba: Pointer to HBA context object.
7983 * @pmbox: Pointer to mailbox object.
7984 * @flag: Flag indicating how the mailbox need to be processed.
7985 *
7986 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007987 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7988 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007989 * The mailbox command can be submitted in polling mode, in which case
7990 * this function will wait in a polling loop for the completion of the
7991 * mailbox.
7992 * If the mailbox is submitted in no_wait mode (not polling) the
7993 * function will submit the command and returns immediately without waiting
7994 * for the mailbox completion. The no_wait is supported only when HBA
7995 * is in SLI2/SLI3 mode - interrupts are enabled.
7996 * The SLI interface allows only one mailbox pending at a time. If the
7997 * mailbox is issued in polling mode and there is already a mailbox
7998 * pending, then the function will return an error. If the mailbox is issued
7999 * in NO_WAIT mode and there is a mailbox pending already, the function
8000 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
8001 * The sli layer owns the mailbox object until the completion of mailbox
8002 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
8003 * return codes the caller owns the mailbox command after the return of
8004 * the function.
8005 **/
James Smart3772a992009-05-22 14:50:54 -04008006static int
8007lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
8008 uint32_t flag)
dea31012005-04-17 16:05:31 -05008009{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008010 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05008011 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008012 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05008013 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008014 int i;
James Smart09372822008-01-11 01:52:54 -05008015 unsigned long timeout;
dea31012005-04-17 16:05:31 -05008016 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04008017 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05008018 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04008019 int processing_queue = 0;
8020
8021 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8022 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04008023 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04008024 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04008025 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8026 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8027 return MBX_SUCCESS;
8028 }
James Smart58da1ff2008-04-07 10:15:56 -04008029 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04008030 pmbox = lpfc_mbox_get(phba);
8031 if (!pmbox) {
8032 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8033 return MBX_SUCCESS;
8034 }
8035 }
dea31012005-04-17 16:05:31 -05008036
James Smarted957682007-06-17 19:56:37 -05008037 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05008038 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05008039 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04008040 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05008041 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05008042 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008043 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04008044 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05008045 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04008046 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05008047 }
8048 }
8049
Linas Vepstas8d63f372007-02-14 14:28:36 -06008050 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04008051 if (unlikely(pci_channel_offline(phba->pcidev))) {
8052 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8053 goto out_not_finished;
8054 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06008055
James Smarta257bf92009-04-06 18:48:10 -04008056 /* If HBA has a deferred error attention, fail the iocb. */
8057 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8058 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8059 goto out_not_finished;
8060 }
8061
dea31012005-04-17 16:05:31 -05008062 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05008063
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008064 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05008065 status = MBX_SUCCESS;
8066
James Smart2e0fef82007-06-17 19:56:36 -05008067 if (phba->link_state == LPFC_HBA_ERROR) {
8068 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05008069
8070 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008071 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8072 "(%d):0311 Mailbox command x%x cannot "
8073 "issue Data: x%x x%x\n",
8074 pmbox->vport ? pmbox->vport->vpi : 0,
8075 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008076 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05008077 }
8078
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008079 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05008080 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
8081 !(hc_copy & HC_MBINT_ENA)) {
8082 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8083 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04008084 "(%d):2528 Mailbox command x%x cannot "
8085 "issue Data: x%x x%x\n",
8086 pmbox->vport ? pmbox->vport->vpi : 0,
8087 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05008088 goto out_not_finished;
8089 }
James Smart92908312006-03-07 15:04:13 -05008090 }
8091
dea31012005-04-17 16:05:31 -05008092 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8093 /* Polling for a mbox command when another one is already active
8094 * is not allowed in SLI. Also, the driver must have established
8095 * SLI2 mode to queue and process multiple mbox commands.
8096 */
8097
8098 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05008099 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008100
8101 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008102 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8103 "(%d):2529 Mailbox command x%x "
8104 "cannot issue Data: x%x x%x\n",
8105 pmbox->vport ? pmbox->vport->vpi : 0,
8106 pmbox->u.mb.mbxCommand,
8107 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008108 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008109 }
8110
James Smart3772a992009-05-22 14:50:54 -04008111 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05008112 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008113 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008114 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8115 "(%d):2530 Mailbox command x%x "
8116 "cannot issue Data: x%x x%x\n",
8117 pmbox->vport ? pmbox->vport->vpi : 0,
8118 pmbox->u.mb.mbxCommand,
8119 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008120 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008121 }
8122
dea31012005-04-17 16:05:31 -05008123 /* Another mailbox command is still being processed, queue this
8124 * command to be processed later.
8125 */
8126 lpfc_mbox_put(phba, pmbox);
8127
8128 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05008129 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008130 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05008131 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008132 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07008133 mbx->mbxCommand,
8134 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008135 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008136
8137 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05008138 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008139
James Smart858c9f62007-06-17 19:56:39 -05008140 if (pmbox->vport) {
8141 lpfc_debugfs_disc_trc(pmbox->vport,
8142 LPFC_DISC_TRC_MBOX_VPORT,
8143 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008144 (uint32_t)mbx->mbxCommand,
8145 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008146 }
8147 else {
8148 lpfc_debugfs_disc_trc(phba->pport,
8149 LPFC_DISC_TRC_MBOX,
8150 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008151 (uint32_t)mbx->mbxCommand,
8152 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008153 }
8154
James Smart2e0fef82007-06-17 19:56:36 -05008155 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05008156 }
8157
dea31012005-04-17 16:05:31 -05008158 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8159
8160 /* If we are not polling, we MUST be in SLI2 mode */
8161 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04008162 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008163 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05008164 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008165 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008166 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008167 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8168 "(%d):2531 Mailbox command x%x "
8169 "cannot issue Data: x%x x%x\n",
8170 pmbox->vport ? pmbox->vport->vpi : 0,
8171 pmbox->u.mb.mbxCommand,
8172 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008173 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008174 }
8175 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04008176 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8177 1000);
8178 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05008179 }
8180
8181 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05008182 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008183 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05008184 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008185 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07008186 mbx->mbxCommand,
8187 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008188 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008189
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008190 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05008191 if (pmbox->vport) {
8192 lpfc_debugfs_disc_trc(pmbox->vport,
8193 LPFC_DISC_TRC_MBOX_VPORT,
8194 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008195 (uint32_t)mbx->mbxCommand,
8196 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008197 }
8198 else {
8199 lpfc_debugfs_disc_trc(phba->pport,
8200 LPFC_DISC_TRC_MBOX,
8201 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008202 (uint32_t)mbx->mbxCommand,
8203 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008204 }
8205 }
8206
dea31012005-04-17 16:05:31 -05008207 psli->slistat.mbox_cmd++;
8208 evtctr = psli->slistat.mbox_event;
8209
8210 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008211 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05008212
James Smart3772a992009-05-22 14:50:54 -04008213 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04008214 /* Populate mbox extension offset word. */
8215 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008216 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008217 = (uint8_t *)phba->mbox_ext
8218 - (uint8_t *)phba->mbox;
8219 }
8220
8221 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008222 if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
8223 lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
8224 (uint8_t *)phba->mbox_ext,
8225 pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008226 }
8227 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008228 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008229 } else {
James Smart7a470272010-03-15 11:25:20 -04008230 /* Populate mbox extension offset word. */
8231 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008232 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008233 = MAILBOX_HBA_EXT_OFFSET;
8234
8235 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008236 if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -04008237 lpfc_memcpy_to_slim(phba->MBslimaddr +
8238 MAILBOX_HBA_EXT_OFFSET,
James Smart3e1f0712018-11-29 16:09:29 -08008239 pmbox->ctx_buf, pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008240
James Smart895427b2017-02-12 13:52:30 -08008241 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008242 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08008243 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
8244 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008245
8246 /* First copy mbox command data to HBA SLIM, skip past first
8247 word */
8248 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008249 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05008250 MAILBOX_CMD_SIZE - sizeof (uint32_t));
8251
8252 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008253 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05008254 to_slim = phba->MBslimaddr;
8255 writel(ldata, to_slim);
8256 readl(to_slim); /* flush */
8257
James Smart895427b2017-02-12 13:52:30 -08008258 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008259 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04008260 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008261 }
8262
8263 wmb();
dea31012005-04-17 16:05:31 -05008264
8265 switch (flag) {
8266 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05008267 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05008268 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05008269 /* Interrupt board to do it */
8270 writel(CA_MBATT, phba->CAregaddr);
8271 readl(phba->CAregaddr); /* flush */
8272 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05008273 break;
8274
8275 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05008276 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05008277 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05008278 /* Interrupt board to do it */
8279 writel(CA_MBATT, phba->CAregaddr);
8280 readl(phba->CAregaddr); /* flush */
8281
James Smart3772a992009-05-22 14:50:54 -04008282 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008283 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04008284 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008285 word0 = le32_to_cpu(word0);
8286 } else {
8287 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05008288 if (lpfc_readl(phba->MBslimaddr, &word0)) {
8289 spin_unlock_irqrestore(&phba->hbalock,
8290 drvr_flag);
8291 goto out_not_finished;
8292 }
dea31012005-04-17 16:05:31 -05008293 }
8294
8295 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008296 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8297 spin_unlock_irqrestore(&phba->hbalock,
8298 drvr_flag);
8299 goto out_not_finished;
8300 }
James Smarta183a152011-10-10 21:32:43 -04008301 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8302 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05008303 i = 0;
dea31012005-04-17 16:05:31 -05008304 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05008305 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
8306 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05008307 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05008308 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05008309 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008310 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05008311 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04008312 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008313 }
8314
8315 /* Check if we took a mbox interrupt while we were
8316 polling */
8317 if (((word0 & OWN_CHIP) != OWN_CHIP)
8318 && (evtctr != psli->slistat.mbox_event))
8319 break;
8320
James Smart09372822008-01-11 01:52:54 -05008321 if (i++ > 10) {
8322 spin_unlock_irqrestore(&phba->hbalock,
8323 drvr_flag);
8324 msleep(1);
8325 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8326 }
dea31012005-04-17 16:05:31 -05008327
James Smart3772a992009-05-22 14:50:54 -04008328 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008329 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04008330 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008331 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008332 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05008333 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04008334 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05008335 /* Check real SLIM for any errors */
8336 slimword0 = readl(phba->MBslimaddr);
8337 slimmb = (MAILBOX_t *) & slimword0;
8338 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
8339 && slimmb->mbxStatus) {
8340 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04008341 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008342 word0 = slimword0;
8343 }
8344 }
8345 } else {
8346 /* First copy command data */
8347 word0 = readl(phba->MBslimaddr);
8348 }
8349 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008350 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8351 spin_unlock_irqrestore(&phba->hbalock,
8352 drvr_flag);
8353 goto out_not_finished;
8354 }
dea31012005-04-17 16:05:31 -05008355 }
8356
James Smart3772a992009-05-22 14:50:54 -04008357 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008358 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08008359 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
8360 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) {
James Smart7a470272010-03-15 11:25:20 -04008363 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -08008364 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008365 pmbox->out_ext_byte_len);
8366 }
dea31012005-04-17 16:05:31 -05008367 } else {
8368 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008369 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08008370 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008371 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008372 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
8373 lpfc_memcpy_from_slim(
8374 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008375 phba->MBslimaddr +
8376 MAILBOX_HBA_EXT_OFFSET,
8377 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05008378 }
8379 }
8380
8381 writel(HA_MBATT, phba->HAregaddr);
8382 readl(phba->HAregaddr); /* flush */
8383
8384 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008385 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05008386 }
8387
James Smart2e0fef82007-06-17 19:56:36 -05008388 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8389 return status;
James Smart58da1ff2008-04-07 10:15:56 -04008390
8391out_not_finished:
8392 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04008393 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04008394 lpfc_mbox_cmpl_put(phba, pmbox);
8395 }
8396 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008397}
8398
James Smarte59058c2008-08-24 21:49:00 -04008399/**
James Smartf1126682009-06-10 17:22:44 -04008400 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8401 * @phba: Pointer to HBA context object.
8402 *
8403 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8404 * the driver internal pending mailbox queue. It will then try to wait out the
8405 * possible outstanding mailbox command before return.
8406 *
8407 * Returns:
8408 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8409 * the outstanding mailbox command timed out.
8410 **/
8411static int
8412lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8413{
8414 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04008415 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04008416 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04008417
8418 /* Mark the asynchronous mailbox command posting as blocked */
8419 spin_lock_irq(&phba->hbalock);
8420 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04008421 /* Determine how long we might wait for the active mailbox
8422 * command to be gracefully completed by firmware.
8423 */
James Smarta183a152011-10-10 21:32:43 -04008424 if (phba->sli.mbox_active)
8425 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8426 phba->sli.mbox_active) *
8427 1000) + jiffies;
8428 spin_unlock_irq(&phba->hbalock);
8429
James Smarte8d3c3b2013-10-10 12:21:30 -04008430 /* Make sure the mailbox is really active */
8431 if (timeout)
8432 lpfc_sli4_process_missed_mbox_completions(phba);
8433
James Smartf1126682009-06-10 17:22:44 -04008434 /* Wait for the outstnading mailbox command to complete */
8435 while (phba->sli.mbox_active) {
8436 /* Check active mailbox complete status every 2ms */
8437 msleep(2);
8438 if (time_after(jiffies, timeout)) {
8439 /* Timeout, marked the outstanding cmd not complete */
8440 rc = 1;
8441 break;
8442 }
8443 }
8444
8445 /* Can not cleanly block async mailbox command, fails it */
8446 if (rc) {
8447 spin_lock_irq(&phba->hbalock);
8448 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8449 spin_unlock_irq(&phba->hbalock);
8450 }
8451 return rc;
8452}
8453
8454/**
8455 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8456 * @phba: Pointer to HBA context object.
8457 *
8458 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8459 * commands from the driver internal pending mailbox queue. It makes sure
8460 * that there is no outstanding mailbox command before resuming posting
8461 * asynchronous mailbox commands. If, for any reason, there is outstanding
8462 * mailbox command, it will try to wait it out before resuming asynchronous
8463 * mailbox command posting.
8464 **/
8465static void
8466lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8467{
8468 struct lpfc_sli *psli = &phba->sli;
8469
8470 spin_lock_irq(&phba->hbalock);
8471 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8472 /* Asynchronous mailbox posting is not blocked, do nothing */
8473 spin_unlock_irq(&phba->hbalock);
8474 return;
8475 }
8476
8477 /* Outstanding synchronous mailbox command is guaranteed to be done,
8478 * successful or timeout, after timing-out the outstanding mailbox
8479 * command shall always be removed, so just unblock posting async
8480 * mailbox command and resume
8481 */
8482 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8483 spin_unlock_irq(&phba->hbalock);
8484
8485 /* wake up worker thread to post asynchronlous mailbox command */
8486 lpfc_worker_wake_up(phba);
8487}
8488
8489/**
James Smart2d843ed2012-09-29 11:29:06 -04008490 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8491 * @phba: Pointer to HBA context object.
8492 * @mboxq: Pointer to mailbox object.
8493 *
8494 * The function waits for the bootstrap mailbox register ready bit from
8495 * port for twice the regular mailbox command timeout value.
8496 *
8497 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8498 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8499 **/
8500static int
8501lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8502{
8503 uint32_t db_ready;
8504 unsigned long timeout;
8505 struct lpfc_register bmbx_reg;
8506
8507 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8508 * 1000) + jiffies;
8509
8510 do {
8511 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8512 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8513 if (!db_ready)
James Smarte2ffe4d2019-03-12 16:30:15 -07008514 mdelay(2);
James Smart2d843ed2012-09-29 11:29:06 -04008515
8516 if (time_after(jiffies, timeout))
8517 return MBXERR_ERROR;
8518 } while (!db_ready);
8519
8520 return 0;
8521}
8522
8523/**
James Smartda0436e2009-05-22 14:51:39 -04008524 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8525 * @phba: Pointer to HBA context object.
8526 * @mboxq: Pointer to mailbox object.
8527 *
8528 * The function posts a mailbox to the port. The mailbox is expected
8529 * to be comletely filled in and ready for the port to operate on it.
8530 * This routine executes a synchronous completion operation on the
8531 * mailbox by polling for its completion.
8532 *
8533 * The caller must not be holding any locks when calling this routine.
8534 *
8535 * Returns:
8536 * MBX_SUCCESS - mailbox posted successfully
8537 * Any of the MBX error values.
8538 **/
8539static int
8540lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8541{
8542 int rc = MBX_SUCCESS;
8543 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04008544 uint32_t mcqe_status;
8545 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04008546 struct lpfc_sli *psli = &phba->sli;
8547 struct lpfc_mqe *mb = &mboxq->u.mqe;
8548 struct lpfc_bmbx_create *mbox_rgn;
8549 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04008550
8551 /*
8552 * Only one mailbox can be active to the bootstrap mailbox region
8553 * at a time and there is no queueing provided.
8554 */
8555 spin_lock_irqsave(&phba->hbalock, iflag);
8556 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8557 spin_unlock_irqrestore(&phba->hbalock, iflag);
8558 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008559 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008560 "cannot issue Data: x%x x%x\n",
8561 mboxq->vport ? mboxq->vport->vpi : 0,
8562 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008563 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8564 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008565 psli->sli_flag, MBX_POLL);
8566 return MBXERR_ERROR;
8567 }
8568 /* The server grabs the token and owns it until release */
8569 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8570 phba->sli.mbox_active = mboxq;
8571 spin_unlock_irqrestore(&phba->hbalock, iflag);
8572
James Smart2d843ed2012-09-29 11:29:06 -04008573 /* wait for bootstrap mbox register for readyness */
8574 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8575 if (rc)
8576 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008577 /*
8578 * Initialize the bootstrap memory region to avoid stale data areas
8579 * in the mailbox post. Then copy the caller's mailbox contents to
8580 * the bmbx mailbox region.
8581 */
8582 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8583 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
James Smart48f8fdb2018-05-04 20:37:51 -07008584 lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8585 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008586
8587 /* Post the high mailbox dma address to the port and wait for ready. */
8588 dma_address = &phba->sli4_hba.bmbx.dma_address;
8589 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8590
James Smart2d843ed2012-09-29 11:29:06 -04008591 /* wait for bootstrap mbox register for hi-address write done */
8592 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8593 if (rc)
8594 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008595
8596 /* Post the low mailbox dma address to the port. */
8597 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008598
James Smart2d843ed2012-09-29 11:29:06 -04008599 /* wait for bootstrap mbox register for low address write done */
8600 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8601 if (rc)
8602 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008603
8604 /*
8605 * Read the CQ to ensure the mailbox has completed.
8606 * If so, update the mailbox status so that the upper layers
8607 * can complete the request normally.
8608 */
James Smart48f8fdb2018-05-04 20:37:51 -07008609 lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8610 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008611 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
James Smart48f8fdb2018-05-04 20:37:51 -07008612 lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8613 sizeof(struct lpfc_mcqe));
James Smartda0436e2009-05-22 14:51:39 -04008614 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008615 /*
8616 * When the CQE status indicates a failure and the mailbox status
8617 * indicates success then copy the CQE status into the mailbox status
8618 * (and prefix it with x4000).
8619 */
James Smartda0436e2009-05-22 14:51:39 -04008620 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008621 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8622 bf_set(lpfc_mqe_status, mb,
8623 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008624 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008625 } else
8626 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008627
8628 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008629 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008630 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8631 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008632 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8633 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8634 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008635 bf_get(lpfc_mqe_status, mb),
8636 mb->un.mb_words[0], mb->un.mb_words[1],
8637 mb->un.mb_words[2], mb->un.mb_words[3],
8638 mb->un.mb_words[4], mb->un.mb_words[5],
8639 mb->un.mb_words[6], mb->un.mb_words[7],
8640 mb->un.mb_words[8], mb->un.mb_words[9],
8641 mb->un.mb_words[10], mb->un.mb_words[11],
8642 mb->un.mb_words[12], mboxq->mcqe.word0,
8643 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8644 mboxq->mcqe.trailer);
8645exit:
8646 /* We are holding the token, no needed for lock when release */
8647 spin_lock_irqsave(&phba->hbalock, iflag);
8648 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8649 phba->sli.mbox_active = NULL;
8650 spin_unlock_irqrestore(&phba->hbalock, iflag);
8651 return rc;
8652}
8653
8654/**
8655 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8656 * @phba: Pointer to HBA context object.
8657 * @pmbox: Pointer to mailbox object.
8658 * @flag: Flag indicating how the mailbox need to be processed.
8659 *
8660 * This function is called by discovery code and HBA management code to submit
8661 * a mailbox command to firmware with SLI-4 interface spec.
8662 *
8663 * Return codes the caller owns the mailbox command after the return of the
8664 * function.
8665 **/
8666static int
8667lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8668 uint32_t flag)
8669{
8670 struct lpfc_sli *psli = &phba->sli;
8671 unsigned long iflags;
8672 int rc;
8673
James Smartb76f2dc2011-07-22 18:37:42 -04008674 /* dump from issue mailbox command if setup */
8675 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8676
James Smart8fa38512009-07-19 10:01:03 -04008677 rc = lpfc_mbox_dev_check(phba);
8678 if (unlikely(rc)) {
8679 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008680 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008681 "cannot issue Data: x%x x%x\n",
8682 mboxq->vport ? mboxq->vport->vpi : 0,
8683 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008684 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8685 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008686 psli->sli_flag, flag);
8687 goto out_not_finished;
8688 }
8689
James Smartda0436e2009-05-22 14:51:39 -04008690 /* Detect polling mode and jump to a handler */
8691 if (!phba->sli4_hba.intr_enable) {
8692 if (flag == MBX_POLL)
8693 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8694 else
8695 rc = -EIO;
8696 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008697 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008698 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008699 "(x%x/x%x) failure: "
8700 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8701 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008702 mboxq->vport ? mboxq->vport->vpi : 0,
8703 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008704 lpfc_sli_config_mbox_subsys_get(phba,
8705 mboxq),
8706 lpfc_sli_config_mbox_opcode_get(phba,
8707 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008708 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8709 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8710 bf_get(lpfc_mcqe_ext_status,
8711 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008712 psli->sli_flag, flag);
8713 return rc;
8714 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008715 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8716 "(%d):2542 Try to issue mailbox command "
James Smart7365f6f2018-02-22 08:18:46 -08008717 "x%x (x%x/x%x) synchronously ahead of async "
James Smartf1126682009-06-10 17:22:44 -04008718 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008719 mboxq->vport ? mboxq->vport->vpi : 0,
8720 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008721 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8722 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008723 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008724 /* Try to block the asynchronous mailbox posting */
8725 rc = lpfc_sli4_async_mbox_block(phba);
8726 if (!rc) {
8727 /* Successfully blocked, now issue sync mbox cmd */
8728 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8729 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008730 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008731 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008732 "(%d):2597 Sync Mailbox command "
8733 "x%x (x%x/x%x) failure: "
8734 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8735 "Data: x%x x%x\n,",
8736 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008737 mboxq->u.mb.mbxCommand,
8738 lpfc_sli_config_mbox_subsys_get(phba,
8739 mboxq),
8740 lpfc_sli_config_mbox_opcode_get(phba,
8741 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008742 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8743 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8744 bf_get(lpfc_mcqe_ext_status,
8745 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008746 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008747 /* Unblock the async mailbox posting afterward */
8748 lpfc_sli4_async_mbox_unblock(phba);
8749 }
8750 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008751 }
8752
8753 /* Now, interrupt mode asynchrous mailbox command */
8754 rc = lpfc_mbox_cmd_check(phba, mboxq);
8755 if (rc) {
8756 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008757 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008758 "cannot issue Data: x%x x%x\n",
8759 mboxq->vport ? mboxq->vport->vpi : 0,
8760 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008761 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8762 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008763 psli->sli_flag, flag);
8764 goto out_not_finished;
8765 }
James Smartda0436e2009-05-22 14:51:39 -04008766
8767 /* Put the mailbox command to the driver internal FIFO */
8768 psli->slistat.mbox_busy++;
8769 spin_lock_irqsave(&phba->hbalock, iflags);
8770 lpfc_mbox_put(phba, mboxq);
8771 spin_unlock_irqrestore(&phba->hbalock, iflags);
8772 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8773 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008774 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008775 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8776 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008777 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8778 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008779 phba->pport->port_state,
8780 psli->sli_flag, MBX_NOWAIT);
8781 /* Wake up worker thread to transport mailbox command from head */
8782 lpfc_worker_wake_up(phba);
8783
8784 return MBX_BUSY;
8785
8786out_not_finished:
8787 return MBX_NOT_FINISHED;
8788}
8789
8790/**
8791 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8792 * @phba: Pointer to HBA context object.
8793 *
8794 * This function is called by worker thread to send a mailbox command to
8795 * SLI4 HBA firmware.
8796 *
8797 **/
8798int
8799lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8800{
8801 struct lpfc_sli *psli = &phba->sli;
8802 LPFC_MBOXQ_t *mboxq;
8803 int rc = MBX_SUCCESS;
8804 unsigned long iflags;
8805 struct lpfc_mqe *mqe;
8806 uint32_t mbx_cmnd;
8807
8808 /* Check interrupt mode before post async mailbox command */
8809 if (unlikely(!phba->sli4_hba.intr_enable))
8810 return MBX_NOT_FINISHED;
8811
8812 /* Check for mailbox command service token */
8813 spin_lock_irqsave(&phba->hbalock, iflags);
8814 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8815 spin_unlock_irqrestore(&phba->hbalock, iflags);
8816 return MBX_NOT_FINISHED;
8817 }
8818 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8819 spin_unlock_irqrestore(&phba->hbalock, iflags);
8820 return MBX_NOT_FINISHED;
8821 }
8822 if (unlikely(phba->sli.mbox_active)) {
8823 spin_unlock_irqrestore(&phba->hbalock, iflags);
8824 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8825 "0384 There is pending active mailbox cmd\n");
8826 return MBX_NOT_FINISHED;
8827 }
8828 /* Take the mailbox command service token */
8829 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8830
8831 /* Get the next mailbox command from head of queue */
8832 mboxq = lpfc_mbox_get(phba);
8833
8834 /* If no more mailbox command waiting for post, we're done */
8835 if (!mboxq) {
8836 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8837 spin_unlock_irqrestore(&phba->hbalock, iflags);
8838 return MBX_SUCCESS;
8839 }
8840 phba->sli.mbox_active = mboxq;
8841 spin_unlock_irqrestore(&phba->hbalock, iflags);
8842
8843 /* Check device readiness for posting mailbox command */
8844 rc = lpfc_mbox_dev_check(phba);
8845 if (unlikely(rc))
8846 /* Driver clean routine will clean up pending mailbox */
8847 goto out_not_finished;
8848
8849 /* Prepare the mbox command to be posted */
8850 mqe = &mboxq->u.mqe;
8851 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8852
8853 /* Start timer for the mbox_tmo and log some mailbox post messages */
8854 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008855 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008856
8857 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008858 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008859 "x%x x%x\n",
8860 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008861 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8862 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008863 phba->pport->port_state, psli->sli_flag);
8864
8865 if (mbx_cmnd != MBX_HEARTBEAT) {
8866 if (mboxq->vport) {
8867 lpfc_debugfs_disc_trc(mboxq->vport,
8868 LPFC_DISC_TRC_MBOX_VPORT,
8869 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8870 mbx_cmnd, mqe->un.mb_words[0],
8871 mqe->un.mb_words[1]);
8872 } else {
8873 lpfc_debugfs_disc_trc(phba->pport,
8874 LPFC_DISC_TRC_MBOX,
8875 "MBOX Send: cmd:x%x mb:x%x x%x",
8876 mbx_cmnd, mqe->un.mb_words[0],
8877 mqe->un.mb_words[1]);
8878 }
8879 }
8880 psli->slistat.mbox_cmd++;
8881
8882 /* Post the mailbox command to the port */
8883 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8884 if (rc != MBX_SUCCESS) {
8885 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008886 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008887 "cannot issue Data: x%x x%x\n",
8888 mboxq->vport ? mboxq->vport->vpi : 0,
8889 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008890 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8891 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008892 psli->sli_flag, MBX_NOWAIT);
8893 goto out_not_finished;
8894 }
8895
8896 return rc;
8897
8898out_not_finished:
8899 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008900 if (phba->sli.mbox_active) {
8901 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8902 __lpfc_mbox_cmpl_put(phba, mboxq);
8903 /* Release the token */
8904 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8905 phba->sli.mbox_active = NULL;
8906 }
James Smartda0436e2009-05-22 14:51:39 -04008907 spin_unlock_irqrestore(&phba->hbalock, iflags);
8908
8909 return MBX_NOT_FINISHED;
8910}
8911
8912/**
8913 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8914 * @phba: Pointer to HBA context object.
8915 * @pmbox: Pointer to mailbox object.
8916 * @flag: Flag indicating how the mailbox need to be processed.
8917 *
8918 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8919 * the API jump table function pointer from the lpfc_hba struct.
8920 *
8921 * Return codes the caller owns the mailbox command after the return of the
8922 * function.
8923 **/
8924int
8925lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8926{
8927 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8928}
8929
8930/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008931 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008932 * @phba: The hba struct for which this call is being executed.
8933 * @dev_grp: The HBA PCI-Device group number.
8934 *
8935 * This routine sets up the mbox interface API function jump table in @phba
8936 * struct.
8937 * Returns: 0 - success, -ENODEV - failure.
8938 **/
8939int
8940lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8941{
8942
8943 switch (dev_grp) {
8944 case LPFC_PCI_DEV_LP:
8945 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8946 phba->lpfc_sli_handle_slow_ring_event =
8947 lpfc_sli_handle_slow_ring_event_s3;
8948 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8949 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8950 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8951 break;
8952 case LPFC_PCI_DEV_OC:
8953 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8954 phba->lpfc_sli_handle_slow_ring_event =
8955 lpfc_sli_handle_slow_ring_event_s4;
8956 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8957 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8958 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8959 break;
8960 default:
8961 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8962 "1420 Invalid HBA PCI-device group: 0x%x\n",
8963 dev_grp);
8964 return -ENODEV;
8965 break;
8966 }
8967 return 0;
8968}
8969
8970/**
James Smart3621a712009-04-06 18:47:14 -04008971 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008972 * @phba: Pointer to HBA context object.
8973 * @pring: Pointer to driver SLI ring object.
8974 * @piocb: Pointer to address of newly added command iocb.
8975 *
8976 * This function is called with hbalock held to add a command
8977 * iocb to the txq when SLI layer cannot submit the command iocb
8978 * to the ring.
8979 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008980void
James Smart92d7f7b2007-06-17 19:56:38 -05008981__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008982 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008983{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008984 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008985 /* Insert the caller's iocb in the txq tail for later processing. */
8986 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008987}
8988
James Smarte59058c2008-08-24 21:49:00 -04008989/**
James Smart3621a712009-04-06 18:47:14 -04008990 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008991 * @phba: Pointer to HBA context object.
8992 * @pring: Pointer to driver SLI ring object.
8993 * @piocb: Pointer to address of newly added command iocb.
8994 *
8995 * This function is called with hbalock held before a new
8996 * iocb is submitted to the firmware. This function checks
8997 * txq to flush the iocbs in txq to Firmware before
8998 * submitting new iocbs to the Firmware.
8999 * If there are iocbs in the txq which need to be submitted
9000 * to firmware, lpfc_sli_next_iocb returns the first element
9001 * of the txq after dequeuing it from txq.
9002 * If there is no iocb in the txq then the function will return
9003 * *piocb and *piocb is set to NULL. Caller needs to check
9004 * *piocb to find if there are more commands in the txq.
9005 **/
dea31012005-04-17 16:05:31 -05009006static struct lpfc_iocbq *
9007lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009008 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05009009{
9010 struct lpfc_iocbq * nextiocb;
9011
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009012 lockdep_assert_held(&phba->hbalock);
9013
dea31012005-04-17 16:05:31 -05009014 nextiocb = lpfc_sli_ringtx_get(phba, pring);
9015 if (!nextiocb) {
9016 nextiocb = *piocb;
9017 *piocb = NULL;
9018 }
9019
9020 return nextiocb;
9021}
9022
James Smarte59058c2008-08-24 21:49:00 -04009023/**
James Smart3772a992009-05-22 14:50:54 -04009024 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009025 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04009026 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04009027 * @piocb: Pointer to command iocb.
9028 * @flag: Flag indicating if this command can be put into txq.
9029 *
James Smart3772a992009-05-22 14:50:54 -04009030 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
9031 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
9032 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
9033 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
9034 * this function allows only iocbs for posting buffers. This function finds
9035 * next available slot in the command ring and posts the command to the
9036 * available slot and writes the port attention register to request HBA start
9037 * processing new iocb. If there is no slot available in the ring and
9038 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
9039 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04009040 *
James Smart3772a992009-05-22 14:50:54 -04009041 * This function is called with hbalock held. The function will return success
9042 * after it successfully submit the iocb to firmware or after adding to the
9043 * txq.
James Smarte59058c2008-08-24 21:49:00 -04009044 **/
James Smart98c9ea52007-10-27 13:37:33 -04009045static int
James Smart3772a992009-05-22 14:50:54 -04009046__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05009047 struct lpfc_iocbq *piocb, uint32_t flag)
9048{
9049 struct lpfc_iocbq *nextiocb;
9050 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08009051 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05009052
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009053 lockdep_assert_held(&phba->hbalock);
9054
James Smart92d7f7b2007-06-17 19:56:38 -05009055 if (piocb->iocb_cmpl && (!piocb->vport) &&
9056 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
9057 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
9058 lpfc_printf_log(phba, KERN_ERR,
9059 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04009060 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05009061 piocb->iocb.ulpCommand);
9062 dump_stack();
9063 return IOCB_ERROR;
9064 }
9065
9066
Linas Vepstas8d63f372007-02-14 14:28:36 -06009067 /* If the PCI channel is in offline state, do not post iocbs. */
9068 if (unlikely(pci_channel_offline(phba->pcidev)))
9069 return IOCB_ERROR;
9070
James Smarta257bf92009-04-06 18:48:10 -04009071 /* If HBA has a deferred error attention, fail the iocb. */
9072 if (unlikely(phba->hba_flag & DEFER_ERATT))
9073 return IOCB_ERROR;
9074
dea31012005-04-17 16:05:31 -05009075 /*
9076 * We should never get an IOCB if we are in a < LINK_DOWN state
9077 */
James Smart2e0fef82007-06-17 19:56:36 -05009078 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05009079 return IOCB_ERROR;
9080
9081 /*
9082 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04009083 * outstanding event.
dea31012005-04-17 16:05:31 -05009084 */
James Smart0b727fe2007-10-27 13:37:25 -04009085 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05009086 goto iocb_busy;
9087
James Smart2e0fef82007-06-17 19:56:36 -05009088 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05009089 /*
James Smart2680eea2007-04-25 09:52:55 -04009090 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05009091 * can be issued if the link is not up.
9092 */
9093 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04009094 case CMD_GEN_REQUEST64_CR:
9095 case CMD_GEN_REQUEST64_CX:
9096 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
9097 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04009098 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04009099 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
9100 MENLO_TRANSPORT_TYPE))
9101
9102 goto iocb_busy;
9103 break;
dea31012005-04-17 16:05:31 -05009104 case CMD_QUE_RING_BUF_CN:
9105 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05009106 /*
9107 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
9108 * completion, iocb_cmpl MUST be 0.
9109 */
9110 if (piocb->iocb_cmpl)
9111 piocb->iocb_cmpl = NULL;
9112 /*FALLTHROUGH*/
9113 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04009114 case CMD_CLOSE_XRI_CN:
9115 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05009116 break;
9117 default:
9118 goto iocb_busy;
9119 }
9120
9121 /*
9122 * For FCP commands, we must be in a state where we can process link
9123 * attention events.
9124 */
James Smart895427b2017-02-12 13:52:30 -08009125 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05009126 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05009127 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05009128 }
dea31012005-04-17 16:05:31 -05009129
dea31012005-04-17 16:05:31 -05009130 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
9131 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
9132 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
9133
9134 if (iocb)
9135 lpfc_sli_update_ring(phba, pring);
9136 else
9137 lpfc_sli_update_full_ring(phba, pring);
9138
9139 if (!piocb)
9140 return IOCB_SUCCESS;
9141
9142 goto out_busy;
9143
9144 iocb_busy:
9145 pring->stats.iocb_cmd_delay++;
9146
9147 out_busy:
9148
9149 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05009150 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05009151 return IOCB_SUCCESS;
9152 }
9153
9154 return IOCB_BUSY;
9155}
9156
James Smart3772a992009-05-22 14:50:54 -04009157/**
James Smart4f774512009-05-22 14:52:35 -04009158 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
9159 * @phba: Pointer to HBA context object.
9160 * @piocb: Pointer to command iocb.
9161 * @sglq: Pointer to the scatter gather queue object.
9162 *
9163 * This routine converts the bpl or bde that is in the IOCB
9164 * to a sgl list for the sli4 hardware. The physical address
9165 * of the bpl/bde is converted back to a virtual address.
9166 * If the IOCB contains a BPL then the list of BDE's is
9167 * converted to sli4_sge's. If the IOCB contains a single
9168 * BDE then it is converted to a single sli_sge.
9169 * The IOCB is still in cpu endianess so the contents of
9170 * the bpl can be used without byte swapping.
9171 *
9172 * Returns valid XRI = Success, NO_XRI = Failure.
9173**/
9174static uint16_t
9175lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
9176 struct lpfc_sglq *sglq)
9177{
9178 uint16_t xritag = NO_XRI;
9179 struct ulp_bde64 *bpl = NULL;
9180 struct ulp_bde64 bde;
9181 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05009182 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04009183 IOCB_t *icmd;
9184 int numBdes = 0;
9185 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05009186 uint32_t offset = 0; /* accumulated offset in the sg request list */
9187 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04009188
9189 if (!piocbq || !sglq)
9190 return xritag;
9191
9192 sgl = (struct sli4_sge *)sglq->sgl;
9193 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05009194 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
9195 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009196 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9197 numBdes = icmd->un.genreq64.bdl.bdeSize /
9198 sizeof(struct ulp_bde64);
9199 /* The addrHigh and addrLow fields within the IOCB
9200 * have not been byteswapped yet so there is no
9201 * need to swap them back.
9202 */
James Smart1b511972011-12-13 13:23:09 -05009203 if (piocbq->context3)
9204 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
9205 else
9206 return xritag;
James Smart4f774512009-05-22 14:52:35 -04009207
James Smart1b511972011-12-13 13:23:09 -05009208 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04009209 if (!bpl)
9210 return xritag;
9211
9212 for (i = 0; i < numBdes; i++) {
9213 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05009214 sgl->addr_hi = bpl->addrHigh;
9215 sgl->addr_lo = bpl->addrLow;
9216
James Smart05580562011-05-24 11:40:48 -04009217 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009218 if ((i+1) == numBdes)
9219 bf_set(lpfc_sli4_sge_last, sgl, 1);
9220 else
9221 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05009222 /* swap the size field back to the cpu so we
9223 * can assign it to the sgl.
9224 */
9225 bde.tus.w = le32_to_cpu(bpl->tus.w);
9226 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05009227 /* The offsets in the sgl need to be accumulated
9228 * separately for the request and reply lists.
9229 * The request is always first, the reply follows.
9230 */
9231 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
9232 /* add up the reply sg entries */
9233 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
9234 inbound++;
9235 /* first inbound? reset the offset */
9236 if (inbound == 1)
9237 offset = 0;
9238 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04009239 bf_set(lpfc_sli4_sge_type, sgl,
9240 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05009241 offset += bde.tus.f.bdeSize;
9242 }
James Smart546fc852011-03-11 16:06:29 -05009243 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009244 bpl++;
9245 sgl++;
9246 }
9247 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
9248 /* The addrHigh and addrLow fields of the BDE have not
9249 * been byteswapped yet so they need to be swapped
9250 * before putting them in the sgl.
9251 */
9252 sgl->addr_hi =
9253 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
9254 sgl->addr_lo =
9255 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04009256 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009257 bf_set(lpfc_sli4_sge_last, sgl, 1);
9258 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05009259 sgl->sge_len =
9260 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04009261 }
9262 return sglq->sli4_xritag;
9263}
9264
9265/**
James Smart4f774512009-05-22 14:52:35 -04009266 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
9267 * @phba: Pointer to HBA context object.
9268 * @piocb: Pointer to command iocb.
9269 * @wqe: Pointer to the work queue entry.
9270 *
9271 * This routine converts the iocb command to its Work Queue Entry
9272 * equivalent. The wqe pointer should not have any fields set when
9273 * this routine is called because it will memcpy over them.
9274 * This routine does not set the CQ_ID or the WQEC bits in the
9275 * wqe.
9276 *
9277 * Returns: 0 = Success, IOCB_ERROR = Failure.
9278 **/
9279static int
9280lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
James Smart205e8242018-03-05 12:04:03 -08009281 union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -04009282{
James Smart5ffc2662009-11-18 15:39:44 -05009283 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04009284 uint8_t ct = 0;
9285 uint32_t fip;
9286 uint32_t abort_tag;
9287 uint8_t command_type = ELS_COMMAND_NON_FIP;
9288 uint8_t cmnd;
9289 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04009290 uint16_t abrt_iotag;
9291 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04009292 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04009293 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05009294 int numBdes, i;
9295 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04009296 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05009297 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05009298 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04009299
James Smart45ed1192009-10-02 15:17:02 -04009300 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04009301 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04009302 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04009303 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05009304 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04009305 command_type = ELS_COMMAND_FIP;
9306 else
9307 command_type = ELS_COMMAND_NON_FIP;
9308
James Smartb5c53952016-03-31 14:12:30 -07009309 if (phba->fcp_embed_io)
9310 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04009311 /* Some of the fields are in the right position already */
9312 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartae9e28f2017-05-15 15:20:51 -07009313 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
9314 /* The ct field has moved so reset */
9315 wqe->generic.wqe_com.word7 = 0;
9316 wqe->generic.wqe_com.word10 = 0;
9317 }
James Smartb5c53952016-03-31 14:12:30 -07009318
9319 abort_tag = (uint32_t) iocbq->iotag;
9320 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009321 /* words0-2 bpl convert bde */
9322 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05009323 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9324 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04009325 bpl = (struct ulp_bde64 *)
9326 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
9327 if (!bpl)
9328 return IOCB_ERROR;
9329
9330 /* Should already be byte swapped. */
9331 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
9332 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
9333 /* swap the size field back to the cpu so we
9334 * can assign it to the sgl.
9335 */
9336 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05009337 xmit_len = wqe->generic.bde.tus.f.bdeSize;
9338 total_len = 0;
9339 for (i = 0; i < numBdes; i++) {
9340 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
9341 total_len += bde.tus.f.bdeSize;
9342 }
James Smart4f774512009-05-22 14:52:35 -04009343 } else
James Smart5ffc2662009-11-18 15:39:44 -05009344 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04009345
9346 iocbq->iocb.ulpIoTag = iocbq->iotag;
9347 cmnd = iocbq->iocb.ulpCommand;
9348
9349 switch (iocbq->iocb.ulpCommand) {
9350 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04009351 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
9352 ndlp = iocbq->context_un.ndlp;
9353 else
9354 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009355 if (!iocbq->iocb.ulpLe) {
9356 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9357 "2007 Only Limited Edition cmd Format"
9358 " supported 0x%x\n",
9359 iocbq->iocb.ulpCommand);
9360 return IOCB_ERROR;
9361 }
James Smartff78d8f2011-12-13 13:21:35 -05009362
James Smart5ffc2662009-11-18 15:39:44 -05009363 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009364 /* Els_reguest64 has a TMO */
9365 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
9366 iocbq->iocb.ulpTimeout);
9367 /* Need a VF for word 4 set the vf bit*/
9368 bf_set(els_req64_vf, &wqe->els_req, 0);
9369 /* And a VFID for word 12 */
9370 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04009371 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04009372 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9373 iocbq->iocb.ulpContext);
9374 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9375 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04009376 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05009377 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05009378 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9379 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05009380 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9381 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05009382 if_type = bf_get(lpfc_sli_intf_if_type,
9383 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009384 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05009385 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05009386 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05009387 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05009388 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05009389 *pcmd == ELS_CMD_PLOGI)) {
9390 bf_set(els_req64_sp, &wqe->els_req, 1);
9391 bf_set(els_req64_sid, &wqe->els_req,
9392 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009393 if ((*pcmd == ELS_CMD_FLOGI) &&
9394 !(phba->fc_topology ==
9395 LPFC_TOPOLOGY_LOOP))
9396 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05009397 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9398 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04009399 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05009400 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05009401 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9402 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9403 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9404 }
James Smartc8685952009-11-18 15:39:16 -05009405 }
James Smart6d368e52011-05-24 11:44:12 -04009406 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9407 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009408 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9409 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9410 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9411 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9412 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9413 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009414 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04009415 break;
James Smart5ffc2662009-11-18 15:39:44 -05009416 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04009417 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9418 iocbq->iocb.un.ulpWord[3]);
9419 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009420 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05009421 /* The entire sequence is transmitted for this IOCB */
9422 xmit_len = total_len;
9423 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05009424 if (phba->link_flag & LS_LOOPBACK_MODE)
9425 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -06009426 /* fall through */
James Smart4f774512009-05-22 14:52:35 -04009427 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04009428 /* word3 iocb=io_tag32 wqe=reserved */
9429 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04009430 /* word4 relative_offset memcpy */
9431 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009432 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9433 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9434 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9435 LPFC_WQE_IOD_WRITE);
9436 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9437 LPFC_WQE_LENLOC_WORD12);
9438 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05009439 wqe->xmit_sequence.xmit_len = xmit_len;
9440 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009441 break;
James Smart4f774512009-05-22 14:52:35 -04009442 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04009443 /* word3 iocb=iotag32 wqe=seq_payload_len */
9444 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009445 /* word4 iocb=rsvd wqe=rsvd */
9446 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9447 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04009448 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009449 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04009450 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9451 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9452 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9453 LPFC_WQE_LENLOC_WORD3);
9454 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04009455 break;
James Smart4f774512009-05-22 14:52:35 -04009456 case CMD_FCP_IWRITE64_CR:
9457 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04009458 /* word3 iocb=iotag wqe=payload_offset_len */
9459 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009460 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9461 xmit_len + sizeof(struct fcp_rsp));
9462 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9463 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009464 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9465 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9466 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9467 iocbq->iocb.ulpFCP2Rcvy);
9468 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9469 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009470 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9471 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9472 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009473 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009474 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009475 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9476 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009477 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9478 if (iocbq->priority) {
9479 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9480 (iocbq->priority << 1));
9481 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009482 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9483 (phba->cfg_XLanePriority << 1));
9484 }
9485 }
James Smartb5c53952016-03-31 14:12:30 -07009486 /* Note, word 10 is already initialized to 0 */
9487
James Smart414abe02018-06-26 08:24:26 -07009488 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9489 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009490 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9491 else
9492 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9493
James Smartb5c53952016-03-31 14:12:30 -07009494 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009495 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009496 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009497 struct fcp_cmnd *fcp_cmnd;
9498 uint32_t *ptr;
9499
9500 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009501
9502 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009503 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009504 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9505
9506 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009507 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009508 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009509 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9510 wqe->generic.bde.addrHigh = 0;
9511 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009512
James Smart205e8242018-03-05 12:04:03 -08009513 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9514 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009515
9516 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009517 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009518 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9519 }
James Smart7851fe22011-07-22 18:36:52 -04009520 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04009521 case CMD_FCP_IREAD64_CR:
9522 /* word3 iocb=iotag wqe=payload_offset_len */
9523 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009524 bf_set(payload_offset_len, &wqe->fcp_iread,
9525 xmit_len + sizeof(struct fcp_rsp));
9526 bf_set(cmd_buff_len, &wqe->fcp_iread,
9527 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009528 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9529 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9530 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9531 iocbq->iocb.ulpFCP2Rcvy);
9532 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04009533 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009534 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9535 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9536 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009537 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009538 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009539 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9540 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009541 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9542 if (iocbq->priority) {
9543 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9544 (iocbq->priority << 1));
9545 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009546 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9547 (phba->cfg_XLanePriority << 1));
9548 }
9549 }
James Smartb5c53952016-03-31 14:12:30 -07009550 /* Note, word 10 is already initialized to 0 */
9551
James Smart414abe02018-06-26 08:24:26 -07009552 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9553 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009554 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9555 else
9556 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9557
James Smartb5c53952016-03-31 14:12:30 -07009558 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009559 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009560 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009561 struct fcp_cmnd *fcp_cmnd;
9562 uint32_t *ptr;
9563
9564 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009565
9566 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009567 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009568 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9569
9570 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009571 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009572 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009573 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9574 wqe->generic.bde.addrHigh = 0;
9575 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009576
James Smart205e8242018-03-05 12:04:03 -08009577 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9578 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009579
9580 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009581 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009582 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9583 }
James Smart7851fe22011-07-22 18:36:52 -04009584 break;
James Smartf1126682009-06-10 17:22:44 -04009585 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04009586 /* word3 iocb=iotag wqe=payload_offset_len */
9587 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9588 bf_set(payload_offset_len, &wqe->fcp_icmd,
9589 xmit_len + sizeof(struct fcp_rsp));
9590 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9591 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009592 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04009593 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04009594 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009595 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9596 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9597 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9598 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9599 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04009600 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9601 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009602 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9603 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009604 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9605 if (iocbq->priority) {
9606 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9607 (iocbq->priority << 1));
9608 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009609 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9610 (phba->cfg_XLanePriority << 1));
9611 }
9612 }
James Smartb5c53952016-03-31 14:12:30 -07009613 /* Note, word 10 is already initialized to 0 */
9614
9615 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009616 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009617 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009618 struct fcp_cmnd *fcp_cmnd;
9619 uint32_t *ptr;
9620
9621 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009622
9623 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009624 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009625 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9626
9627 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009628 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009629 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009630 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9631 wqe->generic.bde.addrHigh = 0;
9632 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009633
James Smart205e8242018-03-05 12:04:03 -08009634 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9635 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009636
9637 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009638 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009639 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9640 }
James Smart7851fe22011-07-22 18:36:52 -04009641 break;
James Smart4f774512009-05-22 14:52:35 -04009642 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009643 /* For this command calculate the xmit length of the
9644 * request bde.
9645 */
9646 xmit_len = 0;
9647 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9648 sizeof(struct ulp_bde64);
9649 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009650 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009651 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9652 break;
James Smart63e801c2010-11-20 23:14:19 -05009653 xmit_len += bde.tus.f.bdeSize;
9654 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009655 /* word3 iocb=IO_TAG wqe=request_payload_len */
9656 wqe->gen_req.request_payload_len = xmit_len;
9657 /* word4 iocb=parameter wqe=relative_offset memcpy */
9658 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009659 /* word6 context tag copied in memcpy */
9660 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9661 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9662 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9663 "2015 Invalid CT %x command 0x%x\n",
9664 ct, iocbq->iocb.ulpCommand);
9665 return IOCB_ERROR;
9666 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009667 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9668 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9669 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9670 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9671 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9672 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9673 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9674 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009675 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009676 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009677 break;
James Smart4f774512009-05-22 14:52:35 -04009678 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009679 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009680 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009681 /* word3 iocb=iotag32 wqe=response_payload_len */
9682 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009683 /* word4 */
9684 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009685 /* word5 iocb=rsvd wge=did */
9686 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009687 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9688
9689 if_type = bf_get(lpfc_sli_intf_if_type,
9690 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009691 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -04009692 if (iocbq->vport->fc_flag & FC_PT2PT) {
9693 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9694 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9695 iocbq->vport->fc_myDID);
9696 if (iocbq->vport->fc_myDID == Fabric_DID) {
9697 bf_set(wqe_els_did,
9698 &wqe->xmit_els_rsp.wqe_dest, 0);
9699 }
9700 }
9701 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009702 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9703 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9704 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9705 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009706 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009707 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009708 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009709 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009710 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9711 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9712 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9713 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9714 LPFC_WQE_LENLOC_WORD3);
9715 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009716 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9717 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009718 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9719 iocbq->context2)->virt);
9720 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009721 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9722 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009723 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009724 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9725 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009726 phba->vpi_ids[phba->pport->vpi]);
9727 }
James Smart4f774512009-05-22 14:52:35 -04009728 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009729 break;
James Smart4f774512009-05-22 14:52:35 -04009730 case CMD_CLOSE_XRI_CN:
9731 case CMD_ABORT_XRI_CN:
9732 case CMD_ABORT_XRI_CX:
9733 /* words 0-2 memcpy should be 0 rserved */
9734 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009735 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9736 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9737 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9738 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9739 } else
9740 fip = 0;
9741
9742 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009743 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009744 * The link is down, or the command was ELS_FIP
9745 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009746 * on the wire.
9747 */
9748 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9749 else
9750 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9751 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009752 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9753 wqe->abort_cmd.rsrvd5 = 0;
9754 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009755 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9756 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009757 /*
9758 * The abort handler will send us CMD_ABORT_XRI_CN or
9759 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9760 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009761 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9762 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9763 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9764 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009765 cmnd = CMD_ABORT_XRI_CX;
9766 command_type = OTHER_COMMAND;
9767 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009768 break;
James Smart6669f9b2009-10-02 15:16:45 -04009769 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009770 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009771 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009772 * we re-construct this WQE here based on information in
9773 * iocbq from scratch.
9774 */
9775 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009776 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009777 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009778 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9779 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009780 LPFC_ABTS_UNSOL_INT) {
9781 /* ABTS sent by initiator to CT exchange, the
9782 * RX_ID field will be filled with the newly
9783 * allocated responder XRI.
9784 */
9785 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9786 iocbq->sli4_xritag);
9787 } else {
9788 /* ABTS sent by responder to CT exchange, the
9789 * RX_ID field will be filled with the responder
9790 * RX_ID from ABTS.
9791 */
9792 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009793 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009794 }
James Smart6669f9b2009-10-02 15:16:45 -04009795 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9796 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009797
9798 /* Use CT=VPI */
9799 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9800 ndlp->nlp_DID);
9801 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9802 iocbq->iocb.ulpContext);
9803 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009804 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009805 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009806 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9807 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9808 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009809 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9810 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009811 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9812 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9813 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9814 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9815 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9816 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9817 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9818 }
9819
James Smart7851fe22011-07-22 18:36:52 -04009820 break;
James Smartae9e28f2017-05-15 15:20:51 -07009821 case CMD_SEND_FRAME:
9822 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9823 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9824 return 0;
James Smart4f774512009-05-22 14:52:35 -04009825 case CMD_XRI_ABORTED_CX:
9826 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009827 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9828 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9829 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9830 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9831 default:
9832 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9833 "2014 Invalid command 0x%x\n",
9834 iocbq->iocb.ulpCommand);
9835 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009836 break;
James Smart4f774512009-05-22 14:52:35 -04009837 }
James Smart6d368e52011-05-24 11:44:12 -04009838
James Smart8012cc32012-10-31 14:44:49 -04009839 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9840 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9841 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9842 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9843 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9844 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9845 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9846 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009847 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9848 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9849 wqe->generic.wqe_com.abort_tag = abort_tag;
9850 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9851 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9852 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9853 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009854 return 0;
9855}
9856
9857/**
9858 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9859 * @phba: Pointer to HBA context object.
9860 * @ring_number: SLI ring number to issue iocb on.
9861 * @piocb: Pointer to command iocb.
9862 * @flag: Flag indicating if this command can be put into txq.
9863 *
9864 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9865 * an iocb command to an HBA with SLI-4 interface spec.
9866 *
9867 * This function is called with hbalock held. The function will return success
9868 * after it successfully submit the iocb to firmware or after adding to the
9869 * txq.
9870 **/
9871static int
9872__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9873 struct lpfc_iocbq *piocb, uint32_t flag)
9874{
9875 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -08009876 union lpfc_wqe128 wqe;
James Smart1ba981f2014-02-20 09:56:45 -05009877 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009878 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009879
James Smart895427b2017-02-12 13:52:30 -08009880 /* Get the WQ */
9881 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9882 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartcdb42be2019-01-28 11:14:21 -08009883 wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].fcp_wq;
James Smart895427b2017-02-12 13:52:30 -08009884 } else {
9885 wq = phba->sli4_hba.els_wq;
9886 }
9887
9888 /* Get corresponding ring */
9889 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009890
James Smartb5c53952016-03-31 14:12:30 -07009891 /*
9892 * The WQE can be either 64 or 128 bytes,
James Smartb5c53952016-03-31 14:12:30 -07009893 */
James Smartb5c53952016-03-31 14:12:30 -07009894
James Smartcda7fa182019-03-04 15:15:43 -08009895 lockdep_assert_held(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -08009896
James Smart4f774512009-05-22 14:52:35 -04009897 if (piocb->sli4_xritag == NO_XRI) {
9898 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009899 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009900 sglq = NULL;
9901 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009902 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009903 if (!(flag & SLI_IOCB_RET_IOCB)) {
9904 __lpfc_sli_ringtx_put(phba,
9905 pring, piocb);
9906 return IOCB_SUCCESS;
9907 } else {
9908 return IOCB_BUSY;
9909 }
9910 } else {
James Smart895427b2017-02-12 13:52:30 -08009911 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009912 if (!sglq) {
9913 if (!(flag & SLI_IOCB_RET_IOCB)) {
9914 __lpfc_sli_ringtx_put(phba,
9915 pring,
9916 piocb);
9917 return IOCB_SUCCESS;
9918 } else
9919 return IOCB_BUSY;
9920 }
9921 }
James Smart4f774512009-05-22 14:52:35 -04009922 }
James Smart2ea259e2017-02-12 13:52:27 -08009923 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009924 /* These IO's already have an XRI and a mapped sgl. */
9925 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009926 else {
James Smart6d368e52011-05-24 11:44:12 -04009927 /*
9928 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009929 * sglq is on the active list
9930 */
James Smartedccdc12013-01-03 15:43:45 -05009931 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009932 if (!sglq)
9933 return IOCB_ERROR;
9934 }
9935
9936 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009937 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009938 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009939 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009940 return IOCB_ERROR;
9941 }
9942
James Smart205e8242018-03-05 12:04:03 -08009943 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
James Smart4f774512009-05-22 14:52:35 -04009944 return IOCB_ERROR;
9945
James Smart205e8242018-03-05 12:04:03 -08009946 if (lpfc_sli4_wq_put(wq, &wqe))
James Smart895427b2017-02-12 13:52:30 -08009947 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009948 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9949
9950 return 0;
9951}
9952
9953/**
James Smart3772a992009-05-22 14:50:54 -04009954 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9955 *
9956 * This routine wraps the actual lockless version for issusing IOCB function
9957 * pointer from the lpfc_hba struct.
9958 *
9959 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009960 * IOCB_ERROR - Error
9961 * IOCB_SUCCESS - Success
9962 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009963 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009964int
James Smart3772a992009-05-22 14:50:54 -04009965__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9966 struct lpfc_iocbq *piocb, uint32_t flag)
9967{
9968 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9969}
9970
9971/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009972 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009973 * @phba: The hba struct for which this call is being executed.
9974 * @dev_grp: The HBA PCI-Device group number.
9975 *
9976 * This routine sets up the SLI interface API function jump table in @phba
9977 * struct.
9978 * Returns: 0 - success, -ENODEV - failure.
9979 **/
9980int
9981lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9982{
9983
9984 switch (dev_grp) {
9985 case LPFC_PCI_DEV_LP:
9986 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9987 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9988 break;
James Smart4f774512009-05-22 14:52:35 -04009989 case LPFC_PCI_DEV_OC:
9990 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9991 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9992 break;
James Smart3772a992009-05-22 14:50:54 -04009993 default:
9994 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9995 "1419 Invalid HBA PCI-device group: 0x%x\n",
9996 dev_grp);
9997 return -ENODEV;
9998 break;
9999 }
10000 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
10001 return 0;
10002}
James Smart92d7f7b2007-06-17 19:56:38 -050010003
James Smarta1efe162015-05-21 13:55:20 -040010004/**
James Smart895427b2017-02-12 13:52:30 -080010005 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -040010006 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -040010007 * @piocb: Pointer to command iocb.
10008 *
James Smart895427b2017-02-12 13:52:30 -080010009 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
10010 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -040010011 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -080010012 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -040010013 */
James Smart895427b2017-02-12 13:52:30 -080010014struct lpfc_sli_ring *
10015lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -040010016{
James Smartc4908502019-01-28 11:14:28 -080010017 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -080010018
James Smart895427b2017-02-12 13:52:30 -080010019 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smartcdb42be2019-01-28 11:14:21 -080010020 if (unlikely(!phba->sli4_hba.hdwq))
James Smart7370d102019-01-28 11:14:20 -080010021 return NULL;
10022 /*
10023 * for abort iocb hba_wqidx should already
10024 * be setup based on what work queue we used.
10025 */
10026 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc4908502019-01-28 11:14:28 -080010027 lpfc_cmd = (struct lpfc_io_buf *)piocb->context1;
James Smart1fbf9742019-01-28 11:14:26 -080010028 piocb->hba_wqidx = lpfc_cmd->hdwq_no;
James Smart9bd2bff52014-09-03 12:57:30 -040010029 }
James Smartcdb42be2019-01-28 11:14:21 -080010030 return phba->sli4_hba.hdwq[piocb->hba_wqidx].fcp_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010031 } else {
10032 if (unlikely(!phba->sli4_hba.els_wq))
10033 return NULL;
10034 piocb->hba_wqidx = 0;
10035 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -040010036 }
James Smart9bd2bff52014-09-03 12:57:30 -040010037}
10038
James Smarte59058c2008-08-24 21:49:00 -040010039/**
James Smart3621a712009-04-06 18:47:14 -040010040 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -040010041 * @phba: Pointer to HBA context object.
10042 * @pring: Pointer to driver SLI ring object.
10043 * @piocb: Pointer to command iocb.
10044 * @flag: Flag indicating if this command can be put into txq.
10045 *
10046 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
10047 * function. This function gets the hbalock and calls
10048 * __lpfc_sli_issue_iocb function and will return the error returned
10049 * by __lpfc_sli_issue_iocb function. This wrapper is used by
10050 * functions which do not hold hbalock.
10051 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010052int
James Smart3772a992009-05-22 14:50:54 -040010053lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -050010054 struct lpfc_iocbq *piocb, uint32_t flag)
10055{
James Smart2a76a282012-08-03 12:35:54 -040010056 struct lpfc_sli_ring *pring;
James Smart92d7f7b2007-06-17 19:56:38 -050010057 unsigned long iflags;
James Smart6a828b02019-01-28 11:14:31 -080010058 int rc;
James Smart92d7f7b2007-06-17 19:56:38 -050010059
James Smart7e56aa22012-08-03 12:35:34 -040010060 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010061 pring = lpfc_sli4_calc_ring(phba, piocb);
10062 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010063 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -040010064
James Smart9bd2bff52014-09-03 12:57:30 -040010065 spin_lock_irqsave(&pring->ring_lock, iflags);
10066 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10067 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart7e56aa22012-08-03 12:35:34 -040010068 } else {
10069 /* For now, SLI2/3 will still use hbalock */
10070 spin_lock_irqsave(&phba->hbalock, iflags);
10071 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10072 spin_unlock_irqrestore(&phba->hbalock, iflags);
10073 }
James Smart92d7f7b2007-06-17 19:56:38 -050010074 return rc;
10075}
10076
James Smarte59058c2008-08-24 21:49:00 -040010077/**
James Smart3621a712009-04-06 18:47:14 -040010078 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010079 * @phba: Pointer to HBA context object.
10080 *
10081 * This function is called while driver attaches with the
10082 * HBA to setup the extra ring. The extra ring is used
10083 * only when driver needs to support target mode functionality
10084 * or IP over FC functionalities.
10085 *
James Smart895427b2017-02-12 13:52:30 -080010086 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -040010087 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010088static int
10089lpfc_extra_ring_setup( struct lpfc_hba *phba)
10090{
10091 struct lpfc_sli *psli;
10092 struct lpfc_sli_ring *pring;
10093
10094 psli = &phba->sli;
10095
10096 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -050010097
10098 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -080010099 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -040010100 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10101 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10102 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10103 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010104
James Smarta4bc3372006-12-02 13:34:16 -050010105 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -080010106 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -050010107
James Smart7e56aa22012-08-03 12:35:34 -040010108 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10109 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10110 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10111 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010112
10113 /* Setup default profile for this ring */
10114 pring->iotag_max = 4096;
10115 pring->num_mask = 1;
10116 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -050010117 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
10118 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010119 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
10120 return 0;
10121}
10122
James Smartcb69f7d2011-12-13 13:21:57 -050010123/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
10124 * @phba: Pointer to HBA context object.
10125 * @iocbq: Pointer to iocb object.
10126 *
10127 * The async_event handler calls this routine when it receives
10128 * an ASYNC_STATUS_CN event from the port. The port generates
10129 * this event when an Abort Sequence request to an rport fails
10130 * twice in succession. The abort could be originated by the
10131 * driver or by the port. The ABTS could have been for an ELS
10132 * or FCP IO. The port only generates this event when an ABTS
10133 * fails to complete after one retry.
10134 */
10135static void
10136lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
10137 struct lpfc_iocbq *iocbq)
10138{
10139 struct lpfc_nodelist *ndlp = NULL;
10140 uint16_t rpi = 0, vpi = 0;
10141 struct lpfc_vport *vport = NULL;
10142
10143 /* The rpi in the ulpContext is vport-sensitive. */
10144 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
10145 rpi = iocbq->iocb.ulpContext;
10146
10147 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10148 "3092 Port generated ABTS async event "
10149 "on vpi %d rpi %d status 0x%x\n",
10150 vpi, rpi, iocbq->iocb.ulpStatus);
10151
10152 vport = lpfc_find_vport_by_vpid(phba, vpi);
10153 if (!vport)
10154 goto err_exit;
10155 ndlp = lpfc_findnode_rpi(vport, rpi);
10156 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
10157 goto err_exit;
10158
10159 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
10160 lpfc_sli_abts_recover_port(vport, ndlp);
10161 return;
10162
10163 err_exit:
10164 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10165 "3095 Event Context not found, no "
10166 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
10167 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
10168 vpi, rpi);
10169}
10170
10171/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
10172 * @phba: pointer to HBA context object.
10173 * @ndlp: nodelist pointer for the impacted rport.
10174 * @axri: pointer to the wcqe containing the failed exchange.
10175 *
10176 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
10177 * port. The port generates this event when an abort exchange request to an
10178 * rport fails twice in succession with no reply. The abort could be originated
10179 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
10180 */
10181void
10182lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
10183 struct lpfc_nodelist *ndlp,
10184 struct sli4_wcqe_xri_aborted *axri)
10185{
10186 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -050010187 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -050010188
James Smart6b5151f2012-01-18 16:24:06 -050010189 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -050010190 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10191 "3115 Node Context not found, driver "
10192 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -050010193 return;
10194 }
10195
James Smartcb69f7d2011-12-13 13:21:57 -050010196 vport = ndlp->vport;
10197 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10198 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -050010199 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -040010200 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -050010201 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -050010202 bf_get(lpfc_wcqe_xa_status, axri),
10203 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -050010204
James Smart5c1db2a2012-03-01 22:34:36 -050010205 /*
10206 * Catch the ABTS protocol failure case. Older OCe FW releases returned
10207 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
10208 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
10209 */
James Smarte3d2b802012-08-14 14:25:43 -040010210 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -050010211 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
10212 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -050010213 lpfc_sli_abts_recover_port(vport, ndlp);
10214}
10215
James Smarte59058c2008-08-24 21:49:00 -040010216/**
James Smart3621a712009-04-06 18:47:14 -040010217 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -040010218 * @phba: Pointer to HBA context object.
10219 * @pring: Pointer to driver SLI ring object.
10220 * @iocbq: Pointer to iocb object.
10221 *
10222 * This function is called by the slow ring event handler
10223 * function when there is an ASYNC event iocb in the ring.
10224 * This function is called with no lock held.
10225 * Currently this function handles only temperature related
10226 * ASYNC events. The function decodes the temperature sensor
10227 * event message and posts events for the management applications.
10228 **/
James Smart98c9ea52007-10-27 13:37:33 -040010229static void
James Smart57127f12007-10-27 13:37:05 -040010230lpfc_sli_async_event_handler(struct lpfc_hba * phba,
10231 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
10232{
10233 IOCB_t *icmd;
10234 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -040010235 struct temp_event temp_event_data;
10236 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -040010237 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -040010238
10239 icmd = &iocbq->iocb;
10240 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -040010241
James Smartcb69f7d2011-12-13 13:21:57 -050010242 switch (evt_code) {
10243 case ASYNC_TEMP_WARN:
10244 case ASYNC_TEMP_SAFE:
10245 temp_event_data.data = (uint32_t) icmd->ulpContext;
10246 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
10247 if (evt_code == ASYNC_TEMP_WARN) {
10248 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
10249 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10250 "0347 Adapter is very hot, please take "
10251 "corrective action. temperature : %d Celsius\n",
10252 (uint32_t) icmd->ulpContext);
10253 } else {
10254 temp_event_data.event_code = LPFC_NORMAL_TEMP;
10255 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10256 "0340 Adapter temperature is OK now. "
10257 "temperature : %d Celsius\n",
10258 (uint32_t) icmd->ulpContext);
10259 }
10260
10261 /* Send temperature change event to applications */
10262 shost = lpfc_shost_from_vport(phba->pport);
10263 fc_host_post_vendor_event(shost, fc_get_event_number(),
10264 sizeof(temp_event_data), (char *) &temp_event_data,
10265 LPFC_NL_VENDOR_ID);
10266 break;
10267 case ASYNC_STATUS_CN:
10268 lpfc_sli_abts_err_handler(phba, iocbq);
10269 break;
10270 default:
James Smarta257bf92009-04-06 18:48:10 -040010271 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -050010272 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -040010273 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -040010274 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -040010275 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
10276 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
10277 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
10278 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -050010279 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -040010280 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
10281 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
10282 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
10283 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
10284
James Smartcb69f7d2011-12-13 13:21:57 -050010285 break;
James Smart57127f12007-10-27 13:37:05 -040010286 }
James Smart57127f12007-10-27 13:37:05 -040010287}
10288
10289
James Smarte59058c2008-08-24 21:49:00 -040010290/**
James Smart895427b2017-02-12 13:52:30 -080010291 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010292 * @phba: Pointer to HBA context object.
10293 *
10294 * lpfc_sli_setup sets up rings of the SLI interface with
10295 * number of iocbs per ring and iotags. This function is
10296 * called while driver attach to the HBA and before the
10297 * interrupts are enabled. So there is no need for locking.
10298 *
10299 * This function always returns 0.
10300 **/
dea31012005-04-17 16:05:31 -050010301int
James Smart895427b2017-02-12 13:52:30 -080010302lpfc_sli4_setup(struct lpfc_hba *phba)
10303{
10304 struct lpfc_sli_ring *pring;
10305
10306 pring = phba->sli4_hba.els_wq->pring;
10307 pring->num_mask = LPFC_MAX_RING_MASK;
10308 pring->prt[0].profile = 0; /* Mask 0 */
10309 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10310 pring->prt[0].type = FC_TYPE_ELS;
10311 pring->prt[0].lpfc_sli_rcv_unsol_event =
10312 lpfc_els_unsol_event;
10313 pring->prt[1].profile = 0; /* Mask 1 */
10314 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10315 pring->prt[1].type = FC_TYPE_ELS;
10316 pring->prt[1].lpfc_sli_rcv_unsol_event =
10317 lpfc_els_unsol_event;
10318 pring->prt[2].profile = 0; /* Mask 2 */
10319 /* NameServer Inquiry */
10320 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10321 /* NameServer */
10322 pring->prt[2].type = FC_TYPE_CT;
10323 pring->prt[2].lpfc_sli_rcv_unsol_event =
10324 lpfc_ct_unsol_event;
10325 pring->prt[3].profile = 0; /* Mask 3 */
10326 /* NameServer response */
10327 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10328 /* NameServer */
10329 pring->prt[3].type = FC_TYPE_CT;
10330 pring->prt[3].lpfc_sli_rcv_unsol_event =
10331 lpfc_ct_unsol_event;
10332 return 0;
10333}
10334
10335/**
10336 * lpfc_sli_setup - SLI ring setup function
10337 * @phba: Pointer to HBA context object.
10338 *
10339 * lpfc_sli_setup sets up rings of the SLI interface with
10340 * number of iocbs per ring and iotags. This function is
10341 * called while driver attach to the HBA and before the
10342 * interrupts are enabled. So there is no need for locking.
10343 *
10344 * This function always returns 0. SLI3 only.
10345 **/
10346int
dea31012005-04-17 16:05:31 -050010347lpfc_sli_setup(struct lpfc_hba *phba)
10348{
James Smarted957682007-06-17 19:56:37 -050010349 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -050010350 struct lpfc_sli *psli = &phba->sli;
10351 struct lpfc_sli_ring *pring;
10352
James Smart2a76a282012-08-03 12:35:54 -040010353 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -050010354 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -050010355
James Bottomley604a3e32005-10-29 10:28:33 -050010356 psli->iocbq_lookup = NULL;
10357 psli->iocbq_lookup_len = 0;
10358 psli->last_iotag = 0;
10359
dea31012005-04-17 16:05:31 -050010360 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010361 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010362 switch (i) {
10363 case LPFC_FCP_RING: /* ring 0 - FCP */
10364 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010365 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10366 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10367 pring->sli.sli3.numCiocb +=
10368 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10369 pring->sli.sli3.numRiocb +=
10370 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10371 pring->sli.sli3.numCiocb +=
10372 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10373 pring->sli.sli3.numRiocb +=
10374 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10375 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010376 SLI3_IOCB_CMD_SIZE :
10377 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010378 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010379 SLI3_IOCB_RSP_SIZE :
10380 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010381 pring->iotag_ctr = 0;
10382 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -050010383 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -050010384 pring->fast_iotag = pring->iotag_max;
10385 pring->num_mask = 0;
10386 break;
James Smarta4bc3372006-12-02 13:34:16 -050010387 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -050010388 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010389 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10390 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10391 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010392 SLI3_IOCB_CMD_SIZE :
10393 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010394 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010395 SLI3_IOCB_RSP_SIZE :
10396 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -050010397 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -050010398 pring->num_mask = 0;
10399 break;
10400 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10401 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010402 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10403 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10404 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010405 SLI3_IOCB_CMD_SIZE :
10406 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010407 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010408 SLI3_IOCB_RSP_SIZE :
10409 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010410 pring->fast_iotag = 0;
10411 pring->iotag_ctr = 0;
10412 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -040010413 pring->lpfc_sli_rcv_async_status =
10414 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -040010415 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -050010416 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -040010417 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10418 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010419 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010420 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010421 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -040010422 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10423 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010424 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010425 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010426 pring->prt[2].profile = 0; /* Mask 2 */
10427 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -040010428 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -050010429 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010430 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010431 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010432 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010433 pring->prt[3].profile = 0; /* Mask 3 */
10434 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -040010435 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -050010436 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010437 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010438 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010439 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010440 break;
10441 }
James Smart7e56aa22012-08-03 12:35:34 -040010442 totiocbsize += (pring->sli.sli3.numCiocb *
10443 pring->sli.sli3.sizeCiocb) +
10444 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050010445 }
James Smarted957682007-06-17 19:56:37 -050010446 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050010447 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040010448 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10449 "SLI2 SLIM Data: x%x x%lx\n",
10450 phba->brd_no, totiocbsize,
10451 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050010452 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010453 if (phba->cfg_multi_ring_support == 2)
10454 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050010455
10456 return 0;
10457}
10458
James Smarte59058c2008-08-24 21:49:00 -040010459/**
James Smart895427b2017-02-12 13:52:30 -080010460 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040010461 * @phba: Pointer to HBA context object.
10462 *
James Smart895427b2017-02-12 13:52:30 -080010463 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040010464 * ring. This function also initializes ring indices of each ring.
10465 * This function is called during the initialization of the SLI
10466 * interface of an HBA.
10467 * This function is called with no lock held and always returns
10468 * 1.
10469 **/
James Smart895427b2017-02-12 13:52:30 -080010470void
10471lpfc_sli4_queue_init(struct lpfc_hba *phba)
10472{
10473 struct lpfc_sli *psli;
10474 struct lpfc_sli_ring *pring;
10475 int i;
10476
10477 psli = &phba->sli;
10478 spin_lock_irq(&phba->hbalock);
10479 INIT_LIST_HEAD(&psli->mboxq);
10480 INIT_LIST_HEAD(&psli->mboxq_cmpl);
10481 /* Initialize list headers for txq and txcmplq as double linked lists */
James Smartcdb42be2019-01-28 11:14:21 -080010482 for (i = 0; i < phba->cfg_hdw_queue; i++) {
10483 pring = phba->sli4_hba.hdwq[i].fcp_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010484 pring->flag = 0;
10485 pring->ringno = LPFC_FCP_RING;
James Smartc4908502019-01-28 11:14:28 -080010486 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010487 INIT_LIST_HEAD(&pring->txq);
10488 INIT_LIST_HEAD(&pring->txcmplq);
10489 INIT_LIST_HEAD(&pring->iocb_continueq);
10490 spin_lock_init(&pring->ring_lock);
10491 }
10492 pring = phba->sli4_hba.els_wq->pring;
10493 pring->flag = 0;
10494 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080010495 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010496 INIT_LIST_HEAD(&pring->txq);
10497 INIT_LIST_HEAD(&pring->txcmplq);
10498 INIT_LIST_HEAD(&pring->iocb_continueq);
10499 spin_lock_init(&pring->ring_lock);
10500
James Smartcdb42be2019-01-28 11:14:21 -080010501 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10502 for (i = 0; i < phba->cfg_hdw_queue; i++) {
10503 pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
James Smartc4908502019-01-28 11:14:28 -080010504 pring->flag = 0;
James Smartcdb42be2019-01-28 11:14:21 -080010505 pring->ringno = LPFC_FCP_RING;
James Smartc4908502019-01-28 11:14:28 -080010506 pring->txcmplq_cnt = 0;
James Smartcdb42be2019-01-28 11:14:21 -080010507 INIT_LIST_HEAD(&pring->txq);
10508 INIT_LIST_HEAD(&pring->txcmplq);
10509 INIT_LIST_HEAD(&pring->iocb_continueq);
10510 spin_lock_init(&pring->ring_lock);
10511 }
James Smart895427b2017-02-12 13:52:30 -080010512 pring = phba->sli4_hba.nvmels_wq->pring;
10513 pring->flag = 0;
10514 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080010515 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010516 INIT_LIST_HEAD(&pring->txq);
10517 INIT_LIST_HEAD(&pring->txcmplq);
10518 INIT_LIST_HEAD(&pring->iocb_continueq);
10519 spin_lock_init(&pring->ring_lock);
10520 }
10521
10522 spin_unlock_irq(&phba->hbalock);
10523}
10524
10525/**
10526 * lpfc_sli_queue_init - Queue initialization function
10527 * @phba: Pointer to HBA context object.
10528 *
10529 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10530 * ring. This function also initializes ring indices of each ring.
10531 * This function is called during the initialization of the SLI
10532 * interface of an HBA.
10533 * This function is called with no lock held and always returns
10534 * 1.
10535 **/
10536void
10537lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010538{
10539 struct lpfc_sli *psli;
10540 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050010541 int i;
dea31012005-04-17 16:05:31 -050010542
10543 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050010544 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010545 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050010546 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050010547 /* Initialize list headers for txq and txcmplq as double linked lists */
10548 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010549 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010550 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040010551 pring->sli.sli3.next_cmdidx = 0;
10552 pring->sli.sli3.local_getidx = 0;
10553 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050010554 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050010555 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050010556 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080010557 pring->flag = 0;
10558 INIT_LIST_HEAD(&pring->txq);
10559 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010560 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010561 }
James Smart2e0fef82007-06-17 19:56:36 -050010562 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010563}
10564
James Smarte59058c2008-08-24 21:49:00 -040010565/**
James Smart04c68492009-05-22 14:52:52 -040010566 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10567 * @phba: Pointer to HBA context object.
10568 *
10569 * This routine flushes the mailbox command subsystem. It will unconditionally
10570 * flush all the mailbox commands in the three possible stages in the mailbox
10571 * command sub-system: pending mailbox command queue; the outstanding mailbox
10572 * command; and completed mailbox command queue. It is caller's responsibility
10573 * to make sure that the driver is in the proper state to flush the mailbox
10574 * command sub-system. Namely, the posting of mailbox commands into the
10575 * pending mailbox command queue from the various clients must be stopped;
10576 * either the HBA is in a state that it will never works on the outstanding
10577 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10578 * mailbox command has been completed.
10579 **/
10580static void
10581lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10582{
10583 LIST_HEAD(completions);
10584 struct lpfc_sli *psli = &phba->sli;
10585 LPFC_MBOXQ_t *pmb;
10586 unsigned long iflag;
10587
James Smart523128e2018-09-10 10:30:46 -070010588 /* Disable softirqs, including timers from obtaining phba->hbalock */
10589 local_bh_disable();
10590
James Smart04c68492009-05-22 14:52:52 -040010591 /* Flush all the mailbox commands in the mbox system */
10592 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart523128e2018-09-10 10:30:46 -070010593
James Smart04c68492009-05-22 14:52:52 -040010594 /* The pending mailbox command queue */
10595 list_splice_init(&phba->sli.mboxq, &completions);
10596 /* The outstanding active mailbox command */
10597 if (psli->mbox_active) {
10598 list_add_tail(&psli->mbox_active->list, &completions);
10599 psli->mbox_active = NULL;
10600 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10601 }
10602 /* The completed mailbox command queue */
10603 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10604 spin_unlock_irqrestore(&phba->hbalock, iflag);
10605
James Smart523128e2018-09-10 10:30:46 -070010606 /* Enable softirqs again, done with phba->hbalock */
10607 local_bh_enable();
10608
James Smart04c68492009-05-22 14:52:52 -040010609 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10610 while (!list_empty(&completions)) {
10611 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10612 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10613 if (pmb->mbox_cmpl)
10614 pmb->mbox_cmpl(phba, pmb);
10615 }
10616}
10617
10618/**
James Smart3621a712009-04-06 18:47:14 -040010619 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010620 * @vport: Pointer to virtual port object.
10621 *
10622 * lpfc_sli_host_down is called to clean up the resources
10623 * associated with a vport before destroying virtual
10624 * port data structures.
10625 * This function does following operations:
10626 * - Free discovery resources associated with this virtual
10627 * port.
10628 * - Free iocbs associated with this virtual port in
10629 * the txq.
10630 * - Send abort for all iocb commands associated with this
10631 * vport in txcmplq.
10632 *
10633 * This function is called with no lock held and always returns 1.
10634 **/
dea31012005-04-17 16:05:31 -050010635int
James Smart92d7f7b2007-06-17 19:56:38 -050010636lpfc_sli_host_down(struct lpfc_vport *vport)
10637{
James Smart858c9f62007-06-17 19:56:39 -050010638 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010639 struct lpfc_hba *phba = vport->phba;
10640 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010641 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010642 struct lpfc_sli_ring *pring;
10643 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010644 int i;
10645 unsigned long flags = 0;
10646 uint16_t prev_pring_flag;
10647
10648 lpfc_cleanup_discovery_resources(vport);
10649
10650 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010651
James Smart895427b2017-02-12 13:52:30 -080010652 /*
10653 * Error everything on the txq since these iocbs
10654 * have not been given to the FW yet.
10655 * Also issue ABTS for everything on the txcmplq
10656 */
10657 if (phba->sli_rev != LPFC_SLI_REV4) {
10658 for (i = 0; i < psli->num_rings; i++) {
10659 pring = &psli->sli3_ring[i];
10660 prev_pring_flag = pring->flag;
10661 /* Only slow rings */
10662 if (pring->ringno == LPFC_ELS_RING) {
10663 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10664 /* Set the lpfc data pending flag */
10665 set_bit(LPFC_DATA_READY, &phba->data_flags);
10666 }
10667 list_for_each_entry_safe(iocb, next_iocb,
10668 &pring->txq, list) {
10669 if (iocb->vport != vport)
10670 continue;
10671 list_move_tail(&iocb->list, &completions);
10672 }
10673 list_for_each_entry_safe(iocb, next_iocb,
10674 &pring->txcmplq, list) {
10675 if (iocb->vport != vport)
10676 continue;
10677 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10678 }
10679 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010680 }
James Smart895427b2017-02-12 13:52:30 -080010681 } else {
10682 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10683 pring = qp->pring;
10684 if (!pring)
10685 continue;
10686 if (pring == phba->sli4_hba.els_wq->pring) {
10687 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10688 /* Set the lpfc data pending flag */
10689 set_bit(LPFC_DATA_READY, &phba->data_flags);
10690 }
10691 prev_pring_flag = pring->flag;
10692 spin_lock_irq(&pring->ring_lock);
10693 list_for_each_entry_safe(iocb, next_iocb,
10694 &pring->txq, list) {
10695 if (iocb->vport != vport)
10696 continue;
10697 list_move_tail(&iocb->list, &completions);
10698 }
10699 spin_unlock_irq(&pring->ring_lock);
10700 list_for_each_entry_safe(iocb, next_iocb,
10701 &pring->txcmplq, list) {
10702 if (iocb->vport != vport)
10703 continue;
10704 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10705 }
10706 pring->flag = prev_pring_flag;
10707 }
James Smart92d7f7b2007-06-17 19:56:38 -050010708 }
James Smart92d7f7b2007-06-17 19:56:38 -050010709 spin_unlock_irqrestore(&phba->hbalock, flags);
10710
James Smarta257bf92009-04-06 18:48:10 -040010711 /* Cancel all the IOCBs from the completions list */
10712 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10713 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010714 return 1;
10715}
10716
James Smarte59058c2008-08-24 21:49:00 -040010717/**
James Smart3621a712009-04-06 18:47:14 -040010718 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010719 * @phba: Pointer to HBA context object.
10720 *
10721 * This function cleans up all iocb, buffers, mailbox commands
10722 * while shutting down the HBA. This function is called with no
10723 * lock held and always returns 1.
10724 * This function does the following to cleanup driver resources:
10725 * - Free discovery resources for each virtual port
10726 * - Cleanup any pending fabric iocbs
10727 * - Iterate through the iocb txq and free each entry
10728 * in the list.
10729 * - Free up any buffer posted to the HBA
10730 * - Free mailbox commands in the mailbox queue.
10731 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010732int
James Smart2e0fef82007-06-17 19:56:36 -050010733lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010734{
James Smart2534ba72007-04-25 09:52:20 -040010735 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010736 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010737 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010738 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010739 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010740 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010741 int i;
10742
10743 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010744 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010745
dea31012005-04-17 16:05:31 -050010746 lpfc_hba_down_prep(phba);
10747
James Smart523128e2018-09-10 10:30:46 -070010748 /* Disable softirqs, including timers from obtaining phba->hbalock */
10749 local_bh_disable();
10750
James Smart92d7f7b2007-06-17 19:56:38 -050010751 lpfc_fabric_abort_hba(phba);
10752
James Smart2e0fef82007-06-17 19:56:36 -050010753 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010754
James Smart895427b2017-02-12 13:52:30 -080010755 /*
10756 * Error everything on the txq since these iocbs
10757 * have not been given to the FW yet.
10758 */
10759 if (phba->sli_rev != LPFC_SLI_REV4) {
10760 for (i = 0; i < psli->num_rings; i++) {
10761 pring = &psli->sli3_ring[i];
10762 /* Only slow rings */
10763 if (pring->ringno == LPFC_ELS_RING) {
10764 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10765 /* Set the lpfc data pending flag */
10766 set_bit(LPFC_DATA_READY, &phba->data_flags);
10767 }
10768 list_splice_init(&pring->txq, &completions);
10769 }
10770 } else {
10771 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10772 pring = qp->pring;
10773 if (!pring)
10774 continue;
10775 spin_lock_irq(&pring->ring_lock);
10776 list_splice_init(&pring->txq, &completions);
10777 spin_unlock_irq(&pring->ring_lock);
10778 if (pring == phba->sli4_hba.els_wq->pring) {
10779 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10780 /* Set the lpfc data pending flag */
10781 set_bit(LPFC_DATA_READY, &phba->data_flags);
10782 }
10783 }
dea31012005-04-17 16:05:31 -050010784 }
James Smart2e0fef82007-06-17 19:56:36 -050010785 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010786
James Smarta257bf92009-04-06 18:48:10 -040010787 /* Cancel all the IOCBs from the completions list */
10788 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10789 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010790
James Smart0ff10d42008-01-11 01:52:36 -050010791 spin_lock_irqsave(&phba->hbalock, flags);
10792 list_splice_init(&phba->elsbuf, &completions);
10793 phba->elsbuf_cnt = 0;
10794 phba->elsbuf_prev_cnt = 0;
10795 spin_unlock_irqrestore(&phba->hbalock, flags);
10796
10797 while (!list_empty(&completions)) {
10798 list_remove_head(&completions, buf_ptr,
10799 struct lpfc_dmabuf, list);
10800 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10801 kfree(buf_ptr);
10802 }
10803
James Smart523128e2018-09-10 10:30:46 -070010804 /* Enable softirqs again, done with phba->hbalock */
10805 local_bh_enable();
10806
dea31012005-04-17 16:05:31 -050010807 /* Return any active mbox cmds */
10808 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010809
James Smartda0436e2009-05-22 14:51:39 -040010810 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010811 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010812 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010813
James Smartda0436e2009-05-22 14:51:39 -040010814 return 1;
10815}
James Smart92d7f7b2007-06-17 19:56:38 -050010816
James Smartda0436e2009-05-22 14:51:39 -040010817/**
James Smart3621a712009-04-06 18:47:14 -040010818 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010819 * @srcp: Source memory pointer.
10820 * @destp: Destination memory pointer.
10821 * @cnt: Number of words required to be copied.
10822 *
10823 * This function is used for copying data between driver memory
10824 * and the SLI memory. This function also changes the endianness
10825 * of each word if native endianness is different from SLI
10826 * endianness. This function can be called with or without
10827 * lock.
10828 **/
dea31012005-04-17 16:05:31 -050010829void
10830lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10831{
10832 uint32_t *src = srcp;
10833 uint32_t *dest = destp;
10834 uint32_t ldata;
10835 int i;
10836
10837 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10838 ldata = *src;
10839 ldata = le32_to_cpu(ldata);
10840 *dest = ldata;
10841 src++;
10842 dest++;
10843 }
10844}
10845
James Smarte59058c2008-08-24 21:49:00 -040010846
10847/**
James Smarta0c87cb2009-07-19 10:01:10 -040010848 * lpfc_sli_bemem_bcopy - SLI memory copy function
10849 * @srcp: Source memory pointer.
10850 * @destp: Destination memory pointer.
10851 * @cnt: Number of words required to be copied.
10852 *
10853 * This function is used for copying data between a data structure
10854 * with big endian representation to local endianness.
10855 * This function can be called with or without lock.
10856 **/
10857void
10858lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10859{
10860 uint32_t *src = srcp;
10861 uint32_t *dest = destp;
10862 uint32_t ldata;
10863 int i;
10864
10865 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10866 ldata = *src;
10867 ldata = be32_to_cpu(ldata);
10868 *dest = ldata;
10869 src++;
10870 dest++;
10871 }
10872}
10873
10874/**
James Smart3621a712009-04-06 18:47:14 -040010875 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010876 * @phba: Pointer to HBA context object.
10877 * @pring: Pointer to driver SLI ring object.
10878 * @mp: Pointer to driver buffer object.
10879 *
10880 * This function is called with no lock held.
10881 * It always return zero after adding the buffer to the postbufq
10882 * buffer list.
10883 **/
dea31012005-04-17 16:05:31 -050010884int
James Smart2e0fef82007-06-17 19:56:36 -050010885lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10886 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010887{
10888 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10889 later */
James Smart2e0fef82007-06-17 19:56:36 -050010890 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010891 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010892 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010893 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010894 return 0;
10895}
10896
James Smarte59058c2008-08-24 21:49:00 -040010897/**
James Smart3621a712009-04-06 18:47:14 -040010898 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010899 * @phba: Pointer to HBA context object.
10900 *
10901 * When HBQ is enabled, buffers are searched based on tags. This function
10902 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10903 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10904 * does not conflict with tags of buffer posted for unsolicited events.
10905 * The function returns the allocated tag. The function is called with
10906 * no locks held.
10907 **/
James Smart76bb24e2007-10-27 13:38:00 -040010908uint32_t
10909lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10910{
10911 spin_lock_irq(&phba->hbalock);
10912 phba->buffer_tag_count++;
10913 /*
10914 * Always set the QUE_BUFTAG_BIT to distiguish between
10915 * a tag assigned by HBQ.
10916 */
10917 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10918 spin_unlock_irq(&phba->hbalock);
10919 return phba->buffer_tag_count;
10920}
10921
James Smarte59058c2008-08-24 21:49:00 -040010922/**
James Smart3621a712009-04-06 18:47:14 -040010923 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010924 * @phba: Pointer to HBA context object.
10925 * @pring: Pointer to driver SLI ring object.
10926 * @tag: Buffer tag.
10927 *
10928 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10929 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10930 * iocb is posted to the response ring with the tag of the buffer.
10931 * This function searches the pring->postbufq list using the tag
10932 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10933 * iocb. If the buffer is found then lpfc_dmabuf object of the
10934 * buffer is returned to the caller else NULL is returned.
10935 * This function is called with no lock held.
10936 **/
James Smart76bb24e2007-10-27 13:38:00 -040010937struct lpfc_dmabuf *
10938lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10939 uint32_t tag)
10940{
10941 struct lpfc_dmabuf *mp, *next_mp;
10942 struct list_head *slp = &pring->postbufq;
10943
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010944 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010945 spin_lock_irq(&phba->hbalock);
10946 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10947 if (mp->buffer_tag == tag) {
10948 list_del_init(&mp->list);
10949 pring->postbufq_cnt--;
10950 spin_unlock_irq(&phba->hbalock);
10951 return mp;
10952 }
10953 }
10954
10955 spin_unlock_irq(&phba->hbalock);
10956 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010957 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010958 "ring %d Data x%lx x%p x%p x%x\n",
10959 pring->ringno, (unsigned long) tag,
10960 slp->next, slp->prev, pring->postbufq_cnt);
10961
10962 return NULL;
10963}
dea31012005-04-17 16:05:31 -050010964
James Smarte59058c2008-08-24 21:49:00 -040010965/**
James Smart3621a712009-04-06 18:47:14 -040010966 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010967 * @phba: Pointer to HBA context object.
10968 * @pring: Pointer to driver SLI ring object.
10969 * @phys: DMA address of the buffer.
10970 *
10971 * This function searches the buffer list using the dma_address
10972 * of unsolicited event to find the driver's lpfc_dmabuf object
10973 * corresponding to the dma_address. The function returns the
10974 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10975 * This function is called by the ct and els unsolicited event
10976 * handlers to get the buffer associated with the unsolicited
10977 * event.
10978 *
10979 * This function is called with no lock held.
10980 **/
dea31012005-04-17 16:05:31 -050010981struct lpfc_dmabuf *
10982lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10983 dma_addr_t phys)
10984{
10985 struct lpfc_dmabuf *mp, *next_mp;
10986 struct list_head *slp = &pring->postbufq;
10987
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010988 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010989 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010990 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10991 if (mp->phys == phys) {
10992 list_del_init(&mp->list);
10993 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010994 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010995 return mp;
10996 }
10997 }
10998
James Smart2e0fef82007-06-17 19:56:36 -050010999 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011000 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040011001 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050011002 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011003 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050011004 slp->next, slp->prev, pring->postbufq_cnt);
11005 return NULL;
11006}
11007
James Smarte59058c2008-08-24 21:49:00 -040011008/**
James Smart3621a712009-04-06 18:47:14 -040011009 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040011010 * @phba: Pointer to HBA context object.
11011 * @cmdiocb: Pointer to driver command iocb object.
11012 * @rspiocb: Pointer to driver response iocb object.
11013 *
11014 * This function is the completion handler for the abort iocbs for
11015 * ELS commands. This function is called from the ELS ring event
11016 * handler with no lock held. This function frees memory resources
11017 * associated with the abort iocb.
11018 **/
dea31012005-04-17 16:05:31 -050011019static void
James Smart2e0fef82007-06-17 19:56:36 -050011020lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11021 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050011022{
James Smart2e0fef82007-06-17 19:56:36 -050011023 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040011024 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050011025 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040011026
11027 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050011028
11029 /*
11030 * Assume that the port already completed and returned, or
11031 * will return the iocb. Just Log the message.
11032 */
James Smart2680eea2007-04-25 09:52:55 -040011033 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
11034 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
11035
James Smart2e0fef82007-06-17 19:56:36 -050011036 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040011037 if (phba->sli_rev < LPFC_SLI_REV4) {
James Smartfaa832e2018-07-31 17:23:18 -070011038 if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
11039 irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
11040 irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
11041 spin_unlock_irq(&phba->hbalock);
11042 goto release_iocb;
11043 }
James Smart45ed1192009-10-02 15:17:02 -040011044 if (abort_iotag != 0 &&
11045 abort_iotag <= phba->sli.last_iotag)
11046 abort_iocb =
11047 phba->sli.iocbq_lookup[abort_iotag];
11048 } else
11049 /* For sli4 the abort_tag is the XRI,
11050 * so the abort routine puts the iotag of the iocb
11051 * being aborted in the context field of the abort
11052 * IOCB.
11053 */
11054 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040011055
James Smart2a9bf3d2010-06-07 15:24:45 -040011056 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
11057 "0327 Cannot abort els iocb %p "
11058 "with tag %x context %x, abort status %x, "
11059 "abort code %x\n",
11060 abort_iocb, abort_iotag, abort_context,
11061 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040011062
James Smartff78d8f2011-12-13 13:21:35 -050011063 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040011064 }
James Smartfaa832e2018-07-31 17:23:18 -070011065release_iocb:
James Bottomley604a3e32005-10-29 10:28:33 -050011066 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050011067 return;
11068}
11069
James Smarte59058c2008-08-24 21:49:00 -040011070/**
James Smart3621a712009-04-06 18:47:14 -040011071 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040011072 * @phba: Pointer to HBA context object.
11073 * @cmdiocb: Pointer to driver command iocb object.
11074 * @rspiocb: Pointer to driver response iocb object.
11075 *
11076 * The function is called from SLI ring event handler with no
11077 * lock held. This function is the completion handler for ELS commands
11078 * which are aborted. The function frees memory resources used for
11079 * the aborted ELS commands.
11080 **/
James Smart92d7f7b2007-06-17 19:56:38 -050011081static void
11082lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11083 struct lpfc_iocbq *rspiocb)
11084{
11085 IOCB_t *irsp = &rspiocb->iocb;
11086
11087 /* ELS cmd tag <ulpIoTag> completes */
11088 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040011089 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050011090 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011091 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050011092 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050011093 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
11094 lpfc_ct_free_iocb(phba, cmdiocb);
11095 else
11096 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050011097 return;
11098}
11099
James Smarte59058c2008-08-24 21:49:00 -040011100/**
James Smart5af5eee2010-10-22 11:06:38 -040011101 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040011102 * @phba: Pointer to HBA context object.
11103 * @pring: Pointer to driver SLI ring object.
11104 * @cmdiocb: Pointer to driver command iocb object.
11105 *
James Smart5af5eee2010-10-22 11:06:38 -040011106 * This function issues an abort iocb for the provided command iocb down to
11107 * the port. Other than the case the outstanding command iocb is an abort
11108 * request, this function issues abort out unconditionally. This function is
11109 * called with hbalock held. The function returns 0 when it fails due to
11110 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040011111 **/
James Smart5af5eee2010-10-22 11:06:38 -040011112static int
11113lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050011114 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050011115{
James Smart2e0fef82007-06-17 19:56:36 -050011116 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011117 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050011118 IOCB_t *icmd = NULL;
11119 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011120 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040011121 unsigned long iflags;
James Smartfaa832e2018-07-31 17:23:18 -070011122 struct lpfc_nodelist *ndlp;
James Smart07951072007-04-25 09:51:38 -040011123
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011124 lockdep_assert_held(&phba->hbalock);
11125
James Smart92d7f7b2007-06-17 19:56:38 -050011126 /*
11127 * There are certain command types we don't want to abort. And we
11128 * don't want to abort commands that are already in the process of
11129 * being aborted.
James Smart07951072007-04-25 09:51:38 -040011130 */
11131 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050011132 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050011133 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11134 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040011135 return 0;
11136
dea31012005-04-17 16:05:31 -050011137 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050011138 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011139 if (abtsiocbp == NULL)
11140 return 0;
dea31012005-04-17 16:05:31 -050011141
James Smart07951072007-04-25 09:51:38 -040011142 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050011143 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040011144 */
11145 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11146
dea31012005-04-17 16:05:31 -050011147 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040011148 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
11149 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040011150 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040011151 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040011152 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
James Smartfaa832e2018-07-31 17:23:18 -070011153 } else {
James Smartda0436e2009-05-22 14:51:39 -040011154 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
James Smartfaa832e2018-07-31 17:23:18 -070011155 if (pring->ringno == LPFC_ELS_RING) {
11156 ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
11157 iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
11158 }
11159 }
dea31012005-04-17 16:05:31 -050011160 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040011161 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050011162
James Smart5ffc2662009-11-18 15:39:44 -050011163 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011164 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011165 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
11166 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011167 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
11168 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011169
James Smart2e0fef82007-06-17 19:56:36 -050011170 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040011171 iabt->ulpCommand = CMD_ABORT_XRI_CN;
11172 else
11173 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
11174
11175 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080011176 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040011177
James Smarte8b62012007-08-02 11:10:09 -040011178 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
11179 "0339 Abort xri x%x, original iotag x%x, "
11180 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040011181 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040011182 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040011183 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040011184
11185 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011186 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
11187 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040011188 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040011189 /* Note: both hbalock and ring_lock need to be set here */
11190 spin_lock_irqsave(&pring->ring_lock, iflags);
11191 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11192 abtsiocbp, 0);
11193 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11194 } else {
11195 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11196 abtsiocbp, 0);
11197 }
James Smart07951072007-04-25 09:51:38 -040011198
James Smartd7c255b2008-08-24 21:50:00 -040011199 if (retval)
11200 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040011201
11202 /*
11203 * Caller to this routine should check for IOCB_ERROR
11204 * and handle it properly. This routine no longer removes
11205 * iocb off txcmplq and call compl in case of IOCB_ERROR.
11206 */
11207 return retval;
11208}
11209
11210/**
11211 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
11212 * @phba: Pointer to HBA context object.
11213 * @pring: Pointer to driver SLI ring object.
11214 * @cmdiocb: Pointer to driver command iocb object.
11215 *
11216 * This function issues an abort iocb for the provided command iocb. In case
11217 * of unloading, the abort iocb will not be issued to commands on the ELS
11218 * ring. Instead, the callback function shall be changed to those commands
11219 * so that nothing happens when them finishes. This function is called with
11220 * hbalock held. The function returns 0 when the command iocb is an abort
11221 * request.
11222 **/
11223int
11224lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11225 struct lpfc_iocbq *cmdiocb)
11226{
11227 struct lpfc_vport *vport = cmdiocb->vport;
11228 int retval = IOCB_ERROR;
11229 IOCB_t *icmd = NULL;
11230
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011231 lockdep_assert_held(&phba->hbalock);
11232
James Smart5af5eee2010-10-22 11:06:38 -040011233 /*
11234 * There are certain command types we don't want to abort. And we
11235 * don't want to abort commands that are already in the process of
11236 * being aborted.
11237 */
11238 icmd = &cmdiocb->iocb;
11239 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
11240 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11241 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11242 return 0;
11243
Dick Kennedy1234a6d2017-09-29 17:34:29 -070011244 if (!pring) {
11245 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11246 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11247 else
11248 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11249 goto abort_iotag_exit;
11250 }
11251
James Smart5af5eee2010-10-22 11:06:38 -040011252 /*
11253 * If we're unloading, don't abort iocb on the ELS ring, but change
11254 * the callback so that nothing happens when it finishes.
11255 */
11256 if ((vport->load_flag & FC_UNLOADING) &&
11257 (pring->ringno == LPFC_ELS_RING)) {
11258 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11259 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11260 else
11261 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11262 goto abort_iotag_exit;
11263 }
11264
11265 /* Now, we try to issue the abort to the cmdiocb out */
11266 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
11267
James Smart07951072007-04-25 09:51:38 -040011268abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050011269 /*
11270 * Caller to this routine should check for IOCB_ERROR
11271 * and handle it properly. This routine no longer removes
11272 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040011273 */
James Smart2e0fef82007-06-17 19:56:36 -050011274 return retval;
dea31012005-04-17 16:05:31 -050011275}
11276
James Smarte59058c2008-08-24 21:49:00 -040011277/**
James Smart5af5eee2010-10-22 11:06:38 -040011278 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11279 * @phba: pointer to lpfc HBA data structure.
11280 *
11281 * This routine will abort all pending and outstanding iocbs to an HBA.
11282 **/
11283void
11284lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11285{
11286 struct lpfc_sli *psli = &phba->sli;
11287 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080011288 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011289 int i;
11290
James Smart895427b2017-02-12 13:52:30 -080011291 if (phba->sli_rev != LPFC_SLI_REV4) {
11292 for (i = 0; i < psli->num_rings; i++) {
11293 pring = &psli->sli3_ring[i];
11294 lpfc_sli_abort_iocb_ring(phba, pring);
11295 }
11296 return;
11297 }
11298 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11299 pring = qp->pring;
11300 if (!pring)
11301 continue;
James Smartdb55fba2014-04-04 13:52:02 -040011302 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040011303 }
11304}
11305
11306/**
James Smart3621a712009-04-06 18:47:14 -040011307 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040011308 * @iocbq: Pointer to driver iocb object.
11309 * @vport: Pointer to driver virtual port object.
11310 * @tgt_id: SCSI ID of the target.
11311 * @lun_id: LUN ID of the scsi device.
11312 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11313 *
James Smart3621a712009-04-06 18:47:14 -040011314 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040011315 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11316 * 0 if the filtering criteria is met for the given iocb and will return
11317 * 1 if the filtering criteria is not met.
11318 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11319 * given iocb is for the SCSI device specified by vport, tgt_id and
11320 * lun_id parameter.
11321 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11322 * given iocb is for the SCSI target specified by vport and tgt_id
11323 * parameters.
11324 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11325 * given iocb is for the SCSI host associated with the given vport.
11326 * This function is called with no locks held.
11327 **/
dea31012005-04-17 16:05:31 -050011328static int
James Smart51ef4c22007-08-02 11:10:31 -040011329lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11330 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011331 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011332{
James Smartc4908502019-01-28 11:14:28 -080011333 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011334 int rc = 1;
11335
James Smartb0e83012018-06-26 08:24:29 -070011336 if (iocbq->vport != vport)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011337 return rc;
11338
James Smartb0e83012018-06-26 08:24:29 -070011339 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
11340 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
James Smart51ef4c22007-08-02 11:10:31 -040011341 return rc;
11342
James Smartc4908502019-01-28 11:14:28 -080011343 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011344
James Smart495a7142008-06-14 22:52:59 -040011345 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050011346 return rc;
11347
11348 switch (ctx_cmd) {
11349 case LPFC_CTX_LUN:
James Smartb0e83012018-06-26 08:24:29 -070011350 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011351 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11352 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050011353 rc = 0;
11354 break;
11355 case LPFC_CTX_TGT:
James Smartb0e83012018-06-26 08:24:29 -070011356 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011357 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050011358 rc = 0;
11359 break;
dea31012005-04-17 16:05:31 -050011360 case LPFC_CTX_HOST:
11361 rc = 0;
11362 break;
11363 default:
11364 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070011365 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050011366 break;
11367 }
11368
11369 return rc;
11370}
11371
James Smarte59058c2008-08-24 21:49:00 -040011372/**
James Smart3621a712009-04-06 18:47:14 -040011373 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040011374 * @vport: Pointer to virtual port.
11375 * @tgt_id: SCSI ID of the target.
11376 * @lun_id: LUN ID of the scsi device.
11377 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11378 *
11379 * This function returns number of FCP commands pending for the vport.
11380 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11381 * commands pending on the vport associated with SCSI device specified
11382 * by tgt_id and lun_id parameters.
11383 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11384 * commands pending on the vport associated with SCSI target specified
11385 * by tgt_id parameter.
11386 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11387 * commands pending on the vport.
11388 * This function returns the number of iocbs which satisfy the filter.
11389 * This function is called without any lock held.
11390 **/
dea31012005-04-17 16:05:31 -050011391int
James Smart51ef4c22007-08-02 11:10:31 -040011392lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11393 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011394{
James Smart51ef4c22007-08-02 11:10:31 -040011395 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011396 struct lpfc_iocbq *iocbq;
11397 int sum, i;
dea31012005-04-17 16:05:31 -050011398
Johannes Thumshirn31979002016-07-18 16:06:03 +020011399 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011400 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11401 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011402
James Smart51ef4c22007-08-02 11:10:31 -040011403 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11404 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011405 sum++;
dea31012005-04-17 16:05:31 -050011406 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020011407 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011408
dea31012005-04-17 16:05:31 -050011409 return sum;
11410}
11411
James Smarte59058c2008-08-24 21:49:00 -040011412/**
James Smart3621a712009-04-06 18:47:14 -040011413 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040011414 * @phba: Pointer to HBA context object
11415 * @cmdiocb: Pointer to command iocb object.
11416 * @rspiocb: Pointer to response iocb object.
11417 *
11418 * This function is called when an aborted FCP iocb completes. This
11419 * function is called by the ring event handler with no lock held.
11420 * This function frees the iocb.
11421 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011422void
James Smart2e0fef82007-06-17 19:56:36 -050011423lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11424 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011425{
James Smartcb69f7d2011-12-13 13:21:57 -050011426 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040011427 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050011428 "original iotag x%x, abort cmd iotag x%x "
11429 "status 0x%x, reason 0x%x\n",
11430 cmdiocb->iocb.un.acxri.abortContextTag,
11431 cmdiocb->iocb.un.acxri.abortIoTag,
11432 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11433 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050011434 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011435 return;
11436}
11437
James Smarte59058c2008-08-24 21:49:00 -040011438/**
James Smart3621a712009-04-06 18:47:14 -040011439 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040011440 * @vport: Pointer to virtual port.
11441 * @pring: Pointer to driver SLI ring object.
11442 * @tgt_id: SCSI ID of the target.
11443 * @lun_id: LUN ID of the scsi device.
11444 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11445 *
11446 * This function sends an abort command for every SCSI command
11447 * associated with the given virtual port pending on the ring
11448 * filtered by lpfc_sli_validate_fcp_iocb function.
11449 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11450 * FCP iocbs associated with lun specified by tgt_id and lun_id
11451 * parameters
11452 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11453 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11454 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11455 * FCP iocbs associated with virtual port.
11456 * This function returns number of iocbs it failed to abort.
11457 * This function is called with no locks held.
11458 **/
dea31012005-04-17 16:05:31 -050011459int
James Smart51ef4c22007-08-02 11:10:31 -040011460lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11461 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050011462{
James Smart51ef4c22007-08-02 11:10:31 -040011463 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011464 struct lpfc_iocbq *iocbq;
11465 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070011466 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050011467 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050011468 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011469 int i;
dea31012005-04-17 16:05:31 -050011470
James Smartb0e83012018-06-26 08:24:29 -070011471 /* all I/Os are in process of being flushed */
11472 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH)
11473 return errcnt;
11474
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011475 for (i = 1; i <= phba->sli.last_iotag; i++) {
11476 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011477
James Smart51ef4c22007-08-02 11:10:31 -040011478 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011479 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011480 continue;
11481
James Smartafbd8d82013-09-06 12:22:13 -040011482 /*
11483 * If the iocbq is already being aborted, don't take a second
11484 * action, but do count it.
11485 */
11486 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11487 continue;
11488
dea31012005-04-17 16:05:31 -050011489 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011490 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011491 if (abtsiocb == NULL) {
11492 errcnt++;
11493 continue;
11494 }
dea31012005-04-17 16:05:31 -050011495
James Smartafbd8d82013-09-06 12:22:13 -040011496 /* indicate the IO is being aborted by the driver. */
11497 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11498
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011499 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011500 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11501 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011502 if (phba->sli_rev == LPFC_SLI_REV4)
11503 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11504 else
11505 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011506 abtsiocb->iocb.ulpLe = 1;
11507 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011508 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011509
James Smart5ffc2662009-11-18 15:39:44 -050011510 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011511 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011512 if (iocbq->iocb_flag & LPFC_IO_FCP)
11513 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011514 if (iocbq->iocb_flag & LPFC_IO_FOF)
11515 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011516
James Smart2e0fef82007-06-17 19:56:36 -050011517 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011518 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11519 else
11520 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11521
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011522 /* Setup callback routine and issue the command. */
11523 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011524 if (phba->sli_rev == LPFC_SLI_REV4) {
11525 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11526 if (!pring_s4)
11527 continue;
11528 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11529 abtsiocb, 0);
11530 } else
11531 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11532 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011533 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011534 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011535 errcnt++;
11536 continue;
11537 }
11538 }
11539
11540 return errcnt;
11541}
11542
James Smarte59058c2008-08-24 21:49:00 -040011543/**
James Smart98912dda2014-04-04 13:52:31 -040011544 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11545 * @vport: Pointer to virtual port.
11546 * @pring: Pointer to driver SLI ring object.
11547 * @tgt_id: SCSI ID of the target.
11548 * @lun_id: LUN ID of the scsi device.
11549 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11550 *
11551 * This function sends an abort command for every SCSI command
11552 * associated with the given virtual port pending on the ring
11553 * filtered by lpfc_sli_validate_fcp_iocb function.
11554 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11555 * FCP iocbs associated with lun specified by tgt_id and lun_id
11556 * parameters
11557 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11558 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11559 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11560 * FCP iocbs associated with virtual port.
11561 * This function returns number of iocbs it aborted .
11562 * This function is called with no locks held right after a taskmgmt
11563 * command is sent.
11564 **/
11565int
11566lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11567 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11568{
11569 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -080011570 struct lpfc_io_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011571 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011572 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011573 struct lpfc_iocbq *iocbq;
11574 IOCB_t *icmd;
11575 int sum, i, ret_val;
11576 unsigned long iflags;
James Smartc2017262019-01-28 11:14:37 -080011577 struct lpfc_sli_ring *pring_s4 = NULL;
James Smart98912dda2014-04-04 13:52:31 -040011578
James Smart59c68ea2018-04-09 14:24:25 -070011579 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011580
11581 /* all I/Os are in process of being flushed */
11582 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart59c68ea2018-04-09 14:24:25 -070011583 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011584 return 0;
11585 }
11586 sum = 0;
11587
11588 for (i = 1; i <= phba->sli.last_iotag; i++) {
11589 iocbq = phba->sli.iocbq_lookup[i];
11590
11591 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11592 cmd) != 0)
11593 continue;
11594
James Smartc2017262019-01-28 11:14:37 -080011595 /* Guard against IO completion being called at same time */
11596 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
11597 spin_lock(&lpfc_cmd->buf_lock);
11598
11599 if (!lpfc_cmd->pCmd) {
11600 spin_unlock(&lpfc_cmd->buf_lock);
11601 continue;
11602 }
11603
11604 if (phba->sli_rev == LPFC_SLI_REV4) {
11605 pring_s4 =
11606 phba->sli4_hba.hdwq[iocbq->hba_wqidx].fcp_wq->pring;
11607 if (!pring_s4) {
11608 spin_unlock(&lpfc_cmd->buf_lock);
11609 continue;
11610 }
11611 /* Note: both hbalock and ring_lock must be set here */
11612 spin_lock(&pring_s4->ring_lock);
11613 }
11614
James Smart98912dda2014-04-04 13:52:31 -040011615 /*
11616 * If the iocbq is already being aborted, don't take a second
11617 * action, but do count it.
11618 */
James Smartc2017262019-01-28 11:14:37 -080011619 if ((iocbq->iocb_flag & LPFC_DRIVER_ABORTED) ||
11620 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
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 /* issue ABTS for this IOCB based on iotag */
11628 abtsiocbq = __lpfc_sli_get_iocbq(phba);
James Smartc2017262019-01-28 11:14:37 -080011629 if (!abtsiocbq) {
11630 if (phba->sli_rev == LPFC_SLI_REV4)
11631 spin_unlock(&pring_s4->ring_lock);
11632 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011633 continue;
James Smartc2017262019-01-28 11:14:37 -080011634 }
James Smart98912dda2014-04-04 13:52:31 -040011635
11636 icmd = &iocbq->iocb;
11637 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11638 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11639 if (phba->sli_rev == LPFC_SLI_REV4)
11640 abtsiocbq->iocb.un.acxri.abortIoTag =
11641 iocbq->sli4_xritag;
11642 else
11643 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11644 abtsiocbq->iocb.ulpLe = 1;
11645 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11646 abtsiocbq->vport = vport;
11647
11648 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011649 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011650 if (iocbq->iocb_flag & LPFC_IO_FCP)
11651 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011652 if (iocbq->iocb_flag & LPFC_IO_FOF)
11653 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011654
James Smart8c50d252014-09-03 12:58:16 -040011655 ndlp = lpfc_cmd->rdata->pnode;
11656
11657 if (lpfc_is_link_up(phba) &&
11658 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011659 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11660 else
11661 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11662
11663 /* Setup callback routine and issue the command. */
11664 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11665
11666 /*
11667 * Indicate the IO is being aborted by the driver and set
11668 * the caller's flag into the aborted IO.
11669 */
11670 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11671
11672 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart98912dda2014-04-04 13:52:31 -040011673 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11674 abtsiocbq, 0);
James Smart59c68ea2018-04-09 14:24:25 -070011675 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011676 } else {
11677 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11678 abtsiocbq, 0);
11679 }
11680
James Smartc2017262019-01-28 11:14:37 -080011681 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011682
11683 if (ret_val == IOCB_ERROR)
11684 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11685 else
11686 sum++;
11687 }
James Smart59c68ea2018-04-09 14:24:25 -070011688 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011689 return sum;
11690}
11691
11692/**
James Smart3621a712009-04-06 18:47:14 -040011693 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011694 * @phba: Pointer to HBA context object.
11695 * @cmdiocbq: Pointer to command iocb.
11696 * @rspiocbq: Pointer to response iocb.
11697 *
11698 * This function is the completion handler for iocbs issued using
11699 * lpfc_sli_issue_iocb_wait function. This function is called by the
11700 * ring event handler function without any lock held. This function
11701 * can be called from both worker thread context and interrupt
11702 * context. This function also can be called from other thread which
11703 * cleans up the SLI layer objects.
11704 * This function copy the contents of the response iocb to the
11705 * response iocb memory object provided by the caller of
11706 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11707 * sleeps for the iocb completion.
11708 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011709static void
11710lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11711 struct lpfc_iocbq *cmdiocbq,
11712 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011713{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011714 wait_queue_head_t *pdone_q;
11715 unsigned long iflags;
James Smartc4908502019-01-28 11:14:28 -080011716 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011717
James Smart2e0fef82007-06-17 19:56:36 -050011718 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011719 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11720
11721 /*
11722 * A time out has occurred for the iocb. If a time out
11723 * completion handler has been supplied, call it. Otherwise,
11724 * just free the iocbq.
11725 */
11726
11727 spin_unlock_irqrestore(&phba->hbalock, iflags);
11728 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11729 cmdiocbq->wait_iocb_cmpl = NULL;
11730 if (cmdiocbq->iocb_cmpl)
11731 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11732 else
11733 lpfc_sli_release_iocbq(phba, cmdiocbq);
11734 return;
11735 }
11736
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011737 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11738 if (cmdiocbq->context2 && rspiocbq)
11739 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11740 &rspiocbq->iocb, sizeof(IOCB_t));
11741
James Smart0f65ff62010-02-26 14:14:23 -050011742 /* Set the exchange busy flag for task management commands */
11743 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11744 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smartc4908502019-01-28 11:14:28 -080011745 lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
James Smart0f65ff62010-02-26 14:14:23 -050011746 cur_iocbq);
11747 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11748 }
11749
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011750 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011751 if (pdone_q)
11752 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011753 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011754 return;
11755}
11756
James Smarte59058c2008-08-24 21:49:00 -040011757/**
James Smartd11e31d2009-06-10 17:23:06 -040011758 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11759 * @phba: Pointer to HBA context object..
11760 * @piocbq: Pointer to command iocb.
11761 * @flag: Flag to test.
11762 *
11763 * This routine grabs the hbalock and then test the iocb_flag to
11764 * see if the passed in flag is set.
11765 * Returns:
11766 * 1 if flag is set.
11767 * 0 if flag is not set.
11768 **/
11769static int
11770lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11771 struct lpfc_iocbq *piocbq, uint32_t flag)
11772{
11773 unsigned long iflags;
11774 int ret;
11775
11776 spin_lock_irqsave(&phba->hbalock, iflags);
11777 ret = piocbq->iocb_flag & flag;
11778 spin_unlock_irqrestore(&phba->hbalock, iflags);
11779 return ret;
11780
11781}
11782
11783/**
James Smart3621a712009-04-06 18:47:14 -040011784 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011785 * @phba: Pointer to HBA context object..
11786 * @pring: Pointer to sli ring.
11787 * @piocb: Pointer to command iocb.
11788 * @prspiocbq: Pointer to response iocb.
11789 * @timeout: Timeout in number of seconds.
11790 *
11791 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011792 * iocb to complete. The iocb_cmpl field of the shall be used
11793 * to handle iocbs which time out. If the field is NULL, the
11794 * function shall free the iocbq structure. If more clean up is
11795 * needed, the caller is expected to provide a completion function
11796 * that will provide the needed clean up. If the iocb command is
11797 * not completed within timeout seconds, the function will either
11798 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11799 * completion function set in the iocb_cmpl field and then return
11800 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11801 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011802 * The function waits for the iocb completion using an
11803 * non-interruptible wait.
11804 * This function will sleep while waiting for iocb completion.
11805 * So, this function should not be called from any context which
11806 * does not allow sleeping. Due to the same reason, this function
11807 * cannot be called with interrupt disabled.
11808 * This function assumes that the iocb completions occur while
11809 * this function sleep. So, this function cannot be called from
11810 * the thread which process iocb completion for this ring.
11811 * This function clears the iocb_flag of the iocb object before
11812 * issuing the iocb and the iocb completion handler sets this
11813 * flag and wakes this thread when the iocb completes.
11814 * The contents of the response iocb will be copied to prspiocbq
11815 * by the completion handler when the command completes.
11816 * This function returns IOCB_SUCCESS when success.
11817 * This function is called with no lock held.
11818 **/
dea31012005-04-17 16:05:31 -050011819int
James Smart2e0fef82007-06-17 19:56:36 -050011820lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011821 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011822 struct lpfc_iocbq *piocb,
11823 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011824 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011825{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011826 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011827 long timeleft, timeout_req = 0;
11828 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011829 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011830 struct lpfc_iocbq *iocb;
11831 int txq_cnt = 0;
11832 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011833 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011834 unsigned long iflags;
11835 bool iocb_completed = true;
11836
James Smart895427b2017-02-12 13:52:30 -080011837 if (phba->sli_rev >= LPFC_SLI_REV4)
11838 pring = lpfc_sli4_calc_ring(phba, piocb);
11839 else
11840 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011841 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011842 * If the caller has provided a response iocbq buffer, then context2
11843 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011844 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011845 if (prspiocbq) {
11846 if (piocb->context2)
11847 return IOCB_ERROR;
11848 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011849 }
11850
James Smart5a0916b2013-07-15 18:31:42 -040011851 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011852 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11853 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011854 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011855
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011856 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011857 if (lpfc_readl(phba->HCregaddr, &creg_val))
11858 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011859 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11860 writel(creg_val, phba->HCregaddr);
11861 readl(phba->HCregaddr); /* flush */
11862 }
11863
James Smart2a9bf3d2010-06-07 15:24:45 -040011864 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11865 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011866 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011867 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011868 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011869 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011870 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011871 spin_lock_irqsave(&phba->hbalock, iflags);
11872 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011873
James Smart5a0916b2013-07-15 18:31:42 -040011874 /*
11875 * IOCB timed out. Inform the wake iocb wait
11876 * completion function and set local status
11877 */
11878
11879 iocb_completed = false;
11880 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11881 }
11882 spin_unlock_irqrestore(&phba->hbalock, iflags);
11883 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011884 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011885 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011886 /* Note: we are not indicating if the IOCB has a success
11887 * status or not - that's for the caller to check.
11888 * IOCB_SUCCESS means just that the command was sent and
11889 * completed. Not that it completed successfully.
11890 * */
James Smart7054a602007-04-25 09:52:34 -040011891 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011892 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011893 "0338 IOCB wait timeout error - no "
11894 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011895 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011896 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011897 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011898 "0330 IOCB wake NOT set, "
11899 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011900 timeout, (timeleft / jiffies));
11901 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011902 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011903 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011904 if (phba->cfg_log_verbose & LOG_SLI) {
11905 list_for_each_entry(iocb, &pring->txq, list) {
11906 txq_cnt++;
11907 }
11908 list_for_each_entry(iocb, &pring->txcmplq, list) {
11909 txcmplq_cnt++;
11910 }
11911 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11912 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11913 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11914 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011915 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011916 } else {
11917 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011918 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011919 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011920 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011921 }
11922
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011923 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011924 if (lpfc_readl(phba->HCregaddr, &creg_val))
11925 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011926 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11927 writel(creg_val, phba->HCregaddr);
11928 readl(phba->HCregaddr); /* flush */
11929 }
11930
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011931 if (prspiocbq)
11932 piocb->context2 = NULL;
11933
11934 piocb->context_un.wait_queue = NULL;
11935 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011936 return retval;
11937}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011938
James Smarte59058c2008-08-24 21:49:00 -040011939/**
James Smart3621a712009-04-06 18:47:14 -040011940 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011941 * @phba: Pointer to HBA context object.
11942 * @pmboxq: Pointer to driver mailbox object.
11943 * @timeout: Timeout in number of seconds.
11944 *
11945 * This function issues the mailbox to firmware and waits for the
11946 * mailbox command to complete. If the mailbox command is not
11947 * completed within timeout seconds, it returns MBX_TIMEOUT.
11948 * The function waits for the mailbox completion using an
11949 * interruptible wait. If the thread is woken up due to a
11950 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11951 * should not free the mailbox resources, if this function returns
11952 * MBX_TIMEOUT.
11953 * This function will sleep while waiting for mailbox completion.
11954 * So, this function should not be called from any context which
11955 * does not allow sleeping. Due to the same reason, this function
11956 * cannot be called with interrupt disabled.
11957 * This function assumes that the mailbox completion occurs while
11958 * this function sleep. So, this function cannot be called from
11959 * the worker thread which processes mailbox completion.
11960 * This function is called in the context of HBA management
11961 * applications.
11962 * This function returns MBX_SUCCESS when successful.
11963 * This function is called with no lock held.
11964 **/
dea31012005-04-17 16:05:31 -050011965int
James Smart2e0fef82007-06-17 19:56:36 -050011966lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011967 uint32_t timeout)
11968{
James Smarte29d74f2018-03-05 12:04:07 -080011969 struct completion mbox_done;
dea31012005-04-17 16:05:31 -050011970 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011971 unsigned long flag;
dea31012005-04-17 16:05:31 -050011972
James Smart495a7142008-06-14 22:52:59 -040011973 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011974 /* setup wake call as IOCB callback */
11975 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea31012005-04-17 16:05:31 -050011976
James Smarte29d74f2018-03-05 12:04:07 -080011977 /* setup context3 field to pass wait_queue pointer to wake function */
11978 init_completion(&mbox_done);
11979 pmboxq->context3 = &mbox_done;
dea31012005-04-17 16:05:31 -050011980 /* now issue the command */
11981 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011982 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smarte29d74f2018-03-05 12:04:07 -080011983 wait_for_completion_timeout(&mbox_done,
11984 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011985
James Smart858c9f62007-06-17 19:56:39 -050011986 spin_lock_irqsave(&phba->hbalock, flag);
James Smarte29d74f2018-03-05 12:04:07 -080011987 pmboxq->context3 = NULL;
James Smart7054a602007-04-25 09:52:34 -040011988 /*
11989 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11990 * else do not free the resources.
11991 */
James Smartd7c47992010-06-08 18:31:54 -040011992 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011993 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011994 } else {
James Smart7054a602007-04-25 09:52:34 -040011995 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011996 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11997 }
11998 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050011999 }
dea31012005-04-17 16:05:31 -050012000 return retval;
12001}
12002
James Smarte59058c2008-08-24 21:49:00 -040012003/**
James Smart3772a992009-05-22 14:50:54 -040012004 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040012005 * @phba: Pointer to HBA context.
12006 *
James Smart3772a992009-05-22 14:50:54 -040012007 * This function is called to shutdown the driver's mailbox sub-system.
12008 * It first marks the mailbox sub-system is in a block state to prevent
12009 * the asynchronous mailbox command from issued off the pending mailbox
12010 * command queue. If the mailbox command sub-system shutdown is due to
12011 * HBA error conditions such as EEH or ERATT, this routine shall invoke
12012 * the mailbox sub-system flush routine to forcefully bring down the
12013 * mailbox sub-system. Otherwise, if it is due to normal condition (such
12014 * as with offline or HBA function reset), this routine will wait for the
12015 * outstanding mailbox command to complete before invoking the mailbox
12016 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040012017 **/
James Smart3772a992009-05-22 14:50:54 -040012018void
James Smart618a5232012-06-12 13:54:36 -040012019lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040012020{
James Smart3772a992009-05-22 14:50:54 -040012021 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040012022 unsigned long timeout;
12023
James Smart618a5232012-06-12 13:54:36 -040012024 if (mbx_action == LPFC_MBX_NO_WAIT) {
12025 /* delay 100ms for port state */
12026 msleep(100);
12027 lpfc_sli_mbox_sys_flush(phba);
12028 return;
12029 }
James Smarta183a152011-10-10 21:32:43 -040012030 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050012031
James Smart523128e2018-09-10 10:30:46 -070012032 /* Disable softirqs, including timers from obtaining phba->hbalock */
12033 local_bh_disable();
12034
James Smart3772a992009-05-22 14:50:54 -040012035 spin_lock_irq(&phba->hbalock);
12036 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040012037
12038 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040012039 /* Determine how long we might wait for the active mailbox
12040 * command to be gracefully completed by firmware.
12041 */
James Smarta183a152011-10-10 21:32:43 -040012042 if (phba->sli.mbox_active)
12043 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
12044 phba->sli.mbox_active) *
12045 1000) + jiffies;
12046 spin_unlock_irq(&phba->hbalock);
12047
James Smart523128e2018-09-10 10:30:46 -070012048 /* Enable softirqs again, done with phba->hbalock */
12049 local_bh_enable();
12050
James Smart3772a992009-05-22 14:50:54 -040012051 while (phba->sli.mbox_active) {
12052 /* Check active mailbox complete status every 2ms */
12053 msleep(2);
12054 if (time_after(jiffies, timeout))
12055 /* Timeout, let the mailbox flush routine to
12056 * forcefully release active mailbox command
12057 */
12058 break;
12059 }
James Smart523128e2018-09-10 10:30:46 -070012060 } else {
James Smartd7069f02012-03-01 22:36:29 -050012061 spin_unlock_irq(&phba->hbalock);
12062
James Smart523128e2018-09-10 10:30:46 -070012063 /* Enable softirqs again, done with phba->hbalock */
12064 local_bh_enable();
12065 }
12066
James Smart3772a992009-05-22 14:50:54 -040012067 lpfc_sli_mbox_sys_flush(phba);
12068}
12069
12070/**
12071 * lpfc_sli_eratt_read - read sli-3 error attention events
12072 * @phba: Pointer to HBA context.
12073 *
12074 * This function is called to read the SLI3 device error attention registers
12075 * for possible error attention events. The caller must hold the hostlock
12076 * with spin_lock_irq().
12077 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012078 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040012079 * Register and returns 0 otherwise.
12080 **/
12081static int
12082lpfc_sli_eratt_read(struct lpfc_hba *phba)
12083{
James Smarted957682007-06-17 19:56:37 -050012084 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040012085
James Smart3772a992009-05-22 14:50:54 -040012086 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050012087 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12088 goto unplug_err;
12089
James Smart3772a992009-05-22 14:50:54 -040012090 if (ha_copy & HA_ERATT) {
12091 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050012092 if (lpfc_sli_read_hs(phba))
12093 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040012094
James Smart3772a992009-05-22 14:50:54 -040012095 /* Check if there is a deferred error condition is active */
12096 if ((HS_FFER1 & phba->work_hs) &&
12097 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012098 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040012099 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040012100 /* Clear all interrupt enable conditions */
12101 writel(0, phba->HCregaddr);
12102 readl(phba->HCregaddr);
12103 }
12104
12105 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040012106 phba->work_ha |= HA_ERATT;
12107 /* Indicate polling handles this ERATT */
12108 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012109 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012110 }
James Smart3772a992009-05-22 14:50:54 -040012111 return 0;
James Smart9940b972011-03-11 16:06:12 -050012112
12113unplug_err:
12114 /* Set the driver HS work bitmap */
12115 phba->work_hs |= UNPLUG_ERR;
12116 /* Set the driver HA work bitmap */
12117 phba->work_ha |= HA_ERATT;
12118 /* Indicate polling handles this ERATT */
12119 phba->hba_flag |= HBA_ERATT_HANDLED;
12120 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012121}
12122
James Smarte59058c2008-08-24 21:49:00 -040012123/**
James Smartda0436e2009-05-22 14:51:39 -040012124 * lpfc_sli4_eratt_read - read sli-4 error attention events
12125 * @phba: Pointer to HBA context.
12126 *
12127 * This function is called to read the SLI4 device error attention registers
12128 * for possible error attention events. The caller must hold the hostlock
12129 * with spin_lock_irq().
12130 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012131 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040012132 * Register and returns 0 otherwise.
12133 **/
12134static int
12135lpfc_sli4_eratt_read(struct lpfc_hba *phba)
12136{
12137 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050012138 uint32_t if_type, portsmphr;
12139 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040012140
James Smart2fcee4b2010-12-15 17:57:46 -050012141 /*
12142 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040012143 * registers for error attention. This can be changed later.
12144 */
James Smart2fcee4b2010-12-15 17:57:46 -050012145 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
12146 switch (if_type) {
12147 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050012148 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
12149 &uerr_sta_lo) ||
12150 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
12151 &uerr_sta_hi)) {
12152 phba->work_hs |= UNPLUG_ERR;
12153 phba->work_ha |= HA_ERATT;
12154 phba->hba_flag |= HBA_ERATT_HANDLED;
12155 return 1;
12156 }
James Smart2fcee4b2010-12-15 17:57:46 -050012157 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
12158 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
12159 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12160 "1423 HBA Unrecoverable error: "
12161 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
12162 "ue_mask_lo_reg=0x%x, "
12163 "ue_mask_hi_reg=0x%x\n",
12164 uerr_sta_lo, uerr_sta_hi,
12165 phba->sli4_hba.ue_mask_lo,
12166 phba->sli4_hba.ue_mask_hi);
12167 phba->work_status[0] = uerr_sta_lo;
12168 phba->work_status[1] = uerr_sta_hi;
12169 phba->work_ha |= HA_ERATT;
12170 phba->hba_flag |= HBA_ERATT_HANDLED;
12171 return 1;
12172 }
12173 break;
12174 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080012175 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050012176 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
12177 &portstat_reg.word0) ||
12178 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
12179 &portsmphr)){
12180 phba->work_hs |= UNPLUG_ERR;
12181 phba->work_ha |= HA_ERATT;
12182 phba->hba_flag |= HBA_ERATT_HANDLED;
12183 return 1;
12184 }
James Smart2fcee4b2010-12-15 17:57:46 -050012185 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
12186 phba->work_status[0] =
12187 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
12188 phba->work_status[1] =
12189 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
12190 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050012191 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050012192 "port status reg 0x%x, "
12193 "port smphr reg 0x%x, "
12194 "error 1=0x%x, error 2=0x%x\n",
12195 portstat_reg.word0,
12196 portsmphr,
12197 phba->work_status[0],
12198 phba->work_status[1]);
12199 phba->work_ha |= HA_ERATT;
12200 phba->hba_flag |= HBA_ERATT_HANDLED;
12201 return 1;
12202 }
12203 break;
12204 case LPFC_SLI_INTF_IF_TYPE_1:
12205 default:
James Smarta747c9c2009-11-18 15:41:10 -050012206 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050012207 "2886 HBA Error Attention on unsupported "
12208 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050012209 return 1;
James Smartda0436e2009-05-22 14:51:39 -040012210 }
James Smart2fcee4b2010-12-15 17:57:46 -050012211
James Smartda0436e2009-05-22 14:51:39 -040012212 return 0;
12213}
12214
12215/**
James Smart3621a712009-04-06 18:47:14 -040012216 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040012217 * @phba: Pointer to HBA context.
12218 *
James Smart3772a992009-05-22 14:50:54 -040012219 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040012220 * error attention register bit for error attention events.
12221 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012222 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040012223 * Register and returns 0 otherwise.
12224 **/
12225int
12226lpfc_sli_check_eratt(struct lpfc_hba *phba)
12227{
12228 uint32_t ha_copy;
12229
12230 /* If somebody is waiting to handle an eratt, don't process it
12231 * here. The brdkill function will do this.
12232 */
12233 if (phba->link_flag & LS_IGNORE_ERATT)
12234 return 0;
12235
12236 /* Check if interrupt handler handles this ERATT */
12237 spin_lock_irq(&phba->hbalock);
12238 if (phba->hba_flag & HBA_ERATT_HANDLED) {
12239 /* Interrupt handler has handled ERATT */
12240 spin_unlock_irq(&phba->hbalock);
12241 return 0;
12242 }
12243
James Smarta257bf92009-04-06 18:48:10 -040012244 /*
12245 * If there is deferred error attention, do not check for error
12246 * attention
12247 */
12248 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12249 spin_unlock_irq(&phba->hbalock);
12250 return 0;
12251 }
12252
James Smart3772a992009-05-22 14:50:54 -040012253 /* If PCI channel is offline, don't process it */
12254 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040012255 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012256 return 0;
12257 }
12258
12259 switch (phba->sli_rev) {
12260 case LPFC_SLI_REV2:
12261 case LPFC_SLI_REV3:
12262 /* Read chip Host Attention (HA) register */
12263 ha_copy = lpfc_sli_eratt_read(phba);
12264 break;
James Smartda0436e2009-05-22 14:51:39 -040012265 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050012266 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040012267 ha_copy = lpfc_sli4_eratt_read(phba);
12268 break;
James Smart3772a992009-05-22 14:50:54 -040012269 default:
12270 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12271 "0299 Invalid SLI revision (%d)\n",
12272 phba->sli_rev);
12273 ha_copy = 0;
12274 break;
James Smart93996272008-08-24 21:50:30 -040012275 }
12276 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012277
12278 return ha_copy;
12279}
12280
12281/**
12282 * lpfc_intr_state_check - Check device state for interrupt handling
12283 * @phba: Pointer to HBA context.
12284 *
12285 * This inline routine checks whether a device or its PCI slot is in a state
12286 * that the interrupt should be handled.
12287 *
12288 * This function returns 0 if the device or the PCI slot is in a state that
12289 * interrupt should be handled, otherwise -EIO.
12290 */
12291static inline int
12292lpfc_intr_state_check(struct lpfc_hba *phba)
12293{
12294 /* If the pci channel is offline, ignore all the interrupts */
12295 if (unlikely(pci_channel_offline(phba->pcidev)))
12296 return -EIO;
12297
12298 /* Update device level interrupt statistics */
12299 phba->sli.slistat.sli_intr++;
12300
12301 /* Ignore all interrupts during initialization. */
12302 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12303 return -EIO;
12304
James Smart93996272008-08-24 21:50:30 -040012305 return 0;
12306}
12307
12308/**
James Smart3772a992009-05-22 14:50:54 -040012309 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040012310 * @irq: Interrupt number.
12311 * @dev_id: The device context pointer.
12312 *
James Smart93996272008-08-24 21:50:30 -040012313 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012314 * service routine when device with SLI-3 interface spec is enabled with
12315 * MSI-X multi-message interrupt mode and there are slow-path events in
12316 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12317 * interrupt mode, this function is called as part of the device-level
12318 * interrupt handler. When the PCI slot is in error recovery or the HBA
12319 * is undergoing initialization, the interrupt handler will not process
12320 * the interrupt. The link attention and ELS ring attention events are
12321 * handled by the worker thread. The interrupt handler signals the worker
12322 * thread and returns for these events. This function is called without
12323 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040012324 * structures.
12325 *
12326 * This function returns IRQ_HANDLED when interrupt is handled else it
12327 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040012328 **/
dea31012005-04-17 16:05:31 -050012329irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012330lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050012331{
James Smart2e0fef82007-06-17 19:56:36 -050012332 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050012333 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050012334 uint32_t work_ha_copy;
12335 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012336 unsigned long iflag;
dea31012005-04-17 16:05:31 -050012337 uint32_t control;
12338
James Smart92d7f7b2007-06-17 19:56:38 -050012339 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050012340 struct lpfc_vport *vport;
12341 struct lpfc_nodelist *ndlp;
12342 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050012343 LPFC_MBOXQ_t *pmb;
12344 int rc;
12345
dea31012005-04-17 16:05:31 -050012346 /*
12347 * Get the driver's phba structure from the dev_id and
12348 * assume the HBA is not interrupting.
12349 */
James Smart93996272008-08-24 21:50:30 -040012350 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050012351
12352 if (unlikely(!phba))
12353 return IRQ_NONE;
12354
dea31012005-04-17 16:05:31 -050012355 /*
James Smart93996272008-08-24 21:50:30 -040012356 * Stuff needs to be attented to when this function is invoked as an
12357 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012358 */
James Smart93996272008-08-24 21:50:30 -040012359 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012360 /* Check device state for handling interrupt */
12361 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012362 return IRQ_NONE;
12363 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050012364 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012365 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12366 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040012367 /* If somebody is waiting to handle an eratt don't process it
12368 * here. The brdkill function will do this.
12369 */
12370 if (phba->link_flag & LS_IGNORE_ERATT)
12371 ha_copy &= ~HA_ERATT;
12372 /* Check the need for handling ERATT in interrupt handler */
12373 if (ha_copy & HA_ERATT) {
12374 if (phba->hba_flag & HBA_ERATT_HANDLED)
12375 /* ERATT polling has handled ERATT */
12376 ha_copy &= ~HA_ERATT;
12377 else
12378 /* Indicate interrupt handler handles ERATT */
12379 phba->hba_flag |= HBA_ERATT_HANDLED;
12380 }
James Smarta257bf92009-04-06 18:48:10 -040012381
12382 /*
12383 * If there is deferred error attention, do not check for any
12384 * interrupt.
12385 */
12386 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012387 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012388 return IRQ_NONE;
12389 }
12390
James Smart93996272008-08-24 21:50:30 -040012391 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050012392 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12393 goto unplug_error;
12394
James Smarta747c9c2009-11-18 15:41:10 -050012395 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12396 HC_LAINT_ENA | HC_ERINT_ENA),
12397 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012398 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12399 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012400 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012401 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012402 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012403 } else
12404 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050012405
dea31012005-04-17 16:05:31 -050012406 work_ha_copy = ha_copy & phba->work_ha_mask;
12407
James Smart93996272008-08-24 21:50:30 -040012408 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050012409 if (work_ha_copy & HA_LATT) {
12410 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12411 /*
12412 * Turn off Link Attention interrupts
12413 * until CLEAR_LA done
12414 */
James Smart5b75da22008-12-04 22:39:35 -050012415 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012416 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050012417 if (lpfc_readl(phba->HCregaddr, &control))
12418 goto unplug_error;
dea31012005-04-17 16:05:31 -050012419 control &= ~HC_LAINT_ENA;
12420 writel(control, phba->HCregaddr);
12421 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012422 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012423 }
12424 else
12425 work_ha_copy &= ~HA_LATT;
12426 }
12427
James Smart93996272008-08-24 21:50:30 -040012428 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050012429 /*
12430 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12431 * the only slow ring.
12432 */
12433 status = (work_ha_copy &
12434 (HA_RXMASK << (4*LPFC_ELS_RING)));
12435 status >>= (4*LPFC_ELS_RING);
12436 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050012437 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012438 if (lpfc_readl(phba->HCregaddr, &control))
12439 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040012440
12441 lpfc_debugfs_slow_ring_trc(phba,
12442 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12443 control, status,
12444 (uint32_t)phba->sli.slistat.sli_intr);
12445
James Smart858c9f62007-06-17 19:56:39 -050012446 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040012447 lpfc_debugfs_slow_ring_trc(phba,
12448 "ISR Disable ring:"
12449 "pwork:x%x hawork:x%x wait:x%x",
12450 phba->work_ha, work_ha_copy,
12451 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012452 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012453
James Smart858c9f62007-06-17 19:56:39 -050012454 control &=
12455 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050012456 writel(control, phba->HCregaddr);
12457 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050012458 }
James Smarta58cbd52007-08-02 11:09:43 -040012459 else {
12460 lpfc_debugfs_slow_ring_trc(phba,
12461 "ISR slow ring: pwork:"
12462 "x%x hawork:x%x wait:x%x",
12463 phba->work_ha, work_ha_copy,
12464 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012465 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012466 }
James Smart5b75da22008-12-04 22:39:35 -050012467 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012468 }
12469 }
James Smart5b75da22008-12-04 22:39:35 -050012470 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012471 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050012472 if (lpfc_sli_read_hs(phba))
12473 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040012474 /*
12475 * Check if there is a deferred error condition
12476 * is active
12477 */
12478 if ((HS_FFER1 & phba->work_hs) &&
12479 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012480 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12481 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040012482 phba->hba_flag |= DEFER_ERATT;
12483 /* Clear all interrupt enable conditions */
12484 writel(0, phba->HCregaddr);
12485 readl(phba->HCregaddr);
12486 }
12487 }
12488
James Smart93996272008-08-24 21:50:30 -040012489 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050012490 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040012491 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040012492 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050012493 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050012494
12495 /* First check out the status word */
12496 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12497 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050012498 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050012499 /*
12500 * Stray Mailbox Interrupt, mbxCommand <cmd>
12501 * mbxStatus <status>
12502 */
James Smart09372822008-01-11 01:52:54 -050012503 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012504 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012505 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012506 "Interrupt mbxCommand x%x "
12507 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012508 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012509 pmbox->mbxCommand,
12510 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012511 /* clear mailbox attention bit */
12512 work_ha_copy &= ~HA_MBATT;
12513 } else {
James Smart97eab632008-04-07 10:16:05 -040012514 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012515 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012516 phba->last_completion_time = jiffies;
12517 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012518 if (pmb->mbox_cmpl) {
12519 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12520 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012521 if (pmb->out_ext_byte_len &&
James Smart3e1f0712018-11-29 16:09:29 -080012522 pmb->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -040012523 lpfc_sli_pcimem_bcopy(
12524 phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -080012525 pmb->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -040012526 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012527 }
James Smart09372822008-01-11 01:52:54 -050012528 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12529 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12530
12531 lpfc_debugfs_disc_trc(vport,
12532 LPFC_DISC_TRC_MBOX_VPORT,
12533 "MBOX dflt rpi: : "
12534 "status:x%x rpi:x%x",
12535 (uint32_t)pmbox->mbxStatus,
12536 pmbox->un.varWords[0], 0);
12537
12538 if (!pmbox->mbxStatus) {
12539 mp = (struct lpfc_dmabuf *)
James Smart3e1f0712018-11-29 16:09:29 -080012540 (pmb->ctx_buf);
James Smart09372822008-01-11 01:52:54 -050012541 ndlp = (struct lpfc_nodelist *)
James Smart3e1f0712018-11-29 16:09:29 -080012542 pmb->ctx_ndlp;
James Smart09372822008-01-11 01:52:54 -050012543
12544 /* Reg_LOGIN of dflt RPI was
12545 * successful. new lets get
12546 * rid of the RPI using the
12547 * same mbox buffer.
12548 */
12549 lpfc_unreg_login(phba,
12550 vport->vpi,
12551 pmbox->un.varWords[0],
12552 pmb);
12553 pmb->mbox_cmpl =
12554 lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080012555 pmb->ctx_buf = mp;
12556 pmb->ctx_ndlp = ndlp;
James Smart09372822008-01-11 01:52:54 -050012557 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012558 rc = lpfc_sli_issue_mbox(phba,
12559 pmb,
12560 MBX_NOWAIT);
12561 if (rc != MBX_BUSY)
12562 lpfc_printf_log(phba,
12563 KERN_ERR,
12564 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012565 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012566 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012567 if (rc != MBX_NOT_FINISHED)
12568 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012569 }
12570 }
James Smart5b75da22008-12-04 22:39:35 -050012571 spin_lock_irqsave(
12572 &phba->pport->work_port_lock,
12573 iflag);
James Smart09372822008-01-11 01:52:54 -050012574 phba->pport->work_port_events &=
12575 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012576 spin_unlock_irqrestore(
12577 &phba->pport->work_port_lock,
12578 iflag);
James Smart09372822008-01-11 01:52:54 -050012579 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012580 }
James Smart97eab632008-04-07 10:16:05 -040012581 } else
James Smart5b75da22008-12-04 22:39:35 -050012582 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012583
James Smart92d7f7b2007-06-17 19:56:38 -050012584 if ((work_ha_copy & HA_MBATT) &&
12585 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012586send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012587 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012588 do {
12589 rc = lpfc_sli_issue_mbox(phba, NULL,
12590 MBX_NOWAIT);
12591 } while (rc == MBX_NOT_FINISHED);
12592 if (rc != MBX_SUCCESS)
12593 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12594 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012595 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012596 }
12597
James Smart5b75da22008-12-04 22:39:35 -050012598 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012599 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012600 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012601 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012602 }
James Smart93996272008-08-24 21:50:30 -040012603 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012604unplug_error:
12605 spin_unlock_irqrestore(&phba->hbalock, iflag);
12606 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012607
James Smart3772a992009-05-22 14:50:54 -040012608} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012609
12610/**
James Smart3772a992009-05-22 14:50:54 -040012611 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012612 * @irq: Interrupt number.
12613 * @dev_id: The device context pointer.
12614 *
12615 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012616 * service routine when device with SLI-3 interface spec is enabled with
12617 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12618 * ring event in the HBA. However, when the device is enabled with either
12619 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12620 * device-level interrupt handler. When the PCI slot is in error recovery
12621 * or the HBA is undergoing initialization, the interrupt handler will not
12622 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12623 * the intrrupt context. This function is called without any lock held.
12624 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012625 *
12626 * This function returns IRQ_HANDLED when interrupt is handled else it
12627 * returns IRQ_NONE.
12628 **/
12629irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012630lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012631{
12632 struct lpfc_hba *phba;
12633 uint32_t ha_copy;
12634 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012635 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012636 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012637
12638 /* Get the driver's phba structure from the dev_id and
12639 * assume the HBA is not interrupting.
12640 */
12641 phba = (struct lpfc_hba *) dev_id;
12642
12643 if (unlikely(!phba))
12644 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012645
12646 /*
James Smart93996272008-08-24 21:50:30 -040012647 * Stuff needs to be attented to when this function is invoked as an
12648 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012649 */
James Smart93996272008-08-24 21:50:30 -040012650 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012651 /* Check device state for handling interrupt */
12652 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012653 return IRQ_NONE;
12654 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012655 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12656 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012657 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012658 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012659 /*
12660 * If there is deferred error attention, do not check for
12661 * any interrupt.
12662 */
12663 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012664 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012665 return IRQ_NONE;
12666 }
James Smart93996272008-08-24 21:50:30 -040012667 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12668 phba->HAregaddr);
12669 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012670 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012671 } else
12672 ha_copy = phba->ha_copy;
12673
12674 /*
12675 * Process all events on FCP ring. Take the optimized path for FCP IO.
12676 */
12677 ha_copy &= ~(phba->work_ha_mask);
12678
12679 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012680 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012681 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012682 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012683 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012684
12685 if (phba->cfg_multi_ring_support == 2) {
12686 /*
James Smart93996272008-08-24 21:50:30 -040012687 * Process all events on extra ring. Take the optimized path
12688 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012689 */
James Smart93996272008-08-24 21:50:30 -040012690 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012691 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012692 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012693 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012694 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012695 status);
12696 }
12697 }
dea31012005-04-17 16:05:31 -050012698 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012699} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012700
James Smart93996272008-08-24 21:50:30 -040012701/**
James Smart3772a992009-05-22 14:50:54 -040012702 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012703 * @irq: Interrupt number.
12704 * @dev_id: The device context pointer.
12705 *
James Smart3772a992009-05-22 14:50:54 -040012706 * This function is the HBA device-level interrupt handler to device with
12707 * SLI-3 interface spec, called from the PCI layer when either MSI or
12708 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12709 * requires driver attention. This function invokes the slow-path interrupt
12710 * attention handling function and fast-path interrupt attention handling
12711 * function in turn to process the relevant HBA attention events. This
12712 * function is called without any lock held. It gets the hbalock to access
12713 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012714 *
12715 * This function returns IRQ_HANDLED when interrupt is handled, else it
12716 * returns IRQ_NONE.
12717 **/
12718irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012719lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012720{
12721 struct lpfc_hba *phba;
12722 irqreturn_t sp_irq_rc, fp_irq_rc;
12723 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012724 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012725
12726 /*
12727 * Get the driver's phba structure from the dev_id and
12728 * assume the HBA is not interrupting.
12729 */
12730 phba = (struct lpfc_hba *) dev_id;
12731
12732 if (unlikely(!phba))
12733 return IRQ_NONE;
12734
James Smart3772a992009-05-22 14:50:54 -040012735 /* Check device state for handling interrupt */
12736 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012737 return IRQ_NONE;
12738
12739 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012740 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12741 spin_unlock(&phba->hbalock);
12742 return IRQ_HANDLED;
12743 }
12744
James Smart93996272008-08-24 21:50:30 -040012745 if (unlikely(!phba->ha_copy)) {
12746 spin_unlock(&phba->hbalock);
12747 return IRQ_NONE;
12748 } else if (phba->ha_copy & HA_ERATT) {
12749 if (phba->hba_flag & HBA_ERATT_HANDLED)
12750 /* ERATT polling has handled ERATT */
12751 phba->ha_copy &= ~HA_ERATT;
12752 else
12753 /* Indicate interrupt handler handles ERATT */
12754 phba->hba_flag |= HBA_ERATT_HANDLED;
12755 }
12756
James Smarta257bf92009-04-06 18:48:10 -040012757 /*
12758 * If there is deferred error attention, do not check for any interrupt.
12759 */
12760 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012761 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012762 return IRQ_NONE;
12763 }
12764
James Smart93996272008-08-24 21:50:30 -040012765 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012766 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12767 spin_unlock(&phba->hbalock);
12768 return IRQ_HANDLED;
12769 }
James Smarta747c9c2009-11-18 15:41:10 -050012770 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12771 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12772 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012773 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012774 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012775 readl(phba->HAregaddr); /* flush */
12776 spin_unlock(&phba->hbalock);
12777
12778 /*
12779 * Invokes slow-path host attention interrupt handling as appropriate.
12780 */
12781
12782 /* status of events with mailbox and link attention */
12783 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12784
12785 /* status of events with ELS ring */
12786 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12787 status2 >>= (4*LPFC_ELS_RING);
12788
12789 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012790 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012791 else
12792 sp_irq_rc = IRQ_NONE;
12793
12794 /*
12795 * Invoke fast-path host attention interrupt handling as appropriate.
12796 */
12797
12798 /* status of events with FCP ring */
12799 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12800 status1 >>= (4*LPFC_FCP_RING);
12801
12802 /* status of events with extra ring */
12803 if (phba->cfg_multi_ring_support == 2) {
12804 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12805 status2 >>= (4*LPFC_EXTRA_RING);
12806 } else
12807 status2 = 0;
12808
12809 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012810 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012811 else
12812 fp_irq_rc = IRQ_NONE;
12813
12814 /* Return device-level interrupt handling status */
12815 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012816} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012817
12818/**
James Smart4f774512009-05-22 14:52:35 -040012819 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12820 * @phba: pointer to lpfc hba data structure.
12821 *
12822 * This routine is invoked by the worker thread to process all the pending
12823 * SLI4 els abort xri events.
12824 **/
12825void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12826{
12827 struct lpfc_cq_event *cq_event;
12828
12829 /* First, declare the els xri abort event has been handled */
12830 spin_lock_irq(&phba->hbalock);
12831 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12832 spin_unlock_irq(&phba->hbalock);
12833 /* Now, handle all the els xri abort events */
12834 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12835 /* Get the first event from the head of the event queue */
12836 spin_lock_irq(&phba->hbalock);
12837 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12838 cq_event, struct lpfc_cq_event, list);
12839 spin_unlock_irq(&phba->hbalock);
12840 /* Notify aborted XRI for ELS work queue */
12841 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12842 /* Free the event processed back to the free pool */
12843 lpfc_sli4_cq_event_release(phba, cq_event);
12844 }
12845}
12846
James Smart341af102010-01-26 23:07:37 -050012847/**
12848 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12849 * @phba: pointer to lpfc hba data structure
12850 * @pIocbIn: pointer to the rspiocbq
12851 * @pIocbOut: pointer to the cmdiocbq
12852 * @wcqe: pointer to the complete wcqe
12853 *
12854 * This routine transfers the fields of a command iocbq to a response iocbq
12855 * by copying all the IOCB fields from command iocbq and transferring the
12856 * completion status information from the complete wcqe.
12857 **/
James Smart4f774512009-05-22 14:52:35 -040012858static void
James Smart341af102010-01-26 23:07:37 -050012859lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12860 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012861 struct lpfc_iocbq *pIocbOut,
12862 struct lpfc_wcqe_complete *wcqe)
12863{
James Smartaf227412013-10-10 12:23:10 -040012864 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012865 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012866 uint32_t status, max_response;
12867 struct lpfc_dmabuf *dmabuf;
12868 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012869 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12870
12871 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12872 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012873 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012874 status = bf_get(lpfc_wcqe_c_status, wcqe);
12875 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012876 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12877 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12878 pIocbIn->iocb.un.fcpi.fcpi_parm =
12879 pIocbOut->iocb.un.fcpi.fcpi_parm -
12880 wcqe->total_data_placed;
12881 else
12882 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012883 else {
James Smart4f774512009-05-22 14:52:35 -040012884 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012885 switch (pIocbOut->iocb.ulpCommand) {
12886 case CMD_ELS_REQUEST64_CR:
12887 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12888 bpl = (struct ulp_bde64 *)dmabuf->virt;
12889 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12890 max_response = bde.tus.f.bdeSize;
12891 break;
12892 case CMD_GEN_REQUEST64_CR:
12893 max_response = 0;
12894 if (!pIocbOut->context3)
12895 break;
12896 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12897 sizeof(struct ulp_bde64);
12898 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12899 bpl = (struct ulp_bde64 *)dmabuf->virt;
12900 for (i = 0; i < numBdes; i++) {
12901 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12902 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12903 max_response += bde.tus.f.bdeSize;
12904 }
12905 break;
12906 default:
12907 max_response = wcqe->total_data_placed;
12908 break;
12909 }
12910 if (max_response < wcqe->total_data_placed)
12911 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12912 else
12913 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12914 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012915 }
James Smart341af102010-01-26 23:07:37 -050012916
James Smartacd68592012-01-18 16:25:09 -050012917 /* Convert BG errors for completion status */
12918 if (status == CQE_STATUS_DI_ERROR) {
12919 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12920
12921 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12922 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12923 else
12924 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12925
12926 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12927 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12928 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12929 BGS_GUARD_ERR_MASK;
12930 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12931 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12932 BGS_APPTAG_ERR_MASK;
12933 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12934 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12935 BGS_REFTAG_ERR_MASK;
12936
12937 /* Check to see if there was any good data before the error */
12938 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12939 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12940 BGS_HI_WATER_MARK_PRESENT_MASK;
12941 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12942 wcqe->total_data_placed;
12943 }
12944
12945 /*
12946 * Set ALL the error bits to indicate we don't know what
12947 * type of error it is.
12948 */
12949 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12950 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12951 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12952 BGS_GUARD_ERR_MASK);
12953 }
12954
James Smart341af102010-01-26 23:07:37 -050012955 /* Pick up HBA exchange busy condition */
12956 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12957 spin_lock_irqsave(&phba->hbalock, iflags);
12958 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12959 spin_unlock_irqrestore(&phba->hbalock, iflags);
12960 }
James Smart4f774512009-05-22 14:52:35 -040012961}
12962
12963/**
James Smart45ed1192009-10-02 15:17:02 -040012964 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12965 * @phba: Pointer to HBA context object.
12966 * @wcqe: Pointer to work-queue completion queue entry.
12967 *
12968 * This routine handles an ELS work-queue completion event and construct
12969 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12970 * discovery engine to handle.
12971 *
12972 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12973 **/
12974static struct lpfc_iocbq *
12975lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12976 struct lpfc_iocbq *irspiocbq)
12977{
James Smart895427b2017-02-12 13:52:30 -080012978 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012979 struct lpfc_iocbq *cmdiocbq;
12980 struct lpfc_wcqe_complete *wcqe;
12981 unsigned long iflags;
12982
James Smart895427b2017-02-12 13:52:30 -080012983 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070012984 if (unlikely(!pring))
12985 return NULL;
James Smart895427b2017-02-12 13:52:30 -080012986
James Smart45ed1192009-10-02 15:17:02 -040012987 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012988 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012989 pring->stats.iocb_event++;
12990 /* Look up the ELS command IOCB and create pseudo response IOCB */
12991 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12992 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040012993 if (unlikely(!cmdiocbq)) {
Dick Kennedy401bb412017-09-29 17:34:28 -070012994 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012995 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12996 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070012997 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12998 wcqe->word0, wcqe->total_data_placed,
12999 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040013000 lpfc_sli_release_iocbq(phba, irspiocbq);
13001 return NULL;
13002 }
13003
Dick Kennedy401bb412017-09-29 17:34:28 -070013004 /* Put the iocb back on the txcmplq */
13005 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
13006 spin_unlock_irqrestore(&pring->ring_lock, iflags);
13007
James Smart45ed1192009-10-02 15:17:02 -040013008 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013009 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040013010
13011 return irspiocbq;
13012}
13013
James Smart8a5ca102017-11-20 16:00:30 -080013014inline struct lpfc_cq_event *
13015lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
13016{
13017 struct lpfc_cq_event *cq_event;
13018
13019 /* Allocate a new internal CQ_EVENT entry */
13020 cq_event = lpfc_sli4_cq_event_alloc(phba);
13021 if (!cq_event) {
13022 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13023 "0602 Failed to alloc CQ_EVENT entry\n");
13024 return NULL;
13025 }
13026
13027 /* Move the CQE into the event */
13028 memcpy(&cq_event->cqe, entry, size);
13029 return cq_event;
13030}
13031
James Smart45ed1192009-10-02 15:17:02 -040013032/**
James Smart04c68492009-05-22 14:52:52 -040013033 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
13034 * @phba: Pointer to HBA context object.
13035 * @cqe: Pointer to mailbox completion queue entry.
13036 *
13037 * This routine process a mailbox completion queue entry with asynchrous
13038 * event.
13039 *
13040 * Return: true if work posted to worker thread, otherwise false.
13041 **/
13042static bool
13043lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13044{
13045 struct lpfc_cq_event *cq_event;
13046 unsigned long iflags;
13047
13048 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13049 "0392 Async Event: word0:x%x, word1:x%x, "
13050 "word2:x%x, word3:x%x\n", mcqe->word0,
13051 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
13052
James Smart8a5ca102017-11-20 16:00:30 -080013053 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
13054 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040013055 return false;
James Smart04c68492009-05-22 14:52:52 -040013056 spin_lock_irqsave(&phba->hbalock, iflags);
13057 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
13058 /* Set the async event flag */
13059 phba->hba_flag |= ASYNC_EVENT;
13060 spin_unlock_irqrestore(&phba->hbalock, iflags);
13061
13062 return true;
13063}
13064
13065/**
13066 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
13067 * @phba: Pointer to HBA context object.
13068 * @cqe: Pointer to mailbox completion queue entry.
13069 *
13070 * This routine process a mailbox completion queue entry with mailbox
13071 * completion event.
13072 *
13073 * Return: true if work posted to worker thread, otherwise false.
13074 **/
13075static bool
13076lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13077{
13078 uint32_t mcqe_status;
13079 MAILBOX_t *mbox, *pmbox;
13080 struct lpfc_mqe *mqe;
13081 struct lpfc_vport *vport;
13082 struct lpfc_nodelist *ndlp;
13083 struct lpfc_dmabuf *mp;
13084 unsigned long iflags;
13085 LPFC_MBOXQ_t *pmb;
13086 bool workposted = false;
13087 int rc;
13088
13089 /* If not a mailbox complete MCQE, out by checking mailbox consume */
13090 if (!bf_get(lpfc_trailer_completed, mcqe))
13091 goto out_no_mqe_complete;
13092
13093 /* Get the reference to the active mbox command */
13094 spin_lock_irqsave(&phba->hbalock, iflags);
13095 pmb = phba->sli.mbox_active;
13096 if (unlikely(!pmb)) {
13097 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
13098 "1832 No pending MBOX command to handle\n");
13099 spin_unlock_irqrestore(&phba->hbalock, iflags);
13100 goto out_no_mqe_complete;
13101 }
13102 spin_unlock_irqrestore(&phba->hbalock, iflags);
13103 mqe = &pmb->u.mqe;
13104 pmbox = (MAILBOX_t *)&pmb->u.mqe;
13105 mbox = phba->mbox;
13106 vport = pmb->vport;
13107
13108 /* Reset heartbeat timer */
13109 phba->last_completion_time = jiffies;
13110 del_timer(&phba->sli.mbox_tmo);
13111
13112 /* Move mbox data to caller's mailbox region, do endian swapping */
13113 if (pmb->mbox_cmpl && mbox)
James Smart48f8fdb2018-05-04 20:37:51 -070013114 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040013115
James Smart73d91e52011-10-10 21:32:10 -040013116 /*
13117 * For mcqe errors, conditionally move a modified error code to
13118 * the mbox so that the error will not be missed.
13119 */
13120 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
13121 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
13122 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
13123 bf_set(lpfc_mqe_status, mqe,
13124 (LPFC_MBX_ERROR_RANGE | mcqe_status));
13125 }
James Smart04c68492009-05-22 14:52:52 -040013126 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13127 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13128 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
13129 "MBOX dflt rpi: status:x%x rpi:x%x",
13130 mcqe_status,
13131 pmbox->un.varWords[0], 0);
13132 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
James Smart3e1f0712018-11-29 16:09:29 -080013133 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
13134 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smart04c68492009-05-22 14:52:52 -040013135 /* Reg_LOGIN of dflt RPI was successful. Now lets get
13136 * RID of the PPI using the same mbox buffer.
13137 */
13138 lpfc_unreg_login(phba, vport->vpi,
13139 pmbox->un.varWords[0], pmb);
13140 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080013141 pmb->ctx_buf = mp;
13142 pmb->ctx_ndlp = ndlp;
James Smart04c68492009-05-22 14:52:52 -040013143 pmb->vport = vport;
13144 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
13145 if (rc != MBX_BUSY)
13146 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
13147 LOG_SLI, "0385 rc should "
13148 "have been MBX_BUSY\n");
13149 if (rc != MBX_NOT_FINISHED)
13150 goto send_current_mbox;
13151 }
13152 }
13153 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
13154 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
13155 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
13156
13157 /* There is mailbox completion work to do */
13158 spin_lock_irqsave(&phba->hbalock, iflags);
13159 __lpfc_mbox_cmpl_put(phba, pmb);
13160 phba->work_ha |= HA_MBATT;
13161 spin_unlock_irqrestore(&phba->hbalock, iflags);
13162 workposted = true;
13163
13164send_current_mbox:
13165 spin_lock_irqsave(&phba->hbalock, iflags);
13166 /* Release the mailbox command posting token */
13167 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13168 /* Setting active mailbox pointer need to be in sync to flag clear */
13169 phba->sli.mbox_active = NULL;
13170 spin_unlock_irqrestore(&phba->hbalock, iflags);
13171 /* Wake up worker thread to post the next pending mailbox command */
13172 lpfc_worker_wake_up(phba);
13173out_no_mqe_complete:
13174 if (bf_get(lpfc_trailer_consumed, mcqe))
13175 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
13176 return workposted;
13177}
13178
13179/**
13180 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
13181 * @phba: Pointer to HBA context object.
13182 * @cqe: Pointer to mailbox completion queue entry.
13183 *
13184 * This routine process a mailbox completion queue entry, it invokes the
13185 * proper mailbox complete handling or asynchrous event handling routine
13186 * according to the MCQE's async bit.
13187 *
13188 * Return: true if work posted to worker thread, otherwise false.
13189 **/
13190static bool
James Smart32517fc2019-01-28 11:14:33 -080013191lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13192 struct lpfc_cqe *cqe)
James Smart04c68492009-05-22 14:52:52 -040013193{
13194 struct lpfc_mcqe mcqe;
13195 bool workposted;
13196
James Smart32517fc2019-01-28 11:14:33 -080013197 cq->CQ_mbox++;
13198
James Smart04c68492009-05-22 14:52:52 -040013199 /* Copy the mailbox MCQE and convert endian order as needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013200 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
James Smart04c68492009-05-22 14:52:52 -040013201
13202 /* Invoke the proper event handling routine */
13203 if (!bf_get(lpfc_trailer_async, &mcqe))
13204 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
13205 else
13206 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
13207 return workposted;
13208}
13209
13210/**
James Smart4f774512009-05-22 14:52:35 -040013211 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
13212 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040013213 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040013214 * @wcqe: Pointer to work-queue completion queue entry.
13215 *
13216 * This routine handles an ELS work-queue completion event.
13217 *
13218 * Return: true if work posted to worker thread, otherwise false.
13219 **/
13220static bool
James Smart2a76a282012-08-03 12:35:54 -040013221lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013222 struct lpfc_wcqe_complete *wcqe)
13223{
James Smart4f774512009-05-22 14:52:35 -040013224 struct lpfc_iocbq *irspiocbq;
13225 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040013226 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050013227 int txq_cnt = 0;
13228 int txcmplq_cnt = 0;
13229 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040013230
James Smart11f0e342018-05-04 20:37:57 -070013231 /* Check for response status */
13232 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13233 /* Log the error status */
13234 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13235 "0357 ELS CQE error: status=x%x: "
13236 "CQE: %08x %08x %08x %08x\n",
13237 bf_get(lpfc_wcqe_c_status, wcqe),
13238 wcqe->word0, wcqe->total_data_placed,
13239 wcqe->parameter, wcqe->word3);
13240 }
13241
James Smart45ed1192009-10-02 15:17:02 -040013242 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040013243 irspiocbq = lpfc_sli_get_iocbq(phba);
13244 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050013245 if (!list_empty(&pring->txq))
13246 txq_cnt++;
13247 if (!list_empty(&pring->txcmplq))
13248 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040013249 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040013250 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
13251 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050013252 txq_cnt, phba->iocb_cnt,
13253 fcp_txcmplq_cnt,
13254 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040013255 return false;
James Smart4f774512009-05-22 14:52:35 -040013256 }
James Smart4f774512009-05-22 14:52:35 -040013257
James Smart45ed1192009-10-02 15:17:02 -040013258 /* Save off the slow-path queue event for work thread to process */
13259 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040013260 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040013261 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013262 &phba->sli4_hba.sp_queue_event);
13263 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040013264 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013265
James Smart45ed1192009-10-02 15:17:02 -040013266 return true;
James Smart4f774512009-05-22 14:52:35 -040013267}
13268
13269/**
13270 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13271 * @phba: Pointer to HBA context object.
13272 * @wcqe: Pointer to work-queue completion queue entry.
13273 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013274 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013275 * proper WQ release routine to the slow-path WQ.
13276 **/
13277static void
13278lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13279 struct lpfc_wcqe_release *wcqe)
13280{
James Smart2e90f4b2011-12-13 13:22:37 -050013281 /* sanity check on queue memory */
13282 if (unlikely(!phba->sli4_hba.els_wq))
13283 return;
James Smart4f774512009-05-22 14:52:35 -040013284 /* Check for the slow-path ELS work queue */
13285 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13286 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13287 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13288 else
13289 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13290 "2579 Slow-path wqe consume event carries "
13291 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13292 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13293 phba->sli4_hba.els_wq->queue_id);
13294}
13295
13296/**
13297 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13298 * @phba: Pointer to HBA context object.
13299 * @cq: Pointer to a WQ completion queue.
13300 * @wcqe: Pointer to work-queue completion queue entry.
13301 *
13302 * This routine handles an XRI abort event.
13303 *
13304 * Return: true if work posted to worker thread, otherwise false.
13305 **/
13306static bool
13307lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13308 struct lpfc_queue *cq,
13309 struct sli4_wcqe_xri_aborted *wcqe)
13310{
13311 bool workposted = false;
13312 struct lpfc_cq_event *cq_event;
13313 unsigned long iflags;
13314
James Smart4f774512009-05-22 14:52:35 -040013315 switch (cq->subtype) {
13316 case LPFC_FCP:
James Smart5e5b5112019-01-28 11:14:22 -080013317 lpfc_sli4_fcp_xri_aborted(phba, wcqe, cq->hdwq);
13318 workposted = false;
James Smart4f774512009-05-22 14:52:35 -040013319 break;
James Smart422c4cb2017-11-20 16:00:32 -080013320 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040013321 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080013322 cq_event = lpfc_cq_event_setup(
13323 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13324 if (!cq_event)
13325 return false;
James Smart5e5b5112019-01-28 11:14:22 -080013326 cq_event->hdwq = cq->hdwq;
James Smart4f774512009-05-22 14:52:35 -040013327 spin_lock_irqsave(&phba->hbalock, iflags);
13328 list_add_tail(&cq_event->list,
13329 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13330 /* Set the els xri abort event flag */
13331 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13332 spin_unlock_irqrestore(&phba->hbalock, iflags);
13333 workposted = true;
13334 break;
James Smart318083a2017-03-04 09:30:30 -080013335 case LPFC_NVME:
James Smart8a5ca102017-11-20 16:00:30 -080013336 /* Notify aborted XRI for NVME work queue */
13337 if (phba->nvmet_support)
13338 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13339 else
James Smart5e5b5112019-01-28 11:14:22 -080013340 lpfc_sli4_nvme_xri_aborted(phba, wcqe, cq->hdwq);
James Smart8a5ca102017-11-20 16:00:30 -080013341
13342 workposted = false;
James Smart318083a2017-03-04 09:30:30 -080013343 break;
James Smart4f774512009-05-22 14:52:35 -040013344 default:
13345 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080013346 "0603 Invalid CQ subtype %d: "
13347 "%08x %08x %08x %08x\n",
13348 cq->subtype, wcqe->word0, wcqe->parameter,
13349 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013350 workposted = false;
13351 break;
13352 }
13353 return workposted;
13354}
13355
James Smarte817e5d2018-12-13 15:17:53 -080013356#define FC_RCTL_MDS_DIAGS 0xF4
13357
James Smart4f774512009-05-22 14:52:35 -040013358/**
James Smart4d9ab992009-10-02 15:16:39 -040013359 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040013360 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040013361 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013362 *
James Smart4d9ab992009-10-02 15:16:39 -040013363 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013364 *
13365 * Return: true if work posted to worker thread, otherwise false.
13366 **/
13367static bool
James Smart4d9ab992009-10-02 15:16:39 -040013368lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13369{
13370 bool workposted = false;
James Smarte817e5d2018-12-13 15:17:53 -080013371 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040013372 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13373 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070013374 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040013375 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040013376 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040013377 unsigned long iflags;
13378
James Smart2e90f4b2011-12-13 13:22:37 -050013379 /* sanity check on queue memory */
13380 if (unlikely(!hrq) || unlikely(!drq))
13381 return workposted;
13382
James Smart7851fe22011-07-22 18:36:52 -040013383 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13384 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13385 else
13386 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13387 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040013388 goto out;
13389
13390 status = bf_get(lpfc_rcqe_status, rcqe);
13391 switch (status) {
13392 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13393 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13394 "2537 Receive Frame Truncated!!\n");
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060013395 /* fall through */
James Smart4d9ab992009-10-02 15:16:39 -040013396 case FC_STATUS_RQ_SUCCESS:
13397 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013398 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040013399 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13400 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040013401 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040013402 spin_unlock_irqrestore(&phba->hbalock, iflags);
13403 goto out;
13404 }
James Smartb84daac2012-08-03 12:35:13 -040013405 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013406 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040013407 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080013408
James Smarte817e5d2018-12-13 15:17:53 -080013409 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13410
13411 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
13412 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
13413 spin_unlock_irqrestore(&phba->hbalock, iflags);
13414 /* Handle MDS Loopback frames */
13415 lpfc_sli4_handle_mds_loopback(phba->pport, dma_buf);
13416 break;
13417 }
13418
13419 /* save off the frame for the work thread to process */
James Smart4d9ab992009-10-02 15:16:39 -040013420 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013421 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040013422 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040013423 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040013424 spin_unlock_irqrestore(&phba->hbalock, iflags);
13425 workposted = true;
13426 break;
James Smart4d9ab992009-10-02 15:16:39 -040013427 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013428 if (phba->nvmet_support) {
13429 tgtp = phba->targetport->private;
13430 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13431 "6402 RQE Error x%x, posted %d err_cnt "
13432 "%d: %x %x %x\n",
13433 status, hrq->RQ_buf_posted,
13434 hrq->RQ_no_posted_buf,
13435 atomic_read(&tgtp->rcv_fcp_cmd_in),
13436 atomic_read(&tgtp->rcv_fcp_cmd_out),
13437 atomic_read(&tgtp->xmt_fcp_release));
13438 }
13439 /* fallthrough */
13440
13441 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040013442 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040013443 /* Post more buffers if possible */
13444 spin_lock_irqsave(&phba->hbalock, iflags);
13445 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13446 spin_unlock_irqrestore(&phba->hbalock, iflags);
13447 workposted = true;
13448 break;
13449 }
13450out:
13451 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040013452}
13453
13454/**
13455 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13456 * @phba: Pointer to HBA context object.
13457 * @cq: Pointer to the completion queue.
James Smart32517fc2019-01-28 11:14:33 -080013458 * @cqe: Pointer to a completion queue entry.
James Smart4d9ab992009-10-02 15:16:39 -040013459 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013460 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040013461 * entry.
13462 *
13463 * Return: true if work posted to worker thread, otherwise false.
13464 **/
13465static bool
13466lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013467 struct lpfc_cqe *cqe)
13468{
James Smart45ed1192009-10-02 15:17:02 -040013469 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040013470 bool workposted = false;
13471
13472 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013473 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013474
13475 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040013476 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040013477 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040013478 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040013479 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040013480 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013481 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013482 break;
13483 case CQE_CODE_RELEASE_WQE:
13484 /* Process the WQ release event */
13485 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013486 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013487 break;
13488 case CQE_CODE_XRI_ABORTED:
13489 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013490 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013491 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013492 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013493 break;
James Smart4d9ab992009-10-02 15:16:39 -040013494 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013495 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013496 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013497 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013498 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013499 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013500 break;
James Smart4f774512009-05-22 14:52:35 -040013501 default:
13502 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13503 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013504 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013505 break;
13506 }
13507 return workposted;
13508}
13509
13510/**
James Smart4f774512009-05-22 14:52:35 -040013511 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13512 * @phba: Pointer to HBA context object.
13513 * @eqe: Pointer to fast-path event queue entry.
13514 *
13515 * This routine process a event queue entry from the slow-path event queue.
13516 * It will check the MajorCode and MinorCode to determine this is for a
13517 * completion event on a completion queue, if not, an error shall be logged
13518 * and just return. Otherwise, it will get to the corresponding completion
13519 * queue and process all the entries on that completion queue, rearm the
13520 * completion queue, and then return.
13521 *
13522 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013523static void
James Smart67d12732012-08-03 12:36:13 -040013524lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13525 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013526{
James Smart67d12732012-08-03 12:36:13 -040013527 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013528 uint16_t cqid;
13529
James Smart4f774512009-05-22 14:52:35 -040013530 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013531 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013532
James Smart4f774512009-05-22 14:52:35 -040013533 list_for_each_entry(childq, &speq->child_list, list) {
13534 if (childq->queue_id == cqid) {
13535 cq = childq;
13536 break;
13537 }
13538 }
13539 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013540 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13541 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13542 "0365 Slow-path CQ identifier "
13543 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013544 return;
James Smart4f774512009-05-22 14:52:35 -040013545 }
13546
James Smart895427b2017-02-12 13:52:30 -080013547 /* Save EQ associated with this CQ */
13548 cq->assoc_qp = speq;
13549
James Smart6a828b02019-01-28 11:14:31 -080013550 if (!queue_work_on(cq->chann, phba->wq, &cq->spwork))
Dick Kennedyf485c182017-09-29 17:34:34 -070013551 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13552 "0390 Cannot schedule soft IRQ "
13553 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
Bart Van Assched6d189c2019-03-28 11:06:22 -070013554 cqid, cq->queue_id, raw_smp_processor_id());
Dick Kennedyf485c182017-09-29 17:34:34 -070013555}
13556
13557/**
James Smart32517fc2019-01-28 11:14:33 -080013558 * __lpfc_sli4_process_cq - Process elements of a CQ
Dick Kennedyf485c182017-09-29 17:34:34 -070013559 * @phba: Pointer to HBA context object.
James Smart32517fc2019-01-28 11:14:33 -080013560 * @cq: Pointer to CQ to be processed
13561 * @handler: Routine to process each cqe
13562 * @delay: Pointer to usdelay to set in case of rescheduling of the handler
Dick Kennedyf485c182017-09-29 17:34:34 -070013563 *
James Smart32517fc2019-01-28 11:14:33 -080013564 * This routine processes completion queue entries in a CQ. While a valid
13565 * queue element is found, the handler is called. During processing checks
13566 * are made for periodic doorbell writes to let the hardware know of
13567 * element consumption.
Dick Kennedyf485c182017-09-29 17:34:34 -070013568 *
James Smart32517fc2019-01-28 11:14:33 -080013569 * If the max limit on cqes to process is hit, or there are no more valid
13570 * entries, the loop stops. If we processed a sufficient number of elements,
13571 * meaning there is sufficient load, rather than rearming and generating
13572 * another interrupt, a cq rescheduling delay will be set. A delay of 0
13573 * indicates no rescheduling.
13574 *
13575 * Returns True if work scheduled, False otherwise.
Dick Kennedyf485c182017-09-29 17:34:34 -070013576 **/
James Smart32517fc2019-01-28 11:14:33 -080013577static bool
13578__lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
13579 bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
13580 struct lpfc_cqe *), unsigned long *delay)
Dick Kennedyf485c182017-09-29 17:34:34 -070013581{
Dick Kennedyf485c182017-09-29 17:34:34 -070013582 struct lpfc_cqe *cqe;
13583 bool workposted = false;
James Smart32517fc2019-01-28 11:14:33 -080013584 int count = 0, consumed = 0;
13585 bool arm = true;
13586
13587 /* default - no reschedule */
13588 *delay = 0;
13589
13590 if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
13591 goto rearm_and_exit;
Dick Kennedyf485c182017-09-29 17:34:34 -070013592
James Smart4f774512009-05-22 14:52:35 -040013593 /* Process all the entries to the CQ */
James Smartd74a89a2019-05-21 17:48:55 -070013594 cq->q_flag = 0;
James Smart32517fc2019-01-28 11:14:33 -080013595 cqe = lpfc_sli4_cq_get(cq);
13596 while (cqe) {
James Smart32517fc2019-01-28 11:14:33 -080013597 workposted |= handler(phba, cq, cqe);
13598 __lpfc_sli4_consume_cqe(phba, cq, cqe);
13599
13600 consumed++;
13601 if (!(++count % cq->max_proc_limit))
13602 break;
13603
13604 if (!(count % cq->notify_interval)) {
13605 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
13606 LPFC_QUEUE_NOARM);
13607 consumed = 0;
James Smart4f774512009-05-22 14:52:35 -040013608 }
James Smartb84daac2012-08-03 12:35:13 -040013609
James Smartd74a89a2019-05-21 17:48:55 -070013610 if (count == LPFC_NVMET_CQ_NOTIFY)
13611 cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
13612
James Smart32517fc2019-01-28 11:14:33 -080013613 cqe = lpfc_sli4_cq_get(cq);
13614 }
13615 if (count >= phba->cfg_cq_poll_threshold) {
13616 *delay = 1;
13617 arm = false;
13618 }
13619
13620 /* Track the max number of CQEs processed in 1 EQ */
13621 if (count > cq->CQ_max_cqe)
13622 cq->CQ_max_cqe = count;
13623
13624 cq->assoc_qp->EQ_cqe_cnt += count;
13625
13626 /* Catch the no cq entry condition */
13627 if (unlikely(count == 0))
13628 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13629 "0369 No entry from completion queue "
13630 "qid=%d\n", cq->queue_id);
13631
13632 cq->queue_claimed = 0;
13633
13634rearm_and_exit:
13635 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
13636 arm ? LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
13637
13638 return workposted;
13639}
13640
13641/**
13642 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13643 * @cq: pointer to CQ to process
13644 *
13645 * This routine calls the cq processing routine with a handler specific
13646 * to the type of queue bound to it.
13647 *
13648 * The CQ routine returns two values: the first is the calling status,
13649 * which indicates whether work was queued to the background discovery
13650 * thread. If true, the routine should wakeup the discovery thread;
13651 * the second is the delay parameter. If non-zero, rather than rearming
13652 * the CQ and yet another interrupt, the CQ handler should be queued so
13653 * that it is processed in a subsequent polling action. The value of
13654 * the delay indicates when to reschedule it.
13655 **/
13656static void
13657__lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
13658{
13659 struct lpfc_hba *phba = cq->phba;
13660 unsigned long delay;
13661 bool workposted = false;
13662
13663 /* Process and rearm the CQ */
13664 switch (cq->type) {
13665 case LPFC_MCQ:
13666 workposted |= __lpfc_sli4_process_cq(phba, cq,
13667 lpfc_sli4_sp_handle_mcqe,
13668 &delay);
13669 break;
13670 case LPFC_WCQ:
13671 if (cq->subtype == LPFC_FCP || cq->subtype == LPFC_NVME)
13672 workposted |= __lpfc_sli4_process_cq(phba, cq,
13673 lpfc_sli4_fp_handle_cqe,
13674 &delay);
13675 else
13676 workposted |= __lpfc_sli4_process_cq(phba, cq,
13677 lpfc_sli4_sp_handle_cqe,
13678 &delay);
James Smart4f774512009-05-22 14:52:35 -040013679 break;
13680 default:
13681 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13682 "0370 Invalid completion queue type (%d)\n",
13683 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013684 return;
James Smart4f774512009-05-22 14:52:35 -040013685 }
13686
James Smart32517fc2019-01-28 11:14:33 -080013687 if (delay) {
13688 if (!queue_delayed_work_on(cq->chann, phba->wq,
13689 &cq->sched_spwork, delay))
13690 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13691 "0394 Cannot schedule soft IRQ "
13692 "for cqid=%d on CPU %d\n",
13693 cq->queue_id, cq->chann);
13694 }
James Smart4f774512009-05-22 14:52:35 -040013695
13696 /* wake up worker thread if there are works to be done */
13697 if (workposted)
13698 lpfc_worker_wake_up(phba);
13699}
13700
13701/**
James Smart32517fc2019-01-28 11:14:33 -080013702 * lpfc_sli4_sp_process_cq - slow-path work handler when started by
13703 * interrupt
13704 * @work: pointer to work element
13705 *
13706 * translates from the work handler and calls the slow-path handler.
13707 **/
13708static void
13709lpfc_sli4_sp_process_cq(struct work_struct *work)
13710{
13711 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
13712
13713 __lpfc_sli4_sp_process_cq(cq);
13714}
13715
13716/**
13717 * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
13718 * @work: pointer to work element
13719 *
13720 * translates from the work handler and calls the slow-path handler.
13721 **/
13722static void
13723lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
13724{
13725 struct lpfc_queue *cq = container_of(to_delayed_work(work),
13726 struct lpfc_queue, sched_spwork);
13727
13728 __lpfc_sli4_sp_process_cq(cq);
13729}
13730
13731/**
James Smart4f774512009-05-22 14:52:35 -040013732 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013733 * @phba: Pointer to HBA context object.
13734 * @cq: Pointer to associated CQ
13735 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013736 *
13737 * This routine process a fast-path work queue completion entry from fast-path
13738 * event queue for FCP command response completion.
13739 **/
13740static void
James Smart2a76a282012-08-03 12:35:54 -040013741lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013742 struct lpfc_wcqe_complete *wcqe)
13743{
James Smart2a76a282012-08-03 12:35:54 -040013744 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013745 struct lpfc_iocbq *cmdiocbq;
13746 struct lpfc_iocbq irspiocbq;
13747 unsigned long iflags;
13748
James Smart4f774512009-05-22 14:52:35 -040013749 /* Check for response status */
13750 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13751 /* If resource errors reported from HBA, reduce queue
13752 * depth of the SCSI device.
13753 */
James Smarte3d2b802012-08-14 14:25:43 -040013754 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13755 IOSTAT_LOCAL_REJECT)) &&
13756 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13757 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013758 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013759
James Smart4f774512009-05-22 14:52:35 -040013760 /* Log the error status */
James Smart11f0e342018-05-04 20:37:57 -070013761 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13762 "0373 FCP CQE error: status=x%x: "
13763 "CQE: %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013764 bf_get(lpfc_wcqe_c_status, wcqe),
James Smart11f0e342018-05-04 20:37:57 -070013765 wcqe->word0, wcqe->total_data_placed,
13766 wcqe->parameter, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013767 }
13768
13769 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013770 spin_lock_irqsave(&pring->ring_lock, iflags);
13771 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013772 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13773 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013774 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013775 if (unlikely(!cmdiocbq)) {
13776 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13777 "0374 FCP complete with no corresponding "
13778 "cmdiocb: iotag (%d)\n",
13779 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13780 return;
13781 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013782#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13783 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13784#endif
James Smart895427b2017-02-12 13:52:30 -080013785 if (cmdiocbq->iocb_cmpl == NULL) {
13786 if (cmdiocbq->wqe_cmpl) {
13787 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13788 spin_lock_irqsave(&phba->hbalock, iflags);
13789 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13790 spin_unlock_irqrestore(&phba->hbalock, iflags);
13791 }
13792
13793 /* Pass the cmd_iocb and the wcqe to the upper layer */
13794 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13795 return;
13796 }
James Smart4f774512009-05-22 14:52:35 -040013797 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13798 "0375 FCP cmdiocb not callback function "
13799 "iotag: (%d)\n",
13800 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13801 return;
13802 }
13803
13804 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013805 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013806
James Smart0f65ff62010-02-26 14:14:23 -050013807 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13808 spin_lock_irqsave(&phba->hbalock, iflags);
13809 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13810 spin_unlock_irqrestore(&phba->hbalock, iflags);
13811 }
13812
James Smart4f774512009-05-22 14:52:35 -040013813 /* Pass the cmd_iocb and the rsp state to the upper layer */
13814 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13815}
13816
13817/**
13818 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13819 * @phba: Pointer to HBA context object.
13820 * @cq: Pointer to completion queue.
13821 * @wcqe: Pointer to work-queue completion queue entry.
13822 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013823 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013824 * proper WQ release routine to the slow-path WQ.
13825 **/
13826static void
13827lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13828 struct lpfc_wcqe_release *wcqe)
13829{
13830 struct lpfc_queue *childwq;
13831 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013832 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013833
13834 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013835 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013836 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013837 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013838 lpfc_sli4_wq_release(childwq,
13839 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080013840 if (childwq->q_flag & HBA_NVMET_WQFULL)
13841 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040013842 wqid_matched = true;
13843 break;
13844 }
13845 }
13846 /* Report warning log message if no match found */
13847 if (wqid_matched != true)
13848 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13849 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013850 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013851}
13852
13853/**
James Smart2d7dbc42017-02-12 13:52:35 -080013854 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13855 * @phba: Pointer to HBA context object.
13856 * @rcqe: Pointer to receive-queue completion queue entry.
13857 *
13858 * This routine process a receive-queue completion queue entry.
13859 *
13860 * Return: true if work posted to worker thread, otherwise false.
13861 **/
13862static bool
13863lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13864 struct lpfc_rcqe *rcqe)
13865{
13866 bool workposted = false;
13867 struct lpfc_queue *hrq;
13868 struct lpfc_queue *drq;
13869 struct rqb_dmabuf *dma_buf;
13870 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013871 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013872 uint32_t status, rq_id;
13873 unsigned long iflags;
13874 uint32_t fctl, idx;
13875
13876 if ((phba->nvmet_support == 0) ||
13877 (phba->sli4_hba.nvmet_cqset == NULL))
13878 return workposted;
13879
13880 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13881 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13882 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13883
13884 /* sanity check on queue memory */
13885 if (unlikely(!hrq) || unlikely(!drq))
13886 return workposted;
13887
13888 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13889 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13890 else
13891 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13892
13893 if ((phba->nvmet_support == 0) ||
13894 (rq_id != hrq->queue_id))
13895 return workposted;
13896
13897 status = bf_get(lpfc_rcqe_status, rcqe);
13898 switch (status) {
13899 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13900 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13901 "6126 Receive Frame Truncated!!\n");
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060013902 /* fall through */
James Smart2d7dbc42017-02-12 13:52:35 -080013903 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080013904 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013905 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080013906 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13907 if (!dma_buf) {
13908 hrq->RQ_no_buf_found++;
13909 spin_unlock_irqrestore(&phba->hbalock, iflags);
13910 goto out;
13911 }
13912 spin_unlock_irqrestore(&phba->hbalock, iflags);
13913 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013914 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013915 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13916
13917 /* Just some basic sanity checks on FCP Command frame */
13918 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13919 fc_hdr->fh_f_ctl[1] << 8 |
13920 fc_hdr->fh_f_ctl[2]);
13921 if (((fctl &
13922 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13923 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13924 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13925 goto drop;
13926
13927 if (fc_hdr->fh_type == FC_TYPE_FCP) {
James Smartd74a89a2019-05-21 17:48:55 -070013928 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013929 lpfc_nvmet_unsol_fcp_event(
James Smartd74a89a2019-05-21 17:48:55 -070013930 phba, idx, dma_buf, cq->isr_timestamp,
13931 cq->q_flag & HBA_NVMET_CQ_NOTIFY);
James Smart2d7dbc42017-02-12 13:52:35 -080013932 return false;
13933 }
13934drop:
James Smart22b738a2019-03-12 16:30:11 -070013935 lpfc_rq_buf_free(phba, &dma_buf->hbuf);
James Smart2d7dbc42017-02-12 13:52:35 -080013936 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013937 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013938 if (phba->nvmet_support) {
13939 tgtp = phba->targetport->private;
13940 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13941 "6401 RQE Error x%x, posted %d err_cnt "
13942 "%d: %x %x %x\n",
13943 status, hrq->RQ_buf_posted,
13944 hrq->RQ_no_posted_buf,
13945 atomic_read(&tgtp->rcv_fcp_cmd_in),
13946 atomic_read(&tgtp->rcv_fcp_cmd_out),
13947 atomic_read(&tgtp->xmt_fcp_release));
13948 }
13949 /* fallthrough */
13950
13951 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013952 hrq->RQ_no_posted_buf++;
13953 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013954 break;
13955 }
13956out:
13957 return workposted;
13958}
13959
13960/**
James Smart895427b2017-02-12 13:52:30 -080013961 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart32517fc2019-01-28 11:14:33 -080013962 * @phba: adapter with cq
James Smart4f774512009-05-22 14:52:35 -040013963 * @cq: Pointer to the completion queue.
13964 * @eqe: Pointer to fast-path completion queue entry.
13965 *
13966 * This routine process a fast-path work queue completion entry from fast-path
13967 * event queue for FCP command response completion.
James Smart32517fc2019-01-28 11:14:33 -080013968 *
13969 * Return: true if work posted to worker thread, otherwise false.
James Smart4f774512009-05-22 14:52:35 -040013970 **/
James Smart32517fc2019-01-28 11:14:33 -080013971static bool
James Smart895427b2017-02-12 13:52:30 -080013972lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013973 struct lpfc_cqe *cqe)
13974{
13975 struct lpfc_wcqe_release wcqe;
13976 bool workposted = false;
13977
13978 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013979 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013980
13981 /* Check and process for different type of WCQE and dispatch */
13982 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13983 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013984 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013985 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013986 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013987 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013988 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13989 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13990 (struct lpfc_wcqe_complete *)&wcqe);
13991 if (cq->subtype == LPFC_NVME_LS)
13992 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013993 (struct lpfc_wcqe_complete *)&wcqe);
13994 break;
13995 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013996 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013997 /* Process the WQ release event */
13998 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13999 (struct lpfc_wcqe_release *)&wcqe);
14000 break;
14001 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040014002 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040014003 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040014004 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040014005 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14006 (struct sli4_wcqe_xri_aborted *)&wcqe);
14007 break;
James Smart895427b2017-02-12 13:52:30 -080014008 case CQE_CODE_RECEIVE_V1:
14009 case CQE_CODE_RECEIVE:
14010 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080014011 if (cq->subtype == LPFC_NVMET) {
14012 workposted = lpfc_sli4_nvmet_handle_rcqe(
14013 phba, cq, (struct lpfc_rcqe *)&wcqe);
14014 }
James Smart895427b2017-02-12 13:52:30 -080014015 break;
James Smart4f774512009-05-22 14:52:35 -040014016 default:
14017 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080014018 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040014019 bf_get(lpfc_wcqe_c_code, &wcqe));
14020 break;
14021 }
14022 return workposted;
14023}
14024
14025/**
James Smart67d12732012-08-03 12:36:13 -040014026 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040014027 * @phba: Pointer to HBA context object.
14028 * @eqe: Pointer to fast-path event queue entry.
14029 *
14030 * This routine process a event queue entry from the fast-path event queue.
14031 * It will check the MajorCode and MinorCode to determine this is for a
14032 * completion event on a completion queue, if not, an error shall be logged
14033 * and just return. Otherwise, it will get to the corresponding completion
14034 * queue and process all the entries on the completion queue, rearm the
14035 * completion queue, and then return.
14036 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070014037static void
James Smart32517fc2019-01-28 11:14:33 -080014038lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
14039 struct lpfc_eqe *eqe)
James Smart4f774512009-05-22 14:52:35 -040014040{
James Smart895427b2017-02-12 13:52:30 -080014041 struct lpfc_queue *cq = NULL;
James Smart32517fc2019-01-28 11:14:33 -080014042 uint32_t qidx = eq->hdwq;
James Smart2d7dbc42017-02-12 13:52:35 -080014043 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040014044
James Smartcb5172e2010-03-15 11:25:07 -040014045 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040014046 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040014047 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040014048 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040014049 bf_get_le32(lpfc_eqe_major_code, eqe),
14050 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070014051 return;
James Smart4f774512009-05-22 14:52:35 -040014052 }
14053
James Smart67d12732012-08-03 12:36:13 -040014054 /* Get the reference to the corresponding CQ */
14055 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14056
James Smart6a828b02019-01-28 11:14:31 -080014057 /* Use the fast lookup method first */
14058 if (cqid <= phba->sli4_hba.cq_max) {
14059 cq = phba->sli4_hba.cq_lookup[cqid];
14060 if (cq)
14061 goto work_cq;
James Smartcdb42be2019-01-28 11:14:21 -080014062 }
14063
14064 /* Next check for NVMET completion */
James Smart2d7dbc42017-02-12 13:52:35 -080014065 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
14066 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
14067 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
14068 /* Process NVMET unsol rcv */
14069 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
14070 goto process_cq;
14071 }
14072 }
14073
James Smart895427b2017-02-12 13:52:30 -080014074 if (phba->sli4_hba.nvmels_cq &&
14075 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
14076 /* Process NVME unsol rcv */
14077 cq = phba->sli4_hba.nvmels_cq;
14078 }
14079
14080 /* Otherwise this is a Slow path event */
14081 if (cq == NULL) {
James Smartcdb42be2019-01-28 11:14:21 -080014082 lpfc_sli4_sp_handle_eqe(phba, eqe,
14083 phba->sli4_hba.hdwq[qidx].hba_eq);
Dick Kennedyf485c182017-09-29 17:34:34 -070014084 return;
James Smart67d12732012-08-03 12:36:13 -040014085 }
14086
James Smart895427b2017-02-12 13:52:30 -080014087process_cq:
James Smart4f774512009-05-22 14:52:35 -040014088 if (unlikely(cqid != cq->queue_id)) {
14089 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14090 "0368 Miss-matched fast-path completion "
14091 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
14092 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070014093 return;
James Smart4f774512009-05-22 14:52:35 -040014094 }
14095
James Smart6a828b02019-01-28 11:14:31 -080014096work_cq:
James Smartd74a89a2019-05-21 17:48:55 -070014097#if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
14098 if (phba->ktime_on)
14099 cq->isr_timestamp = ktime_get_ns();
14100 else
14101 cq->isr_timestamp = 0;
14102#endif
James Smart45aa3122019-01-28 11:14:29 -080014103 if (!queue_work_on(cq->chann, phba->wq, &cq->irqwork))
Dick Kennedyf485c182017-09-29 17:34:34 -070014104 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14105 "0363 Cannot schedule soft IRQ "
14106 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
Bart Van Assched6d189c2019-03-28 11:06:22 -070014107 cqid, cq->queue_id, raw_smp_processor_id());
Dick Kennedyf485c182017-09-29 17:34:34 -070014108}
14109
14110/**
James Smart32517fc2019-01-28 11:14:33 -080014111 * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
14112 * @cq: Pointer to CQ to be processed
Dick Kennedyf485c182017-09-29 17:34:34 -070014113 *
James Smart32517fc2019-01-28 11:14:33 -080014114 * This routine calls the cq processing routine with the handler for
14115 * fast path CQEs.
14116 *
14117 * The CQ routine returns two values: the first is the calling status,
14118 * which indicates whether work was queued to the background discovery
14119 * thread. If true, the routine should wakeup the discovery thread;
14120 * the second is the delay parameter. If non-zero, rather than rearming
14121 * the CQ and yet another interrupt, the CQ handler should be queued so
14122 * that it is processed in a subsequent polling action. The value of
14123 * the delay indicates when to reschedule it.
Dick Kennedyf485c182017-09-29 17:34:34 -070014124 **/
14125static void
James Smart32517fc2019-01-28 11:14:33 -080014126__lpfc_sli4_hba_process_cq(struct lpfc_queue *cq)
Dick Kennedyf485c182017-09-29 17:34:34 -070014127{
Dick Kennedyf485c182017-09-29 17:34:34 -070014128 struct lpfc_hba *phba = cq->phba;
James Smart32517fc2019-01-28 11:14:33 -080014129 unsigned long delay;
Dick Kennedyf485c182017-09-29 17:34:34 -070014130 bool workposted = false;
Dick Kennedyf485c182017-09-29 17:34:34 -070014131
James Smart32517fc2019-01-28 11:14:33 -080014132 /* process and rearm the CQ */
14133 workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
14134 &delay);
14135
14136 if (delay) {
14137 if (!queue_delayed_work_on(cq->chann, phba->wq,
14138 &cq->sched_irqwork, delay))
14139 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14140 "0367 Cannot schedule soft IRQ "
14141 "for cqid=%d on CPU %d\n",
14142 cq->queue_id, cq->chann);
James Smart4f774512009-05-22 14:52:35 -040014143 }
14144
James Smart4f774512009-05-22 14:52:35 -040014145 /* wake up worker thread if there are works to be done */
14146 if (workposted)
14147 lpfc_worker_wake_up(phba);
14148}
14149
James Smart1ba981f2014-02-20 09:56:45 -050014150/**
James Smart32517fc2019-01-28 11:14:33 -080014151 * lpfc_sli4_hba_process_cq - fast-path work handler when started by
14152 * interrupt
14153 * @work: pointer to work element
James Smart1ba981f2014-02-20 09:56:45 -050014154 *
James Smart32517fc2019-01-28 11:14:33 -080014155 * translates from the work handler and calls the fast-path handler.
James Smart1ba981f2014-02-20 09:56:45 -050014156 **/
14157static void
James Smart32517fc2019-01-28 11:14:33 -080014158lpfc_sli4_hba_process_cq(struct work_struct *work)
James Smart1ba981f2014-02-20 09:56:45 -050014159{
James Smart32517fc2019-01-28 11:14:33 -080014160 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
James Smart1ba981f2014-02-20 09:56:45 -050014161
James Smart32517fc2019-01-28 11:14:33 -080014162 __lpfc_sli4_hba_process_cq(cq);
James Smart1ba981f2014-02-20 09:56:45 -050014163}
14164
14165/**
James Smart32517fc2019-01-28 11:14:33 -080014166 * lpfc_sli4_hba_process_cq - fast-path work handler when started by timer
14167 * @work: pointer to work element
James Smart1ba981f2014-02-20 09:56:45 -050014168 *
James Smart32517fc2019-01-28 11:14:33 -080014169 * translates from the work handler and calls the fast-path handler.
James Smart1ba981f2014-02-20 09:56:45 -050014170 **/
James Smart32517fc2019-01-28 11:14:33 -080014171static void
14172lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
James Smart1ba981f2014-02-20 09:56:45 -050014173{
James Smart32517fc2019-01-28 11:14:33 -080014174 struct lpfc_queue *cq = container_of(to_delayed_work(work),
14175 struct lpfc_queue, sched_irqwork);
James Smart1ba981f2014-02-20 09:56:45 -050014176
James Smart32517fc2019-01-28 11:14:33 -080014177 __lpfc_sli4_hba_process_cq(cq);
James Smart1ba981f2014-02-20 09:56:45 -050014178}
14179
James Smart4f774512009-05-22 14:52:35 -040014180/**
James Smart67d12732012-08-03 12:36:13 -040014181 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040014182 * @irq: Interrupt number.
14183 * @dev_id: The device context pointer.
14184 *
14185 * This function is directly called from the PCI layer as an interrupt
14186 * service routine when device with SLI-4 interface spec is enabled with
14187 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
14188 * ring event in the HBA. However, when the device is enabled with either
14189 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14190 * device-level interrupt handler. When the PCI slot is in error recovery
14191 * or the HBA is undergoing initialization, the interrupt handler will not
14192 * process the interrupt. The SCSI FCP fast-path ring event are handled in
14193 * the intrrupt context. This function is called without any lock held.
14194 * It gets the hbalock to access and update SLI data structures. Note that,
14195 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14196 * equal to that of FCP CQ index.
14197 *
James Smart67d12732012-08-03 12:36:13 -040014198 * The link attention and ELS ring attention events are handled
14199 * by the worker thread. The interrupt handler signals the worker thread
14200 * and returns for these events. This function is called without any lock
14201 * held. It gets the hbalock to access and update SLI data structures.
14202 *
James Smart4f774512009-05-22 14:52:35 -040014203 * This function returns IRQ_HANDLED when interrupt is handled else it
14204 * returns IRQ_NONE.
14205 **/
14206irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040014207lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040014208{
14209 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080014210 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040014211 struct lpfc_queue *fpeq;
James Smart4f774512009-05-22 14:52:35 -040014212 unsigned long iflag;
14213 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080014214 int hba_eqidx;
James Smart32517fc2019-01-28 11:14:33 -080014215 struct lpfc_eq_intr_info *eqi;
14216 uint32_t icnt;
James Smart4f774512009-05-22 14:52:35 -040014217
14218 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080014219 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14220 phba = hba_eq_hdl->phba;
14221 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040014222
14223 if (unlikely(!phba))
14224 return IRQ_NONE;
James Smartcdb42be2019-01-28 11:14:21 -080014225 if (unlikely(!phba->sli4_hba.hdwq))
James Smart5350d872011-10-10 21:33:49 -040014226 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014227
14228 /* Get to the EQ struct associated with this vector */
James Smart657add42019-05-21 17:49:06 -070014229 fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
James Smart2e90f4b2011-12-13 13:22:37 -050014230 if (unlikely(!fpeq))
14231 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014232
14233 /* Check device state for handling interrupt */
14234 if (unlikely(lpfc_intr_state_check(phba))) {
14235 /* Check again for link_state with lock held */
14236 spin_lock_irqsave(&phba->hbalock, iflag);
14237 if (phba->link_state < LPFC_LINK_DOWN)
14238 /* Flush, clear interrupt, and rearm the EQ */
14239 lpfc_sli4_eq_flush(phba, fpeq);
14240 spin_unlock_irqrestore(&phba->hbalock, iflag);
14241 return IRQ_NONE;
14242 }
14243
James Smart32517fc2019-01-28 11:14:33 -080014244 eqi = phba->sli4_hba.eq_info;
14245 icnt = this_cpu_inc_return(eqi->icnt);
Bart Van Assched6d189c2019-03-28 11:06:22 -070014246 fpeq->last_cpu = raw_smp_processor_id();
James Smart4f774512009-05-22 14:52:35 -040014247
James Smart32517fc2019-01-28 11:14:33 -080014248 if (icnt > LPFC_EQD_ISR_TRIGGER &&
14249 phba->cfg_irq_chann == 1 &&
14250 phba->cfg_auto_imax &&
14251 fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
14252 phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
14253 lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
James Smartb84daac2012-08-03 12:35:13 -040014254
James Smart32517fc2019-01-28 11:14:33 -080014255 /* process and rearm the EQ */
14256 ecount = lpfc_sli4_process_eq(phba, fpeq);
James Smart4f774512009-05-22 14:52:35 -040014257
14258 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040014259 fpeq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040014260 if (phba->intr_type == MSIX)
14261 /* MSI-X treated interrupt served as no EQ share INT */
14262 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14263 "0358 MSI-X interrupt with no EQE\n");
14264 else
14265 /* Non MSI-X treated on interrupt as EQ share INT */
14266 return IRQ_NONE;
14267 }
14268
14269 return IRQ_HANDLED;
14270} /* lpfc_sli4_fp_intr_handler */
14271
14272/**
14273 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14274 * @irq: Interrupt number.
14275 * @dev_id: The device context pointer.
14276 *
14277 * This function is the device-level interrupt handler to device with SLI-4
14278 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14279 * interrupt mode is enabled and there is an event in the HBA which requires
14280 * driver attention. This function invokes the slow-path interrupt attention
14281 * handling function and fast-path interrupt attention handling function in
14282 * turn to process the relevant HBA attention events. This function is called
14283 * without any lock held. It gets the hbalock to access and update SLI data
14284 * structures.
14285 *
14286 * This function returns IRQ_HANDLED when interrupt is handled, else it
14287 * returns IRQ_NONE.
14288 **/
14289irqreturn_t
14290lpfc_sli4_intr_handler(int irq, void *dev_id)
14291{
14292 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040014293 irqreturn_t hba_irq_rc;
14294 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080014295 int qidx;
James Smart4f774512009-05-22 14:52:35 -040014296
14297 /* Get the driver's phba structure from the dev_id */
14298 phba = (struct lpfc_hba *)dev_id;
14299
14300 if (unlikely(!phba))
14301 return IRQ_NONE;
14302
14303 /*
James Smart4f774512009-05-22 14:52:35 -040014304 * Invoke fast-path host attention interrupt handling as appropriate.
14305 */
James Smart6a828b02019-01-28 11:14:31 -080014306 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040014307 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014308 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040014309 if (hba_irq_rc == IRQ_HANDLED)
14310 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040014311 }
14312
James Smart67d12732012-08-03 12:36:13 -040014313 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014314} /* lpfc_sli4_intr_handler */
14315
14316/**
14317 * lpfc_sli4_queue_free - free a queue structure and associated memory
14318 * @queue: The queue structure to free.
14319 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040014320 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040014321 * the host resident queue. This function must be called after destroying the
14322 * queue on the HBA.
14323 **/
14324void
14325lpfc_sli4_queue_free(struct lpfc_queue *queue)
14326{
14327 struct lpfc_dmabuf *dmabuf;
14328
14329 if (!queue)
14330 return;
14331
James Smart4645f7b2019-03-12 16:30:14 -070014332 if (!list_empty(&queue->wq_list))
14333 list_del(&queue->wq_list);
14334
James Smart4f774512009-05-22 14:52:35 -040014335 while (!list_empty(&queue->page_list)) {
14336 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14337 list);
James Smart81b96ed2017-11-20 16:00:29 -080014338 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040014339 dmabuf->virt, dmabuf->phys);
14340 kfree(dmabuf);
14341 }
James Smart895427b2017-02-12 13:52:30 -080014342 if (queue->rqbp) {
14343 lpfc_free_rq_buffer(queue->phba, queue);
14344 kfree(queue->rqbp);
14345 }
James Smartd1f525a2017-04-21 16:04:55 -070014346
James Smart32517fc2019-01-28 11:14:33 -080014347 if (!list_empty(&queue->cpu_list))
14348 list_del(&queue->cpu_list);
14349
James Smart4f774512009-05-22 14:52:35 -040014350 kfree(queue);
14351 return;
14352}
14353
14354/**
14355 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14356 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080014357 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040014358 * @entry_size: The size of each queue entry for this queue.
14359 * @entry count: The number of entries that this queue will handle.
James Smartc1a21eb2019-03-12 16:30:29 -070014360 * @cpu: The cpu that will primarily utilize this queue.
James Smart4f774512009-05-22 14:52:35 -040014361 *
14362 * This function allocates a queue structure and the DMAable memory used for
14363 * the host resident queue. This function must be called before creating the
14364 * queue on the HBA.
14365 **/
14366struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080014367lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
James Smartc1a21eb2019-03-12 16:30:29 -070014368 uint32_t entry_size, uint32_t entry_count, int cpu)
James Smart4f774512009-05-22 14:52:35 -040014369{
14370 struct lpfc_queue *queue;
14371 struct lpfc_dmabuf *dmabuf;
James Smartcb5172e2010-03-15 11:25:07 -040014372 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart9afbee32019-03-12 16:30:28 -070014373 uint16_t x, pgcnt;
James Smart4f774512009-05-22 14:52:35 -040014374
James Smartcb5172e2010-03-15 11:25:07 -040014375 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014376 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040014377
James Smart9afbee32019-03-12 16:30:28 -070014378 pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080014379
14380 /* If needed, Adjust page count to match the max the adapter supports */
James Smart9afbee32019-03-12 16:30:28 -070014381 if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
14382 pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
14383
James Smartc1a21eb2019-03-12 16:30:29 -070014384 queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),
14385 GFP_KERNEL, cpu_to_node(cpu));
James Smart9afbee32019-03-12 16:30:28 -070014386 if (!queue)
14387 return NULL;
James Smart895427b2017-02-12 13:52:30 -080014388
James Smart4f774512009-05-22 14:52:35 -040014389 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080014390 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080014391 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040014392 INIT_LIST_HEAD(&queue->page_list);
14393 INIT_LIST_HEAD(&queue->child_list);
James Smart32517fc2019-01-28 11:14:33 -080014394 INIT_LIST_HEAD(&queue->cpu_list);
James Smart81b96ed2017-11-20 16:00:29 -080014395
14396 /* Set queue parameters now. If the system cannot provide memory
14397 * resources, the free routine needs to know what was allocated.
14398 */
James Smart9afbee32019-03-12 16:30:28 -070014399 queue->page_count = pgcnt;
14400 queue->q_pgs = (void **)&queue[1];
14401 queue->entry_cnt_per_pg = hw_page_size / entry_size;
James Smart81b96ed2017-11-20 16:00:29 -080014402 queue->entry_size = entry_size;
14403 queue->entry_count = entry_count;
14404 queue->page_size = hw_page_size;
14405 queue->phba = phba;
14406
James Smart9afbee32019-03-12 16:30:28 -070014407 for (x = 0; x < queue->page_count; x++) {
James Smartc1a21eb2019-03-12 16:30:29 -070014408 dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
14409 dev_to_node(&phba->pcidev->dev));
James Smart4f774512009-05-22 14:52:35 -040014410 if (!dmabuf)
14411 goto out_fail;
Luis Chamberlain750afb02019-01-04 09:23:09 +010014412 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
14413 hw_page_size, &dmabuf->phys,
14414 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040014415 if (!dmabuf->virt) {
14416 kfree(dmabuf);
14417 goto out_fail;
14418 }
14419 dmabuf->buffer_tag = x;
14420 list_add_tail(&dmabuf->list, &queue->page_list);
James Smart9afbee32019-03-12 16:30:28 -070014421 /* use lpfc_sli4_qe to index a paritcular entry in this page */
14422 queue->q_pgs[x] = dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -040014423 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014424 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14425 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart32517fc2019-01-28 11:14:33 -080014426 INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
14427 INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014428
James Smart32517fc2019-01-28 11:14:33 -080014429 /* notify_interval will be set during q creation */
James Smart64eb4dc2017-05-15 15:20:49 -070014430
James Smart4f774512009-05-22 14:52:35 -040014431 return queue;
14432out_fail:
14433 lpfc_sli4_queue_free(queue);
14434 return NULL;
14435}
14436
14437/**
James Smart962bc512013-01-03 15:44:00 -050014438 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14439 * @phba: HBA structure that indicates port to create a queue on.
14440 * @pci_barset: PCI BAR set flag.
14441 *
14442 * This function shall perform iomap of the specified PCI BAR address to host
14443 * memory address if not already done so and return it. The returned host
14444 * memory address can be NULL.
14445 */
14446static void __iomem *
14447lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14448{
James Smart962bc512013-01-03 15:44:00 -050014449 if (!phba->pcidev)
14450 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014451
14452 switch (pci_barset) {
14453 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014454 return phba->pci_bar0_memmap_p;
14455 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014456 return phba->pci_bar2_memmap_p;
14457 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014458 return phba->pci_bar4_memmap_p;
14459 default:
14460 break;
14461 }
14462 return NULL;
14463}
14464
14465/**
James Smartcb733e32019-01-28 11:14:32 -080014466 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
14467 * @phba: HBA structure that EQs are on.
14468 * @startq: The starting EQ index to modify
14469 * @numq: The number of EQs (consecutive indexes) to modify
14470 * @usdelay: amount of delay
James Smart173edbb2012-06-12 13:54:50 -040014471 *
James Smartcb733e32019-01-28 11:14:32 -080014472 * This function revises the EQ delay on 1 or more EQs. The EQ delay
14473 * is set either by writing to a register (if supported by the SLI Port)
14474 * or by mailbox command. The mailbox command allows several EQs to be
14475 * updated at once.
James Smart173edbb2012-06-12 13:54:50 -040014476 *
James Smartcb733e32019-01-28 11:14:32 -080014477 * The @phba struct is used to send a mailbox command to HBA. The @startq
14478 * is used to get the starting EQ index to change. The @numq value is
14479 * used to specify how many consecutive EQ indexes, starting at EQ index,
14480 * are to be changed. This function is asynchronous and will wait for any
14481 * mailbox commands to finish before returning.
James Smart173edbb2012-06-12 13:54:50 -040014482 *
James Smartcb733e32019-01-28 11:14:32 -080014483 * On success this function will return a zero. If unable to allocate
14484 * enough memory this function will return -ENOMEM. If a mailbox command
14485 * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
14486 * have had their delay multipler changed.
James Smart173edbb2012-06-12 13:54:50 -040014487 **/
James Smartcb733e32019-01-28 11:14:32 -080014488void
James Smart0cf07f842017-06-01 21:07:10 -070014489lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
James Smartcb733e32019-01-28 11:14:32 -080014490 uint32_t numq, uint32_t usdelay)
James Smart173edbb2012-06-12 13:54:50 -040014491{
14492 struct lpfc_mbx_modify_eq_delay *eq_delay;
14493 LPFC_MBOXQ_t *mbox;
14494 struct lpfc_queue *eq;
James Smartcb733e32019-01-28 11:14:32 -080014495 int cnt = 0, rc, length;
James Smart173edbb2012-06-12 13:54:50 -040014496 uint32_t shdr_status, shdr_add_status;
James Smartcb733e32019-01-28 11:14:32 -080014497 uint32_t dmult;
James Smart895427b2017-02-12 13:52:30 -080014498 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014499 union lpfc_sli4_cfg_shdr *shdr;
James Smart173edbb2012-06-12 13:54:50 -040014500
James Smart6a828b02019-01-28 11:14:31 -080014501 if (startq >= phba->cfg_irq_chann)
James Smartcb733e32019-01-28 11:14:32 -080014502 return;
14503
14504 if (usdelay > 0xFFFF) {
14505 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
14506 "6429 usdelay %d too large. Scaled down to "
14507 "0xFFFF.\n", usdelay);
14508 usdelay = 0xFFFF;
14509 }
14510
14511 /* set values by EQ_DELAY register if supported */
14512 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14513 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070014514 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smartcb733e32019-01-28 11:14:32 -080014515 if (!eq)
14516 continue;
14517
James Smart32517fc2019-01-28 11:14:33 -080014518 lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
James Smartcb733e32019-01-28 11:14:32 -080014519
14520 if (++cnt >= numq)
14521 break;
14522 }
James Smartcb733e32019-01-28 11:14:32 -080014523 return;
14524 }
14525
14526 /* Otherwise, set values by mailbox cmd */
James Smart173edbb2012-06-12 13:54:50 -040014527
14528 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smartcb733e32019-01-28 11:14:32 -080014529 if (!mbox) {
14530 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_FCP | LOG_NVME,
14531 "6428 Failed allocating mailbox cmd buffer."
14532 " EQ delay was not set.\n");
14533 return;
14534 }
James Smart173edbb2012-06-12 13:54:50 -040014535 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14536 sizeof(struct lpfc_sli4_cfg_mhdr));
14537 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14538 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14539 length, LPFC_SLI4_MBX_EMBED);
14540 eq_delay = &mbox->u.mqe.un.eq_delay;
14541
14542 /* Calculate delay multiper from maximum interrupt per second */
James Smartcb733e32019-01-28 11:14:32 -080014543 dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
14544 if (dmult)
14545 dmult--;
James Smart0cf07f842017-06-01 21:07:10 -070014546 if (dmult > LPFC_DMULT_MAX)
14547 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014548
James Smart6a828b02019-01-28 11:14:31 -080014549 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070014550 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smart173edbb2012-06-12 13:54:50 -040014551 if (!eq)
14552 continue;
James Smartcb733e32019-01-28 11:14:32 -080014553 eq->q_mode = usdelay;
James Smart173edbb2012-06-12 13:54:50 -040014554 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14555 eq_delay->u.request.eq[cnt].phase = 0;
14556 eq_delay->u.request.eq[cnt].delay_multi = dmult;
James Smart0cf07f842017-06-01 21:07:10 -070014557
James Smartcb733e32019-01-28 11:14:32 -080014558 if (++cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014559 break;
14560 }
14561 eq_delay->u.request.num_eq = cnt;
14562
14563 mbox->vport = phba->pport;
14564 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080014565 mbox->ctx_buf = NULL;
14566 mbox->ctx_ndlp = NULL;
James Smart173edbb2012-06-12 13:54:50 -040014567 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14568 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14569 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14570 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14571 if (shdr_status || shdr_add_status || rc) {
14572 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14573 "2512 MODIFY_EQ_DELAY mailbox failed with "
14574 "status x%x add_status x%x, mbx status x%x\n",
14575 shdr_status, shdr_add_status, rc);
James Smart173edbb2012-06-12 13:54:50 -040014576 }
14577 mempool_free(mbox, phba->mbox_mem_pool);
James Smartcb733e32019-01-28 11:14:32 -080014578 return;
James Smart173edbb2012-06-12 13:54:50 -040014579}
14580
14581/**
James Smart4f774512009-05-22 14:52:35 -040014582 * lpfc_eq_create - Create an Event Queue on the HBA
14583 * @phba: HBA structure that indicates port to create a queue on.
14584 * @eq: The queue structure to use to create the event queue.
14585 * @imax: The maximum interrupt per second limit.
14586 *
14587 * This function creates an event queue, as detailed in @eq, on a port,
14588 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14589 *
14590 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14591 * is used to get the entry count and entry size that are necessary to
14592 * determine the number of pages to allocate and use for this queue. This
14593 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14594 * event queue. This function is asynchronous and will wait for the mailbox
14595 * command to finish before continuing.
14596 *
14597 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014598 * memory this function will return -ENOMEM. If the queue create mailbox command
14599 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014600 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014601int
James Smartee020062012-09-29 11:28:52 -040014602lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014603{
14604 struct lpfc_mbx_eq_create *eq_create;
14605 LPFC_MBOXQ_t *mbox;
14606 int rc, length, status = 0;
14607 struct lpfc_dmabuf *dmabuf;
14608 uint32_t shdr_status, shdr_add_status;
14609 union lpfc_sli4_cfg_shdr *shdr;
14610 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014611 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14612
James Smart2e90f4b2011-12-13 13:22:37 -050014613 /* sanity check on queue memory */
14614 if (!eq)
14615 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014616 if (!phba->sli4_hba.pc_sli4_params.supported)
14617 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014618
14619 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14620 if (!mbox)
14621 return -ENOMEM;
14622 length = (sizeof(struct lpfc_mbx_eq_create) -
14623 sizeof(struct lpfc_sli4_cfg_mhdr));
14624 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14625 LPFC_MBOX_OPCODE_EQ_CREATE,
14626 length, LPFC_SLI4_MBX_EMBED);
14627 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080014628 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014629 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14630 eq->page_count);
14631 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14632 LPFC_EQE_SIZE);
14633 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080014634
14635 /* Use version 2 of CREATE_EQ if eqav is set */
14636 if (phba->sli4_hba.pc_sli4_params.eqav) {
14637 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14638 LPFC_Q_CREATE_VERSION_2);
14639 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14640 phba->sli4_hba.pc_sli4_params.eqav);
14641 }
14642
James Smart2c9c5a02015-04-07 15:07:15 -040014643 /* don't setup delay multiplier using EQ_CREATE */
14644 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014645 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14646 dmult);
14647 switch (eq->entry_count) {
14648 default:
14649 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14650 "0360 Unsupported EQ count. (%d)\n",
14651 eq->entry_count);
James Smart04d210c2019-05-21 17:49:03 -070014652 if (eq->entry_count < 256) {
14653 status = -EINVAL;
14654 goto out;
14655 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014656 /* fall through - otherwise default to smallest count */
James Smart4f774512009-05-22 14:52:35 -040014657 case 256:
14658 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14659 LPFC_EQ_CNT_256);
14660 break;
14661 case 512:
14662 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14663 LPFC_EQ_CNT_512);
14664 break;
14665 case 1024:
14666 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14667 LPFC_EQ_CNT_1024);
14668 break;
14669 case 2048:
14670 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14671 LPFC_EQ_CNT_2048);
14672 break;
14673 case 4096:
14674 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14675 LPFC_EQ_CNT_4096);
14676 break;
14677 }
14678 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014679 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014680 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14681 putPaddrLow(dmabuf->phys);
14682 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14683 putPaddrHigh(dmabuf->phys);
14684 }
14685 mbox->vport = phba->pport;
14686 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080014687 mbox->ctx_buf = NULL;
14688 mbox->ctx_ndlp = NULL;
James Smart4f774512009-05-22 14:52:35 -040014689 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040014690 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14691 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14692 if (shdr_status || shdr_add_status || rc) {
14693 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14694 "2500 EQ_CREATE mailbox failed with "
14695 "status x%x add_status x%x, mbx status x%x\n",
14696 shdr_status, shdr_add_status, rc);
14697 status = -ENXIO;
14698 }
14699 eq->type = LPFC_EQ;
14700 eq->subtype = LPFC_NONE;
14701 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14702 if (eq->queue_id == 0xFFFF)
14703 status = -ENXIO;
14704 eq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080014705 eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
14706 eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
James Smart04d210c2019-05-21 17:49:03 -070014707out:
James Smart8fa38512009-07-19 10:01:03 -040014708 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014709 return status;
14710}
14711
14712/**
14713 * lpfc_cq_create - Create a Completion Queue on the HBA
14714 * @phba: HBA structure that indicates port to create a queue on.
14715 * @cq: The queue structure to use to create the completion queue.
14716 * @eq: The event queue to bind this completion queue to.
14717 *
14718 * This function creates a completion queue, as detailed in @wq, on a port,
14719 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14720 *
14721 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14722 * is used to get the entry count and entry size that are necessary to
14723 * determine the number of pages to allocate and use for this queue. The @eq
14724 * is used to indicate which event queue to bind this completion queue to. This
14725 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14726 * completion queue. This function is asynchronous and will wait for the mailbox
14727 * command to finish before continuing.
14728 *
14729 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014730 * memory this function will return -ENOMEM. If the queue create mailbox command
14731 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014732 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014733int
James Smart4f774512009-05-22 14:52:35 -040014734lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14735 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14736{
14737 struct lpfc_mbx_cq_create *cq_create;
14738 struct lpfc_dmabuf *dmabuf;
14739 LPFC_MBOXQ_t *mbox;
14740 int rc, length, status = 0;
14741 uint32_t shdr_status, shdr_add_status;
14742 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014743
James Smart2e90f4b2011-12-13 13:22:37 -050014744 /* sanity check on queue memory */
14745 if (!cq || !eq)
14746 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014747
James Smart4f774512009-05-22 14:52:35 -040014748 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14749 if (!mbox)
14750 return -ENOMEM;
14751 length = (sizeof(struct lpfc_mbx_cq_create) -
14752 sizeof(struct lpfc_sli4_cfg_mhdr));
14753 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14754 LPFC_MBOX_OPCODE_CQ_CREATE,
14755 length, LPFC_SLI4_MBX_EMBED);
14756 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014757 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014758 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14759 cq->page_count);
14760 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14761 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014762 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14763 phba->sli4_hba.pc_sli4_params.cqv);
14764 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080014765 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14766 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014767 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14768 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080014769 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14770 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050014771 } else {
14772 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14773 eq->queue_id);
14774 }
James Smart4f774512009-05-22 14:52:35 -040014775 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014776 case 2048:
14777 case 4096:
14778 if (phba->sli4_hba.pc_sli4_params.cqv ==
14779 LPFC_Q_CREATE_VERSION_2) {
14780 cq_create->u.request.context.lpfc_cq_context_count =
14781 cq->entry_count;
14782 bf_set(lpfc_cq_context_count,
14783 &cq_create->u.request.context,
14784 LPFC_CQ_CNT_WORD7);
14785 break;
14786 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014787 /* fall through */
James Smart4f774512009-05-22 14:52:35 -040014788 default:
14789 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014790 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014791 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014792 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014793 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014794 if (cq->entry_count < 256) {
14795 status = -EINVAL;
14796 goto out;
14797 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014798 /* fall through - otherwise default to smallest count */
James Smart4f774512009-05-22 14:52:35 -040014799 case 256:
14800 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14801 LPFC_CQ_CNT_256);
14802 break;
14803 case 512:
14804 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14805 LPFC_CQ_CNT_512);
14806 break;
14807 case 1024:
14808 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14809 LPFC_CQ_CNT_1024);
14810 break;
14811 }
14812 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080014813 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040014814 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14815 putPaddrLow(dmabuf->phys);
14816 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14817 putPaddrHigh(dmabuf->phys);
14818 }
14819 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14820
14821 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014822 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14823 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14824 if (shdr_status || shdr_add_status || rc) {
14825 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14826 "2501 CQ_CREATE mailbox failed with "
14827 "status x%x add_status x%x, mbx status x%x\n",
14828 shdr_status, shdr_add_status, rc);
14829 status = -ENXIO;
14830 goto out;
14831 }
14832 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14833 if (cq->queue_id == 0xFFFF) {
14834 status = -ENXIO;
14835 goto out;
14836 }
14837 /* link the cq onto the parent eq child list */
14838 list_add_tail(&cq->list, &eq->child_list);
14839 /* Set up completion queue's type and subtype */
14840 cq->type = type;
14841 cq->subtype = subtype;
14842 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014843 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080014844 cq->assoc_qp = eq;
James Smart4f774512009-05-22 14:52:35 -040014845 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080014846 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
14847 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
James Smart4f774512009-05-22 14:52:35 -040014848
James Smart6a828b02019-01-28 11:14:31 -080014849 if (cq->queue_id > phba->sli4_hba.cq_max)
14850 phba->sli4_hba.cq_max = cq->queue_id;
James Smart8fa38512009-07-19 10:01:03 -040014851out:
14852 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014853 return status;
14854}
14855
14856/**
James Smart2d7dbc42017-02-12 13:52:35 -080014857 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14858 * @phba: HBA structure that indicates port to create a queue on.
14859 * @cqp: The queue structure array to use to create the completion queues.
James Smartcdb42be2019-01-28 11:14:21 -080014860 * @hdwq: The hardware queue array with the EQ to bind completion queues to.
James Smart2d7dbc42017-02-12 13:52:35 -080014861 *
14862 * This function creates a set of completion queue, s to support MRQ
14863 * as detailed in @cqp, on a port,
14864 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14865 *
14866 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14867 * is used to get the entry count and entry size that are necessary to
14868 * determine the number of pages to allocate and use for this queue. The @eq
14869 * is used to indicate which event queue to bind this completion queue to. This
14870 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14871 * completion queue. This function is asynchronous and will wait for the mailbox
14872 * command to finish before continuing.
14873 *
14874 * On success this function will return a zero. If unable to allocate enough
14875 * memory this function will return -ENOMEM. If the queue create mailbox command
14876 * fails this function will return -ENXIO.
14877 **/
14878int
14879lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
James Smartcdb42be2019-01-28 11:14:21 -080014880 struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
14881 uint32_t subtype)
James Smart2d7dbc42017-02-12 13:52:35 -080014882{
14883 struct lpfc_queue *cq;
14884 struct lpfc_queue *eq;
14885 struct lpfc_mbx_cq_create_set *cq_set;
14886 struct lpfc_dmabuf *dmabuf;
14887 LPFC_MBOXQ_t *mbox;
14888 int rc, length, alloclen, status = 0;
14889 int cnt, idx, numcq, page_idx = 0;
14890 uint32_t shdr_status, shdr_add_status;
14891 union lpfc_sli4_cfg_shdr *shdr;
14892 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14893
14894 /* sanity check on queue memory */
14895 numcq = phba->cfg_nvmet_mrq;
James Smartcdb42be2019-01-28 11:14:21 -080014896 if (!cqp || !hdwq || !numcq)
James Smart2d7dbc42017-02-12 13:52:35 -080014897 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080014898
14899 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14900 if (!mbox)
14901 return -ENOMEM;
14902
14903 length = sizeof(struct lpfc_mbx_cq_create_set);
14904 length += ((numcq * cqp[0]->page_count) *
14905 sizeof(struct dma_address));
14906 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14907 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14908 LPFC_SLI4_MBX_NEMBED);
14909 if (alloclen < length) {
14910 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14911 "3098 Allocated DMA memory size (%d) is "
14912 "less than the requested DMA memory size "
14913 "(%d)\n", alloclen, length);
14914 status = -ENOMEM;
14915 goto out;
14916 }
14917 cq_set = mbox->sge_array->addr[0];
14918 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14919 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14920
14921 for (idx = 0; idx < numcq; idx++) {
14922 cq = cqp[idx];
James Smartcdb42be2019-01-28 11:14:21 -080014923 eq = hdwq[idx].hba_eq;
James Smart2d7dbc42017-02-12 13:52:35 -080014924 if (!cq || !eq) {
14925 status = -ENOMEM;
14926 goto out;
14927 }
James Smart81b96ed2017-11-20 16:00:29 -080014928 if (!phba->sli4_hba.pc_sli4_params.supported)
14929 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080014930
14931 switch (idx) {
14932 case 0:
14933 bf_set(lpfc_mbx_cq_create_set_page_size,
14934 &cq_set->u.request,
14935 (hw_page_size / SLI4_PAGE_SIZE));
14936 bf_set(lpfc_mbx_cq_create_set_num_pages,
14937 &cq_set->u.request, cq->page_count);
14938 bf_set(lpfc_mbx_cq_create_set_evt,
14939 &cq_set->u.request, 1);
14940 bf_set(lpfc_mbx_cq_create_set_valid,
14941 &cq_set->u.request, 1);
14942 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14943 &cq_set->u.request, 0);
14944 bf_set(lpfc_mbx_cq_create_set_num_cq,
14945 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080014946 bf_set(lpfc_mbx_cq_create_set_autovalid,
14947 &cq_set->u.request,
14948 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080014949 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014950 case 2048:
14951 case 4096:
14952 if (phba->sli4_hba.pc_sli4_params.cqv ==
14953 LPFC_Q_CREATE_VERSION_2) {
14954 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14955 &cq_set->u.request,
14956 cq->entry_count);
14957 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14958 &cq_set->u.request,
14959 LPFC_CQ_CNT_WORD7);
14960 break;
14961 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014962 /* fall through */
James Smart2d7dbc42017-02-12 13:52:35 -080014963 default:
14964 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14965 "3118 Bad CQ count. (%d)\n",
14966 cq->entry_count);
14967 if (cq->entry_count < 256) {
14968 status = -EINVAL;
14969 goto out;
14970 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014971 /* fall through - otherwise default to smallest */
James Smart2d7dbc42017-02-12 13:52:35 -080014972 case 256:
14973 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14974 &cq_set->u.request, LPFC_CQ_CNT_256);
14975 break;
14976 case 512:
14977 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14978 &cq_set->u.request, LPFC_CQ_CNT_512);
14979 break;
14980 case 1024:
14981 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14982 &cq_set->u.request, LPFC_CQ_CNT_1024);
14983 break;
14984 }
14985 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14986 &cq_set->u.request, eq->queue_id);
14987 break;
14988 case 1:
14989 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14990 &cq_set->u.request, eq->queue_id);
14991 break;
14992 case 2:
14993 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14994 &cq_set->u.request, eq->queue_id);
14995 break;
14996 case 3:
14997 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14998 &cq_set->u.request, eq->queue_id);
14999 break;
15000 case 4:
15001 bf_set(lpfc_mbx_cq_create_set_eq_id4,
15002 &cq_set->u.request, eq->queue_id);
15003 break;
15004 case 5:
15005 bf_set(lpfc_mbx_cq_create_set_eq_id5,
15006 &cq_set->u.request, eq->queue_id);
15007 break;
15008 case 6:
15009 bf_set(lpfc_mbx_cq_create_set_eq_id6,
15010 &cq_set->u.request, eq->queue_id);
15011 break;
15012 case 7:
15013 bf_set(lpfc_mbx_cq_create_set_eq_id7,
15014 &cq_set->u.request, eq->queue_id);
15015 break;
15016 case 8:
15017 bf_set(lpfc_mbx_cq_create_set_eq_id8,
15018 &cq_set->u.request, eq->queue_id);
15019 break;
15020 case 9:
15021 bf_set(lpfc_mbx_cq_create_set_eq_id9,
15022 &cq_set->u.request, eq->queue_id);
15023 break;
15024 case 10:
15025 bf_set(lpfc_mbx_cq_create_set_eq_id10,
15026 &cq_set->u.request, eq->queue_id);
15027 break;
15028 case 11:
15029 bf_set(lpfc_mbx_cq_create_set_eq_id11,
15030 &cq_set->u.request, eq->queue_id);
15031 break;
15032 case 12:
15033 bf_set(lpfc_mbx_cq_create_set_eq_id12,
15034 &cq_set->u.request, eq->queue_id);
15035 break;
15036 case 13:
15037 bf_set(lpfc_mbx_cq_create_set_eq_id13,
15038 &cq_set->u.request, eq->queue_id);
15039 break;
15040 case 14:
15041 bf_set(lpfc_mbx_cq_create_set_eq_id14,
15042 &cq_set->u.request, eq->queue_id);
15043 break;
15044 case 15:
15045 bf_set(lpfc_mbx_cq_create_set_eq_id15,
15046 &cq_set->u.request, eq->queue_id);
15047 break;
15048 }
15049
15050 /* link the cq onto the parent eq child list */
15051 list_add_tail(&cq->list, &eq->child_list);
15052 /* Set up completion queue's type and subtype */
15053 cq->type = type;
15054 cq->subtype = subtype;
15055 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080015056 cq->assoc_qp = eq;
James Smart2d7dbc42017-02-12 13:52:35 -080015057 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015058 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
15059 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
15060 cq->entry_count);
James Smart81b96ed2017-11-20 16:00:29 -080015061 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080015062
15063 rc = 0;
15064 list_for_each_entry(dmabuf, &cq->page_list, list) {
15065 memset(dmabuf->virt, 0, hw_page_size);
15066 cnt = page_idx + dmabuf->buffer_tag;
15067 cq_set->u.request.page[cnt].addr_lo =
15068 putPaddrLow(dmabuf->phys);
15069 cq_set->u.request.page[cnt].addr_hi =
15070 putPaddrHigh(dmabuf->phys);
15071 rc++;
15072 }
15073 page_idx += rc;
15074 }
15075
15076 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15077
15078 /* The IOCTL status is embedded in the mailbox subheader. */
15079 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15080 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15081 if (shdr_status || shdr_add_status || rc) {
15082 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15083 "3119 CQ_CREATE_SET mailbox failed with "
15084 "status x%x add_status x%x, mbx status x%x\n",
15085 shdr_status, shdr_add_status, rc);
15086 status = -ENXIO;
15087 goto out;
15088 }
15089 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
15090 if (rc == 0xFFFF) {
15091 status = -ENXIO;
15092 goto out;
15093 }
15094
15095 for (idx = 0; idx < numcq; idx++) {
15096 cq = cqp[idx];
15097 cq->queue_id = rc + idx;
James Smart6a828b02019-01-28 11:14:31 -080015098 if (cq->queue_id > phba->sli4_hba.cq_max)
15099 phba->sli4_hba.cq_max = cq->queue_id;
James Smart2d7dbc42017-02-12 13:52:35 -080015100 }
15101
15102out:
15103 lpfc_sli4_mbox_cmd_free(phba, mbox);
15104 return status;
15105}
15106
15107/**
James Smartb19a0612010-04-06 14:48:51 -040015108 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040015109 * @phba: HBA structure that indicates port to create a queue on.
15110 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040015111 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
15112 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040015113 *
James Smartb19a0612010-04-06 14:48:51 -040015114 * This function provides failback (fb) functionality when the
15115 * mq_create_ext fails on older FW generations. It's purpose is identical
15116 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040015117 *
James Smartb19a0612010-04-06 14:48:51 -040015118 * This routine cannot fail as all attributes were previously accessed and
15119 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040015120 **/
James Smartb19a0612010-04-06 14:48:51 -040015121static void
15122lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
15123 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040015124{
15125 struct lpfc_mbx_mq_create *mq_create;
15126 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040015127 int length;
James Smart04c68492009-05-22 14:52:52 -040015128
James Smart04c68492009-05-22 14:52:52 -040015129 length = (sizeof(struct lpfc_mbx_mq_create) -
15130 sizeof(struct lpfc_sli4_cfg_mhdr));
15131 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15132 LPFC_MBOX_OPCODE_MQ_CREATE,
15133 length, LPFC_SLI4_MBX_EMBED);
15134 mq_create = &mbox->u.mqe.un.mq_create;
15135 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040015136 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040015137 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040015138 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040015139 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
15140 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040015141 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015142 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15143 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040015144 break;
15145 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015146 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15147 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040015148 break;
15149 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015150 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15151 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040015152 break;
15153 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015154 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15155 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040015156 break;
15157 }
15158 list_for_each_entry(dmabuf, &mq->page_list, list) {
15159 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040015160 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040015161 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040015162 putPaddrHigh(dmabuf->phys);
15163 }
15164}
15165
15166/**
15167 * lpfc_mq_create - Create a mailbox Queue on the HBA
15168 * @phba: HBA structure that indicates port to create a queue on.
15169 * @mq: The queue structure to use to create the mailbox queue.
15170 * @cq: The completion queue to associate with this cq.
15171 * @subtype: The queue's subtype.
15172 *
15173 * This function creates a mailbox queue, as detailed in @mq, on a port,
15174 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
15175 *
15176 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15177 * is used to get the entry count and entry size that are necessary to
15178 * determine the number of pages to allocate and use for this queue. This
15179 * function will send the MQ_CREATE mailbox command to the HBA to setup the
15180 * mailbox queue. This function is asynchronous and will wait for the mailbox
15181 * command to finish before continuing.
15182 *
15183 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015184 * memory this function will return -ENOMEM. If the queue create mailbox command
15185 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040015186 **/
15187int32_t
15188lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15189 struct lpfc_queue *cq, uint32_t subtype)
15190{
15191 struct lpfc_mbx_mq_create *mq_create;
15192 struct lpfc_mbx_mq_create_ext *mq_create_ext;
15193 struct lpfc_dmabuf *dmabuf;
15194 LPFC_MBOXQ_t *mbox;
15195 int rc, length, status = 0;
15196 uint32_t shdr_status, shdr_add_status;
15197 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015198 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040015199
James Smart2e90f4b2011-12-13 13:22:37 -050015200 /* sanity check on queue memory */
15201 if (!mq || !cq)
15202 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015203 if (!phba->sli4_hba.pc_sli4_params.supported)
15204 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040015205
15206 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15207 if (!mbox)
15208 return -ENOMEM;
15209 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15210 sizeof(struct lpfc_sli4_cfg_mhdr));
15211 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15212 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15213 length, LPFC_SLI4_MBX_EMBED);
15214
15215 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050015216 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050015217 bf_set(lpfc_mbx_mq_create_ext_num_pages,
15218 &mq_create_ext->u.request, mq->page_count);
15219 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15220 &mq_create_ext->u.request, 1);
15221 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040015222 &mq_create_ext->u.request, 1);
15223 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15224 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050015225 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15226 &mq_create_ext->u.request, 1);
15227 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15228 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040015229 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050015230 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15231 phba->sli4_hba.pc_sli4_params.mqv);
15232 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15233 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15234 cq->queue_id);
15235 else
15236 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15237 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040015238 switch (mq->entry_count) {
15239 default:
15240 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15241 "0362 Unsupported MQ count. (%d)\n",
15242 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015243 if (mq->entry_count < 16) {
15244 status = -EINVAL;
15245 goto out;
15246 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015247 /* fall through - otherwise default to smallest count */
James Smartb19a0612010-04-06 14:48:51 -040015248 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015249 bf_set(lpfc_mq_context_ring_size,
15250 &mq_create_ext->u.request.context,
15251 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040015252 break;
15253 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015254 bf_set(lpfc_mq_context_ring_size,
15255 &mq_create_ext->u.request.context,
15256 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040015257 break;
15258 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015259 bf_set(lpfc_mq_context_ring_size,
15260 &mq_create_ext->u.request.context,
15261 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040015262 break;
15263 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015264 bf_set(lpfc_mq_context_ring_size,
15265 &mq_create_ext->u.request.context,
15266 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040015267 break;
15268 }
15269 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015270 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040015271 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15272 putPaddrLow(dmabuf->phys);
15273 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040015274 putPaddrHigh(dmabuf->phys);
15275 }
15276 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040015277 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15278 &mq_create_ext->u.response);
15279 if (rc != MBX_SUCCESS) {
15280 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15281 "2795 MQ_CREATE_EXT failed with "
15282 "status x%x. Failback to MQ_CREATE.\n",
15283 rc);
15284 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15285 mq_create = &mbox->u.mqe.un.mq_create;
15286 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15287 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15288 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15289 &mq_create->u.response);
15290 }
15291
James Smart04c68492009-05-22 14:52:52 -040015292 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040015293 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15294 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15295 if (shdr_status || shdr_add_status || rc) {
15296 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15297 "2502 MQ_CREATE mailbox failed with "
15298 "status x%x add_status x%x, mbx status x%x\n",
15299 shdr_status, shdr_add_status, rc);
15300 status = -ENXIO;
15301 goto out;
15302 }
James Smart04c68492009-05-22 14:52:52 -040015303 if (mq->queue_id == 0xFFFF) {
15304 status = -ENXIO;
15305 goto out;
15306 }
15307 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050015308 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040015309 mq->subtype = subtype;
15310 mq->host_index = 0;
15311 mq->hba_index = 0;
15312
15313 /* link the mq onto the parent cq child list */
15314 list_add_tail(&mq->list, &cq->child_list);
15315out:
James Smart8fa38512009-07-19 10:01:03 -040015316 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015317 return status;
15318}
15319
15320/**
James Smart4f774512009-05-22 14:52:35 -040015321 * lpfc_wq_create - Create a Work Queue on the HBA
15322 * @phba: HBA structure that indicates port to create a queue on.
15323 * @wq: The queue structure to use to create the work queue.
15324 * @cq: The completion queue to bind this work queue to.
15325 * @subtype: The subtype of the work queue indicating its functionality.
15326 *
15327 * This function creates a work queue, as detailed in @wq, on a port, described
15328 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15329 *
15330 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15331 * is used to get the entry count and entry size that are necessary to
15332 * determine the number of pages to allocate and use for this queue. The @cq
15333 * is used to indicate which completion queue to bind this work queue to. This
15334 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15335 * work queue. This function is asynchronous and will wait for the mailbox
15336 * command to finish before continuing.
15337 *
15338 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015339 * memory this function will return -ENOMEM. If the queue create mailbox command
15340 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015341 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015342int
James Smart4f774512009-05-22 14:52:35 -040015343lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15344 struct lpfc_queue *cq, uint32_t subtype)
15345{
15346 struct lpfc_mbx_wq_create *wq_create;
15347 struct lpfc_dmabuf *dmabuf;
15348 LPFC_MBOXQ_t *mbox;
15349 int rc, length, status = 0;
15350 uint32_t shdr_status, shdr_add_status;
15351 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015352 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050015353 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050015354 void __iomem *bar_memmap_p;
15355 uint32_t db_offset;
15356 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080015357 uint8_t dpp_barset;
15358 uint32_t dpp_offset;
15359 unsigned long pg_addr;
James Smart81b96ed2017-11-20 16:00:29 -080015360 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040015361
James Smart2e90f4b2011-12-13 13:22:37 -050015362 /* sanity check on queue memory */
15363 if (!wq || !cq)
15364 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015365 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015366 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040015367
15368 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15369 if (!mbox)
15370 return -ENOMEM;
15371 length = (sizeof(struct lpfc_mbx_wq_create) -
15372 sizeof(struct lpfc_sli4_cfg_mhdr));
15373 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15374 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15375 length, LPFC_SLI4_MBX_EMBED);
15376 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015377 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015378 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15379 wq->page_count);
15380 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15381 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040015382
15383 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050015384 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15385 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050015386
James Smartc176ffa2018-01-30 15:58:46 -080015387 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15388 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080015389 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15390 else
15391 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15392
James Smart0c651872013-07-15 18:33:23 -040015393
James Smart1351e692018-02-22 08:18:43 -080015394 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15395 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15396 else
15397 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15398
15399 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040015400 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050015401 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15402 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070015403 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15404 LPFC_Q_CREATE_VERSION_1);
15405
James Smart5a6f1332011-03-11 16:05:35 -050015406 switch (wq->entry_size) {
15407 default:
15408 case 64:
15409 bf_set(lpfc_mbx_wq_create_wqe_size,
15410 &wq_create->u.request_1,
15411 LPFC_WQ_WQE_SIZE_64);
15412 break;
15413 case 128:
15414 bf_set(lpfc_mbx_wq_create_wqe_size,
15415 &wq_create->u.request_1,
15416 LPFC_WQ_WQE_SIZE_128);
15417 break;
15418 }
James Smart1351e692018-02-22 08:18:43 -080015419 /* Request DPP by default */
15420 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080015421 bf_set(lpfc_mbx_wq_create_page_size,
15422 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015423 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015424 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015425 break;
15426 default:
James Smart1351e692018-02-22 08:18:43 -080015427 page = wq_create->u.request.page;
15428 break;
James Smart5a6f1332011-03-11 16:05:35 -050015429 }
James Smart0c651872013-07-15 18:33:23 -040015430
James Smart4f774512009-05-22 14:52:35 -040015431 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015432 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015433 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15434 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015435 }
James Smart962bc512013-01-03 15:44:00 -050015436
15437 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15438 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15439
James Smart4f774512009-05-22 14:52:35 -040015440 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15441 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015442 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15443 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15444 if (shdr_status || shdr_add_status || rc) {
15445 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15446 "2503 WQ_CREATE mailbox failed with "
15447 "status x%x add_status x%x, mbx status x%x\n",
15448 shdr_status, shdr_add_status, rc);
15449 status = -ENXIO;
15450 goto out;
15451 }
James Smart1351e692018-02-22 08:18:43 -080015452
15453 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15454 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15455 &wq_create->u.response);
15456 else
15457 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15458 &wq_create->u.response_1);
15459
James Smart4f774512009-05-22 14:52:35 -040015460 if (wq->queue_id == 0xFFFF) {
15461 status = -ENXIO;
15462 goto out;
15463 }
James Smart1351e692018-02-22 08:18:43 -080015464
15465 wq->db_format = LPFC_DB_LIST_FORMAT;
15466 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15467 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15468 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15469 &wq_create->u.response);
15470 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15471 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15472 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15473 "3265 WQ[%d] doorbell format "
15474 "not supported: x%x\n",
15475 wq->queue_id, wq->db_format);
15476 status = -EINVAL;
15477 goto out;
15478 }
15479 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15480 &wq_create->u.response);
15481 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15482 pci_barset);
15483 if (!bar_memmap_p) {
15484 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15485 "3263 WQ[%d] failed to memmap "
15486 "pci barset:x%x\n",
15487 wq->queue_id, pci_barset);
15488 status = -ENOMEM;
15489 goto out;
15490 }
15491 db_offset = wq_create->u.response.doorbell_offset;
15492 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15493 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15494 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15495 "3252 WQ[%d] doorbell offset "
15496 "not supported: x%x\n",
15497 wq->queue_id, db_offset);
15498 status = -EINVAL;
15499 goto out;
15500 }
15501 wq->db_regaddr = bar_memmap_p + db_offset;
15502 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15503 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15504 "format:x%x\n", wq->queue_id,
15505 pci_barset, db_offset, wq->db_format);
15506 } else
15507 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015508 } else {
James Smart1351e692018-02-22 08:18:43 -080015509 /* Check if DPP was honored by the firmware */
15510 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15511 &wq_create->u.response_1);
15512 if (wq->dpp_enable) {
15513 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15514 &wq_create->u.response_1);
15515 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15516 pci_barset);
15517 if (!bar_memmap_p) {
15518 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15519 "3267 WQ[%d] failed to memmap "
15520 "pci barset:x%x\n",
15521 wq->queue_id, pci_barset);
15522 status = -ENOMEM;
15523 goto out;
15524 }
15525 db_offset = wq_create->u.response_1.doorbell_offset;
15526 wq->db_regaddr = bar_memmap_p + db_offset;
15527 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15528 &wq_create->u.response_1);
15529 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15530 &wq_create->u.response_1);
15531 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15532 dpp_barset);
15533 if (!bar_memmap_p) {
15534 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15535 "3268 WQ[%d] failed to memmap "
15536 "pci barset:x%x\n",
15537 wq->queue_id, dpp_barset);
15538 status = -ENOMEM;
15539 goto out;
15540 }
15541 dpp_offset = wq_create->u.response_1.dpp_offset;
15542 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15543 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15544 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15545 "dpp_id:x%x dpp_barset:x%x "
15546 "dpp_offset:x%x\n",
15547 wq->queue_id, pci_barset, db_offset,
15548 wq->dpp_id, dpp_barset, dpp_offset);
15549
15550 /* Enable combined writes for DPP aperture */
15551 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15552#ifdef CONFIG_X86
15553 rc = set_memory_wc(pg_addr, 1);
15554 if (rc) {
15555 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15556 "3272 Cannot setup Combined "
15557 "Write on WQ[%d] - disable DPP\n",
15558 wq->queue_id);
15559 phba->cfg_enable_dpp = 0;
15560 }
15561#else
15562 phba->cfg_enable_dpp = 0;
15563#endif
15564 } else
15565 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015566 }
James Smart895427b2017-02-12 13:52:30 -080015567 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15568 if (wq->pring == NULL) {
15569 status = -ENOMEM;
15570 goto out;
15571 }
James Smart4f774512009-05-22 14:52:35 -040015572 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015573 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015574 wq->subtype = subtype;
15575 wq->host_index = 0;
15576 wq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015577 wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040015578
15579 /* link the wq onto the parent cq child list */
15580 list_add_tail(&wq->list, &cq->child_list);
15581out:
James Smart8fa38512009-07-19 10:01:03 -040015582 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015583 return status;
15584}
15585
15586/**
15587 * lpfc_rq_create - Create a Receive Queue on the HBA
15588 * @phba: HBA structure that indicates port to create a queue on.
15589 * @hrq: The queue structure to use to create the header receive queue.
15590 * @drq: The queue structure to use to create the data receive queue.
15591 * @cq: The completion queue to bind this work queue to.
15592 *
15593 * This function creates a receive buffer queue pair , as detailed in @hrq and
15594 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15595 * to the HBA.
15596 *
15597 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15598 * struct is used to get the entry count that is necessary to determine the
15599 * number of pages to use for this queue. The @cq is used to indicate which
15600 * completion queue to bind received buffers that are posted to these queues to.
15601 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15602 * receive queue pair. This function is asynchronous and will wait for the
15603 * mailbox command to finish before continuing.
15604 *
15605 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015606 * memory this function will return -ENOMEM. If the queue create mailbox command
15607 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015608 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015609int
James Smart4f774512009-05-22 14:52:35 -040015610lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15611 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15612{
15613 struct lpfc_mbx_rq_create *rq_create;
15614 struct lpfc_dmabuf *dmabuf;
15615 LPFC_MBOXQ_t *mbox;
15616 int rc, length, status = 0;
15617 uint32_t shdr_status, shdr_add_status;
15618 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015619 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015620 void __iomem *bar_memmap_p;
15621 uint32_t db_offset;
15622 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015623
James Smart2e90f4b2011-12-13 13:22:37 -050015624 /* sanity check on queue memory */
15625 if (!hrq || !drq || !cq)
15626 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015627 if (!phba->sli4_hba.pc_sli4_params.supported)
15628 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015629
15630 if (hrq->entry_count != drq->entry_count)
15631 return -EINVAL;
15632 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15633 if (!mbox)
15634 return -ENOMEM;
15635 length = (sizeof(struct lpfc_mbx_rq_create) -
15636 sizeof(struct lpfc_sli4_cfg_mhdr));
15637 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15638 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15639 length, LPFC_SLI4_MBX_EMBED);
15640 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015641 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15642 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15643 phba->sli4_hba.pc_sli4_params.rqv);
15644 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15645 bf_set(lpfc_rq_context_rqe_count_1,
15646 &rq_create->u.request.context,
15647 hrq->entry_count);
15648 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015649 bf_set(lpfc_rq_context_rqe_size,
15650 &rq_create->u.request.context,
15651 LPFC_RQE_SIZE_8);
15652 bf_set(lpfc_rq_context_page_size,
15653 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015654 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015655 } else {
15656 switch (hrq->entry_count) {
15657 default:
15658 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15659 "2535 Unsupported RQ count. (%d)\n",
15660 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015661 if (hrq->entry_count < 512) {
15662 status = -EINVAL;
15663 goto out;
15664 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015665 /* fall through - otherwise default to smallest count */
James Smart5a6f1332011-03-11 16:05:35 -050015666 case 512:
15667 bf_set(lpfc_rq_context_rqe_count,
15668 &rq_create->u.request.context,
15669 LPFC_RQ_RING_SIZE_512);
15670 break;
15671 case 1024:
15672 bf_set(lpfc_rq_context_rqe_count,
15673 &rq_create->u.request.context,
15674 LPFC_RQ_RING_SIZE_1024);
15675 break;
15676 case 2048:
15677 bf_set(lpfc_rq_context_rqe_count,
15678 &rq_create->u.request.context,
15679 LPFC_RQ_RING_SIZE_2048);
15680 break;
15681 case 4096:
15682 bf_set(lpfc_rq_context_rqe_count,
15683 &rq_create->u.request.context,
15684 LPFC_RQ_RING_SIZE_4096);
15685 break;
15686 }
15687 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15688 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015689 }
15690 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15691 cq->queue_id);
15692 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15693 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015694 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015695 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015696 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15697 putPaddrLow(dmabuf->phys);
15698 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15699 putPaddrHigh(dmabuf->phys);
15700 }
James Smart962bc512013-01-03 15:44:00 -050015701 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15702 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15703
James Smart4f774512009-05-22 14:52:35 -040015704 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15705 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015706 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15707 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15708 if (shdr_status || shdr_add_status || rc) {
15709 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15710 "2504 RQ_CREATE mailbox failed with "
15711 "status x%x add_status x%x, mbx status x%x\n",
15712 shdr_status, shdr_add_status, rc);
15713 status = -ENXIO;
15714 goto out;
15715 }
15716 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15717 if (hrq->queue_id == 0xFFFF) {
15718 status = -ENXIO;
15719 goto out;
15720 }
James Smart962bc512013-01-03 15:44:00 -050015721
15722 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15723 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15724 &rq_create->u.response);
15725 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15726 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15728 "3262 RQ [%d] doorbell format not "
15729 "supported: x%x\n", hrq->queue_id,
15730 hrq->db_format);
15731 status = -EINVAL;
15732 goto out;
15733 }
15734
15735 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15736 &rq_create->u.response);
15737 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15738 if (!bar_memmap_p) {
15739 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15740 "3269 RQ[%d] failed to memmap pci "
15741 "barset:x%x\n", hrq->queue_id,
15742 pci_barset);
15743 status = -ENOMEM;
15744 goto out;
15745 }
15746
15747 db_offset = rq_create->u.response.doorbell_offset;
15748 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15749 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15750 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15751 "3270 RQ[%d] doorbell offset not "
15752 "supported: x%x\n", hrq->queue_id,
15753 db_offset);
15754 status = -EINVAL;
15755 goto out;
15756 }
15757 hrq->db_regaddr = bar_memmap_p + db_offset;
15758 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015759 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15760 "format:x%x\n", hrq->queue_id, pci_barset,
15761 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015762 } else {
15763 hrq->db_format = LPFC_DB_RING_FORMAT;
15764 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15765 }
James Smart4f774512009-05-22 14:52:35 -040015766 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015767 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015768 hrq->subtype = subtype;
15769 hrq->host_index = 0;
15770 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015771 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040015772
15773 /* now create the data queue */
15774 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15775 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15776 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015777 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15778 phba->sli4_hba.pc_sli4_params.rqv);
15779 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15780 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015781 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015782 if (subtype == LPFC_NVMET)
15783 rq_create->u.request.context.buffer_size =
15784 LPFC_NVMET_DATA_BUF_SIZE;
15785 else
15786 rq_create->u.request.context.buffer_size =
15787 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015788 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15789 LPFC_RQE_SIZE_8);
15790 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15791 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015792 } else {
15793 switch (drq->entry_count) {
15794 default:
15795 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15796 "2536 Unsupported RQ count. (%d)\n",
15797 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015798 if (drq->entry_count < 512) {
15799 status = -EINVAL;
15800 goto out;
15801 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015802 /* fall through - otherwise default to smallest count */
James Smart5a6f1332011-03-11 16:05:35 -050015803 case 512:
15804 bf_set(lpfc_rq_context_rqe_count,
15805 &rq_create->u.request.context,
15806 LPFC_RQ_RING_SIZE_512);
15807 break;
15808 case 1024:
15809 bf_set(lpfc_rq_context_rqe_count,
15810 &rq_create->u.request.context,
15811 LPFC_RQ_RING_SIZE_1024);
15812 break;
15813 case 2048:
15814 bf_set(lpfc_rq_context_rqe_count,
15815 &rq_create->u.request.context,
15816 LPFC_RQ_RING_SIZE_2048);
15817 break;
15818 case 4096:
15819 bf_set(lpfc_rq_context_rqe_count,
15820 &rq_create->u.request.context,
15821 LPFC_RQ_RING_SIZE_4096);
15822 break;
15823 }
James Smart3c603be2017-05-15 15:20:44 -070015824 if (subtype == LPFC_NVMET)
15825 bf_set(lpfc_rq_context_buf_size,
15826 &rq_create->u.request.context,
15827 LPFC_NVMET_DATA_BUF_SIZE);
15828 else
15829 bf_set(lpfc_rq_context_buf_size,
15830 &rq_create->u.request.context,
15831 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015832 }
15833 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15834 cq->queue_id);
15835 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15836 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015837 list_for_each_entry(dmabuf, &drq->page_list, list) {
15838 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15839 putPaddrLow(dmabuf->phys);
15840 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15841 putPaddrHigh(dmabuf->phys);
15842 }
James Smart962bc512013-01-03 15:44:00 -050015843 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15844 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015845 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15846 /* The IOCTL status is embedded in the mailbox subheader. */
15847 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15848 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15849 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15850 if (shdr_status || shdr_add_status || rc) {
15851 status = -ENXIO;
15852 goto out;
15853 }
15854 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15855 if (drq->queue_id == 0xFFFF) {
15856 status = -ENXIO;
15857 goto out;
15858 }
15859 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015860 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015861 drq->subtype = subtype;
15862 drq->host_index = 0;
15863 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015864 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040015865
15866 /* link the header and data RQs onto the parent cq child list */
15867 list_add_tail(&hrq->list, &cq->child_list);
15868 list_add_tail(&drq->list, &cq->child_list);
15869
15870out:
James Smart8fa38512009-07-19 10:01:03 -040015871 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015872 return status;
15873}
15874
15875/**
James Smart2d7dbc42017-02-12 13:52:35 -080015876 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15877 * @phba: HBA structure that indicates port to create a queue on.
15878 * @hrqp: The queue structure array to use to create the header receive queues.
15879 * @drqp: The queue structure array to use to create the data receive queues.
15880 * @cqp: The completion queue array to bind these receive queues to.
15881 *
15882 * This function creates a receive buffer queue pair , as detailed in @hrq and
15883 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15884 * to the HBA.
15885 *
15886 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15887 * struct is used to get the entry count that is necessary to determine the
15888 * number of pages to use for this queue. The @cq is used to indicate which
15889 * completion queue to bind received buffers that are posted to these queues to.
15890 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15891 * receive queue pair. This function is asynchronous and will wait for the
15892 * mailbox command to finish before continuing.
15893 *
15894 * On success this function will return a zero. If unable to allocate enough
15895 * memory this function will return -ENOMEM. If the queue create mailbox command
15896 * fails this function will return -ENXIO.
15897 **/
15898int
15899lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15900 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15901 uint32_t subtype)
15902{
15903 struct lpfc_queue *hrq, *drq, *cq;
15904 struct lpfc_mbx_rq_create_v2 *rq_create;
15905 struct lpfc_dmabuf *dmabuf;
15906 LPFC_MBOXQ_t *mbox;
15907 int rc, length, alloclen, status = 0;
15908 int cnt, idx, numrq, page_idx = 0;
15909 uint32_t shdr_status, shdr_add_status;
15910 union lpfc_sli4_cfg_shdr *shdr;
15911 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15912
15913 numrq = phba->cfg_nvmet_mrq;
15914 /* sanity check on array memory */
15915 if (!hrqp || !drqp || !cqp || !numrq)
15916 return -ENODEV;
15917 if (!phba->sli4_hba.pc_sli4_params.supported)
15918 hw_page_size = SLI4_PAGE_SIZE;
15919
15920 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15921 if (!mbox)
15922 return -ENOMEM;
15923
15924 length = sizeof(struct lpfc_mbx_rq_create_v2);
15925 length += ((2 * numrq * hrqp[0]->page_count) *
15926 sizeof(struct dma_address));
15927
15928 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15929 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15930 LPFC_SLI4_MBX_NEMBED);
15931 if (alloclen < length) {
15932 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15933 "3099 Allocated DMA memory size (%d) is "
15934 "less than the requested DMA memory size "
15935 "(%d)\n", alloclen, length);
15936 status = -ENOMEM;
15937 goto out;
15938 }
15939
15940
15941
15942 rq_create = mbox->sge_array->addr[0];
15943 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15944
15945 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15946 cnt = 0;
15947
15948 for (idx = 0; idx < numrq; idx++) {
15949 hrq = hrqp[idx];
15950 drq = drqp[idx];
15951 cq = cqp[idx];
15952
James Smart2d7dbc42017-02-12 13:52:35 -080015953 /* sanity check on queue memory */
15954 if (!hrq || !drq || !cq) {
15955 status = -ENODEV;
15956 goto out;
15957 }
15958
James Smart7aabe842017-03-04 09:30:22 -080015959 if (hrq->entry_count != drq->entry_count) {
15960 status = -EINVAL;
15961 goto out;
15962 }
15963
James Smart2d7dbc42017-02-12 13:52:35 -080015964 if (idx == 0) {
15965 bf_set(lpfc_mbx_rq_create_num_pages,
15966 &rq_create->u.request,
15967 hrq->page_count);
15968 bf_set(lpfc_mbx_rq_create_rq_cnt,
15969 &rq_create->u.request, (numrq * 2));
15970 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15971 1);
15972 bf_set(lpfc_rq_context_base_cq,
15973 &rq_create->u.request.context,
15974 cq->queue_id);
15975 bf_set(lpfc_rq_context_data_size,
15976 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015977 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015978 bf_set(lpfc_rq_context_hdr_size,
15979 &rq_create->u.request.context,
15980 LPFC_HDR_BUF_SIZE);
15981 bf_set(lpfc_rq_context_rqe_count_1,
15982 &rq_create->u.request.context,
15983 hrq->entry_count);
15984 bf_set(lpfc_rq_context_rqe_size,
15985 &rq_create->u.request.context,
15986 LPFC_RQE_SIZE_8);
15987 bf_set(lpfc_rq_context_page_size,
15988 &rq_create->u.request.context,
15989 (PAGE_SIZE/SLI4_PAGE_SIZE));
15990 }
15991 rc = 0;
15992 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15993 memset(dmabuf->virt, 0, hw_page_size);
15994 cnt = page_idx + dmabuf->buffer_tag;
15995 rq_create->u.request.page[cnt].addr_lo =
15996 putPaddrLow(dmabuf->phys);
15997 rq_create->u.request.page[cnt].addr_hi =
15998 putPaddrHigh(dmabuf->phys);
15999 rc++;
16000 }
16001 page_idx += rc;
16002
16003 rc = 0;
16004 list_for_each_entry(dmabuf, &drq->page_list, list) {
16005 memset(dmabuf->virt, 0, hw_page_size);
16006 cnt = page_idx + dmabuf->buffer_tag;
16007 rq_create->u.request.page[cnt].addr_lo =
16008 putPaddrLow(dmabuf->phys);
16009 rq_create->u.request.page[cnt].addr_hi =
16010 putPaddrHigh(dmabuf->phys);
16011 rc++;
16012 }
16013 page_idx += rc;
16014
16015 hrq->db_format = LPFC_DB_RING_FORMAT;
16016 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16017 hrq->type = LPFC_HRQ;
16018 hrq->assoc_qid = cq->queue_id;
16019 hrq->subtype = subtype;
16020 hrq->host_index = 0;
16021 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016022 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080016023
16024 drq->db_format = LPFC_DB_RING_FORMAT;
16025 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16026 drq->type = LPFC_DRQ;
16027 drq->assoc_qid = cq->queue_id;
16028 drq->subtype = subtype;
16029 drq->host_index = 0;
16030 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016031 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080016032
16033 list_add_tail(&hrq->list, &cq->child_list);
16034 list_add_tail(&drq->list, &cq->child_list);
16035 }
16036
16037 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16038 /* The IOCTL status is embedded in the mailbox subheader. */
16039 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16040 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16041 if (shdr_status || shdr_add_status || rc) {
16042 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16043 "3120 RQ_CREATE mailbox failed with "
16044 "status x%x add_status x%x, mbx status x%x\n",
16045 shdr_status, shdr_add_status, rc);
16046 status = -ENXIO;
16047 goto out;
16048 }
16049 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16050 if (rc == 0xFFFF) {
16051 status = -ENXIO;
16052 goto out;
16053 }
16054
16055 /* Initialize all RQs with associated queue id */
16056 for (idx = 0; idx < numrq; idx++) {
16057 hrq = hrqp[idx];
16058 hrq->queue_id = rc + (2 * idx);
16059 drq = drqp[idx];
16060 drq->queue_id = rc + (2 * idx) + 1;
16061 }
16062
16063out:
16064 lpfc_sli4_mbox_cmd_free(phba, mbox);
16065 return status;
16066}
16067
16068/**
James Smart4f774512009-05-22 14:52:35 -040016069 * lpfc_eq_destroy - Destroy an event Queue on the HBA
16070 * @eq: The queue structure associated with the queue to destroy.
16071 *
16072 * This function destroys a queue, as detailed in @eq by sending an mailbox
16073 * command, specific to the type of queue, to the HBA.
16074 *
16075 * The @eq struct is used to get the queue ID of the queue to destroy.
16076 *
16077 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016078 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016079 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016080int
James Smart4f774512009-05-22 14:52:35 -040016081lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
16082{
16083 LPFC_MBOXQ_t *mbox;
16084 int rc, length, status = 0;
16085 uint32_t shdr_status, shdr_add_status;
16086 union lpfc_sli4_cfg_shdr *shdr;
16087
James Smart2e90f4b2011-12-13 13:22:37 -050016088 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016089 if (!eq)
16090 return -ENODEV;
James Smart32517fc2019-01-28 11:14:33 -080016091
James Smart4f774512009-05-22 14:52:35 -040016092 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
16093 if (!mbox)
16094 return -ENOMEM;
16095 length = (sizeof(struct lpfc_mbx_eq_destroy) -
16096 sizeof(struct lpfc_sli4_cfg_mhdr));
16097 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16098 LPFC_MBOX_OPCODE_EQ_DESTROY,
16099 length, LPFC_SLI4_MBX_EMBED);
16100 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
16101 eq->queue_id);
16102 mbox->vport = eq->phba->pport;
16103 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16104
16105 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
16106 /* The IOCTL status is embedded in the mailbox subheader. */
16107 shdr = (union lpfc_sli4_cfg_shdr *)
16108 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
16109 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16110 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16111 if (shdr_status || shdr_add_status || rc) {
16112 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16113 "2505 EQ_DESTROY mailbox failed with "
16114 "status x%x add_status x%x, mbx status x%x\n",
16115 shdr_status, shdr_add_status, rc);
16116 status = -ENXIO;
16117 }
16118
16119 /* Remove eq from any list */
16120 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040016121 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016122 return status;
16123}
16124
16125/**
16126 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
16127 * @cq: The queue structure associated with the queue to destroy.
16128 *
16129 * This function destroys a queue, as detailed in @cq by sending an mailbox
16130 * command, specific to the type of queue, to the HBA.
16131 *
16132 * The @cq struct is used to get the queue ID of the queue to destroy.
16133 *
16134 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016135 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016136 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016137int
James Smart4f774512009-05-22 14:52:35 -040016138lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
16139{
16140 LPFC_MBOXQ_t *mbox;
16141 int rc, length, status = 0;
16142 uint32_t shdr_status, shdr_add_status;
16143 union lpfc_sli4_cfg_shdr *shdr;
16144
James Smart2e90f4b2011-12-13 13:22:37 -050016145 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016146 if (!cq)
16147 return -ENODEV;
16148 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
16149 if (!mbox)
16150 return -ENOMEM;
16151 length = (sizeof(struct lpfc_mbx_cq_destroy) -
16152 sizeof(struct lpfc_sli4_cfg_mhdr));
16153 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16154 LPFC_MBOX_OPCODE_CQ_DESTROY,
16155 length, LPFC_SLI4_MBX_EMBED);
16156 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
16157 cq->queue_id);
16158 mbox->vport = cq->phba->pport;
16159 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16160 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
16161 /* The IOCTL status is embedded in the mailbox subheader. */
16162 shdr = (union lpfc_sli4_cfg_shdr *)
16163 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
16164 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16165 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16166 if (shdr_status || shdr_add_status || rc) {
16167 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16168 "2506 CQ_DESTROY mailbox failed with "
16169 "status x%x add_status x%x, mbx status x%x\n",
16170 shdr_status, shdr_add_status, rc);
16171 status = -ENXIO;
16172 }
16173 /* Remove cq from any list */
16174 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040016175 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016176 return status;
16177}
16178
16179/**
James Smart04c68492009-05-22 14:52:52 -040016180 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
16181 * @qm: The queue structure associated with the queue to destroy.
16182 *
16183 * This function destroys a queue, as detailed in @mq by sending an mailbox
16184 * command, specific to the type of queue, to the HBA.
16185 *
16186 * The @mq struct is used to get the queue ID of the queue to destroy.
16187 *
16188 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016189 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040016190 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016191int
James Smart04c68492009-05-22 14:52:52 -040016192lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16193{
16194 LPFC_MBOXQ_t *mbox;
16195 int rc, length, status = 0;
16196 uint32_t shdr_status, shdr_add_status;
16197 union lpfc_sli4_cfg_shdr *shdr;
16198
James Smart2e90f4b2011-12-13 13:22:37 -050016199 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040016200 if (!mq)
16201 return -ENODEV;
16202 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16203 if (!mbox)
16204 return -ENOMEM;
16205 length = (sizeof(struct lpfc_mbx_mq_destroy) -
16206 sizeof(struct lpfc_sli4_cfg_mhdr));
16207 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16208 LPFC_MBOX_OPCODE_MQ_DESTROY,
16209 length, LPFC_SLI4_MBX_EMBED);
16210 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16211 mq->queue_id);
16212 mbox->vport = mq->phba->pport;
16213 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16214 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16215 /* The IOCTL status is embedded in the mailbox subheader. */
16216 shdr = (union lpfc_sli4_cfg_shdr *)
16217 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16218 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16219 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16220 if (shdr_status || shdr_add_status || rc) {
16221 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16222 "2507 MQ_DESTROY mailbox failed with "
16223 "status x%x add_status x%x, mbx status x%x\n",
16224 shdr_status, shdr_add_status, rc);
16225 status = -ENXIO;
16226 }
16227 /* Remove mq from any list */
16228 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040016229 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040016230 return status;
16231}
16232
16233/**
James Smart4f774512009-05-22 14:52:35 -040016234 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16235 * @wq: The queue structure associated with the queue to destroy.
16236 *
16237 * This function destroys a queue, as detailed in @wq by sending an mailbox
16238 * command, specific to the type of queue, to the HBA.
16239 *
16240 * The @wq struct is used to get the queue ID of the queue to destroy.
16241 *
16242 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016243 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016244 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016245int
James Smart4f774512009-05-22 14:52:35 -040016246lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16247{
16248 LPFC_MBOXQ_t *mbox;
16249 int rc, length, status = 0;
16250 uint32_t shdr_status, shdr_add_status;
16251 union lpfc_sli4_cfg_shdr *shdr;
16252
James Smart2e90f4b2011-12-13 13:22:37 -050016253 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016254 if (!wq)
16255 return -ENODEV;
16256 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16257 if (!mbox)
16258 return -ENOMEM;
16259 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16260 sizeof(struct lpfc_sli4_cfg_mhdr));
16261 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16262 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16263 length, LPFC_SLI4_MBX_EMBED);
16264 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16265 wq->queue_id);
16266 mbox->vport = wq->phba->pport;
16267 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16268 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16269 shdr = (union lpfc_sli4_cfg_shdr *)
16270 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16271 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16272 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16273 if (shdr_status || shdr_add_status || rc) {
16274 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16275 "2508 WQ_DESTROY mailbox failed with "
16276 "status x%x add_status x%x, mbx status x%x\n",
16277 shdr_status, shdr_add_status, rc);
16278 status = -ENXIO;
16279 }
16280 /* Remove wq from any list */
16281 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070016282 kfree(wq->pring);
16283 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040016284 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016285 return status;
16286}
16287
16288/**
16289 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16290 * @rq: The queue structure associated with the queue to destroy.
16291 *
16292 * This function destroys a queue, as detailed in @rq by sending an mailbox
16293 * command, specific to the type of queue, to the HBA.
16294 *
16295 * The @rq struct is used to get the queue ID of the queue to destroy.
16296 *
16297 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016298 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016299 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016300int
James Smart4f774512009-05-22 14:52:35 -040016301lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16302 struct lpfc_queue *drq)
16303{
16304 LPFC_MBOXQ_t *mbox;
16305 int rc, length, status = 0;
16306 uint32_t shdr_status, shdr_add_status;
16307 union lpfc_sli4_cfg_shdr *shdr;
16308
James Smart2e90f4b2011-12-13 13:22:37 -050016309 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016310 if (!hrq || !drq)
16311 return -ENODEV;
16312 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16313 if (!mbox)
16314 return -ENOMEM;
16315 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050016316 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040016317 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16318 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16319 length, LPFC_SLI4_MBX_EMBED);
16320 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16321 hrq->queue_id);
16322 mbox->vport = hrq->phba->pport;
16323 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16324 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16325 /* The IOCTL status is embedded in the mailbox subheader. */
16326 shdr = (union lpfc_sli4_cfg_shdr *)
16327 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16328 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16329 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16330 if (shdr_status || shdr_add_status || rc) {
16331 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16332 "2509 RQ_DESTROY mailbox failed with "
16333 "status x%x add_status x%x, mbx status x%x\n",
16334 shdr_status, shdr_add_status, rc);
16335 if (rc != MBX_TIMEOUT)
16336 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16337 return -ENXIO;
16338 }
16339 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16340 drq->queue_id);
16341 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16342 shdr = (union lpfc_sli4_cfg_shdr *)
16343 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16344 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16345 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16346 if (shdr_status || shdr_add_status || rc) {
16347 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16348 "2510 RQ_DESTROY mailbox failed with "
16349 "status x%x add_status x%x, mbx status x%x\n",
16350 shdr_status, shdr_add_status, rc);
16351 status = -ENXIO;
16352 }
16353 list_del_init(&hrq->list);
16354 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040016355 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016356 return status;
16357}
16358
16359/**
16360 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16361 * @phba: The virtual port for which this call being executed.
16362 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16363 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16364 * @xritag: the xritag that ties this io to the SGL pages.
16365 *
16366 * This routine will post the sgl pages for the IO that has the xritag
16367 * that is in the iocbq structure. The xritag is assigned during iocbq
16368 * creation and persists for as long as the driver is loaded.
16369 * if the caller has fewer than 256 scatter gather segments to map then
16370 * pdma_phys_addr1 should be 0.
16371 * If the caller needs to map more than 256 scatter gather segment then
16372 * pdma_phys_addr1 should be a valid physical address.
16373 * physical address for SGLs must be 64 byte aligned.
16374 * If you are going to map 2 SGL's then the first one must have 256 entries
16375 * the second sgl can have between 1 and 256 entries.
16376 *
16377 * Return codes:
16378 * 0 - Success
16379 * -ENXIO, -ENOMEM - Failure
16380 **/
16381int
16382lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16383 dma_addr_t pdma_phys_addr0,
16384 dma_addr_t pdma_phys_addr1,
16385 uint16_t xritag)
16386{
16387 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16388 LPFC_MBOXQ_t *mbox;
16389 int rc;
16390 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040016391 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040016392 union lpfc_sli4_cfg_shdr *shdr;
16393
16394 if (xritag == NO_XRI) {
16395 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16396 "0364 Invalid param:\n");
16397 return -EINVAL;
16398 }
16399
16400 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16401 if (!mbox)
16402 return -ENOMEM;
16403
16404 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16405 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16406 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050016407 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040016408
16409 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16410 &mbox->u.mqe.un.post_sgl_pages;
16411 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16412 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16413
16414 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16415 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16416 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16417 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16418
16419 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16420 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16421 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16422 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16423 if (!phba->sli4_hba.intr_enable)
16424 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040016425 else {
James Smarta183a152011-10-10 21:32:43 -040016426 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040016427 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16428 }
James Smart4f774512009-05-22 14:52:35 -040016429 /* The IOCTL status is embedded in the mailbox subheader. */
16430 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16431 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16432 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16433 if (rc != MBX_TIMEOUT)
16434 mempool_free(mbox, phba->mbox_mem_pool);
16435 if (shdr_status || shdr_add_status || rc) {
16436 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16437 "2511 POST_SGL mailbox failed with "
16438 "status x%x add_status x%x, mbx status x%x\n",
16439 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040016440 }
16441 return 0;
16442}
James Smart4f774512009-05-22 14:52:35 -040016443
16444/**
James Smart88a2cfb2011-07-22 18:36:33 -040016445 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040016446 * @phba: pointer to lpfc hba data structure.
16447 *
16448 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040016449 * HBA consistent with the SLI-4 interface spec. This routine
16450 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16451 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040016452 *
James Smart88a2cfb2011-07-22 18:36:33 -040016453 * Returns
16454 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16455 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16456 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016457static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016458lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16459{
16460 unsigned long xri;
16461
16462 /*
16463 * Fetch the next logical xri. Because this index is logical,
16464 * the driver starts at 0 each time.
16465 */
16466 spin_lock_irq(&phba->hbalock);
16467 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16468 phba->sli4_hba.max_cfg_param.max_xri, 0);
16469 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16470 spin_unlock_irq(&phba->hbalock);
16471 return NO_XRI;
16472 } else {
16473 set_bit(xri, phba->sli4_hba.xri_bmask);
16474 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040016475 }
James Smart6d368e52011-05-24 11:44:12 -040016476 spin_unlock_irq(&phba->hbalock);
16477 return xri;
16478}
16479
16480/**
16481 * lpfc_sli4_free_xri - Release an xri for reuse.
16482 * @phba: pointer to lpfc hba data structure.
16483 *
16484 * This routine is invoked to release an xri to the pool of
16485 * available rpis maintained by the driver.
16486 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016487static void
James Smart6d368e52011-05-24 11:44:12 -040016488__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16489{
16490 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016491 phba->sli4_hba.max_cfg_param.xri_used--;
16492 }
16493}
16494
16495/**
16496 * lpfc_sli4_free_xri - Release an xri for reuse.
16497 * @phba: pointer to lpfc hba data structure.
16498 *
16499 * This routine is invoked to release an xri to the pool of
16500 * available rpis maintained by the driver.
16501 **/
16502void
16503lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16504{
16505 spin_lock_irq(&phba->hbalock);
16506 __lpfc_sli4_free_xri(phba, xri);
16507 spin_unlock_irq(&phba->hbalock);
16508}
16509
16510/**
James Smart4f774512009-05-22 14:52:35 -040016511 * lpfc_sli4_next_xritag - Get an xritag for the io
16512 * @phba: Pointer to HBA context object.
16513 *
16514 * This function gets an xritag for the iocb. If there is no unused xritag
16515 * it will return 0xffff.
16516 * The function returns the allocated xritag if successful, else returns zero.
16517 * Zero is not a valid xritag.
16518 * The caller is not required to hold any lock.
16519 **/
16520uint16_t
16521lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16522{
James Smart6d368e52011-05-24 11:44:12 -040016523 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016524
James Smart6d368e52011-05-24 11:44:12 -040016525 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016526 if (xri_index == NO_XRI)
16527 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16528 "2004 Failed to allocate XRI.last XRITAG is %d"
16529 " Max XRI is %d, Used XRI is %d\n",
16530 xri_index,
16531 phba->sli4_hba.max_cfg_param.max_xri,
16532 phba->sli4_hba.max_cfg_param.xri_used);
16533 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016534}
16535
16536/**
James Smart895427b2017-02-12 13:52:30 -080016537 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016538 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016539 * @post_sgl_list: pointer to els sgl entry list.
16540 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016541 *
16542 * This routine is invoked to post a block of driver's sgl pages to the
16543 * HBA using non-embedded mailbox command. No Lock is held. This routine
16544 * is only called when the driver is loading and after all IO has been
16545 * stopped.
16546 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016547static int
James Smart895427b2017-02-12 13:52:30 -080016548lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016549 struct list_head *post_sgl_list,
16550 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016551{
James Smart8a9d2e82012-05-09 21:16:12 -040016552 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016553 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16554 struct sgl_page_pairs *sgl_pg_pairs;
16555 void *viraddr;
16556 LPFC_MBOXQ_t *mbox;
16557 uint32_t reqlen, alloclen, pg_pairs;
16558 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016559 uint16_t xritag_start = 0;
16560 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016561 uint32_t shdr_status, shdr_add_status;
16562 union lpfc_sli4_cfg_shdr *shdr;
16563
James Smart895427b2017-02-12 13:52:30 -080016564 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016565 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016566 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016568 "2559 Block sgl registration required DMA "
16569 "size (%d) great than a page\n", reqlen);
16570 return -ENOMEM;
16571 }
James Smart895427b2017-02-12 13:52:30 -080016572
James Smart4f774512009-05-22 14:52:35 -040016573 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016574 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016575 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016576
16577 /* Allocate DMA memory and set up the non-embedded mailbox command */
16578 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16579 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16580 LPFC_SLI4_MBX_NEMBED);
16581
16582 if (alloclen < reqlen) {
16583 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16584 "0285 Allocated DMA memory size (%d) is "
16585 "less than the requested DMA memory "
16586 "size (%d)\n", alloclen, reqlen);
16587 lpfc_sli4_mbox_cmd_free(phba, mbox);
16588 return -ENOMEM;
16589 }
James Smart4f774512009-05-22 14:52:35 -040016590 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016591 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016592 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16593 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16594
James Smart8a9d2e82012-05-09 21:16:12 -040016595 pg_pairs = 0;
16596 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016597 /* Set up the sge entry */
16598 sgl_pg_pairs->sgl_pg0_addr_lo =
16599 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16600 sgl_pg_pairs->sgl_pg0_addr_hi =
16601 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16602 sgl_pg_pairs->sgl_pg1_addr_lo =
16603 cpu_to_le32(putPaddrLow(0));
16604 sgl_pg_pairs->sgl_pg1_addr_hi =
16605 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016606
James Smart4f774512009-05-22 14:52:35 -040016607 /* Keep the first xritag on the list */
16608 if (pg_pairs == 0)
16609 xritag_start = sglq_entry->sli4_xritag;
16610 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016611 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016612 }
James Smart6d368e52011-05-24 11:44:12 -040016613
16614 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016615 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016616 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016617 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016618
James Smart4f774512009-05-22 14:52:35 -040016619 if (!phba->sli4_hba.intr_enable)
16620 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16621 else {
James Smarta183a152011-10-10 21:32:43 -040016622 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016623 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16624 }
16625 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16626 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16627 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16628 if (rc != MBX_TIMEOUT)
16629 lpfc_sli4_mbox_cmd_free(phba, mbox);
16630 if (shdr_status || shdr_add_status || rc) {
16631 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16632 "2513 POST_SGL_BLOCK mailbox command failed "
16633 "status x%x add_status x%x mbx status x%x\n",
16634 shdr_status, shdr_add_status, rc);
16635 rc = -ENXIO;
16636 }
16637 return rc;
16638}
16639
16640/**
James Smart5e5b5112019-01-28 11:14:22 -080016641 * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
James Smart4f774512009-05-22 14:52:35 -040016642 * @phba: pointer to lpfc hba data structure.
James Smart0794d602019-01-28 11:14:19 -080016643 * @nblist: pointer to nvme buffer list.
James Smart4f774512009-05-22 14:52:35 -040016644 * @count: number of scsi buffers on the list.
16645 *
16646 * This routine is invoked to post a block of @count scsi sgl pages from a
James Smart0794d602019-01-28 11:14:19 -080016647 * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
James Smart4f774512009-05-22 14:52:35 -040016648 * No Lock is held.
16649 *
16650 **/
James Smart0794d602019-01-28 11:14:19 -080016651static int
James Smart5e5b5112019-01-28 11:14:22 -080016652lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
16653 int count)
James Smart4f774512009-05-22 14:52:35 -040016654{
James Smartc4908502019-01-28 11:14:28 -080016655 struct lpfc_io_buf *lpfc_ncmd;
James Smart4f774512009-05-22 14:52:35 -040016656 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16657 struct sgl_page_pairs *sgl_pg_pairs;
16658 void *viraddr;
16659 LPFC_MBOXQ_t *mbox;
16660 uint32_t reqlen, alloclen, pg_pairs;
16661 uint32_t mbox_tmo;
16662 uint16_t xritag_start = 0;
16663 int rc = 0;
16664 uint32_t shdr_status, shdr_add_status;
16665 dma_addr_t pdma_phys_bpl1;
16666 union lpfc_sli4_cfg_shdr *shdr;
16667
16668 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016669 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016670 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016671 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016672 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016673 "6118 Block sgl registration required DMA "
James Smart4f774512009-05-22 14:52:35 -040016674 "size (%d) great than a page\n", reqlen);
16675 return -ENOMEM;
16676 }
16677 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16678 if (!mbox) {
16679 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016680 "6119 Failed to allocate mbox cmd memory\n");
James Smart4f774512009-05-22 14:52:35 -040016681 return -ENOMEM;
16682 }
16683
16684 /* Allocate DMA memory and set up the non-embedded mailbox command */
16685 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
James Smart0794d602019-01-28 11:14:19 -080016686 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16687 reqlen, LPFC_SLI4_MBX_NEMBED);
James Smart4f774512009-05-22 14:52:35 -040016688
16689 if (alloclen < reqlen) {
16690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016691 "6120 Allocated DMA memory size (%d) is "
James Smart4f774512009-05-22 14:52:35 -040016692 "less than the requested DMA memory "
16693 "size (%d)\n", alloclen, reqlen);
16694 lpfc_sli4_mbox_cmd_free(phba, mbox);
16695 return -ENOMEM;
16696 }
James Smart6d368e52011-05-24 11:44:12 -040016697
James Smart4f774512009-05-22 14:52:35 -040016698 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016699 viraddr = mbox->sge_array->addr[0];
16700
16701 /* Set up the SGL pages in the non-embedded DMA pages */
16702 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16703 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16704
16705 pg_pairs = 0;
James Smart0794d602019-01-28 11:14:19 -080016706 list_for_each_entry(lpfc_ncmd, nblist, list) {
James Smart4f774512009-05-22 14:52:35 -040016707 /* Set up the sge entry */
16708 sgl_pg_pairs->sgl_pg0_addr_lo =
James Smart0794d602019-01-28 11:14:19 -080016709 cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040016710 sgl_pg_pairs->sgl_pg0_addr_hi =
James Smart0794d602019-01-28 11:14:19 -080016711 cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040016712 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
James Smart0794d602019-01-28 11:14:19 -080016713 pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
16714 SGL_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040016715 else
16716 pdma_phys_bpl1 = 0;
16717 sgl_pg_pairs->sgl_pg1_addr_lo =
16718 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16719 sgl_pg_pairs->sgl_pg1_addr_hi =
16720 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16721 /* Keep the first xritag on the list */
16722 if (pg_pairs == 0)
James Smart0794d602019-01-28 11:14:19 -080016723 xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -040016724 sgl_pg_pairs++;
16725 pg_pairs++;
16726 }
16727 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16728 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16729 /* Perform endian conversion if necessary */
16730 sgl->word0 = cpu_to_le32(sgl->word0);
16731
James Smart0794d602019-01-28 11:14:19 -080016732 if (!phba->sli4_hba.intr_enable) {
James Smart4f774512009-05-22 14:52:35 -040016733 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart0794d602019-01-28 11:14:19 -080016734 } else {
James Smarta183a152011-10-10 21:32:43 -040016735 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016736 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16737 }
James Smart0794d602019-01-28 11:14:19 -080016738 shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040016739 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16740 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16741 if (rc != MBX_TIMEOUT)
16742 lpfc_sli4_mbox_cmd_free(phba, mbox);
16743 if (shdr_status || shdr_add_status || rc) {
16744 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -080016745 "6125 POST_SGL_BLOCK mailbox command failed "
James Smart4f774512009-05-22 14:52:35 -040016746 "status x%x add_status x%x mbx status x%x\n",
16747 shdr_status, shdr_add_status, rc);
16748 rc = -ENXIO;
16749 }
16750 return rc;
16751}
16752
16753/**
James Smart5e5b5112019-01-28 11:14:22 -080016754 * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
James Smart0794d602019-01-28 11:14:19 -080016755 * @phba: pointer to lpfc hba data structure.
16756 * @post_nblist: pointer to the nvme buffer list.
16757 *
16758 * This routine walks a list of nvme buffers that was passed in. It attempts
16759 * to construct blocks of nvme buffer sgls which contains contiguous xris and
16760 * uses the non-embedded SGL block post mailbox commands to post to the port.
16761 * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
16762 * embedded SGL post mailbox command for posting. The @post_nblist passed in
16763 * must be local list, thus no lock is needed when manipulate the list.
16764 *
16765 * Returns: 0 = failure, non-zero number of successfully posted buffers.
16766 **/
16767int
James Smart5e5b5112019-01-28 11:14:22 -080016768lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
16769 struct list_head *post_nblist, int sb_count)
James Smart0794d602019-01-28 11:14:19 -080016770{
James Smartc4908502019-01-28 11:14:28 -080016771 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart0794d602019-01-28 11:14:19 -080016772 int status, sgl_size;
16773 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
16774 dma_addr_t pdma_phys_sgl1;
16775 int last_xritag = NO_XRI;
16776 int cur_xritag;
James Smart0794d602019-01-28 11:14:19 -080016777 LIST_HEAD(prep_nblist);
16778 LIST_HEAD(blck_nblist);
16779 LIST_HEAD(nvme_nblist);
16780
16781 /* sanity check */
16782 if (sb_count <= 0)
16783 return -EINVAL;
16784
16785 sgl_size = phba->cfg_sg_dma_buf_size;
16786 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
16787 list_del_init(&lpfc_ncmd->list);
16788 block_cnt++;
16789 if ((last_xritag != NO_XRI) &&
16790 (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
16791 /* a hole in xri block, form a sgl posting block */
16792 list_splice_init(&prep_nblist, &blck_nblist);
16793 post_cnt = block_cnt - 1;
16794 /* prepare list for next posting block */
16795 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
16796 block_cnt = 1;
16797 } else {
16798 /* prepare list for next posting block */
16799 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
16800 /* enough sgls for non-embed sgl mbox command */
16801 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
16802 list_splice_init(&prep_nblist, &blck_nblist);
16803 post_cnt = block_cnt;
16804 block_cnt = 0;
16805 }
16806 }
16807 num_posting++;
16808 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
16809
16810 /* end of repost sgl list condition for NVME buffers */
16811 if (num_posting == sb_count) {
16812 if (post_cnt == 0) {
16813 /* last sgl posting block */
16814 list_splice_init(&prep_nblist, &blck_nblist);
16815 post_cnt = block_cnt;
16816 } else if (block_cnt == 1) {
16817 /* last single sgl with non-contiguous xri */
16818 if (sgl_size > SGL_PAGE_SIZE)
16819 pdma_phys_sgl1 =
16820 lpfc_ncmd->dma_phys_sgl +
16821 SGL_PAGE_SIZE;
16822 else
16823 pdma_phys_sgl1 = 0;
16824 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
16825 status = lpfc_sli4_post_sgl(
16826 phba, lpfc_ncmd->dma_phys_sgl,
16827 pdma_phys_sgl1, cur_xritag);
16828 if (status) {
James Smartc4908502019-01-28 11:14:28 -080016829 /* Post error. Buffer unavailable. */
16830 lpfc_ncmd->flags |=
16831 LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016832 } else {
James Smartc4908502019-01-28 11:14:28 -080016833 /* Post success. Bffer available. */
16834 lpfc_ncmd->flags &=
16835 ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016836 lpfc_ncmd->status = IOSTAT_SUCCESS;
16837 num_posted++;
16838 }
16839 /* success, put on NVME buffer sgl list */
16840 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
16841 }
16842 }
16843
16844 /* continue until a nembed page worth of sgls */
16845 if (post_cnt == 0)
16846 continue;
16847
16848 /* post block of NVME buffer list sgls */
James Smart5e5b5112019-01-28 11:14:22 -080016849 status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
16850 post_cnt);
James Smart0794d602019-01-28 11:14:19 -080016851
16852 /* don't reset xirtag due to hole in xri block */
16853 if (block_cnt == 0)
16854 last_xritag = NO_XRI;
16855
16856 /* reset NVME buffer post count for next round of posting */
16857 post_cnt = 0;
16858
16859 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
16860 while (!list_empty(&blck_nblist)) {
16861 list_remove_head(&blck_nblist, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -080016862 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -080016863 if (status) {
James Smartc4908502019-01-28 11:14:28 -080016864 /* Post error. Mark buffer unavailable. */
16865 lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016866 } else {
James Smartc4908502019-01-28 11:14:28 -080016867 /* Post success, Mark buffer available. */
16868 lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016869 lpfc_ncmd->status = IOSTAT_SUCCESS;
16870 num_posted++;
16871 }
16872 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
16873 }
16874 }
16875 /* Push NVME buffers with sgl posted to the available list */
James Smart5e5b5112019-01-28 11:14:22 -080016876 lpfc_io_buf_replenish(phba, &nvme_nblist);
16877
James Smart0794d602019-01-28 11:14:19 -080016878 return num_posted;
16879}
16880
16881/**
James Smart4f774512009-05-22 14:52:35 -040016882 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16883 * @phba: pointer to lpfc_hba struct that the frame was received on
16884 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16885 *
16886 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16887 * valid type of frame that the LPFC driver will handle. This function will
16888 * return a zero if the frame is a valid frame or a non zero value when the
16889 * frame does not pass the check.
16890 **/
16891static int
16892lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16893{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016894 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016895 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016896 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016897
16898 switch (fc_hdr->fh_r_ctl) {
16899 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16900 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16901 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16902 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16903 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16904 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16905 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16906 case FC_RCTL_DD_CMD_STATUS: /* command status */
16907 case FC_RCTL_ELS_REQ: /* extended link services request */
16908 case FC_RCTL_ELS_REP: /* extended link services reply */
16909 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16910 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16911 case FC_RCTL_BA_NOP: /* basic link service NOP */
16912 case FC_RCTL_BA_ABTS: /* basic link service abort */
16913 case FC_RCTL_BA_RMC: /* remove connection */
16914 case FC_RCTL_BA_ACC: /* basic accept */
16915 case FC_RCTL_BA_RJT: /* basic reject */
16916 case FC_RCTL_BA_PRMT:
16917 case FC_RCTL_ACK_1: /* acknowledge_1 */
16918 case FC_RCTL_ACK_0: /* acknowledge_0 */
16919 case FC_RCTL_P_RJT: /* port reject */
16920 case FC_RCTL_F_RJT: /* fabric reject */
16921 case FC_RCTL_P_BSY: /* port busy */
16922 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16923 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16924 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016925 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016926 case FC_RCTL_END: /* end */
16927 break;
16928 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16929 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16930 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16931 return lpfc_fc_frame_check(phba, fc_hdr);
16932 default:
16933 goto drop;
16934 }
James Smartae9e28f2017-05-15 15:20:51 -070016935
James Smart4f774512009-05-22 14:52:35 -040016936 switch (fc_hdr->fh_type) {
16937 case FC_TYPE_BLS:
16938 case FC_TYPE_ELS:
16939 case FC_TYPE_FCP:
16940 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016941 case FC_TYPE_NVME:
James Smart4f774512009-05-22 14:52:35 -040016942 break;
16943 case FC_TYPE_IP:
16944 case FC_TYPE_ILS:
16945 default:
16946 goto drop;
16947 }
James Smart546fc852011-03-11 16:06:29 -050016948
James Smart4f774512009-05-22 14:52:35 -040016949 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016950 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040016951 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070016952 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16953 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16954 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16955 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16956 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016957 return 0;
16958drop:
16959 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016960 "2539 Dropped frame rctl:x%x type:x%x\n",
16961 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040016962 return 1;
16963}
16964
16965/**
16966 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16967 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16968 *
16969 * This function processes the FC header to retrieve the VFI from the VF
16970 * header, if one exists. This function will return the VFI if one exists
16971 * or 0 if no VSAN Header exists.
16972 **/
16973static uint32_t
16974lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16975{
16976 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16977
16978 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16979 return 0;
16980 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16981}
16982
16983/**
16984 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16985 * @phba: Pointer to the HBA structure to search for the vport on
16986 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16987 * @fcfi: The FC Fabric ID that the frame came from
16988 *
16989 * This function searches the @phba for a vport that matches the content of the
16990 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16991 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16992 * returns the matching vport pointer or NULL if unable to match frame to a
16993 * vport.
16994 **/
16995static struct lpfc_vport *
16996lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016997 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016998{
16999 struct lpfc_vport **vports;
17000 struct lpfc_vport *vport = NULL;
17001 int i;
James Smart939723a2012-05-09 21:19:03 -040017002
James Smartbf086112011-08-21 21:48:13 -040017003 if (did == Fabric_DID)
17004 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040017005 if ((phba->pport->fc_flag & FC_PT2PT) &&
17006 !(phba->link_state == LPFC_HBA_READY))
17007 return phba->pport;
17008
James Smart4f774512009-05-22 14:52:35 -040017009 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080017010 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040017011 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
17012 if (phba->fcf.fcfi == fcfi &&
17013 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
17014 vports[i]->fc_myDID == did) {
17015 vport = vports[i];
17016 break;
17017 }
17018 }
James Smart895427b2017-02-12 13:52:30 -080017019 }
James Smart4f774512009-05-22 14:52:35 -040017020 lpfc_destroy_vport_work_array(phba, vports);
17021 return vport;
17022}
17023
17024/**
James Smart45ed1192009-10-02 15:17:02 -040017025 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
17026 * @vport: The vport to work on.
17027 *
17028 * This function updates the receive sequence time stamp for this vport. The
17029 * receive sequence time stamp indicates the time that the last frame of the
17030 * the sequence that has been idle for the longest amount of time was received.
17031 * the driver uses this time stamp to indicate if any received sequences have
17032 * timed out.
17033 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017034static void
James Smart45ed1192009-10-02 15:17:02 -040017035lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
17036{
17037 struct lpfc_dmabuf *h_buf;
17038 struct hbq_dmabuf *dmabuf = NULL;
17039
17040 /* get the oldest sequence on the rcv list */
17041 h_buf = list_get_first(&vport->rcv_buffer_list,
17042 struct lpfc_dmabuf, list);
17043 if (!h_buf)
17044 return;
17045 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17046 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
17047}
17048
17049/**
17050 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
17051 * @vport: The vport that the received sequences were sent to.
17052 *
17053 * This function cleans up all outstanding received sequences. This is called
17054 * by the driver when a link event or user action invalidates all the received
17055 * sequences.
17056 **/
17057void
17058lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
17059{
17060 struct lpfc_dmabuf *h_buf, *hnext;
17061 struct lpfc_dmabuf *d_buf, *dnext;
17062 struct hbq_dmabuf *dmabuf = NULL;
17063
17064 /* start with the oldest sequence on the rcv list */
17065 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17066 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17067 list_del_init(&dmabuf->hbuf.list);
17068 list_for_each_entry_safe(d_buf, dnext,
17069 &dmabuf->dbuf.list, list) {
17070 list_del_init(&d_buf->list);
17071 lpfc_in_buf_free(vport->phba, d_buf);
17072 }
17073 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17074 }
17075}
17076
17077/**
17078 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
17079 * @vport: The vport that the received sequences were sent to.
17080 *
17081 * This function determines whether any received sequences have timed out by
17082 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
17083 * indicates that there is at least one timed out sequence this routine will
17084 * go through the received sequences one at a time from most inactive to most
17085 * active to determine which ones need to be cleaned up. Once it has determined
17086 * that a sequence needs to be cleaned up it will simply free up the resources
17087 * without sending an abort.
17088 **/
17089void
17090lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
17091{
17092 struct lpfc_dmabuf *h_buf, *hnext;
17093 struct lpfc_dmabuf *d_buf, *dnext;
17094 struct hbq_dmabuf *dmabuf = NULL;
17095 unsigned long timeout;
17096 int abort_count = 0;
17097
17098 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17099 vport->rcv_buffer_time_stamp);
17100 if (list_empty(&vport->rcv_buffer_list) ||
17101 time_before(jiffies, timeout))
17102 return;
17103 /* start with the oldest sequence on the rcv list */
17104 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17105 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17106 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17107 dmabuf->time_stamp);
17108 if (time_before(jiffies, timeout))
17109 break;
17110 abort_count++;
17111 list_del_init(&dmabuf->hbuf.list);
17112 list_for_each_entry_safe(d_buf, dnext,
17113 &dmabuf->dbuf.list, list) {
17114 list_del_init(&d_buf->list);
17115 lpfc_in_buf_free(vport->phba, d_buf);
17116 }
17117 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17118 }
17119 if (abort_count)
17120 lpfc_update_rcv_time_stamp(vport);
17121}
17122
17123/**
James Smart4f774512009-05-22 14:52:35 -040017124 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
17125 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
17126 *
17127 * This function searches through the existing incomplete sequences that have
17128 * been sent to this @vport. If the frame matches one of the incomplete
17129 * sequences then the dbuf in the @dmabuf is added to the list of frames that
17130 * make up that sequence. If no sequence is found that matches this frame then
17131 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
17132 * This function returns a pointer to the first dmabuf in the sequence list that
17133 * the frame was linked to.
17134 **/
17135static struct hbq_dmabuf *
17136lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17137{
17138 struct fc_frame_header *new_hdr;
17139 struct fc_frame_header *temp_hdr;
17140 struct lpfc_dmabuf *d_buf;
17141 struct lpfc_dmabuf *h_buf;
17142 struct hbq_dmabuf *seq_dmabuf = NULL;
17143 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050017144 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040017145
James Smart4d9ab992009-10-02 15:16:39 -040017146 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017147 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040017148 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050017149
James Smart4f774512009-05-22 14:52:35 -040017150 /* Use the hdr_buf to find the sequence that this frame belongs to */
17151 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17152 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17153 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17154 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17155 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17156 continue;
17157 /* found a pending sequence that matches this frame */
17158 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17159 break;
17160 }
17161 if (!seq_dmabuf) {
17162 /*
17163 * This indicates first frame received for this sequence.
17164 * Queue the buffer on the vport's rcv_buffer_list.
17165 */
17166 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040017167 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017168 return dmabuf;
17169 }
17170 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050017171 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
17172 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040017173 list_del_init(&seq_dmabuf->hbuf.list);
17174 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17175 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017176 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017177 return dmabuf;
17178 }
James Smart45ed1192009-10-02 15:17:02 -040017179 /* move this sequence to the tail to indicate a young sequence */
17180 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
17181 seq_dmabuf->time_stamp = jiffies;
17182 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050017183 if (list_empty(&seq_dmabuf->dbuf.list)) {
17184 temp_hdr = dmabuf->hbuf.virt;
17185 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17186 return seq_dmabuf;
17187 }
James Smart4f774512009-05-22 14:52:35 -040017188 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050017189 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
17190 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040017191 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17192 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
17193 /*
17194 * If the frame's sequence count is greater than the frame on
17195 * the list then insert the frame right after this frame
17196 */
James Smarteeead812009-12-21 17:01:23 -050017197 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
17198 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040017199 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050017200 found = 1;
17201 break;
James Smart4f774512009-05-22 14:52:35 -040017202 }
James Smart4360ca92015-12-16 18:12:04 -050017203
17204 if (&d_buf->list == &seq_dmabuf->dbuf.list)
17205 break;
17206 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040017207 }
James Smart4360ca92015-12-16 18:12:04 -050017208
17209 if (found)
17210 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017211 return NULL;
17212}
17213
17214/**
James Smart6669f9b2009-10-02 15:16:45 -040017215 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
17216 * @vport: pointer to a vitural port
17217 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17218 *
17219 * This function tries to abort from the partially assembed sequence, described
17220 * by the information from basic abbort @dmabuf. It checks to see whether such
17221 * partially assembled sequence held by the driver. If so, it shall free up all
17222 * the frames from the partially assembled sequence.
17223 *
17224 * Return
17225 * true -- if there is matching partially assembled sequence present and all
17226 * the frames freed with the sequence;
17227 * false -- if there is no matching partially assembled sequence present so
17228 * nothing got aborted in the lower layer driver
17229 **/
17230static bool
17231lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
17232 struct hbq_dmabuf *dmabuf)
17233{
17234 struct fc_frame_header *new_hdr;
17235 struct fc_frame_header *temp_hdr;
17236 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
17237 struct hbq_dmabuf *seq_dmabuf = NULL;
17238
17239 /* Use the hdr_buf to find the sequence that matches this frame */
17240 INIT_LIST_HEAD(&dmabuf->dbuf.list);
17241 INIT_LIST_HEAD(&dmabuf->hbuf.list);
17242 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17243 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17244 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17245 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17246 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17247 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17248 continue;
17249 /* found a pending sequence that matches this frame */
17250 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17251 break;
17252 }
17253
17254 /* Free up all the frames from the partially assembled sequence */
17255 if (seq_dmabuf) {
17256 list_for_each_entry_safe(d_buf, n_buf,
17257 &seq_dmabuf->dbuf.list, list) {
17258 list_del_init(&d_buf->list);
17259 lpfc_in_buf_free(vport->phba, d_buf);
17260 }
17261 return true;
17262 }
17263 return false;
17264}
17265
17266/**
James Smart6dd9e312013-01-03 15:43:37 -050017267 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
17268 * @vport: pointer to a vitural port
17269 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17270 *
17271 * This function tries to abort from the assembed sequence from upper level
17272 * protocol, described by the information from basic abbort @dmabuf. It
17273 * checks to see whether such pending context exists at upper level protocol.
17274 * If so, it shall clean up the pending context.
17275 *
17276 * Return
17277 * true -- if there is matching pending context of the sequence cleaned
17278 * at ulp;
17279 * false -- if there is no matching pending context of the sequence present
17280 * at ulp.
17281 **/
17282static bool
17283lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17284{
17285 struct lpfc_hba *phba = vport->phba;
17286 int handled;
17287
17288 /* Accepting abort at ulp with SLI4 only */
17289 if (phba->sli_rev < LPFC_SLI_REV4)
17290 return false;
17291
17292 /* Register all caring upper level protocols to attend abort */
17293 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
17294 if (handled)
17295 return true;
17296
17297 return false;
17298}
17299
17300/**
James Smart546fc852011-03-11 16:06:29 -050017301 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040017302 * @phba: Pointer to HBA context object.
17303 * @cmd_iocbq: pointer to the command iocbq structure.
17304 * @rsp_iocbq: pointer to the response iocbq structure.
17305 *
James Smart546fc852011-03-11 16:06:29 -050017306 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040017307 * event. It properly releases the memory allocated to the sequence abort
17308 * accept iocb.
17309 **/
17310static void
James Smart546fc852011-03-11 16:06:29 -050017311lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040017312 struct lpfc_iocbq *cmd_iocbq,
17313 struct lpfc_iocbq *rsp_iocbq)
17314{
James Smart6dd9e312013-01-03 15:43:37 -050017315 struct lpfc_nodelist *ndlp;
17316
17317 if (cmd_iocbq) {
17318 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
17319 lpfc_nlp_put(ndlp);
17320 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017321 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050017322 }
James Smart6b5151f2012-01-18 16:24:06 -050017323
17324 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
17325 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
17326 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17327 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
17328 rsp_iocbq->iocb.ulpStatus,
17329 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040017330}
17331
17332/**
James Smart6d368e52011-05-24 11:44:12 -040017333 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
17334 * @phba: Pointer to HBA context object.
17335 * @xri: xri id in transaction.
17336 *
17337 * This function validates the xri maps to the known range of XRIs allocated an
17338 * used by the driver.
17339 **/
James Smart7851fe22011-07-22 18:36:52 -040017340uint16_t
James Smart6d368e52011-05-24 11:44:12 -040017341lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
17342 uint16_t xri)
17343{
James Smarta2fc4aef2014-09-03 12:57:55 -040017344 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040017345
17346 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
17347 if (xri == phba->sli4_hba.xri_ids[i])
17348 return i;
17349 }
17350 return NO_XRI;
17351}
17352
James Smart6d368e52011-05-24 11:44:12 -040017353/**
James Smart546fc852011-03-11 16:06:29 -050017354 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017355 * @phba: Pointer to HBA context object.
17356 * @fc_hdr: pointer to a FC frame header.
17357 *
James Smart546fc852011-03-11 16:06:29 -050017358 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017359 * event after aborting the sequence handling.
17360 **/
James Smart86c67372017-04-21 16:05:04 -070017361void
James Smart6dd9e312013-01-03 15:43:37 -050017362lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17363 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040017364{
James Smart6dd9e312013-01-03 15:43:37 -050017365 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040017366 struct lpfc_iocbq *ctiocb = NULL;
17367 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040017368 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050017369 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040017370 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050017371 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040017372
17373 if (!lpfc_is_link_up(phba))
17374 return;
17375
17376 sid = sli4_sid_from_fc_hdr(fc_hdr);
17377 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050017378 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040017379
James Smart6dd9e312013-01-03 15:43:37 -050017380 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040017381 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070017382 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050017383 if (!ndlp) {
17384 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17385 "1268 Failed to allocate ndlp for "
17386 "oxid:x%x SID:x%x\n", oxid, sid);
17387 return;
17388 }
James Smart6dd9e312013-01-03 15:43:37 -050017389 /* Put ndlp onto pport node list */
17390 lpfc_enqueue_node(vport, ndlp);
17391 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17392 /* re-setup ndlp without removing from node list */
17393 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17394 if (!ndlp) {
17395 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17396 "3275 Failed to active ndlp found "
17397 "for oxid:x%x SID:x%x\n", oxid, sid);
17398 return;
17399 }
James Smart6669f9b2009-10-02 15:16:45 -040017400 }
17401
James Smart546fc852011-03-11 16:06:29 -050017402 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040017403 ctiocb = lpfc_sli_get_iocbq(phba);
17404 if (!ctiocb)
17405 return;
17406
James Smart5ffc2662009-11-18 15:39:44 -050017407 /* Extract the F_CTL field from FC_HDR */
17408 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17409
James Smart6669f9b2009-10-02 15:16:45 -040017410 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040017411 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050017412 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040017413 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17414 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17415 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17416
17417 /* Fill in the rest of iocb fields */
17418 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17419 icmd->ulpBdeCount = 0;
17420 icmd->ulpLe = 1;
17421 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040017422 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050017423 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017424
James Smart6669f9b2009-10-02 15:16:45 -040017425 ctiocb->iocb_cmpl = NULL;
17426 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050017427 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040017428 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050017429 ctiocb->sli4_xritag = NO_XRI;
17430
James Smartee0f4fe2012-05-09 21:19:14 -040017431 if (fctl & FC_FC_EX_CTX)
17432 /* Exchange responder sent the abort so we
17433 * own the oxid.
17434 */
17435 xri = oxid;
17436 else
17437 xri = rxid;
17438 lxri = lpfc_sli4_xri_inrange(phba, xri);
17439 if (lxri != NO_XRI)
17440 lpfc_set_rrq_active(phba, ndlp, lxri,
17441 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050017442 /* For BA_ABTS from exchange responder, if the logical xri with
17443 * the oxid maps to the FCP XRI range, the port no longer has
17444 * that exchange context, send a BLS_RJT. Override the IOCB for
17445 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050017446 */
James Smart6dd9e312013-01-03 15:43:37 -050017447 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080017448 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050017449 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17450 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17451 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17452 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17453 }
17454
17455 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17456 * the driver no longer has that exchange, send a BLS_RJT. Override
17457 * the IOCB for a BA_RJT.
17458 */
17459 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050017460 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17461 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17462 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17463 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17464 }
James Smart6669f9b2009-10-02 15:16:45 -040017465
James Smart5ffc2662009-11-18 15:39:44 -050017466 if (fctl & FC_FC_EX_CTX) {
17467 /* ABTS sent by responder to CT exchange, construction
17468 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17469 * field and RX_ID from ABTS for RX_ID field.
17470 */
James Smart546fc852011-03-11 16:06:29 -050017471 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050017472 } else {
17473 /* ABTS sent by initiator to CT exchange, construction
17474 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050017475 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050017476 */
James Smart546fc852011-03-11 16:06:29 -050017477 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050017478 }
James Smartf09c3ac2012-03-01 22:33:29 -050017479 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050017480 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050017481
James Smart546fc852011-03-11 16:06:29 -050017482 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050017483 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17484 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17485 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050017486
17487 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17488 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050017489 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17490 "2925 Failed to issue CT ABTS RSP x%x on "
17491 "xri x%x, Data x%x\n",
17492 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17493 phba->link_state);
17494 lpfc_nlp_put(ndlp);
17495 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050017496 lpfc_sli_release_iocbq(phba, ctiocb);
17497 }
James Smart6669f9b2009-10-02 15:16:45 -040017498}
17499
17500/**
17501 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17502 * @vport: Pointer to the vport on which this sequence was received
17503 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17504 *
17505 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17506 * receive sequence is only partially assembed by the driver, it shall abort
17507 * the partially assembled frames for the sequence. Otherwise, if the
17508 * unsolicited receive sequence has been completely assembled and passed to
17509 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17510 * unsolicited sequence has been aborted. After that, it will issue a basic
17511 * accept to accept the abort.
17512 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017513static void
James Smart6669f9b2009-10-02 15:16:45 -040017514lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17515 struct hbq_dmabuf *dmabuf)
17516{
17517 struct lpfc_hba *phba = vport->phba;
17518 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050017519 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050017520 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040017521
James Smart6669f9b2009-10-02 15:16:45 -040017522 /* Make a copy of fc_hdr before the dmabuf being released */
17523 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050017524 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040017525
James Smart5ffc2662009-11-18 15:39:44 -050017526 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050017527 /* ABTS by responder to exchange, no cleanup needed */
17528 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050017529 } else {
James Smart6dd9e312013-01-03 15:43:37 -050017530 /* ABTS by initiator to exchange, need to do cleanup */
17531 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17532 if (aborted == false)
17533 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050017534 }
James Smart6dd9e312013-01-03 15:43:37 -050017535 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17536
James Smart86c67372017-04-21 16:05:04 -070017537 if (phba->nvmet_support) {
17538 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17539 return;
17540 }
17541
James Smart6dd9e312013-01-03 15:43:37 -050017542 /* Respond with BA_ACC or BA_RJT accordingly */
17543 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040017544}
17545
17546/**
James Smart4f774512009-05-22 14:52:35 -040017547 * lpfc_seq_complete - Indicates if a sequence is complete
17548 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17549 *
17550 * This function checks the sequence, starting with the frame described by
17551 * @dmabuf, to see if all the frames associated with this sequence are present.
17552 * the frames associated with this sequence are linked to the @dmabuf using the
17553 * dbuf list. This function looks for two major things. 1) That the first frame
17554 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17555 * set. 3) That there are no holes in the sequence count. The function will
17556 * return 1 when the sequence is complete, otherwise it will return 0.
17557 **/
17558static int
17559lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17560{
17561 struct fc_frame_header *hdr;
17562 struct lpfc_dmabuf *d_buf;
17563 struct hbq_dmabuf *seq_dmabuf;
17564 uint32_t fctl;
17565 int seq_count = 0;
17566
17567 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17568 /* make sure first fame of sequence has a sequence count of zero */
17569 if (hdr->fh_seq_cnt != seq_count)
17570 return 0;
17571 fctl = (hdr->fh_f_ctl[0] << 16 |
17572 hdr->fh_f_ctl[1] << 8 |
17573 hdr->fh_f_ctl[2]);
17574 /* If last frame of sequence we can return success. */
17575 if (fctl & FC_FC_END_SEQ)
17576 return 1;
17577 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17578 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17579 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17580 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050017581 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040017582 return 0;
17583 fctl = (hdr->fh_f_ctl[0] << 16 |
17584 hdr->fh_f_ctl[1] << 8 |
17585 hdr->fh_f_ctl[2]);
17586 /* If last frame of sequence we can return success. */
17587 if (fctl & FC_FC_END_SEQ)
17588 return 1;
17589 }
17590 return 0;
17591}
17592
17593/**
17594 * lpfc_prep_seq - Prep sequence for ULP processing
17595 * @vport: Pointer to the vport on which this sequence was received
17596 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17597 *
17598 * This function takes a sequence, described by a list of frames, and creates
17599 * a list of iocbq structures to describe the sequence. This iocbq list will be
17600 * used to issue to the generic unsolicited sequence handler. This routine
17601 * returns a pointer to the first iocbq in the list. If the function is unable
17602 * to allocate an iocbq then it throw out the received frames that were not
17603 * able to be described and return a pointer to the first iocbq. If unable to
17604 * allocate any iocbqs (including the first) this function will return NULL.
17605 **/
17606static struct lpfc_iocbq *
17607lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17608{
James Smart7851fe22011-07-22 18:36:52 -040017609 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040017610 struct lpfc_dmabuf *d_buf, *n_buf;
17611 struct lpfc_iocbq *first_iocbq, *iocbq;
17612 struct fc_frame_header *fc_hdr;
17613 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040017614 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050017615 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040017616
17617 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17618 /* remove from receive buffer list */
17619 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017620 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017621 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040017622 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040017623 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017624 /* Get an iocbq struct to fill in. */
17625 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17626 if (first_iocbq) {
17627 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040017628 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017629 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080017630 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040017631
17632 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17633 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17634 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17635 first_iocbq->iocb.un.rcvels.parmRo =
17636 sli4_did_from_fc_hdr(fc_hdr);
17637 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17638 } else
17639 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040017640 first_iocbq->iocb.ulpContext = NO_XRI;
17641 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17642 be16_to_cpu(fc_hdr->fh_ox_id);
17643 /* iocbq is prepped for internal consumption. Physical vpi. */
17644 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17645 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040017646 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040017647 tot_len = bf_get(lpfc_rcqe_length,
17648 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17649
James Smart4f774512009-05-22 14:52:35 -040017650 first_iocbq->context2 = &seq_dmabuf->dbuf;
17651 first_iocbq->context3 = NULL;
17652 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040017653 if (tot_len > LPFC_DATA_BUF_SIZE)
17654 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040017655 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040017656 else
17657 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17658
James Smart4f774512009-05-22 14:52:35 -040017659 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040017660
James Smart7851fe22011-07-22 18:36:52 -040017661 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040017662 }
17663 iocbq = first_iocbq;
17664 /*
17665 * Each IOCBq can have two Buffers assigned, so go through the list
17666 * of buffers for this sequence and save two buffers in each IOCBq
17667 */
17668 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17669 if (!iocbq) {
17670 lpfc_in_buf_free(vport->phba, d_buf);
17671 continue;
17672 }
17673 if (!iocbq->context3) {
17674 iocbq->context3 = d_buf;
17675 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040017676 /* We need to get the size out of the right CQE */
17677 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17678 len = bf_get(lpfc_rcqe_length,
17679 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017680 pbde = (struct ulp_bde64 *)
17681 &iocbq->iocb.unsli3.sli3Words[4];
17682 if (len > LPFC_DATA_BUF_SIZE)
17683 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17684 else
17685 pbde->tus.f.bdeSize = len;
17686
James Smart7851fe22011-07-22 18:36:52 -040017687 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17688 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040017689 } else {
17690 iocbq = lpfc_sli_get_iocbq(vport->phba);
17691 if (!iocbq) {
17692 if (first_iocbq) {
17693 first_iocbq->iocb.ulpStatus =
17694 IOSTAT_FCP_RSP_ERROR;
17695 first_iocbq->iocb.un.ulpWord[4] =
17696 IOERR_NO_RESOURCES;
17697 }
17698 lpfc_in_buf_free(vport->phba, d_buf);
17699 continue;
17700 }
James Smart7851fe22011-07-22 18:36:52 -040017701 /* We need to get the size out of the right CQE */
17702 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17703 len = bf_get(lpfc_rcqe_length,
17704 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017705 iocbq->context2 = d_buf;
17706 iocbq->context3 = NULL;
17707 iocbq->iocb.ulpBdeCount = 1;
17708 if (len > LPFC_DATA_BUF_SIZE)
17709 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17710 LPFC_DATA_BUF_SIZE;
17711 else
17712 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17713
James Smart7851fe22011-07-22 18:36:52 -040017714 tot_len += len;
17715 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17716
James Smart4f774512009-05-22 14:52:35 -040017717 iocbq->iocb.un.rcvels.remoteID = sid;
17718 list_add_tail(&iocbq->list, &first_iocbq->list);
17719 }
17720 }
17721 return first_iocbq;
17722}
17723
James Smart6669f9b2009-10-02 15:16:45 -040017724static void
17725lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17726 struct hbq_dmabuf *seq_dmabuf)
17727{
17728 struct fc_frame_header *fc_hdr;
17729 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17730 struct lpfc_hba *phba = vport->phba;
17731
17732 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17733 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17734 if (!iocbq) {
17735 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17736 "2707 Ring %d handler: Failed to allocate "
17737 "iocb Rctl x%x Type x%x received\n",
17738 LPFC_ELS_RING,
17739 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17740 return;
17741 }
17742 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017743 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017744 iocbq, fc_hdr->fh_r_ctl,
17745 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017746 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017747 "2540 Ring %d handler: unexpected Rctl "
17748 "x%x Type x%x received\n",
17749 LPFC_ELS_RING,
17750 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17751
17752 /* Free iocb created in lpfc_prep_seq */
17753 list_for_each_entry_safe(curr_iocb, next_iocb,
17754 &iocbq->list, list) {
17755 list_del_init(&curr_iocb->list);
17756 lpfc_sli_release_iocbq(phba, curr_iocb);
17757 }
17758 lpfc_sli_release_iocbq(phba, iocbq);
17759}
17760
James Smartae9e28f2017-05-15 15:20:51 -070017761static void
17762lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17763 struct lpfc_iocbq *rspiocb)
17764{
17765 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17766
17767 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017768 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017769 kfree(pcmd);
17770 lpfc_sli_release_iocbq(phba, cmdiocb);
James Smarte817e5d2018-12-13 15:17:53 -080017771 lpfc_drain_txq(phba);
James Smartae9e28f2017-05-15 15:20:51 -070017772}
17773
17774static void
17775lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17776 struct hbq_dmabuf *dmabuf)
17777{
17778 struct fc_frame_header *fc_hdr;
17779 struct lpfc_hba *phba = vport->phba;
17780 struct lpfc_iocbq *iocbq = NULL;
17781 union lpfc_wqe *wqe;
17782 struct lpfc_dmabuf *pcmd = NULL;
17783 uint32_t frame_len;
17784 int rc;
James Smarte817e5d2018-12-13 15:17:53 -080017785 unsigned long iflags;
James Smartae9e28f2017-05-15 15:20:51 -070017786
17787 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17788 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17789
17790 /* Send the received frame back */
17791 iocbq = lpfc_sli_get_iocbq(phba);
James Smarte817e5d2018-12-13 15:17:53 -080017792 if (!iocbq) {
17793 /* Queue cq event and wakeup worker thread to process it */
17794 spin_lock_irqsave(&phba->hbalock, iflags);
17795 list_add_tail(&dmabuf->cq_event.list,
17796 &phba->sli4_hba.sp_queue_event);
17797 phba->hba_flag |= HBA_SP_QUEUE_EVT;
17798 spin_unlock_irqrestore(&phba->hbalock, iflags);
17799 lpfc_worker_wake_up(phba);
17800 return;
17801 }
James Smartae9e28f2017-05-15 15:20:51 -070017802
17803 /* Allocate buffer for command payload */
17804 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17805 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020017806 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070017807 &pcmd->phys);
17808 if (!pcmd || !pcmd->virt)
17809 goto exit;
17810
17811 INIT_LIST_HEAD(&pcmd->list);
17812
17813 /* copyin the payload */
17814 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17815
17816 /* fill in BDE's for command */
17817 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17818 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17819 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17820 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17821
17822 iocbq->context2 = pcmd;
17823 iocbq->vport = vport;
17824 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17825 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17826
17827 /*
17828 * Setup rest of the iocb as though it were a WQE
17829 * Build the SEND_FRAME WQE
17830 */
17831 wqe = (union lpfc_wqe *)&iocbq->iocb;
17832
17833 wqe->send_frame.frame_len = frame_len;
17834 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17835 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17836 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17837 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17838 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17839 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17840
17841 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17842 iocbq->iocb.ulpLe = 1;
17843 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17844 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17845 if (rc == IOCB_ERROR)
17846 goto exit;
17847
17848 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17849 return;
17850
17851exit:
17852 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17853 "2023 Unable to process MDS loopback frame\n");
17854 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017855 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017856 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070017857 if (iocbq)
17858 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070017859 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17860}
17861
James Smart4f774512009-05-22 14:52:35 -040017862/**
17863 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17864 * @phba: Pointer to HBA context object.
17865 *
17866 * This function is called with no lock held. This function processes all
17867 * the received buffers and gives it to upper layers when a received buffer
17868 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017869 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017870 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17871 * appropriate receive function when the final frame in a sequence is received.
17872 **/
James Smart4d9ab992009-10-02 15:16:39 -040017873void
17874lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17875 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017876{
James Smart4d9ab992009-10-02 15:16:39 -040017877 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017878 struct fc_frame_header *fc_hdr;
17879 struct lpfc_vport *vport;
17880 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017881 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017882
James Smart4f774512009-05-22 14:52:35 -040017883 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017884 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017885
James Smarte817e5d2018-12-13 15:17:53 -080017886 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
17887 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
17888 vport = phba->pport;
17889 /* Handle MDS Loopback frames */
17890 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17891 return;
17892 }
17893
James Smart4d9ab992009-10-02 15:16:39 -040017894 /* check to see if this a valid type of frame */
17895 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17896 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17897 return;
17898 }
James Smart2ea259e2017-02-12 13:52:27 -080017899
James Smart7851fe22011-07-22 18:36:52 -040017900 if ((bf_get(lpfc_cqe_code,
17901 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17902 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17903 &dmabuf->cq_event.cqe.rcqe_cmpl);
17904 else
17905 fcfi = bf_get(lpfc_rcqe_fcf_id,
17906 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017907
James Smart895427b2017-02-12 13:52:30 -080017908 /* d_id this frame is directed to */
17909 did = sli4_did_from_fc_hdr(fc_hdr);
17910
17911 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017912 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017913 /* throw out the frame */
17914 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17915 return;
17916 }
James Smart939723a2012-05-09 21:19:03 -040017917
James Smart939723a2012-05-09 21:19:03 -040017918 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17919 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17920 (did != Fabric_DID)) {
17921 /*
17922 * Throw out the frame if we are not pt2pt.
17923 * The pt2pt protocol allows for discovery frames
17924 * to be received without a registered VPI.
17925 */
17926 if (!(vport->fc_flag & FC_PT2PT) ||
17927 (phba->link_state == LPFC_HBA_READY)) {
17928 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17929 return;
17930 }
17931 }
17932
James Smart6669f9b2009-10-02 15:16:45 -040017933 /* Handle the basic abort sequence (BA_ABTS) event */
17934 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17935 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17936 return;
17937 }
17938
James Smart4d9ab992009-10-02 15:16:39 -040017939 /* Link this frame */
17940 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17941 if (!seq_dmabuf) {
17942 /* unable to add frame to vport - throw it out */
17943 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17944 return;
17945 }
17946 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017947 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017948 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017949
James Smart6669f9b2009-10-02 15:16:45 -040017950 /* Send the complete sequence to the upper layer protocol */
17951 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017952}
James Smart6fb120a2009-05-22 14:52:59 -040017953
17954/**
17955 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17956 * @phba: pointer to lpfc hba data structure.
17957 *
17958 * This routine is invoked to post rpi header templates to the
17959 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017960 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17961 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017962 *
17963 * This routine does not require any locks. It's usage is expected
17964 * to be driver load or reset recovery when the driver is
17965 * sequential.
17966 *
17967 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017968 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017969 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017970 * When this error occurs, the driver is not guaranteed
17971 * to have any rpi regions posted to the device and
17972 * must either attempt to repost the regions or take a
17973 * fatal error.
17974 **/
17975int
17976lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17977{
17978 struct lpfc_rpi_hdr *rpi_page;
17979 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017980 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017981
James Smart6d368e52011-05-24 11:44:12 -040017982 /* SLI4 ports that support extents do not require RPI headers. */
17983 if (!phba->sli4_hba.rpi_hdrs_in_use)
17984 goto exit;
17985 if (phba->sli4_hba.extents_in_use)
17986 return -EIO;
17987
James Smart6fb120a2009-05-22 14:52:59 -040017988 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017989 /*
17990 * Assign the rpi headers a physical rpi only if the driver
17991 * has not initialized those resources. A port reset only
17992 * needs the headers posted.
17993 */
17994 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17995 LPFC_RPI_RSRC_RDY)
17996 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17997
James Smart6fb120a2009-05-22 14:52:59 -040017998 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17999 if (rc != MBX_SUCCESS) {
18000 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18001 "2008 Error %d posting all rpi "
18002 "headers\n", rc);
18003 rc = -EIO;
18004 break;
18005 }
18006 }
18007
James Smart6d368e52011-05-24 11:44:12 -040018008 exit:
18009 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
18010 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040018011 return rc;
18012}
18013
18014/**
18015 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
18016 * @phba: pointer to lpfc hba data structure.
18017 * @rpi_page: pointer to the rpi memory region.
18018 *
18019 * This routine is invoked to post a single rpi header to the
18020 * HBA consistent with the SLI-4 interface spec. This memory region
18021 * maps up to 64 rpi context regions.
18022 *
18023 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018024 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040018025 * -ENOMEM - No available memory
18026 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040018027 **/
18028int
18029lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
18030{
18031 LPFC_MBOXQ_t *mboxq;
18032 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
18033 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018034 uint32_t shdr_status, shdr_add_status;
18035 union lpfc_sli4_cfg_shdr *shdr;
18036
James Smart6d368e52011-05-24 11:44:12 -040018037 /* SLI4 ports that support extents do not require RPI headers. */
18038 if (!phba->sli4_hba.rpi_hdrs_in_use)
18039 return rc;
18040 if (phba->sli4_hba.extents_in_use)
18041 return -EIO;
18042
James Smart6fb120a2009-05-22 14:52:59 -040018043 /* The port is notified of the header region via a mailbox command. */
18044 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18045 if (!mboxq) {
18046 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18047 "2001 Unable to allocate memory for issuing "
18048 "SLI_CONFIG_SPECIAL mailbox command\n");
18049 return -ENOMEM;
18050 }
18051
18052 /* Post all rpi memory regions to the port. */
18053 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040018054 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18055 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
18056 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050018057 sizeof(struct lpfc_sli4_cfg_mhdr),
18058 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040018059
18060
18061 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040018062 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
18063 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040018064 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
18065 hdr_tmpl, rpi_page->page_count);
18066
James Smart6fb120a2009-05-22 14:52:59 -040018067 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
18068 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040018069 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040018070 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
18071 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18072 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18073 if (rc != MBX_TIMEOUT)
18074 mempool_free(mboxq, phba->mbox_mem_pool);
18075 if (shdr_status || shdr_add_status || rc) {
18076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18077 "2514 POST_RPI_HDR mailbox failed with "
18078 "status x%x add_status x%x, mbx status x%x\n",
18079 shdr_status, shdr_add_status, rc);
18080 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070018081 } else {
18082 /*
18083 * The next_rpi stores the next logical module-64 rpi value used
18084 * to post physical rpis in subsequent rpi postings.
18085 */
18086 spin_lock_irq(&phba->hbalock);
18087 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
18088 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040018089 }
18090 return rc;
18091}
18092
18093/**
18094 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
18095 * @phba: pointer to lpfc hba data structure.
18096 *
18097 * This routine is invoked to post rpi header templates to the
18098 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040018099 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18100 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040018101 *
18102 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018103 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040018104 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
18105 **/
18106int
18107lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
18108{
James Smart6d368e52011-05-24 11:44:12 -040018109 unsigned long rpi;
18110 uint16_t max_rpi, rpi_limit;
18111 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018112 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040018113 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040018114
James Smart6fb120a2009-05-22 14:52:59 -040018115 /*
James Smart6d368e52011-05-24 11:44:12 -040018116 * Fetch the next logical rpi. Because this index is logical,
18117 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040018118 */
James Smart4902b382013-10-10 12:20:35 -040018119 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040018120 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
18121 rpi_limit = phba->sli4_hba.next_rpi;
18122
James Smart6d368e52011-05-24 11:44:12 -040018123 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
18124 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040018125 rpi = LPFC_RPI_ALLOC_ERROR;
18126 else {
18127 set_bit(rpi, phba->sli4_hba.rpi_bmask);
18128 phba->sli4_hba.max_cfg_param.rpi_used++;
18129 phba->sli4_hba.rpi_count++;
18130 }
James Smartbe6bb942015-04-07 15:07:22 -040018131 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
18132 "0001 rpi:%x max:%x lim:%x\n",
18133 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040018134
18135 /*
18136 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040018137 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040018138 */
18139 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
18140 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040018141 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018142 return rpi;
18143 }
18144
18145 /*
James Smart6d368e52011-05-24 11:44:12 -040018146 * RPI header postings are not required for SLI4 ports capable of
18147 * extents.
18148 */
18149 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040018150 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040018151 return rpi;
18152 }
18153
18154 /*
James Smart6fb120a2009-05-22 14:52:59 -040018155 * If the driver is running low on rpi resources, allocate another
18156 * page now. Note that the next_rpi value is used because
18157 * it represents how many are actually in use whereas max_rpi notes
18158 * how many are supported max by the device.
18159 */
James Smart6d368e52011-05-24 11:44:12 -040018160 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040018161 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018162 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
18163 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
18164 if (!rpi_hdr) {
18165 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18166 "2002 Error Could not grow rpi "
18167 "count\n");
18168 } else {
James Smart6d368e52011-05-24 11:44:12 -040018169 lrpi = rpi_hdr->start_rpi;
18170 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040018171 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
18172 }
18173 }
18174
18175 return rpi;
18176}
18177
18178/**
18179 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18180 * @phba: pointer to lpfc hba data structure.
18181 *
18182 * This routine is invoked to release an rpi to the pool of
18183 * available rpis maintained by the driver.
18184 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018185static void
James Smartd7c47992010-06-08 18:31:54 -040018186__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18187{
18188 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
18189 phba->sli4_hba.rpi_count--;
18190 phba->sli4_hba.max_cfg_param.rpi_used--;
18191 }
18192}
18193
18194/**
18195 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18196 * @phba: pointer to lpfc hba data structure.
18197 *
18198 * This routine is invoked to release an rpi to the pool of
18199 * available rpis maintained by the driver.
18200 **/
18201void
James Smart6fb120a2009-05-22 14:52:59 -040018202lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18203{
18204 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040018205 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040018206 spin_unlock_irq(&phba->hbalock);
18207}
18208
18209/**
18210 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
18211 * @phba: pointer to lpfc hba data structure.
18212 *
18213 * This routine is invoked to remove the memory region that
18214 * provided rpi via a bitmask.
18215 **/
18216void
18217lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
18218{
18219 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040018220 kfree(phba->sli4_hba.rpi_ids);
18221 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040018222}
18223
18224/**
18225 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
18226 * @phba: pointer to lpfc hba data structure.
18227 *
18228 * This routine is invoked to remove the memory region that
18229 * provided rpi via a bitmask.
18230 **/
18231int
James Smart6b5151f2012-01-18 16:24:06 -050018232lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
18233 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040018234{
18235 LPFC_MBOXQ_t *mboxq;
18236 struct lpfc_hba *phba = ndlp->phba;
18237 int rc;
18238
18239 /* The port is notified of the header region via a mailbox command. */
18240 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18241 if (!mboxq)
18242 return -ENOMEM;
18243
18244 /* Post all rpi memory regions to the port. */
18245 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050018246 if (cmpl) {
18247 mboxq->mbox_cmpl = cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080018248 mboxq->ctx_buf = arg;
18249 mboxq->ctx_ndlp = ndlp;
James Smart72859902012-01-18 16:25:38 -050018250 } else
18251 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050018252 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040018253 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18254 if (rc == MBX_NOT_FINISHED) {
18255 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18256 "2010 Resume RPI Mailbox failed "
18257 "status %d, mbxStatus x%x\n", rc,
18258 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
18259 mempool_free(mboxq, phba->mbox_mem_pool);
18260 return -EIO;
18261 }
18262 return 0;
18263}
18264
18265/**
18266 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050018267 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040018268 *
James Smart76a95d72010-11-20 23:11:48 -050018269 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040018270 *
18271 * Returns:
18272 * 0 success
18273 * -Evalue otherwise
18274 **/
18275int
James Smart76a95d72010-11-20 23:11:48 -050018276lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040018277{
18278 LPFC_MBOXQ_t *mboxq;
18279 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040018280 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040018281 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050018282 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040018283 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18284 if (!mboxq)
18285 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050018286 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040018287 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040018288 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040018289 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050018290 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040018291 "2022 INIT VPI Mailbox failed "
18292 "status %d, mbxStatus x%x\n", rc,
18293 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040018294 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040018295 }
James Smart6a9c52c2009-10-02 15:16:51 -040018296 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050018297 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040018298
18299 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040018300}
18301
18302/**
18303 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
18304 * @phba: pointer to lpfc hba data structure.
18305 * @mboxq: Pointer to mailbox object.
18306 *
18307 * This routine is invoked to manually add a single FCF record. The caller
18308 * must pass a completely initialized FCF_Record. This routine takes
18309 * care of the nonembedded mailbox operations.
18310 **/
18311static void
18312lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
18313{
18314 void *virt_addr;
18315 union lpfc_sli4_cfg_shdr *shdr;
18316 uint32_t shdr_status, shdr_add_status;
18317
18318 virt_addr = mboxq->sge_array->addr[0];
18319 /* The IOCTL status is embedded in the mailbox subheader. */
18320 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
18321 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18322 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18323
18324 if ((shdr_status || shdr_add_status) &&
18325 (shdr_status != STATUS_FCF_IN_USE))
18326 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18327 "2558 ADD_FCF_RECORD mailbox failed with "
18328 "status x%x add_status x%x\n",
18329 shdr_status, shdr_add_status);
18330
18331 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18332}
18333
18334/**
18335 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
18336 * @phba: pointer to lpfc hba data structure.
18337 * @fcf_record: pointer to the initialized fcf record to add.
18338 *
18339 * This routine is invoked to manually add a single FCF record. The caller
18340 * must pass a completely initialized FCF_Record. This routine takes
18341 * care of the nonembedded mailbox operations.
18342 **/
18343int
18344lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
18345{
18346 int rc = 0;
18347 LPFC_MBOXQ_t *mboxq;
18348 uint8_t *bytep;
18349 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040018350 struct lpfc_mbx_sge sge;
18351 uint32_t alloc_len, req_len;
18352 uint32_t fcfindex;
18353
18354 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18355 if (!mboxq) {
18356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18357 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18358 return -ENOMEM;
18359 }
18360
18361 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18362 sizeof(uint32_t);
18363
18364 /* Allocate DMA memory and set up the non-embedded mailbox command */
18365 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18366 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18367 req_len, LPFC_SLI4_MBX_NEMBED);
18368 if (alloc_len < req_len) {
18369 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18370 "2523 Allocated DMA memory size (x%x) is "
18371 "less than the requested DMA memory "
18372 "size (x%x)\n", alloc_len, req_len);
18373 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18374 return -ENOMEM;
18375 }
18376
18377 /*
18378 * Get the first SGE entry from the non-embedded DMA memory. This
18379 * routine only uses a single SGE.
18380 */
18381 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040018382 virt_addr = mboxq->sge_array->addr[0];
18383 /*
18384 * Configure the FCF record for FCFI 0. This is the driver's
18385 * hardcoded default and gets used in nonFIP mode.
18386 */
18387 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18388 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18389 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18390
18391 /*
18392 * Copy the fcf_index and the FCF Record Data. The data starts after
18393 * the FCoE header plus word10. The data copy needs to be endian
18394 * correct.
18395 */
18396 bytep += sizeof(uint32_t);
18397 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18398 mboxq->vport = phba->pport;
18399 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18400 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18401 if (rc == MBX_NOT_FINISHED) {
18402 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18403 "2515 ADD_FCF_RECORD mailbox failed with "
18404 "status 0x%x\n", rc);
18405 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18406 rc = -EIO;
18407 } else
18408 rc = 0;
18409
18410 return rc;
18411}
18412
18413/**
18414 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18415 * @phba: pointer to lpfc hba data structure.
18416 * @fcf_record: pointer to the fcf record to write the default data.
18417 * @fcf_index: FCF table entry index.
18418 *
18419 * This routine is invoked to build the driver's default FCF record. The
18420 * values used are hardcoded. This routine handles memory initialization.
18421 *
18422 **/
18423void
18424lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18425 struct fcf_record *fcf_record,
18426 uint16_t fcf_index)
18427{
18428 memset(fcf_record, 0, sizeof(struct fcf_record));
18429 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18430 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18431 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18432 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18433 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18434 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18435 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18436 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18437 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18438 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18439 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18440 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18441 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040018442 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040018443 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18444 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18445 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18446 /* Set the VLAN bit map */
18447 if (phba->valid_vlan) {
18448 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18449 = 1 << (phba->vlan_id % 8);
18450 }
18451}
18452
18453/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018454 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040018455 * @phba: pointer to lpfc hba data structure.
18456 * @fcf_index: FCF table entry offset.
18457 *
James Smart0c9ab6f2010-02-26 14:15:57 -050018458 * This routine is invoked to scan the entire FCF table by reading FCF
18459 * record and processing it one at a time starting from the @fcf_index
18460 * for initial FCF discovery or fast FCF failover rediscovery.
18461 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018462 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018463 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040018464 **/
18465int
James Smart0c9ab6f2010-02-26 14:15:57 -050018466lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040018467{
18468 int rc = 0, error;
18469 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040018470
James Smart32b97932009-07-19 10:01:21 -040018471 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050018472 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040018473 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18474 if (!mboxq) {
18475 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18476 "2000 Failed to allocate mbox for "
18477 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040018478 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050018479 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018480 }
James Smartecfd03c2010-02-12 14:41:27 -050018481 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050018482 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050018483 if (rc) {
18484 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050018485 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018486 }
James Smartecfd03c2010-02-12 14:41:27 -050018487 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040018488 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050018489 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040018490
18491 spin_lock_irq(&phba->hbalock);
18492 phba->hba_flag |= FCF_TS_INPROG;
18493 spin_unlock_irq(&phba->hbalock);
18494
James Smart6fb120a2009-05-22 14:52:59 -040018495 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050018496 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040018497 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050018498 else {
James Smart38b92ef2010-08-04 16:11:39 -040018499 /* Reset eligible FCF count for new scan */
18500 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040018501 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018502 error = 0;
James Smart32b97932009-07-19 10:01:21 -040018503 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018504fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040018505 if (error) {
18506 if (mboxq)
18507 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040018508 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040018509 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040018510 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040018511 spin_unlock_irq(&phba->hbalock);
18512 }
James Smart6fb120a2009-05-22 14:52:59 -040018513 return error;
18514}
James Smarta0c87cb2009-07-19 10:01:10 -040018515
18516/**
James Smarta93ff372010-10-22 11:06:08 -040018517 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050018518 * @phba: pointer to lpfc hba data structure.
18519 * @fcf_index: FCF table entry offset.
18520 *
18521 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040018522 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050018523 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018524 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018525 * otherwise.
18526 **/
18527int
18528lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18529{
18530 int rc = 0, error;
18531 LPFC_MBOXQ_t *mboxq;
18532
18533 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18534 if (!mboxq) {
18535 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18536 "2763 Failed to allocate mbox for "
18537 "READ_FCF cmd\n");
18538 error = -ENOMEM;
18539 goto fail_fcf_read;
18540 }
18541 /* Construct the read FCF record mailbox command */
18542 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18543 if (rc) {
18544 error = -EINVAL;
18545 goto fail_fcf_read;
18546 }
18547 /* Issue the mailbox command asynchronously */
18548 mboxq->vport = phba->pport;
18549 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18550 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18551 if (rc == MBX_NOT_FINISHED)
18552 error = -EIO;
18553 else
18554 error = 0;
18555
18556fail_fcf_read:
18557 if (error && mboxq)
18558 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18559 return error;
18560}
18561
18562/**
18563 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18564 * @phba: pointer to lpfc hba data structure.
18565 * @fcf_index: FCF table entry offset.
18566 *
18567 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040018568 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050018569 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018570 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018571 * otherwise.
18572 **/
18573int
18574lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18575{
18576 int rc = 0, error;
18577 LPFC_MBOXQ_t *mboxq;
18578
18579 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18580 if (!mboxq) {
18581 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18582 "2758 Failed to allocate mbox for "
18583 "READ_FCF cmd\n");
18584 error = -ENOMEM;
18585 goto fail_fcf_read;
18586 }
18587 /* Construct the read FCF record mailbox command */
18588 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18589 if (rc) {
18590 error = -EINVAL;
18591 goto fail_fcf_read;
18592 }
18593 /* Issue the mailbox command asynchronously */
18594 mboxq->vport = phba->pport;
18595 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18596 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18597 if (rc == MBX_NOT_FINISHED)
18598 error = -EIO;
18599 else
18600 error = 0;
18601
18602fail_fcf_read:
18603 if (error && mboxq)
18604 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18605 return error;
18606}
18607
18608/**
James Smartf5cb5302015-12-16 18:11:52 -050018609 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040018610 * phba pointer to the lpfc_hba struct for this port.
18611 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18612 * routine when the rr_bmask is empty. The FCF indecies are put into the
18613 * rr_bmask based on their priority level. Starting from the highest priority
18614 * to the lowest. The most likely FCF candidate will be in the highest
18615 * priority group. When this routine is called it searches the fcf_pri list for
18616 * next lowest priority group and repopulates the rr_bmask with only those
18617 * fcf_indexes.
18618 * returns:
18619 * 1=success 0=failure
18620 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018621static int
James Smart7d791df2011-07-22 18:37:52 -040018622lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18623{
18624 uint16_t next_fcf_pri;
18625 uint16_t last_index;
18626 struct lpfc_fcf_pri *fcf_pri;
18627 int rc;
18628 int ret = 0;
18629
18630 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18631 LPFC_SLI4_FCF_TBL_INDX_MAX);
18632 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18633 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050018634
18635 /* Verify the priority list has 2 or more entries */
18636 spin_lock_irq(&phba->hbalock);
18637 if (list_empty(&phba->fcf.fcf_pri_list) ||
18638 list_is_singular(&phba->fcf.fcf_pri_list)) {
18639 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040018640 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18641 "3061 Last IDX %d\n", last_index);
18642 return 0; /* Empty rr list */
18643 }
James Smart25626692013-03-01 16:36:54 -050018644 spin_unlock_irq(&phba->hbalock);
18645
James Smart7d791df2011-07-22 18:37:52 -040018646 next_fcf_pri = 0;
18647 /*
18648 * Clear the rr_bmask and set all of the bits that are at this
18649 * priority.
18650 */
18651 memset(phba->fcf.fcf_rr_bmask, 0,
18652 sizeof(*phba->fcf.fcf_rr_bmask));
18653 spin_lock_irq(&phba->hbalock);
18654 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18655 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18656 continue;
18657 /*
18658 * the 1st priority that has not FLOGI failed
18659 * will be the highest.
18660 */
18661 if (!next_fcf_pri)
18662 next_fcf_pri = fcf_pri->fcf_rec.priority;
18663 spin_unlock_irq(&phba->hbalock);
18664 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18665 rc = lpfc_sli4_fcf_rr_index_set(phba,
18666 fcf_pri->fcf_rec.fcf_index);
18667 if (rc)
18668 return 0;
18669 }
18670 spin_lock_irq(&phba->hbalock);
18671 }
18672 /*
18673 * if next_fcf_pri was not set above and the list is not empty then
18674 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070018675 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040018676 */
18677 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18678 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18679 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18680 /*
18681 * the 1st priority that has not FLOGI failed
18682 * will be the highest.
18683 */
18684 if (!next_fcf_pri)
18685 next_fcf_pri = fcf_pri->fcf_rec.priority;
18686 spin_unlock_irq(&phba->hbalock);
18687 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18688 rc = lpfc_sli4_fcf_rr_index_set(phba,
18689 fcf_pri->fcf_rec.fcf_index);
18690 if (rc)
18691 return 0;
18692 }
18693 spin_lock_irq(&phba->hbalock);
18694 }
18695 } else
18696 ret = 1;
18697 spin_unlock_irq(&phba->hbalock);
18698
18699 return ret;
18700}
18701/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018702 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18703 * @phba: pointer to lpfc hba data structure.
18704 *
18705 * This routine is to get the next eligible FCF record index in a round
18706 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040018707 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050018708 * shall be returned, otherwise, the next eligible FCF record's index
18709 * shall be returned.
18710 **/
18711uint16_t
18712lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18713{
18714 uint16_t next_fcf_index;
18715
James Smart421c6622013-01-03 15:44:16 -050018716initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040018717 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050018718 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18719
James Smart7d791df2011-07-22 18:37:52 -040018720next_priority:
James Smart421c6622013-01-03 15:44:16 -050018721 /* Determine the next fcf index to check */
18722 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050018723 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18724 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040018725 next_fcf_index);
18726
James Smart0c9ab6f2010-02-26 14:15:57 -050018727 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018728 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18729 /*
18730 * If we have wrapped then we need to clear the bits that
18731 * have been tested so that we can detect when we should
18732 * change the priority level.
18733 */
James Smart0c9ab6f2010-02-26 14:15:57 -050018734 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18735 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040018736 }
18737
James Smart0c9ab6f2010-02-26 14:15:57 -050018738
James Smart3804dc82010-07-14 15:31:37 -040018739 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018740 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18741 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18742 /*
18743 * If next fcf index is not found check if there are lower
18744 * Priority level fcf's in the fcf_priority list.
18745 * Set up the rr_bmask with all of the avaiable fcf bits
18746 * at that level and continue the selection process.
18747 */
18748 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018749 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018750 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18751 "2844 No roundrobin failover FCF available\n");
James Smart036cad12018-10-23 13:41:06 -070018752
18753 return LPFC_FCOE_FCF_NEXT_NONE;
James Smart3804dc82010-07-14 15:31:37 -040018754 }
18755
James Smart7d791df2011-07-22 18:37:52 -040018756 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18757 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018758 LPFC_FCF_FLOGI_FAILED) {
18759 if (list_is_singular(&phba->fcf.fcf_pri_list))
18760 return LPFC_FCOE_FCF_NEXT_NONE;
18761
James Smart7d791df2011-07-22 18:37:52 -040018762 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018763 }
James Smart7d791df2011-07-22 18:37:52 -040018764
James Smart3804dc82010-07-14 15:31:37 -040018765 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018766 "2845 Get next roundrobin failover FCF (x%x)\n",
18767 next_fcf_index);
18768
James Smart0c9ab6f2010-02-26 14:15:57 -050018769 return next_fcf_index;
18770}
18771
18772/**
18773 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18774 * @phba: pointer to lpfc hba data structure.
18775 *
18776 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018777 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018778 * does not go beyond the range of the driver allocated bmask dimension
18779 * before setting the bit.
18780 *
18781 * Returns 0 if the index bit successfully set, otherwise, it returns
18782 * -EINVAL.
18783 **/
18784int
18785lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18786{
18787 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18788 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018789 "2610 FCF (x%x) reached driver's book "
18790 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018791 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18792 return -EINVAL;
18793 }
18794 /* Set the eligible FCF record index bmask */
18795 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18796
James Smart3804dc82010-07-14 15:31:37 -040018797 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018798 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018799 "bmask\n", fcf_index);
18800
James Smart0c9ab6f2010-02-26 14:15:57 -050018801 return 0;
18802}
18803
18804/**
James Smart3804dc82010-07-14 15:31:37 -040018805 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018806 * @phba: pointer to lpfc hba data structure.
18807 *
18808 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018809 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018810 * does not go beyond the range of the driver allocated bmask dimension
18811 * before clearing the bit.
18812 **/
18813void
18814lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18815{
James Smart9a803a72013-09-06 12:17:56 -040018816 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018817 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18818 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018819 "2762 FCF (x%x) reached driver's book "
18820 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018821 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18822 return;
18823 }
18824 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018825 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018826 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18827 list) {
James Smart7d791df2011-07-22 18:37:52 -040018828 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18829 list_del_init(&fcf_pri->list);
18830 break;
18831 }
18832 }
18833 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018834 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018835
18836 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018837 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018838 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018839}
18840
18841/**
James Smartecfd03c2010-02-12 14:41:27 -050018842 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18843 * @phba: pointer to lpfc hba data structure.
18844 *
18845 * This routine is the completion routine for the rediscover FCF table mailbox
18846 * command. If the mailbox command returned failure, it will try to stop the
18847 * FCF rediscover wait timer.
18848 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018849static void
James Smartecfd03c2010-02-12 14:41:27 -050018850lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18851{
18852 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18853 uint32_t shdr_status, shdr_add_status;
18854
18855 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18856
18857 shdr_status = bf_get(lpfc_mbox_hdr_status,
18858 &redisc_fcf->header.cfg_shdr.response);
18859 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18860 &redisc_fcf->header.cfg_shdr.response);
18861 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018862 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018863 "2746 Requesting for FCF rediscovery failed "
18864 "status x%x add_status x%x\n",
18865 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018866 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018867 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018868 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018869 spin_unlock_irq(&phba->hbalock);
18870 /*
18871 * CVL event triggered FCF rediscover request failed,
18872 * last resort to re-try current registered FCF entry.
18873 */
18874 lpfc_retry_pport_discovery(phba);
18875 } else {
18876 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018877 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018878 spin_unlock_irq(&phba->hbalock);
18879 /*
18880 * DEAD FCF event triggered FCF rediscover request
18881 * failed, last resort to fail over as a link down
18882 * to FCF registration.
18883 */
18884 lpfc_sli4_fcf_dead_failthrough(phba);
18885 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018886 } else {
18887 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018888 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018889 /*
18890 * Start FCF rediscovery wait timer for pending FCF
18891 * before rescan FCF record table.
18892 */
18893 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018894 }
James Smartecfd03c2010-02-12 14:41:27 -050018895
18896 mempool_free(mbox, phba->mbox_mem_pool);
18897}
18898
18899/**
James Smart3804dc82010-07-14 15:31:37 -040018900 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018901 * @phba: pointer to lpfc hba data structure.
18902 *
18903 * This routine is invoked to request for rediscovery of the entire FCF table
18904 * by the port.
18905 **/
18906int
18907lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18908{
18909 LPFC_MBOXQ_t *mbox;
18910 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18911 int rc, length;
18912
James Smart0c9ab6f2010-02-26 14:15:57 -050018913 /* Cancel retry delay timers to all vports before FCF rediscover */
18914 lpfc_cancel_all_vport_retry_delay_timer(phba);
18915
James Smartecfd03c2010-02-12 14:41:27 -050018916 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18917 if (!mbox) {
18918 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18919 "2745 Failed to allocate mbox for "
18920 "requesting FCF rediscover.\n");
18921 return -ENOMEM;
18922 }
18923
18924 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18925 sizeof(struct lpfc_sli4_cfg_mhdr));
18926 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18927 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18928 length, LPFC_SLI4_MBX_EMBED);
18929
18930 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18931 /* Set count to 0 for invalidating the entire FCF database */
18932 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18933
18934 /* Issue the mailbox command asynchronously */
18935 mbox->vport = phba->pport;
18936 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18937 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18938
18939 if (rc == MBX_NOT_FINISHED) {
18940 mempool_free(mbox, phba->mbox_mem_pool);
18941 return -EIO;
18942 }
18943 return 0;
18944}
18945
18946/**
James Smartfc2b9892010-02-26 14:15:29 -050018947 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18948 * @phba: pointer to lpfc hba data structure.
18949 *
18950 * This function is the failover routine as a last resort to the FCF DEAD
18951 * event when driver failed to perform fast FCF failover.
18952 **/
18953void
18954lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18955{
18956 uint32_t link_state;
18957
18958 /*
18959 * Last resort as FCF DEAD event failover will treat this as
18960 * a link down, but save the link state because we don't want
18961 * it to be changed to Link Down unless it is already down.
18962 */
18963 link_state = phba->link_state;
18964 lpfc_linkdown(phba);
18965 phba->link_state = link_state;
18966
18967 /* Unregister FCF if no devices connected to it */
18968 lpfc_unregister_unused_fcf(phba);
18969}
18970
18971/**
James Smart026abb82011-12-13 13:20:45 -050018972 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018973 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018974 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018975 *
James Smart026abb82011-12-13 13:20:45 -050018976 * This function gets SLI3 port configure region 23 data through memory dump
18977 * mailbox command. When it successfully retrieves data, the size of the data
18978 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018979 **/
James Smart026abb82011-12-13 13:20:45 -050018980static uint32_t
18981lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018982{
18983 LPFC_MBOXQ_t *pmb = NULL;
18984 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018985 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018986 int rc;
18987
James Smart026abb82011-12-13 13:20:45 -050018988 if (!rgn23_data)
18989 return 0;
18990
James Smarta0c87cb2009-07-19 10:01:10 -040018991 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18992 if (!pmb) {
18993 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018994 "2600 failed to allocate mailbox memory\n");
18995 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018996 }
18997 mb = &pmb->u.mb;
18998
James Smarta0c87cb2009-07-19 10:01:10 -040018999 do {
19000 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
19001 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
19002
19003 if (rc != MBX_SUCCESS) {
19004 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050019005 "2601 failed to read config "
19006 "region 23, rc 0x%x Status 0x%x\n",
19007 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040019008 mb->un.varDmp.word_cnt = 0;
19009 }
19010 /*
19011 * dump mem may return a zero when finished or we got a
19012 * mailbox error, either way we are done.
19013 */
19014 if (mb->un.varDmp.word_cnt == 0)
19015 break;
19016 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
19017 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
19018
19019 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050019020 rgn23_data + offset,
19021 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040019022 offset += mb->un.varDmp.word_cnt;
19023 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
19024
James Smart026abb82011-12-13 13:20:45 -050019025 mempool_free(pmb, phba->mbox_mem_pool);
19026 return offset;
19027}
19028
19029/**
19030 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
19031 * @phba: pointer to lpfc hba data structure.
19032 * @rgn23_data: pointer to configure region 23 data.
19033 *
19034 * This function gets SLI4 port configure region 23 data through memory dump
19035 * mailbox command. When it successfully retrieves data, the size of the data
19036 * will be returned, otherwise, 0 will be returned.
19037 **/
19038static uint32_t
19039lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
19040{
19041 LPFC_MBOXQ_t *mboxq = NULL;
19042 struct lpfc_dmabuf *mp = NULL;
19043 struct lpfc_mqe *mqe;
19044 uint32_t data_length = 0;
19045 int rc;
19046
19047 if (!rgn23_data)
19048 return 0;
19049
19050 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19051 if (!mboxq) {
19052 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19053 "3105 failed to allocate mailbox memory\n");
19054 return 0;
19055 }
19056
19057 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
19058 goto out;
19059 mqe = &mboxq->u.mqe;
James Smart3e1f0712018-11-29 16:09:29 -080019060 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smart026abb82011-12-13 13:20:45 -050019061 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19062 if (rc)
19063 goto out;
19064 data_length = mqe->un.mb_words[5];
19065 if (data_length == 0)
19066 goto out;
19067 if (data_length > DMP_RGN23_SIZE) {
19068 data_length = 0;
19069 goto out;
19070 }
19071 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
19072out:
19073 mempool_free(mboxq, phba->mbox_mem_pool);
19074 if (mp) {
19075 lpfc_mbuf_free(phba, mp->virt, mp->phys);
19076 kfree(mp);
19077 }
19078 return data_length;
19079}
19080
19081/**
19082 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
19083 * @phba: pointer to lpfc hba data structure.
19084 *
19085 * This function read region 23 and parse TLV for port status to
19086 * decide if the user disaled the port. If the TLV indicates the
19087 * port is disabled, the hba_flag is set accordingly.
19088 **/
19089void
19090lpfc_sli_read_link_ste(struct lpfc_hba *phba)
19091{
19092 uint8_t *rgn23_data = NULL;
19093 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
19094 uint32_t offset = 0;
19095
19096 /* Get adapter Region 23 data */
19097 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
19098 if (!rgn23_data)
19099 goto out;
19100
19101 if (phba->sli_rev < LPFC_SLI_REV4)
19102 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
19103 else {
19104 if_type = bf_get(lpfc_sli_intf_if_type,
19105 &phba->sli4_hba.sli_intf);
19106 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
19107 goto out;
19108 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
19109 }
James Smarta0c87cb2009-07-19 10:01:10 -040019110
19111 if (!data_size)
19112 goto out;
19113
19114 /* Check the region signature first */
19115 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
19116 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19117 "2619 Config region 23 has bad signature\n");
19118 goto out;
19119 }
19120 offset += 4;
19121
19122 /* Check the data structure version */
19123 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
19124 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19125 "2620 Config region 23 has bad version\n");
19126 goto out;
19127 }
19128 offset += 4;
19129
19130 /* Parse TLV entries in the region */
19131 while (offset < data_size) {
19132 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
19133 break;
19134 /*
19135 * If the TLV is not driver specific TLV or driver id is
19136 * not linux driver id, skip the record.
19137 */
19138 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
19139 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
19140 (rgn23_data[offset + 3] != 0)) {
19141 offset += rgn23_data[offset + 1] * 4 + 4;
19142 continue;
19143 }
19144
19145 /* Driver found a driver specific TLV in the config region */
19146 sub_tlv_len = rgn23_data[offset + 1] * 4;
19147 offset += 4;
19148 tlv_offset = 0;
19149
19150 /*
19151 * Search for configured port state sub-TLV.
19152 */
19153 while ((offset < data_size) &&
19154 (tlv_offset < sub_tlv_len)) {
19155 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
19156 offset += 4;
19157 tlv_offset += 4;
19158 break;
19159 }
19160 if (rgn23_data[offset] != PORT_STE_TYPE) {
19161 offset += rgn23_data[offset + 1] * 4 + 4;
19162 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
19163 continue;
19164 }
19165
19166 /* This HBA contains PORT_STE configured */
19167 if (!rgn23_data[offset + 2])
19168 phba->hba_flag |= LINK_DISABLED;
19169
19170 goto out;
19171 }
19172 }
James Smart026abb82011-12-13 13:20:45 -050019173
James Smarta0c87cb2009-07-19 10:01:10 -040019174out:
James Smarta0c87cb2009-07-19 10:01:10 -040019175 kfree(rgn23_data);
19176 return;
19177}
James Smart695a8142010-01-26 23:08:03 -050019178
19179/**
James Smart52d52442011-05-24 11:42:45 -040019180 * lpfc_wr_object - write an object to the firmware
19181 * @phba: HBA structure that indicates port to create a queue on.
19182 * @dmabuf_list: list of dmabufs to write to the port.
19183 * @size: the total byte value of the objects to write to the port.
19184 * @offset: the current offset to be used to start the transfer.
19185 *
19186 * This routine will create a wr_object mailbox command to send to the port.
19187 * the mailbox command will be constructed using the dma buffers described in
19188 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
19189 * BDEs that the imbedded mailbox can support. The @offset variable will be
19190 * used to indicate the starting offset of the transfer and will also return
19191 * the offset after the write object mailbox has completed. @size is used to
19192 * determine the end of the object and whether the eof bit should be set.
19193 *
19194 * Return 0 is successful and offset will contain the the new offset to use
19195 * for the next write.
19196 * Return negative value for error cases.
19197 **/
19198int
19199lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
19200 uint32_t size, uint32_t *offset)
19201{
19202 struct lpfc_mbx_wr_object *wr_object;
19203 LPFC_MBOXQ_t *mbox;
19204 int rc = 0, i = 0;
James Smart50212672018-12-13 15:17:57 -080019205 uint32_t shdr_status, shdr_add_status, shdr_change_status;
James Smart52d52442011-05-24 11:42:45 -040019206 uint32_t mbox_tmo;
James Smart52d52442011-05-24 11:42:45 -040019207 struct lpfc_dmabuf *dmabuf;
19208 uint32_t written = 0;
James Smart50212672018-12-13 15:17:57 -080019209 bool check_change_status = false;
James Smart52d52442011-05-24 11:42:45 -040019210
19211 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19212 if (!mbox)
19213 return -ENOMEM;
19214
19215 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
19216 LPFC_MBOX_OPCODE_WRITE_OBJECT,
19217 sizeof(struct lpfc_mbx_wr_object) -
19218 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
19219
19220 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
19221 wr_object->u.request.write_offset = *offset;
19222 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
19223 wr_object->u.request.object_name[0] =
19224 cpu_to_le32(wr_object->u.request.object_name[0]);
19225 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
19226 list_for_each_entry(dmabuf, dmabuf_list, list) {
19227 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
19228 break;
19229 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
19230 wr_object->u.request.bde[i].addrHigh =
19231 putPaddrHigh(dmabuf->phys);
19232 if (written + SLI4_PAGE_SIZE >= size) {
19233 wr_object->u.request.bde[i].tus.f.bdeSize =
19234 (size - written);
19235 written += (size - written);
19236 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
James Smart50212672018-12-13 15:17:57 -080019237 bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
19238 check_change_status = true;
James Smart52d52442011-05-24 11:42:45 -040019239 } else {
19240 wr_object->u.request.bde[i].tus.f.bdeSize =
19241 SLI4_PAGE_SIZE;
19242 written += SLI4_PAGE_SIZE;
19243 }
19244 i++;
19245 }
19246 wr_object->u.request.bde_count = i;
19247 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
19248 if (!phba->sli4_hba.intr_enable)
19249 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
19250 else {
James Smarta183a152011-10-10 21:32:43 -040019251 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040019252 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
19253 }
19254 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart50212672018-12-13 15:17:57 -080019255 shdr_status = bf_get(lpfc_mbox_hdr_status,
19256 &wr_object->header.cfg_shdr.response);
19257 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
19258 &wr_object->header.cfg_shdr.response);
19259 if (check_change_status) {
19260 shdr_change_status = bf_get(lpfc_wr_object_change_status,
19261 &wr_object->u.response);
19262 switch (shdr_change_status) {
19263 case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
19264 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19265 "3198 Firmware write complete: System "
19266 "reboot required to instantiate\n");
19267 break;
19268 case (LPFC_CHANGE_STATUS_FW_RESET):
19269 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19270 "3199 Firmware write complete: Firmware"
19271 " reset required to instantiate\n");
19272 break;
19273 case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
19274 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19275 "3200 Firmware write complete: Port "
19276 "Migration or PCI Reset required to "
19277 "instantiate\n");
19278 break;
19279 case (LPFC_CHANGE_STATUS_PCI_RESET):
19280 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19281 "3201 Firmware write complete: PCI "
19282 "Reset required to instantiate\n");
19283 break;
19284 default:
19285 break;
19286 }
19287 }
James Smart52d52442011-05-24 11:42:45 -040019288 if (rc != MBX_TIMEOUT)
19289 mempool_free(mbox, phba->mbox_mem_pool);
19290 if (shdr_status || shdr_add_status || rc) {
19291 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19292 "3025 Write Object mailbox failed with "
19293 "status x%x add_status x%x, mbx status x%x\n",
19294 shdr_status, shdr_add_status, rc);
19295 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080019296 *offset = shdr_add_status;
James Smart52d52442011-05-24 11:42:45 -040019297 } else
19298 *offset += wr_object->u.response.actual_write_length;
19299 return rc;
19300}
19301
19302/**
James Smart695a8142010-01-26 23:08:03 -050019303 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
19304 * @vport: pointer to vport data structure.
19305 *
19306 * This function iterate through the mailboxq and clean up all REG_LOGIN
19307 * and REG_VPI mailbox commands associated with the vport. This function
19308 * is called when driver want to restart discovery of the vport due to
19309 * a Clear Virtual Link event.
19310 **/
19311void
19312lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
19313{
19314 struct lpfc_hba *phba = vport->phba;
19315 LPFC_MBOXQ_t *mb, *nextmb;
19316 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040019317 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040019318 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040019319 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040019320 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050019321 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050019322
James Smartd439d282010-09-29 11:18:45 -040019323 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050019324 spin_lock_irq(&phba->hbalock);
19325 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
19326 if (mb->vport != vport)
19327 continue;
19328
19329 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19330 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19331 continue;
19332
James Smartd439d282010-09-29 11:18:45 -040019333 list_del(&mb->list);
19334 list_add_tail(&mb->list, &mbox_cmd_list);
19335 }
19336 /* Clean up active mailbox command with the vport */
19337 mb = phba->sli.mbox_active;
19338 if (mb && (mb->vport == vport)) {
19339 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
19340 (mb->u.mb.mbxCommand == MBX_REG_VPI))
19341 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19342 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019343 act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -040019344 /* Put reference count for delayed processing */
19345 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
19346 /* Unregister the RPI when mailbox complete */
19347 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19348 }
19349 }
James Smart63e801c2010-11-20 23:14:19 -050019350 /* Cleanup any mailbox completions which are not yet processed */
19351 do {
19352 restart_loop = 0;
19353 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
19354 /*
19355 * If this mailox is already processed or it is
19356 * for another vport ignore it.
19357 */
19358 if ((mb->vport != vport) ||
19359 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
19360 continue;
19361
19362 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19363 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19364 continue;
19365
19366 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19367 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019368 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smart63e801c2010-11-20 23:14:19 -050019369 /* Unregister the RPI when mailbox complete */
19370 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19371 restart_loop = 1;
19372 spin_unlock_irq(&phba->hbalock);
19373 spin_lock(shost->host_lock);
19374 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19375 spin_unlock(shost->host_lock);
19376 spin_lock_irq(&phba->hbalock);
19377 break;
19378 }
19379 }
19380 } while (restart_loop);
19381
James Smartd439d282010-09-29 11:18:45 -040019382 spin_unlock_irq(&phba->hbalock);
19383
19384 /* Release the cleaned-up mailbox commands */
19385 while (!list_empty(&mbox_cmd_list)) {
19386 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050019387 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019388 mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
James Smart695a8142010-01-26 23:08:03 -050019389 if (mp) {
19390 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19391 kfree(mp);
19392 }
James Smart3e1f0712018-11-29 16:09:29 -080019393 mb->ctx_buf = NULL;
19394 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
19395 mb->ctx_ndlp = NULL;
James Smart78730cf2010-04-06 15:06:30 -040019396 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019397 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040019398 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019399 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040019400 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040019401 }
James Smart695a8142010-01-26 23:08:03 -050019402 }
James Smart695a8142010-01-26 23:08:03 -050019403 mempool_free(mb, phba->mbox_mem_pool);
19404 }
James Smartd439d282010-09-29 11:18:45 -040019405
19406 /* Release the ndlp with the cleaned-up active mailbox command */
19407 if (act_mbx_ndlp) {
19408 spin_lock(shost->host_lock);
19409 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19410 spin_unlock(shost->host_lock);
19411 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050019412 }
James Smart695a8142010-01-26 23:08:03 -050019413}
19414
James Smart2a9bf3d2010-06-07 15:24:45 -040019415/**
19416 * lpfc_drain_txq - Drain the txq
19417 * @phba: Pointer to HBA context object.
19418 *
19419 * This function attempt to submit IOCBs on the txq
19420 * to the adapter. For SLI4 adapters, the txq contains
19421 * ELS IOCBs that have been deferred because the there
19422 * are no SGLs. This congestion can occur with large
19423 * vport counts during node discovery.
19424 **/
19425
19426uint32_t
19427lpfc_drain_txq(struct lpfc_hba *phba)
19428{
19429 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080019430 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090019431 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040019432 unsigned long iflags = 0;
19433 char *fail_msg = NULL;
19434 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080019435 union lpfc_wqe128 wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040019436 uint32_t txq_cnt = 0;
James Smartdc19e3b2018-05-24 21:08:57 -070019437 struct lpfc_queue *wq;
James Smart2a9bf3d2010-06-07 15:24:45 -040019438
James Smartdc19e3b2018-05-24 21:08:57 -070019439 if (phba->link_flag & LS_MDS_LOOPBACK) {
19440 /* MDS WQE are posted only to first WQ*/
James Smartcdb42be2019-01-28 11:14:21 -080019441 wq = phba->sli4_hba.hdwq[0].fcp_wq;
James Smartdc19e3b2018-05-24 21:08:57 -070019442 if (unlikely(!wq))
19443 return 0;
19444 pring = wq->pring;
19445 } else {
19446 wq = phba->sli4_hba.els_wq;
19447 if (unlikely(!wq))
19448 return 0;
19449 pring = lpfc_phba_elsring(phba);
19450 }
19451
19452 if (unlikely(!pring) || list_empty(&pring->txq))
Dick Kennedy1234a6d2017-09-29 17:34:29 -070019453 return 0;
James Smart895427b2017-02-12 13:52:30 -080019454
James Smart398d81c2013-05-31 17:04:19 -040019455 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050019456 list_for_each_entry(piocbq, &pring->txq, list) {
19457 txq_cnt++;
19458 }
19459
19460 if (txq_cnt > pring->txq_max)
19461 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019462
James Smart398d81c2013-05-31 17:04:19 -040019463 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019464
James Smart0e9bb8d2013-03-01 16:35:12 -050019465 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040019466 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019467
James Smart19ca7602010-11-20 23:11:55 -050019468 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040019469 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040019470 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040019471 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19472 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050019473 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040019474 break;
19475 }
James Smart895427b2017-02-12 13:52:30 -080019476 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040019477 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050019478 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040019479 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019480 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040019481 }
James Smart0e9bb8d2013-03-01 16:35:12 -050019482 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040019483
19484 /* The xri and iocb resources secured,
19485 * attempt to issue request
19486 */
James Smart6d368e52011-05-24 11:44:12 -040019487 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040019488 piocbq->sli4_xritag = sglq->sli4_xritag;
19489 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19490 fail_msg = "to convert bpl to sgl";
James Smart205e8242018-03-05 12:04:03 -080019491 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019492 fail_msg = "to convert iocb to wqe";
James Smartdc19e3b2018-05-24 21:08:57 -070019493 else if (lpfc_sli4_wq_put(wq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019494 fail_msg = " - Wq is full";
19495 else
19496 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19497
19498 if (fail_msg) {
19499 /* Failed means we can't issue and need to cancel */
19500 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19501 "2822 IOCB failed %s iotag 0x%x "
19502 "xri 0x%x\n",
19503 fail_msg,
19504 piocbq->iotag, piocbq->sli4_xritag);
19505 list_add_tail(&piocbq->list, &completions);
19506 }
James Smart398d81c2013-05-31 17:04:19 -040019507 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019508 }
19509
James Smart2a9bf3d2010-06-07 15:24:45 -040019510 /* Cancel all the IOCBs that cannot be issued */
19511 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19512 IOERR_SLI_ABORTED);
19513
James Smart0e9bb8d2013-03-01 16:35:12 -050019514 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019515}
James Smart895427b2017-02-12 13:52:30 -080019516
19517/**
19518 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19519 * @phba: Pointer to HBA context object.
19520 * @pwqe: Pointer to command WQE.
19521 * @sglq: Pointer to the scatter gather queue object.
19522 *
19523 * This routine converts the bpl or bde that is in the WQE
19524 * to a sgl list for the sli4 hardware. The physical address
19525 * of the bpl/bde is converted back to a virtual address.
19526 * If the WQE contains a BPL then the list of BDE's is
19527 * converted to sli4_sge's. If the WQE contains a single
19528 * BDE then it is converted to a single sli_sge.
19529 * The WQE is still in cpu endianness so the contents of
19530 * the bpl can be used without byte swapping.
19531 *
19532 * Returns valid XRI = Success, NO_XRI = Failure.
19533 */
19534static uint16_t
19535lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19536 struct lpfc_sglq *sglq)
19537{
19538 uint16_t xritag = NO_XRI;
19539 struct ulp_bde64 *bpl = NULL;
19540 struct ulp_bde64 bde;
19541 struct sli4_sge *sgl = NULL;
19542 struct lpfc_dmabuf *dmabuf;
James Smart205e8242018-03-05 12:04:03 -080019543 union lpfc_wqe128 *wqe;
James Smart895427b2017-02-12 13:52:30 -080019544 int numBdes = 0;
19545 int i = 0;
19546 uint32_t offset = 0; /* accumulated offset in the sg request list */
19547 int inbound = 0; /* number of sg reply entries inbound from firmware */
19548 uint32_t cmd;
19549
19550 if (!pwqeq || !sglq)
19551 return xritag;
19552
19553 sgl = (struct sli4_sge *)sglq->sgl;
19554 wqe = &pwqeq->wqe;
19555 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19556
19557 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19558 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19559 return sglq->sli4_xritag;
19560 numBdes = pwqeq->rsvd2;
19561 if (numBdes) {
19562 /* The addrHigh and addrLow fields within the WQE
19563 * have not been byteswapped yet so there is no
19564 * need to swap them back.
19565 */
19566 if (pwqeq->context3)
19567 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19568 else
19569 return xritag;
19570
19571 bpl = (struct ulp_bde64 *)dmabuf->virt;
19572 if (!bpl)
19573 return xritag;
19574
19575 for (i = 0; i < numBdes; i++) {
19576 /* Should already be byte swapped. */
19577 sgl->addr_hi = bpl->addrHigh;
19578 sgl->addr_lo = bpl->addrLow;
19579
19580 sgl->word2 = le32_to_cpu(sgl->word2);
19581 if ((i+1) == numBdes)
19582 bf_set(lpfc_sli4_sge_last, sgl, 1);
19583 else
19584 bf_set(lpfc_sli4_sge_last, sgl, 0);
19585 /* swap the size field back to the cpu so we
19586 * can assign it to the sgl.
19587 */
19588 bde.tus.w = le32_to_cpu(bpl->tus.w);
19589 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19590 /* The offsets in the sgl need to be accumulated
19591 * separately for the request and reply lists.
19592 * The request is always first, the reply follows.
19593 */
19594 switch (cmd) {
19595 case CMD_GEN_REQUEST64_WQE:
19596 /* add up the reply sg entries */
19597 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19598 inbound++;
19599 /* first inbound? reset the offset */
19600 if (inbound == 1)
19601 offset = 0;
19602 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19603 bf_set(lpfc_sli4_sge_type, sgl,
19604 LPFC_SGE_TYPE_DATA);
19605 offset += bde.tus.f.bdeSize;
19606 break;
19607 case CMD_FCP_TRSP64_WQE:
19608 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19609 bf_set(lpfc_sli4_sge_type, sgl,
19610 LPFC_SGE_TYPE_DATA);
19611 break;
19612 case CMD_FCP_TSEND64_WQE:
19613 case CMD_FCP_TRECEIVE64_WQE:
19614 bf_set(lpfc_sli4_sge_type, sgl,
19615 bpl->tus.f.bdeFlags);
19616 if (i < 3)
19617 offset = 0;
19618 else
19619 offset += bde.tus.f.bdeSize;
19620 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19621 break;
19622 }
19623 sgl->word2 = cpu_to_le32(sgl->word2);
19624 bpl++;
19625 sgl++;
19626 }
19627 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19628 /* The addrHigh and addrLow fields of the BDE have not
19629 * been byteswapped yet so they need to be swapped
19630 * before putting them in the sgl.
19631 */
19632 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19633 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19634 sgl->word2 = le32_to_cpu(sgl->word2);
19635 bf_set(lpfc_sli4_sge_last, sgl, 1);
19636 sgl->word2 = cpu_to_le32(sgl->word2);
19637 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19638 }
19639 return sglq->sli4_xritag;
19640}
19641
19642/**
19643 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19644 * @phba: Pointer to HBA context object.
19645 * @ring_number: Base sli ring number
19646 * @pwqe: Pointer to command WQE.
19647 **/
19648int
James Smart1fbf9742019-01-28 11:14:26 -080019649lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
James Smart895427b2017-02-12 13:52:30 -080019650 struct lpfc_iocbq *pwqe)
19651{
James Smart205e8242018-03-05 12:04:03 -080019652 union lpfc_wqe128 *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080019653 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080019654 struct lpfc_queue *wq;
19655 struct lpfc_sglq *sglq;
19656 struct lpfc_sli_ring *pring;
19657 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070019658 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080019659
19660 /* NVME_LS and NVME_LS ABTS requests. */
19661 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19662 pring = phba->sli4_hba.nvmels_wq->pring;
James Smart6a828b02019-01-28 11:14:31 -080019663 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
19664 qp, wq_access);
James Smart895427b2017-02-12 13:52:30 -080019665 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19666 if (!sglq) {
19667 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19668 return WQE_BUSY;
19669 }
19670 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19671 pwqe->sli4_xritag = sglq->sli4_xritag;
19672 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19673 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19674 return WQE_ERROR;
19675 }
19676 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19677 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070019678 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19679 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019680 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019681 return ret;
James Smart895427b2017-02-12 13:52:30 -080019682 }
Dick Kennedycd22d602017-08-23 16:55:35 -070019683
James Smart895427b2017-02-12 13:52:30 -080019684 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19685 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19686 return 0;
19687 }
19688
19689 /* NVME_FCREQ and NVME_ABTS requests */
19690 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19691 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smart1fbf9742019-01-28 11:14:26 -080019692 wq = qp->nvme_wq;
19693 pring = wq->pring;
James Smart895427b2017-02-12 13:52:30 -080019694
James Smart1fbf9742019-01-28 11:14:26 -080019695 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->nvme_cq_map);
James Smart895427b2017-02-12 13:52:30 -080019696
James Smart6a828b02019-01-28 11:14:31 -080019697 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
19698 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070019699 ret = lpfc_sli4_wq_put(wq, wqe);
19700 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019701 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019702 return ret;
James Smart895427b2017-02-12 13:52:30 -080019703 }
19704 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19705 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19706 return 0;
19707 }
19708
James Smartf358dd02017-02-12 13:52:34 -080019709 /* NVMET requests */
19710 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19711 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smart1fbf9742019-01-28 11:14:26 -080019712 wq = qp->nvme_wq;
19713 pring = wq->pring;
James Smartf358dd02017-02-12 13:52:34 -080019714
James Smartf358dd02017-02-12 13:52:34 -080019715 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070019716 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080019717 if (pwqe->sli4_xritag == NO_XRI) {
19718 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19719 pwqe->sli4_xritag = sglq->sli4_xritag;
19720 }
19721 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19722 pwqe->sli4_xritag);
James Smart1fbf9742019-01-28 11:14:26 -080019723 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->nvme_cq_map);
19724
James Smart6a828b02019-01-28 11:14:31 -080019725 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
19726 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070019727 ret = lpfc_sli4_wq_put(wq, wqe);
19728 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080019729 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019730 return ret;
James Smartf358dd02017-02-12 13:52:34 -080019731 }
19732 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19733 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19734 return 0;
19735 }
James Smart895427b2017-02-12 13:52:30 -080019736 return WQE_ERROR;
19737}
James Smartc4908502019-01-28 11:14:28 -080019738
19739#ifdef LPFC_MXP_STAT
19740/**
19741 * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
19742 * @phba: pointer to lpfc hba data structure.
19743 * @hwqid: belong to which HWQ.
19744 *
19745 * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
19746 * 15 seconds after a test case is running.
19747 *
19748 * The user should call lpfc_debugfs_multixripools_write before running a test
19749 * case to clear stat_snapshot_taken. Then the user starts a test case. During
19750 * test case is running, stat_snapshot_taken is incremented by 1 every time when
19751 * this routine is called from heartbeat timer. When stat_snapshot_taken is
19752 * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
19753 **/
19754void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
19755{
19756 struct lpfc_sli4_hdw_queue *qp;
19757 struct lpfc_multixri_pool *multixri_pool;
19758 struct lpfc_pvt_pool *pvt_pool;
19759 struct lpfc_pbl_pool *pbl_pool;
19760 u32 txcmplq_cnt;
19761
19762 qp = &phba->sli4_hba.hdwq[hwqid];
19763 multixri_pool = qp->p_multixri_pool;
19764 if (!multixri_pool)
19765 return;
19766
19767 if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
19768 pvt_pool = &qp->p_multixri_pool->pvt_pool;
19769 pbl_pool = &qp->p_multixri_pool->pbl_pool;
19770 txcmplq_cnt = qp->fcp_wq->pring->txcmplq_cnt;
19771 if (qp->nvme_wq)
19772 txcmplq_cnt += qp->nvme_wq->pring->txcmplq_cnt;
19773
19774 multixri_pool->stat_pbl_count = pbl_pool->count;
19775 multixri_pool->stat_pvt_count = pvt_pool->count;
19776 multixri_pool->stat_busy_count = txcmplq_cnt;
19777 }
19778
19779 multixri_pool->stat_snapshot_taken++;
19780}
19781#endif
19782
19783/**
19784 * lpfc_adjust_pvt_pool_count - Adjust private pool count
19785 * @phba: pointer to lpfc hba data structure.
19786 * @hwqid: belong to which HWQ.
19787 *
19788 * This routine moves some XRIs from private to public pool when private pool
19789 * is not busy.
19790 **/
19791void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
19792{
19793 struct lpfc_multixri_pool *multixri_pool;
19794 u32 io_req_count;
19795 u32 prev_io_req_count;
19796
19797 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
19798 if (!multixri_pool)
19799 return;
19800 io_req_count = multixri_pool->io_req_count;
19801 prev_io_req_count = multixri_pool->prev_io_req_count;
19802
19803 if (prev_io_req_count != io_req_count) {
19804 /* Private pool is busy */
19805 multixri_pool->prev_io_req_count = io_req_count;
19806 } else {
19807 /* Private pool is not busy.
19808 * Move XRIs from private to public pool.
19809 */
19810 lpfc_move_xri_pvt_to_pbl(phba, hwqid);
19811 }
19812}
19813
19814/**
19815 * lpfc_adjust_high_watermark - Adjust high watermark
19816 * @phba: pointer to lpfc hba data structure.
19817 * @hwqid: belong to which HWQ.
19818 *
19819 * This routine sets high watermark as number of outstanding XRIs,
19820 * but make sure the new value is between xri_limit/2 and xri_limit.
19821 **/
19822void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
19823{
19824 u32 new_watermark;
19825 u32 watermark_max;
19826 u32 watermark_min;
19827 u32 xri_limit;
19828 u32 txcmplq_cnt;
19829 u32 abts_io_bufs;
19830 struct lpfc_multixri_pool *multixri_pool;
19831 struct lpfc_sli4_hdw_queue *qp;
19832
19833 qp = &phba->sli4_hba.hdwq[hwqid];
19834 multixri_pool = qp->p_multixri_pool;
19835 if (!multixri_pool)
19836 return;
19837 xri_limit = multixri_pool->xri_limit;
19838
19839 watermark_max = xri_limit;
19840 watermark_min = xri_limit / 2;
19841
19842 txcmplq_cnt = qp->fcp_wq->pring->txcmplq_cnt;
19843 abts_io_bufs = qp->abts_scsi_io_bufs;
19844 if (qp->nvme_wq) {
19845 txcmplq_cnt += qp->nvme_wq->pring->txcmplq_cnt;
19846 abts_io_bufs += qp->abts_nvme_io_bufs;
19847 }
19848
19849 new_watermark = txcmplq_cnt + abts_io_bufs;
19850 new_watermark = min(watermark_max, new_watermark);
19851 new_watermark = max(watermark_min, new_watermark);
19852 multixri_pool->pvt_pool.high_watermark = new_watermark;
19853
19854#ifdef LPFC_MXP_STAT
19855 multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
19856 new_watermark);
19857#endif
19858}
19859
19860/**
19861 * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
19862 * @phba: pointer to lpfc hba data structure.
19863 * @hwqid: belong to which HWQ.
19864 *
19865 * This routine is called from hearbeat timer when pvt_pool is idle.
19866 * All free XRIs are moved from private to public pool on hwqid with 2 steps.
19867 * The first step moves (all - low_watermark) amount of XRIs.
19868 * The second step moves the rest of XRIs.
19869 **/
19870void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
19871{
19872 struct lpfc_pbl_pool *pbl_pool;
19873 struct lpfc_pvt_pool *pvt_pool;
James Smart6a828b02019-01-28 11:14:31 -080019874 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080019875 struct lpfc_io_buf *lpfc_ncmd;
19876 struct lpfc_io_buf *lpfc_ncmd_next;
19877 unsigned long iflag;
19878 struct list_head tmp_list;
19879 u32 tmp_count;
19880
James Smart6a828b02019-01-28 11:14:31 -080019881 qp = &phba->sli4_hba.hdwq[hwqid];
19882 pbl_pool = &qp->p_multixri_pool->pbl_pool;
19883 pvt_pool = &qp->p_multixri_pool->pvt_pool;
James Smartc4908502019-01-28 11:14:28 -080019884 tmp_count = 0;
19885
James Smart6a828b02019-01-28 11:14:31 -080019886 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
19887 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080019888
19889 if (pvt_pool->count > pvt_pool->low_watermark) {
19890 /* Step 1: move (all - low_watermark) from pvt_pool
19891 * to pbl_pool
19892 */
19893
19894 /* Move low watermark of bufs from pvt_pool to tmp_list */
19895 INIT_LIST_HEAD(&tmp_list);
19896 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
19897 &pvt_pool->list, list) {
19898 list_move_tail(&lpfc_ncmd->list, &tmp_list);
19899 tmp_count++;
19900 if (tmp_count >= pvt_pool->low_watermark)
19901 break;
19902 }
19903
19904 /* Move all bufs from pvt_pool to pbl_pool */
19905 list_splice_init(&pvt_pool->list, &pbl_pool->list);
19906
19907 /* Move all bufs from tmp_list to pvt_pool */
19908 list_splice(&tmp_list, &pvt_pool->list);
19909
19910 pbl_pool->count += (pvt_pool->count - tmp_count);
19911 pvt_pool->count = tmp_count;
19912 } else {
19913 /* Step 2: move the rest from pvt_pool to pbl_pool */
19914 list_splice_init(&pvt_pool->list, &pbl_pool->list);
19915 pbl_pool->count += pvt_pool->count;
19916 pvt_pool->count = 0;
19917 }
19918
19919 spin_unlock(&pvt_pool->lock);
19920 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
19921}
19922
19923/**
19924 * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
19925 * @phba: pointer to lpfc hba data structure
19926 * @pbl_pool: specified public free XRI pool
19927 * @pvt_pool: specified private free XRI pool
19928 * @count: number of XRIs to move
19929 *
19930 * This routine tries to move some free common bufs from the specified pbl_pool
19931 * to the specified pvt_pool. It might move less than count XRIs if there's not
19932 * enough in public pool.
19933 *
19934 * Return:
19935 * true - if XRIs are successfully moved from the specified pbl_pool to the
19936 * specified pvt_pool
19937 * false - if the specified pbl_pool is empty or locked by someone else
19938 **/
19939static bool
James Smart6a828b02019-01-28 11:14:31 -080019940_lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
19941 struct lpfc_pbl_pool *pbl_pool,
James Smartc4908502019-01-28 11:14:28 -080019942 struct lpfc_pvt_pool *pvt_pool, u32 count)
19943{
19944 struct lpfc_io_buf *lpfc_ncmd;
19945 struct lpfc_io_buf *lpfc_ncmd_next;
19946 unsigned long iflag;
19947 int ret;
19948
19949 ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
19950 if (ret) {
19951 if (pbl_pool->count) {
19952 /* Move a batch of XRIs from public to private pool */
James Smart6a828b02019-01-28 11:14:31 -080019953 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080019954 list_for_each_entry_safe(lpfc_ncmd,
19955 lpfc_ncmd_next,
19956 &pbl_pool->list,
19957 list) {
19958 list_move_tail(&lpfc_ncmd->list,
19959 &pvt_pool->list);
19960 pvt_pool->count++;
19961 pbl_pool->count--;
19962 count--;
19963 if (count == 0)
19964 break;
19965 }
19966
19967 spin_unlock(&pvt_pool->lock);
19968 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
19969 return true;
19970 }
19971 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
19972 }
19973
19974 return false;
19975}
19976
19977/**
19978 * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
19979 * @phba: pointer to lpfc hba data structure.
19980 * @hwqid: belong to which HWQ.
19981 * @count: number of XRIs to move
19982 *
19983 * This routine tries to find some free common bufs in one of public pools with
19984 * Round Robin method. The search always starts from local hwqid, then the next
19985 * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
19986 * a batch of free common bufs are moved to private pool on hwqid.
19987 * It might move less than count XRIs if there's not enough in public pool.
19988 **/
19989void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
19990{
19991 struct lpfc_multixri_pool *multixri_pool;
19992 struct lpfc_multixri_pool *next_multixri_pool;
19993 struct lpfc_pvt_pool *pvt_pool;
19994 struct lpfc_pbl_pool *pbl_pool;
James Smart6a828b02019-01-28 11:14:31 -080019995 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080019996 u32 next_hwqid;
19997 u32 hwq_count;
19998 int ret;
19999
James Smart6a828b02019-01-28 11:14:31 -080020000 qp = &phba->sli4_hba.hdwq[hwqid];
20001 multixri_pool = qp->p_multixri_pool;
James Smartc4908502019-01-28 11:14:28 -080020002 pvt_pool = &multixri_pool->pvt_pool;
20003 pbl_pool = &multixri_pool->pbl_pool;
20004
20005 /* Check if local pbl_pool is available */
James Smart6a828b02019-01-28 11:14:31 -080020006 ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080020007 if (ret) {
20008#ifdef LPFC_MXP_STAT
20009 multixri_pool->local_pbl_hit_count++;
20010#endif
20011 return;
20012 }
20013
20014 hwq_count = phba->cfg_hdw_queue;
20015
20016 /* Get the next hwqid which was found last time */
20017 next_hwqid = multixri_pool->rrb_next_hwqid;
20018
20019 do {
20020 /* Go to next hwq */
20021 next_hwqid = (next_hwqid + 1) % hwq_count;
20022
20023 next_multixri_pool =
20024 phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
20025 pbl_pool = &next_multixri_pool->pbl_pool;
20026
20027 /* Check if the public free xri pool is available */
20028 ret = _lpfc_move_xri_pbl_to_pvt(
James Smart6a828b02019-01-28 11:14:31 -080020029 phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080020030
20031 /* Exit while-loop if success or all hwqid are checked */
20032 } while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
20033
20034 /* Starting point for the next time */
20035 multixri_pool->rrb_next_hwqid = next_hwqid;
20036
20037 if (!ret) {
20038 /* stats: all public pools are empty*/
20039 multixri_pool->pbl_empty_count++;
20040 }
20041
20042#ifdef LPFC_MXP_STAT
20043 if (ret) {
20044 if (next_hwqid == hwqid)
20045 multixri_pool->local_pbl_hit_count++;
20046 else
20047 multixri_pool->other_pbl_hit_count++;
20048 }
20049#endif
20050}
20051
20052/**
20053 * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
20054 * @phba: pointer to lpfc hba data structure.
20055 * @qp: belong to which HWQ.
20056 *
20057 * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
20058 * low watermark.
20059 **/
20060void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
20061{
20062 struct lpfc_multixri_pool *multixri_pool;
20063 struct lpfc_pvt_pool *pvt_pool;
20064
20065 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
20066 pvt_pool = &multixri_pool->pvt_pool;
20067
20068 if (pvt_pool->count < pvt_pool->low_watermark)
20069 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20070}
20071
20072/**
20073 * lpfc_release_io_buf - Return one IO buf back to free pool
20074 * @phba: pointer to lpfc hba data structure.
20075 * @lpfc_ncmd: IO buf to be returned.
20076 * @qp: belong to which HWQ.
20077 *
20078 * This routine returns one IO buf back to free pool. If this is an urgent IO,
20079 * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
20080 * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
20081 * xri_limit. If cfg_xri_rebalancing==0, the IO buf is returned to
20082 * lpfc_io_buf_list_put.
20083 **/
20084void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
20085 struct lpfc_sli4_hdw_queue *qp)
20086{
20087 unsigned long iflag;
20088 struct lpfc_pbl_pool *pbl_pool;
20089 struct lpfc_pvt_pool *pvt_pool;
20090 struct lpfc_epd_pool *epd_pool;
20091 u32 txcmplq_cnt;
20092 u32 xri_owned;
20093 u32 xri_limit;
20094 u32 abts_io_bufs;
20095
20096 /* MUST zero fields if buffer is reused by another protocol */
20097 lpfc_ncmd->nvmeCmd = NULL;
20098 lpfc_ncmd->cur_iocbq.wqe_cmpl = NULL;
20099 lpfc_ncmd->cur_iocbq.iocb_cmpl = NULL;
20100
20101 if (phba->cfg_xri_rebalancing) {
20102 if (lpfc_ncmd->expedite) {
20103 /* Return to expedite pool */
20104 epd_pool = &phba->epd_pool;
20105 spin_lock_irqsave(&epd_pool->lock, iflag);
20106 list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
20107 epd_pool->count++;
20108 spin_unlock_irqrestore(&epd_pool->lock, iflag);
20109 return;
20110 }
20111
20112 /* Avoid invalid access if an IO sneaks in and is being rejected
20113 * just _after_ xri pools are destroyed in lpfc_offline.
20114 * Nothing much can be done at this point.
20115 */
20116 if (!qp->p_multixri_pool)
20117 return;
20118
20119 pbl_pool = &qp->p_multixri_pool->pbl_pool;
20120 pvt_pool = &qp->p_multixri_pool->pvt_pool;
20121
20122 txcmplq_cnt = qp->fcp_wq->pring->txcmplq_cnt;
20123 abts_io_bufs = qp->abts_scsi_io_bufs;
20124 if (qp->nvme_wq) {
20125 txcmplq_cnt += qp->nvme_wq->pring->txcmplq_cnt;
20126 abts_io_bufs += qp->abts_nvme_io_bufs;
20127 }
20128
20129 xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
20130 xri_limit = qp->p_multixri_pool->xri_limit;
20131
20132#ifdef LPFC_MXP_STAT
20133 if (xri_owned <= xri_limit)
20134 qp->p_multixri_pool->below_limit_count++;
20135 else
20136 qp->p_multixri_pool->above_limit_count++;
20137#endif
20138
20139 /* XRI goes to either public or private free xri pool
20140 * based on watermark and xri_limit
20141 */
20142 if ((pvt_pool->count < pvt_pool->low_watermark) ||
20143 (xri_owned < xri_limit &&
20144 pvt_pool->count < pvt_pool->high_watermark)) {
James Smart6a828b02019-01-28 11:14:31 -080020145 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
20146 qp, free_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020147 list_add_tail(&lpfc_ncmd->list,
20148 &pvt_pool->list);
20149 pvt_pool->count++;
20150 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20151 } else {
James Smart6a828b02019-01-28 11:14:31 -080020152 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
20153 qp, free_pub_pool);
James Smartc4908502019-01-28 11:14:28 -080020154 list_add_tail(&lpfc_ncmd->list,
20155 &pbl_pool->list);
20156 pbl_pool->count++;
20157 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20158 }
20159 } else {
James Smart6a828b02019-01-28 11:14:31 -080020160 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
20161 qp, free_xri);
James Smartc4908502019-01-28 11:14:28 -080020162 list_add_tail(&lpfc_ncmd->list,
20163 &qp->lpfc_io_buf_list_put);
20164 qp->put_io_bufs++;
20165 spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
20166 iflag);
20167 }
20168}
20169
20170/**
20171 * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
20172 * @phba: pointer to lpfc hba data structure.
20173 * @pvt_pool: pointer to private pool data structure.
20174 * @ndlp: pointer to lpfc nodelist data structure.
20175 *
20176 * This routine tries to get one free IO buf from private pool.
20177 *
20178 * Return:
20179 * pointer to one free IO buf - if private pool is not empty
20180 * NULL - if private pool is empty
20181 **/
20182static struct lpfc_io_buf *
20183lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
James Smart6a828b02019-01-28 11:14:31 -080020184 struct lpfc_sli4_hdw_queue *qp,
James Smartc4908502019-01-28 11:14:28 -080020185 struct lpfc_pvt_pool *pvt_pool,
20186 struct lpfc_nodelist *ndlp)
20187{
20188 struct lpfc_io_buf *lpfc_ncmd;
20189 struct lpfc_io_buf *lpfc_ncmd_next;
20190 unsigned long iflag;
20191
James Smart6a828b02019-01-28 11:14:31 -080020192 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020193 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20194 &pvt_pool->list, list) {
20195 if (lpfc_test_rrq_active(
20196 phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
20197 continue;
20198 list_del(&lpfc_ncmd->list);
20199 pvt_pool->count--;
20200 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20201 return lpfc_ncmd;
20202 }
20203 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20204
20205 return NULL;
20206}
20207
20208/**
20209 * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
20210 * @phba: pointer to lpfc hba data structure.
20211 *
20212 * This routine tries to get one free IO buf from expedite pool.
20213 *
20214 * Return:
20215 * pointer to one free IO buf - if expedite pool is not empty
20216 * NULL - if expedite pool is empty
20217 **/
20218static struct lpfc_io_buf *
20219lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
20220{
20221 struct lpfc_io_buf *lpfc_ncmd;
20222 struct lpfc_io_buf *lpfc_ncmd_next;
20223 unsigned long iflag;
20224 struct lpfc_epd_pool *epd_pool;
20225
20226 epd_pool = &phba->epd_pool;
20227 lpfc_ncmd = NULL;
20228
20229 spin_lock_irqsave(&epd_pool->lock, iflag);
20230 if (epd_pool->count > 0) {
20231 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20232 &epd_pool->list, list) {
20233 list_del(&lpfc_ncmd->list);
20234 epd_pool->count--;
20235 break;
20236 }
20237 }
20238 spin_unlock_irqrestore(&epd_pool->lock, iflag);
20239
20240 return lpfc_ncmd;
20241}
20242
20243/**
20244 * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
20245 * @phba: pointer to lpfc hba data structure.
20246 * @ndlp: pointer to lpfc nodelist data structure.
20247 * @hwqid: belong to which HWQ
20248 * @expedite: 1 means this request is urgent.
20249 *
20250 * This routine will do the following actions and then return a pointer to
20251 * one free IO buf.
20252 *
20253 * 1. If private free xri count is empty, move some XRIs from public to
20254 * private pool.
20255 * 2. Get one XRI from private free xri pool.
20256 * 3. If we fail to get one from pvt_pool and this is an expedite request,
20257 * get one free xri from expedite pool.
20258 *
20259 * Note: ndlp is only used on SCSI side for RRQ testing.
20260 * The caller should pass NULL for ndlp on NVME side.
20261 *
20262 * Return:
20263 * pointer to one free IO buf - if private pool is not empty
20264 * NULL - if private pool is empty
20265 **/
20266static struct lpfc_io_buf *
20267lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
20268 struct lpfc_nodelist *ndlp,
20269 int hwqid, int expedite)
20270{
20271 struct lpfc_sli4_hdw_queue *qp;
20272 struct lpfc_multixri_pool *multixri_pool;
20273 struct lpfc_pvt_pool *pvt_pool;
20274 struct lpfc_io_buf *lpfc_ncmd;
20275
20276 qp = &phba->sli4_hba.hdwq[hwqid];
20277 lpfc_ncmd = NULL;
20278 multixri_pool = qp->p_multixri_pool;
20279 pvt_pool = &multixri_pool->pvt_pool;
20280 multixri_pool->io_req_count++;
20281
20282 /* If pvt_pool is empty, move some XRIs from public to private pool */
20283 if (pvt_pool->count == 0)
20284 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20285
20286 /* Get one XRI from private free xri pool */
James Smart6a828b02019-01-28 11:14:31 -080020287 lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
James Smartc4908502019-01-28 11:14:28 -080020288
20289 if (lpfc_ncmd) {
20290 lpfc_ncmd->hdwq = qp;
20291 lpfc_ncmd->hdwq_no = hwqid;
20292 } else if (expedite) {
20293 /* If we fail to get one from pvt_pool and this is an expedite
20294 * request, get one free xri from expedite pool.
20295 */
20296 lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
20297 }
20298
20299 return lpfc_ncmd;
20300}
20301
20302static inline struct lpfc_io_buf *
20303lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
20304{
20305 struct lpfc_sli4_hdw_queue *qp;
20306 struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
20307
20308 qp = &phba->sli4_hba.hdwq[idx];
20309 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
20310 &qp->lpfc_io_buf_list_get, list) {
20311 if (lpfc_test_rrq_active(phba, ndlp,
20312 lpfc_cmd->cur_iocbq.sli4_lxritag))
20313 continue;
20314
20315 if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
20316 continue;
20317
20318 list_del_init(&lpfc_cmd->list);
20319 qp->get_io_bufs--;
20320 lpfc_cmd->hdwq = qp;
20321 lpfc_cmd->hdwq_no = idx;
20322 return lpfc_cmd;
20323 }
20324 return NULL;
20325}
20326
20327/**
20328 * lpfc_get_io_buf - Get one IO buffer from free pool
20329 * @phba: The HBA for which this call is being executed.
20330 * @ndlp: pointer to lpfc nodelist data structure.
20331 * @hwqid: belong to which HWQ
20332 * @expedite: 1 means this request is urgent.
20333 *
20334 * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
20335 * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
20336 * a IO buffer from head of @hdwq io_buf_list and returns to caller.
20337 *
20338 * Note: ndlp is only used on SCSI side for RRQ testing.
20339 * The caller should pass NULL for ndlp on NVME side.
20340 *
20341 * Return codes:
20342 * NULL - Error
20343 * Pointer to lpfc_io_buf - Success
20344 **/
20345struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
20346 struct lpfc_nodelist *ndlp,
20347 u32 hwqid, int expedite)
20348{
20349 struct lpfc_sli4_hdw_queue *qp;
20350 unsigned long iflag;
20351 struct lpfc_io_buf *lpfc_cmd;
20352
20353 qp = &phba->sli4_hba.hdwq[hwqid];
20354 lpfc_cmd = NULL;
20355
20356 if (phba->cfg_xri_rebalancing)
20357 lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
20358 phba, ndlp, hwqid, expedite);
20359 else {
James Smart6a828b02019-01-28 11:14:31 -080020360 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
20361 qp, alloc_xri_get);
James Smartc4908502019-01-28 11:14:28 -080020362 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
20363 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
20364 if (!lpfc_cmd) {
James Smart6a828b02019-01-28 11:14:31 -080020365 lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
20366 qp, alloc_xri_put);
James Smartc4908502019-01-28 11:14:28 -080020367 list_splice(&qp->lpfc_io_buf_list_put,
20368 &qp->lpfc_io_buf_list_get);
20369 qp->get_io_bufs += qp->put_io_bufs;
20370 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
20371 qp->put_io_bufs = 0;
20372 spin_unlock(&qp->io_buf_list_put_lock);
20373 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
20374 expedite)
20375 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
20376 }
20377 spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
20378 }
20379
20380 return lpfc_cmd;
20381}