blob: e0a8c800819582d5b128972a52ca8335d4a674e3 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart128bdda2018-01-30 15:59:03 -08004 * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
James Smartd080abe2017-02-12 13:52:39 -08005 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
James Smart50611572016-03-31 14:12:34 -07006 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08008 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050010 * *
11 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
dea31012005-04-17 16:05:31 -050022 *******************************************************************/
23
dea31012005-04-17 16:05:31 -050024#include <linux/blkdev.h>
25#include <linux/pci.h>
26#include <linux/interrupt.h>
27#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010029#include <linux/lockdep.h>
dea31012005-04-17 16:05:31 -050030
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040031#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050032#include <scsi/scsi_cmnd.h>
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040035#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040036#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040037#include <linux/aer.h>
James Smart1351e692018-02-22 08:18:43 -080038#ifdef CONFIG_X86
39#include <asm/set_memory.h>
40#endif
dea31012005-04-17 16:05:31 -050041
James Smart895427b2017-02-12 13:52:30 -080042#include <linux/nvme-fc-driver.h>
43
James Smartda0436e2009-05-22 14:51:39 -040044#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050045#include "lpfc_hw.h"
46#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040047#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040048#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050049#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050050#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080051#include "lpfc_scsi.h"
52#include "lpfc_nvme.h"
James Smartf358dd02017-02-12 13:52:34 -080053#include "lpfc_nvmet.h"
dea31012005-04-17 16:05:31 -050054#include "lpfc_crtn.h"
55#include "lpfc_logmsg.h"
56#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050057#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040058#include "lpfc_vport.h"
James Smart61bda8f2016-10-13 15:06:05 -070059#include "lpfc_version.h"
dea31012005-04-17 16:05:31 -050060
61/* There are only four IOCB completion types. */
62typedef enum _lpfc_iocb_type {
63 LPFC_UNKNOWN_IOCB,
64 LPFC_UNSOL_IOCB,
65 LPFC_SOL_IOCB,
66 LPFC_ABORT_IOCB
67} lpfc_iocb_type;
68
James Smart4f774512009-05-22 14:52:35 -040069
70/* Provide function prototypes local to this module. */
71static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
72 uint32_t);
73static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040074 uint8_t *, uint32_t *);
75static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
76 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040077static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
78 struct hbq_dmabuf *);
James Smartae9e28f2017-05-15 15:20:51 -070079static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
80 struct hbq_dmabuf *dmabuf);
James Smart895427b2017-02-12 13:52:30 -080081static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
James Smart05580562011-05-24 11:40:48 -040082 struct lpfc_cqe *);
James Smart895427b2017-02-12 13:52:30 -080083static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
James Smart8a9d2e82012-05-09 21:16:12 -040084 int);
Dick Kennedyf485c182017-09-29 17:34:34 -070085static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
86 struct lpfc_eqe *eqe, uint32_t qidx);
James Smarte8d3c3b2013-10-10 12:21:30 -040087static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
88static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart895427b2017-02-12 13:52:30 -080089static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
90 struct lpfc_sli_ring *pring,
91 struct lpfc_iocbq *cmdiocb);
James Smart05580562011-05-24 11:40:48 -040092
James Smart4f774512009-05-22 14:52:35 -040093static IOCB_t *
94lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
95{
96 return &iocbq->iocb;
97}
98
99/**
100 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
101 * @q: The Work Queue to operate on.
102 * @wqe: The work Queue Entry to put on the Work queue.
103 *
104 * This routine will copy the contents of @wqe to the next available entry on
105 * the @q. This function will then ring the Work Queue Doorbell to signal the
106 * HBA to start processing the Work Queue Entry. This function returns 0 if
107 * successful. If no entries are available on @q then this function will return
108 * -ENOMEM.
109 * The caller is expected to hold the hbalock when calling this routine.
110 **/
Dick Kennedycd22d602017-08-23 16:55:35 -0700111static int
James Smart205e8242018-03-05 12:04:03 -0800112lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -0400113{
James Smart2e90f4b2011-12-13 13:22:37 -0500114 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400115 struct lpfc_register doorbell;
116 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400117 uint32_t idx;
James Smart1351e692018-02-22 08:18:43 -0800118 uint32_t i = 0;
119 uint8_t *tmp;
James Smart4f774512009-05-22 14:52:35 -0400120
James Smart2e90f4b2011-12-13 13:22:37 -0500121 /* sanity check on queue memory */
122 if (unlikely(!q))
123 return -ENOMEM;
124 temp_wqe = q->qe[q->host_index].wqe;
125
James Smart4f774512009-05-22 14:52:35 -0400126 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400127 idx = ((q->host_index + 1) % q->entry_count);
128 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400129 q->WQ_overflow++;
Dick Kennedycd22d602017-08-23 16:55:35 -0700130 return -EBUSY;
James Smartb84daac2012-08-03 12:35:13 -0400131 }
132 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400133 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500134 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400135 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smart04673e32018-01-30 15:58:45 -0800136 else
137 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
James Smartfedd3b72011-02-16 12:39:24 -0500138 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
139 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400140 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
James Smart1351e692018-02-22 08:18:43 -0800141 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
142 /* write to DPP aperture taking advatage of Combined Writes */
James Smart4c066192018-03-05 10:29:03 -0800143 tmp = (uint8_t *)temp_wqe;
144#ifdef __raw_writeq
James Smart1351e692018-02-22 08:18:43 -0800145 for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
James Smart4c066192018-03-05 10:29:03 -0800146 __raw_writeq(*((uint64_t *)(tmp + i)),
147 q->dpp_regaddr + i);
148#else
149 for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
150 __raw_writel(*((uint32_t *)(tmp + i)),
151 q->dpp_regaddr + i);
152#endif
James Smart1351e692018-02-22 08:18:43 -0800153 }
154 /* ensure WQE bcopy and DPP flushed before doorbell write */
James Smart6b3b3bd2016-12-19 15:07:30 -0800155 wmb();
James Smart4f774512009-05-22 14:52:35 -0400156
157 /* Update the host index before invoking device */
158 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400159
160 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400161
162 /* Ring Doorbell */
163 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500164 if (q->db_format == LPFC_DB_LIST_FORMAT) {
James Smart1351e692018-02-22 08:18:43 -0800165 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
166 bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
167 bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
168 bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
169 q->dpp_id);
170 bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
171 q->queue_id);
172 } else {
173 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
174 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
175 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
176 }
James Smart962bc512013-01-03 15:44:00 -0500177 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
178 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
179 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
180 } else {
181 return -EINVAL;
182 }
183 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400184
185 return 0;
186}
187
188/**
189 * lpfc_sli4_wq_release - Updates internal hba index for WQ
190 * @q: The Work Queue to operate on.
191 * @index: The index to advance the hba index to.
192 *
193 * This routine will update the HBA index of a queue to reflect consumption of
194 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
195 * an entry the host calls this function to update the queue's internal
196 * pointers. This routine returns the number of entries that were consumed by
197 * the HBA.
198 **/
199static uint32_t
200lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
201{
202 uint32_t released = 0;
203
James Smart2e90f4b2011-12-13 13:22:37 -0500204 /* sanity check on queue memory */
205 if (unlikely(!q))
206 return 0;
207
James Smart4f774512009-05-22 14:52:35 -0400208 if (q->hba_index == index)
209 return 0;
210 do {
211 q->hba_index = ((q->hba_index + 1) % q->entry_count);
212 released++;
213 } while (q->hba_index != index);
214 return released;
215}
216
217/**
218 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
219 * @q: The Mailbox Queue to operate on.
220 * @wqe: The Mailbox Queue Entry to put on the Work queue.
221 *
222 * This routine will copy the contents of @mqe to the next available entry on
223 * the @q. This function will then ring the Work Queue Doorbell to signal the
224 * HBA to start processing the Work Queue Entry. This function returns 0 if
225 * successful. If no entries are available on @q then this function will return
226 * -ENOMEM.
227 * The caller is expected to hold the hbalock when calling this routine.
228 **/
229static uint32_t
230lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
231{
James Smart2e90f4b2011-12-13 13:22:37 -0500232 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400233 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400234
James Smart2e90f4b2011-12-13 13:22:37 -0500235 /* sanity check on queue memory */
236 if (unlikely(!q))
237 return -ENOMEM;
238 temp_mqe = q->qe[q->host_index].mqe;
239
James Smart4f774512009-05-22 14:52:35 -0400240 /* If the host has not yet processed the next entry then we are done */
241 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
242 return -ENOMEM;
243 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
244 /* Save off the mailbox pointer for completion */
245 q->phba->mbox = (MAILBOX_t *)temp_mqe;
246
247 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400248 q->host_index = ((q->host_index + 1) % q->entry_count);
249
250 /* Ring Doorbell */
251 doorbell.word0 = 0;
252 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
253 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
254 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400255 return 0;
256}
257
258/**
259 * lpfc_sli4_mq_release - Updates internal hba index for MQ
260 * @q: The Mailbox Queue to operate on.
261 *
262 * This routine will update the HBA index of a queue to reflect consumption of
263 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
264 * an entry the host calls this function to update the queue's internal
265 * pointers. This routine returns the number of entries that were consumed by
266 * the HBA.
267 **/
268static uint32_t
269lpfc_sli4_mq_release(struct lpfc_queue *q)
270{
James Smart2e90f4b2011-12-13 13:22:37 -0500271 /* sanity check on queue memory */
272 if (unlikely(!q))
273 return 0;
274
James Smart4f774512009-05-22 14:52:35 -0400275 /* Clear the mailbox pointer for completion */
276 q->phba->mbox = NULL;
277 q->hba_index = ((q->hba_index + 1) % q->entry_count);
278 return 1;
279}
280
281/**
282 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
283 * @q: The Event Queue to get the first valid EQE from
284 *
285 * This routine will get the first valid Event Queue Entry from @q, update
286 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
287 * the Queue (no more work to do), or the Queue is full of EQEs that have been
288 * processed, but not popped back to the HBA then this routine will return NULL.
289 **/
290static struct lpfc_eqe *
291lpfc_sli4_eq_get(struct lpfc_queue *q)
292{
James Smart7365f6f2018-02-22 08:18:46 -0800293 struct lpfc_hba *phba;
James Smart2e90f4b2011-12-13 13:22:37 -0500294 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400295 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500296
297 /* sanity check on queue memory */
298 if (unlikely(!q))
299 return NULL;
James Smart7365f6f2018-02-22 08:18:46 -0800300 phba = q->phba;
James Smart2e90f4b2011-12-13 13:22:37 -0500301 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400302
303 /* If the next EQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800304 if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400305 return NULL;
306 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400307 idx = ((q->hba_index + 1) % q->entry_count);
308 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400309 return NULL;
310
James Smart027140e2012-08-03 12:35:44 -0400311 q->hba_index = idx;
James Smart7365f6f2018-02-22 08:18:46 -0800312 /* if the index wrapped around, toggle the valid bit */
313 if (phba->sli4_hba.pc_sli4_params.eqav && !q->hba_index)
314 q->qe_valid = (q->qe_valid) ? 0 : 1;
315
James Smart27f344e2014-05-07 17:16:46 -0400316
317 /*
318 * insert barrier for instruction interlock : data from the hardware
319 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800320 * upon. Speculative instructions were allowing a bcopy at the start
321 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
322 * after our return, to copy data before the valid bit check above
323 * was done. As such, some of the copied data was stale. The barrier
324 * ensures the check is before any data is copied.
James Smart27f344e2014-05-07 17:16:46 -0400325 */
326 mb();
James Smart4f774512009-05-22 14:52:35 -0400327 return eqe;
328}
329
330/**
James Smartba20c852012-08-03 12:36:52 -0400331 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
332 * @q: The Event Queue to disable interrupts
333 *
334 **/
James Smartb71413d2018-02-22 08:18:40 -0800335inline void
James Smartba20c852012-08-03 12:36:52 -0400336lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
337{
338 struct lpfc_register doorbell;
339
340 doorbell.word0 = 0;
341 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
342 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
343 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
344 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
345 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800346 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smartba20c852012-08-03 12:36:52 -0400347}
348
349/**
James Smart27d6ac02018-02-22 08:18:42 -0800350 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
351 * @q: The Event Queue to disable interrupts
352 *
353 **/
354inline void
355lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
356{
357 struct lpfc_register doorbell;
358
359 doorbell.word0 = 0;
360 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
361 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
362 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
363 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
364 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
365 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
366}
367
368/**
James Smart4f774512009-05-22 14:52:35 -0400369 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
370 * @q: The Event Queue that the host has completed processing for.
371 * @arm: Indicates whether the host wants to arms this CQ.
372 *
373 * This routine will mark all Event Queue Entries on @q, from the last
374 * known completed entry to the last entry that was processed, as completed
375 * by clearing the valid bit for each completion queue entry. Then it will
376 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
377 * The internal host index in the @q will be updated by this routine to indicate
378 * that the host has finished processing the entries. The @arm parameter
379 * indicates that the queue should be rearmed when ringing the doorbell.
380 *
381 * This function will return the number of EQEs that were popped.
382 **/
383uint32_t
384lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
385{
386 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800387 struct lpfc_hba *phba;
James Smart4f774512009-05-22 14:52:35 -0400388 struct lpfc_eqe *temp_eqe;
389 struct lpfc_register doorbell;
390
James Smart2e90f4b2011-12-13 13:22:37 -0500391 /* sanity check on queue memory */
392 if (unlikely(!q))
393 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800394 phba = q->phba;
James Smart2e90f4b2011-12-13 13:22:37 -0500395
James Smart4f774512009-05-22 14:52:35 -0400396 /* while there are valid entries */
397 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800398 if (!phba->sli4_hba.pc_sli4_params.eqav) {
399 temp_eqe = q->qe[q->host_index].eqe;
400 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
401 }
James Smart4f774512009-05-22 14:52:35 -0400402 released++;
403 q->host_index = ((q->host_index + 1) % q->entry_count);
404 }
405 if (unlikely(released == 0 && !arm))
406 return 0;
407
408 /* ring doorbell for number popped */
409 doorbell.word0 = 0;
410 if (arm) {
411 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
412 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
413 }
414 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
415 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500416 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
417 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
418 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800419 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500420 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
421 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
James Smart9dd35422018-02-22 08:18:41 -0800422 readl(q->phba->sli4_hba.EQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400423 return released;
424}
425
426/**
James Smart27d6ac02018-02-22 08:18:42 -0800427 * lpfc_sli4_if6_eq_release - Indicates the host has finished processing an EQ
428 * @q: The Event Queue that the host has completed processing for.
429 * @arm: Indicates whether the host wants to arms this CQ.
430 *
431 * This routine will mark all Event Queue Entries on @q, from the last
432 * known completed entry to the last entry that was processed, as completed
433 * by clearing the valid bit for each completion queue entry. Then it will
434 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
435 * The internal host index in the @q will be updated by this routine to indicate
436 * that the host has finished processing the entries. The @arm parameter
437 * indicates that the queue should be rearmed when ringing the doorbell.
438 *
439 * This function will return the number of EQEs that were popped.
440 **/
441uint32_t
442lpfc_sli4_if6_eq_release(struct lpfc_queue *q, bool arm)
443{
444 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800445 struct lpfc_hba *phba;
James Smart27d6ac02018-02-22 08:18:42 -0800446 struct lpfc_eqe *temp_eqe;
447 struct lpfc_register doorbell;
448
449 /* sanity check on queue memory */
450 if (unlikely(!q))
451 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800452 phba = q->phba;
James Smart27d6ac02018-02-22 08:18:42 -0800453
454 /* while there are valid entries */
455 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800456 if (!phba->sli4_hba.pc_sli4_params.eqav) {
457 temp_eqe = q->qe[q->host_index].eqe;
458 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
459 }
James Smart27d6ac02018-02-22 08:18:42 -0800460 released++;
461 q->host_index = ((q->host_index + 1) % q->entry_count);
462 }
463 if (unlikely(released == 0 && !arm))
464 return 0;
465
466 /* ring doorbell for number popped */
467 doorbell.word0 = 0;
468 if (arm)
469 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
470 bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, released);
471 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
472 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
473 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
474 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
475 readl(q->phba->sli4_hba.EQDBregaddr);
476 return released;
477}
478
479/**
James Smart4f774512009-05-22 14:52:35 -0400480 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
481 * @q: The Completion Queue to get the first valid CQE from
482 *
483 * This routine will get the first valid Completion Queue Entry from @q, update
484 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
485 * the Queue (no more work to do), or the Queue is full of CQEs that have been
486 * processed, but not popped back to the HBA then this routine will return NULL.
487 **/
488static struct lpfc_cqe *
489lpfc_sli4_cq_get(struct lpfc_queue *q)
490{
James Smart7365f6f2018-02-22 08:18:46 -0800491 struct lpfc_hba *phba;
James Smart4f774512009-05-22 14:52:35 -0400492 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400493 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400494
James Smart2e90f4b2011-12-13 13:22:37 -0500495 /* sanity check on queue memory */
496 if (unlikely(!q))
497 return NULL;
James Smart7365f6f2018-02-22 08:18:46 -0800498 phba = q->phba;
499 cqe = q->qe[q->hba_index].cqe;
James Smart2e90f4b2011-12-13 13:22:37 -0500500
James Smart4f774512009-05-22 14:52:35 -0400501 /* If the next CQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800502 if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400503 return NULL;
504 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400505 idx = ((q->hba_index + 1) % q->entry_count);
506 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400507 return NULL;
508
James Smart027140e2012-08-03 12:35:44 -0400509 q->hba_index = idx;
James Smart7365f6f2018-02-22 08:18:46 -0800510 /* if the index wrapped around, toggle the valid bit */
511 if (phba->sli4_hba.pc_sli4_params.cqav && !q->hba_index)
512 q->qe_valid = (q->qe_valid) ? 0 : 1;
James Smart27f344e2014-05-07 17:16:46 -0400513
514 /*
515 * insert barrier for instruction interlock : data from the hardware
516 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800517 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
518 * instructions allowing action on content before valid bit checked,
519 * add barrier here as well. May not be needed as "content" is a
520 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400521 */
522 mb();
James Smart4f774512009-05-22 14:52:35 -0400523 return cqe;
524}
525
526/**
527 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
528 * @q: The Completion Queue that the host has completed processing for.
529 * @arm: Indicates whether the host wants to arms this CQ.
530 *
531 * This routine will mark all Completion queue entries on @q, from the last
532 * known completed entry to the last entry that was processed, as completed
533 * by clearing the valid bit for each completion queue entry. Then it will
534 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
535 * The internal host index in the @q will be updated by this routine to indicate
536 * that the host has finished processing the entries. The @arm parameter
537 * indicates that the queue should be rearmed when ringing the doorbell.
538 *
539 * This function will return the number of CQEs that were released.
540 **/
541uint32_t
542lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
543{
544 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800545 struct lpfc_hba *phba;
James Smart4f774512009-05-22 14:52:35 -0400546 struct lpfc_cqe *temp_qe;
547 struct lpfc_register doorbell;
548
James Smart2e90f4b2011-12-13 13:22:37 -0500549 /* sanity check on queue memory */
550 if (unlikely(!q))
551 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800552 phba = q->phba;
553
James Smart4f774512009-05-22 14:52:35 -0400554 /* while there are valid entries */
555 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800556 if (!phba->sli4_hba.pc_sli4_params.cqav) {
557 temp_qe = q->qe[q->host_index].cqe;
558 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
559 }
James Smart4f774512009-05-22 14:52:35 -0400560 released++;
561 q->host_index = ((q->host_index + 1) % q->entry_count);
562 }
563 if (unlikely(released == 0 && !arm))
564 return 0;
565
566 /* ring doorbell for number popped */
567 doorbell.word0 = 0;
568 if (arm)
569 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
570 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
571 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500572 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
573 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
574 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800575 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400576 return released;
577}
578
579/**
James Smart27d6ac02018-02-22 08:18:42 -0800580 * lpfc_sli4_if6_cq_release - Indicates the host has finished processing a CQ
581 * @q: The Completion Queue that the host has completed processing for.
582 * @arm: Indicates whether the host wants to arms this CQ.
583 *
584 * This routine will mark all Completion queue entries on @q, from the last
585 * known completed entry to the last entry that was processed, as completed
586 * by clearing the valid bit for each completion queue entry. Then it will
587 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
588 * The internal host index in the @q will be updated by this routine to indicate
589 * that the host has finished processing the entries. The @arm parameter
590 * indicates that the queue should be rearmed when ringing the doorbell.
591 *
592 * This function will return the number of CQEs that were released.
593 **/
594uint32_t
595lpfc_sli4_if6_cq_release(struct lpfc_queue *q, bool arm)
596{
597 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800598 struct lpfc_hba *phba;
James Smart27d6ac02018-02-22 08:18:42 -0800599 struct lpfc_cqe *temp_qe;
600 struct lpfc_register doorbell;
601
602 /* sanity check on queue memory */
603 if (unlikely(!q))
604 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800605 phba = q->phba;
606
James Smart27d6ac02018-02-22 08:18:42 -0800607 /* while there are valid entries */
608 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800609 if (!phba->sli4_hba.pc_sli4_params.cqav) {
610 temp_qe = q->qe[q->host_index].cqe;
611 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
612 }
James Smart27d6ac02018-02-22 08:18:42 -0800613 released++;
614 q->host_index = ((q->host_index + 1) % q->entry_count);
615 }
616 if (unlikely(released == 0 && !arm))
617 return 0;
618
619 /* ring doorbell for number popped */
620 doorbell.word0 = 0;
621 if (arm)
622 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
623 bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, released);
624 bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
625 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
626 return released;
627}
628
629/**
James Smart4f774512009-05-22 14:52:35 -0400630 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
631 * @q: The Header Receive Queue to operate on.
632 * @wqe: The Receive Queue Entry to put on the Receive queue.
633 *
634 * This routine will copy the contents of @wqe to the next available entry on
635 * the @q. This function will then ring the Receive Queue Doorbell to signal the
636 * HBA to start processing the Receive Queue Entry. This function returns the
637 * index that the rqe was copied to if successful. If no entries are available
638 * on @q then this function will return -ENOMEM.
639 * The caller is expected to hold the hbalock when calling this routine.
640 **/
James Smart895427b2017-02-12 13:52:30 -0800641int
James Smart4f774512009-05-22 14:52:35 -0400642lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
643 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
644{
James Smart2e90f4b2011-12-13 13:22:37 -0500645 struct lpfc_rqe *temp_hrqe;
646 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400647 struct lpfc_register doorbell;
James Smartcbc5de12017-12-08 17:18:04 -0800648 int hq_put_index;
649 int dq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400650
James Smart2e90f4b2011-12-13 13:22:37 -0500651 /* sanity check on queue memory */
652 if (unlikely(!hq) || unlikely(!dq))
653 return -ENOMEM;
James Smartcbc5de12017-12-08 17:18:04 -0800654 hq_put_index = hq->host_index;
655 dq_put_index = dq->host_index;
656 temp_hrqe = hq->qe[hq_put_index].rqe;
657 temp_drqe = dq->qe[dq_put_index].rqe;
James Smart2e90f4b2011-12-13 13:22:37 -0500658
James Smart4f774512009-05-22 14:52:35 -0400659 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
660 return -EINVAL;
James Smartcbc5de12017-12-08 17:18:04 -0800661 if (hq_put_index != dq_put_index)
James Smart4f774512009-05-22 14:52:35 -0400662 return -EINVAL;
663 /* If the host has not yet processed the next entry then we are done */
James Smartcbc5de12017-12-08 17:18:04 -0800664 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
James Smart4f774512009-05-22 14:52:35 -0400665 return -EBUSY;
666 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
667 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
668
669 /* Update the host index to point to the next slot */
James Smartcbc5de12017-12-08 17:18:04 -0800670 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
671 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
James Smart61f3d4b2017-05-15 15:20:41 -0700672 hq->RQ_buf_posted++;
James Smart4f774512009-05-22 14:52:35 -0400673
674 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400675 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400676 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500677 if (hq->db_format == LPFC_DB_RING_FORMAT) {
678 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
679 hq->entry_repost);
680 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
681 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
682 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
683 hq->entry_repost);
684 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
685 hq->host_index);
686 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
687 } else {
688 return -EINVAL;
689 }
690 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400691 }
James Smartcbc5de12017-12-08 17:18:04 -0800692 return hq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400693}
694
695/**
696 * lpfc_sli4_rq_release - Updates internal hba index for RQ
697 * @q: The Header Receive Queue to operate on.
698 *
699 * This routine will update the HBA index of a queue to reflect consumption of
700 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
701 * consumed an entry the host calls this function to update the queue's
702 * internal pointers. This routine returns the number of entries that were
703 * consumed by the HBA.
704 **/
705static uint32_t
706lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
707{
James Smart2e90f4b2011-12-13 13:22:37 -0500708 /* sanity check on queue memory */
709 if (unlikely(!hq) || unlikely(!dq))
710 return 0;
711
James Smart4f774512009-05-22 14:52:35 -0400712 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
713 return 0;
714 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
715 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
716 return 1;
717}
718
James Smarte59058c2008-08-24 21:49:00 -0400719/**
James Smart3621a712009-04-06 18:47:14 -0400720 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400721 * @phba: Pointer to HBA context object.
722 * @pring: Pointer to driver SLI ring object.
723 *
724 * This function returns pointer to next command iocb entry
725 * in the command ring. The caller must hold hbalock to prevent
726 * other threads consume the next command iocb.
727 * SLI-2/SLI-3 provide different sized iocbs.
728 **/
James Smarted957682007-06-17 19:56:37 -0500729static inline IOCB_t *
730lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
731{
James Smart7e56aa22012-08-03 12:35:34 -0400732 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
733 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500734}
735
James Smarte59058c2008-08-24 21:49:00 -0400736/**
James Smart3621a712009-04-06 18:47:14 -0400737 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400738 * @phba: Pointer to HBA context object.
739 * @pring: Pointer to driver SLI ring object.
740 *
741 * This function returns pointer to next response iocb entry
742 * in the response ring. The caller must hold hbalock to make sure
743 * that no other thread consume the next response iocb.
744 * SLI-2/SLI-3 provide different sized iocbs.
745 **/
James Smarted957682007-06-17 19:56:37 -0500746static inline IOCB_t *
747lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
748{
James Smart7e56aa22012-08-03 12:35:34 -0400749 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
750 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500751}
752
James Smarte59058c2008-08-24 21:49:00 -0400753/**
James Smart3621a712009-04-06 18:47:14 -0400754 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400755 * @phba: Pointer to HBA context object.
756 *
757 * This function is called with hbalock held. This function
758 * allocates a new driver iocb object from the iocb pool. If the
759 * allocation is successful, it returns pointer to the newly
760 * allocated iocb object else it returns NULL.
761 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400762struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500763__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400764{
765 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
766 struct lpfc_iocbq * iocbq = NULL;
767
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100768 lockdep_assert_held(&phba->hbalock);
769
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400770 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400771 if (iocbq)
772 phba->iocb_cnt++;
773 if (phba->iocb_cnt > phba->iocb_max)
774 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400775 return iocbq;
776}
777
James Smarte59058c2008-08-24 21:49:00 -0400778/**
James Smartda0436e2009-05-22 14:51:39 -0400779 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
780 * @phba: Pointer to HBA context object.
781 * @xritag: XRI value.
782 *
783 * This function clears the sglq pointer from the array of acive
784 * sglq's. The xritag that is passed in is used to index into the
785 * array. Before the xritag can be used it needs to be adjusted
786 * by subtracting the xribase.
787 *
788 * Returns sglq ponter = success, NULL = Failure.
789 **/
James Smart895427b2017-02-12 13:52:30 -0800790struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400791__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
792{
James Smartda0436e2009-05-22 14:51:39 -0400793 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400794
795 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
796 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400797 return sglq;
798}
799
800/**
801 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
802 * @phba: Pointer to HBA context object.
803 * @xritag: XRI value.
804 *
805 * This function returns the sglq pointer from the array of acive
806 * sglq's. The xritag that is passed in is used to index into the
807 * array. Before the xritag can be used it needs to be adjusted
808 * by subtracting the xribase.
809 *
810 * Returns sglq ponter = success, NULL = Failure.
811 **/
James Smart0f65ff62010-02-26 14:14:23 -0500812struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400813__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
814{
James Smartda0436e2009-05-22 14:51:39 -0400815 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400816
817 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400818 return sglq;
819}
820
821/**
James Smart1151e3e2011-02-16 12:39:35 -0500822 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500823 * @phba: Pointer to HBA context object.
824 * @xritag: xri used in this exchange.
825 * @rrq: The RRQ to be cleared.
826 *
James Smart19ca7602010-11-20 23:11:55 -0500827 **/
James Smart1151e3e2011-02-16 12:39:35 -0500828void
829lpfc_clr_rrq_active(struct lpfc_hba *phba,
830 uint16_t xritag,
831 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500832{
James Smart1151e3e2011-02-16 12:39:35 -0500833 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500834
James Smart1151e3e2011-02-16 12:39:35 -0500835 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
836 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500837
838 /* The target DID could have been swapped (cable swap)
839 * we should use the ndlp from the findnode if it is
840 * available.
841 */
James Smart1151e3e2011-02-16 12:39:35 -0500842 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500843 ndlp = rrq->ndlp;
844
James Smart1151e3e2011-02-16 12:39:35 -0500845 if (!ndlp)
846 goto out;
847
James Smartcff261f2013-12-17 20:29:47 -0500848 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500849 rrq->send_rrq = 0;
850 rrq->xritag = 0;
851 rrq->rrq_stop_time = 0;
852 }
James Smart1151e3e2011-02-16 12:39:35 -0500853out:
James Smart19ca7602010-11-20 23:11:55 -0500854 mempool_free(rrq, phba->rrq_pool);
855}
856
857/**
858 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
859 * @phba: Pointer to HBA context object.
860 *
861 * This function is called with hbalock held. This function
862 * Checks if stop_time (ratov from setting rrq active) has
863 * been reached, if it has and the send_rrq flag is set then
864 * it will call lpfc_send_rrq. If the send_rrq flag is not set
865 * then it will just call the routine to clear the rrq and
866 * free the rrq resource.
867 * The timer is set to the next rrq that is going to expire before
868 * leaving the routine.
869 *
870 **/
871void
872lpfc_handle_rrq_active(struct lpfc_hba *phba)
873{
874 struct lpfc_node_rrq *rrq;
875 struct lpfc_node_rrq *nextrrq;
876 unsigned long next_time;
877 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500878 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500879
880 spin_lock_irqsave(&phba->hbalock, iflags);
881 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400882 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500883 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500884 &phba->active_rrq_list, list) {
885 if (time_after(jiffies, rrq->rrq_stop_time))
886 list_move(&rrq->list, &send_rrq);
887 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500888 next_time = rrq->rrq_stop_time;
889 }
890 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500891 if ((!list_empty(&phba->active_rrq_list)) &&
892 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500893 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500894 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
895 list_del(&rrq->list);
896 if (!rrq->send_rrq)
897 /* this call will free the rrq */
898 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
899 else if (lpfc_send_rrq(phba, rrq)) {
900 /* if we send the rrq then the completion handler
901 * will clear the bit in the xribitmap.
902 */
903 lpfc_clr_rrq_active(phba, rrq->xritag,
904 rrq);
905 }
906 }
James Smart19ca7602010-11-20 23:11:55 -0500907}
908
909/**
910 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
911 * @vport: Pointer to vport context object.
912 * @xri: The xri used in the exchange.
913 * @did: The targets DID for this exchange.
914 *
915 * returns NULL = rrq not found in the phba->active_rrq_list.
916 * rrq = rrq for this xri and target.
917 **/
918struct lpfc_node_rrq *
919lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
920{
921 struct lpfc_hba *phba = vport->phba;
922 struct lpfc_node_rrq *rrq;
923 struct lpfc_node_rrq *nextrrq;
924 unsigned long iflags;
925
926 if (phba->sli_rev != LPFC_SLI_REV4)
927 return NULL;
928 spin_lock_irqsave(&phba->hbalock, iflags);
929 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
930 if (rrq->vport == vport && rrq->xritag == xri &&
931 rrq->nlp_DID == did){
932 list_del(&rrq->list);
933 spin_unlock_irqrestore(&phba->hbalock, iflags);
934 return rrq;
935 }
936 }
937 spin_unlock_irqrestore(&phba->hbalock, iflags);
938 return NULL;
939}
940
941/**
942 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
943 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500944 * @ndlp: Pointer to the lpfc_node_list structure.
945 * If ndlp is NULL Remove all active RRQs for this vport from the
946 * phba->active_rrq_list and clear the rrq.
947 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500948 **/
949void
James Smart1151e3e2011-02-16 12:39:35 -0500950lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500951
952{
953 struct lpfc_hba *phba = vport->phba;
954 struct lpfc_node_rrq *rrq;
955 struct lpfc_node_rrq *nextrrq;
956 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500957 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500958
959 if (phba->sli_rev != LPFC_SLI_REV4)
960 return;
James Smart1151e3e2011-02-16 12:39:35 -0500961 if (!ndlp) {
962 lpfc_sli4_vport_delete_els_xri_aborted(vport);
963 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500964 }
James Smart1151e3e2011-02-16 12:39:35 -0500965 spin_lock_irqsave(&phba->hbalock, iflags);
966 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
967 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
968 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500969 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500970
971 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
972 list_del(&rrq->list);
973 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
974 }
James Smart19ca7602010-11-20 23:11:55 -0500975}
976
977/**
James Smart1151e3e2011-02-16 12:39:35 -0500978 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500979 * @phba: Pointer to HBA context object.
980 * @ndlp: Targets nodelist pointer for this exchange.
981 * @xritag the xri in the bitmap to test.
982 *
983 * This function is called with hbalock held. This function
984 * returns 0 = rrq not active for this xri
985 * 1 = rrq is valid for this xri.
986 **/
James Smart1151e3e2011-02-16 12:39:35 -0500987int
988lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500989 uint16_t xritag)
990{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100991 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -0500992 if (!ndlp)
993 return 0;
James Smartcff261f2013-12-17 20:29:47 -0500994 if (!ndlp->active_rrqs_xri_bitmap)
995 return 0;
996 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500997 return 1;
998 else
999 return 0;
1000}
1001
1002/**
1003 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1004 * @phba: Pointer to HBA context object.
1005 * @ndlp: nodelist pointer for this target.
1006 * @xritag: xri used in this exchange.
1007 * @rxid: Remote Exchange ID.
1008 * @send_rrq: Flag used to determine if we should send rrq els cmd.
1009 *
1010 * This function takes the hbalock.
1011 * The active bit is always set in the active rrq xri_bitmap even
1012 * if there is no slot avaiable for the other rrq information.
1013 *
1014 * returns 0 rrq actived for this xri
1015 * < 0 No memory or invalid ndlp.
1016 **/
1017int
1018lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -05001019 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -05001020{
James Smart19ca7602010-11-20 23:11:55 -05001021 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -05001022 struct lpfc_node_rrq *rrq;
1023 int empty;
1024
1025 if (!ndlp)
1026 return -EINVAL;
1027
1028 if (!phba->cfg_enable_rrq)
1029 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001030
1031 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001032 if (phba->pport->load_flag & FC_UNLOADING) {
1033 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1034 goto out;
1035 }
1036
1037 /*
1038 * set the active bit even if there is no mem available.
1039 */
1040 if (NLP_CHK_FREE_REQ(ndlp))
1041 goto out;
1042
1043 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1044 goto out;
1045
James Smartcff261f2013-12-17 20:29:47 -05001046 if (!ndlp->active_rrqs_xri_bitmap)
1047 goto out;
1048
1049 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -05001050 goto out;
1051
James Smart19ca7602010-11-20 23:11:55 -05001052 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001053 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
1054 if (!rrq) {
1055 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1056 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1057 " DID:0x%x Send:%d\n",
1058 xritag, rxid, ndlp->nlp_DID, send_rrq);
1059 return -EINVAL;
1060 }
James Smarte5771b42013-03-01 16:37:14 -05001061 if (phba->cfg_enable_rrq == 1)
1062 rrq->send_rrq = send_rrq;
1063 else
1064 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -05001065 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -04001066 rrq->rrq_stop_time = jiffies +
1067 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -05001068 rrq->ndlp = ndlp;
1069 rrq->nlp_DID = ndlp->nlp_DID;
1070 rrq->vport = ndlp->vport;
1071 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -05001072 spin_lock_irqsave(&phba->hbalock, iflags);
1073 empty = list_empty(&phba->active_rrq_list);
1074 list_add_tail(&rrq->list, &phba->active_rrq_list);
1075 phba->hba_flag |= HBA_RRQ_ACTIVE;
1076 if (empty)
1077 lpfc_worker_wake_up(phba);
1078 spin_unlock_irqrestore(&phba->hbalock, iflags);
1079 return 0;
1080out:
1081 spin_unlock_irqrestore(&phba->hbalock, iflags);
1082 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1083 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1084 " DID:0x%x Send:%d\n",
1085 xritag, rxid, ndlp->nlp_DID, send_rrq);
1086 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001087}
1088
1089/**
James Smart895427b2017-02-12 13:52:30 -08001090 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -04001091 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -05001092 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -04001093 *
James Smartdafe8ce2014-09-03 12:56:40 -04001094 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -04001095 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -04001096 * list is not empty then it is successful, it returns pointer to the newly
1097 * allocated sglq object else it returns NULL.
1098 **/
1099static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -08001100__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -04001101{
James Smart895427b2017-02-12 13:52:30 -08001102 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -04001103 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001104 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001105 struct lpfc_scsi_buf *lpfc_cmd;
1106 struct lpfc_nodelist *ndlp;
1107 int found = 0;
1108
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001109 lockdep_assert_held(&phba->hbalock);
1110
James Smart19ca7602010-11-20 23:11:55 -05001111 if (piocbq->iocb_flag & LPFC_IO_FCP) {
1112 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
1113 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -05001114 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -04001115 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -05001116 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -04001117 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1118 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1119 ndlp = NULL;
1120 else
1121 ndlp = piocbq->context_un.ndlp;
1122 } else {
James Smart19ca7602010-11-20 23:11:55 -05001123 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -04001124 }
James Smart19ca7602010-11-20 23:11:55 -05001125
James Smart895427b2017-02-12 13:52:30 -08001126 spin_lock(&phba->sli4_hba.sgl_list_lock);
1127 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -05001128 start_sglq = sglq;
1129 while (!found) {
1130 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -08001131 break;
James Smart895427b2017-02-12 13:52:30 -08001132 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1133 test_bit(sglq->sli4_lxritag,
1134 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -05001135 /* This xri has an rrq outstanding for this DID.
1136 * put it back in the list and get another xri.
1137 */
James Smart895427b2017-02-12 13:52:30 -08001138 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001139 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -08001140 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -05001141 struct lpfc_sglq, list);
1142 if (sglq == start_sglq) {
James Smart14041bd2017-06-01 21:07:01 -07001143 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001144 sglq = NULL;
1145 break;
1146 } else
1147 continue;
1148 }
1149 sglq->ndlp = ndlp;
1150 found = 1;
James Smart6d368e52011-05-24 11:44:12 -04001151 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -05001152 sglq->state = SGL_ALLOCATED;
1153 }
James Smart895427b2017-02-12 13:52:30 -08001154 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001155 return sglq;
1156}
1157
1158/**
James Smartf358dd02017-02-12 13:52:34 -08001159 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1160 * @phba: Pointer to HBA context object.
1161 * @piocb: Pointer to the iocbq.
1162 *
1163 * This function is called with the sgl_list lock held. This function
1164 * gets a new driver sglq object from the sglq list. If the
1165 * list is not empty then it is successful, it returns pointer to the newly
1166 * allocated sglq object else it returns NULL.
1167 **/
1168struct lpfc_sglq *
1169__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1170{
1171 struct list_head *lpfc_nvmet_sgl_list;
1172 struct lpfc_sglq *sglq = NULL;
1173
1174 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1175
1176 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1177
1178 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1179 if (!sglq)
1180 return NULL;
1181 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1182 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001183 return sglq;
1184}
1185
James Smarte59058c2008-08-24 21:49:00 -04001186/**
James Smart3621a712009-04-06 18:47:14 -04001187 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001188 * @phba: Pointer to HBA context object.
1189 *
1190 * This function is called with no lock held. This function
1191 * allocates a new driver iocb object from the iocb pool. If the
1192 * allocation is successful, it returns pointer to the newly
1193 * allocated iocb object else it returns NULL.
1194 **/
James Smart2e0fef82007-06-17 19:56:36 -05001195struct lpfc_iocbq *
1196lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001197{
James Smart2e0fef82007-06-17 19:56:36 -05001198 struct lpfc_iocbq * iocbq = NULL;
1199 unsigned long iflags;
1200
1201 spin_lock_irqsave(&phba->hbalock, iflags);
1202 iocbq = __lpfc_sli_get_iocbq(phba);
1203 spin_unlock_irqrestore(&phba->hbalock, iflags);
1204 return iocbq;
1205}
1206
James Smarte59058c2008-08-24 21:49:00 -04001207/**
James Smart4f774512009-05-22 14:52:35 -04001208 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1209 * @phba: Pointer to HBA context object.
1210 * @iocbq: Pointer to driver iocb object.
1211 *
1212 * This function is called with hbalock held to release driver
1213 * iocb object to the iocb pool. The iotag in the iocb object
1214 * does not change for each use of the iocb object. This function
1215 * clears all other fields of the iocb object when it is freed.
1216 * The sqlq structure that holds the xritag and phys and virtual
1217 * mappings for the scatter gather list is retrieved from the
1218 * active array of sglq. The get of the sglq pointer also clears
1219 * the entry in the array. If the status of the IO indiactes that
1220 * this IO was aborted then the sglq entry it put on the
1221 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1222 * IO has good status or fails for any other reason then the sglq
James Smart895427b2017-02-12 13:52:30 -08001223 * entry is added to the free list (lpfc_els_sgl_list).
James Smart4f774512009-05-22 14:52:35 -04001224 **/
1225static void
1226__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1227{
1228 struct lpfc_sglq *sglq;
1229 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001230 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001231 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001232
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001233 lockdep_assert_held(&phba->hbalock);
1234
James Smart4f774512009-05-22 14:52:35 -04001235 if (iocbq->sli4_xritag == NO_XRI)
1236 sglq = NULL;
1237 else
James Smart6d368e52011-05-24 11:44:12 -04001238 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1239
James Smart0e9bb8d2013-03-01 16:35:12 -05001240
James Smart4f774512009-05-22 14:52:35 -04001241 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001242 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1243 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1244 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001245 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001246 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001247 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001248 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1249 spin_unlock_irqrestore(
1250 &phba->sli4_hba.sgl_list_lock, iflag);
1251 goto out;
1252 }
1253
James Smart895427b2017-02-12 13:52:30 -08001254 pring = phba->sli4_hba.els_wq->pring;
James Smart4f774512009-05-22 14:52:35 -04001255 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1256 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001257 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1258 iflag);
James Smart341af102010-01-26 23:07:37 -05001259 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001260 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001261 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001262 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001263 } else {
James Smart895427b2017-02-12 13:52:30 -08001264 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1265 iflag);
James Smart4f774512009-05-22 14:52:35 -04001266 sglq->state = SGL_FREED;
1267 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001268 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001269 &phba->sli4_hba.lpfc_els_sgl_list);
1270 spin_unlock_irqrestore(
1271 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001272
1273 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001274 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001275 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001276 }
James Smart4f774512009-05-22 14:52:35 -04001277 }
1278
James Smartf358dd02017-02-12 13:52:34 -08001279out:
James Smart4f774512009-05-22 14:52:35 -04001280 /*
1281 * Clean all volatile data fields, preserve iotag and node struct.
1282 */
1283 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001284 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001285 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001286 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1287 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001288 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1289}
1290
James Smart2a9bf3d2010-06-07 15:24:45 -04001291
James Smart4f774512009-05-22 14:52:35 -04001292/**
James Smart3772a992009-05-22 14:50:54 -04001293 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1294 * @phba: Pointer to HBA context object.
1295 * @iocbq: Pointer to driver iocb object.
1296 *
1297 * This function is called with hbalock held to release driver
1298 * iocb object to the iocb pool. The iotag in the iocb object
1299 * does not change for each use of the iocb object. This function
1300 * clears all other fields of the iocb object when it is freed.
1301 **/
1302static void
1303__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1304{
1305 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1306
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001307 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001308
1309 /*
James Smart3772a992009-05-22 14:50:54 -04001310 * Clean all volatile data fields, preserve iotag and node struct.
1311 */
1312 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1313 iocbq->sli4_xritag = NO_XRI;
1314 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1315}
1316
1317/**
James Smart3621a712009-04-06 18:47:14 -04001318 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001319 * @phba: Pointer to HBA context object.
1320 * @iocbq: Pointer to driver iocb object.
1321 *
1322 * This function is called with hbalock held to release driver
1323 * iocb object to the iocb pool. The iotag in the iocb object
1324 * does not change for each use of the iocb object. This function
1325 * clears all other fields of the iocb object when it is freed.
1326 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001327static void
James Smart2e0fef82007-06-17 19:56:36 -05001328__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1329{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001330 lockdep_assert_held(&phba->hbalock);
1331
James Smart3772a992009-05-22 14:50:54 -04001332 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001333 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001334}
1335
James Smarte59058c2008-08-24 21:49:00 -04001336/**
James Smart3621a712009-04-06 18:47:14 -04001337 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001338 * @phba: Pointer to HBA context object.
1339 * @iocbq: Pointer to driver iocb object.
1340 *
1341 * This function is called with no lock held to release the iocb to
1342 * iocb pool.
1343 **/
James Smart2e0fef82007-06-17 19:56:36 -05001344void
1345lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1346{
1347 unsigned long iflags;
1348
1349 /*
1350 * Clean all volatile data fields, preserve iotag and node struct.
1351 */
1352 spin_lock_irqsave(&phba->hbalock, iflags);
1353 __lpfc_sli_release_iocbq(phba, iocbq);
1354 spin_unlock_irqrestore(&phba->hbalock, iflags);
1355}
1356
James Smarte59058c2008-08-24 21:49:00 -04001357/**
James Smarta257bf92009-04-06 18:48:10 -04001358 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1359 * @phba: Pointer to HBA context object.
1360 * @iocblist: List of IOCBs.
1361 * @ulpstatus: ULP status in IOCB command field.
1362 * @ulpWord4: ULP word-4 in IOCB command field.
1363 *
1364 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1365 * on the list by invoking the complete callback function associated with the
1366 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1367 * fields.
1368 **/
1369void
1370lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1371 uint32_t ulpstatus, uint32_t ulpWord4)
1372{
1373 struct lpfc_iocbq *piocb;
1374
1375 while (!list_empty(iocblist)) {
1376 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001377 if (!piocb->iocb_cmpl)
1378 lpfc_sli_release_iocbq(phba, piocb);
1379 else {
1380 piocb->iocb.ulpStatus = ulpstatus;
1381 piocb->iocb.un.ulpWord[4] = ulpWord4;
1382 (piocb->iocb_cmpl) (phba, piocb, piocb);
1383 }
1384 }
1385 return;
1386}
1387
1388/**
James Smart3621a712009-04-06 18:47:14 -04001389 * lpfc_sli_iocb_cmd_type - Get the iocb type
1390 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001391 *
1392 * This function is called by ring event handler function to get the iocb type.
1393 * This function translates the iocb command to an iocb command type used to
1394 * decide the final disposition of each completed IOCB.
1395 * The function returns
1396 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1397 * LPFC_SOL_IOCB if it is a solicited iocb completion
1398 * LPFC_ABORT_IOCB if it is an abort iocb
1399 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1400 *
1401 * The caller is not required to hold any lock.
1402 **/
dea31012005-04-17 16:05:31 -05001403static lpfc_iocb_type
1404lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1405{
1406 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1407
1408 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1409 return 0;
1410
1411 switch (iocb_cmnd) {
1412 case CMD_XMIT_SEQUENCE_CR:
1413 case CMD_XMIT_SEQUENCE_CX:
1414 case CMD_XMIT_BCAST_CN:
1415 case CMD_XMIT_BCAST_CX:
1416 case CMD_ELS_REQUEST_CR:
1417 case CMD_ELS_REQUEST_CX:
1418 case CMD_CREATE_XRI_CR:
1419 case CMD_CREATE_XRI_CX:
1420 case CMD_GET_RPI_CN:
1421 case CMD_XMIT_ELS_RSP_CX:
1422 case CMD_GET_RPI_CR:
1423 case CMD_FCP_IWRITE_CR:
1424 case CMD_FCP_IWRITE_CX:
1425 case CMD_FCP_IREAD_CR:
1426 case CMD_FCP_IREAD_CX:
1427 case CMD_FCP_ICMND_CR:
1428 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001429 case CMD_FCP_TSEND_CX:
1430 case CMD_FCP_TRSP_CX:
1431 case CMD_FCP_TRECEIVE_CX:
1432 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001433 case CMD_ADAPTER_MSG:
1434 case CMD_ADAPTER_DUMP:
1435 case CMD_XMIT_SEQUENCE64_CR:
1436 case CMD_XMIT_SEQUENCE64_CX:
1437 case CMD_XMIT_BCAST64_CN:
1438 case CMD_XMIT_BCAST64_CX:
1439 case CMD_ELS_REQUEST64_CR:
1440 case CMD_ELS_REQUEST64_CX:
1441 case CMD_FCP_IWRITE64_CR:
1442 case CMD_FCP_IWRITE64_CX:
1443 case CMD_FCP_IREAD64_CR:
1444 case CMD_FCP_IREAD64_CX:
1445 case CMD_FCP_ICMND64_CR:
1446 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001447 case CMD_FCP_TSEND64_CX:
1448 case CMD_FCP_TRSP64_CX:
1449 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001450 case CMD_GEN_REQUEST64_CR:
1451 case CMD_GEN_REQUEST64_CX:
1452 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001453 case DSSCMD_IWRITE64_CR:
1454 case DSSCMD_IWRITE64_CX:
1455 case DSSCMD_IREAD64_CR:
1456 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001457 type = LPFC_SOL_IOCB;
1458 break;
1459 case CMD_ABORT_XRI_CN:
1460 case CMD_ABORT_XRI_CX:
1461 case CMD_CLOSE_XRI_CN:
1462 case CMD_CLOSE_XRI_CX:
1463 case CMD_XRI_ABORTED_CX:
1464 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001465 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001466 type = LPFC_ABORT_IOCB;
1467 break;
1468 case CMD_RCV_SEQUENCE_CX:
1469 case CMD_RCV_ELS_REQ_CX:
1470 case CMD_RCV_SEQUENCE64_CX:
1471 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001472 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001473 case CMD_IOCB_RCV_SEQ64_CX:
1474 case CMD_IOCB_RCV_ELS64_CX:
1475 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001476 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001477 type = LPFC_UNSOL_IOCB;
1478 break;
James Smart3163f722008-02-08 18:50:25 -05001479 case CMD_IOCB_XMIT_MSEQ64_CR:
1480 case CMD_IOCB_XMIT_MSEQ64_CX:
1481 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1482 case CMD_IOCB_RCV_ELS_LIST64_CX:
1483 case CMD_IOCB_CLOSE_EXTENDED_CN:
1484 case CMD_IOCB_ABORT_EXTENDED_CN:
1485 case CMD_IOCB_RET_HBQE64_CN:
1486 case CMD_IOCB_FCP_IBIDIR64_CR:
1487 case CMD_IOCB_FCP_IBIDIR64_CX:
1488 case CMD_IOCB_FCP_ITASKMGT64_CX:
1489 case CMD_IOCB_LOGENTRY_CN:
1490 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1491 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001492 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001493 type = LPFC_UNKNOWN_IOCB;
1494 break;
dea31012005-04-17 16:05:31 -05001495 default:
1496 type = LPFC_UNKNOWN_IOCB;
1497 break;
1498 }
1499
1500 return type;
1501}
1502
James Smarte59058c2008-08-24 21:49:00 -04001503/**
James Smart3621a712009-04-06 18:47:14 -04001504 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001505 * @phba: Pointer to HBA context object.
1506 *
1507 * This function is called from SLI initialization code
1508 * to configure every ring of the HBA's SLI interface. The
1509 * caller is not required to hold any lock. This function issues
1510 * a config_ring mailbox command for each ring.
1511 * This function returns zero if successful else returns a negative
1512 * error code.
1513 **/
dea31012005-04-17 16:05:31 -05001514static int
James Smarted957682007-06-17 19:56:37 -05001515lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001516{
1517 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001518 LPFC_MBOXQ_t *pmb;
1519 MAILBOX_t *pmbox;
1520 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001521
James Smarted957682007-06-17 19:56:37 -05001522 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1523 if (!pmb)
1524 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001525 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001526 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001527 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001528 lpfc_config_ring(phba, i, pmb);
1529 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1530 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001531 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001532 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001533 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1534 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001535 rc, pmbox->mbxCommand,
1536 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001537 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001538 ret = -ENXIO;
1539 break;
dea31012005-04-17 16:05:31 -05001540 }
1541 }
James Smarted957682007-06-17 19:56:37 -05001542 mempool_free(pmb, phba->mbox_mem_pool);
1543 return ret;
dea31012005-04-17 16:05:31 -05001544}
1545
James Smarte59058c2008-08-24 21:49:00 -04001546/**
James Smart3621a712009-04-06 18:47:14 -04001547 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001548 * @phba: Pointer to HBA context object.
1549 * @pring: Pointer to driver SLI ring object.
1550 * @piocb: Pointer to the driver iocb object.
1551 *
1552 * This function is called with hbalock held. The function adds the
1553 * new iocb to txcmplq of the given ring. This function always returns
1554 * 0. If this function is called for ELS ring, this function checks if
1555 * there is a vport associated with the ELS command. This function also
1556 * starts els_tmofunc timer if this is an ELS command.
1557 **/
dea31012005-04-17 16:05:31 -05001558static int
James Smart2e0fef82007-06-17 19:56:36 -05001559lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1560 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001561{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001562 lockdep_assert_held(&phba->hbalock);
1563
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001564 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001565
dea31012005-04-17 16:05:31 -05001566 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001567 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001568
James Smart92d7f7b2007-06-17 19:56:38 -05001569 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1570 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001571 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1572 BUG_ON(!piocb->vport);
1573 if (!(piocb->vport->load_flag & FC_UNLOADING))
1574 mod_timer(&piocb->vport->els_tmofunc,
1575 jiffies +
1576 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1577 }
dea31012005-04-17 16:05:31 -05001578
James Smart2e0fef82007-06-17 19:56:36 -05001579 return 0;
dea31012005-04-17 16:05:31 -05001580}
1581
James Smarte59058c2008-08-24 21:49:00 -04001582/**
James Smart3621a712009-04-06 18:47:14 -04001583 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001584 * @phba: Pointer to HBA context object.
1585 * @pring: Pointer to driver SLI ring object.
1586 *
1587 * This function is called with hbalock held to get next
1588 * iocb in txq of the given ring. If there is any iocb in
1589 * the txq, the function returns first iocb in the list after
1590 * removing the iocb from the list, else it returns NULL.
1591 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001592struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001593lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001594{
dea31012005-04-17 16:05:31 -05001595 struct lpfc_iocbq *cmd_iocb;
1596
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001597 lockdep_assert_held(&phba->hbalock);
1598
James Smart858c9f62007-06-17 19:56:39 -05001599 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001600 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001601}
1602
James Smarte59058c2008-08-24 21:49:00 -04001603/**
James Smart3621a712009-04-06 18:47:14 -04001604 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001605 * @phba: Pointer to HBA context object.
1606 * @pring: Pointer to driver SLI ring object.
1607 *
1608 * This function is called with hbalock held and the caller must post the
1609 * iocb without releasing the lock. If the caller releases the lock,
1610 * iocb slot returned by the function is not guaranteed to be available.
1611 * The function returns pointer to the next available iocb slot if there
1612 * is available slot in the ring, else it returns NULL.
1613 * If the get index of the ring is ahead of the put index, the function
1614 * will post an error attention event to the worker thread to take the
1615 * HBA to offline state.
1616 **/
dea31012005-04-17 16:05:31 -05001617static IOCB_t *
1618lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1619{
James Smart34b02dc2008-08-24 21:49:55 -04001620 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001621 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001622
1623 lockdep_assert_held(&phba->hbalock);
1624
James Smart7e56aa22012-08-03 12:35:34 -04001625 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1626 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1627 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001628
James Smart7e56aa22012-08-03 12:35:34 -04001629 if (unlikely(pring->sli.sli3.local_getidx ==
1630 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001631
James Smart7e56aa22012-08-03 12:35:34 -04001632 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001633
James Smart7e56aa22012-08-03 12:35:34 -04001634 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001635 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001636 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001637 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001638 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001639 pring->sli.sli3.local_getidx,
1640 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001641
James Smart2e0fef82007-06-17 19:56:36 -05001642 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001643 /*
1644 * All error attention handlers are posted to
1645 * worker thread
1646 */
1647 phba->work_ha |= HA_ERATT;
1648 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001649
James Smart5e9d9b82008-06-14 22:52:53 -04001650 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001651
1652 return NULL;
1653 }
1654
James Smart7e56aa22012-08-03 12:35:34 -04001655 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001656 return NULL;
1657 }
1658
James Smarted957682007-06-17 19:56:37 -05001659 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001660}
1661
James Smarte59058c2008-08-24 21:49:00 -04001662/**
James Smart3621a712009-04-06 18:47:14 -04001663 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001664 * @phba: Pointer to HBA context object.
1665 * @iocbq: Pointer to driver iocb object.
1666 *
1667 * This function gets an iotag for the iocb. If there is no unused iotag and
1668 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1669 * array and assigns a new iotag.
1670 * The function returns the allocated iotag if successful, else returns zero.
1671 * Zero is not a valid iotag.
1672 * The caller is not required to hold any lock.
1673 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001674uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001675lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001676{
James Smart2e0fef82007-06-17 19:56:36 -05001677 struct lpfc_iocbq **new_arr;
1678 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001679 size_t new_len;
1680 struct lpfc_sli *psli = &phba->sli;
1681 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001682
James Smart2e0fef82007-06-17 19:56:36 -05001683 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001684 iotag = psli->last_iotag;
1685 if(++iotag < psli->iocbq_lookup_len) {
1686 psli->last_iotag = iotag;
1687 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001688 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001689 iocbq->iotag = iotag;
1690 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001691 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001692 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1693 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001694 spin_unlock_irq(&phba->hbalock);
1695 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001696 GFP_KERNEL);
1697 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001698 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001699 old_arr = psli->iocbq_lookup;
1700 if (new_len <= psli->iocbq_lookup_len) {
1701 /* highly unprobable case */
1702 kfree(new_arr);
1703 iotag = psli->last_iotag;
1704 if(++iotag < psli->iocbq_lookup_len) {
1705 psli->last_iotag = iotag;
1706 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001707 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001708 iocbq->iotag = iotag;
1709 return iotag;
1710 }
James Smart2e0fef82007-06-17 19:56:36 -05001711 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001712 return 0;
1713 }
1714 if (psli->iocbq_lookup)
1715 memcpy(new_arr, old_arr,
1716 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001717 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001718 psli->iocbq_lookup = new_arr;
1719 psli->iocbq_lookup_len = new_len;
1720 psli->last_iotag = iotag;
1721 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001722 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001723 iocbq->iotag = iotag;
1724 kfree(old_arr);
1725 return iotag;
1726 }
James Smart8f6d98d2006-08-01 07:34:00 -04001727 } else
James Smart2e0fef82007-06-17 19:56:36 -05001728 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001729
James Smartbc739052010-08-04 16:11:18 -04001730 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001731 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1732 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001733
James Bottomley604a3e32005-10-29 10:28:33 -05001734 return 0;
dea31012005-04-17 16:05:31 -05001735}
1736
James Smarte59058c2008-08-24 21:49:00 -04001737/**
James Smart3621a712009-04-06 18:47:14 -04001738 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001739 * @phba: Pointer to HBA context object.
1740 * @pring: Pointer to driver SLI ring object.
1741 * @iocb: Pointer to iocb slot in the ring.
1742 * @nextiocb: Pointer to driver iocb object which need to be
1743 * posted to firmware.
1744 *
1745 * This function is called with hbalock held to post a new iocb to
1746 * the firmware. This function copies the new iocb to ring iocb slot and
1747 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1748 * a completion call back for this iocb else the function will free the
1749 * iocb object.
1750 **/
dea31012005-04-17 16:05:31 -05001751static void
1752lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1753 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1754{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001755 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001756 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001757 * Set up an iotag
dea31012005-04-17 16:05:31 -05001758 */
James Bottomley604a3e32005-10-29 10:28:33 -05001759 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001760
James Smarte2a0a9d2008-12-04 22:40:02 -05001761
James Smarta58cbd52007-08-02 11:09:43 -04001762 if (pring->ringno == LPFC_ELS_RING) {
1763 lpfc_debugfs_slow_ring_trc(phba,
1764 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1765 *(((uint32_t *) &nextiocb->iocb) + 4),
1766 *(((uint32_t *) &nextiocb->iocb) + 6),
1767 *(((uint32_t *) &nextiocb->iocb) + 7));
1768 }
1769
dea31012005-04-17 16:05:31 -05001770 /*
1771 * Issue iocb command to adapter
1772 */
James Smart92d7f7b2007-06-17 19:56:38 -05001773 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001774 wmb();
1775 pring->stats.iocb_cmd++;
1776
1777 /*
1778 * If there is no completion routine to call, we can release the
1779 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1780 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1781 */
1782 if (nextiocb->iocb_cmpl)
1783 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001784 else
James Smart2e0fef82007-06-17 19:56:36 -05001785 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001786
1787 /*
1788 * Let the HBA know what IOCB slot will be the next one the
1789 * driver will put a command into.
1790 */
James Smart7e56aa22012-08-03 12:35:34 -04001791 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1792 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001793}
1794
James Smarte59058c2008-08-24 21:49:00 -04001795/**
James Smart3621a712009-04-06 18:47:14 -04001796 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001797 * @phba: Pointer to HBA context object.
1798 * @pring: Pointer to driver SLI ring object.
1799 *
1800 * The caller is not required to hold any lock for calling this function.
1801 * This function updates the chip attention bits for the ring to inform firmware
1802 * that there are pending work to be done for this ring and requests an
1803 * interrupt when there is space available in the ring. This function is
1804 * called when the driver is unable to post more iocbs to the ring due
1805 * to unavailability of space in the ring.
1806 **/
dea31012005-04-17 16:05:31 -05001807static void
James Smart2e0fef82007-06-17 19:56:36 -05001808lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001809{
1810 int ringno = pring->ringno;
1811
1812 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1813
1814 wmb();
1815
1816 /*
1817 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1818 * The HBA will tell us when an IOCB entry is available.
1819 */
1820 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1821 readl(phba->CAregaddr); /* flush */
1822
1823 pring->stats.iocb_cmd_full++;
1824}
1825
James Smarte59058c2008-08-24 21:49:00 -04001826/**
James Smart3621a712009-04-06 18:47:14 -04001827 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001828 * @phba: Pointer to HBA context object.
1829 * @pring: Pointer to driver SLI ring object.
1830 *
1831 * This function updates the chip attention register bit for the
1832 * given ring to inform HBA that there is more work to be done
1833 * in this ring. The caller is not required to hold any lock.
1834 **/
dea31012005-04-17 16:05:31 -05001835static void
James Smart2e0fef82007-06-17 19:56:36 -05001836lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001837{
1838 int ringno = pring->ringno;
1839
1840 /*
1841 * Tell the HBA that there is work to do in this ring.
1842 */
James Smart34b02dc2008-08-24 21:49:55 -04001843 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1844 wmb();
1845 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1846 readl(phba->CAregaddr); /* flush */
1847 }
dea31012005-04-17 16:05:31 -05001848}
1849
James Smarte59058c2008-08-24 21:49:00 -04001850/**
James Smart3621a712009-04-06 18:47:14 -04001851 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001852 * @phba: Pointer to HBA context object.
1853 * @pring: Pointer to driver SLI ring object.
1854 *
1855 * This function is called with hbalock held to post pending iocbs
1856 * in the txq to the firmware. This function is called when driver
1857 * detects space available in the ring.
1858 **/
dea31012005-04-17 16:05:31 -05001859static void
James Smart2e0fef82007-06-17 19:56:36 -05001860lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001861{
1862 IOCB_t *iocb;
1863 struct lpfc_iocbq *nextiocb;
1864
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001865 lockdep_assert_held(&phba->hbalock);
1866
dea31012005-04-17 16:05:31 -05001867 /*
1868 * Check to see if:
1869 * (a) there is anything on the txq to send
1870 * (b) link is up
1871 * (c) link attention events can be processed (fcp ring only)
1872 * (d) IOCB processing is not blocked by the outstanding mbox command.
1873 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001874
1875 if (lpfc_is_link_up(phba) &&
1876 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001877 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001878 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001879
1880 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1881 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1882 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1883
1884 if (iocb)
1885 lpfc_sli_update_ring(phba, pring);
1886 else
1887 lpfc_sli_update_full_ring(phba, pring);
1888 }
1889
1890 return;
1891}
1892
James Smarte59058c2008-08-24 21:49:00 -04001893/**
James Smart3621a712009-04-06 18:47:14 -04001894 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001895 * @phba: Pointer to HBA context object.
1896 * @hbqno: HBQ number.
1897 *
1898 * This function is called with hbalock held to get the next
1899 * available slot for the given HBQ. If there is free slot
1900 * available for the HBQ it will return pointer to the next available
1901 * HBQ entry else it will return NULL.
1902 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001903static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001904lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1905{
1906 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1907
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001908 lockdep_assert_held(&phba->hbalock);
1909
James Smarted957682007-06-17 19:56:37 -05001910 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1911 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1912 hbqp->next_hbqPutIdx = 0;
1913
1914 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001915 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001916 uint32_t getidx = le32_to_cpu(raw_index);
1917
1918 hbqp->local_hbqGetIdx = getidx;
1919
1920 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1921 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001922 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001923 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001924 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001925 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001926 hbqp->entry_count);
1927
1928 phba->link_state = LPFC_HBA_ERROR;
1929 return NULL;
1930 }
1931
1932 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1933 return NULL;
1934 }
1935
James Smart51ef4c22007-08-02 11:10:31 -04001936 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1937 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001938}
1939
James Smarte59058c2008-08-24 21:49:00 -04001940/**
James Smart3621a712009-04-06 18:47:14 -04001941 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001942 * @phba: Pointer to HBA context object.
1943 *
1944 * This function is called with no lock held to free all the
1945 * hbq buffers while uninitializing the SLI interface. It also
1946 * frees the HBQ buffers returned by the firmware but not yet
1947 * processed by the upper layers.
1948 **/
James Smarted957682007-06-17 19:56:37 -05001949void
1950lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1951{
James Smart92d7f7b2007-06-17 19:56:38 -05001952 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1953 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001954 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001955 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001956
James Smart51ef4c22007-08-02 11:10:31 -04001957 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001958 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001959 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001960 for (i = 0; i < hbq_count; ++i) {
1961 list_for_each_entry_safe(dmabuf, next_dmabuf,
1962 &phba->hbqs[i].hbq_buffer_list, list) {
1963 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1964 list_del(&hbq_buf->dbuf.list);
1965 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1966 }
James Smarta8adb832007-10-27 13:37:53 -04001967 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001968 }
James Smart3163f722008-02-08 18:50:25 -05001969
1970 /* Mark the HBQs not in use */
1971 phba->hbq_in_use = 0;
1972 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001973}
1974
James Smarte59058c2008-08-24 21:49:00 -04001975/**
James Smart3621a712009-04-06 18:47:14 -04001976 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001977 * @phba: Pointer to HBA context object.
1978 * @hbqno: HBQ number.
1979 * @hbq_buf: Pointer to HBQ buffer.
1980 *
1981 * This function is called with the hbalock held to post a
1982 * hbq buffer to the firmware. If the function finds an empty
1983 * slot in the HBQ, it will post the buffer. The function will return
1984 * pointer to the hbq entry if it successfully post the buffer
1985 * else it will return NULL.
1986 **/
James Smart3772a992009-05-22 14:50:54 -04001987static int
James Smarted957682007-06-17 19:56:37 -05001988lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001989 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001990{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001991 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04001992 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1993}
1994
1995/**
1996 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1997 * @phba: Pointer to HBA context object.
1998 * @hbqno: HBQ number.
1999 * @hbq_buf: Pointer to HBQ buffer.
2000 *
2001 * This function is called with the hbalock held to post a hbq buffer to the
2002 * firmware. If the function finds an empty slot in the HBQ, it will post the
2003 * buffer and place it on the hbq_buffer_list. The function will return zero if
2004 * it successfully post the buffer else it will return an error.
2005 **/
2006static int
2007lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2008 struct hbq_dmabuf *hbq_buf)
2009{
James Smarted957682007-06-17 19:56:37 -05002010 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05002011 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05002012
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002013 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05002014 /* Get next HBQ entry slot to use */
2015 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2016 if (hbqe) {
2017 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2018
James Smart92d7f7b2007-06-17 19:56:38 -05002019 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2020 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08002021 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05002022 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002023 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2024 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2025 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05002026 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2027 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05002028 /* flush */
James Smarted957682007-06-17 19:56:37 -05002029 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04002030 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04002031 return 0;
2032 } else
2033 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05002034}
2035
James Smart4f774512009-05-22 14:52:35 -04002036/**
2037 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2038 * @phba: Pointer to HBA context object.
2039 * @hbqno: HBQ number.
2040 * @hbq_buf: Pointer to HBQ buffer.
2041 *
2042 * This function is called with the hbalock held to post an RQE to the SLI4
2043 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2044 * the hbq_buffer_list and return zero, otherwise it will return an error.
2045 **/
2046static int
2047lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2048 struct hbq_dmabuf *hbq_buf)
2049{
2050 int rc;
2051 struct lpfc_rqe hrqe;
2052 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08002053 struct lpfc_queue *hrq;
2054 struct lpfc_queue *drq;
2055
2056 if (hbqno != LPFC_ELS_HBQ)
2057 return 1;
2058 hrq = phba->sli4_hba.hdr_rq;
2059 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04002060
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002061 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04002062 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2063 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2064 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2065 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08002066 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04002067 if (rc < 0)
2068 return rc;
James Smart895427b2017-02-12 13:52:30 -08002069 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04002070 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2071 return 0;
2072}
2073
James Smarte59058c2008-08-24 21:49:00 -04002074/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05002075static struct lpfc_hbq_init lpfc_els_hbq = {
2076 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05002077 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05002078 .mask_count = 0,
2079 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04002080 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05002081 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04002082 .init_count = 40,
2083 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05002084};
James Smarted957682007-06-17 19:56:37 -05002085
James Smarte59058c2008-08-24 21:49:00 -04002086/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04002087struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05002088 &lpfc_els_hbq,
2089};
2090
James Smarte59058c2008-08-24 21:49:00 -04002091/**
James Smart3621a712009-04-06 18:47:14 -04002092 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04002093 * @phba: Pointer to HBA context object.
2094 * @hbqno: HBQ number.
2095 * @count: Number of HBQ buffers to be posted.
2096 *
James Smartd7c255b2008-08-24 21:50:00 -04002097 * This function is called with no lock held to post more hbq buffers to the
2098 * given HBQ. The function returns the number of HBQ buffers successfully
2099 * posted.
James Smarte59058c2008-08-24 21:49:00 -04002100 **/
James Smart311464e2007-08-02 11:10:37 -04002101static int
James Smart92d7f7b2007-06-17 19:56:38 -05002102lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2103{
James Smartd7c255b2008-08-24 21:50:00 -04002104 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05002105 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05002106 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04002107 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002108 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04002109 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04002110
James Smartd7c255b2008-08-24 21:50:00 -04002111 if ((phba->hbqs[hbqno].buffer_count + count) >
2112 lpfc_hbq_defs[hbqno]->entry_count)
2113 count = lpfc_hbq_defs[hbqno]->entry_count -
2114 phba->hbqs[hbqno].buffer_count;
2115 if (!count)
2116 return 0;
2117 /* Allocate HBQ entries */
2118 for (i = 0; i < count; i++) {
2119 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2120 if (!hbq_buffer)
2121 break;
2122 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2123 }
James Smart3163f722008-02-08 18:50:25 -05002124 /* Check whether HBQ is still in use */
2125 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002126 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04002127 goto err;
2128 while (!list_empty(&hbq_buf_list)) {
2129 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2130 dbuf.list);
2131 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2132 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04002133 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04002134 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04002135 posted++;
2136 } else
James Smart51ef4c22007-08-02 11:10:31 -04002137 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05002138 }
James Smart3163f722008-02-08 18:50:25 -05002139 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04002140 return posted;
2141err:
2142 spin_unlock_irqrestore(&phba->hbalock, flags);
2143 while (!list_empty(&hbq_buf_list)) {
2144 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2145 dbuf.list);
2146 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2147 }
James Smart92d7f7b2007-06-17 19:56:38 -05002148 return 0;
James Smarted957682007-06-17 19:56:37 -05002149}
2150
James Smarte59058c2008-08-24 21:49:00 -04002151/**
James Smart3621a712009-04-06 18:47:14 -04002152 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04002153 * @phba: Pointer to HBA context object.
2154 * @qno: HBQ number.
2155 *
2156 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04002157 * is called with no lock held. The function returns the number of HBQ entries
2158 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002159 **/
James Smarted957682007-06-17 19:56:37 -05002160int
James Smart92d7f7b2007-06-17 19:56:38 -05002161lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002162{
James Smartdef9c7a2009-12-21 17:02:28 -05002163 if (phba->sli_rev == LPFC_SLI_REV4)
2164 return 0;
2165 else
2166 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2167 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05002168}
2169
James Smarte59058c2008-08-24 21:49:00 -04002170/**
James Smart3621a712009-04-06 18:47:14 -04002171 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002172 * @phba: Pointer to HBA context object.
2173 * @qno: HBQ queue number.
2174 *
2175 * This function is called from SLI initialization code path with
2176 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002177 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002178 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002179static int
James Smart92d7f7b2007-06-17 19:56:38 -05002180lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002181{
James Smartdef9c7a2009-12-21 17:02:28 -05002182 if (phba->sli_rev == LPFC_SLI_REV4)
2183 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002184 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002185 else
2186 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2187 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002188}
2189
James Smarte59058c2008-08-24 21:49:00 -04002190/**
James Smart3772a992009-05-22 14:50:54 -04002191 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2192 * @phba: Pointer to HBA context object.
2193 * @hbqno: HBQ number.
2194 *
2195 * This function removes the first hbq buffer on an hbq list and returns a
2196 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2197 **/
2198static struct hbq_dmabuf *
2199lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2200{
2201 struct lpfc_dmabuf *d_buf;
2202
2203 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2204 if (!d_buf)
2205 return NULL;
2206 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2207}
2208
2209/**
James Smart2d7dbc42017-02-12 13:52:35 -08002210 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2211 * @phba: Pointer to HBA context object.
2212 * @hbqno: HBQ number.
2213 *
2214 * This function removes the first RQ buffer on an RQ buffer list and returns a
2215 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2216 **/
2217static struct rqb_dmabuf *
2218lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2219{
2220 struct lpfc_dmabuf *h_buf;
2221 struct lpfc_rqb *rqbp;
2222
2223 rqbp = hrq->rqbp;
2224 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2225 struct lpfc_dmabuf, list);
2226 if (!h_buf)
2227 return NULL;
2228 rqbp->buffer_count--;
2229 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2230}
2231
2232/**
James Smart3621a712009-04-06 18:47:14 -04002233 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002234 * @phba: Pointer to HBA context object.
2235 * @tag: Tag of the hbq buffer.
2236 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002237 * This function searches for the hbq buffer associated with the given tag in
2238 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2239 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002240 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002241static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002242lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2243{
James Smart92d7f7b2007-06-17 19:56:38 -05002244 struct lpfc_dmabuf *d_buf;
2245 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002246 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002247
James Smart51ef4c22007-08-02 11:10:31 -04002248 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002249 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002250 return NULL;
2251
James Smart3772a992009-05-22 14:50:54 -04002252 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002253 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002254 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002255 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002256 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002257 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002258 }
2259 }
James Smart3772a992009-05-22 14:50:54 -04002260 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002261 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002262 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002263 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002264 return NULL;
James Smarted957682007-06-17 19:56:37 -05002265}
2266
James Smarte59058c2008-08-24 21:49:00 -04002267/**
James Smart3621a712009-04-06 18:47:14 -04002268 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002269 * @phba: Pointer to HBA context object.
2270 * @hbq_buffer: Pointer to HBQ buffer.
2271 *
2272 * This function is called with hbalock. This function gives back
2273 * the hbq buffer to firmware. If the HBQ does not have space to
2274 * post the buffer, it will free the buffer.
2275 **/
James Smarted957682007-06-17 19:56:37 -05002276void
James Smart51ef4c22007-08-02 11:10:31 -04002277lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002278{
2279 uint32_t hbqno;
2280
James Smart51ef4c22007-08-02 11:10:31 -04002281 if (hbq_buffer) {
2282 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002283 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002284 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002285 }
2286}
2287
James Smarte59058c2008-08-24 21:49:00 -04002288/**
James Smart3621a712009-04-06 18:47:14 -04002289 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002290 * @mbxCommand: mailbox command code.
2291 *
2292 * This function is called by the mailbox event handler function to verify
2293 * that the completed mailbox command is a legitimate mailbox command. If the
2294 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2295 * and the mailbox event handler will take the HBA offline.
2296 **/
dea31012005-04-17 16:05:31 -05002297static int
2298lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2299{
2300 uint8_t ret;
2301
2302 switch (mbxCommand) {
2303 case MBX_LOAD_SM:
2304 case MBX_READ_NV:
2305 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002306 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002307 case MBX_RUN_BIU_DIAG:
2308 case MBX_INIT_LINK:
2309 case MBX_DOWN_LINK:
2310 case MBX_CONFIG_LINK:
2311 case MBX_CONFIG_RING:
2312 case MBX_RESET_RING:
2313 case MBX_READ_CONFIG:
2314 case MBX_READ_RCONFIG:
2315 case MBX_READ_SPARM:
2316 case MBX_READ_STATUS:
2317 case MBX_READ_RPI:
2318 case MBX_READ_XRI:
2319 case MBX_READ_REV:
2320 case MBX_READ_LNK_STAT:
2321 case MBX_REG_LOGIN:
2322 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002323 case MBX_CLEAR_LA:
2324 case MBX_DUMP_MEMORY:
2325 case MBX_DUMP_CONTEXT:
2326 case MBX_RUN_DIAGS:
2327 case MBX_RESTART:
2328 case MBX_UPDATE_CFG:
2329 case MBX_DOWN_LOAD:
2330 case MBX_DEL_LD_ENTRY:
2331 case MBX_RUN_PROGRAM:
2332 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002333 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002334 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002335 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002336 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002337 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002338 case MBX_LOAD_AREA:
2339 case MBX_RUN_BIU_DIAG64:
2340 case MBX_CONFIG_PORT:
2341 case MBX_READ_SPARM64:
2342 case MBX_READ_RPI64:
2343 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002344 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002345 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002346 case MBX_SET_DEBUG:
2347 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002348 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002349 case MBX_REG_VPI:
2350 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002351 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002352 case MBX_PORT_CAPABILITIES:
2353 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002354 case MBX_SLI4_CONFIG:
2355 case MBX_SLI4_REQ_FTRS:
2356 case MBX_REG_FCFI:
2357 case MBX_UNREG_FCFI:
2358 case MBX_REG_VFI:
2359 case MBX_UNREG_VFI:
2360 case MBX_INIT_VPI:
2361 case MBX_INIT_VFI:
2362 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002363 case MBX_READ_EVENT_LOG_STATUS:
2364 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002365 case MBX_SECURITY_MGMT:
2366 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002367 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002368 ret = mbxCommand;
2369 break;
2370 default:
2371 ret = MBX_SHUTDOWN;
2372 break;
2373 }
James Smart2e0fef82007-06-17 19:56:36 -05002374 return ret;
dea31012005-04-17 16:05:31 -05002375}
James Smarte59058c2008-08-24 21:49:00 -04002376
2377/**
James Smart3621a712009-04-06 18:47:14 -04002378 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002379 * @phba: Pointer to HBA context object.
2380 * @pmboxq: Pointer to mailbox command.
2381 *
2382 * This is completion handler function for mailbox commands issued from
2383 * lpfc_sli_issue_mbox_wait function. This function is called by the
2384 * mailbox event handler function with no lock held. This function
2385 * will wake up thread waiting on the wait queue pointed by context1
2386 * of the mailbox.
2387 **/
James Smart04c68492009-05-22 14:52:52 -04002388void
James Smart2e0fef82007-06-17 19:56:36 -05002389lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002390{
James Smart858c9f62007-06-17 19:56:39 -05002391 unsigned long drvr_flag;
James Smarte29d74f2018-03-05 12:04:07 -08002392 struct completion *pmbox_done;
dea31012005-04-17 16:05:31 -05002393
2394 /*
James Smarte29d74f2018-03-05 12:04:07 -08002395 * If pmbox_done is empty, the driver thread gave up waiting and
dea31012005-04-17 16:05:31 -05002396 * continued running.
2397 */
James Smart7054a602007-04-25 09:52:34 -04002398 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002399 spin_lock_irqsave(&phba->hbalock, drvr_flag);
James Smarte29d74f2018-03-05 12:04:07 -08002400 pmbox_done = (struct completion *)pmboxq->context3;
2401 if (pmbox_done)
2402 complete(pmbox_done);
James Smart858c9f62007-06-17 19:56:39 -05002403 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002404 return;
2405}
2406
James Smarte59058c2008-08-24 21:49:00 -04002407
2408/**
James Smart3621a712009-04-06 18:47:14 -04002409 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002410 * @phba: Pointer to HBA context object.
2411 * @pmb: Pointer to mailbox object.
2412 *
2413 * This function is the default mailbox completion handler. It
2414 * frees the memory resources associated with the completed mailbox
2415 * command. If the completed command is a REG_LOGIN mailbox command,
2416 * this function will issue a UREG_LOGIN to re-claim the RPI.
2417 **/
dea31012005-04-17 16:05:31 -05002418void
James Smart2e0fef82007-06-17 19:56:36 -05002419lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002420{
James Smartd439d282010-09-29 11:18:45 -04002421 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002422 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002423 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002424 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002425 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002426 int rc;
2427
dea31012005-04-17 16:05:31 -05002428 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002429
dea31012005-04-17 16:05:31 -05002430 if (mp) {
2431 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2432 kfree(mp);
2433 }
James Smart7054a602007-04-25 09:52:34 -04002434
2435 /*
2436 * If a REG_LOGIN succeeded after node is destroyed or node
2437 * is in re-discovery driver need to cleanup the RPI.
2438 */
James Smart2e0fef82007-06-17 19:56:36 -05002439 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002440 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2441 !pmb->u.mb.mbxStatus) {
2442 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002443 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002444 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002445 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002446 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002447 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2448 if (rc != MBX_NOT_FINISHED)
2449 return;
2450 }
2451
James Smart695a8142010-01-26 23:08:03 -05002452 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2453 !(phba->pport->load_flag & FC_UNLOADING) &&
2454 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002455 shost = lpfc_shost_from_vport(vport);
2456 spin_lock_irq(shost->host_lock);
2457 vport->vpi_state |= LPFC_VPI_REGISTERED;
2458 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2459 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002460 }
2461
James Smartd439d282010-09-29 11:18:45 -04002462 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2463 ndlp = (struct lpfc_nodelist *)pmb->context2;
2464 lpfc_nlp_put(ndlp);
2465 pmb->context2 = NULL;
2466 }
2467
James Smartdcf2a4e2010-09-29 11:18:53 -04002468 /* Check security permission status on INIT_LINK mailbox command */
2469 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2470 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2471 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2472 "2860 SLI authentication is required "
2473 "for INIT_LINK but has not done yet\n");
2474
James Smart04c68492009-05-22 14:52:52 -04002475 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2476 lpfc_sli4_mbox_cmd_free(phba, pmb);
2477 else
2478 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002479}
James Smartbe6bb942015-04-07 15:07:22 -04002480 /**
2481 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2482 * @phba: Pointer to HBA context object.
2483 * @pmb: Pointer to mailbox object.
2484 *
2485 * This function is the unreg rpi mailbox completion handler. It
2486 * frees the memory resources associated with the completed mailbox
2487 * command. An additional refrenece is put on the ndlp to prevent
2488 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2489 * the unreg mailbox command completes, this routine puts the
2490 * reference back.
2491 *
2492 **/
2493void
2494lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2495{
2496 struct lpfc_vport *vport = pmb->vport;
2497 struct lpfc_nodelist *ndlp;
2498
2499 ndlp = pmb->context1;
2500 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2501 if (phba->sli_rev == LPFC_SLI_REV4 &&
2502 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002503 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002504 LPFC_SLI_INTF_IF_TYPE_2)) {
2505 if (ndlp) {
2506 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2507 "0010 UNREG_LOGIN vpi:%x "
2508 "rpi:%x DID:%x map:%x %p\n",
2509 vport->vpi, ndlp->nlp_rpi,
2510 ndlp->nlp_DID,
2511 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002512 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002513 lpfc_nlp_put(ndlp);
2514 }
2515 }
2516 }
2517
2518 mempool_free(pmb, phba->mbox_mem_pool);
2519}
dea31012005-04-17 16:05:31 -05002520
James Smarte59058c2008-08-24 21:49:00 -04002521/**
James Smart3621a712009-04-06 18:47:14 -04002522 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002523 * @phba: Pointer to HBA context object.
2524 *
2525 * This function is called with no lock held. This function processes all
2526 * the completed mailbox commands and gives it to upper layers. The interrupt
2527 * service routine processes mailbox completion interrupt and adds completed
2528 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2529 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2530 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2531 * function returns the mailbox commands to the upper layer by calling the
2532 * completion handler function of each mailbox.
2533 **/
dea31012005-04-17 16:05:31 -05002534int
James Smart2e0fef82007-06-17 19:56:36 -05002535lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002536{
James Smart92d7f7b2007-06-17 19:56:38 -05002537 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002538 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002539 int rc;
2540 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002541
2542 phba->sli.slistat.mbox_event++;
2543
James Smart92d7f7b2007-06-17 19:56:38 -05002544 /* Get all completed mailboxe buffers into the cmplq */
2545 spin_lock_irq(&phba->hbalock);
2546 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2547 spin_unlock_irq(&phba->hbalock);
2548
dea31012005-04-17 16:05:31 -05002549 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002550 do {
2551 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2552 if (pmb == NULL)
2553 break;
2554
James Smart04c68492009-05-22 14:52:52 -04002555 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002556
James Smart858c9f62007-06-17 19:56:39 -05002557 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2558 if (pmb->vport) {
2559 lpfc_debugfs_disc_trc(pmb->vport,
2560 LPFC_DISC_TRC_MBOX_VPORT,
2561 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2562 (uint32_t)pmbox->mbxCommand,
2563 pmbox->un.varWords[0],
2564 pmbox->un.varWords[1]);
2565 }
2566 else {
2567 lpfc_debugfs_disc_trc(phba->pport,
2568 LPFC_DISC_TRC_MBOX,
2569 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2570 (uint32_t)pmbox->mbxCommand,
2571 pmbox->un.varWords[0],
2572 pmbox->un.varWords[1]);
2573 }
2574 }
2575
dea31012005-04-17 16:05:31 -05002576 /*
2577 * It is a fatal error if unknown mbox command completion.
2578 */
2579 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2580 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002581 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002582 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002583 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002584 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002585 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002586 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002587 lpfc_sli_config_mbox_subsys_get(phba,
2588 pmb),
2589 lpfc_sli_config_mbox_opcode_get(phba,
2590 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002591 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002592 phba->work_hs = HS_FFER3;
2593 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002594 continue;
dea31012005-04-17 16:05:31 -05002595 }
2596
dea31012005-04-17 16:05:31 -05002597 if (pmbox->mbxStatus) {
2598 phba->sli.slistat.mbox_stat_err++;
2599 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2600 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002601 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002602 LOG_MBOX | LOG_SLI,
2603 "(%d):0305 Mbox cmd cmpl "
2604 "error - RETRYing Data: x%x "
2605 "(x%x/x%x) x%x x%x x%x\n",
2606 pmb->vport ? pmb->vport->vpi : 0,
2607 pmbox->mbxCommand,
2608 lpfc_sli_config_mbox_subsys_get(phba,
2609 pmb),
2610 lpfc_sli_config_mbox_opcode_get(phba,
2611 pmb),
2612 pmbox->mbxStatus,
2613 pmbox->un.varWords[0],
2614 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002615 pmbox->mbxStatus = 0;
2616 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002617 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002618 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002619 continue;
dea31012005-04-17 16:05:31 -05002620 }
2621 }
2622
2623 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002624 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002625 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002626 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2627 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002628 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002629 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002630 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2631 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002632 pmb->mbox_cmpl,
2633 *((uint32_t *) pmbox),
2634 pmbox->un.varWords[0],
2635 pmbox->un.varWords[1],
2636 pmbox->un.varWords[2],
2637 pmbox->un.varWords[3],
2638 pmbox->un.varWords[4],
2639 pmbox->un.varWords[5],
2640 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002641 pmbox->un.varWords[7],
2642 pmbox->un.varWords[8],
2643 pmbox->un.varWords[9],
2644 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002645
James Smart92d7f7b2007-06-17 19:56:38 -05002646 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002647 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002648 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002649 return 0;
dea31012005-04-17 16:05:31 -05002650}
James Smart92d7f7b2007-06-17 19:56:38 -05002651
James Smarte59058c2008-08-24 21:49:00 -04002652/**
James Smart3621a712009-04-06 18:47:14 -04002653 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002654 * @phba: Pointer to HBA context object.
2655 * @pring: Pointer to driver SLI ring object.
2656 * @tag: buffer tag.
2657 *
2658 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2659 * is set in the tag the buffer is posted for a particular exchange,
2660 * the function will return the buffer without replacing the buffer.
2661 * If the buffer is for unsolicited ELS or CT traffic, this function
2662 * returns the buffer and also posts another buffer to the firmware.
2663 **/
James Smart76bb24e2007-10-27 13:38:00 -04002664static struct lpfc_dmabuf *
2665lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002666 struct lpfc_sli_ring *pring,
2667 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002668{
James Smart9f1e1b52008-12-04 22:39:40 -05002669 struct hbq_dmabuf *hbq_entry;
2670
James Smart76bb24e2007-10-27 13:38:00 -04002671 if (tag & QUE_BUFTAG_BIT)
2672 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002673 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2674 if (!hbq_entry)
2675 return NULL;
2676 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002677}
James Smart57127f12007-10-27 13:37:05 -04002678
James Smart3772a992009-05-22 14:50:54 -04002679/**
2680 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2681 * @phba: Pointer to HBA context object.
2682 * @pring: Pointer to driver SLI ring object.
2683 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2684 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2685 * @fch_type: the type for the first frame of the sequence.
2686 *
2687 * This function is called with no lock held. This function uses the r_ctl and
2688 * type of the received sequence to find the correct callback function to call
2689 * to process the sequence.
2690 **/
2691static int
2692lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2693 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2694 uint32_t fch_type)
2695{
2696 int i;
2697
James Smartf358dd02017-02-12 13:52:34 -08002698 switch (fch_type) {
2699 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002700 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002701 return 1;
2702 default:
2703 break;
2704 }
2705
James Smart3772a992009-05-22 14:50:54 -04002706 /* unSolicited Responses */
2707 if (pring->prt[0].profile) {
2708 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2709 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2710 saveq);
2711 return 1;
2712 }
2713 /* We must search, based on rctl / type
2714 for the right routine */
2715 for (i = 0; i < pring->num_mask; i++) {
2716 if ((pring->prt[i].rctl == fch_r_ctl) &&
2717 (pring->prt[i].type == fch_type)) {
2718 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2719 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2720 (phba, pring, saveq);
2721 return 1;
2722 }
2723 }
2724 return 0;
2725}
James Smarte59058c2008-08-24 21:49:00 -04002726
2727/**
James Smart3621a712009-04-06 18:47:14 -04002728 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002729 * @phba: Pointer to HBA context object.
2730 * @pring: Pointer to driver SLI ring object.
2731 * @saveq: Pointer to the unsolicited iocb.
2732 *
2733 * This function is called with no lock held by the ring event handler
2734 * when there is an unsolicited iocb posted to the response ring by the
2735 * firmware. This function gets the buffer associated with the iocbs
2736 * and calls the event handler for the ring. This function handles both
2737 * qring buffers and hbq buffers.
2738 * When the function returns 1 the caller can free the iocb object otherwise
2739 * upper layer functions will free the iocb objects.
2740 **/
dea31012005-04-17 16:05:31 -05002741static int
2742lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2743 struct lpfc_iocbq *saveq)
2744{
2745 IOCB_t * irsp;
2746 WORD5 * w5p;
2747 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002748 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002749 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002750
dea31012005-04-17 16:05:31 -05002751 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002752
2753 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2754 if (pring->lpfc_sli_rcv_async_status)
2755 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2756 else
2757 lpfc_printf_log(phba,
2758 KERN_WARNING,
2759 LOG_SLI,
2760 "0316 Ring %d handler: unexpected "
2761 "ASYNC_STATUS iocb received evt_code "
2762 "0x%x\n",
2763 pring->ringno,
2764 irsp->un.asyncstat.evt_code);
2765 return 1;
2766 }
2767
James Smart3163f722008-02-08 18:50:25 -05002768 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2769 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2770 if (irsp->ulpBdeCount > 0) {
2771 dmzbuf = lpfc_sli_get_buff(phba, pring,
2772 irsp->un.ulpWord[3]);
2773 lpfc_in_buf_free(phba, dmzbuf);
2774 }
2775
2776 if (irsp->ulpBdeCount > 1) {
2777 dmzbuf = lpfc_sli_get_buff(phba, pring,
2778 irsp->unsli3.sli3Words[3]);
2779 lpfc_in_buf_free(phba, dmzbuf);
2780 }
2781
2782 if (irsp->ulpBdeCount > 2) {
2783 dmzbuf = lpfc_sli_get_buff(phba, pring,
2784 irsp->unsli3.sli3Words[7]);
2785 lpfc_in_buf_free(phba, dmzbuf);
2786 }
2787
2788 return 1;
2789 }
2790
James Smart92d7f7b2007-06-17 19:56:38 -05002791 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002792 if (irsp->ulpBdeCount != 0) {
2793 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002794 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002795 if (!saveq->context2)
2796 lpfc_printf_log(phba,
2797 KERN_ERR,
2798 LOG_SLI,
2799 "0341 Ring %d Cannot find buffer for "
2800 "an unsolicited iocb. tag 0x%x\n",
2801 pring->ringno,
2802 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002803 }
2804 if (irsp->ulpBdeCount == 2) {
2805 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002806 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002807 if (!saveq->context3)
2808 lpfc_printf_log(phba,
2809 KERN_ERR,
2810 LOG_SLI,
2811 "0342 Ring %d Cannot find buffer for an"
2812 " unsolicited iocb. tag 0x%x\n",
2813 pring->ringno,
2814 irsp->unsli3.sli3Words[7]);
2815 }
2816 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002817 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002818 if (irsp->ulpBdeCount != 0) {
2819 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2820 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002821 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002822 lpfc_printf_log(phba,
2823 KERN_ERR,
2824 LOG_SLI,
2825 "0343 Ring %d Cannot find "
2826 "buffer for an unsolicited iocb"
2827 ". tag 0x%x\n", pring->ringno,
2828 irsp->un.ulpWord[3]);
2829 }
2830 if (irsp->ulpBdeCount == 2) {
2831 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2832 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002833 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002834 lpfc_printf_log(phba,
2835 KERN_ERR,
2836 LOG_SLI,
2837 "0344 Ring %d Cannot find "
2838 "buffer for an unsolicited "
2839 "iocb. tag 0x%x\n",
2840 pring->ringno,
2841 irsp->unsli3.sli3Words[7]);
2842 }
2843 }
James Smart92d7f7b2007-06-17 19:56:38 -05002844 }
James Smart9c2face2008-01-11 01:53:18 -05002845 if (irsp->ulpBdeCount != 0 &&
2846 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2847 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2848 int found = 0;
2849
2850 /* search continue save q for same XRI */
2851 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002852 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2853 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002854 list_add_tail(&saveq->list, &iocbq->list);
2855 found = 1;
2856 break;
2857 }
2858 }
2859 if (!found)
2860 list_add_tail(&saveq->clist,
2861 &pring->iocb_continue_saveq);
2862 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2863 list_del_init(&iocbq->clist);
2864 saveq = iocbq;
2865 irsp = &(saveq->iocb);
2866 } else
2867 return 0;
2868 }
2869 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2870 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2871 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002872 Rctl = FC_RCTL_ELS_REQ;
2873 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002874 } else {
2875 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2876 Rctl = w5p->hcsw.Rctl;
2877 Type = w5p->hcsw.Type;
2878
2879 /* Firmware Workaround */
2880 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2881 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2882 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002883 Rctl = FC_RCTL_ELS_REQ;
2884 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002885 w5p->hcsw.Rctl = Rctl;
2886 w5p->hcsw.Type = Type;
2887 }
2888 }
James Smart92d7f7b2007-06-17 19:56:38 -05002889
James Smart3772a992009-05-22 14:50:54 -04002890 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002891 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002892 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002893 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002894 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002895
James Smart92d7f7b2007-06-17 19:56:38 -05002896 return 1;
dea31012005-04-17 16:05:31 -05002897}
2898
James Smarte59058c2008-08-24 21:49:00 -04002899/**
James Smart3621a712009-04-06 18:47:14 -04002900 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002901 * @phba: Pointer to HBA context object.
2902 * @pring: Pointer to driver SLI ring object.
2903 * @prspiocb: Pointer to response iocb object.
2904 *
2905 * This function looks up the iocb_lookup table to get the command iocb
2906 * corresponding to the given response iocb using the iotag of the
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002907 * response iocb. This function is called with the hbalock held
2908 * for sli3 devices or the ring_lock for sli4 devices.
James Smarte59058c2008-08-24 21:49:00 -04002909 * This function returns the command iocb object if it finds the command
2910 * iocb else returns NULL.
2911 **/
dea31012005-04-17 16:05:31 -05002912static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002913lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2914 struct lpfc_sli_ring *pring,
2915 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002916{
dea31012005-04-17 16:05:31 -05002917 struct lpfc_iocbq *cmd_iocb = NULL;
2918 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002919 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002920
James Bottomley604a3e32005-10-29 10:28:33 -05002921 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002922
James Bottomley604a3e32005-10-29 10:28:33 -05002923 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2924 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002925 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002926 /* remove from txcmpl queue list */
2927 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002928 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart89533e92016-10-13 15:06:15 -07002929 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002930 }
dea31012005-04-17 16:05:31 -05002931 }
2932
dea31012005-04-17 16:05:31 -05002933 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002934 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05002935 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002936 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002937 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002938 return NULL;
2939}
2940
James Smarte59058c2008-08-24 21:49:00 -04002941/**
James Smart3772a992009-05-22 14:50:54 -04002942 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2943 * @phba: Pointer to HBA context object.
2944 * @pring: Pointer to driver SLI ring object.
2945 * @iotag: IOCB tag.
2946 *
2947 * This function looks up the iocb_lookup table to get the command iocb
2948 * corresponding to the given iotag. This function is called with the
2949 * hbalock held.
2950 * This function returns the command iocb object if it finds the command
2951 * iocb else returns NULL.
2952 **/
2953static struct lpfc_iocbq *
2954lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2955 struct lpfc_sli_ring *pring, uint16_t iotag)
2956{
James Smart895427b2017-02-12 13:52:30 -08002957 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04002958
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002959 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002960 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2961 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002962 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2963 /* remove from txcmpl queue list */
2964 list_del_init(&cmd_iocb->list);
2965 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002966 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002967 }
James Smart3772a992009-05-22 14:50:54 -04002968 }
James Smart89533e92016-10-13 15:06:15 -07002969
James Smart3772a992009-05-22 14:50:54 -04002970 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08002971 "0372 iotag x%x lookup error: max iotag (x%x) "
2972 "iocb_flag x%x\n",
2973 iotag, phba->sli.last_iotag,
2974 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04002975 return NULL;
2976}
2977
2978/**
James Smart3621a712009-04-06 18:47:14 -04002979 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002980 * @phba: Pointer to HBA context object.
2981 * @pring: Pointer to driver SLI ring object.
2982 * @saveq: Pointer to the response iocb to be processed.
2983 *
2984 * This function is called by the ring event handler for non-fcp
2985 * rings when there is a new response iocb in the response ring.
2986 * The caller is not required to hold any locks. This function
2987 * gets the command iocb associated with the response iocb and
2988 * calls the completion handler for the command iocb. If there
2989 * is no completion handler, the function will free the resources
2990 * associated with command iocb. If the response iocb is for
2991 * an already aborted command iocb, the status of the completion
2992 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2993 * This function always returns 1.
2994 **/
dea31012005-04-17 16:05:31 -05002995static int
James Smart2e0fef82007-06-17 19:56:36 -05002996lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002997 struct lpfc_iocbq *saveq)
2998{
James Smart2e0fef82007-06-17 19:56:36 -05002999 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05003000 int rc = 1;
3001 unsigned long iflag;
3002
3003 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003004 if (phba->sli_rev == LPFC_SLI_REV4)
3005 spin_lock_irqsave(&pring->ring_lock, iflag);
3006 else
3007 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003008 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003009 if (phba->sli_rev == LPFC_SLI_REV4)
3010 spin_unlock_irqrestore(&pring->ring_lock, iflag);
3011 else
3012 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart2e0fef82007-06-17 19:56:36 -05003013
dea31012005-04-17 16:05:31 -05003014 if (cmdiocbp) {
3015 if (cmdiocbp->iocb_cmpl) {
3016 /*
James Smartea2151b2008-09-07 11:52:10 -04003017 * If an ELS command failed send an event to mgmt
3018 * application.
3019 */
3020 if (saveq->iocb.ulpStatus &&
3021 (pring->ringno == LPFC_ELS_RING) &&
3022 (cmdiocbp->iocb.ulpCommand ==
3023 CMD_ELS_REQUEST64_CR))
3024 lpfc_send_els_failure_event(phba,
3025 cmdiocbp, saveq);
3026
3027 /*
dea31012005-04-17 16:05:31 -05003028 * Post all ELS completions to the worker thread.
3029 * All other are passed to the completion callback.
3030 */
3031 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05003032 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3033 (cmdiocbp->iocb_flag &
3034 LPFC_DRIVER_ABORTED)) {
3035 spin_lock_irqsave(&phba->hbalock,
3036 iflag);
James Smart07951072007-04-25 09:51:38 -04003037 cmdiocbp->iocb_flag &=
3038 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05003039 spin_unlock_irqrestore(&phba->hbalock,
3040 iflag);
James Smart07951072007-04-25 09:51:38 -04003041 saveq->iocb.ulpStatus =
3042 IOSTAT_LOCAL_REJECT;
3043 saveq->iocb.un.ulpWord[4] =
3044 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05003045
3046 /* Firmware could still be in progress
3047 * of DMAing payload, so don't free data
3048 * buffer till after a hbeat.
3049 */
James Smart341af102010-01-26 23:07:37 -05003050 spin_lock_irqsave(&phba->hbalock,
3051 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05003052 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05003053 spin_unlock_irqrestore(&phba->hbalock,
3054 iflag);
3055 }
James Smart0f65ff62010-02-26 14:14:23 -05003056 if (phba->sli_rev == LPFC_SLI_REV4) {
3057 if (saveq->iocb_flag &
3058 LPFC_EXCHANGE_BUSY) {
3059 /* Set cmdiocb flag for the
3060 * exchange busy so sgl (xri)
3061 * will not be released until
3062 * the abort xri is received
3063 * from hba.
3064 */
3065 spin_lock_irqsave(
3066 &phba->hbalock, iflag);
3067 cmdiocbp->iocb_flag |=
3068 LPFC_EXCHANGE_BUSY;
3069 spin_unlock_irqrestore(
3070 &phba->hbalock, iflag);
3071 }
3072 if (cmdiocbp->iocb_flag &
3073 LPFC_DRIVER_ABORTED) {
3074 /*
3075 * Clear LPFC_DRIVER_ABORTED
3076 * bit in case it was driver
3077 * initiated abort.
3078 */
3079 spin_lock_irqsave(
3080 &phba->hbalock, iflag);
3081 cmdiocbp->iocb_flag &=
3082 ~LPFC_DRIVER_ABORTED;
3083 spin_unlock_irqrestore(
3084 &phba->hbalock, iflag);
3085 cmdiocbp->iocb.ulpStatus =
3086 IOSTAT_LOCAL_REJECT;
3087 cmdiocbp->iocb.un.ulpWord[4] =
3088 IOERR_ABORT_REQUESTED;
3089 /*
3090 * For SLI4, irsiocb contains
3091 * NO_XRI in sli_xritag, it
3092 * shall not affect releasing
3093 * sgl (xri) process.
3094 */
3095 saveq->iocb.ulpStatus =
3096 IOSTAT_LOCAL_REJECT;
3097 saveq->iocb.un.ulpWord[4] =
3098 IOERR_SLI_ABORTED;
3099 spin_lock_irqsave(
3100 &phba->hbalock, iflag);
3101 saveq->iocb_flag |=
3102 LPFC_DELAY_MEM_FREE;
3103 spin_unlock_irqrestore(
3104 &phba->hbalock, iflag);
3105 }
James Smart07951072007-04-25 09:51:38 -04003106 }
dea31012005-04-17 16:05:31 -05003107 }
James Smart2e0fef82007-06-17 19:56:36 -05003108 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003109 } else
3110 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003111 } else {
3112 /*
3113 * Unknown initiating command based on the response iotag.
3114 * This could be the case on the ELS ring because of
3115 * lpfc_els_abort().
3116 */
3117 if (pring->ringno != LPFC_ELS_RING) {
3118 /*
3119 * Ring <ringno> handler: unexpected completion IoTag
3120 * <IoTag>
3121 */
James Smarta257bf92009-04-06 18:48:10 -04003122 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003123 "0322 Ring %d handler: "
3124 "unexpected completion IoTag x%x "
3125 "Data: x%x x%x x%x x%x\n",
3126 pring->ringno,
3127 saveq->iocb.ulpIoTag,
3128 saveq->iocb.ulpStatus,
3129 saveq->iocb.un.ulpWord[4],
3130 saveq->iocb.ulpCommand,
3131 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003132 }
3133 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003134
dea31012005-04-17 16:05:31 -05003135 return rc;
3136}
3137
James Smarte59058c2008-08-24 21:49:00 -04003138/**
James Smart3621a712009-04-06 18:47:14 -04003139 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003140 * @phba: Pointer to HBA context object.
3141 * @pring: Pointer to driver SLI ring object.
3142 *
3143 * This function is called from the iocb ring event handlers when
3144 * put pointer is ahead of the get pointer for a ring. This function signal
3145 * an error attention condition to the worker thread and the worker
3146 * thread will transition the HBA to offline state.
3147 **/
James Smart2e0fef82007-06-17 19:56:36 -05003148static void
3149lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003150{
James Smart34b02dc2008-08-24 21:49:55 -04003151 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003152 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003153 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003154 * rsp ring <portRspMax>
3155 */
3156 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003157 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003158 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003159 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003160 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003161
James Smart2e0fef82007-06-17 19:56:36 -05003162 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003163
3164 /*
3165 * All error attention handlers are posted to
3166 * worker thread
3167 */
3168 phba->work_ha |= HA_ERATT;
3169 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003170
James Smart5e9d9b82008-06-14 22:52:53 -04003171 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003172
3173 return;
3174}
3175
James Smarte59058c2008-08-24 21:49:00 -04003176/**
James Smart3621a712009-04-06 18:47:14 -04003177 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04003178 * @ptr: Pointer to address of HBA context object.
3179 *
3180 * This function is invoked by the Error Attention polling timer when the
3181 * timer times out. It will check the SLI Error Attention register for
3182 * possible attention events. If so, it will post an Error Attention event
3183 * and wake up worker thread to process it. Otherwise, it will set up the
3184 * Error Attention polling timer for the next poll.
3185 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003186void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003187{
3188 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003189 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003190 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003191
Kees Cookf22eb4d2017-09-06 20:24:26 -07003192 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003193
James Smartaa6fbb72012-08-03 12:36:03 -04003194 /* Here we will also keep track of interrupts per sec of the hba */
3195 sli_intr = phba->sli.slistat.sli_intr;
3196
3197 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3198 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3199 sli_intr);
3200 else
3201 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3202
James Smart65791f12016-07-06 12:35:56 -07003203 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3204 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003205 phba->sli.slistat.sli_ips = cnt;
3206
3207 phba->sli.slistat.sli_prev_intr = sli_intr;
3208
James Smart93996272008-08-24 21:50:30 -04003209 /* Check chip HA register for error event */
3210 eratt = lpfc_sli_check_eratt(phba);
3211
3212 if (eratt)
3213 /* Tell the worker thread there is work to do */
3214 lpfc_worker_wake_up(phba);
3215 else
3216 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003217 mod_timer(&phba->eratt_poll,
3218 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003219 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003220 return;
3221}
3222
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003223
James Smarte59058c2008-08-24 21:49:00 -04003224/**
James Smart3621a712009-04-06 18:47:14 -04003225 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003226 * @phba: Pointer to HBA context object.
3227 * @pring: Pointer to driver SLI ring object.
3228 * @mask: Host attention register mask for this ring.
3229 *
3230 * This function is called from the interrupt context when there is a ring
3231 * event for the fcp ring. The caller does not hold any lock.
3232 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003233 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003234 * LE bit set. The function will call the completion handler of the command iocb
3235 * if the response iocb indicates a completion for a command iocb or it is
3236 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3237 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003238 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003239 * to check it explicitly.
3240 */
3241int
James Smart2e0fef82007-06-17 19:56:36 -05003242lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3243 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003244{
James Smart34b02dc2008-08-24 21:49:55 -04003245 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003246 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003247 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003248 struct lpfc_iocbq *cmdiocbq = NULL;
3249 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003250 uint32_t status;
3251 uint32_t portRspPut, portRspMax;
3252 int rc = 1;
3253 lpfc_iocb_type type;
3254 unsigned long iflag;
3255 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003256
James Smart2e0fef82007-06-17 19:56:36 -05003257 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003258 pring->stats.iocb_event++;
3259
dea31012005-04-17 16:05:31 -05003260 /*
3261 * The next available response entry should never exceed the maximum
3262 * entries. If it does, treat it as an adapter hardware error.
3263 */
James Smart7e56aa22012-08-03 12:35:34 -04003264 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003265 portRspPut = le32_to_cpu(pgp->rspPutInx);
3266 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003267 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003268 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003269 return 1;
3270 }
James Smart45ed1192009-10-02 15:17:02 -04003271 if (phba->fcp_ring_in_use) {
3272 spin_unlock_irqrestore(&phba->hbalock, iflag);
3273 return 1;
3274 } else
3275 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003276
3277 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003278 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003279 /*
3280 * Fetch an entry off the ring and copy it into a local data
3281 * structure. The copy involves a byte-swap since the
3282 * network byte order and pci byte orders are different.
3283 */
James Smarted957682007-06-17 19:56:37 -05003284 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003285 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003286
James Smart7e56aa22012-08-03 12:35:34 -04003287 if (++pring->sli.sli3.rspidx >= portRspMax)
3288 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003289
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003290 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3291 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003292 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003293 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003294 irsp = &rspiocbq.iocb;
3295
dea31012005-04-17 16:05:31 -05003296 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3297 pring->stats.iocb_rsp++;
3298 rsp_cmpl++;
3299
3300 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003301 /*
3302 * If resource errors reported from HBA, reduce
3303 * queuedepths of the SCSI device.
3304 */
3305 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003306 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3307 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003308 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003309 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003310 spin_lock_irqsave(&phba->hbalock, iflag);
3311 }
3312
dea31012005-04-17 16:05:31 -05003313 /* Rsp ring <ringno> error: IOCB */
3314 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003315 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003316 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003317 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003318 irsp->un.ulpWord[0],
3319 irsp->un.ulpWord[1],
3320 irsp->un.ulpWord[2],
3321 irsp->un.ulpWord[3],
3322 irsp->un.ulpWord[4],
3323 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003324 *(uint32_t *)&irsp->un1,
3325 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003326 }
3327
3328 switch (type) {
3329 case LPFC_ABORT_IOCB:
3330 case LPFC_SOL_IOCB:
3331 /*
3332 * Idle exchange closed via ABTS from port. No iocb
3333 * resources need to be recovered.
3334 */
3335 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003336 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003337 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003338 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003339 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003340 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003341 break;
3342 }
3343
James Bottomley604a3e32005-10-29 10:28:33 -05003344 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3345 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003346 if (unlikely(!cmdiocbq))
3347 break;
3348 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3349 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3350 if (cmdiocbq->iocb_cmpl) {
3351 spin_unlock_irqrestore(&phba->hbalock, iflag);
3352 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3353 &rspiocbq);
3354 spin_lock_irqsave(&phba->hbalock, iflag);
3355 }
dea31012005-04-17 16:05:31 -05003356 break;
James Smarta4bc3372006-12-02 13:34:16 -05003357 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003358 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003359 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003360 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003361 break;
dea31012005-04-17 16:05:31 -05003362 default:
3363 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3364 char adaptermsg[LPFC_MAX_ADPTMSG];
3365 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3366 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3367 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003368 dev_warn(&((phba->pcidev)->dev),
3369 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003370 phba->brd_no, adaptermsg);
3371 } else {
3372 /* Unknown IOCB command */
3373 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003374 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003375 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003376 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003377 irsp->ulpStatus,
3378 irsp->ulpIoTag,
3379 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003380 }
3381 break;
3382 }
3383
3384 /*
3385 * The response IOCB has been processed. Update the ring
3386 * pointer in SLIM. If the port response put pointer has not
3387 * been updated, sync the pgp->rspPutInx and fetch the new port
3388 * response put pointer.
3389 */
James Smart7e56aa22012-08-03 12:35:34 -04003390 writel(pring->sli.sli3.rspidx,
3391 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003392
James Smart7e56aa22012-08-03 12:35:34 -04003393 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003394 portRspPut = le32_to_cpu(pgp->rspPutInx);
3395 }
3396
3397 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3398 pring->stats.iocb_rsp_full++;
3399 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3400 writel(status, phba->CAregaddr);
3401 readl(phba->CAregaddr);
3402 }
3403 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3404 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3405 pring->stats.iocb_cmd_empty++;
3406
3407 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003408 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003409 lpfc_sli_resume_iocb(phba, pring);
3410
3411 if ((pring->lpfc_sli_cmd_available))
3412 (pring->lpfc_sli_cmd_available) (phba, pring);
3413
3414 }
3415
James Smart45ed1192009-10-02 15:17:02 -04003416 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003417 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003418 return rc;
3419}
3420
James Smarte59058c2008-08-24 21:49:00 -04003421/**
James Smart3772a992009-05-22 14:50:54 -04003422 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3423 * @phba: Pointer to HBA context object.
3424 * @pring: Pointer to driver SLI ring object.
3425 * @rspiocbp: Pointer to driver response IOCB object.
3426 *
3427 * This function is called from the worker thread when there is a slow-path
3428 * response IOCB to process. This function chains all the response iocbs until
3429 * seeing the iocb with the LE bit set. The function will call
3430 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3431 * completion of a command iocb. The function will call the
3432 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3433 * The function frees the resources or calls the completion handler if this
3434 * iocb is an abort completion. The function returns NULL when the response
3435 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3436 * this function shall chain the iocb on to the iocb_continueq and return the
3437 * response iocb passed in.
3438 **/
3439static struct lpfc_iocbq *
3440lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3441 struct lpfc_iocbq *rspiocbp)
3442{
3443 struct lpfc_iocbq *saveq;
3444 struct lpfc_iocbq *cmdiocbp;
3445 struct lpfc_iocbq *next_iocb;
3446 IOCB_t *irsp = NULL;
3447 uint32_t free_saveq;
3448 uint8_t iocb_cmd_type;
3449 lpfc_iocb_type type;
3450 unsigned long iflag;
3451 int rc;
3452
3453 spin_lock_irqsave(&phba->hbalock, iflag);
3454 /* First add the response iocb to the countinueq list */
3455 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3456 pring->iocb_continueq_cnt++;
3457
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003458 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003459 irsp = &rspiocbp->iocb;
3460 if (irsp->ulpLe) {
3461 /*
3462 * By default, the driver expects to free all resources
3463 * associated with this iocb completion.
3464 */
3465 free_saveq = 1;
3466 saveq = list_get_first(&pring->iocb_continueq,
3467 struct lpfc_iocbq, list);
3468 irsp = &(saveq->iocb);
3469 list_del_init(&pring->iocb_continueq);
3470 pring->iocb_continueq_cnt = 0;
3471
3472 pring->stats.iocb_rsp++;
3473
3474 /*
3475 * If resource errors reported from HBA, reduce
3476 * queuedepths of the SCSI device.
3477 */
3478 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003479 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3480 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003481 spin_unlock_irqrestore(&phba->hbalock, iflag);
3482 phba->lpfc_rampdown_queue_depth(phba);
3483 spin_lock_irqsave(&phba->hbalock, iflag);
3484 }
3485
3486 if (irsp->ulpStatus) {
3487 /* Rsp ring <ringno> error: IOCB */
3488 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3489 "0328 Rsp Ring %d error: "
3490 "IOCB Data: "
3491 "x%x x%x x%x x%x "
3492 "x%x x%x x%x x%x "
3493 "x%x x%x x%x x%x "
3494 "x%x x%x x%x x%x\n",
3495 pring->ringno,
3496 irsp->un.ulpWord[0],
3497 irsp->un.ulpWord[1],
3498 irsp->un.ulpWord[2],
3499 irsp->un.ulpWord[3],
3500 irsp->un.ulpWord[4],
3501 irsp->un.ulpWord[5],
3502 *(((uint32_t *) irsp) + 6),
3503 *(((uint32_t *) irsp) + 7),
3504 *(((uint32_t *) irsp) + 8),
3505 *(((uint32_t *) irsp) + 9),
3506 *(((uint32_t *) irsp) + 10),
3507 *(((uint32_t *) irsp) + 11),
3508 *(((uint32_t *) irsp) + 12),
3509 *(((uint32_t *) irsp) + 13),
3510 *(((uint32_t *) irsp) + 14),
3511 *(((uint32_t *) irsp) + 15));
3512 }
3513
3514 /*
3515 * Fetch the IOCB command type and call the correct completion
3516 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3517 * get freed back to the lpfc_iocb_list by the discovery
3518 * kernel thread.
3519 */
3520 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3521 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3522 switch (type) {
3523 case LPFC_SOL_IOCB:
3524 spin_unlock_irqrestore(&phba->hbalock, iflag);
3525 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3526 spin_lock_irqsave(&phba->hbalock, iflag);
3527 break;
3528
3529 case LPFC_UNSOL_IOCB:
3530 spin_unlock_irqrestore(&phba->hbalock, iflag);
3531 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3532 spin_lock_irqsave(&phba->hbalock, iflag);
3533 if (!rc)
3534 free_saveq = 0;
3535 break;
3536
3537 case LPFC_ABORT_IOCB:
3538 cmdiocbp = NULL;
3539 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3540 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3541 saveq);
3542 if (cmdiocbp) {
3543 /* Call the specified completion routine */
3544 if (cmdiocbp->iocb_cmpl) {
3545 spin_unlock_irqrestore(&phba->hbalock,
3546 iflag);
3547 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3548 saveq);
3549 spin_lock_irqsave(&phba->hbalock,
3550 iflag);
3551 } else
3552 __lpfc_sli_release_iocbq(phba,
3553 cmdiocbp);
3554 }
3555 break;
3556
3557 case LPFC_UNKNOWN_IOCB:
3558 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3559 char adaptermsg[LPFC_MAX_ADPTMSG];
3560 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3561 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3562 MAX_MSG_DATA);
3563 dev_warn(&((phba->pcidev)->dev),
3564 "lpfc%d: %s\n",
3565 phba->brd_no, adaptermsg);
3566 } else {
3567 /* Unknown IOCB command */
3568 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3569 "0335 Unknown IOCB "
3570 "command Data: x%x "
3571 "x%x x%x x%x\n",
3572 irsp->ulpCommand,
3573 irsp->ulpStatus,
3574 irsp->ulpIoTag,
3575 irsp->ulpContext);
3576 }
3577 break;
3578 }
3579
3580 if (free_saveq) {
3581 list_for_each_entry_safe(rspiocbp, next_iocb,
3582 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003583 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003584 __lpfc_sli_release_iocbq(phba, rspiocbp);
3585 }
3586 __lpfc_sli_release_iocbq(phba, saveq);
3587 }
3588 rspiocbp = NULL;
3589 }
3590 spin_unlock_irqrestore(&phba->hbalock, iflag);
3591 return rspiocbp;
3592}
3593
3594/**
3595 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003596 * @phba: Pointer to HBA context object.
3597 * @pring: Pointer to driver SLI ring object.
3598 * @mask: Host attention register mask for this ring.
3599 *
James Smart3772a992009-05-22 14:50:54 -04003600 * This routine wraps the actual slow_ring event process routine from the
3601 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003602 **/
James Smart3772a992009-05-22 14:50:54 -04003603void
James Smart2e0fef82007-06-17 19:56:36 -05003604lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3605 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003606{
James Smart3772a992009-05-22 14:50:54 -04003607 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3608}
3609
3610/**
3611 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3612 * @phba: Pointer to HBA context object.
3613 * @pring: Pointer to driver SLI ring object.
3614 * @mask: Host attention register mask for this ring.
3615 *
3616 * This function is called from the worker thread when there is a ring event
3617 * for non-fcp rings. The caller does not hold any lock. The function will
3618 * remove each response iocb in the response ring and calls the handle
3619 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3620 **/
3621static void
3622lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3623 struct lpfc_sli_ring *pring, uint32_t mask)
3624{
James Smart34b02dc2008-08-24 21:49:55 -04003625 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003626 IOCB_t *entry;
3627 IOCB_t *irsp = NULL;
3628 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003629 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003630 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003631 uint32_t status;
dea31012005-04-17 16:05:31 -05003632
James Smart34b02dc2008-08-24 21:49:55 -04003633 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003634 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003635 pring->stats.iocb_event++;
3636
dea31012005-04-17 16:05:31 -05003637 /*
3638 * The next available response entry should never exceed the maximum
3639 * entries. If it does, treat it as an adapter hardware error.
3640 */
James Smart7e56aa22012-08-03 12:35:34 -04003641 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003642 portRspPut = le32_to_cpu(pgp->rspPutInx);
3643 if (portRspPut >= portRspMax) {
3644 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003645 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003646 * rsp ring <portRspMax>
3647 */
James Smarted957682007-06-17 19:56:37 -05003648 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003649 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003650 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003651 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003652
James Smart2e0fef82007-06-17 19:56:36 -05003653 phba->link_state = LPFC_HBA_ERROR;
3654 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003655
3656 phba->work_hs = HS_FFER3;
3657 lpfc_handle_eratt(phba);
3658
James Smart3772a992009-05-22 14:50:54 -04003659 return;
dea31012005-04-17 16:05:31 -05003660 }
3661
3662 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003663 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003664 /*
3665 * Build a completion list and call the appropriate handler.
3666 * The process is to get the next available response iocb, get
3667 * a free iocb from the list, copy the response data into the
3668 * free iocb, insert to the continuation list, and update the
3669 * next response index to slim. This process makes response
3670 * iocb's in the ring available to DMA as fast as possible but
3671 * pays a penalty for a copy operation. Since the iocb is
3672 * only 32 bytes, this penalty is considered small relative to
3673 * the PCI reads for register values and a slim write. When
3674 * the ulpLe field is set, the entire Command has been
3675 * received.
3676 */
James Smarted957682007-06-17 19:56:37 -05003677 entry = lpfc_resp_iocb(phba, pring);
3678
James Smart858c9f62007-06-17 19:56:39 -05003679 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003680 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003681 if (rspiocbp == NULL) {
3682 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003683 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003684 break;
3685 }
3686
James Smarted957682007-06-17 19:56:37 -05003687 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3688 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003689 irsp = &rspiocbp->iocb;
3690
James Smart7e56aa22012-08-03 12:35:34 -04003691 if (++pring->sli.sli3.rspidx >= portRspMax)
3692 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003693
James Smarta58cbd52007-08-02 11:09:43 -04003694 if (pring->ringno == LPFC_ELS_RING) {
3695 lpfc_debugfs_slow_ring_trc(phba,
3696 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3697 *(((uint32_t *) irsp) + 4),
3698 *(((uint32_t *) irsp) + 6),
3699 *(((uint32_t *) irsp) + 7));
3700 }
3701
James Smart7e56aa22012-08-03 12:35:34 -04003702 writel(pring->sli.sli3.rspidx,
3703 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003704
James Smart3772a992009-05-22 14:50:54 -04003705 spin_unlock_irqrestore(&phba->hbalock, iflag);
3706 /* Handle the response IOCB */
3707 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3708 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003709
3710 /*
3711 * If the port response put pointer has not been updated, sync
3712 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3713 * response put pointer.
3714 */
James Smart7e56aa22012-08-03 12:35:34 -04003715 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003716 portRspPut = le32_to_cpu(pgp->rspPutInx);
3717 }
James Smart7e56aa22012-08-03 12:35:34 -04003718 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003719
James Smart92d7f7b2007-06-17 19:56:38 -05003720 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003721 /* At least one response entry has been freed */
3722 pring->stats.iocb_rsp_full++;
3723 /* SET RxRE_RSP in Chip Att register */
3724 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3725 writel(status, phba->CAregaddr);
3726 readl(phba->CAregaddr); /* flush */
3727 }
3728 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3729 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3730 pring->stats.iocb_cmd_empty++;
3731
3732 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003733 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003734 lpfc_sli_resume_iocb(phba, pring);
3735
3736 if ((pring->lpfc_sli_cmd_available))
3737 (pring->lpfc_sli_cmd_available) (phba, pring);
3738
3739 }
3740
James Smart2e0fef82007-06-17 19:56:36 -05003741 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003742 return;
dea31012005-04-17 16:05:31 -05003743}
3744
James Smarte59058c2008-08-24 21:49:00 -04003745/**
James Smart4f774512009-05-22 14:52:35 -04003746 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3747 * @phba: Pointer to HBA context object.
3748 * @pring: Pointer to driver SLI ring object.
3749 * @mask: Host attention register mask for this ring.
3750 *
3751 * This function is called from the worker thread when there is a pending
3752 * ELS response iocb on the driver internal slow-path response iocb worker
3753 * queue. The caller does not hold any lock. The function will remove each
3754 * response iocb from the response worker queue and calls the handle
3755 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3756 **/
3757static void
3758lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3759 struct lpfc_sli_ring *pring, uint32_t mask)
3760{
3761 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003762 struct hbq_dmabuf *dmabuf;
3763 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003764 unsigned long iflag;
3765
James Smart45ed1192009-10-02 15:17:02 -04003766 spin_lock_irqsave(&phba->hbalock, iflag);
3767 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3768 spin_unlock_irqrestore(&phba->hbalock, iflag);
3769 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003770 /* Get the response iocb from the head of work queue */
3771 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003772 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003773 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003774 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003775
3776 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3777 case CQE_CODE_COMPL_WQE:
3778 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3779 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003780 /* Translate ELS WCQE to response IOCBQ */
3781 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3782 irspiocbq);
3783 if (irspiocbq)
3784 lpfc_sli_sp_handle_rspiocb(phba, pring,
3785 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003786 break;
3787 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003788 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003789 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3790 cq_event);
3791 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3792 break;
3793 default:
3794 break;
3795 }
James Smart4f774512009-05-22 14:52:35 -04003796 }
3797}
3798
3799/**
James Smart3621a712009-04-06 18:47:14 -04003800 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003801 * @phba: Pointer to HBA context object.
3802 * @pring: Pointer to driver SLI ring object.
3803 *
3804 * This function aborts all iocbs in the given ring and frees all the iocb
3805 * objects in txq. This function issues an abort iocb for all the iocb commands
3806 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3807 * the return of this function. The caller is not required to hold any locks.
3808 **/
James Smart2e0fef82007-06-17 19:56:36 -05003809void
dea31012005-04-17 16:05:31 -05003810lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3811{
James Smart2534ba72007-04-25 09:52:20 -04003812 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003813 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003814
James Smart92d7f7b2007-06-17 19:56:38 -05003815 if (pring->ringno == LPFC_ELS_RING) {
3816 lpfc_fabric_abort_hba(phba);
3817 }
3818
dea31012005-04-17 16:05:31 -05003819 /* Error everything on txq and txcmplq
3820 * First do the txq.
3821 */
James Smartdb55fba2014-04-04 13:52:02 -04003822 if (phba->sli_rev >= LPFC_SLI_REV4) {
3823 spin_lock_irq(&pring->ring_lock);
3824 list_splice_init(&pring->txq, &completions);
3825 pring->txq_cnt = 0;
3826 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003827
James Smartdb55fba2014-04-04 13:52:02 -04003828 spin_lock_irq(&phba->hbalock);
3829 /* Next issue ABTS for everything on the txcmplq */
3830 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3831 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3832 spin_unlock_irq(&phba->hbalock);
3833 } else {
3834 spin_lock_irq(&phba->hbalock);
3835 list_splice_init(&pring->txq, &completions);
3836 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003837
James Smartdb55fba2014-04-04 13:52:02 -04003838 /* Next issue ABTS for everything on the txcmplq */
3839 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3840 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3841 spin_unlock_irq(&phba->hbalock);
3842 }
James Smart2534ba72007-04-25 09:52:20 -04003843
James Smarta257bf92009-04-06 18:48:10 -04003844 /* Cancel all the IOCBs from the completions list */
3845 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3846 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003847}
3848
James Smarte59058c2008-08-24 21:49:00 -04003849/**
James Smart895427b2017-02-12 13:52:30 -08003850 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3851 * @phba: Pointer to HBA context object.
3852 * @pring: Pointer to driver SLI ring object.
3853 *
3854 * This function aborts all iocbs in the given ring and frees all the iocb
3855 * objects in txq. This function issues an abort iocb for all the iocb commands
3856 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3857 * the return of this function. The caller is not required to hold any locks.
3858 **/
3859void
3860lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3861{
3862 LIST_HEAD(completions);
3863 struct lpfc_iocbq *iocb, *next_iocb;
3864
3865 if (pring->ringno == LPFC_ELS_RING)
3866 lpfc_fabric_abort_hba(phba);
3867
3868 spin_lock_irq(&phba->hbalock);
3869 /* Next issue ABTS for everything on the txcmplq */
3870 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3871 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3872 spin_unlock_irq(&phba->hbalock);
3873}
3874
3875
3876/**
James Smartdb55fba2014-04-04 13:52:02 -04003877 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3878 * @phba: Pointer to HBA context object.
3879 * @pring: Pointer to driver SLI ring object.
3880 *
3881 * This function aborts all iocbs in FCP rings and frees all the iocb
3882 * objects in txq. This function issues an abort iocb for all the iocb commands
3883 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3884 * the return of this function. The caller is not required to hold any locks.
3885 **/
3886void
3887lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3888{
3889 struct lpfc_sli *psli = &phba->sli;
3890 struct lpfc_sli_ring *pring;
3891 uint32_t i;
3892
3893 /* Look on all the FCP Rings for the iotag */
3894 if (phba->sli_rev >= LPFC_SLI_REV4) {
3895 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003896 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003897 lpfc_sli_abort_iocb_ring(phba, pring);
3898 }
3899 } else {
James Smart895427b2017-02-12 13:52:30 -08003900 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003901 lpfc_sli_abort_iocb_ring(phba, pring);
3902 }
3903}
3904
James Smart895427b2017-02-12 13:52:30 -08003905/**
3906 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3907 * @phba: Pointer to HBA context object.
3908 *
3909 * This function aborts all wqes in NVME rings. This function issues an
3910 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3911 * the txcmplq is not guaranteed to complete before the return of this
3912 * function. The caller is not required to hold any locks.
3913 **/
3914void
3915lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3916{
3917 struct lpfc_sli_ring *pring;
3918 uint32_t i;
3919
3920 if (phba->sli_rev < LPFC_SLI_REV4)
3921 return;
3922
3923 /* Abort all IO on each NVME ring. */
3924 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3925 pring = phba->sli4_hba.nvme_wq[i]->pring;
3926 lpfc_sli_abort_wqe_ring(phba, pring);
3927 }
3928}
3929
James Smartdb55fba2014-04-04 13:52:02 -04003930
3931/**
James Smart3621a712009-04-06 18:47:14 -04003932 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003933 * @phba: Pointer to HBA context object.
3934 *
3935 * This function flushes all iocbs in the fcp ring and frees all the iocb
3936 * objects in txq and txcmplq. This function will not issue abort iocbs
3937 * for all the iocb commands in txcmplq, they will just be returned with
3938 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3939 * slot has been permanently disabled.
3940 **/
3941void
3942lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3943{
3944 LIST_HEAD(txq);
3945 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003946 struct lpfc_sli *psli = &phba->sli;
3947 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003948 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08003949 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04003950
3951 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003952 /* Indicate the I/O queues are flushed */
3953 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003954 spin_unlock_irq(&phba->hbalock);
3955
James Smartdb55fba2014-04-04 13:52:02 -04003956 /* Look on all the FCP Rings for the iotag */
3957 if (phba->sli_rev >= LPFC_SLI_REV4) {
3958 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003959 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smarta8e497d2008-08-24 21:50:11 -04003960
James Smartdb55fba2014-04-04 13:52:02 -04003961 spin_lock_irq(&pring->ring_lock);
3962 /* Retrieve everything on txq */
3963 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08003964 list_for_each_entry_safe(piocb, next_iocb,
3965 &pring->txcmplq, list)
3966 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04003967 /* Retrieve everything on the txcmplq */
3968 list_splice_init(&pring->txcmplq, &txcmplq);
3969 pring->txq_cnt = 0;
3970 pring->txcmplq_cnt = 0;
3971 spin_unlock_irq(&pring->ring_lock);
3972
3973 /* Flush the txq */
3974 lpfc_sli_cancel_iocbs(phba, &txq,
3975 IOSTAT_LOCAL_REJECT,
3976 IOERR_SLI_DOWN);
3977 /* Flush the txcmpq */
3978 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3979 IOSTAT_LOCAL_REJECT,
3980 IOERR_SLI_DOWN);
3981 }
3982 } else {
James Smart895427b2017-02-12 13:52:30 -08003983 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003984
3985 spin_lock_irq(&phba->hbalock);
3986 /* Retrieve everything on txq */
3987 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08003988 list_for_each_entry_safe(piocb, next_iocb,
3989 &pring->txcmplq, list)
3990 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04003991 /* Retrieve everything on the txcmplq */
3992 list_splice_init(&pring->txcmplq, &txcmplq);
3993 pring->txq_cnt = 0;
3994 pring->txcmplq_cnt = 0;
3995 spin_unlock_irq(&phba->hbalock);
3996
3997 /* Flush the txq */
3998 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3999 IOERR_SLI_DOWN);
4000 /* Flush the txcmpq */
4001 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4002 IOERR_SLI_DOWN);
4003 }
James Smarta8e497d2008-08-24 21:50:11 -04004004}
4005
4006/**
James Smart895427b2017-02-12 13:52:30 -08004007 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
4008 * @phba: Pointer to HBA context object.
4009 *
4010 * This function flushes all wqes in the nvme rings and frees all resources
4011 * in the txcmplq. This function does not issue abort wqes for the IO
4012 * commands in txcmplq, they will just be returned with
4013 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4014 * slot has been permanently disabled.
4015 **/
4016void
4017lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
4018{
4019 LIST_HEAD(txcmplq);
4020 struct lpfc_sli_ring *pring;
4021 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004022 struct lpfc_iocbq *piocb, *next_iocb;
James Smart895427b2017-02-12 13:52:30 -08004023
4024 if (phba->sli_rev < LPFC_SLI_REV4)
4025 return;
4026
4027 /* Hint to other driver operations that a flush is in progress. */
4028 spin_lock_irq(&phba->hbalock);
4029 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
4030 spin_unlock_irq(&phba->hbalock);
4031
4032 /* Cycle through all NVME rings and complete each IO with
4033 * a local driver reason code. This is a flush so no
4034 * abort exchange to FW.
4035 */
4036 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
4037 pring = phba->sli4_hba.nvme_wq[i]->pring;
4038
James Smart895427b2017-02-12 13:52:30 -08004039 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -08004040 list_for_each_entry_safe(piocb, next_iocb,
4041 &pring->txcmplq, list)
4042 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4043 /* Retrieve everything on the txcmplq */
James Smart895427b2017-02-12 13:52:30 -08004044 list_splice_init(&pring->txcmplq, &txcmplq);
4045 pring->txcmplq_cnt = 0;
4046 spin_unlock_irq(&pring->ring_lock);
4047
4048 /* Flush the txcmpq &&&PAE */
4049 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4050 IOSTAT_LOCAL_REJECT,
4051 IOERR_SLI_DOWN);
4052 }
4053}
4054
4055/**
James Smart3772a992009-05-22 14:50:54 -04004056 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004057 * @phba: Pointer to HBA context object.
4058 * @mask: Bit mask to be checked.
4059 *
4060 * This function reads the host status register and compares
4061 * with the provided bit mask to check if HBA completed
4062 * the restart. This function will wait in a loop for the
4063 * HBA to complete restart. If the HBA does not restart within
4064 * 15 iterations, the function will reset the HBA again. The
4065 * function returns 1 when HBA fail to restart otherwise returns
4066 * zero.
4067 **/
James Smart3772a992009-05-22 14:50:54 -04004068static int
4069lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004070{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004071 uint32_t status;
4072 int i = 0;
4073 int retval = 0;
dea31012005-04-17 16:05:31 -05004074
Jamie Wellnitz41415862006-02-28 19:25:27 -05004075 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004076 if (lpfc_readl(phba->HSregaddr, &status))
4077 return 1;
dea31012005-04-17 16:05:31 -05004078
Jamie Wellnitz41415862006-02-28 19:25:27 -05004079 /*
4080 * Check status register every 100ms for 5 retries, then every
4081 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4082 * every 2.5 sec for 4.
4083 * Break our of the loop if errors occurred during init.
4084 */
4085 while (((status & mask) != mask) &&
4086 !(status & HS_FFERM) &&
4087 i++ < 20) {
dea31012005-04-17 16:05:31 -05004088
Jamie Wellnitz41415862006-02-28 19:25:27 -05004089 if (i <= 5)
4090 msleep(10);
4091 else if (i <= 10)
4092 msleep(500);
4093 else
4094 msleep(2500);
dea31012005-04-17 16:05:31 -05004095
Jamie Wellnitz41415862006-02-28 19:25:27 -05004096 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004097 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004098 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004099 lpfc_sli_brdrestart(phba);
4100 }
4101 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004102 if (lpfc_readl(phba->HSregaddr, &status)) {
4103 retval = 1;
4104 break;
4105 }
dea31012005-04-17 16:05:31 -05004106 }
dea31012005-04-17 16:05:31 -05004107
Jamie Wellnitz41415862006-02-28 19:25:27 -05004108 /* Check to see if any errors occurred during init */
4109 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05004110 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4111 "2751 Adapter failed to restart, "
4112 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4113 status,
4114 readl(phba->MBslimaddr + 0xa8),
4115 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004116 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004117 retval = 1;
4118 }
dea31012005-04-17 16:05:31 -05004119
Jamie Wellnitz41415862006-02-28 19:25:27 -05004120 return retval;
dea31012005-04-17 16:05:31 -05004121}
4122
James Smartda0436e2009-05-22 14:51:39 -04004123/**
4124 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4125 * @phba: Pointer to HBA context object.
4126 * @mask: Bit mask to be checked.
4127 *
4128 * This function checks the host status register to check if HBA is
4129 * ready. This function will wait in a loop for the HBA to be ready
4130 * If the HBA is not ready , the function will will reset the HBA PCI
4131 * function again. The function returns 1 when HBA fail to be ready
4132 * otherwise returns zero.
4133 **/
4134static int
4135lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4136{
4137 uint32_t status;
4138 int retval = 0;
4139
4140 /* Read the HBA Host Status Register */
4141 status = lpfc_sli4_post_status_check(phba);
4142
4143 if (status) {
4144 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4145 lpfc_sli_brdrestart(phba);
4146 status = lpfc_sli4_post_status_check(phba);
4147 }
4148
4149 /* Check to see if any errors occurred during init */
4150 if (status) {
4151 phba->link_state = LPFC_HBA_ERROR;
4152 retval = 1;
4153 } else
4154 phba->sli4_hba.intr_enable = 0;
4155
4156 return retval;
4157}
4158
4159/**
4160 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4161 * @phba: Pointer to HBA context object.
4162 * @mask: Bit mask to be checked.
4163 *
4164 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4165 * from the API jump table function pointer from the lpfc_hba struct.
4166 **/
4167int
4168lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4169{
4170 return phba->lpfc_sli_brdready(phba, mask);
4171}
4172
James Smart92908312006-03-07 15:04:13 -05004173#define BARRIER_TEST_PATTERN (0xdeadbeef)
4174
James Smarte59058c2008-08-24 21:49:00 -04004175/**
James Smart3621a712009-04-06 18:47:14 -04004176 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004177 * @phba: Pointer to HBA context object.
4178 *
James Smart1b511972011-12-13 13:23:09 -05004179 * This function is called before resetting an HBA. This function is called
4180 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004181 **/
James Smart2e0fef82007-06-17 19:56:36 -05004182void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004183{
James Smart65a29c12006-07-06 15:50:50 -04004184 uint32_t __iomem *resp_buf;
4185 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004186 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004187 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004188 int i;
4189 uint8_t hdrtype;
4190
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004191 lockdep_assert_held(&phba->hbalock);
4192
James Smart92908312006-03-07 15:04:13 -05004193 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4194 if (hdrtype != 0x80 ||
4195 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4196 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4197 return;
4198
4199 /*
4200 * Tell the other part of the chip to suspend temporarily all
4201 * its DMA activity.
4202 */
James Smart65a29c12006-07-06 15:50:50 -04004203 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004204
4205 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004206 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4207 return;
James Smart92908312006-03-07 15:04:13 -05004208 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4209 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004210 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004211
James Smart9940b972011-03-11 16:06:12 -05004212 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4213 return;
4214 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004215 /* Clear Chip error bit */
4216 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004217 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004218 }
4219
4220 mbox = 0;
4221 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4222 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4223
4224 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004225 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004226 writel(mbox, mbox_buf);
4227
James Smart9940b972011-03-11 16:06:12 -05004228 for (i = 0; i < 50; i++) {
4229 if (lpfc_readl((resp_buf + 1), &resp_data))
4230 return;
4231 if (resp_data != ~(BARRIER_TEST_PATTERN))
4232 mdelay(1);
4233 else
4234 break;
4235 }
4236 resp_data = 0;
4237 if (lpfc_readl((resp_buf + 1), &resp_data))
4238 return;
4239 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004240 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004241 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004242 goto restore_hc;
4243 else
4244 goto clear_errat;
4245 }
4246
4247 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004248 resp_data = 0;
4249 for (i = 0; i < 500; i++) {
4250 if (lpfc_readl(resp_buf, &resp_data))
4251 return;
4252 if (resp_data != mbox)
4253 mdelay(1);
4254 else
4255 break;
4256 }
James Smart92908312006-03-07 15:04:13 -05004257
4258clear_errat:
4259
James Smart9940b972011-03-11 16:06:12 -05004260 while (++i < 500) {
4261 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4262 return;
4263 if (!(ha_copy & HA_ERATT))
4264 mdelay(1);
4265 else
4266 break;
4267 }
James Smart92908312006-03-07 15:04:13 -05004268
4269 if (readl(phba->HAregaddr) & HA_ERATT) {
4270 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004271 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004272 }
4273
4274restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004275 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004276 writel(hc_copy, phba->HCregaddr);
4277 readl(phba->HCregaddr); /* flush */
4278}
4279
James Smarte59058c2008-08-24 21:49:00 -04004280/**
James Smart3621a712009-04-06 18:47:14 -04004281 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004282 * @phba: Pointer to HBA context object.
4283 *
4284 * This function issues a kill_board mailbox command and waits for
4285 * the error attention interrupt. This function is called for stopping
4286 * the firmware processing. The caller is not required to hold any
4287 * locks. This function calls lpfc_hba_down_post function to free
4288 * any pending commands after the kill. The function will return 1 when it
4289 * fails to kill the board else will return 0.
4290 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004291int
James Smart2e0fef82007-06-17 19:56:36 -05004292lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004293{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004294 struct lpfc_sli *psli;
4295 LPFC_MBOXQ_t *pmb;
4296 uint32_t status;
4297 uint32_t ha_copy;
4298 int retval;
4299 int i = 0;
4300
4301 psli = &phba->sli;
4302
4303 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004304 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004305 "0329 Kill HBA Data: x%x x%x\n",
4306 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004307
James Smart98c9ea52007-10-27 13:37:33 -04004308 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4309 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004310 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004311
4312 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004313 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004314 if (lpfc_readl(phba->HCregaddr, &status)) {
4315 spin_unlock_irq(&phba->hbalock);
4316 mempool_free(pmb, phba->mbox_mem_pool);
4317 return 1;
4318 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004319 status &= ~HC_ERINT_ENA;
4320 writel(status, phba->HCregaddr);
4321 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004322 phba->link_flag |= LS_IGNORE_ERATT;
4323 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004324
4325 lpfc_kill_board(phba, pmb);
4326 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4327 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4328
4329 if (retval != MBX_SUCCESS) {
4330 if (retval != MBX_BUSY)
4331 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004332 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4333 "2752 KILL_BOARD command failed retval %d\n",
4334 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004335 spin_lock_irq(&phba->hbalock);
4336 phba->link_flag &= ~LS_IGNORE_ERATT;
4337 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004338 return 1;
4339 }
4340
James Smartf4b4c682009-05-22 14:53:12 -04004341 spin_lock_irq(&phba->hbalock);
4342 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4343 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004344
Jamie Wellnitz41415862006-02-28 19:25:27 -05004345 mempool_free(pmb, phba->mbox_mem_pool);
4346
4347 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4348 * attention every 100ms for 3 seconds. If we don't get ERATT after
4349 * 3 seconds we still set HBA_ERROR state because the status of the
4350 * board is now undefined.
4351 */
James Smart9940b972011-03-11 16:06:12 -05004352 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4353 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004354 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4355 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004356 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4357 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004358 }
4359
4360 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004361 if (ha_copy & HA_ERATT) {
4362 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004363 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004364 }
James Smart2e0fef82007-06-17 19:56:36 -05004365 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004366 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004367 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004368 phba->link_flag &= ~LS_IGNORE_ERATT;
4369 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004370
Jamie Wellnitz41415862006-02-28 19:25:27 -05004371 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004372 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004373
James Smart2e0fef82007-06-17 19:56:36 -05004374 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004375}
4376
James Smarte59058c2008-08-24 21:49:00 -04004377/**
James Smart3772a992009-05-22 14:50:54 -04004378 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004379 * @phba: Pointer to HBA context object.
4380 *
4381 * This function resets the HBA by writing HC_INITFF to the control
4382 * register. After the HBA resets, this function resets all the iocb ring
4383 * indices. This function disables PCI layer parity checking during
4384 * the reset.
4385 * This function returns 0 always.
4386 * The caller is not required to hold any locks.
4387 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004388int
James Smart2e0fef82007-06-17 19:56:36 -05004389lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004390{
4391 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004392 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004393 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004394 int i;
dea31012005-04-17 16:05:31 -05004395
Jamie Wellnitz41415862006-02-28 19:25:27 -05004396 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004397
Jamie Wellnitz41415862006-02-28 19:25:27 -05004398 /* Reset HBA */
4399 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004400 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004401 (phba->pport) ? phba->pport->port_state : 0,
4402 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004403
4404 /* perform board reset */
4405 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004406 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004407 if (phba->pport) {
4408 phba->pport->fc_myDID = 0;
4409 phba->pport->fc_prevDID = 0;
4410 }
dea31012005-04-17 16:05:31 -05004411
Jamie Wellnitz41415862006-02-28 19:25:27 -05004412 /* Turn off parity checking and serr during the physical reset */
4413 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4414 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4415 (cfg_value &
4416 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4417
James Smart3772a992009-05-22 14:50:54 -04004418 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4419
Jamie Wellnitz41415862006-02-28 19:25:27 -05004420 /* Now toggle INITFF bit in the Host Control Register */
4421 writel(HC_INITFF, phba->HCregaddr);
4422 mdelay(1);
4423 readl(phba->HCregaddr); /* flush */
4424 writel(0, phba->HCregaddr);
4425 readl(phba->HCregaddr); /* flush */
4426
4427 /* Restore PCI cmd register */
4428 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004429
4430 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004431 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004432 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004433 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004434 pring->sli.sli3.rspidx = 0;
4435 pring->sli.sli3.next_cmdidx = 0;
4436 pring->sli.sli3.local_getidx = 0;
4437 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004438 pring->missbufcnt = 0;
4439 }
dea31012005-04-17 16:05:31 -05004440
James Smart2e0fef82007-06-17 19:56:36 -05004441 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004442 return 0;
4443}
4444
James Smarte59058c2008-08-24 21:49:00 -04004445/**
James Smartda0436e2009-05-22 14:51:39 -04004446 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4447 * @phba: Pointer to HBA context object.
4448 *
4449 * This function resets a SLI4 HBA. This function disables PCI layer parity
4450 * checking during resets the device. The caller is not required to hold
4451 * any locks.
4452 *
4453 * This function returns 0 always.
4454 **/
4455int
4456lpfc_sli4_brdreset(struct lpfc_hba *phba)
4457{
4458 struct lpfc_sli *psli = &phba->sli;
4459 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004460 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004461
4462 /* Reset HBA */
4463 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004464 "0295 Reset HBA Data: x%x x%x x%x\n",
4465 phba->pport->port_state, psli->sli_flag,
4466 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004467
4468 /* perform board reset */
4469 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004470 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004471 phba->pport->fc_myDID = 0;
4472 phba->pport->fc_prevDID = 0;
4473
James Smartda0436e2009-05-22 14:51:39 -04004474 spin_lock_irq(&phba->hbalock);
4475 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4476 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004477 spin_unlock_irq(&phba->hbalock);
4478
James Smart02936352014-04-04 13:52:12 -04004479 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4480 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4481 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4482 return rc;
4483 }
4484
James Smartda0436e2009-05-22 14:51:39 -04004485 /* Now physically reset the device */
4486 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4487 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004488
4489 /* Turn off parity checking and serr during the physical reset */
4490 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4491 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4492 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4493
James Smart88318812012-09-29 11:29:29 -04004494 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004495 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004496
James Smartbe858b62010-12-15 17:57:20 -05004497 /* Restore PCI cmd register */
4498 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4499
James Smart27b01b82012-05-09 21:19:44 -04004500 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004501}
4502
4503/**
4504 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004505 * @phba: Pointer to HBA context object.
4506 *
4507 * This function is called in the SLI initialization code path to
4508 * restart the HBA. The caller is not required to hold any lock.
4509 * This function writes MBX_RESTART mailbox command to the SLIM and
4510 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4511 * function to free any pending commands. The function enables
4512 * POST only during the first initialization. The function returns zero.
4513 * The function does not guarantee completion of MBX_RESTART mailbox
4514 * command before the return of this function.
4515 **/
James Smartda0436e2009-05-22 14:51:39 -04004516static int
4517lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004518{
4519 MAILBOX_t *mb;
4520 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004521 volatile uint32_t word0;
4522 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004523 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004524
James Smart2e0fef82007-06-17 19:56:36 -05004525 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004526
James Smart0d878412009-10-02 15:16:56 -04004527 /* Take PCIe device Advanced Error Reporting (AER) state */
4528 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4529
Jamie Wellnitz41415862006-02-28 19:25:27 -05004530 psli = &phba->sli;
4531
4532 /* Restart HBA */
4533 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004534 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004535 (phba->pport) ? phba->pport->port_state : 0,
4536 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004537
4538 word0 = 0;
4539 mb = (MAILBOX_t *) &word0;
4540 mb->mbxCommand = MBX_RESTART;
4541 mb->mbxHc = 1;
4542
James Smart92908312006-03-07 15:04:13 -05004543 lpfc_reset_barrier(phba);
4544
Jamie Wellnitz41415862006-02-28 19:25:27 -05004545 to_slim = phba->MBslimaddr;
4546 writel(*(uint32_t *) mb, to_slim);
4547 readl(to_slim); /* flush */
4548
4549 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004550 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004551 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004552 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004553 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004554 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004555 writel(*(uint32_t *) mb, to_slim);
4556 readl(to_slim); /* flush */
4557
4558 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004559 if (phba->pport)
4560 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004561 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004562 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004563 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004564
James Smart64ba8812006-08-02 15:24:34 -04004565 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4566 psli->stats_start = get_seconds();
4567
James Smarteaf15d52008-12-04 22:39:29 -05004568 /* Give the INITFF and Post time to settle. */
4569 mdelay(100);
dea31012005-04-17 16:05:31 -05004570
James Smart0d878412009-10-02 15:16:56 -04004571 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4572 if (hba_aer_enabled)
4573 pci_disable_pcie_error_reporting(phba->pcidev);
4574
Jamie Wellnitz41415862006-02-28 19:25:27 -05004575 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004576
4577 return 0;
4578}
4579
James Smarte59058c2008-08-24 21:49:00 -04004580/**
James Smartda0436e2009-05-22 14:51:39 -04004581 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4582 * @phba: Pointer to HBA context object.
4583 *
4584 * This function is called in the SLI initialization code path to restart
4585 * a SLI4 HBA. The caller is not required to hold any lock.
4586 * At the end of the function, it calls lpfc_hba_down_post function to
4587 * free any pending commands.
4588 **/
4589static int
4590lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4591{
4592 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004593 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004594 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004595
4596 /* Restart HBA */
4597 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4598 "0296 Restart HBA Data: x%x x%x\n",
4599 phba->pport->port_state, psli->sli_flag);
4600
James Smart75baf692010-06-08 18:31:21 -04004601 /* Take PCIe device Advanced Error Reporting (AER) state */
4602 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4603
James Smart27b01b82012-05-09 21:19:44 -04004604 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004605
4606 spin_lock_irq(&phba->hbalock);
4607 phba->pport->stopped = 0;
4608 phba->link_state = LPFC_INIT_START;
4609 phba->hba_flag = 0;
4610 spin_unlock_irq(&phba->hbalock);
4611
4612 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4613 psli->stats_start = get_seconds();
4614
James Smart75baf692010-06-08 18:31:21 -04004615 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4616 if (hba_aer_enabled)
4617 pci_disable_pcie_error_reporting(phba->pcidev);
4618
James Smartda0436e2009-05-22 14:51:39 -04004619 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004620 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004621
James Smart27b01b82012-05-09 21:19:44 -04004622 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004623}
4624
4625/**
4626 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4627 * @phba: Pointer to HBA context object.
4628 *
4629 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4630 * API jump table function pointer from the lpfc_hba struct.
4631**/
4632int
4633lpfc_sli_brdrestart(struct lpfc_hba *phba)
4634{
4635 return phba->lpfc_sli_brdrestart(phba);
4636}
4637
4638/**
James Smart3621a712009-04-06 18:47:14 -04004639 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004640 * @phba: Pointer to HBA context object.
4641 *
4642 * This function is called after a HBA restart to wait for successful
4643 * restart of the HBA. Successful restart of the HBA is indicated by
4644 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4645 * iteration, the function will restart the HBA again. The function returns
4646 * zero if HBA successfully restarted else returns negative error code.
4647 **/
James Smart4492b732017-04-27 15:08:26 -07004648int
dea31012005-04-17 16:05:31 -05004649lpfc_sli_chipset_init(struct lpfc_hba *phba)
4650{
4651 uint32_t status, i = 0;
4652
4653 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004654 if (lpfc_readl(phba->HSregaddr, &status))
4655 return -EIO;
dea31012005-04-17 16:05:31 -05004656
4657 /* Check status register to see what current state is */
4658 i = 0;
4659 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4660
James Smartdcf2a4e2010-09-29 11:18:53 -04004661 /* Check every 10ms for 10 retries, then every 100ms for 90
4662 * retries, then every 1 sec for 50 retires for a total of
4663 * ~60 seconds before reset the board again and check every
4664 * 1 sec for 50 retries. The up to 60 seconds before the
4665 * board ready is required by the Falcon FIPS zeroization
4666 * complete, and any reset the board in between shall cause
4667 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004668 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004669 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004670 /* Adapter failed to init, timeout, status reg
4671 <status> */
James Smarted957682007-06-17 19:56:37 -05004672 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004673 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004674 "timeout, status reg x%x, "
4675 "FW Data: A8 x%x AC x%x\n", status,
4676 readl(phba->MBslimaddr + 0xa8),
4677 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004678 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004679 return -ETIMEDOUT;
4680 }
4681
4682 /* Check to see if any errors occurred during init */
4683 if (status & HS_FFERM) {
4684 /* ERROR: During chipset initialization */
4685 /* Adapter failed to init, chipset, status reg
4686 <status> */
James Smarted957682007-06-17 19:56:37 -05004687 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004688 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004689 "chipset, status reg x%x, "
4690 "FW Data: A8 x%x AC x%x\n", status,
4691 readl(phba->MBslimaddr + 0xa8),
4692 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004693 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004694 return -EIO;
4695 }
4696
James Smartdcf2a4e2010-09-29 11:18:53 -04004697 if (i <= 10)
dea31012005-04-17 16:05:31 -05004698 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004699 else if (i <= 100)
4700 msleep(100);
4701 else
4702 msleep(1000);
dea31012005-04-17 16:05:31 -05004703
James Smartdcf2a4e2010-09-29 11:18:53 -04004704 if (i == 150) {
4705 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004706 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004707 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004708 }
4709 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004710 if (lpfc_readl(phba->HSregaddr, &status))
4711 return -EIO;
dea31012005-04-17 16:05:31 -05004712 }
4713
4714 /* Check to see if any errors occurred during init */
4715 if (status & HS_FFERM) {
4716 /* ERROR: During chipset initialization */
4717 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004718 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004719 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004720 "status reg x%x, "
4721 "FW Data: A8 x%x AC x%x\n", status,
4722 readl(phba->MBslimaddr + 0xa8),
4723 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004724 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004725 return -EIO;
4726 }
4727
4728 /* Clear all interrupt enable conditions */
4729 writel(0, phba->HCregaddr);
4730 readl(phba->HCregaddr); /* flush */
4731
4732 /* setup host attn register */
4733 writel(0xffffffff, phba->HAregaddr);
4734 readl(phba->HAregaddr); /* flush */
4735 return 0;
4736}
4737
James Smarte59058c2008-08-24 21:49:00 -04004738/**
James Smart3621a712009-04-06 18:47:14 -04004739 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004740 *
4741 * This function calculates and returns the number of HBQs required to be
4742 * configured.
4743 **/
James Smart78b2d852007-08-02 11:10:21 -04004744int
James Smarted957682007-06-17 19:56:37 -05004745lpfc_sli_hbq_count(void)
4746{
James Smart92d7f7b2007-06-17 19:56:38 -05004747 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004748}
4749
James Smarte59058c2008-08-24 21:49:00 -04004750/**
James Smart3621a712009-04-06 18:47:14 -04004751 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004752 *
4753 * This function adds the number of hbq entries in every HBQ to get
4754 * the total number of hbq entries required for the HBA and returns
4755 * the total count.
4756 **/
James Smarted957682007-06-17 19:56:37 -05004757static int
4758lpfc_sli_hbq_entry_count(void)
4759{
4760 int hbq_count = lpfc_sli_hbq_count();
4761 int count = 0;
4762 int i;
4763
4764 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004765 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004766 return count;
4767}
4768
James Smarte59058c2008-08-24 21:49:00 -04004769/**
James Smart3621a712009-04-06 18:47:14 -04004770 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004771 *
4772 * This function calculates amount of memory required for all hbq entries
4773 * to be configured and returns the total memory required.
4774 **/
dea31012005-04-17 16:05:31 -05004775int
James Smarted957682007-06-17 19:56:37 -05004776lpfc_sli_hbq_size(void)
4777{
4778 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4779}
4780
James Smarte59058c2008-08-24 21:49:00 -04004781/**
James Smart3621a712009-04-06 18:47:14 -04004782 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004783 * @phba: Pointer to HBA context object.
4784 *
4785 * This function is called during the SLI initialization to configure
4786 * all the HBQs and post buffers to the HBQ. The caller is not
4787 * required to hold any locks. This function will return zero if successful
4788 * else it will return negative error code.
4789 **/
James Smarted957682007-06-17 19:56:37 -05004790static int
4791lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4792{
4793 int hbq_count = lpfc_sli_hbq_count();
4794 LPFC_MBOXQ_t *pmb;
4795 MAILBOX_t *pmbox;
4796 uint32_t hbqno;
4797 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004798
James Smart92d7f7b2007-06-17 19:56:38 -05004799 /* Get a Mailbox buffer to setup mailbox
4800 * commands for HBA initialization
4801 */
James Smarted957682007-06-17 19:56:37 -05004802 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4803
4804 if (!pmb)
4805 return -ENOMEM;
4806
James Smart04c68492009-05-22 14:52:52 -04004807 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004808
4809 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4810 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004811 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004812
4813 hbq_entry_index = 0;
4814 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4815 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4816 phba->hbqs[hbqno].hbqPutIdx = 0;
4817 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4818 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004819 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004820 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4821 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004822 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4823
4824 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4825 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4826 mbxStatus <status>, ring <num> */
4827
4828 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004829 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004830 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004831 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004832 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004833 pmbox->mbxStatus, hbqno);
4834
4835 phba->link_state = LPFC_HBA_ERROR;
4836 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004837 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004838 }
4839 }
4840 phba->hbq_count = hbq_count;
4841
James Smarted957682007-06-17 19:56:37 -05004842 mempool_free(pmb, phba->mbox_mem_pool);
4843
James Smart92d7f7b2007-06-17 19:56:38 -05004844 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004845 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4846 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004847 return 0;
4848}
4849
James Smarte59058c2008-08-24 21:49:00 -04004850/**
James Smart4f774512009-05-22 14:52:35 -04004851 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4852 * @phba: Pointer to HBA context object.
4853 *
4854 * This function is called during the SLI initialization to configure
4855 * all the HBQs and post buffers to the HBQ. The caller is not
4856 * required to hold any locks. This function will return zero if successful
4857 * else it will return negative error code.
4858 **/
4859static int
4860lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4861{
4862 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004863 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4864 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004865 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004866 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004867 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004868 return 0;
4869}
4870
4871/**
James Smart3621a712009-04-06 18:47:14 -04004872 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004873 * @phba: Pointer to HBA context object.
4874 * @sli_mode: sli mode - 2/3
4875 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004876 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004877 * to issue config_port mailbox command. This function restarts the
4878 * HBA firmware and issues a config_port mailbox command to configure
4879 * the SLI interface in the sli mode specified by sli_mode
4880 * variable. The caller is not required to hold any locks.
4881 * The function returns 0 if successful, else returns negative error
4882 * code.
4883 **/
James Smart93996272008-08-24 21:50:30 -04004884int
4885lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004886{
4887 LPFC_MBOXQ_t *pmb;
4888 uint32_t resetcount = 0, rc = 0, done = 0;
4889
4890 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4891 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004892 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004893 return -ENOMEM;
4894 }
4895
James Smarted957682007-06-17 19:56:37 -05004896 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004897 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004898 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004899 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004900 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004901 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004902 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004903 rc = lpfc_sli_chipset_init(phba);
4904 if (rc)
4905 break;
4906
James Smart2e0fef82007-06-17 19:56:36 -05004907 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004908 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004909 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004910 resetcount++;
4911
James Smarted957682007-06-17 19:56:37 -05004912 /* Call pre CONFIG_PORT mailbox command initialization. A
4913 * value of 0 means the call was successful. Any other
4914 * nonzero value is a failure, but if ERESTART is returned,
4915 * the driver may reset the HBA and try again.
4916 */
dea31012005-04-17 16:05:31 -05004917 rc = lpfc_config_port_prep(phba);
4918 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004919 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004920 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004921 } else if (rc)
dea31012005-04-17 16:05:31 -05004922 break;
James Smart6d368e52011-05-24 11:44:12 -04004923
James Smart2e0fef82007-06-17 19:56:36 -05004924 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004925 lpfc_config_port(phba, pmb);
4926 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004927 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4928 LPFC_SLI3_HBQ_ENABLED |
4929 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004930 LPFC_SLI3_BG_ENABLED |
4931 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004932 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004933 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004934 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004935 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004936 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004937 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004938 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004939 spin_unlock_irq(&phba->hbalock);
4940 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004941 } else {
4942 /* Allow asynchronous mailbox command to go through */
4943 spin_lock_irq(&phba->hbalock);
4944 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4945 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004946 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004947
4948 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4949 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4950 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4951 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004952 }
dea31012005-04-17 16:05:31 -05004953 }
James Smarted957682007-06-17 19:56:37 -05004954 if (!done) {
4955 rc = -EINVAL;
4956 goto do_prep_failed;
4957 }
James Smart04c68492009-05-22 14:52:52 -04004958 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4959 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004960 rc = -ENXIO;
4961 goto do_prep_failed;
4962 }
James Smart04c68492009-05-22 14:52:52 -04004963 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004964 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004965 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4966 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4967 phba->max_vpi : phba->max_vports;
4968
James Smart34b02dc2008-08-24 21:49:55 -04004969 } else
4970 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004971 phba->fips_level = 0;
4972 phba->fips_spec_rev = 0;
4973 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004974 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004975 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4976 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4977 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4978 "2850 Security Crypto Active. FIPS x%d "
4979 "(Spec Rev: x%d)",
4980 phba->fips_level, phba->fips_spec_rev);
4981 }
4982 if (pmb->u.mb.un.varCfgPort.sec_err) {
4983 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4984 "2856 Config Port Security Crypto "
4985 "Error: x%x ",
4986 pmb->u.mb.un.varCfgPort.sec_err);
4987 }
James Smart04c68492009-05-22 14:52:52 -04004988 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004989 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004990 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004991 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004992
4993 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4994 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004995
James Smartf44ac122018-03-05 12:04:08 -08004996 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
4997 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
4998 phba->cfg_enable_bg = 0;
4999 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smarte2a0a9d2008-12-04 22:40:02 -05005000 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5001 "0443 Adapter did not grant "
5002 "BlockGuard\n");
James Smartf44ac122018-03-05 12:04:08 -08005003 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005004 }
James Smart34b02dc2008-08-24 21:49:55 -04005005 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05005006 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04005007 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04005008 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05005009 }
James Smart92d7f7b2007-06-17 19:56:38 -05005010do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05005011 mempool_free(pmb, phba->mbox_mem_pool);
5012 return rc;
5013}
5014
James Smarte59058c2008-08-24 21:49:00 -04005015
5016/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08005017 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04005018 * @phba: Pointer to HBA context object.
5019 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005020 * This function is the main SLI initialization function. This function
5021 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04005022 * error attention handler code. Caller is not required to hold any
5023 * locks. This function issues config_port mailbox command to configure
5024 * the SLI, setup iocb rings and HBQ rings. In the end the function
5025 * calls the config_port_post function to issue init_link mailbox
5026 * command and to start the discovery. The function will return zero
5027 * if successful, else it will return negative error code.
5028 **/
James Smarted957682007-06-17 19:56:37 -05005029int
5030lpfc_sli_hba_setup(struct lpfc_hba *phba)
5031{
5032 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04005033 int mode = 3, i;
5034 int longs;
James Smarted957682007-06-17 19:56:37 -05005035
James Smart12247e82016-07-06 12:36:09 -07005036 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05005037 case 2:
James Smart78b2d852007-08-02 11:10:21 -04005038 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05005039 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005040 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05005041 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07005042 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05005043 break;
5044 }
James Smarted957682007-06-17 19:56:37 -05005045 mode = 2;
5046 break;
5047 case 0:
5048 case 3:
5049 break;
5050 default:
James Smart92d7f7b2007-06-17 19:56:38 -05005051 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005052 "1819 Unrecognized sli_mode parameter: %d.\n",
5053 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05005054
5055 break;
5056 }
James Smartb5c53952016-03-31 14:12:30 -07005057 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005058
James Smart93996272008-08-24 21:50:30 -04005059 rc = lpfc_sli_config_port(phba, mode);
5060
James Smart12247e82016-07-06 12:36:09 -07005061 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05005062 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005063 "1820 Unable to select SLI-3. "
5064 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05005065 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04005066 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07005067 else if (rc && mode == 2)
5068 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05005069 if (rc)
dea31012005-04-17 16:05:31 -05005070 goto lpfc_sli_hba_setup_error;
5071
James Smart0d878412009-10-02 15:16:56 -04005072 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5073 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5074 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5075 if (!rc) {
5076 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5077 "2709 This device supports "
5078 "Advanced Error Reporting (AER)\n");
5079 spin_lock_irq(&phba->hbalock);
5080 phba->hba_flag |= HBA_AER_ENABLED;
5081 spin_unlock_irq(&phba->hbalock);
5082 } else {
5083 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5084 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005085 "Advanced Error Reporting (AER): %d\n",
5086 rc);
James Smart0d878412009-10-02 15:16:56 -04005087 phba->cfg_aer_support = 0;
5088 }
5089 }
5090
James Smarted957682007-06-17 19:56:37 -05005091 if (phba->sli_rev == 3) {
5092 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5093 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005094 } else {
5095 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5096 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005097 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005098 }
5099
5100 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005101 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5102 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005103 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005104
5105 if (rc)
5106 goto lpfc_sli_hba_setup_error;
5107
James Smart6d368e52011-05-24 11:44:12 -04005108 /* Initialize VPIs. */
5109 if (phba->sli_rev == LPFC_SLI_REV3) {
5110 /*
5111 * The VPI bitmask and physical ID array are allocated
5112 * and initialized once only - at driver load. A port
5113 * reset doesn't need to reinitialize this memory.
5114 */
5115 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5116 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5117 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
5118 GFP_KERNEL);
5119 if (!phba->vpi_bmask) {
5120 rc = -ENOMEM;
5121 goto lpfc_sli_hba_setup_error;
5122 }
5123
5124 phba->vpi_ids = kzalloc(
5125 (phba->max_vpi+1) * sizeof(uint16_t),
5126 GFP_KERNEL);
5127 if (!phba->vpi_ids) {
5128 kfree(phba->vpi_bmask);
5129 rc = -ENOMEM;
5130 goto lpfc_sli_hba_setup_error;
5131 }
5132 for (i = 0; i < phba->max_vpi; i++)
5133 phba->vpi_ids[i] = i;
5134 }
5135 }
5136
James Smart93996272008-08-24 21:50:30 -04005137 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005138 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5139 rc = lpfc_sli_hbq_setup(phba);
5140 if (rc)
5141 goto lpfc_sli_hba_setup_error;
5142 }
James Smart04c68492009-05-22 14:52:52 -04005143 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005144 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005145 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005146
5147 rc = lpfc_config_port_post(phba);
5148 if (rc)
5149 goto lpfc_sli_hba_setup_error;
5150
James Smarted957682007-06-17 19:56:37 -05005151 return rc;
5152
James Smart92d7f7b2007-06-17 19:56:38 -05005153lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005154 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05005155 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005156 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005157 return rc;
5158}
5159
James Smartda0436e2009-05-22 14:51:39 -04005160/**
5161 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5162 * @phba: Pointer to HBA context object.
5163 * @mboxq: mailbox pointer.
5164 * This function issue a dump mailbox command to read config region
5165 * 23 and parse the records in the region and populate driver
5166 * data structure.
5167 **/
5168static int
James Smartff78d8f2011-12-13 13:21:35 -05005169lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005170{
James Smartff78d8f2011-12-13 13:21:35 -05005171 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005172 struct lpfc_dmabuf *mp;
5173 struct lpfc_mqe *mqe;
5174 uint32_t data_length;
5175 int rc;
5176
5177 /* Program the default value of vlan_id and fc_map */
5178 phba->valid_vlan = 0;
5179 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5180 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5181 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5182
James Smartff78d8f2011-12-13 13:21:35 -05005183 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5184 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005185 return -ENOMEM;
5186
James Smartff78d8f2011-12-13 13:21:35 -05005187 mqe = &mboxq->u.mqe;
5188 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5189 rc = -ENOMEM;
5190 goto out_free_mboxq;
5191 }
5192
James Smartda0436e2009-05-22 14:51:39 -04005193 mp = (struct lpfc_dmabuf *) mboxq->context1;
5194 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5195
5196 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5197 "(%d):2571 Mailbox cmd x%x Status x%x "
5198 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5199 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5200 "CQ: x%x x%x x%x x%x\n",
5201 mboxq->vport ? mboxq->vport->vpi : 0,
5202 bf_get(lpfc_mqe_command, mqe),
5203 bf_get(lpfc_mqe_status, mqe),
5204 mqe->un.mb_words[0], mqe->un.mb_words[1],
5205 mqe->un.mb_words[2], mqe->un.mb_words[3],
5206 mqe->un.mb_words[4], mqe->un.mb_words[5],
5207 mqe->un.mb_words[6], mqe->un.mb_words[7],
5208 mqe->un.mb_words[8], mqe->un.mb_words[9],
5209 mqe->un.mb_words[10], mqe->un.mb_words[11],
5210 mqe->un.mb_words[12], mqe->un.mb_words[13],
5211 mqe->un.mb_words[14], mqe->un.mb_words[15],
5212 mqe->un.mb_words[16], mqe->un.mb_words[50],
5213 mboxq->mcqe.word0,
5214 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5215 mboxq->mcqe.trailer);
5216
5217 if (rc) {
5218 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5219 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005220 rc = -EIO;
5221 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005222 }
5223 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005224 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005225 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5226 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005227 rc = -EIO;
5228 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005229 }
James Smartda0436e2009-05-22 14:51:39 -04005230
5231 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5232 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5233 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005234 rc = 0;
5235
5236out_free_mboxq:
5237 mempool_free(mboxq, phba->mbox_mem_pool);
5238 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005239}
5240
5241/**
5242 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5243 * @phba: pointer to lpfc hba data structure.
5244 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5245 * @vpd: pointer to the memory to hold resulting port vpd data.
5246 * @vpd_size: On input, the number of bytes allocated to @vpd.
5247 * On output, the number of data bytes in @vpd.
5248 *
5249 * This routine executes a READ_REV SLI4 mailbox command. In
5250 * addition, this routine gets the port vpd data.
5251 *
5252 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005253 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005254 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005255 **/
5256static int
5257lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5258 uint8_t *vpd, uint32_t *vpd_size)
5259{
5260 int rc = 0;
5261 uint32_t dma_size;
5262 struct lpfc_dmabuf *dmabuf;
5263 struct lpfc_mqe *mqe;
5264
5265 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5266 if (!dmabuf)
5267 return -ENOMEM;
5268
5269 /*
5270 * Get a DMA buffer for the vpd data resulting from the READ_REV
5271 * mailbox command.
5272 */
5273 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04005274 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5275 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005276 if (!dmabuf->virt) {
5277 kfree(dmabuf);
5278 return -ENOMEM;
5279 }
James Smartda0436e2009-05-22 14:51:39 -04005280
5281 /*
5282 * The SLI4 implementation of READ_REV conflicts at word1,
5283 * bits 31:16 and SLI4 adds vpd functionality not present
5284 * in SLI3. This code corrects the conflicts.
5285 */
5286 lpfc_read_rev(phba, mboxq);
5287 mqe = &mboxq->u.mqe;
5288 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5289 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5290 mqe->un.read_rev.word1 &= 0x0000FFFF;
5291 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5292 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5293
5294 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5295 if (rc) {
5296 dma_free_coherent(&phba->pcidev->dev, dma_size,
5297 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005298 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005299 return -EIO;
5300 }
5301
James Smartda0436e2009-05-22 14:51:39 -04005302 /*
5303 * The available vpd length cannot be bigger than the
5304 * DMA buffer passed to the port. Catch the less than
5305 * case and update the caller's size.
5306 */
5307 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5308 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5309
James Smartd7c47992010-06-08 18:31:54 -04005310 memcpy(vpd, dmabuf->virt, *vpd_size);
5311
James Smartda0436e2009-05-22 14:51:39 -04005312 dma_free_coherent(&phba->pcidev->dev, dma_size,
5313 dmabuf->virt, dmabuf->phys);
5314 kfree(dmabuf);
5315 return 0;
5316}
5317
5318/**
James Smartcd1c8302011-10-10 21:33:25 -04005319 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5320 * @phba: pointer to lpfc hba data structure.
5321 *
5322 * This routine retrieves SLI4 device physical port name this PCI function
5323 * is attached to.
5324 *
5325 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005326 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005327 * otherwise - failed to retrieve physical port name
5328 **/
5329static int
5330lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5331{
5332 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005333 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5334 struct lpfc_controller_attribute *cntl_attr;
5335 struct lpfc_mbx_get_port_name *get_port_name;
5336 void *virtaddr = NULL;
5337 uint32_t alloclen, reqlen;
5338 uint32_t shdr_status, shdr_add_status;
5339 union lpfc_sli4_cfg_shdr *shdr;
5340 char cport_name = 0;
5341 int rc;
5342
5343 /* We assume nothing at this point */
5344 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5345 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5346
5347 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5348 if (!mboxq)
5349 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005350 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005351 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5352 lpfc_sli4_read_config(phba);
5353 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5354 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005355
5356 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5357 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5358 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5359 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5360 LPFC_SLI4_MBX_NEMBED);
5361 if (alloclen < reqlen) {
5362 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5363 "3084 Allocated DMA memory size (%d) is "
5364 "less than the requested DMA memory size "
5365 "(%d)\n", alloclen, reqlen);
5366 rc = -ENOMEM;
5367 goto out_free_mboxq;
5368 }
5369 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5370 virtaddr = mboxq->sge_array->addr[0];
5371 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5372 shdr = &mbx_cntl_attr->cfg_shdr;
5373 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5374 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5375 if (shdr_status || shdr_add_status || rc) {
5376 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5377 "3085 Mailbox x%x (x%x/x%x) failed, "
5378 "rc:x%x, status:x%x, add_status:x%x\n",
5379 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5380 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5381 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5382 rc, shdr_status, shdr_add_status);
5383 rc = -ENXIO;
5384 goto out_free_mboxq;
5385 }
5386 cntl_attr = &mbx_cntl_attr->cntl_attr;
5387 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5388 phba->sli4_hba.lnk_info.lnk_tp =
5389 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5390 phba->sli4_hba.lnk_info.lnk_no =
5391 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5392 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5393 "3086 lnk_type:%d, lnk_numb:%d\n",
5394 phba->sli4_hba.lnk_info.lnk_tp,
5395 phba->sli4_hba.lnk_info.lnk_no);
5396
5397retrieve_ppname:
5398 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5399 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5400 sizeof(struct lpfc_mbx_get_port_name) -
5401 sizeof(struct lpfc_sli4_cfg_mhdr),
5402 LPFC_SLI4_MBX_EMBED);
5403 get_port_name = &mboxq->u.mqe.un.get_port_name;
5404 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5405 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5406 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5407 phba->sli4_hba.lnk_info.lnk_tp);
5408 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5409 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5410 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5411 if (shdr_status || shdr_add_status || rc) {
5412 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5413 "3087 Mailbox x%x (x%x/x%x) failed: "
5414 "rc:x%x, status:x%x, add_status:x%x\n",
5415 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5416 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5417 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5418 rc, shdr_status, shdr_add_status);
5419 rc = -ENXIO;
5420 goto out_free_mboxq;
5421 }
5422 switch (phba->sli4_hba.lnk_info.lnk_no) {
5423 case LPFC_LINK_NUMBER_0:
5424 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5425 &get_port_name->u.response);
5426 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5427 break;
5428 case LPFC_LINK_NUMBER_1:
5429 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5430 &get_port_name->u.response);
5431 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5432 break;
5433 case LPFC_LINK_NUMBER_2:
5434 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5435 &get_port_name->u.response);
5436 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5437 break;
5438 case LPFC_LINK_NUMBER_3:
5439 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5440 &get_port_name->u.response);
5441 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5442 break;
5443 default:
5444 break;
5445 }
5446
5447 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5448 phba->Port[0] = cport_name;
5449 phba->Port[1] = '\0';
5450 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5451 "3091 SLI get port name: %s\n", phba->Port);
5452 }
5453
5454out_free_mboxq:
5455 if (rc != MBX_TIMEOUT) {
5456 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5457 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5458 else
5459 mempool_free(mboxq, phba->mbox_mem_pool);
5460 }
5461 return rc;
5462}
5463
5464/**
James Smartda0436e2009-05-22 14:51:39 -04005465 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5466 * @phba: pointer to lpfc hba data structure.
5467 *
5468 * This routine is called to explicitly arm the SLI4 device's completion and
5469 * event queues
5470 **/
5471static void
5472lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5473{
James Smart895427b2017-02-12 13:52:30 -08005474 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08005475 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartda0436e2009-05-22 14:51:39 -04005476
James Smartb71413d2018-02-22 08:18:40 -08005477 sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
5478 sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
5479 if (sli4_hba->nvmels_cq)
5480 sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
James Smart895427b2017-02-12 13:52:30 -08005481 LPFC_QUEUE_REARM);
5482
James Smartb71413d2018-02-22 08:18:40 -08005483 if (sli4_hba->fcp_cq)
James Smart895427b2017-02-12 13:52:30 -08005484 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005485 sli4_hba->sli4_cq_release(sli4_hba->fcp_cq[qidx],
James Smart895427b2017-02-12 13:52:30 -08005486 LPFC_QUEUE_REARM);
5487
James Smartb71413d2018-02-22 08:18:40 -08005488 if (sli4_hba->nvme_cq)
James Smart895427b2017-02-12 13:52:30 -08005489 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005490 sli4_hba->sli4_cq_release(sli4_hba->nvme_cq[qidx],
James Smart895427b2017-02-12 13:52:30 -08005491 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005492
James Smartf38fa0b2014-04-04 13:52:21 -04005493 if (phba->cfg_fof)
James Smartb71413d2018-02-22 08:18:40 -08005494 sli4_hba->sli4_cq_release(sli4_hba->oas_cq, LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005495
James Smartb71413d2018-02-22 08:18:40 -08005496 if (sli4_hba->hba_eq)
James Smart895427b2017-02-12 13:52:30 -08005497 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005498 sli4_hba->sli4_eq_release(sli4_hba->hba_eq[qidx],
5499 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005500
James Smart2d7dbc42017-02-12 13:52:35 -08005501 if (phba->nvmet_support) {
5502 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smartb71413d2018-02-22 08:18:40 -08005503 sli4_hba->sli4_cq_release(
5504 sli4_hba->nvmet_cqset[qidx],
James Smart2d7dbc42017-02-12 13:52:35 -08005505 LPFC_QUEUE_REARM);
5506 }
James Smart2e90f4b2011-12-13 13:22:37 -05005507 }
James Smart1ba981f2014-02-20 09:56:45 -05005508
5509 if (phba->cfg_fof)
James Smartb71413d2018-02-22 08:18:40 -08005510 sli4_hba->sli4_eq_release(sli4_hba->fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005511}
5512
5513/**
James Smart6d368e52011-05-24 11:44:12 -04005514 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5515 * @phba: Pointer to HBA context object.
5516 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005517 * @extnt_count: buffer to hold port available extent count.
5518 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005519 *
James Smartb76f2dc2011-07-22 18:37:42 -04005520 * This function calls the port and retrievs the number of available
5521 * extents and their size for a particular extent type.
5522 *
5523 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005524 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005525int
James Smart6d368e52011-05-24 11:44:12 -04005526lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5527 uint16_t *extnt_count, uint16_t *extnt_size)
5528{
5529 int rc = 0;
5530 uint32_t length;
5531 uint32_t mbox_tmo;
5532 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5533 LPFC_MBOXQ_t *mbox;
5534
5535 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5536 if (!mbox)
5537 return -ENOMEM;
5538
5539 /* Find out how many extents are available for this resource type */
5540 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5541 sizeof(struct lpfc_sli4_cfg_mhdr));
5542 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5543 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5544 length, LPFC_SLI4_MBX_EMBED);
5545
5546 /* Send an extents count of 0 - the GET doesn't use it. */
5547 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5548 LPFC_SLI4_MBX_EMBED);
5549 if (unlikely(rc)) {
5550 rc = -EIO;
5551 goto err_exit;
5552 }
5553
5554 if (!phba->sli4_hba.intr_enable)
5555 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5556 else {
James Smarta183a152011-10-10 21:32:43 -04005557 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005558 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5559 }
5560 if (unlikely(rc)) {
5561 rc = -EIO;
5562 goto err_exit;
5563 }
5564
5565 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5566 if (bf_get(lpfc_mbox_hdr_status,
5567 &rsrc_info->header.cfg_shdr.response)) {
5568 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5569 "2930 Failed to get resource extents "
5570 "Status 0x%x Add'l Status 0x%x\n",
5571 bf_get(lpfc_mbox_hdr_status,
5572 &rsrc_info->header.cfg_shdr.response),
5573 bf_get(lpfc_mbox_hdr_add_status,
5574 &rsrc_info->header.cfg_shdr.response));
5575 rc = -EIO;
5576 goto err_exit;
5577 }
5578
5579 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5580 &rsrc_info->u.rsp);
5581 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5582 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005583
5584 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5585 "3162 Retrieved extents type-%d from port: count:%d, "
5586 "size:%d\n", type, *extnt_count, *extnt_size);
5587
5588err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005589 mempool_free(mbox, phba->mbox_mem_pool);
5590 return rc;
5591}
5592
5593/**
5594 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5595 * @phba: Pointer to HBA context object.
5596 * @type: The extent type to check.
5597 *
5598 * This function reads the current available extents from the port and checks
5599 * if the extent count or extent size has changed since the last access.
5600 * Callers use this routine post port reset to understand if there is a
5601 * extent reprovisioning requirement.
5602 *
5603 * Returns:
5604 * -Error: error indicates problem.
5605 * 1: Extent count or size has changed.
5606 * 0: No changes.
5607 **/
5608static int
5609lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5610{
5611 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5612 uint16_t size_diff, rsrc_ext_size;
5613 int rc = 0;
5614 struct lpfc_rsrc_blks *rsrc_entry;
5615 struct list_head *rsrc_blk_list = NULL;
5616
5617 size_diff = 0;
5618 curr_ext_cnt = 0;
5619 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5620 &rsrc_ext_cnt,
5621 &rsrc_ext_size);
5622 if (unlikely(rc))
5623 return -EIO;
5624
5625 switch (type) {
5626 case LPFC_RSC_TYPE_FCOE_RPI:
5627 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5628 break;
5629 case LPFC_RSC_TYPE_FCOE_VPI:
5630 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5631 break;
5632 case LPFC_RSC_TYPE_FCOE_XRI:
5633 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5634 break;
5635 case LPFC_RSC_TYPE_FCOE_VFI:
5636 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5637 break;
5638 default:
5639 break;
5640 }
5641
5642 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5643 curr_ext_cnt++;
5644 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5645 size_diff++;
5646 }
5647
5648 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5649 rc = 1;
5650
5651 return rc;
5652}
5653
5654/**
5655 * lpfc_sli4_cfg_post_extnts -
5656 * @phba: Pointer to HBA context object.
5657 * @extnt_cnt - number of available extents.
5658 * @type - the extent type (rpi, xri, vfi, vpi).
5659 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5660 * @mbox - pointer to the caller's allocated mailbox structure.
5661 *
5662 * This function executes the extents allocation request. It also
5663 * takes care of the amount of memory needed to allocate or get the
5664 * allocated extents. It is the caller's responsibility to evaluate
5665 * the response.
5666 *
5667 * Returns:
5668 * -Error: Error value describes the condition found.
5669 * 0: if successful
5670 **/
5671static int
James Smart8a9d2e82012-05-09 21:16:12 -04005672lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005673 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5674{
5675 int rc = 0;
5676 uint32_t req_len;
5677 uint32_t emb_len;
5678 uint32_t alloc_len, mbox_tmo;
5679
5680 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005681 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005682
5683 /*
5684 * Calculate the size of an embedded mailbox. The uint32_t
5685 * accounts for extents-specific word.
5686 */
5687 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5688 sizeof(uint32_t);
5689
5690 /*
5691 * Presume the allocation and response will fit into an embedded
5692 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5693 */
5694 *emb = LPFC_SLI4_MBX_EMBED;
5695 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005696 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005697 sizeof(union lpfc_sli4_cfg_shdr) +
5698 sizeof(uint32_t);
5699 *emb = LPFC_SLI4_MBX_NEMBED;
5700 }
5701
5702 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5703 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5704 req_len, *emb);
5705 if (alloc_len < req_len) {
5706 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005707 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005708 "less than the requested DMA memory "
5709 "size (x%x)\n", alloc_len, req_len);
5710 return -ENOMEM;
5711 }
James Smart8a9d2e82012-05-09 21:16:12 -04005712 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005713 if (unlikely(rc))
5714 return -EIO;
5715
5716 if (!phba->sli4_hba.intr_enable)
5717 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5718 else {
James Smarta183a152011-10-10 21:32:43 -04005719 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005720 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5721 }
5722
5723 if (unlikely(rc))
5724 rc = -EIO;
5725 return rc;
5726}
5727
5728/**
5729 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5730 * @phba: Pointer to HBA context object.
5731 * @type: The resource extent type to allocate.
5732 *
5733 * This function allocates the number of elements for the specified
5734 * resource type.
5735 **/
5736static int
5737lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5738{
5739 bool emb = false;
5740 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5741 uint16_t rsrc_id, rsrc_start, j, k;
5742 uint16_t *ids;
5743 int i, rc;
5744 unsigned long longs;
5745 unsigned long *bmask;
5746 struct lpfc_rsrc_blks *rsrc_blks;
5747 LPFC_MBOXQ_t *mbox;
5748 uint32_t length;
5749 struct lpfc_id_range *id_array = NULL;
5750 void *virtaddr = NULL;
5751 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5752 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5753 struct list_head *ext_blk_list;
5754
5755 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5756 &rsrc_cnt,
5757 &rsrc_size);
5758 if (unlikely(rc))
5759 return -EIO;
5760
5761 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5762 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5763 "3009 No available Resource Extents "
5764 "for resource type 0x%x: Count: 0x%x, "
5765 "Size 0x%x\n", type, rsrc_cnt,
5766 rsrc_size);
5767 return -ENOMEM;
5768 }
5769
James Smart8a9d2e82012-05-09 21:16:12 -04005770 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5771 "2903 Post resource extents type-0x%x: "
5772 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005773
5774 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5775 if (!mbox)
5776 return -ENOMEM;
5777
James Smart8a9d2e82012-05-09 21:16:12 -04005778 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005779 if (unlikely(rc)) {
5780 rc = -EIO;
5781 goto err_exit;
5782 }
5783
5784 /*
5785 * Figure out where the response is located. Then get local pointers
5786 * to the response data. The port does not guarantee to respond to
5787 * all extents counts request so update the local variable with the
5788 * allocated count from the port.
5789 */
5790 if (emb == LPFC_SLI4_MBX_EMBED) {
5791 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5792 id_array = &rsrc_ext->u.rsp.id[0];
5793 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5794 } else {
5795 virtaddr = mbox->sge_array->addr[0];
5796 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5797 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5798 id_array = &n_rsrc->id;
5799 }
5800
5801 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5802 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5803
5804 /*
5805 * Based on the resource size and count, correct the base and max
5806 * resource values.
5807 */
5808 length = sizeof(struct lpfc_rsrc_blks);
5809 switch (type) {
5810 case LPFC_RSC_TYPE_FCOE_RPI:
5811 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5812 sizeof(unsigned long),
5813 GFP_KERNEL);
5814 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5815 rc = -ENOMEM;
5816 goto err_exit;
5817 }
5818 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5819 sizeof(uint16_t),
5820 GFP_KERNEL);
5821 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5822 kfree(phba->sli4_hba.rpi_bmask);
5823 rc = -ENOMEM;
5824 goto err_exit;
5825 }
5826
5827 /*
5828 * The next_rpi was initialized with the maximum available
5829 * count but the port may allocate a smaller number. Catch
5830 * that case and update the next_rpi.
5831 */
5832 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5833
5834 /* Initialize local ptrs for common extent processing later. */
5835 bmask = phba->sli4_hba.rpi_bmask;
5836 ids = phba->sli4_hba.rpi_ids;
5837 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5838 break;
5839 case LPFC_RSC_TYPE_FCOE_VPI:
5840 phba->vpi_bmask = kzalloc(longs *
5841 sizeof(unsigned long),
5842 GFP_KERNEL);
5843 if (unlikely(!phba->vpi_bmask)) {
5844 rc = -ENOMEM;
5845 goto err_exit;
5846 }
5847 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5848 sizeof(uint16_t),
5849 GFP_KERNEL);
5850 if (unlikely(!phba->vpi_ids)) {
5851 kfree(phba->vpi_bmask);
5852 rc = -ENOMEM;
5853 goto err_exit;
5854 }
5855
5856 /* Initialize local ptrs for common extent processing later. */
5857 bmask = phba->vpi_bmask;
5858 ids = phba->vpi_ids;
5859 ext_blk_list = &phba->lpfc_vpi_blk_list;
5860 break;
5861 case LPFC_RSC_TYPE_FCOE_XRI:
5862 phba->sli4_hba.xri_bmask = kzalloc(longs *
5863 sizeof(unsigned long),
5864 GFP_KERNEL);
5865 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5866 rc = -ENOMEM;
5867 goto err_exit;
5868 }
James Smart8a9d2e82012-05-09 21:16:12 -04005869 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005870 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5871 sizeof(uint16_t),
5872 GFP_KERNEL);
5873 if (unlikely(!phba->sli4_hba.xri_ids)) {
5874 kfree(phba->sli4_hba.xri_bmask);
5875 rc = -ENOMEM;
5876 goto err_exit;
5877 }
5878
5879 /* Initialize local ptrs for common extent processing later. */
5880 bmask = phba->sli4_hba.xri_bmask;
5881 ids = phba->sli4_hba.xri_ids;
5882 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5883 break;
5884 case LPFC_RSC_TYPE_FCOE_VFI:
5885 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5886 sizeof(unsigned long),
5887 GFP_KERNEL);
5888 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5889 rc = -ENOMEM;
5890 goto err_exit;
5891 }
5892 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5893 sizeof(uint16_t),
5894 GFP_KERNEL);
5895 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5896 kfree(phba->sli4_hba.vfi_bmask);
5897 rc = -ENOMEM;
5898 goto err_exit;
5899 }
5900
5901 /* Initialize local ptrs for common extent processing later. */
5902 bmask = phba->sli4_hba.vfi_bmask;
5903 ids = phba->sli4_hba.vfi_ids;
5904 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5905 break;
5906 default:
5907 /* Unsupported Opcode. Fail call. */
5908 id_array = NULL;
5909 bmask = NULL;
5910 ids = NULL;
5911 ext_blk_list = NULL;
5912 goto err_exit;
5913 }
5914
5915 /*
5916 * Complete initializing the extent configuration with the
5917 * allocated ids assigned to this function. The bitmask serves
5918 * as an index into the array and manages the available ids. The
5919 * array just stores the ids communicated to the port via the wqes.
5920 */
5921 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5922 if ((i % 2) == 0)
5923 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5924 &id_array[k]);
5925 else
5926 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5927 &id_array[k]);
5928
5929 rsrc_blks = kzalloc(length, GFP_KERNEL);
5930 if (unlikely(!rsrc_blks)) {
5931 rc = -ENOMEM;
5932 kfree(bmask);
5933 kfree(ids);
5934 goto err_exit;
5935 }
5936 rsrc_blks->rsrc_start = rsrc_id;
5937 rsrc_blks->rsrc_size = rsrc_size;
5938 list_add_tail(&rsrc_blks->list, ext_blk_list);
5939 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08005940 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart6d368e52011-05-24 11:44:12 -04005941 phba->sli4_hba.scsi_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08005942 lpfc_sli4_get_iocb_cnt(phba);
5943 phba->sli4_hba.nvme_xri_start =
5944 phba->sli4_hba.scsi_xri_start +
5945 phba->sli4_hba.scsi_xri_max;
5946 }
James Smart6d368e52011-05-24 11:44:12 -04005947
5948 while (rsrc_id < (rsrc_start + rsrc_size)) {
5949 ids[j] = rsrc_id;
5950 rsrc_id++;
5951 j++;
5952 }
5953 /* Entire word processed. Get next word.*/
5954 if ((i % 2) == 1)
5955 k++;
5956 }
5957 err_exit:
5958 lpfc_sli4_mbox_cmd_free(phba, mbox);
5959 return rc;
5960}
5961
James Smart895427b2017-02-12 13:52:30 -08005962
5963
James Smart6d368e52011-05-24 11:44:12 -04005964/**
5965 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5966 * @phba: Pointer to HBA context object.
5967 * @type: the extent's type.
5968 *
5969 * This function deallocates all extents of a particular resource type.
5970 * SLI4 does not allow for deallocating a particular extent range. It
5971 * is the caller's responsibility to release all kernel memory resources.
5972 **/
5973static int
5974lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5975{
5976 int rc;
5977 uint32_t length, mbox_tmo = 0;
5978 LPFC_MBOXQ_t *mbox;
5979 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5980 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5981
5982 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5983 if (!mbox)
5984 return -ENOMEM;
5985
5986 /*
5987 * This function sends an embedded mailbox because it only sends the
5988 * the resource type. All extents of this type are released by the
5989 * port.
5990 */
5991 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5992 sizeof(struct lpfc_sli4_cfg_mhdr));
5993 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5994 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5995 length, LPFC_SLI4_MBX_EMBED);
5996
5997 /* Send an extents count of 0 - the dealloc doesn't use it. */
5998 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5999 LPFC_SLI4_MBX_EMBED);
6000 if (unlikely(rc)) {
6001 rc = -EIO;
6002 goto out_free_mbox;
6003 }
6004 if (!phba->sli4_hba.intr_enable)
6005 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6006 else {
James Smarta183a152011-10-10 21:32:43 -04006007 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006008 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6009 }
6010 if (unlikely(rc)) {
6011 rc = -EIO;
6012 goto out_free_mbox;
6013 }
6014
6015 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6016 if (bf_get(lpfc_mbox_hdr_status,
6017 &dealloc_rsrc->header.cfg_shdr.response)) {
6018 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6019 "2919 Failed to release resource extents "
6020 "for type %d - Status 0x%x Add'l Status 0x%x. "
6021 "Resource memory not released.\n",
6022 type,
6023 bf_get(lpfc_mbox_hdr_status,
6024 &dealloc_rsrc->header.cfg_shdr.response),
6025 bf_get(lpfc_mbox_hdr_add_status,
6026 &dealloc_rsrc->header.cfg_shdr.response));
6027 rc = -EIO;
6028 goto out_free_mbox;
6029 }
6030
6031 /* Release kernel memory resources for the specific type. */
6032 switch (type) {
6033 case LPFC_RSC_TYPE_FCOE_VPI:
6034 kfree(phba->vpi_bmask);
6035 kfree(phba->vpi_ids);
6036 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6037 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6038 &phba->lpfc_vpi_blk_list, list) {
6039 list_del_init(&rsrc_blk->list);
6040 kfree(rsrc_blk);
6041 }
James Smart16a3a202013-04-17 20:14:38 -04006042 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006043 break;
6044 case LPFC_RSC_TYPE_FCOE_XRI:
6045 kfree(phba->sli4_hba.xri_bmask);
6046 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006047 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6048 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6049 list_del_init(&rsrc_blk->list);
6050 kfree(rsrc_blk);
6051 }
6052 break;
6053 case LPFC_RSC_TYPE_FCOE_VFI:
6054 kfree(phba->sli4_hba.vfi_bmask);
6055 kfree(phba->sli4_hba.vfi_ids);
6056 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6057 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6058 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6059 list_del_init(&rsrc_blk->list);
6060 kfree(rsrc_blk);
6061 }
6062 break;
6063 case LPFC_RSC_TYPE_FCOE_RPI:
6064 /* RPI bitmask and physical id array are cleaned up earlier. */
6065 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6066 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6067 list_del_init(&rsrc_blk->list);
6068 kfree(rsrc_blk);
6069 }
6070 break;
6071 default:
6072 break;
6073 }
6074
6075 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6076
6077 out_free_mbox:
6078 mempool_free(mbox, phba->mbox_mem_pool);
6079 return rc;
6080}
6081
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006082static void
James Smart7bdedb32016-07-06 12:36:00 -07006083lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6084 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006085{
James Smart65791f12016-07-06 12:35:56 -07006086 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07006087
James Smart65791f12016-07-06 12:35:56 -07006088 len = sizeof(struct lpfc_mbx_set_feature) -
6089 sizeof(struct lpfc_sli4_cfg_mhdr);
6090 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6091 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6092 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006093
James Smart7bdedb32016-07-06 12:36:00 -07006094 switch (feature) {
6095 case LPFC_SET_UE_RECOVERY:
6096 bf_set(lpfc_mbx_set_feature_UER,
6097 &mbox->u.mqe.un.set_feature, 1);
6098 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6099 mbox->u.mqe.un.set_feature.param_len = 8;
6100 break;
6101 case LPFC_SET_MDS_DIAGS:
6102 bf_set(lpfc_mbx_set_feature_mds,
6103 &mbox->u.mqe.un.set_feature, 1);
6104 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006105 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006106 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6107 mbox->u.mqe.un.set_feature.param_len = 8;
6108 break;
James Smart65791f12016-07-06 12:35:56 -07006109 }
James Smart7bdedb32016-07-06 12:36:00 -07006110
6111 return;
James Smart65791f12016-07-06 12:35:56 -07006112}
6113
James Smart6d368e52011-05-24 11:44:12 -04006114/**
6115 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6116 * @phba: Pointer to HBA context object.
6117 *
6118 * This function allocates all SLI4 resource identifiers.
6119 **/
6120int
6121lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6122{
6123 int i, rc, error = 0;
6124 uint16_t count, base;
6125 unsigned long longs;
6126
James Smartff78d8f2011-12-13 13:21:35 -05006127 if (!phba->sli4_hba.rpi_hdrs_in_use)
6128 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04006129 if (phba->sli4_hba.extents_in_use) {
6130 /*
6131 * The port supports resource extents. The XRI, VPI, VFI, RPI
6132 * resource extent count must be read and allocated before
6133 * provisioning the resource id arrays.
6134 */
6135 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6136 LPFC_IDX_RSRC_RDY) {
6137 /*
6138 * Extent-based resources are set - the driver could
6139 * be in a port reset. Figure out if any corrective
6140 * actions need to be taken.
6141 */
6142 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6143 LPFC_RSC_TYPE_FCOE_VFI);
6144 if (rc != 0)
6145 error++;
6146 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6147 LPFC_RSC_TYPE_FCOE_VPI);
6148 if (rc != 0)
6149 error++;
6150 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6151 LPFC_RSC_TYPE_FCOE_XRI);
6152 if (rc != 0)
6153 error++;
6154 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6155 LPFC_RSC_TYPE_FCOE_RPI);
6156 if (rc != 0)
6157 error++;
6158
6159 /*
6160 * It's possible that the number of resources
6161 * provided to this port instance changed between
6162 * resets. Detect this condition and reallocate
6163 * resources. Otherwise, there is no action.
6164 */
6165 if (error) {
6166 lpfc_printf_log(phba, KERN_INFO,
6167 LOG_MBOX | LOG_INIT,
6168 "2931 Detected extent resource "
6169 "change. Reallocating all "
6170 "extents.\n");
6171 rc = lpfc_sli4_dealloc_extent(phba,
6172 LPFC_RSC_TYPE_FCOE_VFI);
6173 rc = lpfc_sli4_dealloc_extent(phba,
6174 LPFC_RSC_TYPE_FCOE_VPI);
6175 rc = lpfc_sli4_dealloc_extent(phba,
6176 LPFC_RSC_TYPE_FCOE_XRI);
6177 rc = lpfc_sli4_dealloc_extent(phba,
6178 LPFC_RSC_TYPE_FCOE_RPI);
6179 } else
6180 return 0;
6181 }
6182
6183 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6184 if (unlikely(rc))
6185 goto err_exit;
6186
6187 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6188 if (unlikely(rc))
6189 goto err_exit;
6190
6191 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6192 if (unlikely(rc))
6193 goto err_exit;
6194
6195 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6196 if (unlikely(rc))
6197 goto err_exit;
6198 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6199 LPFC_IDX_RSRC_RDY);
6200 return rc;
6201 } else {
6202 /*
6203 * The port does not support resource extents. The XRI, VPI,
6204 * VFI, RPI resource ids were determined from READ_CONFIG.
6205 * Just allocate the bitmasks and provision the resource id
6206 * arrays. If a port reset is active, the resources don't
6207 * need any action - just exit.
6208 */
6209 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006210 LPFC_IDX_RSRC_RDY) {
6211 lpfc_sli4_dealloc_resource_identifiers(phba);
6212 lpfc_sli4_remove_rpis(phba);
6213 }
James Smart6d368e52011-05-24 11:44:12 -04006214 /* RPIs. */
6215 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006216 if (count <= 0) {
6217 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6218 "3279 Invalid provisioning of "
6219 "rpi:%d\n", count);
6220 rc = -EINVAL;
6221 goto err_exit;
6222 }
James Smart6d368e52011-05-24 11:44:12 -04006223 base = phba->sli4_hba.max_cfg_param.rpi_base;
6224 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6225 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6226 sizeof(unsigned long),
6227 GFP_KERNEL);
6228 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6229 rc = -ENOMEM;
6230 goto err_exit;
6231 }
6232 phba->sli4_hba.rpi_ids = kzalloc(count *
6233 sizeof(uint16_t),
6234 GFP_KERNEL);
6235 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6236 rc = -ENOMEM;
6237 goto free_rpi_bmask;
6238 }
6239
6240 for (i = 0; i < count; i++)
6241 phba->sli4_hba.rpi_ids[i] = base + i;
6242
6243 /* VPIs. */
6244 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006245 if (count <= 0) {
6246 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6247 "3280 Invalid provisioning of "
6248 "vpi:%d\n", count);
6249 rc = -EINVAL;
6250 goto free_rpi_ids;
6251 }
James Smart6d368e52011-05-24 11:44:12 -04006252 base = phba->sli4_hba.max_cfg_param.vpi_base;
6253 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6254 phba->vpi_bmask = kzalloc(longs *
6255 sizeof(unsigned long),
6256 GFP_KERNEL);
6257 if (unlikely(!phba->vpi_bmask)) {
6258 rc = -ENOMEM;
6259 goto free_rpi_ids;
6260 }
6261 phba->vpi_ids = kzalloc(count *
6262 sizeof(uint16_t),
6263 GFP_KERNEL);
6264 if (unlikely(!phba->vpi_ids)) {
6265 rc = -ENOMEM;
6266 goto free_vpi_bmask;
6267 }
6268
6269 for (i = 0; i < count; i++)
6270 phba->vpi_ids[i] = base + i;
6271
6272 /* XRIs. */
6273 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006274 if (count <= 0) {
6275 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6276 "3281 Invalid provisioning of "
6277 "xri:%d\n", count);
6278 rc = -EINVAL;
6279 goto free_vpi_ids;
6280 }
James Smart6d368e52011-05-24 11:44:12 -04006281 base = phba->sli4_hba.max_cfg_param.xri_base;
6282 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6283 phba->sli4_hba.xri_bmask = kzalloc(longs *
6284 sizeof(unsigned long),
6285 GFP_KERNEL);
6286 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6287 rc = -ENOMEM;
6288 goto free_vpi_ids;
6289 }
James Smart41899be2012-03-01 22:34:19 -05006290 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006291 phba->sli4_hba.xri_ids = kzalloc(count *
6292 sizeof(uint16_t),
6293 GFP_KERNEL);
6294 if (unlikely(!phba->sli4_hba.xri_ids)) {
6295 rc = -ENOMEM;
6296 goto free_xri_bmask;
6297 }
6298
6299 for (i = 0; i < count; i++)
6300 phba->sli4_hba.xri_ids[i] = base + i;
6301
6302 /* VFIs. */
6303 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006304 if (count <= 0) {
6305 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6306 "3282 Invalid provisioning of "
6307 "vfi:%d\n", count);
6308 rc = -EINVAL;
6309 goto free_xri_ids;
6310 }
James Smart6d368e52011-05-24 11:44:12 -04006311 base = phba->sli4_hba.max_cfg_param.vfi_base;
6312 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6313 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6314 sizeof(unsigned long),
6315 GFP_KERNEL);
6316 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6317 rc = -ENOMEM;
6318 goto free_xri_ids;
6319 }
6320 phba->sli4_hba.vfi_ids = kzalloc(count *
6321 sizeof(uint16_t),
6322 GFP_KERNEL);
6323 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6324 rc = -ENOMEM;
6325 goto free_vfi_bmask;
6326 }
6327
6328 for (i = 0; i < count; i++)
6329 phba->sli4_hba.vfi_ids[i] = base + i;
6330
6331 /*
6332 * Mark all resources ready. An HBA reset doesn't need
6333 * to reset the initialization.
6334 */
6335 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6336 LPFC_IDX_RSRC_RDY);
6337 return 0;
6338 }
6339
6340 free_vfi_bmask:
6341 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006342 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006343 free_xri_ids:
6344 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006345 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006346 free_xri_bmask:
6347 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006348 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006349 free_vpi_ids:
6350 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006351 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006352 free_vpi_bmask:
6353 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006354 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006355 free_rpi_ids:
6356 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006357 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006358 free_rpi_bmask:
6359 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006360 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006361 err_exit:
6362 return rc;
6363}
6364
6365/**
6366 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6367 * @phba: Pointer to HBA context object.
6368 *
6369 * This function allocates the number of elements for the specified
6370 * resource type.
6371 **/
6372int
6373lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6374{
6375 if (phba->sli4_hba.extents_in_use) {
6376 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6377 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6378 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6379 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6380 } else {
6381 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006382 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006383 kfree(phba->vpi_ids);
6384 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6385 kfree(phba->sli4_hba.xri_bmask);
6386 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006387 kfree(phba->sli4_hba.vfi_bmask);
6388 kfree(phba->sli4_hba.vfi_ids);
6389 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6390 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6391 }
6392
6393 return 0;
6394}
6395
6396/**
James Smartb76f2dc2011-07-22 18:37:42 -04006397 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6398 * @phba: Pointer to HBA context object.
6399 * @type: The resource extent type.
6400 * @extnt_count: buffer to hold port extent count response
6401 * @extnt_size: buffer to hold port extent size response.
6402 *
6403 * This function calls the port to read the host allocated extents
6404 * for a particular type.
6405 **/
6406int
6407lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6408 uint16_t *extnt_cnt, uint16_t *extnt_size)
6409{
6410 bool emb;
6411 int rc = 0;
6412 uint16_t curr_blks = 0;
6413 uint32_t req_len, emb_len;
6414 uint32_t alloc_len, mbox_tmo;
6415 struct list_head *blk_list_head;
6416 struct lpfc_rsrc_blks *rsrc_blk;
6417 LPFC_MBOXQ_t *mbox;
6418 void *virtaddr = NULL;
6419 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6420 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6421 union lpfc_sli4_cfg_shdr *shdr;
6422
6423 switch (type) {
6424 case LPFC_RSC_TYPE_FCOE_VPI:
6425 blk_list_head = &phba->lpfc_vpi_blk_list;
6426 break;
6427 case LPFC_RSC_TYPE_FCOE_XRI:
6428 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6429 break;
6430 case LPFC_RSC_TYPE_FCOE_VFI:
6431 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6432 break;
6433 case LPFC_RSC_TYPE_FCOE_RPI:
6434 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6435 break;
6436 default:
6437 return -EIO;
6438 }
6439
6440 /* Count the number of extents currently allocatd for this type. */
6441 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6442 if (curr_blks == 0) {
6443 /*
6444 * The GET_ALLOCATED mailbox does not return the size,
6445 * just the count. The size should be just the size
6446 * stored in the current allocated block and all sizes
6447 * for an extent type are the same so set the return
6448 * value now.
6449 */
6450 *extnt_size = rsrc_blk->rsrc_size;
6451 }
6452 curr_blks++;
6453 }
6454
James Smartb76f2dc2011-07-22 18:37:42 -04006455 /*
6456 * Calculate the size of an embedded mailbox. The uint32_t
6457 * accounts for extents-specific word.
6458 */
6459 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6460 sizeof(uint32_t);
6461
6462 /*
6463 * Presume the allocation and response will fit into an embedded
6464 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6465 */
6466 emb = LPFC_SLI4_MBX_EMBED;
6467 req_len = emb_len;
6468 if (req_len > emb_len) {
6469 req_len = curr_blks * sizeof(uint16_t) +
6470 sizeof(union lpfc_sli4_cfg_shdr) +
6471 sizeof(uint32_t);
6472 emb = LPFC_SLI4_MBX_NEMBED;
6473 }
6474
6475 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6476 if (!mbox)
6477 return -ENOMEM;
6478 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6479
6480 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6481 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6482 req_len, emb);
6483 if (alloc_len < req_len) {
6484 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6485 "2983 Allocated DMA memory size (x%x) is "
6486 "less than the requested DMA memory "
6487 "size (x%x)\n", alloc_len, req_len);
6488 rc = -ENOMEM;
6489 goto err_exit;
6490 }
6491 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6492 if (unlikely(rc)) {
6493 rc = -EIO;
6494 goto err_exit;
6495 }
6496
6497 if (!phba->sli4_hba.intr_enable)
6498 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6499 else {
James Smarta183a152011-10-10 21:32:43 -04006500 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006501 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6502 }
6503
6504 if (unlikely(rc)) {
6505 rc = -EIO;
6506 goto err_exit;
6507 }
6508
6509 /*
6510 * Figure out where the response is located. Then get local pointers
6511 * to the response data. The port does not guarantee to respond to
6512 * all extents counts request so update the local variable with the
6513 * allocated count from the port.
6514 */
6515 if (emb == LPFC_SLI4_MBX_EMBED) {
6516 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6517 shdr = &rsrc_ext->header.cfg_shdr;
6518 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6519 } else {
6520 virtaddr = mbox->sge_array->addr[0];
6521 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6522 shdr = &n_rsrc->cfg_shdr;
6523 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6524 }
6525
6526 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6527 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6528 "2984 Failed to read allocated resources "
6529 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6530 type,
6531 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6532 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6533 rc = -EIO;
6534 goto err_exit;
6535 }
6536 err_exit:
6537 lpfc_sli4_mbox_cmd_free(phba, mbox);
6538 return rc;
6539}
6540
6541/**
James Smart0ef69962017-04-21 16:04:50 -07006542 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006543 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006544 * @pring: Pointer to driver SLI ring object.
6545 * @sgl_list: linked link of sgl buffers to post
6546 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006547 *
James Smart895427b2017-02-12 13:52:30 -08006548 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006549 * repost them to the port by using SGL block post. This is needed after a
6550 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006551 * of buffer sgls which contains contiguous xris and uses the non-embedded
6552 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006553 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6554 * mailbox command for posting.
6555 *
6556 * Returns: 0 = success, non-zero failure.
6557 **/
6558static int
James Smart895427b2017-02-12 13:52:30 -08006559lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6560 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006561{
6562 struct lpfc_sglq *sglq_entry = NULL;
6563 struct lpfc_sglq *sglq_entry_next = NULL;
6564 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006565 int status, total_cnt;
6566 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006567 int last_xritag = NO_XRI;
6568 LIST_HEAD(prep_sgl_list);
6569 LIST_HEAD(blck_sgl_list);
6570 LIST_HEAD(allc_sgl_list);
6571 LIST_HEAD(post_sgl_list);
6572 LIST_HEAD(free_sgl_list);
6573
James Smart38c20672013-03-01 16:37:44 -05006574 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006575 spin_lock(&phba->sli4_hba.sgl_list_lock);
6576 list_splice_init(sgl_list, &allc_sgl_list);
6577 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006578 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006579
James Smart895427b2017-02-12 13:52:30 -08006580 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006581 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6582 &allc_sgl_list, list) {
6583 list_del_init(&sglq_entry->list);
6584 block_cnt++;
6585 if ((last_xritag != NO_XRI) &&
6586 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6587 /* a hole in xri block, form a sgl posting block */
6588 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6589 post_cnt = block_cnt - 1;
6590 /* prepare list for next posting block */
6591 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6592 block_cnt = 1;
6593 } else {
6594 /* prepare list for next posting block */
6595 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6596 /* enough sgls for non-embed sgl mbox command */
6597 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6598 list_splice_init(&prep_sgl_list,
6599 &blck_sgl_list);
6600 post_cnt = block_cnt;
6601 block_cnt = 0;
6602 }
6603 }
6604 num_posted++;
6605
6606 /* keep track of last sgl's xritag */
6607 last_xritag = sglq_entry->sli4_xritag;
6608
James Smart895427b2017-02-12 13:52:30 -08006609 /* end of repost sgl list condition for buffers */
6610 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006611 if (post_cnt == 0) {
6612 list_splice_init(&prep_sgl_list,
6613 &blck_sgl_list);
6614 post_cnt = block_cnt;
6615 } else if (block_cnt == 1) {
6616 status = lpfc_sli4_post_sgl(phba,
6617 sglq_entry->phys, 0,
6618 sglq_entry->sli4_xritag);
6619 if (!status) {
6620 /* successful, put sgl to posted list */
6621 list_add_tail(&sglq_entry->list,
6622 &post_sgl_list);
6623 } else {
6624 /* Failure, put sgl to free list */
6625 lpfc_printf_log(phba, KERN_WARNING,
6626 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006627 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006628 "sgl, xritag:x%x\n",
6629 sglq_entry->sli4_xritag);
6630 list_add_tail(&sglq_entry->list,
6631 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006632 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006633 }
6634 }
6635 }
6636
6637 /* continue until a nembed page worth of sgls */
6638 if (post_cnt == 0)
6639 continue;
6640
James Smart895427b2017-02-12 13:52:30 -08006641 /* post the buffer list sgls as a block */
6642 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6643 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006644
6645 if (!status) {
6646 /* success, put sgl list to posted sgl list */
6647 list_splice_init(&blck_sgl_list, &post_sgl_list);
6648 } else {
6649 /* Failure, put sgl list to free sgl list */
6650 sglq_entry_first = list_first_entry(&blck_sgl_list,
6651 struct lpfc_sglq,
6652 list);
6653 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006654 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04006655 "xritag:x%x-x%x\n",
6656 sglq_entry_first->sli4_xritag,
6657 (sglq_entry_first->sli4_xritag +
6658 post_cnt - 1));
6659 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006660 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006661 }
6662
6663 /* don't reset xirtag due to hole in xri block */
6664 if (block_cnt == 0)
6665 last_xritag = NO_XRI;
6666
James Smart895427b2017-02-12 13:52:30 -08006667 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04006668 post_cnt = 0;
6669 }
6670
James Smart895427b2017-02-12 13:52:30 -08006671 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04006672 lpfc_free_sgl_list(phba, &free_sgl_list);
6673
James Smart895427b2017-02-12 13:52:30 -08006674 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04006675 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006676 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006677 spin_lock(&phba->sli4_hba.sgl_list_lock);
6678 list_splice_init(&post_sgl_list, sgl_list);
6679 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006680 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006681 } else {
6682 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006683 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006684 return -EIO;
6685 }
James Smart895427b2017-02-12 13:52:30 -08006686
6687 /* return the number of XRIs actually posted */
6688 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006689}
6690
James Smart61bda8f2016-10-13 15:06:05 -07006691void
6692lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6693{
6694 uint32_t len;
6695
6696 len = sizeof(struct lpfc_mbx_set_host_data) -
6697 sizeof(struct lpfc_sli4_cfg_mhdr);
6698 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6699 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6700 LPFC_SLI4_MBX_EMBED);
6701
6702 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08006703 mbox->u.mqe.un.set_host_data.param_len =
6704 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07006705 snprintf(mbox->u.mqe.un.set_host_data.data,
6706 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6707 "Linux %s v"LPFC_DRIVER_VERSION,
6708 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6709}
6710
James Smarta8cf5df2017-05-15 15:20:46 -07006711int
James Smart6c621a22017-05-15 15:20:45 -07006712lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07006713 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07006714{
6715 int rc, i;
6716 struct lpfc_rqe hrqe;
6717 struct lpfc_rqe drqe;
6718 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08006719 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07006720 struct rqb_dmabuf *rqb_buffer;
6721 LIST_HEAD(rqb_buf_list);
6722
James Smart411de512018-01-30 15:58:52 -08006723 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07006724 rqbp = hrq->rqbp;
6725 for (i = 0; i < count; i++) {
6726 /* IF RQ is already full, don't bother */
6727 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6728 break;
6729 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6730 if (!rqb_buffer)
6731 break;
6732 rqb_buffer->hrq = hrq;
6733 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07006734 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07006735 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6736 }
6737 while (!list_empty(&rqb_buf_list)) {
6738 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6739 hbuf.list);
6740
6741 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6742 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6743 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6744 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6745 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6746 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08006747 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6748 "6421 Cannot post to HRQ %d: %x %x %x "
6749 "DRQ %x %x\n",
6750 hrq->queue_id,
6751 hrq->host_index,
6752 hrq->hba_index,
6753 hrq->entry_count,
6754 drq->host_index,
6755 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07006756 rqbp->rqb_free_buffer(phba, rqb_buffer);
6757 } else {
6758 list_add_tail(&rqb_buffer->hbuf.list,
6759 &rqbp->rqb_buffer_list);
6760 rqbp->buffer_count++;
6761 }
6762 }
James Smart411de512018-01-30 15:58:52 -08006763 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07006764 return 1;
6765}
6766
James Smart8a9d2e82012-05-09 21:16:12 -04006767/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08006768 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04006769 * @phba: Pointer to HBA context object.
6770 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08006771 * This function is the main SLI4 device initialization PCI function. This
6772 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04006773 * HBA error attention handler code. Caller is not required to hold any
6774 * locks.
6775 **/
6776int
6777lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6778{
James Smart6c621a22017-05-15 15:20:45 -07006779 int rc, i, cnt;
James Smartda0436e2009-05-22 14:51:39 -04006780 LPFC_MBOXQ_t *mboxq;
6781 struct lpfc_mqe *mqe;
6782 uint8_t *vpd;
6783 uint32_t vpd_size;
6784 uint32_t ftr_rsp = 0;
6785 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6786 struct lpfc_vport *vport = phba->pport;
6787 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08006788 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04006789
6790 /* Perform a PCI function reset to start from clean */
6791 rc = lpfc_pci_function_reset(phba);
6792 if (unlikely(rc))
6793 return -ENODEV;
6794
6795 /* Check the HBA Host Status Register for readyness */
6796 rc = lpfc_sli4_post_status_check(phba);
6797 if (unlikely(rc))
6798 return -ENODEV;
6799 else {
6800 spin_lock_irq(&phba->hbalock);
6801 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6802 spin_unlock_irq(&phba->hbalock);
6803 }
6804
6805 /*
6806 * Allocate a single mailbox container for initializing the
6807 * port.
6808 */
6809 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6810 if (!mboxq)
6811 return -ENOMEM;
6812
James Smartda0436e2009-05-22 14:51:39 -04006813 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006814 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006815 vpd = kzalloc(vpd_size, GFP_KERNEL);
6816 if (!vpd) {
6817 rc = -ENOMEM;
6818 goto out_free_mbox;
6819 }
6820
6821 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006822 if (unlikely(rc)) {
6823 kfree(vpd);
6824 goto out_free_mbox;
6825 }
James Smart572709e2013-07-15 18:32:43 -04006826
James Smartda0436e2009-05-22 14:51:39 -04006827 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006828 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006829 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006830 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006831 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6832 } else {
James Smart76a95d72010-11-20 23:11:48 -05006833 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006834 }
James Smart45ed1192009-10-02 15:17:02 -04006835
6836 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6837 LPFC_DCBX_CEE_MODE)
6838 phba->hba_flag |= HBA_FIP_SUPPORT;
6839 else
6840 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6841
James Smart4f2e66c2012-05-09 21:17:07 -04006842 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6843
James Smartc31098c2011-04-16 11:03:33 -04006844 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006845 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6846 "0376 READ_REV Error. SLI Level %d "
6847 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006848 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006849 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006850 kfree(vpd);
6851 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006852 }
James Smartcd1c8302011-10-10 21:33:25 -04006853
6854 /*
James Smartff78d8f2011-12-13 13:21:35 -05006855 * Continue initialization with default values even if driver failed
6856 * to read FCoE param config regions, only read parameters if the
6857 * board is FCoE
6858 */
6859 if (phba->hba_flag & HBA_FCOE_MODE &&
6860 lpfc_sli4_read_fcoe_params(phba))
6861 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6862 "2570 Failed to read FCoE parameters\n");
6863
6864 /*
James Smartcd1c8302011-10-10 21:33:25 -04006865 * Retrieve sli4 device physical port name, failure of doing it
6866 * is considered as non-fatal.
6867 */
6868 rc = lpfc_sli4_retrieve_pport_name(phba);
6869 if (!rc)
6870 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6871 "3080 Successful retrieving SLI4 device "
6872 "physical port name: %s.\n", phba->Port);
6873
James Smartda0436e2009-05-22 14:51:39 -04006874 /*
6875 * Evaluate the read rev and vpd data. Populate the driver
6876 * state with the results. If this routine fails, the failure
6877 * is not fatal as the driver will use generic values.
6878 */
6879 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6880 if (unlikely(!rc)) {
6881 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6882 "0377 Error %d parsing vpd. "
6883 "Using defaults.\n", rc);
6884 rc = 0;
6885 }
James Smart76a95d72010-11-20 23:11:48 -05006886 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006887
James Smartf1126682009-06-10 17:22:44 -04006888 /* Save information as VPD data */
6889 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6890 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
James Smart4e565cf2018-02-22 08:18:50 -08006891
6892 /*
6893 * This is because first G7 ASIC doesn't support the standard
6894 * 0x5a NVME cmd descriptor type/subtype
6895 */
6896 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6897 LPFC_SLI_INTF_IF_TYPE_6) &&
6898 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
6899 (phba->vpd.rev.smRev == 0) &&
6900 (phba->cfg_nvme_embed_cmd == 1))
6901 phba->cfg_nvme_embed_cmd = 0;
6902
James Smartf1126682009-06-10 17:22:44 -04006903 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6904 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6905 &mqe->un.read_rev);
6906 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6907 &mqe->un.read_rev);
6908 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6909 &mqe->un.read_rev);
6910 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6911 &mqe->un.read_rev);
6912 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6913 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6914 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6915 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6916 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6917 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6918 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6919 "(%d):0380 READ_REV Status x%x "
6920 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6921 mboxq->vport ? mboxq->vport->vpi : 0,
6922 bf_get(lpfc_mqe_status, mqe),
6923 phba->vpd.rev.opFwName,
6924 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6925 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006926
James Smart572709e2013-07-15 18:32:43 -04006927 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6928 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6929 if (phba->pport->cfg_lun_queue_depth > rc) {
6930 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6931 "3362 LUN queue depth changed from %d to %d\n",
6932 phba->pport->cfg_lun_queue_depth, rc);
6933 phba->pport->cfg_lun_queue_depth = rc;
6934 }
6935
James Smart65791f12016-07-06 12:35:56 -07006936 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006937 LPFC_SLI_INTF_IF_TYPE_0) {
6938 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6939 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6940 if (rc == MBX_SUCCESS) {
6941 phba->hba_flag |= HBA_RECOVERABLE_UE;
6942 /* Set 1Sec interval to detect UE */
6943 phba->eratt_poll_interval = 1;
6944 phba->sli4_hba.ue_to_sr = bf_get(
6945 lpfc_mbx_set_feature_UESR,
6946 &mboxq->u.mqe.un.set_feature);
6947 phba->sli4_hba.ue_to_rp = bf_get(
6948 lpfc_mbx_set_feature_UERP,
6949 &mboxq->u.mqe.un.set_feature);
6950 }
6951 }
6952
6953 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6954 /* Enable MDS Diagnostics only if the SLI Port supports it */
6955 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6956 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6957 if (rc != MBX_SUCCESS)
6958 phba->mds_diags_support = 0;
6959 }
James Smart572709e2013-07-15 18:32:43 -04006960
James Smartda0436e2009-05-22 14:51:39 -04006961 /*
6962 * Discover the port's supported feature set and match it against the
6963 * hosts requests.
6964 */
6965 lpfc_request_features(phba, mboxq);
6966 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6967 if (unlikely(rc)) {
6968 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006969 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006970 }
6971
6972 /*
6973 * The port must support FCP initiator mode as this is the
6974 * only mode running in the host.
6975 */
6976 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6977 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6978 "0378 No support for fcpi mode.\n");
6979 ftr_rsp++;
6980 }
James Smart0bc2b7c2018-02-22 08:18:48 -08006981
6982 /* Performance Hints are ONLY for FCoE */
6983 if (phba->hba_flag & HBA_FCOE_MODE) {
6984 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6985 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6986 else
6987 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
6988 }
6989
James Smartda0436e2009-05-22 14:51:39 -04006990 /*
6991 * If the port cannot support the host's requested features
6992 * then turn off the global config parameters to disable the
6993 * feature in the driver. This is not a fatal error.
6994 */
James Smartf44ac122018-03-05 12:04:08 -08006995 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
6996 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
6997 phba->cfg_enable_bg = 0;
6998 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smartbf086112011-08-21 21:48:13 -04006999 ftr_rsp++;
James Smartf44ac122018-03-05 12:04:08 -08007000 }
James Smartbf086112011-08-21 21:48:13 -04007001 }
James Smartda0436e2009-05-22 14:51:39 -04007002
7003 if (phba->max_vpi && phba->cfg_enable_npiv &&
7004 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7005 ftr_rsp++;
7006
7007 if (ftr_rsp) {
7008 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7009 "0379 Feature Mismatch Data: x%08x %08x "
7010 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7011 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7012 phba->cfg_enable_npiv, phba->max_vpi);
7013 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7014 phba->cfg_enable_bg = 0;
7015 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7016 phba->cfg_enable_npiv = 0;
7017 }
7018
7019 /* These SLI3 features are assumed in SLI4 */
7020 spin_lock_irq(&phba->hbalock);
7021 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7022 spin_unlock_irq(&phba->hbalock);
7023
James Smart6d368e52011-05-24 11:44:12 -04007024 /*
7025 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
7026 * calls depends on these resources to complete port setup.
7027 */
7028 rc = lpfc_sli4_alloc_resource_identifiers(phba);
7029 if (rc) {
7030 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7031 "2920 Failed to alloc Resource IDs "
7032 "rc = x%x\n", rc);
7033 goto out_free_mbox;
7034 }
7035
James Smart61bda8f2016-10-13 15:06:05 -07007036 lpfc_set_host_data(phba, mboxq);
7037
7038 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7039 if (rc) {
7040 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7041 "2134 Failed to set host os driver version %x",
7042 rc);
7043 }
7044
James Smartda0436e2009-05-22 14:51:39 -04007045 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05007046 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7047 if (rc) {
7048 phba->link_state = LPFC_HBA_ERROR;
7049 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05007050 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05007051 }
7052
James Smartda0436e2009-05-22 14:51:39 -04007053 mboxq->vport = vport;
7054 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7055 mp = (struct lpfc_dmabuf *) mboxq->context1;
7056 if (rc == MBX_SUCCESS) {
7057 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7058 rc = 0;
7059 }
7060
7061 /*
7062 * This memory was allocated by the lpfc_read_sparam routine. Release
7063 * it to the mbuf pool.
7064 */
7065 lpfc_mbuf_free(phba, mp->virt, mp->phys);
7066 kfree(mp);
7067 mboxq->context1 = NULL;
7068 if (unlikely(rc)) {
7069 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7070 "0382 READ_SPARAM command failed "
7071 "status %d, mbxStatus x%x\n",
7072 rc, bf_get(lpfc_mqe_status, mqe));
7073 phba->link_state = LPFC_HBA_ERROR;
7074 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007075 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007076 }
7077
James Smart05580562011-05-24 11:40:48 -04007078 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04007079
7080 /* Update the fc_host data structures with new wwn. */
7081 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7082 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7083
James Smart895427b2017-02-12 13:52:30 -08007084 /* Create all the SLI4 queues */
7085 rc = lpfc_sli4_queue_create(phba);
7086 if (rc) {
7087 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7088 "3089 Failed to allocate queues\n");
7089 rc = -ENODEV;
7090 goto out_free_mbox;
7091 }
7092 /* Set up all the queues to the device */
7093 rc = lpfc_sli4_queue_setup(phba);
7094 if (unlikely(rc)) {
7095 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7096 "0381 Error %d during queue setup.\n ", rc);
7097 goto out_stop_timers;
7098 }
7099 /* Initialize the driver internal SLI layer lists. */
7100 lpfc_sli4_setup(phba);
7101 lpfc_sli4_queue_init(phba);
7102
7103 /* update host els xri-sgl sizes and mappings */
7104 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007105 if (unlikely(rc)) {
7106 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7107 "1400 Failed to update xri-sgl size and "
7108 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08007109 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007110 }
7111
James Smart8a9d2e82012-05-09 21:16:12 -04007112 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08007113 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7114 phba->sli4_hba.els_xri_cnt);
7115 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04007116 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7117 "0582 Error %d during els sgl post "
7118 "operation\n", rc);
7119 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007120 goto out_destroy_queue;
7121 }
7122 phba->sli4_hba.els_xri_cnt = rc;
7123
James Smartf358dd02017-02-12 13:52:34 -08007124 if (phba->nvmet_support) {
7125 /* update host nvmet xri-sgl sizes and mappings */
7126 rc = lpfc_sli4_nvmet_sgl_update(phba);
7127 if (unlikely(rc)) {
7128 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7129 "6308 Failed to update nvmet-sgl size "
7130 "and mapping: %d\n", rc);
7131 goto out_destroy_queue;
7132 }
7133
7134 /* register the nvmet sgl pool to the port */
7135 rc = lpfc_sli4_repost_sgl_list(
7136 phba,
7137 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7138 phba->sli4_hba.nvmet_xri_cnt);
7139 if (unlikely(rc < 0)) {
7140 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7141 "3117 Error %d during nvmet "
7142 "sgl post\n", rc);
7143 rc = -ENODEV;
7144 goto out_destroy_queue;
7145 }
7146 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07007147
7148 cnt = phba->cfg_iocb_cnt * 1024;
7149 /* We need 1 iocbq for every SGL, for IO processing */
7150 cnt += phba->sli4_hba.nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08007151 } else {
James Smart895427b2017-02-12 13:52:30 -08007152 /* update host scsi xri-sgl sizes and mappings */
7153 rc = lpfc_sli4_scsi_sgl_update(phba);
7154 if (unlikely(rc)) {
7155 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7156 "6309 Failed to update scsi-sgl size "
7157 "and mapping: %d\n", rc);
7158 goto out_destroy_queue;
7159 }
7160
7161 /* update host nvme xri-sgl sizes and mappings */
7162 rc = lpfc_sli4_nvme_sgl_update(phba);
7163 if (unlikely(rc)) {
7164 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7165 "6082 Failed to update nvme-sgl size "
7166 "and mapping: %d\n", rc);
7167 goto out_destroy_queue;
7168 }
James Smart6c621a22017-05-15 15:20:45 -07007169
7170 cnt = phba->cfg_iocb_cnt * 1024;
James Smart11e644e2017-06-15 22:56:48 -07007171 }
7172
7173 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07007174 /* Initialize and populate the iocb list per host */
7175 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007176 "2821 initialize iocb list %d total %d\n",
James Smart6c621a22017-05-15 15:20:45 -07007177 phba->cfg_iocb_cnt, cnt);
7178 rc = lpfc_init_iocb_list(phba, cnt);
7179 if (rc) {
7180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007181 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07007182 goto out_destroy_queue;
7183 }
James Smart8a9d2e82012-05-09 21:16:12 -04007184 }
7185
James Smart11e644e2017-06-15 22:56:48 -07007186 if (phba->nvmet_support)
7187 lpfc_nvmet_create_targetport(phba);
7188
James Smart2d7dbc42017-02-12 13:52:35 -08007189 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007190 /* Post initial buffers to all RQs created */
7191 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7192 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7193 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7194 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7195 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007196 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007197 rqbp->buffer_count = 0;
7198
James Smart2d7dbc42017-02-12 13:52:35 -08007199 lpfc_post_rq_buffer(
7200 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7201 phba->sli4_hba.nvmet_mrq_data[i],
James Smart2448e482018-04-09 14:24:24 -07007202 phba->cfg_nvmet_mrq_post, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007203 }
7204 }
7205
James Smart895427b2017-02-12 13:52:30 -08007206 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
7207 /* register the allocated scsi sgl pool to the port */
7208 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
7209 if (unlikely(rc)) {
7210 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7211 "0383 Error %d during scsi sgl post "
7212 "operation\n", rc);
7213 /* Some Scsi buffers were moved to abort scsi list */
7214 /* A pci function reset will repost them */
7215 rc = -ENODEV;
7216 goto out_destroy_queue;
7217 }
James Smartda0436e2009-05-22 14:51:39 -04007218 }
7219
James Smart01649562017-02-12 13:52:32 -08007220 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7221 (phba->nvmet_support == 0)) {
7222
7223 /* register the allocated nvme sgl pool to the port */
7224 rc = lpfc_repost_nvme_sgl_list(phba);
7225 if (unlikely(rc)) {
7226 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7227 "6116 Error %d during nvme sgl post "
7228 "operation\n", rc);
7229 /* Some NVME buffers were moved to abort nvme list */
7230 /* A pci function reset will repost them */
7231 rc = -ENODEV;
7232 goto out_destroy_queue;
7233 }
James Smartda0436e2009-05-22 14:51:39 -04007234 }
7235
7236 /* Post the rpi header region to the device. */
7237 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7238 if (unlikely(rc)) {
7239 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7240 "0393 Error %d during rpi post operation\n",
7241 rc);
7242 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007243 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007244 }
James Smart97f2ecf2012-03-01 22:35:23 -05007245 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007246
James Smart895427b2017-02-12 13:52:30 -08007247 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007248 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007249 /*
7250 * The FC Port needs to register FCFI (index 0)
7251 */
7252 lpfc_reg_fcfi(phba, mboxq);
7253 mboxq->vport = phba->pport;
7254 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7255 if (rc != MBX_SUCCESS)
7256 goto out_unset_queue;
7257 rc = 0;
7258 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7259 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007260 } else {
7261 /* We are a NVME Target mode with MRQ > 1 */
7262
7263 /* First register the FCFI */
7264 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7265 mboxq->vport = phba->pport;
7266 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7267 if (rc != MBX_SUCCESS)
7268 goto out_unset_queue;
7269 rc = 0;
7270 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7271 &mboxq->u.mqe.un.reg_fcfi_mrq);
7272
7273 /* Next register the MRQs */
7274 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7275 mboxq->vport = phba->pport;
7276 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7277 if (rc != MBX_SUCCESS)
7278 goto out_unset_queue;
7279 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007280 }
7281 /* Check if the port is configured to be disabled */
7282 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007283 }
7284
7285 /* Arm the CQs and then EQs on device */
7286 lpfc_sli4_arm_cqeq_intr(phba);
7287
7288 /* Indicate device interrupt mode */
7289 phba->sli4_hba.intr_enable = 1;
7290
7291 /* Allow asynchronous mailbox command to go through */
7292 spin_lock_irq(&phba->hbalock);
7293 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7294 spin_unlock_irq(&phba->hbalock);
7295
7296 /* Post receive buffers to the device */
7297 lpfc_sli4_rb_setup(phba);
7298
James Smartfc2b9892010-02-26 14:15:29 -05007299 /* Reset HBA FCF states after HBA reset */
7300 phba->fcf.fcf_flag = 0;
7301 phba->fcf.current_rec.flag = 0;
7302
James Smartda0436e2009-05-22 14:51:39 -04007303 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007304 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007305 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007306
7307 /* Start heart beat timer */
7308 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007309 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007310 phba->hb_outstanding = 0;
7311 phba->last_completion_time = jiffies;
7312
7313 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007314 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007315 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007316
James Smart75baf692010-06-08 18:31:21 -04007317 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7318 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7319 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7320 if (!rc) {
7321 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7322 "2829 This device supports "
7323 "Advanced Error Reporting (AER)\n");
7324 spin_lock_irq(&phba->hbalock);
7325 phba->hba_flag |= HBA_AER_ENABLED;
7326 spin_unlock_irq(&phba->hbalock);
7327 } else {
7328 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7329 "2830 This device does not support "
7330 "Advanced Error Reporting (AER)\n");
7331 phba->cfg_aer_support = 0;
7332 }
James Smart0a96e972011-07-22 18:37:28 -04007333 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007334 }
7335
James Smartda0436e2009-05-22 14:51:39 -04007336 /*
7337 * The port is ready, set the host's link state to LINK_DOWN
7338 * in preparation for link interrupts.
7339 */
James Smartda0436e2009-05-22 14:51:39 -04007340 spin_lock_irq(&phba->hbalock);
7341 phba->link_state = LPFC_LINK_DOWN;
7342 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05007343 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7344 (phba->hba_flag & LINK_DISABLED)) {
7345 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7346 "3103 Adapter Link is disabled.\n");
7347 lpfc_down_link(phba, mboxq);
7348 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7349 if (rc != MBX_SUCCESS) {
7350 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7351 "3104 Adapter failed to issue "
7352 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7353 goto out_unset_queue;
7354 }
7355 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007356 /* don't perform init_link on SLI4 FC port loopback test */
7357 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7358 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7359 if (rc)
7360 goto out_unset_queue;
7361 }
James Smart5350d872011-10-10 21:33:49 -04007362 }
7363 mempool_free(mboxq, phba->mbox_mem_pool);
7364 return rc;
James Smart76a95d72010-11-20 23:11:48 -05007365out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007366 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007367 lpfc_sli4_queue_unset(phba);
7368out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007369 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007370 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007371out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007372 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007373out_free_mbox:
7374 mempool_free(mboxq, phba->mbox_mem_pool);
7375 return rc;
7376}
James Smarte59058c2008-08-24 21:49:00 -04007377
7378/**
James Smart3621a712009-04-06 18:47:14 -04007379 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007380 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007381 *
James Smarte59058c2008-08-24 21:49:00 -04007382 * This is the callback function for mailbox timer. The mailbox
7383 * timer is armed when a new mailbox command is issued and the timer
7384 * is deleted when the mailbox complete. The function is called by
7385 * the kernel timer code when a mailbox does not complete within
7386 * expected time. This function wakes up the worker thread to
7387 * process the mailbox timeout and returns. All the processing is
7388 * done by the worker thread function lpfc_mbox_timeout_handler.
7389 **/
dea31012005-04-17 16:05:31 -05007390void
Kees Cookf22eb4d2017-09-06 20:24:26 -07007391lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05007392{
Kees Cookf22eb4d2017-09-06 20:24:26 -07007393 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05007394 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007395 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007396
James Smart2e0fef82007-06-17 19:56:36 -05007397 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007398 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007399 if (!tmo_posted)
7400 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7401 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7402
James Smart5e9d9b82008-06-14 22:52:53 -04007403 if (!tmo_posted)
7404 lpfc_worker_wake_up(phba);
7405 return;
dea31012005-04-17 16:05:31 -05007406}
7407
James Smarte8d3c3b2013-10-10 12:21:30 -04007408/**
7409 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7410 * are pending
7411 * @phba: Pointer to HBA context object.
7412 *
7413 * This function checks if any mailbox completions are present on the mailbox
7414 * completion queue.
7415 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007416static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007417lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7418{
7419
7420 uint32_t idx;
7421 struct lpfc_queue *mcq;
7422 struct lpfc_mcqe *mcqe;
7423 bool pending_completions = false;
James Smart7365f6f2018-02-22 08:18:46 -08007424 uint8_t qe_valid;
James Smarte8d3c3b2013-10-10 12:21:30 -04007425
7426 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7427 return false;
7428
7429 /* Check for completions on mailbox completion queue */
7430
7431 mcq = phba->sli4_hba.mbx_cq;
7432 idx = mcq->hba_index;
James Smart7365f6f2018-02-22 08:18:46 -08007433 qe_valid = mcq->qe_valid;
7434 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe) == qe_valid) {
James Smarte8d3c3b2013-10-10 12:21:30 -04007435 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7436 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7437 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7438 pending_completions = true;
7439 break;
7440 }
7441 idx = (idx + 1) % mcq->entry_count;
7442 if (mcq->hba_index == idx)
7443 break;
James Smart7365f6f2018-02-22 08:18:46 -08007444
7445 /* if the index wrapped around, toggle the valid bit */
7446 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7447 qe_valid = (qe_valid) ? 0 : 1;
James Smarte8d3c3b2013-10-10 12:21:30 -04007448 }
7449 return pending_completions;
7450
7451}
7452
7453/**
7454 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7455 * that were missed.
7456 * @phba: Pointer to HBA context object.
7457 *
7458 * For sli4, it is possible to miss an interrupt. As such mbox completions
7459 * maybe missed causing erroneous mailbox timeouts to occur. This function
7460 * checks to see if mbox completions are on the mailbox completion queue
7461 * and will process all the completions associated with the eq for the
7462 * mailbox completion queue.
7463 **/
7464bool
7465lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7466{
James Smartb71413d2018-02-22 08:18:40 -08007467 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04007468 uint32_t eqidx;
7469 struct lpfc_queue *fpeq = NULL;
7470 struct lpfc_eqe *eqe;
7471 bool mbox_pending;
7472
7473 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7474 return false;
7475
7476 /* Find the eq associated with the mcq */
7477
James Smartb71413d2018-02-22 08:18:40 -08007478 if (sli4_hba->hba_eq)
James Smart895427b2017-02-12 13:52:30 -08007479 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
James Smartb71413d2018-02-22 08:18:40 -08007480 if (sli4_hba->hba_eq[eqidx]->queue_id ==
7481 sli4_hba->mbx_cq->assoc_qid) {
7482 fpeq = sli4_hba->hba_eq[eqidx];
James Smarte8d3c3b2013-10-10 12:21:30 -04007483 break;
7484 }
7485 if (!fpeq)
7486 return false;
7487
7488 /* Turn off interrupts from this EQ */
7489
James Smartb71413d2018-02-22 08:18:40 -08007490 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04007491
7492 /* Check to see if a mbox completion is pending */
7493
7494 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7495
7496 /*
7497 * If a mbox completion is pending, process all the events on EQ
7498 * associated with the mbox completion queue (this could include
7499 * mailbox commands, async events, els commands, receive queue data
7500 * and fcp commands)
7501 */
7502
7503 if (mbox_pending)
7504 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7505 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7506 fpeq->EQ_processed++;
7507 }
7508
7509 /* Always clear and re-arm the EQ */
7510
James Smartb71413d2018-02-22 08:18:40 -08007511 sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04007512
7513 return mbox_pending;
7514
7515}
James Smarte59058c2008-08-24 21:49:00 -04007516
7517/**
James Smart3621a712009-04-06 18:47:14 -04007518 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007519 * @phba: Pointer to HBA context object.
7520 *
7521 * This function is called from worker thread when a mailbox command times out.
7522 * The caller is not required to hold any locks. This function will reset the
7523 * HBA and recover all the pending commands.
7524 **/
dea31012005-04-17 16:05:31 -05007525void
7526lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7527{
James Smart2e0fef82007-06-17 19:56:36 -05007528 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007529 MAILBOX_t *mb = NULL;
7530
James Smart1dcb58e2007-04-25 09:51:30 -04007531 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007532
James Smarte8d3c3b2013-10-10 12:21:30 -04007533 /* If the mailbox completed, process the completion and return */
7534 if (lpfc_sli4_process_missed_mbox_completions(phba))
7535 return;
7536
James Smarteb016562014-09-03 12:58:06 -04007537 if (pmbox != NULL)
7538 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007539 /* Check the pmbox pointer first. There is a race condition
7540 * between the mbox timeout handler getting executed in the
7541 * worklist and the mailbox actually completing. When this
7542 * race condition occurs, the mbox_active will be NULL.
7543 */
7544 spin_lock_irq(&phba->hbalock);
7545 if (pmbox == NULL) {
7546 lpfc_printf_log(phba, KERN_WARNING,
7547 LOG_MBOX | LOG_SLI,
7548 "0353 Active Mailbox cleared - mailbox timeout "
7549 "exiting\n");
7550 spin_unlock_irq(&phba->hbalock);
7551 return;
7552 }
7553
dea31012005-04-17 16:05:31 -05007554 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007555 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007556 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007557 mb->mbxCommand,
7558 phba->pport->port_state,
7559 phba->sli.sli_flag,
7560 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007561 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007562
James Smart1dcb58e2007-04-25 09:51:30 -04007563 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7564 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007565 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007566 */
James Smart2e0fef82007-06-17 19:56:36 -05007567 spin_lock_irq(&phba->pport->work_port_lock);
7568 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7569 spin_unlock_irq(&phba->pport->work_port_lock);
7570 spin_lock_irq(&phba->hbalock);
7571 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007572 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007573 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007574
James Smartdb55fba2014-04-04 13:52:02 -04007575 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007576
7577 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007578 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007579
7580 /* Reset the HBA device */
7581 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007582}
7583
James Smarte59058c2008-08-24 21:49:00 -04007584/**
James Smart3772a992009-05-22 14:50:54 -04007585 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007586 * @phba: Pointer to HBA context object.
7587 * @pmbox: Pointer to mailbox object.
7588 * @flag: Flag indicating how the mailbox need to be processed.
7589 *
7590 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007591 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7592 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007593 * The mailbox command can be submitted in polling mode, in which case
7594 * this function will wait in a polling loop for the completion of the
7595 * mailbox.
7596 * If the mailbox is submitted in no_wait mode (not polling) the
7597 * function will submit the command and returns immediately without waiting
7598 * for the mailbox completion. The no_wait is supported only when HBA
7599 * is in SLI2/SLI3 mode - interrupts are enabled.
7600 * The SLI interface allows only one mailbox pending at a time. If the
7601 * mailbox is issued in polling mode and there is already a mailbox
7602 * pending, then the function will return an error. If the mailbox is issued
7603 * in NO_WAIT mode and there is a mailbox pending already, the function
7604 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7605 * The sli layer owns the mailbox object until the completion of mailbox
7606 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7607 * return codes the caller owns the mailbox command after the return of
7608 * the function.
7609 **/
James Smart3772a992009-05-22 14:50:54 -04007610static int
7611lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7612 uint32_t flag)
dea31012005-04-17 16:05:31 -05007613{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007614 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007615 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007616 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007617 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007618 int i;
James Smart09372822008-01-11 01:52:54 -05007619 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007620 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007621 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007622 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007623 int processing_queue = 0;
7624
7625 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7626 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007627 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007628 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007629 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7630 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7631 return MBX_SUCCESS;
7632 }
James Smart58da1ff2008-04-07 10:15:56 -04007633 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007634 pmbox = lpfc_mbox_get(phba);
7635 if (!pmbox) {
7636 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7637 return MBX_SUCCESS;
7638 }
7639 }
dea31012005-04-17 16:05:31 -05007640
James Smarted957682007-06-17 19:56:37 -05007641 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007642 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007643 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007644 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007645 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007646 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007647 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007648 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007649 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007650 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007651 }
7652 }
7653
Linas Vepstas8d63f372007-02-14 14:28:36 -06007654 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007655 if (unlikely(pci_channel_offline(phba->pcidev))) {
7656 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7657 goto out_not_finished;
7658 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007659
James Smarta257bf92009-04-06 18:48:10 -04007660 /* If HBA has a deferred error attention, fail the iocb. */
7661 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7662 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7663 goto out_not_finished;
7664 }
7665
dea31012005-04-17 16:05:31 -05007666 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007667
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007668 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007669 status = MBX_SUCCESS;
7670
James Smart2e0fef82007-06-17 19:56:36 -05007671 if (phba->link_state == LPFC_HBA_ERROR) {
7672 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007673
7674 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007675 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7676 "(%d):0311 Mailbox command x%x cannot "
7677 "issue Data: x%x x%x\n",
7678 pmbox->vport ? pmbox->vport->vpi : 0,
7679 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007680 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007681 }
7682
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007683 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007684 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7685 !(hc_copy & HC_MBINT_ENA)) {
7686 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7687 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007688 "(%d):2528 Mailbox command x%x cannot "
7689 "issue Data: x%x x%x\n",
7690 pmbox->vport ? pmbox->vport->vpi : 0,
7691 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007692 goto out_not_finished;
7693 }
James Smart92908312006-03-07 15:04:13 -05007694 }
7695
dea31012005-04-17 16:05:31 -05007696 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7697 /* Polling for a mbox command when another one is already active
7698 * is not allowed in SLI. Also, the driver must have established
7699 * SLI2 mode to queue and process multiple mbox commands.
7700 */
7701
7702 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007703 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007704
7705 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007706 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7707 "(%d):2529 Mailbox command x%x "
7708 "cannot issue Data: x%x x%x\n",
7709 pmbox->vport ? pmbox->vport->vpi : 0,
7710 pmbox->u.mb.mbxCommand,
7711 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007712 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007713 }
7714
James Smart3772a992009-05-22 14:50:54 -04007715 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007716 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007717 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007718 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7719 "(%d):2530 Mailbox command x%x "
7720 "cannot issue Data: x%x x%x\n",
7721 pmbox->vport ? pmbox->vport->vpi : 0,
7722 pmbox->u.mb.mbxCommand,
7723 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007724 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007725 }
7726
dea31012005-04-17 16:05:31 -05007727 /* Another mailbox command is still being processed, queue this
7728 * command to be processed later.
7729 */
7730 lpfc_mbox_put(phba, pmbox);
7731
7732 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007733 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007734 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007735 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007736 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07007737 mbx->mbxCommand,
7738 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007739 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007740
7741 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007742 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007743
James Smart858c9f62007-06-17 19:56:39 -05007744 if (pmbox->vport) {
7745 lpfc_debugfs_disc_trc(pmbox->vport,
7746 LPFC_DISC_TRC_MBOX_VPORT,
7747 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007748 (uint32_t)mbx->mbxCommand,
7749 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007750 }
7751 else {
7752 lpfc_debugfs_disc_trc(phba->pport,
7753 LPFC_DISC_TRC_MBOX,
7754 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007755 (uint32_t)mbx->mbxCommand,
7756 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007757 }
7758
James Smart2e0fef82007-06-17 19:56:36 -05007759 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007760 }
7761
dea31012005-04-17 16:05:31 -05007762 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7763
7764 /* If we are not polling, we MUST be in SLI2 mode */
7765 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007766 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007767 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007768 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007769 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007770 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007771 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7772 "(%d):2531 Mailbox command x%x "
7773 "cannot issue Data: x%x x%x\n",
7774 pmbox->vport ? pmbox->vport->vpi : 0,
7775 pmbox->u.mb.mbxCommand,
7776 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007777 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007778 }
7779 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007780 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7781 1000);
7782 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007783 }
7784
7785 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007786 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007787 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007788 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007789 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07007790 mbx->mbxCommand,
7791 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007792 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007793
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007794 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007795 if (pmbox->vport) {
7796 lpfc_debugfs_disc_trc(pmbox->vport,
7797 LPFC_DISC_TRC_MBOX_VPORT,
7798 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007799 (uint32_t)mbx->mbxCommand,
7800 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007801 }
7802 else {
7803 lpfc_debugfs_disc_trc(phba->pport,
7804 LPFC_DISC_TRC_MBOX,
7805 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007806 (uint32_t)mbx->mbxCommand,
7807 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007808 }
7809 }
7810
dea31012005-04-17 16:05:31 -05007811 psli->slistat.mbox_cmd++;
7812 evtctr = psli->slistat.mbox_event;
7813
7814 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007815 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007816
James Smart3772a992009-05-22 14:50:54 -04007817 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007818 /* Populate mbox extension offset word. */
7819 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007820 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007821 = (uint8_t *)phba->mbox_ext
7822 - (uint8_t *)phba->mbox;
7823 }
7824
7825 /* Copy the mailbox extension data */
7826 if (pmbox->in_ext_byte_len && pmbox->context2) {
7827 lpfc_sli_pcimem_bcopy(pmbox->context2,
7828 (uint8_t *)phba->mbox_ext,
7829 pmbox->in_ext_byte_len);
7830 }
7831 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007832 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007833 } else {
James Smart7a470272010-03-15 11:25:20 -04007834 /* Populate mbox extension offset word. */
7835 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007836 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007837 = MAILBOX_HBA_EXT_OFFSET;
7838
7839 /* Copy the mailbox extension data */
James Smart895427b2017-02-12 13:52:30 -08007840 if (pmbox->in_ext_byte_len && pmbox->context2)
James Smart7a470272010-03-15 11:25:20 -04007841 lpfc_memcpy_to_slim(phba->MBslimaddr +
7842 MAILBOX_HBA_EXT_OFFSET,
7843 pmbox->context2, pmbox->in_ext_byte_len);
7844
James Smart895427b2017-02-12 13:52:30 -08007845 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007846 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08007847 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7848 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007849
7850 /* First copy mbox command data to HBA SLIM, skip past first
7851 word */
7852 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007853 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007854 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7855
7856 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007857 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007858 to_slim = phba->MBslimaddr;
7859 writel(ldata, to_slim);
7860 readl(to_slim); /* flush */
7861
James Smart895427b2017-02-12 13:52:30 -08007862 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007863 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007864 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007865 }
7866
7867 wmb();
dea31012005-04-17 16:05:31 -05007868
7869 switch (flag) {
7870 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007871 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007872 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007873 /* Interrupt board to do it */
7874 writel(CA_MBATT, phba->CAregaddr);
7875 readl(phba->CAregaddr); /* flush */
7876 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007877 break;
7878
7879 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007880 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007881 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007882 /* Interrupt board to do it */
7883 writel(CA_MBATT, phba->CAregaddr);
7884 readl(phba->CAregaddr); /* flush */
7885
James Smart3772a992009-05-22 14:50:54 -04007886 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007887 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007888 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007889 word0 = le32_to_cpu(word0);
7890 } else {
7891 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007892 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7893 spin_unlock_irqrestore(&phba->hbalock,
7894 drvr_flag);
7895 goto out_not_finished;
7896 }
dea31012005-04-17 16:05:31 -05007897 }
7898
7899 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007900 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7901 spin_unlock_irqrestore(&phba->hbalock,
7902 drvr_flag);
7903 goto out_not_finished;
7904 }
James Smarta183a152011-10-10 21:32:43 -04007905 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7906 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007907 i = 0;
dea31012005-04-17 16:05:31 -05007908 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007909 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7910 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007911 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007912 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007913 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007914 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007915 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007916 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007917 }
7918
7919 /* Check if we took a mbox interrupt while we were
7920 polling */
7921 if (((word0 & OWN_CHIP) != OWN_CHIP)
7922 && (evtctr != psli->slistat.mbox_event))
7923 break;
7924
James Smart09372822008-01-11 01:52:54 -05007925 if (i++ > 10) {
7926 spin_unlock_irqrestore(&phba->hbalock,
7927 drvr_flag);
7928 msleep(1);
7929 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7930 }
dea31012005-04-17 16:05:31 -05007931
James Smart3772a992009-05-22 14:50:54 -04007932 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007933 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007934 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007935 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007936 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007937 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007938 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007939 /* Check real SLIM for any errors */
7940 slimword0 = readl(phba->MBslimaddr);
7941 slimmb = (MAILBOX_t *) & slimword0;
7942 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7943 && slimmb->mbxStatus) {
7944 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007945 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007946 word0 = slimword0;
7947 }
7948 }
7949 } else {
7950 /* First copy command data */
7951 word0 = readl(phba->MBslimaddr);
7952 }
7953 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007954 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7955 spin_unlock_irqrestore(&phba->hbalock,
7956 drvr_flag);
7957 goto out_not_finished;
7958 }
dea31012005-04-17 16:05:31 -05007959 }
7960
James Smart3772a992009-05-22 14:50:54 -04007961 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007962 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08007963 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7964 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007965 /* Copy the mailbox extension data */
7966 if (pmbox->out_ext_byte_len && pmbox->context2) {
7967 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7968 pmbox->context2,
7969 pmbox->out_ext_byte_len);
7970 }
dea31012005-04-17 16:05:31 -05007971 } else {
7972 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007973 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08007974 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007975 /* Copy the mailbox extension data */
7976 if (pmbox->out_ext_byte_len && pmbox->context2) {
7977 lpfc_memcpy_from_slim(pmbox->context2,
7978 phba->MBslimaddr +
7979 MAILBOX_HBA_EXT_OFFSET,
7980 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007981 }
7982 }
7983
7984 writel(HA_MBATT, phba->HAregaddr);
7985 readl(phba->HAregaddr); /* flush */
7986
7987 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007988 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007989 }
7990
James Smart2e0fef82007-06-17 19:56:36 -05007991 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7992 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007993
7994out_not_finished:
7995 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007996 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007997 lpfc_mbox_cmpl_put(phba, pmbox);
7998 }
7999 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008000}
8001
James Smarte59058c2008-08-24 21:49:00 -04008002/**
James Smartf1126682009-06-10 17:22:44 -04008003 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8004 * @phba: Pointer to HBA context object.
8005 *
8006 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8007 * the driver internal pending mailbox queue. It will then try to wait out the
8008 * possible outstanding mailbox command before return.
8009 *
8010 * Returns:
8011 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8012 * the outstanding mailbox command timed out.
8013 **/
8014static int
8015lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8016{
8017 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04008018 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04008019 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04008020
8021 /* Mark the asynchronous mailbox command posting as blocked */
8022 spin_lock_irq(&phba->hbalock);
8023 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04008024 /* Determine how long we might wait for the active mailbox
8025 * command to be gracefully completed by firmware.
8026 */
James Smarta183a152011-10-10 21:32:43 -04008027 if (phba->sli.mbox_active)
8028 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8029 phba->sli.mbox_active) *
8030 1000) + jiffies;
8031 spin_unlock_irq(&phba->hbalock);
8032
James Smarte8d3c3b2013-10-10 12:21:30 -04008033 /* Make sure the mailbox is really active */
8034 if (timeout)
8035 lpfc_sli4_process_missed_mbox_completions(phba);
8036
James Smartf1126682009-06-10 17:22:44 -04008037 /* Wait for the outstnading mailbox command to complete */
8038 while (phba->sli.mbox_active) {
8039 /* Check active mailbox complete status every 2ms */
8040 msleep(2);
8041 if (time_after(jiffies, timeout)) {
8042 /* Timeout, marked the outstanding cmd not complete */
8043 rc = 1;
8044 break;
8045 }
8046 }
8047
8048 /* Can not cleanly block async mailbox command, fails it */
8049 if (rc) {
8050 spin_lock_irq(&phba->hbalock);
8051 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8052 spin_unlock_irq(&phba->hbalock);
8053 }
8054 return rc;
8055}
8056
8057/**
8058 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8059 * @phba: Pointer to HBA context object.
8060 *
8061 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8062 * commands from the driver internal pending mailbox queue. It makes sure
8063 * that there is no outstanding mailbox command before resuming posting
8064 * asynchronous mailbox commands. If, for any reason, there is outstanding
8065 * mailbox command, it will try to wait it out before resuming asynchronous
8066 * mailbox command posting.
8067 **/
8068static void
8069lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8070{
8071 struct lpfc_sli *psli = &phba->sli;
8072
8073 spin_lock_irq(&phba->hbalock);
8074 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8075 /* Asynchronous mailbox posting is not blocked, do nothing */
8076 spin_unlock_irq(&phba->hbalock);
8077 return;
8078 }
8079
8080 /* Outstanding synchronous mailbox command is guaranteed to be done,
8081 * successful or timeout, after timing-out the outstanding mailbox
8082 * command shall always be removed, so just unblock posting async
8083 * mailbox command and resume
8084 */
8085 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8086 spin_unlock_irq(&phba->hbalock);
8087
8088 /* wake up worker thread to post asynchronlous mailbox command */
8089 lpfc_worker_wake_up(phba);
8090}
8091
8092/**
James Smart2d843ed2012-09-29 11:29:06 -04008093 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8094 * @phba: Pointer to HBA context object.
8095 * @mboxq: Pointer to mailbox object.
8096 *
8097 * The function waits for the bootstrap mailbox register ready bit from
8098 * port for twice the regular mailbox command timeout value.
8099 *
8100 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8101 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8102 **/
8103static int
8104lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8105{
8106 uint32_t db_ready;
8107 unsigned long timeout;
8108 struct lpfc_register bmbx_reg;
8109
8110 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8111 * 1000) + jiffies;
8112
8113 do {
8114 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8115 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8116 if (!db_ready)
8117 msleep(2);
8118
8119 if (time_after(jiffies, timeout))
8120 return MBXERR_ERROR;
8121 } while (!db_ready);
8122
8123 return 0;
8124}
8125
8126/**
James Smartda0436e2009-05-22 14:51:39 -04008127 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8128 * @phba: Pointer to HBA context object.
8129 * @mboxq: Pointer to mailbox object.
8130 *
8131 * The function posts a mailbox to the port. The mailbox is expected
8132 * to be comletely filled in and ready for the port to operate on it.
8133 * This routine executes a synchronous completion operation on the
8134 * mailbox by polling for its completion.
8135 *
8136 * The caller must not be holding any locks when calling this routine.
8137 *
8138 * Returns:
8139 * MBX_SUCCESS - mailbox posted successfully
8140 * Any of the MBX error values.
8141 **/
8142static int
8143lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8144{
8145 int rc = MBX_SUCCESS;
8146 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04008147 uint32_t mcqe_status;
8148 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04008149 struct lpfc_sli *psli = &phba->sli;
8150 struct lpfc_mqe *mb = &mboxq->u.mqe;
8151 struct lpfc_bmbx_create *mbox_rgn;
8152 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04008153
8154 /*
8155 * Only one mailbox can be active to the bootstrap mailbox region
8156 * at a time and there is no queueing provided.
8157 */
8158 spin_lock_irqsave(&phba->hbalock, iflag);
8159 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8160 spin_unlock_irqrestore(&phba->hbalock, iflag);
8161 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008162 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008163 "cannot issue Data: x%x x%x\n",
8164 mboxq->vport ? mboxq->vport->vpi : 0,
8165 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008166 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8167 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008168 psli->sli_flag, MBX_POLL);
8169 return MBXERR_ERROR;
8170 }
8171 /* The server grabs the token and owns it until release */
8172 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8173 phba->sli.mbox_active = mboxq;
8174 spin_unlock_irqrestore(&phba->hbalock, iflag);
8175
James Smart2d843ed2012-09-29 11:29:06 -04008176 /* wait for bootstrap mbox register for readyness */
8177 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8178 if (rc)
8179 goto exit;
8180
James Smartda0436e2009-05-22 14:51:39 -04008181 /*
8182 * Initialize the bootstrap memory region to avoid stale data areas
8183 * in the mailbox post. Then copy the caller's mailbox contents to
8184 * the bmbx mailbox region.
8185 */
8186 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8187 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
8188 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8189 sizeof(struct lpfc_mqe));
8190
8191 /* Post the high mailbox dma address to the port and wait for ready. */
8192 dma_address = &phba->sli4_hba.bmbx.dma_address;
8193 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8194
James Smart2d843ed2012-09-29 11:29:06 -04008195 /* wait for bootstrap mbox register for hi-address write done */
8196 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8197 if (rc)
8198 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008199
8200 /* Post the low mailbox dma address to the port. */
8201 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008202
James Smart2d843ed2012-09-29 11:29:06 -04008203 /* wait for bootstrap mbox register for low address write done */
8204 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8205 if (rc)
8206 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008207
8208 /*
8209 * Read the CQ to ensure the mailbox has completed.
8210 * If so, update the mailbox status so that the upper layers
8211 * can complete the request normally.
8212 */
8213 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8214 sizeof(struct lpfc_mqe));
8215 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
8216 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8217 sizeof(struct lpfc_mcqe));
8218 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008219 /*
8220 * When the CQE status indicates a failure and the mailbox status
8221 * indicates success then copy the CQE status into the mailbox status
8222 * (and prefix it with x4000).
8223 */
James Smartda0436e2009-05-22 14:51:39 -04008224 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008225 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8226 bf_set(lpfc_mqe_status, mb,
8227 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008228 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008229 } else
8230 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008231
8232 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008233 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008234 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8235 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008236 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8237 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8238 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008239 bf_get(lpfc_mqe_status, mb),
8240 mb->un.mb_words[0], mb->un.mb_words[1],
8241 mb->un.mb_words[2], mb->un.mb_words[3],
8242 mb->un.mb_words[4], mb->un.mb_words[5],
8243 mb->un.mb_words[6], mb->un.mb_words[7],
8244 mb->un.mb_words[8], mb->un.mb_words[9],
8245 mb->un.mb_words[10], mb->un.mb_words[11],
8246 mb->un.mb_words[12], mboxq->mcqe.word0,
8247 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8248 mboxq->mcqe.trailer);
8249exit:
8250 /* We are holding the token, no needed for lock when release */
8251 spin_lock_irqsave(&phba->hbalock, iflag);
8252 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8253 phba->sli.mbox_active = NULL;
8254 spin_unlock_irqrestore(&phba->hbalock, iflag);
8255 return rc;
8256}
8257
8258/**
8259 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8260 * @phba: Pointer to HBA context object.
8261 * @pmbox: Pointer to mailbox object.
8262 * @flag: Flag indicating how the mailbox need to be processed.
8263 *
8264 * This function is called by discovery code and HBA management code to submit
8265 * a mailbox command to firmware with SLI-4 interface spec.
8266 *
8267 * Return codes the caller owns the mailbox command after the return of the
8268 * function.
8269 **/
8270static int
8271lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8272 uint32_t flag)
8273{
8274 struct lpfc_sli *psli = &phba->sli;
8275 unsigned long iflags;
8276 int rc;
8277
James Smartb76f2dc2011-07-22 18:37:42 -04008278 /* dump from issue mailbox command if setup */
8279 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8280
James Smart8fa38512009-07-19 10:01:03 -04008281 rc = lpfc_mbox_dev_check(phba);
8282 if (unlikely(rc)) {
8283 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008284 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008285 "cannot issue Data: x%x x%x\n",
8286 mboxq->vport ? mboxq->vport->vpi : 0,
8287 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008288 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8289 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008290 psli->sli_flag, flag);
8291 goto out_not_finished;
8292 }
8293
James Smartda0436e2009-05-22 14:51:39 -04008294 /* Detect polling mode and jump to a handler */
8295 if (!phba->sli4_hba.intr_enable) {
8296 if (flag == MBX_POLL)
8297 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8298 else
8299 rc = -EIO;
8300 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008301 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008302 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008303 "(x%x/x%x) failure: "
8304 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8305 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008306 mboxq->vport ? mboxq->vport->vpi : 0,
8307 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008308 lpfc_sli_config_mbox_subsys_get(phba,
8309 mboxq),
8310 lpfc_sli_config_mbox_opcode_get(phba,
8311 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008312 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8313 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8314 bf_get(lpfc_mcqe_ext_status,
8315 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008316 psli->sli_flag, flag);
8317 return rc;
8318 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008319 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8320 "(%d):2542 Try to issue mailbox command "
James Smart7365f6f2018-02-22 08:18:46 -08008321 "x%x (x%x/x%x) synchronously ahead of async "
James Smartf1126682009-06-10 17:22:44 -04008322 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008323 mboxq->vport ? mboxq->vport->vpi : 0,
8324 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008325 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8326 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008327 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008328 /* Try to block the asynchronous mailbox posting */
8329 rc = lpfc_sli4_async_mbox_block(phba);
8330 if (!rc) {
8331 /* Successfully blocked, now issue sync mbox cmd */
8332 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8333 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008334 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008335 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008336 "(%d):2597 Sync Mailbox command "
8337 "x%x (x%x/x%x) failure: "
8338 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8339 "Data: x%x x%x\n,",
8340 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008341 mboxq->u.mb.mbxCommand,
8342 lpfc_sli_config_mbox_subsys_get(phba,
8343 mboxq),
8344 lpfc_sli_config_mbox_opcode_get(phba,
8345 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008346 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8347 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8348 bf_get(lpfc_mcqe_ext_status,
8349 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008350 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008351 /* Unblock the async mailbox posting afterward */
8352 lpfc_sli4_async_mbox_unblock(phba);
8353 }
8354 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008355 }
8356
8357 /* Now, interrupt mode asynchrous mailbox command */
8358 rc = lpfc_mbox_cmd_check(phba, mboxq);
8359 if (rc) {
8360 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008361 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008362 "cannot issue Data: x%x x%x\n",
8363 mboxq->vport ? mboxq->vport->vpi : 0,
8364 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008365 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8366 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008367 psli->sli_flag, flag);
8368 goto out_not_finished;
8369 }
James Smartda0436e2009-05-22 14:51:39 -04008370
8371 /* Put the mailbox command to the driver internal FIFO */
8372 psli->slistat.mbox_busy++;
8373 spin_lock_irqsave(&phba->hbalock, iflags);
8374 lpfc_mbox_put(phba, mboxq);
8375 spin_unlock_irqrestore(&phba->hbalock, iflags);
8376 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8377 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008378 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008379 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8380 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008381 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8382 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008383 phba->pport->port_state,
8384 psli->sli_flag, MBX_NOWAIT);
8385 /* Wake up worker thread to transport mailbox command from head */
8386 lpfc_worker_wake_up(phba);
8387
8388 return MBX_BUSY;
8389
8390out_not_finished:
8391 return MBX_NOT_FINISHED;
8392}
8393
8394/**
8395 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8396 * @phba: Pointer to HBA context object.
8397 *
8398 * This function is called by worker thread to send a mailbox command to
8399 * SLI4 HBA firmware.
8400 *
8401 **/
8402int
8403lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8404{
8405 struct lpfc_sli *psli = &phba->sli;
8406 LPFC_MBOXQ_t *mboxq;
8407 int rc = MBX_SUCCESS;
8408 unsigned long iflags;
8409 struct lpfc_mqe *mqe;
8410 uint32_t mbx_cmnd;
8411
8412 /* Check interrupt mode before post async mailbox command */
8413 if (unlikely(!phba->sli4_hba.intr_enable))
8414 return MBX_NOT_FINISHED;
8415
8416 /* Check for mailbox command service token */
8417 spin_lock_irqsave(&phba->hbalock, iflags);
8418 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8419 spin_unlock_irqrestore(&phba->hbalock, iflags);
8420 return MBX_NOT_FINISHED;
8421 }
8422 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8423 spin_unlock_irqrestore(&phba->hbalock, iflags);
8424 return MBX_NOT_FINISHED;
8425 }
8426 if (unlikely(phba->sli.mbox_active)) {
8427 spin_unlock_irqrestore(&phba->hbalock, iflags);
8428 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8429 "0384 There is pending active mailbox cmd\n");
8430 return MBX_NOT_FINISHED;
8431 }
8432 /* Take the mailbox command service token */
8433 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8434
8435 /* Get the next mailbox command from head of queue */
8436 mboxq = lpfc_mbox_get(phba);
8437
8438 /* If no more mailbox command waiting for post, we're done */
8439 if (!mboxq) {
8440 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8441 spin_unlock_irqrestore(&phba->hbalock, iflags);
8442 return MBX_SUCCESS;
8443 }
8444 phba->sli.mbox_active = mboxq;
8445 spin_unlock_irqrestore(&phba->hbalock, iflags);
8446
8447 /* Check device readiness for posting mailbox command */
8448 rc = lpfc_mbox_dev_check(phba);
8449 if (unlikely(rc))
8450 /* Driver clean routine will clean up pending mailbox */
8451 goto out_not_finished;
8452
8453 /* Prepare the mbox command to be posted */
8454 mqe = &mboxq->u.mqe;
8455 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8456
8457 /* Start timer for the mbox_tmo and log some mailbox post messages */
8458 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008459 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008460
8461 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008462 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008463 "x%x x%x\n",
8464 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008465 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8466 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008467 phba->pport->port_state, psli->sli_flag);
8468
8469 if (mbx_cmnd != MBX_HEARTBEAT) {
8470 if (mboxq->vport) {
8471 lpfc_debugfs_disc_trc(mboxq->vport,
8472 LPFC_DISC_TRC_MBOX_VPORT,
8473 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8474 mbx_cmnd, mqe->un.mb_words[0],
8475 mqe->un.mb_words[1]);
8476 } else {
8477 lpfc_debugfs_disc_trc(phba->pport,
8478 LPFC_DISC_TRC_MBOX,
8479 "MBOX Send: cmd:x%x mb:x%x x%x",
8480 mbx_cmnd, mqe->un.mb_words[0],
8481 mqe->un.mb_words[1]);
8482 }
8483 }
8484 psli->slistat.mbox_cmd++;
8485
8486 /* Post the mailbox command to the port */
8487 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8488 if (rc != MBX_SUCCESS) {
8489 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008490 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008491 "cannot issue Data: x%x x%x\n",
8492 mboxq->vport ? mboxq->vport->vpi : 0,
8493 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008494 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8495 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008496 psli->sli_flag, MBX_NOWAIT);
8497 goto out_not_finished;
8498 }
8499
8500 return rc;
8501
8502out_not_finished:
8503 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008504 if (phba->sli.mbox_active) {
8505 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8506 __lpfc_mbox_cmpl_put(phba, mboxq);
8507 /* Release the token */
8508 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8509 phba->sli.mbox_active = NULL;
8510 }
James Smartda0436e2009-05-22 14:51:39 -04008511 spin_unlock_irqrestore(&phba->hbalock, iflags);
8512
8513 return MBX_NOT_FINISHED;
8514}
8515
8516/**
8517 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8518 * @phba: Pointer to HBA context object.
8519 * @pmbox: Pointer to mailbox object.
8520 * @flag: Flag indicating how the mailbox need to be processed.
8521 *
8522 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8523 * the API jump table function pointer from the lpfc_hba struct.
8524 *
8525 * Return codes the caller owns the mailbox command after the return of the
8526 * function.
8527 **/
8528int
8529lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8530{
8531 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8532}
8533
8534/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008535 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008536 * @phba: The hba struct for which this call is being executed.
8537 * @dev_grp: The HBA PCI-Device group number.
8538 *
8539 * This routine sets up the mbox interface API function jump table in @phba
8540 * struct.
8541 * Returns: 0 - success, -ENODEV - failure.
8542 **/
8543int
8544lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8545{
8546
8547 switch (dev_grp) {
8548 case LPFC_PCI_DEV_LP:
8549 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8550 phba->lpfc_sli_handle_slow_ring_event =
8551 lpfc_sli_handle_slow_ring_event_s3;
8552 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8553 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8554 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8555 break;
8556 case LPFC_PCI_DEV_OC:
8557 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8558 phba->lpfc_sli_handle_slow_ring_event =
8559 lpfc_sli_handle_slow_ring_event_s4;
8560 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8561 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8562 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8563 break;
8564 default:
8565 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8566 "1420 Invalid HBA PCI-device group: 0x%x\n",
8567 dev_grp);
8568 return -ENODEV;
8569 break;
8570 }
8571 return 0;
8572}
8573
8574/**
James Smart3621a712009-04-06 18:47:14 -04008575 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008576 * @phba: Pointer to HBA context object.
8577 * @pring: Pointer to driver SLI ring object.
8578 * @piocb: Pointer to address of newly added command iocb.
8579 *
8580 * This function is called with hbalock held to add a command
8581 * iocb to the txq when SLI layer cannot submit the command iocb
8582 * to the ring.
8583 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008584void
James Smart92d7f7b2007-06-17 19:56:38 -05008585__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008586 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008587{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008588 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008589 /* Insert the caller's iocb in the txq tail for later processing. */
8590 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008591}
8592
James Smarte59058c2008-08-24 21:49:00 -04008593/**
James Smart3621a712009-04-06 18:47:14 -04008594 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008595 * @phba: Pointer to HBA context object.
8596 * @pring: Pointer to driver SLI ring object.
8597 * @piocb: Pointer to address of newly added command iocb.
8598 *
8599 * This function is called with hbalock held before a new
8600 * iocb is submitted to the firmware. This function checks
8601 * txq to flush the iocbs in txq to Firmware before
8602 * submitting new iocbs to the Firmware.
8603 * If there are iocbs in the txq which need to be submitted
8604 * to firmware, lpfc_sli_next_iocb returns the first element
8605 * of the txq after dequeuing it from txq.
8606 * If there is no iocb in the txq then the function will return
8607 * *piocb and *piocb is set to NULL. Caller needs to check
8608 * *piocb to find if there are more commands in the txq.
8609 **/
dea31012005-04-17 16:05:31 -05008610static struct lpfc_iocbq *
8611lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008612 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008613{
8614 struct lpfc_iocbq * nextiocb;
8615
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008616 lockdep_assert_held(&phba->hbalock);
8617
dea31012005-04-17 16:05:31 -05008618 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8619 if (!nextiocb) {
8620 nextiocb = *piocb;
8621 *piocb = NULL;
8622 }
8623
8624 return nextiocb;
8625}
8626
James Smarte59058c2008-08-24 21:49:00 -04008627/**
James Smart3772a992009-05-22 14:50:54 -04008628 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008629 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008630 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008631 * @piocb: Pointer to command iocb.
8632 * @flag: Flag indicating if this command can be put into txq.
8633 *
James Smart3772a992009-05-22 14:50:54 -04008634 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8635 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8636 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8637 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8638 * this function allows only iocbs for posting buffers. This function finds
8639 * next available slot in the command ring and posts the command to the
8640 * available slot and writes the port attention register to request HBA start
8641 * processing new iocb. If there is no slot available in the ring and
8642 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8643 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008644 *
James Smart3772a992009-05-22 14:50:54 -04008645 * This function is called with hbalock held. The function will return success
8646 * after it successfully submit the iocb to firmware or after adding to the
8647 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008648 **/
James Smart98c9ea52007-10-27 13:37:33 -04008649static int
James Smart3772a992009-05-22 14:50:54 -04008650__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008651 struct lpfc_iocbq *piocb, uint32_t flag)
8652{
8653 struct lpfc_iocbq *nextiocb;
8654 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08008655 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05008656
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008657 lockdep_assert_held(&phba->hbalock);
8658
James Smart92d7f7b2007-06-17 19:56:38 -05008659 if (piocb->iocb_cmpl && (!piocb->vport) &&
8660 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8661 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8662 lpfc_printf_log(phba, KERN_ERR,
8663 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008664 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008665 piocb->iocb.ulpCommand);
8666 dump_stack();
8667 return IOCB_ERROR;
8668 }
8669
8670
Linas Vepstas8d63f372007-02-14 14:28:36 -06008671 /* If the PCI channel is in offline state, do not post iocbs. */
8672 if (unlikely(pci_channel_offline(phba->pcidev)))
8673 return IOCB_ERROR;
8674
James Smarta257bf92009-04-06 18:48:10 -04008675 /* If HBA has a deferred error attention, fail the iocb. */
8676 if (unlikely(phba->hba_flag & DEFER_ERATT))
8677 return IOCB_ERROR;
8678
dea31012005-04-17 16:05:31 -05008679 /*
8680 * We should never get an IOCB if we are in a < LINK_DOWN state
8681 */
James Smart2e0fef82007-06-17 19:56:36 -05008682 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008683 return IOCB_ERROR;
8684
8685 /*
8686 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008687 * outstanding event.
dea31012005-04-17 16:05:31 -05008688 */
James Smart0b727fe2007-10-27 13:37:25 -04008689 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008690 goto iocb_busy;
8691
James Smart2e0fef82007-06-17 19:56:36 -05008692 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008693 /*
James Smart2680eea2007-04-25 09:52:55 -04008694 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008695 * can be issued if the link is not up.
8696 */
8697 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008698 case CMD_GEN_REQUEST64_CR:
8699 case CMD_GEN_REQUEST64_CX:
8700 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8701 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008702 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008703 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8704 MENLO_TRANSPORT_TYPE))
8705
8706 goto iocb_busy;
8707 break;
dea31012005-04-17 16:05:31 -05008708 case CMD_QUE_RING_BUF_CN:
8709 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008710 /*
8711 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8712 * completion, iocb_cmpl MUST be 0.
8713 */
8714 if (piocb->iocb_cmpl)
8715 piocb->iocb_cmpl = NULL;
8716 /*FALLTHROUGH*/
8717 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008718 case CMD_CLOSE_XRI_CN:
8719 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008720 break;
8721 default:
8722 goto iocb_busy;
8723 }
8724
8725 /*
8726 * For FCP commands, we must be in a state where we can process link
8727 * attention events.
8728 */
James Smart895427b2017-02-12 13:52:30 -08008729 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05008730 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008731 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008732 }
dea31012005-04-17 16:05:31 -05008733
dea31012005-04-17 16:05:31 -05008734 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8735 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8736 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8737
8738 if (iocb)
8739 lpfc_sli_update_ring(phba, pring);
8740 else
8741 lpfc_sli_update_full_ring(phba, pring);
8742
8743 if (!piocb)
8744 return IOCB_SUCCESS;
8745
8746 goto out_busy;
8747
8748 iocb_busy:
8749 pring->stats.iocb_cmd_delay++;
8750
8751 out_busy:
8752
8753 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008754 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008755 return IOCB_SUCCESS;
8756 }
8757
8758 return IOCB_BUSY;
8759}
8760
James Smart3772a992009-05-22 14:50:54 -04008761/**
James Smart4f774512009-05-22 14:52:35 -04008762 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8763 * @phba: Pointer to HBA context object.
8764 * @piocb: Pointer to command iocb.
8765 * @sglq: Pointer to the scatter gather queue object.
8766 *
8767 * This routine converts the bpl or bde that is in the IOCB
8768 * to a sgl list for the sli4 hardware. The physical address
8769 * of the bpl/bde is converted back to a virtual address.
8770 * If the IOCB contains a BPL then the list of BDE's is
8771 * converted to sli4_sge's. If the IOCB contains a single
8772 * BDE then it is converted to a single sli_sge.
8773 * The IOCB is still in cpu endianess so the contents of
8774 * the bpl can be used without byte swapping.
8775 *
8776 * Returns valid XRI = Success, NO_XRI = Failure.
8777**/
8778static uint16_t
8779lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8780 struct lpfc_sglq *sglq)
8781{
8782 uint16_t xritag = NO_XRI;
8783 struct ulp_bde64 *bpl = NULL;
8784 struct ulp_bde64 bde;
8785 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008786 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008787 IOCB_t *icmd;
8788 int numBdes = 0;
8789 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008790 uint32_t offset = 0; /* accumulated offset in the sg request list */
8791 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008792
8793 if (!piocbq || !sglq)
8794 return xritag;
8795
8796 sgl = (struct sli4_sge *)sglq->sgl;
8797 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008798 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8799 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008800 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8801 numBdes = icmd->un.genreq64.bdl.bdeSize /
8802 sizeof(struct ulp_bde64);
8803 /* The addrHigh and addrLow fields within the IOCB
8804 * have not been byteswapped yet so there is no
8805 * need to swap them back.
8806 */
James Smart1b511972011-12-13 13:23:09 -05008807 if (piocbq->context3)
8808 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8809 else
8810 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008811
James Smart1b511972011-12-13 13:23:09 -05008812 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008813 if (!bpl)
8814 return xritag;
8815
8816 for (i = 0; i < numBdes; i++) {
8817 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008818 sgl->addr_hi = bpl->addrHigh;
8819 sgl->addr_lo = bpl->addrLow;
8820
James Smart05580562011-05-24 11:40:48 -04008821 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008822 if ((i+1) == numBdes)
8823 bf_set(lpfc_sli4_sge_last, sgl, 1);
8824 else
8825 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008826 /* swap the size field back to the cpu so we
8827 * can assign it to the sgl.
8828 */
8829 bde.tus.w = le32_to_cpu(bpl->tus.w);
8830 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008831 /* The offsets in the sgl need to be accumulated
8832 * separately for the request and reply lists.
8833 * The request is always first, the reply follows.
8834 */
8835 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8836 /* add up the reply sg entries */
8837 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8838 inbound++;
8839 /* first inbound? reset the offset */
8840 if (inbound == 1)
8841 offset = 0;
8842 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008843 bf_set(lpfc_sli4_sge_type, sgl,
8844 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008845 offset += bde.tus.f.bdeSize;
8846 }
James Smart546fc852011-03-11 16:06:29 -05008847 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008848 bpl++;
8849 sgl++;
8850 }
8851 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8852 /* The addrHigh and addrLow fields of the BDE have not
8853 * been byteswapped yet so they need to be swapped
8854 * before putting them in the sgl.
8855 */
8856 sgl->addr_hi =
8857 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8858 sgl->addr_lo =
8859 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008860 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008861 bf_set(lpfc_sli4_sge_last, sgl, 1);
8862 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008863 sgl->sge_len =
8864 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008865 }
8866 return sglq->sli4_xritag;
8867}
8868
8869/**
James Smart4f774512009-05-22 14:52:35 -04008870 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8871 * @phba: Pointer to HBA context object.
8872 * @piocb: Pointer to command iocb.
8873 * @wqe: Pointer to the work queue entry.
8874 *
8875 * This routine converts the iocb command to its Work Queue Entry
8876 * equivalent. The wqe pointer should not have any fields set when
8877 * this routine is called because it will memcpy over them.
8878 * This routine does not set the CQ_ID or the WQEC bits in the
8879 * wqe.
8880 *
8881 * Returns: 0 = Success, IOCB_ERROR = Failure.
8882 **/
8883static int
8884lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
James Smart205e8242018-03-05 12:04:03 -08008885 union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -04008886{
James Smart5ffc2662009-11-18 15:39:44 -05008887 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008888 uint8_t ct = 0;
8889 uint32_t fip;
8890 uint32_t abort_tag;
8891 uint8_t command_type = ELS_COMMAND_NON_FIP;
8892 uint8_t cmnd;
8893 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008894 uint16_t abrt_iotag;
8895 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008896 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008897 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008898 int numBdes, i;
8899 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008900 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008901 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008902 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008903
James Smart45ed1192009-10-02 15:17:02 -04008904 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008905 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008906 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008907 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008908 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008909 command_type = ELS_COMMAND_FIP;
8910 else
8911 command_type = ELS_COMMAND_NON_FIP;
8912
James Smartb5c53952016-03-31 14:12:30 -07008913 if (phba->fcp_embed_io)
8914 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008915 /* Some of the fields are in the right position already */
8916 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartae9e28f2017-05-15 15:20:51 -07008917 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8918 /* The ct field has moved so reset */
8919 wqe->generic.wqe_com.word7 = 0;
8920 wqe->generic.wqe_com.word10 = 0;
8921 }
James Smartb5c53952016-03-31 14:12:30 -07008922
8923 abort_tag = (uint32_t) iocbq->iotag;
8924 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008925 /* words0-2 bpl convert bde */
8926 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008927 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8928 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008929 bpl = (struct ulp_bde64 *)
8930 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8931 if (!bpl)
8932 return IOCB_ERROR;
8933
8934 /* Should already be byte swapped. */
8935 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8936 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8937 /* swap the size field back to the cpu so we
8938 * can assign it to the sgl.
8939 */
8940 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008941 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8942 total_len = 0;
8943 for (i = 0; i < numBdes; i++) {
8944 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8945 total_len += bde.tus.f.bdeSize;
8946 }
James Smart4f774512009-05-22 14:52:35 -04008947 } else
James Smart5ffc2662009-11-18 15:39:44 -05008948 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008949
8950 iocbq->iocb.ulpIoTag = iocbq->iotag;
8951 cmnd = iocbq->iocb.ulpCommand;
8952
8953 switch (iocbq->iocb.ulpCommand) {
8954 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008955 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8956 ndlp = iocbq->context_un.ndlp;
8957 else
8958 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008959 if (!iocbq->iocb.ulpLe) {
8960 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8961 "2007 Only Limited Edition cmd Format"
8962 " supported 0x%x\n",
8963 iocbq->iocb.ulpCommand);
8964 return IOCB_ERROR;
8965 }
James Smartff78d8f2011-12-13 13:21:35 -05008966
James Smart5ffc2662009-11-18 15:39:44 -05008967 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008968 /* Els_reguest64 has a TMO */
8969 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8970 iocbq->iocb.ulpTimeout);
8971 /* Need a VF for word 4 set the vf bit*/
8972 bf_set(els_req64_vf, &wqe->els_req, 0);
8973 /* And a VFID for word 12 */
8974 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008975 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008976 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8977 iocbq->iocb.ulpContext);
8978 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8979 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008980 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008981 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008982 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8983 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008984 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8985 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008986 if_type = bf_get(lpfc_sli_intf_if_type,
8987 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08008988 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008989 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008990 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008991 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008992 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008993 *pcmd == ELS_CMD_PLOGI)) {
8994 bf_set(els_req64_sp, &wqe->els_req, 1);
8995 bf_set(els_req64_sid, &wqe->els_req,
8996 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008997 if ((*pcmd == ELS_CMD_FLOGI) &&
8998 !(phba->fc_topology ==
8999 LPFC_TOPOLOGY_LOOP))
9000 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05009001 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9002 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04009003 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05009004 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05009005 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9006 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9007 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9008 }
James Smartc8685952009-11-18 15:39:16 -05009009 }
James Smart6d368e52011-05-24 11:44:12 -04009010 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9011 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009012 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9013 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9014 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9015 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9016 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9017 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009018 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04009019 break;
James Smart5ffc2662009-11-18 15:39:44 -05009020 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04009021 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9022 iocbq->iocb.un.ulpWord[3]);
9023 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009024 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05009025 /* The entire sequence is transmitted for this IOCB */
9026 xmit_len = total_len;
9027 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05009028 if (phba->link_flag & LS_LOOPBACK_MODE)
9029 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04009030 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04009031 /* word3 iocb=io_tag32 wqe=reserved */
9032 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04009033 /* word4 relative_offset memcpy */
9034 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009035 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9036 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9037 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9038 LPFC_WQE_IOD_WRITE);
9039 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9040 LPFC_WQE_LENLOC_WORD12);
9041 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05009042 wqe->xmit_sequence.xmit_len = xmit_len;
9043 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009044 break;
James Smart4f774512009-05-22 14:52:35 -04009045 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04009046 /* word3 iocb=iotag32 wqe=seq_payload_len */
9047 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009048 /* word4 iocb=rsvd wqe=rsvd */
9049 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9050 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04009051 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009052 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04009053 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9054 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9055 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9056 LPFC_WQE_LENLOC_WORD3);
9057 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04009058 break;
James Smart4f774512009-05-22 14:52:35 -04009059 case CMD_FCP_IWRITE64_CR:
9060 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04009061 /* word3 iocb=iotag wqe=payload_offset_len */
9062 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009063 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9064 xmit_len + sizeof(struct fcp_rsp));
9065 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9066 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009067 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9068 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9069 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9070 iocbq->iocb.ulpFCP2Rcvy);
9071 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9072 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009073 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9074 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9075 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009076 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009077 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009078 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9079 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009080 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9081 if (iocbq->priority) {
9082 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9083 (iocbq->priority << 1));
9084 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009085 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9086 (phba->cfg_XLanePriority << 1));
9087 }
9088 }
James Smartb5c53952016-03-31 14:12:30 -07009089 /* Note, word 10 is already initialized to 0 */
9090
James Smart0bc2b7c2018-02-22 08:18:48 -08009091 /* Don't set PBDE for Perf hints, just fcp_embed_pbde */
9092 if (phba->fcp_embed_pbde)
9093 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9094 else
9095 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9096
James Smartb5c53952016-03-31 14:12:30 -07009097 if (phba->fcp_embed_io) {
9098 struct lpfc_scsi_buf *lpfc_cmd;
9099 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009100 struct fcp_cmnd *fcp_cmnd;
9101 uint32_t *ptr;
9102
9103 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009104
9105 lpfc_cmd = iocbq->context1;
9106 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9107 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9108
9109 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009110 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009111 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009112 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9113 wqe->generic.bde.addrHigh = 0;
9114 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009115
James Smart205e8242018-03-05 12:04:03 -08009116 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9117 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009118
9119 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009120 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009121 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9122 }
James Smart7851fe22011-07-22 18:36:52 -04009123 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04009124 case CMD_FCP_IREAD64_CR:
9125 /* word3 iocb=iotag wqe=payload_offset_len */
9126 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009127 bf_set(payload_offset_len, &wqe->fcp_iread,
9128 xmit_len + sizeof(struct fcp_rsp));
9129 bf_set(cmd_buff_len, &wqe->fcp_iread,
9130 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009131 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9132 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9133 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9134 iocbq->iocb.ulpFCP2Rcvy);
9135 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04009136 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009137 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9138 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9139 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009140 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009141 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009142 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9143 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009144 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9145 if (iocbq->priority) {
9146 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9147 (iocbq->priority << 1));
9148 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009149 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9150 (phba->cfg_XLanePriority << 1));
9151 }
9152 }
James Smartb5c53952016-03-31 14:12:30 -07009153 /* Note, word 10 is already initialized to 0 */
9154
James Smart0bc2b7c2018-02-22 08:18:48 -08009155 /* Don't set PBDE for Perf hints, just fcp_embed_pbde */
9156 if (phba->fcp_embed_pbde)
9157 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9158 else
9159 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9160
James Smartb5c53952016-03-31 14:12:30 -07009161 if (phba->fcp_embed_io) {
9162 struct lpfc_scsi_buf *lpfc_cmd;
9163 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009164 struct fcp_cmnd *fcp_cmnd;
9165 uint32_t *ptr;
9166
9167 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009168
9169 lpfc_cmd = iocbq->context1;
9170 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9171 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9172
9173 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009174 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009175 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009176 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9177 wqe->generic.bde.addrHigh = 0;
9178 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009179
James Smart205e8242018-03-05 12:04:03 -08009180 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9181 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009182
9183 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009184 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009185 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9186 }
James Smart7851fe22011-07-22 18:36:52 -04009187 break;
James Smartf1126682009-06-10 17:22:44 -04009188 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04009189 /* word3 iocb=iotag wqe=payload_offset_len */
9190 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9191 bf_set(payload_offset_len, &wqe->fcp_icmd,
9192 xmit_len + sizeof(struct fcp_rsp));
9193 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9194 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009195 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04009196 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04009197 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009198 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9199 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9200 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9201 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9202 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04009203 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9204 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009205 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9206 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009207 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9208 if (iocbq->priority) {
9209 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9210 (iocbq->priority << 1));
9211 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009212 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9213 (phba->cfg_XLanePriority << 1));
9214 }
9215 }
James Smartb5c53952016-03-31 14:12:30 -07009216 /* Note, word 10 is already initialized to 0 */
9217
9218 if (phba->fcp_embed_io) {
9219 struct lpfc_scsi_buf *lpfc_cmd;
9220 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009221 struct fcp_cmnd *fcp_cmnd;
9222 uint32_t *ptr;
9223
9224 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009225
9226 lpfc_cmd = iocbq->context1;
9227 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9228 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9229
9230 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009231 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009232 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009233 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9234 wqe->generic.bde.addrHigh = 0;
9235 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009236
James Smart205e8242018-03-05 12:04:03 -08009237 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9238 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009239
9240 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009241 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009242 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9243 }
James Smart7851fe22011-07-22 18:36:52 -04009244 break;
James Smart4f774512009-05-22 14:52:35 -04009245 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009246 /* For this command calculate the xmit length of the
9247 * request bde.
9248 */
9249 xmit_len = 0;
9250 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9251 sizeof(struct ulp_bde64);
9252 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009253 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009254 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9255 break;
James Smart63e801c2010-11-20 23:14:19 -05009256 xmit_len += bde.tus.f.bdeSize;
9257 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009258 /* word3 iocb=IO_TAG wqe=request_payload_len */
9259 wqe->gen_req.request_payload_len = xmit_len;
9260 /* word4 iocb=parameter wqe=relative_offset memcpy */
9261 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009262 /* word6 context tag copied in memcpy */
9263 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9264 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9265 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9266 "2015 Invalid CT %x command 0x%x\n",
9267 ct, iocbq->iocb.ulpCommand);
9268 return IOCB_ERROR;
9269 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009270 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9271 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9272 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9273 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9274 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9275 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9276 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9277 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009278 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009279 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009280 break;
James Smart4f774512009-05-22 14:52:35 -04009281 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009282 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009283 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009284 /* word3 iocb=iotag32 wqe=response_payload_len */
9285 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009286 /* word4 */
9287 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009288 /* word5 iocb=rsvd wge=did */
9289 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009290 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9291
9292 if_type = bf_get(lpfc_sli_intf_if_type,
9293 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009294 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -04009295 if (iocbq->vport->fc_flag & FC_PT2PT) {
9296 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9297 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9298 iocbq->vport->fc_myDID);
9299 if (iocbq->vport->fc_myDID == Fabric_DID) {
9300 bf_set(wqe_els_did,
9301 &wqe->xmit_els_rsp.wqe_dest, 0);
9302 }
9303 }
9304 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009305 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9306 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9307 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9308 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009309 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009310 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009311 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009312 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009313 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9314 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9315 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9316 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9317 LPFC_WQE_LENLOC_WORD3);
9318 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009319 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9320 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009321 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9322 iocbq->context2)->virt);
9323 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009324 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9325 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009326 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009327 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9328 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009329 phba->vpi_ids[phba->pport->vpi]);
9330 }
James Smart4f774512009-05-22 14:52:35 -04009331 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009332 break;
James Smart4f774512009-05-22 14:52:35 -04009333 case CMD_CLOSE_XRI_CN:
9334 case CMD_ABORT_XRI_CN:
9335 case CMD_ABORT_XRI_CX:
9336 /* words 0-2 memcpy should be 0 rserved */
9337 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009338 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9339 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9340 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9341 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9342 } else
9343 fip = 0;
9344
9345 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009346 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009347 * The link is down, or the command was ELS_FIP
9348 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009349 * on the wire.
9350 */
9351 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9352 else
9353 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9354 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009355 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9356 wqe->abort_cmd.rsrvd5 = 0;
9357 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009358 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9359 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009360 /*
9361 * The abort handler will send us CMD_ABORT_XRI_CN or
9362 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9363 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009364 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9365 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9366 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9367 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009368 cmnd = CMD_ABORT_XRI_CX;
9369 command_type = OTHER_COMMAND;
9370 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009371 break;
James Smart6669f9b2009-10-02 15:16:45 -04009372 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009373 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009374 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009375 * we re-construct this WQE here based on information in
9376 * iocbq from scratch.
9377 */
9378 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009379 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009380 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009381 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9382 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009383 LPFC_ABTS_UNSOL_INT) {
9384 /* ABTS sent by initiator to CT exchange, the
9385 * RX_ID field will be filled with the newly
9386 * allocated responder XRI.
9387 */
9388 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9389 iocbq->sli4_xritag);
9390 } else {
9391 /* ABTS sent by responder to CT exchange, the
9392 * RX_ID field will be filled with the responder
9393 * RX_ID from ABTS.
9394 */
9395 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009396 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009397 }
James Smart6669f9b2009-10-02 15:16:45 -04009398 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9399 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009400
9401 /* Use CT=VPI */
9402 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9403 ndlp->nlp_DID);
9404 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9405 iocbq->iocb.ulpContext);
9406 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009407 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009408 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009409 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9410 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9411 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009412 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9413 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009414 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9415 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9416 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9417 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9418 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9419 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9420 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9421 }
9422
James Smart7851fe22011-07-22 18:36:52 -04009423 break;
James Smartae9e28f2017-05-15 15:20:51 -07009424 case CMD_SEND_FRAME:
9425 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9426 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9427 return 0;
James Smart4f774512009-05-22 14:52:35 -04009428 case CMD_XRI_ABORTED_CX:
9429 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009430 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9431 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9432 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9433 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9434 default:
9435 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9436 "2014 Invalid command 0x%x\n",
9437 iocbq->iocb.ulpCommand);
9438 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009439 break;
James Smart4f774512009-05-22 14:52:35 -04009440 }
James Smart6d368e52011-05-24 11:44:12 -04009441
James Smart8012cc32012-10-31 14:44:49 -04009442 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9443 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9444 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9445 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9446 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9447 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9448 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9449 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009450 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9451 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9452 wqe->generic.wqe_com.abort_tag = abort_tag;
9453 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9454 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9455 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9456 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009457 return 0;
9458}
9459
9460/**
9461 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9462 * @phba: Pointer to HBA context object.
9463 * @ring_number: SLI ring number to issue iocb on.
9464 * @piocb: Pointer to command iocb.
9465 * @flag: Flag indicating if this command can be put into txq.
9466 *
9467 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9468 * an iocb command to an HBA with SLI-4 interface spec.
9469 *
9470 * This function is called with hbalock held. The function will return success
9471 * after it successfully submit the iocb to firmware or after adding to the
9472 * txq.
9473 **/
9474static int
9475__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9476 struct lpfc_iocbq *piocb, uint32_t flag)
9477{
9478 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -08009479 union lpfc_wqe128 wqe;
James Smart1ba981f2014-02-20 09:56:45 -05009480 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009481 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009482
James Smart895427b2017-02-12 13:52:30 -08009483 /* Get the WQ */
9484 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9485 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9486 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9487 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9488 else
9489 wq = phba->sli4_hba.oas_wq;
9490 } else {
9491 wq = phba->sli4_hba.els_wq;
9492 }
9493
9494 /* Get corresponding ring */
9495 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009496
James Smartb5c53952016-03-31 14:12:30 -07009497 /*
9498 * The WQE can be either 64 or 128 bytes,
James Smartb5c53952016-03-31 14:12:30 -07009499 */
James Smartb5c53952016-03-31 14:12:30 -07009500
James Smart895427b2017-02-12 13:52:30 -08009501 lockdep_assert_held(&phba->hbalock);
9502
James Smart4f774512009-05-22 14:52:35 -04009503 if (piocb->sli4_xritag == NO_XRI) {
9504 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009505 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009506 sglq = NULL;
9507 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009508 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009509 if (!(flag & SLI_IOCB_RET_IOCB)) {
9510 __lpfc_sli_ringtx_put(phba,
9511 pring, piocb);
9512 return IOCB_SUCCESS;
9513 } else {
9514 return IOCB_BUSY;
9515 }
9516 } else {
James Smart895427b2017-02-12 13:52:30 -08009517 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009518 if (!sglq) {
9519 if (!(flag & SLI_IOCB_RET_IOCB)) {
9520 __lpfc_sli_ringtx_put(phba,
9521 pring,
9522 piocb);
9523 return IOCB_SUCCESS;
9524 } else
9525 return IOCB_BUSY;
9526 }
9527 }
James Smart4f774512009-05-22 14:52:35 -04009528 }
James Smart2ea259e2017-02-12 13:52:27 -08009529 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009530 /* These IO's already have an XRI and a mapped sgl. */
9531 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009532 else {
James Smart6d368e52011-05-24 11:44:12 -04009533 /*
9534 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009535 * sglq is on the active list
9536 */
James Smartedccdc12013-01-03 15:43:45 -05009537 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009538 if (!sglq)
9539 return IOCB_ERROR;
9540 }
9541
9542 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009543 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009544 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009545 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009546 return IOCB_ERROR;
9547 }
9548
James Smart205e8242018-03-05 12:04:03 -08009549 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
James Smart4f774512009-05-22 14:52:35 -04009550 return IOCB_ERROR;
9551
James Smart205e8242018-03-05 12:04:03 -08009552 if (lpfc_sli4_wq_put(wq, &wqe))
James Smart895427b2017-02-12 13:52:30 -08009553 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009554 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9555
9556 return 0;
9557}
9558
9559/**
James Smart3772a992009-05-22 14:50:54 -04009560 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9561 *
9562 * This routine wraps the actual lockless version for issusing IOCB function
9563 * pointer from the lpfc_hba struct.
9564 *
9565 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009566 * IOCB_ERROR - Error
9567 * IOCB_SUCCESS - Success
9568 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009569 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009570int
James Smart3772a992009-05-22 14:50:54 -04009571__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9572 struct lpfc_iocbq *piocb, uint32_t flag)
9573{
9574 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9575}
9576
9577/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009578 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009579 * @phba: The hba struct for which this call is being executed.
9580 * @dev_grp: The HBA PCI-Device group number.
9581 *
9582 * This routine sets up the SLI interface API function jump table in @phba
9583 * struct.
9584 * Returns: 0 - success, -ENODEV - failure.
9585 **/
9586int
9587lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9588{
9589
9590 switch (dev_grp) {
9591 case LPFC_PCI_DEV_LP:
9592 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9593 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9594 break;
James Smart4f774512009-05-22 14:52:35 -04009595 case LPFC_PCI_DEV_OC:
9596 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9597 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9598 break;
James Smart3772a992009-05-22 14:50:54 -04009599 default:
9600 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9601 "1419 Invalid HBA PCI-device group: 0x%x\n",
9602 dev_grp);
9603 return -ENODEV;
9604 break;
9605 }
9606 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9607 return 0;
9608}
James Smart92d7f7b2007-06-17 19:56:38 -05009609
James Smarta1efe162015-05-21 13:55:20 -04009610/**
James Smart895427b2017-02-12 13:52:30 -08009611 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009612 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009613 * @piocb: Pointer to command iocb.
9614 *
James Smart895427b2017-02-12 13:52:30 -08009615 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9616 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009617 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009618 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009619 */
James Smart895427b2017-02-12 13:52:30 -08009620struct lpfc_sli_ring *
9621lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009622{
James Smart895427b2017-02-12 13:52:30 -08009623 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smart8b0dff12015-05-22 10:42:38 -04009624 if (!(phba->cfg_fof) ||
James Smart895427b2017-02-12 13:52:30 -08009625 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
James Smart8b0dff12015-05-22 10:42:38 -04009626 if (unlikely(!phba->sli4_hba.fcp_wq))
James Smart895427b2017-02-12 13:52:30 -08009627 return NULL;
James Smart8b0dff12015-05-22 10:42:38 -04009628 /*
James Smart895427b2017-02-12 13:52:30 -08009629 * for abort iocb hba_wqidx should already
James Smart8b0dff12015-05-22 10:42:38 -04009630 * be setup based on what work queue we used.
9631 */
Dick Kennedy8e036a92017-09-29 17:34:35 -07009632 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart895427b2017-02-12 13:52:30 -08009633 piocb->hba_wqidx =
James Smart8b0dff12015-05-22 10:42:38 -04009634 lpfc_sli4_scmd_to_wqidx_distr(phba,
9635 piocb->context1);
Dick Kennedy8e036a92017-09-29 17:34:35 -07009636 piocb->hba_wqidx = piocb->hba_wqidx %
9637 phba->cfg_fcp_io_channel;
9638 }
James Smart895427b2017-02-12 13:52:30 -08009639 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
James Smart8b0dff12015-05-22 10:42:38 -04009640 } else {
9641 if (unlikely(!phba->sli4_hba.oas_wq))
James Smart895427b2017-02-12 13:52:30 -08009642 return NULL;
9643 piocb->hba_wqidx = 0;
9644 return phba->sli4_hba.oas_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009645 }
James Smart895427b2017-02-12 13:52:30 -08009646 } else {
9647 if (unlikely(!phba->sli4_hba.els_wq))
9648 return NULL;
9649 piocb->hba_wqidx = 0;
9650 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009651 }
James Smart9bd2bff52014-09-03 12:57:30 -04009652}
9653
James Smarte59058c2008-08-24 21:49:00 -04009654/**
James Smart3621a712009-04-06 18:47:14 -04009655 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009656 * @phba: Pointer to HBA context object.
9657 * @pring: Pointer to driver SLI ring object.
9658 * @piocb: Pointer to command iocb.
9659 * @flag: Flag indicating if this command can be put into txq.
9660 *
9661 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9662 * function. This function gets the hbalock and calls
9663 * __lpfc_sli_issue_iocb function and will return the error returned
9664 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9665 * functions which do not hold hbalock.
9666 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009667int
James Smart3772a992009-05-22 14:50:54 -04009668lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009669 struct lpfc_iocbq *piocb, uint32_t flag)
9670{
James Smart895427b2017-02-12 13:52:30 -08009671 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009672 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009673 struct lpfc_queue *fpeq;
9674 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009675 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009676 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009677
James Smart7e56aa22012-08-03 12:35:34 -04009678 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08009679 pring = lpfc_sli4_calc_ring(phba, piocb);
9680 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -04009681 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -04009682
James Smart9bd2bff52014-09-03 12:57:30 -04009683 spin_lock_irqsave(&pring->ring_lock, iflags);
9684 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9685 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009686
James Smart9bd2bff52014-09-03 12:57:30 -04009687 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -08009688 idx = piocb->hba_wqidx;
9689 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009690
James Smart895427b2017-02-12 13:52:30 -08009691 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009692
James Smart9bd2bff52014-09-03 12:57:30 -04009693 /* Get associated EQ with this index */
9694 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009695
James Smart9bd2bff52014-09-03 12:57:30 -04009696 /* Turn off interrupts from this EQ */
James Smartb71413d2018-02-22 08:18:40 -08009697 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009698
James Smart9bd2bff52014-09-03 12:57:30 -04009699 /*
9700 * Process all the events on FCP EQ
9701 */
9702 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9703 lpfc_sli4_hba_handle_eqe(phba,
9704 eqe, idx);
9705 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009706 }
James Smartba20c852012-08-03 12:36:52 -04009707
James Smart9bd2bff52014-09-03 12:57:30 -04009708 /* Always clear and re-arm the EQ */
James Smartb71413d2018-02-22 08:18:40 -08009709 phba->sli4_hba.sli4_eq_release(fpeq,
James Smart9bd2bff52014-09-03 12:57:30 -04009710 LPFC_QUEUE_REARM);
9711 }
James Smart895427b2017-02-12 13:52:30 -08009712 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009713 }
James Smart7e56aa22012-08-03 12:35:34 -04009714 } else {
9715 /* For now, SLI2/3 will still use hbalock */
9716 spin_lock_irqsave(&phba->hbalock, iflags);
9717 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9718 spin_unlock_irqrestore(&phba->hbalock, iflags);
9719 }
James Smart92d7f7b2007-06-17 19:56:38 -05009720 return rc;
9721}
9722
James Smarte59058c2008-08-24 21:49:00 -04009723/**
James Smart3621a712009-04-06 18:47:14 -04009724 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009725 * @phba: Pointer to HBA context object.
9726 *
9727 * This function is called while driver attaches with the
9728 * HBA to setup the extra ring. The extra ring is used
9729 * only when driver needs to support target mode functionality
9730 * or IP over FC functionalities.
9731 *
James Smart895427b2017-02-12 13:52:30 -08009732 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04009733 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009734static int
9735lpfc_extra_ring_setup( struct lpfc_hba *phba)
9736{
9737 struct lpfc_sli *psli;
9738 struct lpfc_sli_ring *pring;
9739
9740 psli = &phba->sli;
9741
9742 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009743
9744 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -08009745 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -04009746 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9747 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9748 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9749 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009750
James Smarta4bc3372006-12-02 13:34:16 -05009751 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -08009752 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -05009753
James Smart7e56aa22012-08-03 12:35:34 -04009754 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9755 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9756 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9757 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009758
9759 /* Setup default profile for this ring */
9760 pring->iotag_max = 4096;
9761 pring->num_mask = 1;
9762 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009763 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9764 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009765 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9766 return 0;
9767}
9768
James Smartcb69f7d2011-12-13 13:21:57 -05009769/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9770 * @phba: Pointer to HBA context object.
9771 * @iocbq: Pointer to iocb object.
9772 *
9773 * The async_event handler calls this routine when it receives
9774 * an ASYNC_STATUS_CN event from the port. The port generates
9775 * this event when an Abort Sequence request to an rport fails
9776 * twice in succession. The abort could be originated by the
9777 * driver or by the port. The ABTS could have been for an ELS
9778 * or FCP IO. The port only generates this event when an ABTS
9779 * fails to complete after one retry.
9780 */
9781static void
9782lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9783 struct lpfc_iocbq *iocbq)
9784{
9785 struct lpfc_nodelist *ndlp = NULL;
9786 uint16_t rpi = 0, vpi = 0;
9787 struct lpfc_vport *vport = NULL;
9788
9789 /* The rpi in the ulpContext is vport-sensitive. */
9790 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9791 rpi = iocbq->iocb.ulpContext;
9792
9793 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9794 "3092 Port generated ABTS async event "
9795 "on vpi %d rpi %d status 0x%x\n",
9796 vpi, rpi, iocbq->iocb.ulpStatus);
9797
9798 vport = lpfc_find_vport_by_vpid(phba, vpi);
9799 if (!vport)
9800 goto err_exit;
9801 ndlp = lpfc_findnode_rpi(vport, rpi);
9802 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9803 goto err_exit;
9804
9805 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9806 lpfc_sli_abts_recover_port(vport, ndlp);
9807 return;
9808
9809 err_exit:
9810 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9811 "3095 Event Context not found, no "
9812 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9813 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9814 vpi, rpi);
9815}
9816
9817/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9818 * @phba: pointer to HBA context object.
9819 * @ndlp: nodelist pointer for the impacted rport.
9820 * @axri: pointer to the wcqe containing the failed exchange.
9821 *
9822 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9823 * port. The port generates this event when an abort exchange request to an
9824 * rport fails twice in succession with no reply. The abort could be originated
9825 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9826 */
9827void
9828lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9829 struct lpfc_nodelist *ndlp,
9830 struct sli4_wcqe_xri_aborted *axri)
9831{
9832 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009833 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009834
James Smart6b5151f2012-01-18 16:24:06 -05009835 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009836 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9837 "3115 Node Context not found, driver "
9838 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009839 return;
9840 }
9841
James Smartcb69f7d2011-12-13 13:21:57 -05009842 vport = ndlp->vport;
9843 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9844 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009845 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009846 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009847 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009848 bf_get(lpfc_wcqe_xa_status, axri),
9849 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009850
James Smart5c1db2a2012-03-01 22:34:36 -05009851 /*
9852 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9853 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9854 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9855 */
James Smarte3d2b802012-08-14 14:25:43 -04009856 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009857 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9858 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009859 lpfc_sli_abts_recover_port(vport, ndlp);
9860}
9861
James Smarte59058c2008-08-24 21:49:00 -04009862/**
James Smart3621a712009-04-06 18:47:14 -04009863 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009864 * @phba: Pointer to HBA context object.
9865 * @pring: Pointer to driver SLI ring object.
9866 * @iocbq: Pointer to iocb object.
9867 *
9868 * This function is called by the slow ring event handler
9869 * function when there is an ASYNC event iocb in the ring.
9870 * This function is called with no lock held.
9871 * Currently this function handles only temperature related
9872 * ASYNC events. The function decodes the temperature sensor
9873 * event message and posts events for the management applications.
9874 **/
James Smart98c9ea52007-10-27 13:37:33 -04009875static void
James Smart57127f12007-10-27 13:37:05 -04009876lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9877 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9878{
9879 IOCB_t *icmd;
9880 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009881 struct temp_event temp_event_data;
9882 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009883 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009884
9885 icmd = &iocbq->iocb;
9886 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009887
James Smartcb69f7d2011-12-13 13:21:57 -05009888 switch (evt_code) {
9889 case ASYNC_TEMP_WARN:
9890 case ASYNC_TEMP_SAFE:
9891 temp_event_data.data = (uint32_t) icmd->ulpContext;
9892 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9893 if (evt_code == ASYNC_TEMP_WARN) {
9894 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9895 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9896 "0347 Adapter is very hot, please take "
9897 "corrective action. temperature : %d Celsius\n",
9898 (uint32_t) icmd->ulpContext);
9899 } else {
9900 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9901 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9902 "0340 Adapter temperature is OK now. "
9903 "temperature : %d Celsius\n",
9904 (uint32_t) icmd->ulpContext);
9905 }
9906
9907 /* Send temperature change event to applications */
9908 shost = lpfc_shost_from_vport(phba->pport);
9909 fc_host_post_vendor_event(shost, fc_get_event_number(),
9910 sizeof(temp_event_data), (char *) &temp_event_data,
9911 LPFC_NL_VENDOR_ID);
9912 break;
9913 case ASYNC_STATUS_CN:
9914 lpfc_sli_abts_err_handler(phba, iocbq);
9915 break;
9916 default:
James Smarta257bf92009-04-06 18:48:10 -04009917 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009918 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009919 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009920 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009921 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9922 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9923 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9924 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009925 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009926 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9927 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9928 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9929 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9930
James Smartcb69f7d2011-12-13 13:21:57 -05009931 break;
James Smart57127f12007-10-27 13:37:05 -04009932 }
James Smart57127f12007-10-27 13:37:05 -04009933}
9934
9935
James Smarte59058c2008-08-24 21:49:00 -04009936/**
James Smart895427b2017-02-12 13:52:30 -08009937 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009938 * @phba: Pointer to HBA context object.
9939 *
9940 * lpfc_sli_setup sets up rings of the SLI interface with
9941 * number of iocbs per ring and iotags. This function is
9942 * called while driver attach to the HBA and before the
9943 * interrupts are enabled. So there is no need for locking.
9944 *
9945 * This function always returns 0.
9946 **/
dea31012005-04-17 16:05:31 -05009947int
James Smart895427b2017-02-12 13:52:30 -08009948lpfc_sli4_setup(struct lpfc_hba *phba)
9949{
9950 struct lpfc_sli_ring *pring;
9951
9952 pring = phba->sli4_hba.els_wq->pring;
9953 pring->num_mask = LPFC_MAX_RING_MASK;
9954 pring->prt[0].profile = 0; /* Mask 0 */
9955 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9956 pring->prt[0].type = FC_TYPE_ELS;
9957 pring->prt[0].lpfc_sli_rcv_unsol_event =
9958 lpfc_els_unsol_event;
9959 pring->prt[1].profile = 0; /* Mask 1 */
9960 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9961 pring->prt[1].type = FC_TYPE_ELS;
9962 pring->prt[1].lpfc_sli_rcv_unsol_event =
9963 lpfc_els_unsol_event;
9964 pring->prt[2].profile = 0; /* Mask 2 */
9965 /* NameServer Inquiry */
9966 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9967 /* NameServer */
9968 pring->prt[2].type = FC_TYPE_CT;
9969 pring->prt[2].lpfc_sli_rcv_unsol_event =
9970 lpfc_ct_unsol_event;
9971 pring->prt[3].profile = 0; /* Mask 3 */
9972 /* NameServer response */
9973 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9974 /* NameServer */
9975 pring->prt[3].type = FC_TYPE_CT;
9976 pring->prt[3].lpfc_sli_rcv_unsol_event =
9977 lpfc_ct_unsol_event;
9978 return 0;
9979}
9980
9981/**
9982 * lpfc_sli_setup - SLI ring setup function
9983 * @phba: Pointer to HBA context object.
9984 *
9985 * lpfc_sli_setup sets up rings of the SLI interface with
9986 * number of iocbs per ring and iotags. This function is
9987 * called while driver attach to the HBA and before the
9988 * interrupts are enabled. So there is no need for locking.
9989 *
9990 * This function always returns 0. SLI3 only.
9991 **/
9992int
dea31012005-04-17 16:05:31 -05009993lpfc_sli_setup(struct lpfc_hba *phba)
9994{
James Smarted957682007-06-17 19:56:37 -05009995 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009996 struct lpfc_sli *psli = &phba->sli;
9997 struct lpfc_sli_ring *pring;
9998
James Smart2a76a282012-08-03 12:35:54 -04009999 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -050010000 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -050010001
James Bottomley604a3e32005-10-29 10:28:33 -050010002 psli->iocbq_lookup = NULL;
10003 psli->iocbq_lookup_len = 0;
10004 psli->last_iotag = 0;
10005
dea31012005-04-17 16:05:31 -050010006 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010007 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010008 switch (i) {
10009 case LPFC_FCP_RING: /* ring 0 - FCP */
10010 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010011 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10012 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10013 pring->sli.sli3.numCiocb +=
10014 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10015 pring->sli.sli3.numRiocb +=
10016 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10017 pring->sli.sli3.numCiocb +=
10018 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10019 pring->sli.sli3.numRiocb +=
10020 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10021 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010022 SLI3_IOCB_CMD_SIZE :
10023 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010024 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010025 SLI3_IOCB_RSP_SIZE :
10026 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010027 pring->iotag_ctr = 0;
10028 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -050010029 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -050010030 pring->fast_iotag = pring->iotag_max;
10031 pring->num_mask = 0;
10032 break;
James Smarta4bc3372006-12-02 13:34:16 -050010033 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -050010034 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010035 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10036 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10037 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010038 SLI3_IOCB_CMD_SIZE :
10039 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010040 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010041 SLI3_IOCB_RSP_SIZE :
10042 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -050010043 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -050010044 pring->num_mask = 0;
10045 break;
10046 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10047 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010048 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10049 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10050 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010051 SLI3_IOCB_CMD_SIZE :
10052 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010053 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010054 SLI3_IOCB_RSP_SIZE :
10055 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010056 pring->fast_iotag = 0;
10057 pring->iotag_ctr = 0;
10058 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -040010059 pring->lpfc_sli_rcv_async_status =
10060 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -040010061 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -050010062 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -040010063 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10064 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010065 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010066 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010067 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -040010068 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10069 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010070 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010071 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010072 pring->prt[2].profile = 0; /* Mask 2 */
10073 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -040010074 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -050010075 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010076 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010077 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010078 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010079 pring->prt[3].profile = 0; /* Mask 3 */
10080 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -040010081 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -050010082 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010083 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010084 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010085 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010086 break;
10087 }
James Smart7e56aa22012-08-03 12:35:34 -040010088 totiocbsize += (pring->sli.sli3.numCiocb *
10089 pring->sli.sli3.sizeCiocb) +
10090 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050010091 }
James Smarted957682007-06-17 19:56:37 -050010092 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050010093 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040010094 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10095 "SLI2 SLIM Data: x%x x%lx\n",
10096 phba->brd_no, totiocbsize,
10097 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050010098 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010099 if (phba->cfg_multi_ring_support == 2)
10100 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050010101
10102 return 0;
10103}
10104
James Smarte59058c2008-08-24 21:49:00 -040010105/**
James Smart895427b2017-02-12 13:52:30 -080010106 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040010107 * @phba: Pointer to HBA context object.
10108 *
James Smart895427b2017-02-12 13:52:30 -080010109 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040010110 * ring. This function also initializes ring indices of each ring.
10111 * This function is called during the initialization of the SLI
10112 * interface of an HBA.
10113 * This function is called with no lock held and always returns
10114 * 1.
10115 **/
James Smart895427b2017-02-12 13:52:30 -080010116void
10117lpfc_sli4_queue_init(struct lpfc_hba *phba)
10118{
10119 struct lpfc_sli *psli;
10120 struct lpfc_sli_ring *pring;
10121 int i;
10122
10123 psli = &phba->sli;
10124 spin_lock_irq(&phba->hbalock);
10125 INIT_LIST_HEAD(&psli->mboxq);
10126 INIT_LIST_HEAD(&psli->mboxq_cmpl);
10127 /* Initialize list headers for txq and txcmplq as double linked lists */
10128 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
10129 pring = phba->sli4_hba.fcp_wq[i]->pring;
10130 pring->flag = 0;
10131 pring->ringno = LPFC_FCP_RING;
10132 INIT_LIST_HEAD(&pring->txq);
10133 INIT_LIST_HEAD(&pring->txcmplq);
10134 INIT_LIST_HEAD(&pring->iocb_continueq);
10135 spin_lock_init(&pring->ring_lock);
10136 }
10137 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
10138 pring = phba->sli4_hba.nvme_wq[i]->pring;
10139 pring->flag = 0;
10140 pring->ringno = LPFC_FCP_RING;
10141 INIT_LIST_HEAD(&pring->txq);
10142 INIT_LIST_HEAD(&pring->txcmplq);
10143 INIT_LIST_HEAD(&pring->iocb_continueq);
10144 spin_lock_init(&pring->ring_lock);
10145 }
10146 pring = phba->sli4_hba.els_wq->pring;
10147 pring->flag = 0;
10148 pring->ringno = LPFC_ELS_RING;
10149 INIT_LIST_HEAD(&pring->txq);
10150 INIT_LIST_HEAD(&pring->txcmplq);
10151 INIT_LIST_HEAD(&pring->iocb_continueq);
10152 spin_lock_init(&pring->ring_lock);
10153
10154 if (phba->cfg_nvme_io_channel) {
10155 pring = phba->sli4_hba.nvmels_wq->pring;
10156 pring->flag = 0;
10157 pring->ringno = LPFC_ELS_RING;
10158 INIT_LIST_HEAD(&pring->txq);
10159 INIT_LIST_HEAD(&pring->txcmplq);
10160 INIT_LIST_HEAD(&pring->iocb_continueq);
10161 spin_lock_init(&pring->ring_lock);
10162 }
10163
10164 if (phba->cfg_fof) {
10165 pring = phba->sli4_hba.oas_wq->pring;
10166 pring->flag = 0;
10167 pring->ringno = LPFC_FCP_RING;
10168 INIT_LIST_HEAD(&pring->txq);
10169 INIT_LIST_HEAD(&pring->txcmplq);
10170 INIT_LIST_HEAD(&pring->iocb_continueq);
10171 spin_lock_init(&pring->ring_lock);
10172 }
10173
10174 spin_unlock_irq(&phba->hbalock);
10175}
10176
10177/**
10178 * lpfc_sli_queue_init - Queue initialization function
10179 * @phba: Pointer to HBA context object.
10180 *
10181 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10182 * ring. This function also initializes ring indices of each ring.
10183 * This function is called during the initialization of the SLI
10184 * interface of an HBA.
10185 * This function is called with no lock held and always returns
10186 * 1.
10187 **/
10188void
10189lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010190{
10191 struct lpfc_sli *psli;
10192 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050010193 int i;
dea31012005-04-17 16:05:31 -050010194
10195 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050010196 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010197 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050010198 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050010199 /* Initialize list headers for txq and txcmplq as double linked lists */
10200 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010201 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010202 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040010203 pring->sli.sli3.next_cmdidx = 0;
10204 pring->sli.sli3.local_getidx = 0;
10205 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050010206 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050010207 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050010208 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080010209 pring->flag = 0;
10210 INIT_LIST_HEAD(&pring->txq);
10211 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010212 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010213 }
James Smart2e0fef82007-06-17 19:56:36 -050010214 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010215}
10216
James Smarte59058c2008-08-24 21:49:00 -040010217/**
James Smart04c68492009-05-22 14:52:52 -040010218 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10219 * @phba: Pointer to HBA context object.
10220 *
10221 * This routine flushes the mailbox command subsystem. It will unconditionally
10222 * flush all the mailbox commands in the three possible stages in the mailbox
10223 * command sub-system: pending mailbox command queue; the outstanding mailbox
10224 * command; and completed mailbox command queue. It is caller's responsibility
10225 * to make sure that the driver is in the proper state to flush the mailbox
10226 * command sub-system. Namely, the posting of mailbox commands into the
10227 * pending mailbox command queue from the various clients must be stopped;
10228 * either the HBA is in a state that it will never works on the outstanding
10229 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10230 * mailbox command has been completed.
10231 **/
10232static void
10233lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10234{
10235 LIST_HEAD(completions);
10236 struct lpfc_sli *psli = &phba->sli;
10237 LPFC_MBOXQ_t *pmb;
10238 unsigned long iflag;
10239
10240 /* Flush all the mailbox commands in the mbox system */
10241 spin_lock_irqsave(&phba->hbalock, iflag);
10242 /* The pending mailbox command queue */
10243 list_splice_init(&phba->sli.mboxq, &completions);
10244 /* The outstanding active mailbox command */
10245 if (psli->mbox_active) {
10246 list_add_tail(&psli->mbox_active->list, &completions);
10247 psli->mbox_active = NULL;
10248 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10249 }
10250 /* The completed mailbox command queue */
10251 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10252 spin_unlock_irqrestore(&phba->hbalock, iflag);
10253
10254 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10255 while (!list_empty(&completions)) {
10256 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10257 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10258 if (pmb->mbox_cmpl)
10259 pmb->mbox_cmpl(phba, pmb);
10260 }
10261}
10262
10263/**
James Smart3621a712009-04-06 18:47:14 -040010264 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010265 * @vport: Pointer to virtual port object.
10266 *
10267 * lpfc_sli_host_down is called to clean up the resources
10268 * associated with a vport before destroying virtual
10269 * port data structures.
10270 * This function does following operations:
10271 * - Free discovery resources associated with this virtual
10272 * port.
10273 * - Free iocbs associated with this virtual port in
10274 * the txq.
10275 * - Send abort for all iocb commands associated with this
10276 * vport in txcmplq.
10277 *
10278 * This function is called with no lock held and always returns 1.
10279 **/
dea31012005-04-17 16:05:31 -050010280int
James Smart92d7f7b2007-06-17 19:56:38 -050010281lpfc_sli_host_down(struct lpfc_vport *vport)
10282{
James Smart858c9f62007-06-17 19:56:39 -050010283 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010284 struct lpfc_hba *phba = vport->phba;
10285 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010286 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010287 struct lpfc_sli_ring *pring;
10288 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010289 int i;
10290 unsigned long flags = 0;
10291 uint16_t prev_pring_flag;
10292
10293 lpfc_cleanup_discovery_resources(vport);
10294
10295 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010296
James Smart895427b2017-02-12 13:52:30 -080010297 /*
10298 * Error everything on the txq since these iocbs
10299 * have not been given to the FW yet.
10300 * Also issue ABTS for everything on the txcmplq
10301 */
10302 if (phba->sli_rev != LPFC_SLI_REV4) {
10303 for (i = 0; i < psli->num_rings; i++) {
10304 pring = &psli->sli3_ring[i];
10305 prev_pring_flag = pring->flag;
10306 /* Only slow rings */
10307 if (pring->ringno == LPFC_ELS_RING) {
10308 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10309 /* Set the lpfc data pending flag */
10310 set_bit(LPFC_DATA_READY, &phba->data_flags);
10311 }
10312 list_for_each_entry_safe(iocb, next_iocb,
10313 &pring->txq, list) {
10314 if (iocb->vport != vport)
10315 continue;
10316 list_move_tail(&iocb->list, &completions);
10317 }
10318 list_for_each_entry_safe(iocb, next_iocb,
10319 &pring->txcmplq, list) {
10320 if (iocb->vport != vport)
10321 continue;
10322 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10323 }
10324 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010325 }
James Smart895427b2017-02-12 13:52:30 -080010326 } else {
10327 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10328 pring = qp->pring;
10329 if (!pring)
10330 continue;
10331 if (pring == phba->sli4_hba.els_wq->pring) {
10332 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10333 /* Set the lpfc data pending flag */
10334 set_bit(LPFC_DATA_READY, &phba->data_flags);
10335 }
10336 prev_pring_flag = pring->flag;
10337 spin_lock_irq(&pring->ring_lock);
10338 list_for_each_entry_safe(iocb, next_iocb,
10339 &pring->txq, list) {
10340 if (iocb->vport != vport)
10341 continue;
10342 list_move_tail(&iocb->list, &completions);
10343 }
10344 spin_unlock_irq(&pring->ring_lock);
10345 list_for_each_entry_safe(iocb, next_iocb,
10346 &pring->txcmplq, list) {
10347 if (iocb->vport != vport)
10348 continue;
10349 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10350 }
10351 pring->flag = prev_pring_flag;
10352 }
James Smart92d7f7b2007-06-17 19:56:38 -050010353 }
James Smart92d7f7b2007-06-17 19:56:38 -050010354 spin_unlock_irqrestore(&phba->hbalock, flags);
10355
James Smarta257bf92009-04-06 18:48:10 -040010356 /* Cancel all the IOCBs from the completions list */
10357 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10358 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010359 return 1;
10360}
10361
James Smarte59058c2008-08-24 21:49:00 -040010362/**
James Smart3621a712009-04-06 18:47:14 -040010363 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010364 * @phba: Pointer to HBA context object.
10365 *
10366 * This function cleans up all iocb, buffers, mailbox commands
10367 * while shutting down the HBA. This function is called with no
10368 * lock held and always returns 1.
10369 * This function does the following to cleanup driver resources:
10370 * - Free discovery resources for each virtual port
10371 * - Cleanup any pending fabric iocbs
10372 * - Iterate through the iocb txq and free each entry
10373 * in the list.
10374 * - Free up any buffer posted to the HBA
10375 * - Free mailbox commands in the mailbox queue.
10376 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010377int
James Smart2e0fef82007-06-17 19:56:36 -050010378lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010379{
James Smart2534ba72007-04-25 09:52:20 -040010380 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010381 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010382 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010383 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010384 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010385 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010386 int i;
10387
10388 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010389 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010390
dea31012005-04-17 16:05:31 -050010391 lpfc_hba_down_prep(phba);
10392
James Smart92d7f7b2007-06-17 19:56:38 -050010393 lpfc_fabric_abort_hba(phba);
10394
James Smart2e0fef82007-06-17 19:56:36 -050010395 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010396
James Smart895427b2017-02-12 13:52:30 -080010397 /*
10398 * Error everything on the txq since these iocbs
10399 * have not been given to the FW yet.
10400 */
10401 if (phba->sli_rev != LPFC_SLI_REV4) {
10402 for (i = 0; i < psli->num_rings; i++) {
10403 pring = &psli->sli3_ring[i];
10404 /* Only slow rings */
10405 if (pring->ringno == LPFC_ELS_RING) {
10406 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10407 /* Set the lpfc data pending flag */
10408 set_bit(LPFC_DATA_READY, &phba->data_flags);
10409 }
10410 list_splice_init(&pring->txq, &completions);
10411 }
10412 } else {
10413 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10414 pring = qp->pring;
10415 if (!pring)
10416 continue;
10417 spin_lock_irq(&pring->ring_lock);
10418 list_splice_init(&pring->txq, &completions);
10419 spin_unlock_irq(&pring->ring_lock);
10420 if (pring == phba->sli4_hba.els_wq->pring) {
10421 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10422 /* Set the lpfc data pending flag */
10423 set_bit(LPFC_DATA_READY, &phba->data_flags);
10424 }
10425 }
dea31012005-04-17 16:05:31 -050010426 }
James Smart2e0fef82007-06-17 19:56:36 -050010427 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010428
James Smarta257bf92009-04-06 18:48:10 -040010429 /* Cancel all the IOCBs from the completions list */
10430 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10431 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010432
James Smart0ff10d42008-01-11 01:52:36 -050010433 spin_lock_irqsave(&phba->hbalock, flags);
10434 list_splice_init(&phba->elsbuf, &completions);
10435 phba->elsbuf_cnt = 0;
10436 phba->elsbuf_prev_cnt = 0;
10437 spin_unlock_irqrestore(&phba->hbalock, flags);
10438
10439 while (!list_empty(&completions)) {
10440 list_remove_head(&completions, buf_ptr,
10441 struct lpfc_dmabuf, list);
10442 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10443 kfree(buf_ptr);
10444 }
10445
dea31012005-04-17 16:05:31 -050010446 /* Return any active mbox cmds */
10447 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010448
James Smartda0436e2009-05-22 14:51:39 -040010449 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010450 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010451 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010452
James Smartda0436e2009-05-22 14:51:39 -040010453 return 1;
10454}
James Smart92d7f7b2007-06-17 19:56:38 -050010455
James Smartda0436e2009-05-22 14:51:39 -040010456/**
James Smart3621a712009-04-06 18:47:14 -040010457 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010458 * @srcp: Source memory pointer.
10459 * @destp: Destination memory pointer.
10460 * @cnt: Number of words required to be copied.
10461 *
10462 * This function is used for copying data between driver memory
10463 * and the SLI memory. This function also changes the endianness
10464 * of each word if native endianness is different from SLI
10465 * endianness. This function can be called with or without
10466 * lock.
10467 **/
dea31012005-04-17 16:05:31 -050010468void
10469lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10470{
10471 uint32_t *src = srcp;
10472 uint32_t *dest = destp;
10473 uint32_t ldata;
10474 int i;
10475
10476 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10477 ldata = *src;
10478 ldata = le32_to_cpu(ldata);
10479 *dest = ldata;
10480 src++;
10481 dest++;
10482 }
10483}
10484
James Smarte59058c2008-08-24 21:49:00 -040010485
10486/**
James Smarta0c87cb2009-07-19 10:01:10 -040010487 * lpfc_sli_bemem_bcopy - SLI memory copy function
10488 * @srcp: Source memory pointer.
10489 * @destp: Destination memory pointer.
10490 * @cnt: Number of words required to be copied.
10491 *
10492 * This function is used for copying data between a data structure
10493 * with big endian representation to local endianness.
10494 * This function can be called with or without lock.
10495 **/
10496void
10497lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10498{
10499 uint32_t *src = srcp;
10500 uint32_t *dest = destp;
10501 uint32_t ldata;
10502 int i;
10503
10504 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10505 ldata = *src;
10506 ldata = be32_to_cpu(ldata);
10507 *dest = ldata;
10508 src++;
10509 dest++;
10510 }
10511}
10512
10513/**
James Smart3621a712009-04-06 18:47:14 -040010514 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010515 * @phba: Pointer to HBA context object.
10516 * @pring: Pointer to driver SLI ring object.
10517 * @mp: Pointer to driver buffer object.
10518 *
10519 * This function is called with no lock held.
10520 * It always return zero after adding the buffer to the postbufq
10521 * buffer list.
10522 **/
dea31012005-04-17 16:05:31 -050010523int
James Smart2e0fef82007-06-17 19:56:36 -050010524lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10525 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010526{
10527 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10528 later */
James Smart2e0fef82007-06-17 19:56:36 -050010529 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010530 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010531 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010532 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010533 return 0;
10534}
10535
James Smarte59058c2008-08-24 21:49:00 -040010536/**
James Smart3621a712009-04-06 18:47:14 -040010537 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010538 * @phba: Pointer to HBA context object.
10539 *
10540 * When HBQ is enabled, buffers are searched based on tags. This function
10541 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10542 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10543 * does not conflict with tags of buffer posted for unsolicited events.
10544 * The function returns the allocated tag. The function is called with
10545 * no locks held.
10546 **/
James Smart76bb24e2007-10-27 13:38:00 -040010547uint32_t
10548lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10549{
10550 spin_lock_irq(&phba->hbalock);
10551 phba->buffer_tag_count++;
10552 /*
10553 * Always set the QUE_BUFTAG_BIT to distiguish between
10554 * a tag assigned by HBQ.
10555 */
10556 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10557 spin_unlock_irq(&phba->hbalock);
10558 return phba->buffer_tag_count;
10559}
10560
James Smarte59058c2008-08-24 21:49:00 -040010561/**
James Smart3621a712009-04-06 18:47:14 -040010562 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010563 * @phba: Pointer to HBA context object.
10564 * @pring: Pointer to driver SLI ring object.
10565 * @tag: Buffer tag.
10566 *
10567 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10568 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10569 * iocb is posted to the response ring with the tag of the buffer.
10570 * This function searches the pring->postbufq list using the tag
10571 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10572 * iocb. If the buffer is found then lpfc_dmabuf object of the
10573 * buffer is returned to the caller else NULL is returned.
10574 * This function is called with no lock held.
10575 **/
James Smart76bb24e2007-10-27 13:38:00 -040010576struct lpfc_dmabuf *
10577lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10578 uint32_t tag)
10579{
10580 struct lpfc_dmabuf *mp, *next_mp;
10581 struct list_head *slp = &pring->postbufq;
10582
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010583 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010584 spin_lock_irq(&phba->hbalock);
10585 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10586 if (mp->buffer_tag == tag) {
10587 list_del_init(&mp->list);
10588 pring->postbufq_cnt--;
10589 spin_unlock_irq(&phba->hbalock);
10590 return mp;
10591 }
10592 }
10593
10594 spin_unlock_irq(&phba->hbalock);
10595 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010596 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010597 "ring %d Data x%lx x%p x%p x%x\n",
10598 pring->ringno, (unsigned long) tag,
10599 slp->next, slp->prev, pring->postbufq_cnt);
10600
10601 return NULL;
10602}
dea31012005-04-17 16:05:31 -050010603
James Smarte59058c2008-08-24 21:49:00 -040010604/**
James Smart3621a712009-04-06 18:47:14 -040010605 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010606 * @phba: Pointer to HBA context object.
10607 * @pring: Pointer to driver SLI ring object.
10608 * @phys: DMA address of the buffer.
10609 *
10610 * This function searches the buffer list using the dma_address
10611 * of unsolicited event to find the driver's lpfc_dmabuf object
10612 * corresponding to the dma_address. The function returns the
10613 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10614 * This function is called by the ct and els unsolicited event
10615 * handlers to get the buffer associated with the unsolicited
10616 * event.
10617 *
10618 * This function is called with no lock held.
10619 **/
dea31012005-04-17 16:05:31 -050010620struct lpfc_dmabuf *
10621lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10622 dma_addr_t phys)
10623{
10624 struct lpfc_dmabuf *mp, *next_mp;
10625 struct list_head *slp = &pring->postbufq;
10626
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010627 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010628 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010629 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10630 if (mp->phys == phys) {
10631 list_del_init(&mp->list);
10632 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010633 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010634 return mp;
10635 }
10636 }
10637
James Smart2e0fef82007-06-17 19:56:36 -050010638 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010639 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010640 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010641 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010642 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010643 slp->next, slp->prev, pring->postbufq_cnt);
10644 return NULL;
10645}
10646
James Smarte59058c2008-08-24 21:49:00 -040010647/**
James Smart3621a712009-04-06 18:47:14 -040010648 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010649 * @phba: Pointer to HBA context object.
10650 * @cmdiocb: Pointer to driver command iocb object.
10651 * @rspiocb: Pointer to driver response iocb object.
10652 *
10653 * This function is the completion handler for the abort iocbs for
10654 * ELS commands. This function is called from the ELS ring event
10655 * handler with no lock held. This function frees memory resources
10656 * associated with the abort iocb.
10657 **/
dea31012005-04-17 16:05:31 -050010658static void
James Smart2e0fef82007-06-17 19:56:36 -050010659lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10660 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050010661{
James Smart2e0fef82007-06-17 19:56:36 -050010662 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040010663 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050010664 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040010665
10666 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050010667
10668 /*
10669 * Assume that the port already completed and returned, or
10670 * will return the iocb. Just Log the message.
10671 */
James Smart2680eea2007-04-25 09:52:55 -040010672 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10673 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10674
James Smart2e0fef82007-06-17 19:56:36 -050010675 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040010676 if (phba->sli_rev < LPFC_SLI_REV4) {
10677 if (abort_iotag != 0 &&
10678 abort_iotag <= phba->sli.last_iotag)
10679 abort_iocb =
10680 phba->sli.iocbq_lookup[abort_iotag];
10681 } else
10682 /* For sli4 the abort_tag is the XRI,
10683 * so the abort routine puts the iotag of the iocb
10684 * being aborted in the context field of the abort
10685 * IOCB.
10686 */
10687 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040010688
James Smart2a9bf3d2010-06-07 15:24:45 -040010689 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10690 "0327 Cannot abort els iocb %p "
10691 "with tag %x context %x, abort status %x, "
10692 "abort code %x\n",
10693 abort_iocb, abort_iotag, abort_context,
10694 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040010695
James Smartff78d8f2011-12-13 13:21:35 -050010696 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040010697 }
James Bottomley604a3e32005-10-29 10:28:33 -050010698 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050010699 return;
10700}
10701
James Smarte59058c2008-08-24 21:49:00 -040010702/**
James Smart3621a712009-04-06 18:47:14 -040010703 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040010704 * @phba: Pointer to HBA context object.
10705 * @cmdiocb: Pointer to driver command iocb object.
10706 * @rspiocb: Pointer to driver response iocb object.
10707 *
10708 * The function is called from SLI ring event handler with no
10709 * lock held. This function is the completion handler for ELS commands
10710 * which are aborted. The function frees memory resources used for
10711 * the aborted ELS commands.
10712 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010713static void
10714lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10715 struct lpfc_iocbq *rspiocb)
10716{
10717 IOCB_t *irsp = &rspiocb->iocb;
10718
10719 /* ELS cmd tag <ulpIoTag> completes */
10720 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040010721 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050010722 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010723 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050010724 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050010725 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10726 lpfc_ct_free_iocb(phba, cmdiocb);
10727 else
10728 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050010729 return;
10730}
10731
James Smarte59058c2008-08-24 21:49:00 -040010732/**
James Smart5af5eee2010-10-22 11:06:38 -040010733 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040010734 * @phba: Pointer to HBA context object.
10735 * @pring: Pointer to driver SLI ring object.
10736 * @cmdiocb: Pointer to driver command iocb object.
10737 *
James Smart5af5eee2010-10-22 11:06:38 -040010738 * This function issues an abort iocb for the provided command iocb down to
10739 * the port. Other than the case the outstanding command iocb is an abort
10740 * request, this function issues abort out unconditionally. This function is
10741 * called with hbalock held. The function returns 0 when it fails due to
10742 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040010743 **/
James Smart5af5eee2010-10-22 11:06:38 -040010744static int
10745lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010746 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050010747{
James Smart2e0fef82007-06-17 19:56:36 -050010748 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010749 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050010750 IOCB_t *icmd = NULL;
10751 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010752 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040010753 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -040010754
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010755 lockdep_assert_held(&phba->hbalock);
10756
James Smart92d7f7b2007-06-17 19:56:38 -050010757 /*
10758 * There are certain command types we don't want to abort. And we
10759 * don't want to abort commands that are already in the process of
10760 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010761 */
10762 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010763 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010764 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10765 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010766 return 0;
10767
dea31012005-04-17 16:05:31 -050010768 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010769 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010770 if (abtsiocbp == NULL)
10771 return 0;
dea31012005-04-17 16:05:31 -050010772
James Smart07951072007-04-25 09:51:38 -040010773 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010774 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010775 */
10776 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10777
dea31012005-04-17 16:05:31 -050010778 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010779 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10780 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010781 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010782 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010783 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10784 }
James Smartda0436e2009-05-22 14:51:39 -040010785 else
10786 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010787 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010788 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010789
James Smart5ffc2662009-11-18 15:39:44 -050010790 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010791 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010792 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10793 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010794 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10795 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010796
James Smart2e0fef82007-06-17 19:56:36 -050010797 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010798 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10799 else
10800 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10801
10802 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010803 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010804
James Smarte8b62012007-08-02 11:10:09 -040010805 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10806 "0339 Abort xri x%x, original iotag x%x, "
10807 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010808 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010809 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010810 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010811
10812 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010813 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10814 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010815 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040010816 /* Note: both hbalock and ring_lock need to be set here */
10817 spin_lock_irqsave(&pring->ring_lock, iflags);
10818 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10819 abtsiocbp, 0);
10820 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10821 } else {
10822 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10823 abtsiocbp, 0);
10824 }
James Smart07951072007-04-25 09:51:38 -040010825
James Smartd7c255b2008-08-24 21:50:00 -040010826 if (retval)
10827 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010828
10829 /*
10830 * Caller to this routine should check for IOCB_ERROR
10831 * and handle it properly. This routine no longer removes
10832 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10833 */
10834 return retval;
10835}
10836
10837/**
10838 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10839 * @phba: Pointer to HBA context object.
10840 * @pring: Pointer to driver SLI ring object.
10841 * @cmdiocb: Pointer to driver command iocb object.
10842 *
10843 * This function issues an abort iocb for the provided command iocb. In case
10844 * of unloading, the abort iocb will not be issued to commands on the ELS
10845 * ring. Instead, the callback function shall be changed to those commands
10846 * so that nothing happens when them finishes. This function is called with
10847 * hbalock held. The function returns 0 when the command iocb is an abort
10848 * request.
10849 **/
10850int
10851lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10852 struct lpfc_iocbq *cmdiocb)
10853{
10854 struct lpfc_vport *vport = cmdiocb->vport;
10855 int retval = IOCB_ERROR;
10856 IOCB_t *icmd = NULL;
10857
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010858 lockdep_assert_held(&phba->hbalock);
10859
James Smart5af5eee2010-10-22 11:06:38 -040010860 /*
10861 * There are certain command types we don't want to abort. And we
10862 * don't want to abort commands that are already in the process of
10863 * being aborted.
10864 */
10865 icmd = &cmdiocb->iocb;
10866 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10867 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10868 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10869 return 0;
10870
Dick Kennedy1234a6d2017-09-29 17:34:29 -070010871 if (!pring) {
10872 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10873 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10874 else
10875 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10876 goto abort_iotag_exit;
10877 }
10878
James Smart5af5eee2010-10-22 11:06:38 -040010879 /*
10880 * If we're unloading, don't abort iocb on the ELS ring, but change
10881 * the callback so that nothing happens when it finishes.
10882 */
10883 if ((vport->load_flag & FC_UNLOADING) &&
10884 (pring->ringno == LPFC_ELS_RING)) {
10885 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10886 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10887 else
10888 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10889 goto abort_iotag_exit;
10890 }
10891
10892 /* Now, we try to issue the abort to the cmdiocb out */
10893 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10894
James Smart07951072007-04-25 09:51:38 -040010895abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010896 /*
10897 * Caller to this routine should check for IOCB_ERROR
10898 * and handle it properly. This routine no longer removes
10899 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010900 */
James Smart2e0fef82007-06-17 19:56:36 -050010901 return retval;
dea31012005-04-17 16:05:31 -050010902}
10903
James Smarte59058c2008-08-24 21:49:00 -040010904/**
James Smart895427b2017-02-12 13:52:30 -080010905 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10906 * @phba: Pointer to HBA context object.
10907 * @pring: Pointer to driver SLI ring object.
10908 * @cmdiocb: Pointer to driver command iocb object.
10909 *
10910 * This function issues an abort iocb for the provided command iocb down to
10911 * the port. Other than the case the outstanding command iocb is an abort
10912 * request, this function issues abort out unconditionally. This function is
10913 * called with hbalock held. The function returns 0 when it fails due to
10914 * memory allocation failure or when the command iocb is an abort request.
10915 **/
10916static int
10917lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10918 struct lpfc_iocbq *cmdiocb)
10919{
10920 struct lpfc_vport *vport = cmdiocb->vport;
10921 struct lpfc_iocbq *abtsiocbp;
James Smart205e8242018-03-05 12:04:03 -080010922 union lpfc_wqe128 *abts_wqe;
James Smart895427b2017-02-12 13:52:30 -080010923 int retval;
10924
10925 /*
10926 * There are certain command types we don't want to abort. And we
10927 * don't want to abort commands that are already in the process of
10928 * being aborted.
10929 */
10930 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10931 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10932 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10933 return 0;
10934
10935 /* issue ABTS for this io based on iotag */
10936 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10937 if (abtsiocbp == NULL)
10938 return 0;
10939
10940 /* This signals the response to set the correct status
10941 * before calling the completion handler
10942 */
10943 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10944
10945 /* Complete prepping the abort wqe and issue to the FW. */
10946 abts_wqe = &abtsiocbp->wqe;
10947 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10948 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10949
10950 /* Explicitly set reserved fields to zero.*/
10951 abts_wqe->abort_cmd.rsrvd4 = 0;
10952 abts_wqe->abort_cmd.rsrvd5 = 0;
10953
10954 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10955 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10956 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10957
10958 /* word 7 */
10959 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10960 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10961 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10962 cmdiocb->iocb.ulpClass);
10963
10964 /* word 8 - tell the FW to abort the IO associated with this
10965 * outstanding exchange ID.
10966 */
10967 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10968
10969 /* word 9 - this is the iotag for the abts_wqe completion. */
10970 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10971 abtsiocbp->iotag);
10972
10973 /* word 10 */
10974 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10975 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10976 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10977
10978 /* word 11 */
10979 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10980 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10981 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10982
10983 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10984 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10985 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080010986 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart895427b2017-02-12 13:52:30 -080010987 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
Dick Kennedycd22d602017-08-23 16:55:35 -070010988 if (retval) {
James Smart895427b2017-02-12 13:52:30 -080010989 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10990 "6147 Failed abts issue_wqe with status x%x "
10991 "for oxid x%x\n",
10992 retval, cmdiocb->sli4_xritag);
10993 lpfc_sli_release_iocbq(phba, abtsiocbp);
10994 return retval;
10995 }
10996
10997 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10998 "6148 Drv Abort NVME Request Issued for "
10999 "ox_id x%x on reqtag x%x\n",
11000 cmdiocb->sli4_xritag,
11001 abtsiocbp->iotag);
11002
11003 return retval;
11004}
11005
11006/**
James Smart5af5eee2010-10-22 11:06:38 -040011007 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11008 * @phba: pointer to lpfc HBA data structure.
11009 *
11010 * This routine will abort all pending and outstanding iocbs to an HBA.
11011 **/
11012void
11013lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11014{
11015 struct lpfc_sli *psli = &phba->sli;
11016 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080011017 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011018 int i;
11019
James Smart895427b2017-02-12 13:52:30 -080011020 if (phba->sli_rev != LPFC_SLI_REV4) {
11021 for (i = 0; i < psli->num_rings; i++) {
11022 pring = &psli->sli3_ring[i];
11023 lpfc_sli_abort_iocb_ring(phba, pring);
11024 }
11025 return;
11026 }
11027 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11028 pring = qp->pring;
11029 if (!pring)
11030 continue;
James Smartdb55fba2014-04-04 13:52:02 -040011031 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040011032 }
11033}
11034
11035/**
James Smart3621a712009-04-06 18:47:14 -040011036 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040011037 * @iocbq: Pointer to driver iocb object.
11038 * @vport: Pointer to driver virtual port object.
11039 * @tgt_id: SCSI ID of the target.
11040 * @lun_id: LUN ID of the scsi device.
11041 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11042 *
James Smart3621a712009-04-06 18:47:14 -040011043 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040011044 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11045 * 0 if the filtering criteria is met for the given iocb and will return
11046 * 1 if the filtering criteria is not met.
11047 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11048 * given iocb is for the SCSI device specified by vport, tgt_id and
11049 * lun_id parameter.
11050 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11051 * given iocb is for the SCSI target specified by vport and tgt_id
11052 * parameters.
11053 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11054 * given iocb is for the SCSI host associated with the given vport.
11055 * This function is called with no locks held.
11056 **/
dea31012005-04-17 16:05:31 -050011057static int
James Smart51ef4c22007-08-02 11:10:31 -040011058lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11059 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011060 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011061{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011062 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011063 int rc = 1;
11064
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011065 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
11066 return rc;
11067
James Smart51ef4c22007-08-02 11:10:31 -040011068 if (iocbq->vport != vport)
11069 return rc;
11070
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011071 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011072
James Smart495a7142008-06-14 22:52:59 -040011073 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050011074 return rc;
11075
11076 switch (ctx_cmd) {
11077 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040011078 if ((lpfc_cmd->rdata->pnode) &&
11079 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11080 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050011081 rc = 0;
11082 break;
11083 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040011084 if ((lpfc_cmd->rdata->pnode) &&
11085 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050011086 rc = 0;
11087 break;
dea31012005-04-17 16:05:31 -050011088 case LPFC_CTX_HOST:
11089 rc = 0;
11090 break;
11091 default:
11092 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070011093 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050011094 break;
11095 }
11096
11097 return rc;
11098}
11099
James Smarte59058c2008-08-24 21:49:00 -040011100/**
James Smart3621a712009-04-06 18:47:14 -040011101 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040011102 * @vport: Pointer to virtual port.
11103 * @tgt_id: SCSI ID of the target.
11104 * @lun_id: LUN ID of the scsi device.
11105 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11106 *
11107 * This function returns number of FCP commands pending for the vport.
11108 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11109 * commands pending on the vport associated with SCSI device specified
11110 * by tgt_id and lun_id parameters.
11111 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11112 * commands pending on the vport associated with SCSI target specified
11113 * by tgt_id parameter.
11114 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11115 * commands pending on the vport.
11116 * This function returns the number of iocbs which satisfy the filter.
11117 * This function is called without any lock held.
11118 **/
dea31012005-04-17 16:05:31 -050011119int
James Smart51ef4c22007-08-02 11:10:31 -040011120lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11121 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011122{
James Smart51ef4c22007-08-02 11:10:31 -040011123 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011124 struct lpfc_iocbq *iocbq;
11125 int sum, i;
dea31012005-04-17 16:05:31 -050011126
Johannes Thumshirn31979002016-07-18 16:06:03 +020011127 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011128 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11129 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011130
James Smart51ef4c22007-08-02 11:10:31 -040011131 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11132 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011133 sum++;
dea31012005-04-17 16:05:31 -050011134 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020011135 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011136
dea31012005-04-17 16:05:31 -050011137 return sum;
11138}
11139
James Smarte59058c2008-08-24 21:49:00 -040011140/**
James Smart3621a712009-04-06 18:47:14 -040011141 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040011142 * @phba: Pointer to HBA context object
11143 * @cmdiocb: Pointer to command iocb object.
11144 * @rspiocb: Pointer to response iocb object.
11145 *
11146 * This function is called when an aborted FCP iocb completes. This
11147 * function is called by the ring event handler with no lock held.
11148 * This function frees the iocb.
11149 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011150void
James Smart2e0fef82007-06-17 19:56:36 -050011151lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11152 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011153{
James Smartcb69f7d2011-12-13 13:21:57 -050011154 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040011155 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050011156 "original iotag x%x, abort cmd iotag x%x "
11157 "status 0x%x, reason 0x%x\n",
11158 cmdiocb->iocb.un.acxri.abortContextTag,
11159 cmdiocb->iocb.un.acxri.abortIoTag,
11160 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11161 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050011162 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011163 return;
11164}
11165
James Smarte59058c2008-08-24 21:49:00 -040011166/**
James Smart3621a712009-04-06 18:47:14 -040011167 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040011168 * @vport: Pointer to virtual port.
11169 * @pring: Pointer to driver SLI ring object.
11170 * @tgt_id: SCSI ID of the target.
11171 * @lun_id: LUN ID of the scsi device.
11172 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11173 *
11174 * This function sends an abort command for every SCSI command
11175 * associated with the given virtual port pending on the ring
11176 * filtered by lpfc_sli_validate_fcp_iocb function.
11177 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11178 * FCP iocbs associated with lun specified by tgt_id and lun_id
11179 * parameters
11180 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11181 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11182 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11183 * FCP iocbs associated with virtual port.
11184 * This function returns number of iocbs it failed to abort.
11185 * This function is called with no locks held.
11186 **/
dea31012005-04-17 16:05:31 -050011187int
James Smart51ef4c22007-08-02 11:10:31 -040011188lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11189 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050011190{
James Smart51ef4c22007-08-02 11:10:31 -040011191 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011192 struct lpfc_iocbq *iocbq;
11193 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070011194 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050011195 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050011196 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011197 int i;
dea31012005-04-17 16:05:31 -050011198
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011199 for (i = 1; i <= phba->sli.last_iotag; i++) {
11200 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011201
James Smart51ef4c22007-08-02 11:10:31 -040011202 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011203 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011204 continue;
11205
James Smartafbd8d82013-09-06 12:22:13 -040011206 /*
11207 * If the iocbq is already being aborted, don't take a second
11208 * action, but do count it.
11209 */
11210 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11211 continue;
11212
dea31012005-04-17 16:05:31 -050011213 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011214 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011215 if (abtsiocb == NULL) {
11216 errcnt++;
11217 continue;
11218 }
dea31012005-04-17 16:05:31 -050011219
James Smartafbd8d82013-09-06 12:22:13 -040011220 /* indicate the IO is being aborted by the driver. */
11221 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11222
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011223 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011224 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11225 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011226 if (phba->sli_rev == LPFC_SLI_REV4)
11227 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11228 else
11229 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011230 abtsiocb->iocb.ulpLe = 1;
11231 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011232 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011233
James Smart5ffc2662009-11-18 15:39:44 -050011234 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011235 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011236 if (iocbq->iocb_flag & LPFC_IO_FCP)
11237 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011238 if (iocbq->iocb_flag & LPFC_IO_FOF)
11239 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011240
James Smart2e0fef82007-06-17 19:56:36 -050011241 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011242 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11243 else
11244 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11245
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011246 /* Setup callback routine and issue the command. */
11247 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011248 if (phba->sli_rev == LPFC_SLI_REV4) {
11249 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11250 if (!pring_s4)
11251 continue;
11252 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11253 abtsiocb, 0);
11254 } else
11255 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11256 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011257 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011258 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011259 errcnt++;
11260 continue;
11261 }
11262 }
11263
11264 return errcnt;
11265}
11266
James Smarte59058c2008-08-24 21:49:00 -040011267/**
James Smart98912dda2014-04-04 13:52:31 -040011268 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11269 * @vport: Pointer to virtual port.
11270 * @pring: Pointer to driver SLI ring object.
11271 * @tgt_id: SCSI ID of the target.
11272 * @lun_id: LUN ID of the scsi device.
11273 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11274 *
11275 * This function sends an abort command for every SCSI command
11276 * associated with the given virtual port pending on the ring
11277 * filtered by lpfc_sli_validate_fcp_iocb function.
11278 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11279 * FCP iocbs associated with lun specified by tgt_id and lun_id
11280 * parameters
11281 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11282 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11283 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11284 * FCP iocbs associated with virtual port.
11285 * This function returns number of iocbs it aborted .
11286 * This function is called with no locks held right after a taskmgmt
11287 * command is sent.
11288 **/
11289int
11290lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11291 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11292{
11293 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040011294 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011295 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011296 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011297 struct lpfc_iocbq *iocbq;
11298 IOCB_t *icmd;
11299 int sum, i, ret_val;
11300 unsigned long iflags;
11301 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040011302
11303 spin_lock_irq(&phba->hbalock);
11304
11305 /* all I/Os are in process of being flushed */
11306 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11307 spin_unlock_irq(&phba->hbalock);
11308 return 0;
11309 }
11310 sum = 0;
11311
11312 for (i = 1; i <= phba->sli.last_iotag; i++) {
11313 iocbq = phba->sli.iocbq_lookup[i];
11314
11315 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11316 cmd) != 0)
11317 continue;
11318
11319 /*
11320 * If the iocbq is already being aborted, don't take a second
11321 * action, but do count it.
11322 */
11323 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11324 continue;
11325
11326 /* issue ABTS for this IOCB based on iotag */
11327 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11328 if (abtsiocbq == NULL)
11329 continue;
11330
11331 icmd = &iocbq->iocb;
11332 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11333 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11334 if (phba->sli_rev == LPFC_SLI_REV4)
11335 abtsiocbq->iocb.un.acxri.abortIoTag =
11336 iocbq->sli4_xritag;
11337 else
11338 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11339 abtsiocbq->iocb.ulpLe = 1;
11340 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11341 abtsiocbq->vport = vport;
11342
11343 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011344 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011345 if (iocbq->iocb_flag & LPFC_IO_FCP)
11346 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011347 if (iocbq->iocb_flag & LPFC_IO_FOF)
11348 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011349
James Smart8c50d252014-09-03 12:58:16 -040011350 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11351 ndlp = lpfc_cmd->rdata->pnode;
11352
11353 if (lpfc_is_link_up(phba) &&
11354 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011355 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11356 else
11357 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11358
11359 /* Setup callback routine and issue the command. */
11360 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11361
11362 /*
11363 * Indicate the IO is being aborted by the driver and set
11364 * the caller's flag into the aborted IO.
11365 */
11366 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11367
11368 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011369 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11370 if (pring_s4 == NULL)
11371 continue;
James Smart98912dda2014-04-04 13:52:31 -040011372 /* Note: both hbalock and ring_lock must be set here */
11373 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11374 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11375 abtsiocbq, 0);
11376 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11377 } else {
11378 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11379 abtsiocbq, 0);
11380 }
11381
11382
11383 if (ret_val == IOCB_ERROR)
11384 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11385 else
11386 sum++;
11387 }
11388 spin_unlock_irq(&phba->hbalock);
11389 return sum;
11390}
11391
11392/**
James Smart3621a712009-04-06 18:47:14 -040011393 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011394 * @phba: Pointer to HBA context object.
11395 * @cmdiocbq: Pointer to command iocb.
11396 * @rspiocbq: Pointer to response iocb.
11397 *
11398 * This function is the completion handler for iocbs issued using
11399 * lpfc_sli_issue_iocb_wait function. This function is called by the
11400 * ring event handler function without any lock held. This function
11401 * can be called from both worker thread context and interrupt
11402 * context. This function also can be called from other thread which
11403 * cleans up the SLI layer objects.
11404 * This function copy the contents of the response iocb to the
11405 * response iocb memory object provided by the caller of
11406 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11407 * sleeps for the iocb completion.
11408 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011409static void
11410lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11411 struct lpfc_iocbq *cmdiocbq,
11412 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011413{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011414 wait_queue_head_t *pdone_q;
11415 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050011416 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011417
James Smart2e0fef82007-06-17 19:56:36 -050011418 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011419 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11420
11421 /*
11422 * A time out has occurred for the iocb. If a time out
11423 * completion handler has been supplied, call it. Otherwise,
11424 * just free the iocbq.
11425 */
11426
11427 spin_unlock_irqrestore(&phba->hbalock, iflags);
11428 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11429 cmdiocbq->wait_iocb_cmpl = NULL;
11430 if (cmdiocbq->iocb_cmpl)
11431 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11432 else
11433 lpfc_sli_release_iocbq(phba, cmdiocbq);
11434 return;
11435 }
11436
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011437 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11438 if (cmdiocbq->context2 && rspiocbq)
11439 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11440 &rspiocbq->iocb, sizeof(IOCB_t));
11441
James Smart0f65ff62010-02-26 14:14:23 -050011442 /* Set the exchange busy flag for task management commands */
11443 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11444 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11445 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11446 cur_iocbq);
11447 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11448 }
11449
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011450 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011451 if (pdone_q)
11452 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011453 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011454 return;
11455}
11456
James Smarte59058c2008-08-24 21:49:00 -040011457/**
James Smartd11e31d2009-06-10 17:23:06 -040011458 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11459 * @phba: Pointer to HBA context object..
11460 * @piocbq: Pointer to command iocb.
11461 * @flag: Flag to test.
11462 *
11463 * This routine grabs the hbalock and then test the iocb_flag to
11464 * see if the passed in flag is set.
11465 * Returns:
11466 * 1 if flag is set.
11467 * 0 if flag is not set.
11468 **/
11469static int
11470lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11471 struct lpfc_iocbq *piocbq, uint32_t flag)
11472{
11473 unsigned long iflags;
11474 int ret;
11475
11476 spin_lock_irqsave(&phba->hbalock, iflags);
11477 ret = piocbq->iocb_flag & flag;
11478 spin_unlock_irqrestore(&phba->hbalock, iflags);
11479 return ret;
11480
11481}
11482
11483/**
James Smart3621a712009-04-06 18:47:14 -040011484 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011485 * @phba: Pointer to HBA context object..
11486 * @pring: Pointer to sli ring.
11487 * @piocb: Pointer to command iocb.
11488 * @prspiocbq: Pointer to response iocb.
11489 * @timeout: Timeout in number of seconds.
11490 *
11491 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011492 * iocb to complete. The iocb_cmpl field of the shall be used
11493 * to handle iocbs which time out. If the field is NULL, the
11494 * function shall free the iocbq structure. If more clean up is
11495 * needed, the caller is expected to provide a completion function
11496 * that will provide the needed clean up. If the iocb command is
11497 * not completed within timeout seconds, the function will either
11498 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11499 * completion function set in the iocb_cmpl field and then return
11500 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11501 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011502 * The function waits for the iocb completion using an
11503 * non-interruptible wait.
11504 * This function will sleep while waiting for iocb completion.
11505 * So, this function should not be called from any context which
11506 * does not allow sleeping. Due to the same reason, this function
11507 * cannot be called with interrupt disabled.
11508 * This function assumes that the iocb completions occur while
11509 * this function sleep. So, this function cannot be called from
11510 * the thread which process iocb completion for this ring.
11511 * This function clears the iocb_flag of the iocb object before
11512 * issuing the iocb and the iocb completion handler sets this
11513 * flag and wakes this thread when the iocb completes.
11514 * The contents of the response iocb will be copied to prspiocbq
11515 * by the completion handler when the command completes.
11516 * This function returns IOCB_SUCCESS when success.
11517 * This function is called with no lock held.
11518 **/
dea31012005-04-17 16:05:31 -050011519int
James Smart2e0fef82007-06-17 19:56:36 -050011520lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011521 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011522 struct lpfc_iocbq *piocb,
11523 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011524 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011525{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011526 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011527 long timeleft, timeout_req = 0;
11528 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011529 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011530 struct lpfc_iocbq *iocb;
11531 int txq_cnt = 0;
11532 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011533 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011534 unsigned long iflags;
11535 bool iocb_completed = true;
11536
James Smart895427b2017-02-12 13:52:30 -080011537 if (phba->sli_rev >= LPFC_SLI_REV4)
11538 pring = lpfc_sli4_calc_ring(phba, piocb);
11539 else
11540 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011541 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011542 * If the caller has provided a response iocbq buffer, then context2
11543 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011544 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011545 if (prspiocbq) {
11546 if (piocb->context2)
11547 return IOCB_ERROR;
11548 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011549 }
11550
James Smart5a0916b2013-07-15 18:31:42 -040011551 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011552 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11553 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011554 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011555
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011556 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011557 if (lpfc_readl(phba->HCregaddr, &creg_val))
11558 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011559 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11560 writel(creg_val, phba->HCregaddr);
11561 readl(phba->HCregaddr); /* flush */
11562 }
11563
James Smart2a9bf3d2010-06-07 15:24:45 -040011564 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11565 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011566 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011567 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011568 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011569 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011570 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011571 spin_lock_irqsave(&phba->hbalock, iflags);
11572 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011573
James Smart5a0916b2013-07-15 18:31:42 -040011574 /*
11575 * IOCB timed out. Inform the wake iocb wait
11576 * completion function and set local status
11577 */
11578
11579 iocb_completed = false;
11580 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11581 }
11582 spin_unlock_irqrestore(&phba->hbalock, iflags);
11583 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011584 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011585 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011586 /* Note: we are not indicating if the IOCB has a success
11587 * status or not - that's for the caller to check.
11588 * IOCB_SUCCESS means just that the command was sent and
11589 * completed. Not that it completed successfully.
11590 * */
James Smart7054a602007-04-25 09:52:34 -040011591 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011592 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011593 "0338 IOCB wait timeout error - no "
11594 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011595 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011596 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011597 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011598 "0330 IOCB wake NOT set, "
11599 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011600 timeout, (timeleft / jiffies));
11601 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011602 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011603 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011604 if (phba->cfg_log_verbose & LOG_SLI) {
11605 list_for_each_entry(iocb, &pring->txq, list) {
11606 txq_cnt++;
11607 }
11608 list_for_each_entry(iocb, &pring->txcmplq, list) {
11609 txcmplq_cnt++;
11610 }
11611 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11612 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11613 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11614 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011615 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011616 } else {
11617 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011618 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011619 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011620 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011621 }
11622
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011623 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011624 if (lpfc_readl(phba->HCregaddr, &creg_val))
11625 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011626 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11627 writel(creg_val, phba->HCregaddr);
11628 readl(phba->HCregaddr); /* flush */
11629 }
11630
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011631 if (prspiocbq)
11632 piocb->context2 = NULL;
11633
11634 piocb->context_un.wait_queue = NULL;
11635 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011636 return retval;
11637}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011638
James Smarte59058c2008-08-24 21:49:00 -040011639/**
James Smart3621a712009-04-06 18:47:14 -040011640 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011641 * @phba: Pointer to HBA context object.
11642 * @pmboxq: Pointer to driver mailbox object.
11643 * @timeout: Timeout in number of seconds.
11644 *
11645 * This function issues the mailbox to firmware and waits for the
11646 * mailbox command to complete. If the mailbox command is not
11647 * completed within timeout seconds, it returns MBX_TIMEOUT.
11648 * The function waits for the mailbox completion using an
11649 * interruptible wait. If the thread is woken up due to a
11650 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11651 * should not free the mailbox resources, if this function returns
11652 * MBX_TIMEOUT.
11653 * This function will sleep while waiting for mailbox completion.
11654 * So, this function should not be called from any context which
11655 * does not allow sleeping. Due to the same reason, this function
11656 * cannot be called with interrupt disabled.
11657 * This function assumes that the mailbox completion occurs while
11658 * this function sleep. So, this function cannot be called from
11659 * the worker thread which processes mailbox completion.
11660 * This function is called in the context of HBA management
11661 * applications.
11662 * This function returns MBX_SUCCESS when successful.
11663 * This function is called with no lock held.
11664 **/
dea31012005-04-17 16:05:31 -050011665int
James Smart2e0fef82007-06-17 19:56:36 -050011666lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011667 uint32_t timeout)
11668{
James Smarte29d74f2018-03-05 12:04:07 -080011669 struct completion mbox_done;
dea31012005-04-17 16:05:31 -050011670 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011671 unsigned long flag;
dea31012005-04-17 16:05:31 -050011672
James Smart495a7142008-06-14 22:52:59 -040011673 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011674 /* setup wake call as IOCB callback */
11675 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea31012005-04-17 16:05:31 -050011676
James Smarte29d74f2018-03-05 12:04:07 -080011677 /* setup context3 field to pass wait_queue pointer to wake function */
11678 init_completion(&mbox_done);
11679 pmboxq->context3 = &mbox_done;
dea31012005-04-17 16:05:31 -050011680 /* now issue the command */
11681 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011682 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smarte29d74f2018-03-05 12:04:07 -080011683 wait_for_completion_timeout(&mbox_done,
11684 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011685
James Smart858c9f62007-06-17 19:56:39 -050011686 spin_lock_irqsave(&phba->hbalock, flag);
James Smarte29d74f2018-03-05 12:04:07 -080011687 pmboxq->context3 = NULL;
James Smart7054a602007-04-25 09:52:34 -040011688 /*
11689 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11690 * else do not free the resources.
11691 */
James Smartd7c47992010-06-08 18:31:54 -040011692 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011693 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011694 } else {
James Smart7054a602007-04-25 09:52:34 -040011695 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011696 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11697 }
11698 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050011699 }
dea31012005-04-17 16:05:31 -050011700 return retval;
11701}
11702
James Smarte59058c2008-08-24 21:49:00 -040011703/**
James Smart3772a992009-05-22 14:50:54 -040011704 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011705 * @phba: Pointer to HBA context.
11706 *
James Smart3772a992009-05-22 14:50:54 -040011707 * This function is called to shutdown the driver's mailbox sub-system.
11708 * It first marks the mailbox sub-system is in a block state to prevent
11709 * the asynchronous mailbox command from issued off the pending mailbox
11710 * command queue. If the mailbox command sub-system shutdown is due to
11711 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11712 * the mailbox sub-system flush routine to forcefully bring down the
11713 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11714 * as with offline or HBA function reset), this routine will wait for the
11715 * outstanding mailbox command to complete before invoking the mailbox
11716 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040011717 **/
James Smart3772a992009-05-22 14:50:54 -040011718void
James Smart618a5232012-06-12 13:54:36 -040011719lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040011720{
James Smart3772a992009-05-22 14:50:54 -040011721 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040011722 unsigned long timeout;
11723
James Smart618a5232012-06-12 13:54:36 -040011724 if (mbx_action == LPFC_MBX_NO_WAIT) {
11725 /* delay 100ms for port state */
11726 msleep(100);
11727 lpfc_sli_mbox_sys_flush(phba);
11728 return;
11729 }
James Smarta183a152011-10-10 21:32:43 -040011730 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050011731
James Smart3772a992009-05-22 14:50:54 -040011732 spin_lock_irq(&phba->hbalock);
11733 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040011734
11735 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040011736 /* Determine how long we might wait for the active mailbox
11737 * command to be gracefully completed by firmware.
11738 */
James Smarta183a152011-10-10 21:32:43 -040011739 if (phba->sli.mbox_active)
11740 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11741 phba->sli.mbox_active) *
11742 1000) + jiffies;
11743 spin_unlock_irq(&phba->hbalock);
11744
James Smart3772a992009-05-22 14:50:54 -040011745 while (phba->sli.mbox_active) {
11746 /* Check active mailbox complete status every 2ms */
11747 msleep(2);
11748 if (time_after(jiffies, timeout))
11749 /* Timeout, let the mailbox flush routine to
11750 * forcefully release active mailbox command
11751 */
11752 break;
11753 }
James Smartd7069f02012-03-01 22:36:29 -050011754 } else
11755 spin_unlock_irq(&phba->hbalock);
11756
James Smart3772a992009-05-22 14:50:54 -040011757 lpfc_sli_mbox_sys_flush(phba);
11758}
11759
11760/**
11761 * lpfc_sli_eratt_read - read sli-3 error attention events
11762 * @phba: Pointer to HBA context.
11763 *
11764 * This function is called to read the SLI3 device error attention registers
11765 * for possible error attention events. The caller must hold the hostlock
11766 * with spin_lock_irq().
11767 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011768 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040011769 * Register and returns 0 otherwise.
11770 **/
11771static int
11772lpfc_sli_eratt_read(struct lpfc_hba *phba)
11773{
James Smarted957682007-06-17 19:56:37 -050011774 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040011775
James Smart3772a992009-05-22 14:50:54 -040011776 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050011777 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11778 goto unplug_err;
11779
James Smart3772a992009-05-22 14:50:54 -040011780 if (ha_copy & HA_ERATT) {
11781 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050011782 if (lpfc_sli_read_hs(phba))
11783 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040011784
James Smart3772a992009-05-22 14:50:54 -040011785 /* Check if there is a deferred error condition is active */
11786 if ((HS_FFER1 & phba->work_hs) &&
11787 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011788 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040011789 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040011790 /* Clear all interrupt enable conditions */
11791 writel(0, phba->HCregaddr);
11792 readl(phba->HCregaddr);
11793 }
11794
11795 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040011796 phba->work_ha |= HA_ERATT;
11797 /* Indicate polling handles this ERATT */
11798 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011799 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011800 }
James Smart3772a992009-05-22 14:50:54 -040011801 return 0;
James Smart9940b972011-03-11 16:06:12 -050011802
11803unplug_err:
11804 /* Set the driver HS work bitmap */
11805 phba->work_hs |= UNPLUG_ERR;
11806 /* Set the driver HA work bitmap */
11807 phba->work_ha |= HA_ERATT;
11808 /* Indicate polling handles this ERATT */
11809 phba->hba_flag |= HBA_ERATT_HANDLED;
11810 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011811}
11812
James Smarte59058c2008-08-24 21:49:00 -040011813/**
James Smartda0436e2009-05-22 14:51:39 -040011814 * lpfc_sli4_eratt_read - read sli-4 error attention events
11815 * @phba: Pointer to HBA context.
11816 *
11817 * This function is called to read the SLI4 device error attention registers
11818 * for possible error attention events. The caller must hold the hostlock
11819 * with spin_lock_irq().
11820 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011821 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040011822 * Register and returns 0 otherwise.
11823 **/
11824static int
11825lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11826{
11827 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050011828 uint32_t if_type, portsmphr;
11829 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040011830
James Smart2fcee4b2010-12-15 17:57:46 -050011831 /*
11832 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040011833 * registers for error attention. This can be changed later.
11834 */
James Smart2fcee4b2010-12-15 17:57:46 -050011835 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11836 switch (if_type) {
11837 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050011838 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11839 &uerr_sta_lo) ||
11840 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11841 &uerr_sta_hi)) {
11842 phba->work_hs |= UNPLUG_ERR;
11843 phba->work_ha |= HA_ERATT;
11844 phba->hba_flag |= HBA_ERATT_HANDLED;
11845 return 1;
11846 }
James Smart2fcee4b2010-12-15 17:57:46 -050011847 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11848 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11849 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11850 "1423 HBA Unrecoverable error: "
11851 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11852 "ue_mask_lo_reg=0x%x, "
11853 "ue_mask_hi_reg=0x%x\n",
11854 uerr_sta_lo, uerr_sta_hi,
11855 phba->sli4_hba.ue_mask_lo,
11856 phba->sli4_hba.ue_mask_hi);
11857 phba->work_status[0] = uerr_sta_lo;
11858 phba->work_status[1] = uerr_sta_hi;
11859 phba->work_ha |= HA_ERATT;
11860 phba->hba_flag |= HBA_ERATT_HANDLED;
11861 return 1;
11862 }
11863 break;
11864 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080011865 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050011866 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11867 &portstat_reg.word0) ||
11868 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11869 &portsmphr)){
11870 phba->work_hs |= UNPLUG_ERR;
11871 phba->work_ha |= HA_ERATT;
11872 phba->hba_flag |= HBA_ERATT_HANDLED;
11873 return 1;
11874 }
James Smart2fcee4b2010-12-15 17:57:46 -050011875 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11876 phba->work_status[0] =
11877 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11878 phba->work_status[1] =
11879 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11880 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011881 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011882 "port status reg 0x%x, "
11883 "port smphr reg 0x%x, "
11884 "error 1=0x%x, error 2=0x%x\n",
11885 portstat_reg.word0,
11886 portsmphr,
11887 phba->work_status[0],
11888 phba->work_status[1]);
11889 phba->work_ha |= HA_ERATT;
11890 phba->hba_flag |= HBA_ERATT_HANDLED;
11891 return 1;
11892 }
11893 break;
11894 case LPFC_SLI_INTF_IF_TYPE_1:
11895 default:
James Smarta747c9c2009-11-18 15:41:10 -050011896 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011897 "2886 HBA Error Attention on unsupported "
11898 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011899 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011900 }
James Smart2fcee4b2010-12-15 17:57:46 -050011901
James Smartda0436e2009-05-22 14:51:39 -040011902 return 0;
11903}
11904
11905/**
James Smart3621a712009-04-06 18:47:14 -040011906 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011907 * @phba: Pointer to HBA context.
11908 *
James Smart3772a992009-05-22 14:50:54 -040011909 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011910 * error attention register bit for error attention events.
11911 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011912 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011913 * Register and returns 0 otherwise.
11914 **/
11915int
11916lpfc_sli_check_eratt(struct lpfc_hba *phba)
11917{
11918 uint32_t ha_copy;
11919
11920 /* If somebody is waiting to handle an eratt, don't process it
11921 * here. The brdkill function will do this.
11922 */
11923 if (phba->link_flag & LS_IGNORE_ERATT)
11924 return 0;
11925
11926 /* Check if interrupt handler handles this ERATT */
11927 spin_lock_irq(&phba->hbalock);
11928 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11929 /* Interrupt handler has handled ERATT */
11930 spin_unlock_irq(&phba->hbalock);
11931 return 0;
11932 }
11933
James Smarta257bf92009-04-06 18:48:10 -040011934 /*
11935 * If there is deferred error attention, do not check for error
11936 * attention
11937 */
11938 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11939 spin_unlock_irq(&phba->hbalock);
11940 return 0;
11941 }
11942
James Smart3772a992009-05-22 14:50:54 -040011943 /* If PCI channel is offline, don't process it */
11944 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011945 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011946 return 0;
11947 }
11948
11949 switch (phba->sli_rev) {
11950 case LPFC_SLI_REV2:
11951 case LPFC_SLI_REV3:
11952 /* Read chip Host Attention (HA) register */
11953 ha_copy = lpfc_sli_eratt_read(phba);
11954 break;
James Smartda0436e2009-05-22 14:51:39 -040011955 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011956 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011957 ha_copy = lpfc_sli4_eratt_read(phba);
11958 break;
James Smart3772a992009-05-22 14:50:54 -040011959 default:
11960 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11961 "0299 Invalid SLI revision (%d)\n",
11962 phba->sli_rev);
11963 ha_copy = 0;
11964 break;
James Smart93996272008-08-24 21:50:30 -040011965 }
11966 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011967
11968 return ha_copy;
11969}
11970
11971/**
11972 * lpfc_intr_state_check - Check device state for interrupt handling
11973 * @phba: Pointer to HBA context.
11974 *
11975 * This inline routine checks whether a device or its PCI slot is in a state
11976 * that the interrupt should be handled.
11977 *
11978 * This function returns 0 if the device or the PCI slot is in a state that
11979 * interrupt should be handled, otherwise -EIO.
11980 */
11981static inline int
11982lpfc_intr_state_check(struct lpfc_hba *phba)
11983{
11984 /* If the pci channel is offline, ignore all the interrupts */
11985 if (unlikely(pci_channel_offline(phba->pcidev)))
11986 return -EIO;
11987
11988 /* Update device level interrupt statistics */
11989 phba->sli.slistat.sli_intr++;
11990
11991 /* Ignore all interrupts during initialization. */
11992 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11993 return -EIO;
11994
James Smart93996272008-08-24 21:50:30 -040011995 return 0;
11996}
11997
11998/**
James Smart3772a992009-05-22 14:50:54 -040011999 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040012000 * @irq: Interrupt number.
12001 * @dev_id: The device context pointer.
12002 *
James Smart93996272008-08-24 21:50:30 -040012003 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012004 * service routine when device with SLI-3 interface spec is enabled with
12005 * MSI-X multi-message interrupt mode and there are slow-path events in
12006 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12007 * interrupt mode, this function is called as part of the device-level
12008 * interrupt handler. When the PCI slot is in error recovery or the HBA
12009 * is undergoing initialization, the interrupt handler will not process
12010 * the interrupt. The link attention and ELS ring attention events are
12011 * handled by the worker thread. The interrupt handler signals the worker
12012 * thread and returns for these events. This function is called without
12013 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040012014 * structures.
12015 *
12016 * This function returns IRQ_HANDLED when interrupt is handled else it
12017 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040012018 **/
dea31012005-04-17 16:05:31 -050012019irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012020lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050012021{
James Smart2e0fef82007-06-17 19:56:36 -050012022 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050012023 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050012024 uint32_t work_ha_copy;
12025 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012026 unsigned long iflag;
dea31012005-04-17 16:05:31 -050012027 uint32_t control;
12028
James Smart92d7f7b2007-06-17 19:56:38 -050012029 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050012030 struct lpfc_vport *vport;
12031 struct lpfc_nodelist *ndlp;
12032 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050012033 LPFC_MBOXQ_t *pmb;
12034 int rc;
12035
dea31012005-04-17 16:05:31 -050012036 /*
12037 * Get the driver's phba structure from the dev_id and
12038 * assume the HBA is not interrupting.
12039 */
James Smart93996272008-08-24 21:50:30 -040012040 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050012041
12042 if (unlikely(!phba))
12043 return IRQ_NONE;
12044
dea31012005-04-17 16:05:31 -050012045 /*
James Smart93996272008-08-24 21:50:30 -040012046 * Stuff needs to be attented to when this function is invoked as an
12047 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012048 */
James Smart93996272008-08-24 21:50:30 -040012049 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012050 /* Check device state for handling interrupt */
12051 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012052 return IRQ_NONE;
12053 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050012054 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012055 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12056 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040012057 /* If somebody is waiting to handle an eratt don't process it
12058 * here. The brdkill function will do this.
12059 */
12060 if (phba->link_flag & LS_IGNORE_ERATT)
12061 ha_copy &= ~HA_ERATT;
12062 /* Check the need for handling ERATT in interrupt handler */
12063 if (ha_copy & HA_ERATT) {
12064 if (phba->hba_flag & HBA_ERATT_HANDLED)
12065 /* ERATT polling has handled ERATT */
12066 ha_copy &= ~HA_ERATT;
12067 else
12068 /* Indicate interrupt handler handles ERATT */
12069 phba->hba_flag |= HBA_ERATT_HANDLED;
12070 }
James Smarta257bf92009-04-06 18:48:10 -040012071
12072 /*
12073 * If there is deferred error attention, do not check for any
12074 * interrupt.
12075 */
12076 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012077 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012078 return IRQ_NONE;
12079 }
12080
James Smart93996272008-08-24 21:50:30 -040012081 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050012082 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12083 goto unplug_error;
12084
James Smarta747c9c2009-11-18 15:41:10 -050012085 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12086 HC_LAINT_ENA | HC_ERINT_ENA),
12087 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012088 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12089 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012090 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012091 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012092 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012093 } else
12094 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050012095
dea31012005-04-17 16:05:31 -050012096 work_ha_copy = ha_copy & phba->work_ha_mask;
12097
James Smart93996272008-08-24 21:50:30 -040012098 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050012099 if (work_ha_copy & HA_LATT) {
12100 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12101 /*
12102 * Turn off Link Attention interrupts
12103 * until CLEAR_LA done
12104 */
James Smart5b75da22008-12-04 22:39:35 -050012105 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012106 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050012107 if (lpfc_readl(phba->HCregaddr, &control))
12108 goto unplug_error;
dea31012005-04-17 16:05:31 -050012109 control &= ~HC_LAINT_ENA;
12110 writel(control, phba->HCregaddr);
12111 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012112 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012113 }
12114 else
12115 work_ha_copy &= ~HA_LATT;
12116 }
12117
James Smart93996272008-08-24 21:50:30 -040012118 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050012119 /*
12120 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12121 * the only slow ring.
12122 */
12123 status = (work_ha_copy &
12124 (HA_RXMASK << (4*LPFC_ELS_RING)));
12125 status >>= (4*LPFC_ELS_RING);
12126 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050012127 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012128 if (lpfc_readl(phba->HCregaddr, &control))
12129 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040012130
12131 lpfc_debugfs_slow_ring_trc(phba,
12132 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12133 control, status,
12134 (uint32_t)phba->sli.slistat.sli_intr);
12135
James Smart858c9f62007-06-17 19:56:39 -050012136 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040012137 lpfc_debugfs_slow_ring_trc(phba,
12138 "ISR Disable ring:"
12139 "pwork:x%x hawork:x%x wait:x%x",
12140 phba->work_ha, work_ha_copy,
12141 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012142 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012143
James Smart858c9f62007-06-17 19:56:39 -050012144 control &=
12145 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050012146 writel(control, phba->HCregaddr);
12147 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050012148 }
James Smarta58cbd52007-08-02 11:09:43 -040012149 else {
12150 lpfc_debugfs_slow_ring_trc(phba,
12151 "ISR slow ring: pwork:"
12152 "x%x hawork:x%x wait:x%x",
12153 phba->work_ha, work_ha_copy,
12154 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012155 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012156 }
James Smart5b75da22008-12-04 22:39:35 -050012157 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012158 }
12159 }
James Smart5b75da22008-12-04 22:39:35 -050012160 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012161 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050012162 if (lpfc_sli_read_hs(phba))
12163 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040012164 /*
12165 * Check if there is a deferred error condition
12166 * is active
12167 */
12168 if ((HS_FFER1 & phba->work_hs) &&
12169 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012170 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12171 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040012172 phba->hba_flag |= DEFER_ERATT;
12173 /* Clear all interrupt enable conditions */
12174 writel(0, phba->HCregaddr);
12175 readl(phba->HCregaddr);
12176 }
12177 }
12178
James Smart93996272008-08-24 21:50:30 -040012179 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050012180 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040012181 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040012182 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050012183 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050012184
12185 /* First check out the status word */
12186 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12187 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050012188 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050012189 /*
12190 * Stray Mailbox Interrupt, mbxCommand <cmd>
12191 * mbxStatus <status>
12192 */
James Smart09372822008-01-11 01:52:54 -050012193 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012194 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012195 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012196 "Interrupt mbxCommand x%x "
12197 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012198 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012199 pmbox->mbxCommand,
12200 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012201 /* clear mailbox attention bit */
12202 work_ha_copy &= ~HA_MBATT;
12203 } else {
James Smart97eab632008-04-07 10:16:05 -040012204 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012205 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012206 phba->last_completion_time = jiffies;
12207 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012208 if (pmb->mbox_cmpl) {
12209 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12210 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012211 if (pmb->out_ext_byte_len &&
12212 pmb->context2)
12213 lpfc_sli_pcimem_bcopy(
12214 phba->mbox_ext,
12215 pmb->context2,
12216 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012217 }
James Smart09372822008-01-11 01:52:54 -050012218 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12219 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12220
12221 lpfc_debugfs_disc_trc(vport,
12222 LPFC_DISC_TRC_MBOX_VPORT,
12223 "MBOX dflt rpi: : "
12224 "status:x%x rpi:x%x",
12225 (uint32_t)pmbox->mbxStatus,
12226 pmbox->un.varWords[0], 0);
12227
12228 if (!pmbox->mbxStatus) {
12229 mp = (struct lpfc_dmabuf *)
12230 (pmb->context1);
12231 ndlp = (struct lpfc_nodelist *)
12232 pmb->context2;
12233
12234 /* Reg_LOGIN of dflt RPI was
12235 * successful. new lets get
12236 * rid of the RPI using the
12237 * same mbox buffer.
12238 */
12239 lpfc_unreg_login(phba,
12240 vport->vpi,
12241 pmbox->un.varWords[0],
12242 pmb);
12243 pmb->mbox_cmpl =
12244 lpfc_mbx_cmpl_dflt_rpi;
12245 pmb->context1 = mp;
12246 pmb->context2 = ndlp;
12247 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012248 rc = lpfc_sli_issue_mbox(phba,
12249 pmb,
12250 MBX_NOWAIT);
12251 if (rc != MBX_BUSY)
12252 lpfc_printf_log(phba,
12253 KERN_ERR,
12254 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012255 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012256 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012257 if (rc != MBX_NOT_FINISHED)
12258 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012259 }
12260 }
James Smart5b75da22008-12-04 22:39:35 -050012261 spin_lock_irqsave(
12262 &phba->pport->work_port_lock,
12263 iflag);
James Smart09372822008-01-11 01:52:54 -050012264 phba->pport->work_port_events &=
12265 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012266 spin_unlock_irqrestore(
12267 &phba->pport->work_port_lock,
12268 iflag);
James Smart09372822008-01-11 01:52:54 -050012269 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012270 }
James Smart97eab632008-04-07 10:16:05 -040012271 } else
James Smart5b75da22008-12-04 22:39:35 -050012272 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012273
James Smart92d7f7b2007-06-17 19:56:38 -050012274 if ((work_ha_copy & HA_MBATT) &&
12275 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012276send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012277 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012278 do {
12279 rc = lpfc_sli_issue_mbox(phba, NULL,
12280 MBX_NOWAIT);
12281 } while (rc == MBX_NOT_FINISHED);
12282 if (rc != MBX_SUCCESS)
12283 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12284 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012285 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012286 }
12287
James Smart5b75da22008-12-04 22:39:35 -050012288 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012289 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012290 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012291 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012292 }
James Smart93996272008-08-24 21:50:30 -040012293 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012294unplug_error:
12295 spin_unlock_irqrestore(&phba->hbalock, iflag);
12296 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012297
James Smart3772a992009-05-22 14:50:54 -040012298} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012299
12300/**
James Smart3772a992009-05-22 14:50:54 -040012301 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012302 * @irq: Interrupt number.
12303 * @dev_id: The device context pointer.
12304 *
12305 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012306 * service routine when device with SLI-3 interface spec is enabled with
12307 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12308 * ring event in the HBA. However, when the device is enabled with either
12309 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12310 * device-level interrupt handler. When the PCI slot is in error recovery
12311 * or the HBA is undergoing initialization, the interrupt handler will not
12312 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12313 * the intrrupt context. This function is called without any lock held.
12314 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012315 *
12316 * This function returns IRQ_HANDLED when interrupt is handled else it
12317 * returns IRQ_NONE.
12318 **/
12319irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012320lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012321{
12322 struct lpfc_hba *phba;
12323 uint32_t ha_copy;
12324 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012325 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012326 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012327
12328 /* Get the driver's phba structure from the dev_id and
12329 * assume the HBA is not interrupting.
12330 */
12331 phba = (struct lpfc_hba *) dev_id;
12332
12333 if (unlikely(!phba))
12334 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012335
12336 /*
James Smart93996272008-08-24 21:50:30 -040012337 * Stuff needs to be attented to when this function is invoked as an
12338 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012339 */
James Smart93996272008-08-24 21:50:30 -040012340 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012341 /* Check device state for handling interrupt */
12342 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012343 return IRQ_NONE;
12344 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012345 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12346 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012347 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012348 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012349 /*
12350 * If there is deferred error attention, do not check for
12351 * any interrupt.
12352 */
12353 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012354 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012355 return IRQ_NONE;
12356 }
James Smart93996272008-08-24 21:50:30 -040012357 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12358 phba->HAregaddr);
12359 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012360 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012361 } else
12362 ha_copy = phba->ha_copy;
12363
12364 /*
12365 * Process all events on FCP ring. Take the optimized path for FCP IO.
12366 */
12367 ha_copy &= ~(phba->work_ha_mask);
12368
12369 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012370 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012371 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012372 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012373 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012374
12375 if (phba->cfg_multi_ring_support == 2) {
12376 /*
James Smart93996272008-08-24 21:50:30 -040012377 * Process all events on extra ring. Take the optimized path
12378 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012379 */
James Smart93996272008-08-24 21:50:30 -040012380 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012381 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012382 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012383 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012384 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012385 status);
12386 }
12387 }
dea31012005-04-17 16:05:31 -050012388 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012389} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012390
James Smart93996272008-08-24 21:50:30 -040012391/**
James Smart3772a992009-05-22 14:50:54 -040012392 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012393 * @irq: Interrupt number.
12394 * @dev_id: The device context pointer.
12395 *
James Smart3772a992009-05-22 14:50:54 -040012396 * This function is the HBA device-level interrupt handler to device with
12397 * SLI-3 interface spec, called from the PCI layer when either MSI or
12398 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12399 * requires driver attention. This function invokes the slow-path interrupt
12400 * attention handling function and fast-path interrupt attention handling
12401 * function in turn to process the relevant HBA attention events. This
12402 * function is called without any lock held. It gets the hbalock to access
12403 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012404 *
12405 * This function returns IRQ_HANDLED when interrupt is handled, else it
12406 * returns IRQ_NONE.
12407 **/
12408irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012409lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012410{
12411 struct lpfc_hba *phba;
12412 irqreturn_t sp_irq_rc, fp_irq_rc;
12413 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012414 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012415
12416 /*
12417 * Get the driver's phba structure from the dev_id and
12418 * assume the HBA is not interrupting.
12419 */
12420 phba = (struct lpfc_hba *) dev_id;
12421
12422 if (unlikely(!phba))
12423 return IRQ_NONE;
12424
James Smart3772a992009-05-22 14:50:54 -040012425 /* Check device state for handling interrupt */
12426 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012427 return IRQ_NONE;
12428
12429 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012430 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12431 spin_unlock(&phba->hbalock);
12432 return IRQ_HANDLED;
12433 }
12434
James Smart93996272008-08-24 21:50:30 -040012435 if (unlikely(!phba->ha_copy)) {
12436 spin_unlock(&phba->hbalock);
12437 return IRQ_NONE;
12438 } else if (phba->ha_copy & HA_ERATT) {
12439 if (phba->hba_flag & HBA_ERATT_HANDLED)
12440 /* ERATT polling has handled ERATT */
12441 phba->ha_copy &= ~HA_ERATT;
12442 else
12443 /* Indicate interrupt handler handles ERATT */
12444 phba->hba_flag |= HBA_ERATT_HANDLED;
12445 }
12446
James Smarta257bf92009-04-06 18:48:10 -040012447 /*
12448 * If there is deferred error attention, do not check for any interrupt.
12449 */
12450 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012451 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012452 return IRQ_NONE;
12453 }
12454
James Smart93996272008-08-24 21:50:30 -040012455 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012456 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12457 spin_unlock(&phba->hbalock);
12458 return IRQ_HANDLED;
12459 }
James Smarta747c9c2009-11-18 15:41:10 -050012460 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12461 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12462 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012463 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012464 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012465 readl(phba->HAregaddr); /* flush */
12466 spin_unlock(&phba->hbalock);
12467
12468 /*
12469 * Invokes slow-path host attention interrupt handling as appropriate.
12470 */
12471
12472 /* status of events with mailbox and link attention */
12473 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12474
12475 /* status of events with ELS ring */
12476 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12477 status2 >>= (4*LPFC_ELS_RING);
12478
12479 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012480 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012481 else
12482 sp_irq_rc = IRQ_NONE;
12483
12484 /*
12485 * Invoke fast-path host attention interrupt handling as appropriate.
12486 */
12487
12488 /* status of events with FCP ring */
12489 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12490 status1 >>= (4*LPFC_FCP_RING);
12491
12492 /* status of events with extra ring */
12493 if (phba->cfg_multi_ring_support == 2) {
12494 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12495 status2 >>= (4*LPFC_EXTRA_RING);
12496 } else
12497 status2 = 0;
12498
12499 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012500 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012501 else
12502 fp_irq_rc = IRQ_NONE;
12503
12504 /* Return device-level interrupt handling status */
12505 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012506} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012507
12508/**
12509 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12510 * @phba: pointer to lpfc hba data structure.
12511 *
12512 * This routine is invoked by the worker thread to process all the pending
12513 * SLI4 FCP abort XRI events.
12514 **/
12515void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12516{
12517 struct lpfc_cq_event *cq_event;
12518
12519 /* First, declare the fcp xri abort event has been handled */
12520 spin_lock_irq(&phba->hbalock);
12521 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12522 spin_unlock_irq(&phba->hbalock);
12523 /* Now, handle all the fcp xri abort events */
12524 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12525 /* Get the first event from the head of the event queue */
12526 spin_lock_irq(&phba->hbalock);
12527 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12528 cq_event, struct lpfc_cq_event, list);
12529 spin_unlock_irq(&phba->hbalock);
12530 /* Notify aborted XRI for FCP work queue */
12531 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12532 /* Free the event processed back to the free pool */
12533 lpfc_sli4_cq_event_release(phba, cq_event);
12534 }
12535}
12536
12537/**
12538 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12539 * @phba: pointer to lpfc hba data structure.
12540 *
12541 * This routine is invoked by the worker thread to process all the pending
12542 * SLI4 els abort xri events.
12543 **/
12544void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12545{
12546 struct lpfc_cq_event *cq_event;
12547
12548 /* First, declare the els xri abort event has been handled */
12549 spin_lock_irq(&phba->hbalock);
12550 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12551 spin_unlock_irq(&phba->hbalock);
12552 /* Now, handle all the els xri abort events */
12553 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12554 /* Get the first event from the head of the event queue */
12555 spin_lock_irq(&phba->hbalock);
12556 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12557 cq_event, struct lpfc_cq_event, list);
12558 spin_unlock_irq(&phba->hbalock);
12559 /* Notify aborted XRI for ELS work queue */
12560 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12561 /* Free the event processed back to the free pool */
12562 lpfc_sli4_cq_event_release(phba, cq_event);
12563 }
12564}
12565
James Smart341af102010-01-26 23:07:37 -050012566/**
12567 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12568 * @phba: pointer to lpfc hba data structure
12569 * @pIocbIn: pointer to the rspiocbq
12570 * @pIocbOut: pointer to the cmdiocbq
12571 * @wcqe: pointer to the complete wcqe
12572 *
12573 * This routine transfers the fields of a command iocbq to a response iocbq
12574 * by copying all the IOCB fields from command iocbq and transferring the
12575 * completion status information from the complete wcqe.
12576 **/
James Smart4f774512009-05-22 14:52:35 -040012577static void
James Smart341af102010-01-26 23:07:37 -050012578lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12579 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012580 struct lpfc_iocbq *pIocbOut,
12581 struct lpfc_wcqe_complete *wcqe)
12582{
James Smartaf227412013-10-10 12:23:10 -040012583 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012584 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012585 uint32_t status, max_response;
12586 struct lpfc_dmabuf *dmabuf;
12587 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012588 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12589
12590 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12591 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012592 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012593 status = bf_get(lpfc_wcqe_c_status, wcqe);
12594 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012595 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12596 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12597 pIocbIn->iocb.un.fcpi.fcpi_parm =
12598 pIocbOut->iocb.un.fcpi.fcpi_parm -
12599 wcqe->total_data_placed;
12600 else
12601 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012602 else {
James Smart4f774512009-05-22 14:52:35 -040012603 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012604 switch (pIocbOut->iocb.ulpCommand) {
12605 case CMD_ELS_REQUEST64_CR:
12606 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12607 bpl = (struct ulp_bde64 *)dmabuf->virt;
12608 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12609 max_response = bde.tus.f.bdeSize;
12610 break;
12611 case CMD_GEN_REQUEST64_CR:
12612 max_response = 0;
12613 if (!pIocbOut->context3)
12614 break;
12615 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12616 sizeof(struct ulp_bde64);
12617 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12618 bpl = (struct ulp_bde64 *)dmabuf->virt;
12619 for (i = 0; i < numBdes; i++) {
12620 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12621 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12622 max_response += bde.tus.f.bdeSize;
12623 }
12624 break;
12625 default:
12626 max_response = wcqe->total_data_placed;
12627 break;
12628 }
12629 if (max_response < wcqe->total_data_placed)
12630 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12631 else
12632 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12633 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012634 }
James Smart341af102010-01-26 23:07:37 -050012635
James Smartacd68592012-01-18 16:25:09 -050012636 /* Convert BG errors for completion status */
12637 if (status == CQE_STATUS_DI_ERROR) {
12638 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12639
12640 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12641 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12642 else
12643 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12644
12645 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12646 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12647 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12648 BGS_GUARD_ERR_MASK;
12649 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12650 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12651 BGS_APPTAG_ERR_MASK;
12652 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12653 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12654 BGS_REFTAG_ERR_MASK;
12655
12656 /* Check to see if there was any good data before the error */
12657 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12658 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12659 BGS_HI_WATER_MARK_PRESENT_MASK;
12660 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12661 wcqe->total_data_placed;
12662 }
12663
12664 /*
12665 * Set ALL the error bits to indicate we don't know what
12666 * type of error it is.
12667 */
12668 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12669 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12670 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12671 BGS_GUARD_ERR_MASK);
12672 }
12673
James Smart341af102010-01-26 23:07:37 -050012674 /* Pick up HBA exchange busy condition */
12675 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12676 spin_lock_irqsave(&phba->hbalock, iflags);
12677 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12678 spin_unlock_irqrestore(&phba->hbalock, iflags);
12679 }
James Smart4f774512009-05-22 14:52:35 -040012680}
12681
12682/**
James Smart45ed1192009-10-02 15:17:02 -040012683 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12684 * @phba: Pointer to HBA context object.
12685 * @wcqe: Pointer to work-queue completion queue entry.
12686 *
12687 * This routine handles an ELS work-queue completion event and construct
12688 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12689 * discovery engine to handle.
12690 *
12691 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12692 **/
12693static struct lpfc_iocbq *
12694lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12695 struct lpfc_iocbq *irspiocbq)
12696{
James Smart895427b2017-02-12 13:52:30 -080012697 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012698 struct lpfc_iocbq *cmdiocbq;
12699 struct lpfc_wcqe_complete *wcqe;
12700 unsigned long iflags;
12701
James Smart895427b2017-02-12 13:52:30 -080012702 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070012703 if (unlikely(!pring))
12704 return NULL;
James Smart895427b2017-02-12 13:52:30 -080012705
James Smart45ed1192009-10-02 15:17:02 -040012706 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012707 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012708 pring->stats.iocb_event++;
12709 /* Look up the ELS command IOCB and create pseudo response IOCB */
12710 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12711 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040012712 if (unlikely(!cmdiocbq)) {
Dick Kennedy401bb412017-09-29 17:34:28 -070012713 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012714 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12715 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070012716 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12717 wcqe->word0, wcqe->total_data_placed,
12718 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040012719 lpfc_sli_release_iocbq(phba, irspiocbq);
12720 return NULL;
12721 }
12722
Dick Kennedy401bb412017-09-29 17:34:28 -070012723 /* Put the iocb back on the txcmplq */
12724 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12725 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12726
James Smart45ed1192009-10-02 15:17:02 -040012727 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012728 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040012729
12730 return irspiocbq;
12731}
12732
James Smart8a5ca102017-11-20 16:00:30 -080012733inline struct lpfc_cq_event *
12734lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12735{
12736 struct lpfc_cq_event *cq_event;
12737
12738 /* Allocate a new internal CQ_EVENT entry */
12739 cq_event = lpfc_sli4_cq_event_alloc(phba);
12740 if (!cq_event) {
12741 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12742 "0602 Failed to alloc CQ_EVENT entry\n");
12743 return NULL;
12744 }
12745
12746 /* Move the CQE into the event */
12747 memcpy(&cq_event->cqe, entry, size);
12748 return cq_event;
12749}
12750
James Smart45ed1192009-10-02 15:17:02 -040012751/**
James Smart04c68492009-05-22 14:52:52 -040012752 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12753 * @phba: Pointer to HBA context object.
12754 * @cqe: Pointer to mailbox completion queue entry.
12755 *
12756 * This routine process a mailbox completion queue entry with asynchrous
12757 * event.
12758 *
12759 * Return: true if work posted to worker thread, otherwise false.
12760 **/
12761static bool
12762lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12763{
12764 struct lpfc_cq_event *cq_event;
12765 unsigned long iflags;
12766
12767 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12768 "0392 Async Event: word0:x%x, word1:x%x, "
12769 "word2:x%x, word3:x%x\n", mcqe->word0,
12770 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12771
James Smart8a5ca102017-11-20 16:00:30 -080012772 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12773 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040012774 return false;
James Smart04c68492009-05-22 14:52:52 -040012775 spin_lock_irqsave(&phba->hbalock, iflags);
12776 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12777 /* Set the async event flag */
12778 phba->hba_flag |= ASYNC_EVENT;
12779 spin_unlock_irqrestore(&phba->hbalock, iflags);
12780
12781 return true;
12782}
12783
12784/**
12785 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12786 * @phba: Pointer to HBA context object.
12787 * @cqe: Pointer to mailbox completion queue entry.
12788 *
12789 * This routine process a mailbox completion queue entry with mailbox
12790 * completion event.
12791 *
12792 * Return: true if work posted to worker thread, otherwise false.
12793 **/
12794static bool
12795lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12796{
12797 uint32_t mcqe_status;
12798 MAILBOX_t *mbox, *pmbox;
12799 struct lpfc_mqe *mqe;
12800 struct lpfc_vport *vport;
12801 struct lpfc_nodelist *ndlp;
12802 struct lpfc_dmabuf *mp;
12803 unsigned long iflags;
12804 LPFC_MBOXQ_t *pmb;
12805 bool workposted = false;
12806 int rc;
12807
12808 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12809 if (!bf_get(lpfc_trailer_completed, mcqe))
12810 goto out_no_mqe_complete;
12811
12812 /* Get the reference to the active mbox command */
12813 spin_lock_irqsave(&phba->hbalock, iflags);
12814 pmb = phba->sli.mbox_active;
12815 if (unlikely(!pmb)) {
12816 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12817 "1832 No pending MBOX command to handle\n");
12818 spin_unlock_irqrestore(&phba->hbalock, iflags);
12819 goto out_no_mqe_complete;
12820 }
12821 spin_unlock_irqrestore(&phba->hbalock, iflags);
12822 mqe = &pmb->u.mqe;
12823 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12824 mbox = phba->mbox;
12825 vport = pmb->vport;
12826
12827 /* Reset heartbeat timer */
12828 phba->last_completion_time = jiffies;
12829 del_timer(&phba->sli.mbox_tmo);
12830
12831 /* Move mbox data to caller's mailbox region, do endian swapping */
12832 if (pmb->mbox_cmpl && mbox)
12833 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040012834
James Smart73d91e52011-10-10 21:32:10 -040012835 /*
12836 * For mcqe errors, conditionally move a modified error code to
12837 * the mbox so that the error will not be missed.
12838 */
12839 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12840 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12841 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12842 bf_set(lpfc_mqe_status, mqe,
12843 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12844 }
James Smart04c68492009-05-22 14:52:52 -040012845 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12846 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12847 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12848 "MBOX dflt rpi: status:x%x rpi:x%x",
12849 mcqe_status,
12850 pmbox->un.varWords[0], 0);
12851 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12852 mp = (struct lpfc_dmabuf *)(pmb->context1);
12853 ndlp = (struct lpfc_nodelist *)pmb->context2;
12854 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12855 * RID of the PPI using the same mbox buffer.
12856 */
12857 lpfc_unreg_login(phba, vport->vpi,
12858 pmbox->un.varWords[0], pmb);
12859 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12860 pmb->context1 = mp;
12861 pmb->context2 = ndlp;
12862 pmb->vport = vport;
12863 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12864 if (rc != MBX_BUSY)
12865 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12866 LOG_SLI, "0385 rc should "
12867 "have been MBX_BUSY\n");
12868 if (rc != MBX_NOT_FINISHED)
12869 goto send_current_mbox;
12870 }
12871 }
12872 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12873 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12874 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12875
12876 /* There is mailbox completion work to do */
12877 spin_lock_irqsave(&phba->hbalock, iflags);
12878 __lpfc_mbox_cmpl_put(phba, pmb);
12879 phba->work_ha |= HA_MBATT;
12880 spin_unlock_irqrestore(&phba->hbalock, iflags);
12881 workposted = true;
12882
12883send_current_mbox:
12884 spin_lock_irqsave(&phba->hbalock, iflags);
12885 /* Release the mailbox command posting token */
12886 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12887 /* Setting active mailbox pointer need to be in sync to flag clear */
12888 phba->sli.mbox_active = NULL;
12889 spin_unlock_irqrestore(&phba->hbalock, iflags);
12890 /* Wake up worker thread to post the next pending mailbox command */
12891 lpfc_worker_wake_up(phba);
12892out_no_mqe_complete:
12893 if (bf_get(lpfc_trailer_consumed, mcqe))
12894 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12895 return workposted;
12896}
12897
12898/**
12899 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12900 * @phba: Pointer to HBA context object.
12901 * @cqe: Pointer to mailbox completion queue entry.
12902 *
12903 * This routine process a mailbox completion queue entry, it invokes the
12904 * proper mailbox complete handling or asynchrous event handling routine
12905 * according to the MCQE's async bit.
12906 *
12907 * Return: true if work posted to worker thread, otherwise false.
12908 **/
12909static bool
12910lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12911{
12912 struct lpfc_mcqe mcqe;
12913 bool workposted;
12914
12915 /* Copy the mailbox MCQE and convert endian order as needed */
12916 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12917
12918 /* Invoke the proper event handling routine */
12919 if (!bf_get(lpfc_trailer_async, &mcqe))
12920 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12921 else
12922 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12923 return workposted;
12924}
12925
12926/**
James Smart4f774512009-05-22 14:52:35 -040012927 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12928 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012929 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012930 * @wcqe: Pointer to work-queue completion queue entry.
12931 *
12932 * This routine handles an ELS work-queue completion event.
12933 *
12934 * Return: true if work posted to worker thread, otherwise false.
12935 **/
12936static bool
James Smart2a76a282012-08-03 12:35:54 -040012937lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012938 struct lpfc_wcqe_complete *wcqe)
12939{
James Smart4f774512009-05-22 14:52:35 -040012940 struct lpfc_iocbq *irspiocbq;
12941 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012942 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012943 int txq_cnt = 0;
12944 int txcmplq_cnt = 0;
12945 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012946
James Smart45ed1192009-10-02 15:17:02 -040012947 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012948 irspiocbq = lpfc_sli_get_iocbq(phba);
12949 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012950 if (!list_empty(&pring->txq))
12951 txq_cnt++;
12952 if (!list_empty(&pring->txcmplq))
12953 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012954 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012955 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12956 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012957 txq_cnt, phba->iocb_cnt,
12958 fcp_txcmplq_cnt,
12959 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012960 return false;
James Smart4f774512009-05-22 14:52:35 -040012961 }
James Smart4f774512009-05-22 14:52:35 -040012962
James Smart45ed1192009-10-02 15:17:02 -040012963 /* Save off the slow-path queue event for work thread to process */
12964 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012965 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012966 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012967 &phba->sli4_hba.sp_queue_event);
12968 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012969 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012970
James Smart45ed1192009-10-02 15:17:02 -040012971 return true;
James Smart4f774512009-05-22 14:52:35 -040012972}
12973
12974/**
12975 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12976 * @phba: Pointer to HBA context object.
12977 * @wcqe: Pointer to work-queue completion queue entry.
12978 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080012979 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040012980 * proper WQ release routine to the slow-path WQ.
12981 **/
12982static void
12983lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12984 struct lpfc_wcqe_release *wcqe)
12985{
James Smart2e90f4b2011-12-13 13:22:37 -050012986 /* sanity check on queue memory */
12987 if (unlikely(!phba->sli4_hba.els_wq))
12988 return;
James Smart4f774512009-05-22 14:52:35 -040012989 /* Check for the slow-path ELS work queue */
12990 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12991 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12992 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12993 else
12994 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12995 "2579 Slow-path wqe consume event carries "
12996 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12997 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12998 phba->sli4_hba.els_wq->queue_id);
12999}
13000
13001/**
13002 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13003 * @phba: Pointer to HBA context object.
13004 * @cq: Pointer to a WQ completion queue.
13005 * @wcqe: Pointer to work-queue completion queue entry.
13006 *
13007 * This routine handles an XRI abort event.
13008 *
13009 * Return: true if work posted to worker thread, otherwise false.
13010 **/
13011static bool
13012lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13013 struct lpfc_queue *cq,
13014 struct sli4_wcqe_xri_aborted *wcqe)
13015{
13016 bool workposted = false;
13017 struct lpfc_cq_event *cq_event;
13018 unsigned long iflags;
13019
James Smart4f774512009-05-22 14:52:35 -040013020 switch (cq->subtype) {
13021 case LPFC_FCP:
James Smart8a5ca102017-11-20 16:00:30 -080013022 cq_event = lpfc_cq_event_setup(
13023 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13024 if (!cq_event)
13025 return false;
James Smart4f774512009-05-22 14:52:35 -040013026 spin_lock_irqsave(&phba->hbalock, iflags);
13027 list_add_tail(&cq_event->list,
13028 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
13029 /* Set the fcp xri abort event flag */
13030 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
13031 spin_unlock_irqrestore(&phba->hbalock, iflags);
13032 workposted = true;
13033 break;
James Smart422c4cb2017-11-20 16:00:32 -080013034 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040013035 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080013036 cq_event = lpfc_cq_event_setup(
13037 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13038 if (!cq_event)
13039 return false;
James Smart4f774512009-05-22 14:52:35 -040013040 spin_lock_irqsave(&phba->hbalock, iflags);
13041 list_add_tail(&cq_event->list,
13042 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13043 /* Set the els xri abort event flag */
13044 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13045 spin_unlock_irqrestore(&phba->hbalock, iflags);
13046 workposted = true;
13047 break;
James Smart318083a2017-03-04 09:30:30 -080013048 case LPFC_NVME:
James Smart8a5ca102017-11-20 16:00:30 -080013049 /* Notify aborted XRI for NVME work queue */
13050 if (phba->nvmet_support)
13051 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13052 else
13053 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
13054
13055 workposted = false;
James Smart318083a2017-03-04 09:30:30 -080013056 break;
James Smart4f774512009-05-22 14:52:35 -040013057 default:
13058 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080013059 "0603 Invalid CQ subtype %d: "
13060 "%08x %08x %08x %08x\n",
13061 cq->subtype, wcqe->word0, wcqe->parameter,
13062 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013063 workposted = false;
13064 break;
13065 }
13066 return workposted;
13067}
13068
13069/**
James Smart4d9ab992009-10-02 15:16:39 -040013070 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040013071 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040013072 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013073 *
James Smart4d9ab992009-10-02 15:16:39 -040013074 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013075 *
13076 * Return: true if work posted to worker thread, otherwise false.
13077 **/
13078static bool
James Smart4d9ab992009-10-02 15:16:39 -040013079lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13080{
13081 bool workposted = false;
James Smart895427b2017-02-12 13:52:30 -080013082 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040013083 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13084 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070013085 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040013086 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040013087 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040013088 unsigned long iflags;
13089
James Smart2e90f4b2011-12-13 13:22:37 -050013090 /* sanity check on queue memory */
13091 if (unlikely(!hrq) || unlikely(!drq))
13092 return workposted;
13093
James Smart7851fe22011-07-22 18:36:52 -040013094 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13095 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13096 else
13097 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13098 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040013099 goto out;
13100
13101 status = bf_get(lpfc_rcqe_status, rcqe);
13102 switch (status) {
13103 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13104 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13105 "2537 Receive Frame Truncated!!\n");
13106 case FC_STATUS_RQ_SUCCESS:
13107 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013108 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040013109 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13110 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040013111 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040013112 spin_unlock_irqrestore(&phba->hbalock, iflags);
13113 goto out;
13114 }
James Smartb84daac2012-08-03 12:35:13 -040013115 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013116 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040013117 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080013118
13119 /* If a NVME LS event (type 0x28), treat it as Fast path */
13120 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13121
James Smart4d9ab992009-10-02 15:16:39 -040013122 /* save off the frame for the word thread to process */
13123 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013124 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040013125 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040013126 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040013127 spin_unlock_irqrestore(&phba->hbalock, iflags);
13128 workposted = true;
13129 break;
James Smart4d9ab992009-10-02 15:16:39 -040013130 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013131 if (phba->nvmet_support) {
13132 tgtp = phba->targetport->private;
13133 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13134 "6402 RQE Error x%x, posted %d err_cnt "
13135 "%d: %x %x %x\n",
13136 status, hrq->RQ_buf_posted,
13137 hrq->RQ_no_posted_buf,
13138 atomic_read(&tgtp->rcv_fcp_cmd_in),
13139 atomic_read(&tgtp->rcv_fcp_cmd_out),
13140 atomic_read(&tgtp->xmt_fcp_release));
13141 }
13142 /* fallthrough */
13143
13144 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040013145 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040013146 /* Post more buffers if possible */
13147 spin_lock_irqsave(&phba->hbalock, iflags);
13148 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13149 spin_unlock_irqrestore(&phba->hbalock, iflags);
13150 workposted = true;
13151 break;
13152 }
13153out:
13154 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040013155}
13156
13157/**
13158 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13159 * @phba: Pointer to HBA context object.
13160 * @cq: Pointer to the completion queue.
13161 * @wcqe: Pointer to a completion queue entry.
13162 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013163 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040013164 * entry.
13165 *
13166 * Return: true if work posted to worker thread, otherwise false.
13167 **/
13168static bool
13169lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013170 struct lpfc_cqe *cqe)
13171{
James Smart45ed1192009-10-02 15:17:02 -040013172 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040013173 bool workposted = false;
13174
13175 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040013176 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013177
13178 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040013179 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040013180 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040013181 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040013182 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040013183 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013184 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013185 break;
13186 case CQE_CODE_RELEASE_WQE:
13187 /* Process the WQ release event */
13188 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013189 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013190 break;
13191 case CQE_CODE_XRI_ABORTED:
13192 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013193 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013194 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013195 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013196 break;
James Smart4d9ab992009-10-02 15:16:39 -040013197 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013198 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013199 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013200 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013201 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013202 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013203 break;
James Smart4f774512009-05-22 14:52:35 -040013204 default:
13205 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13206 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013207 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013208 break;
13209 }
13210 return workposted;
13211}
13212
13213/**
James Smart4f774512009-05-22 14:52:35 -040013214 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13215 * @phba: Pointer to HBA context object.
13216 * @eqe: Pointer to fast-path event queue entry.
13217 *
13218 * This routine process a event queue entry from the slow-path event queue.
13219 * It will check the MajorCode and MinorCode to determine this is for a
13220 * completion event on a completion queue, if not, an error shall be logged
13221 * and just return. Otherwise, it will get to the corresponding completion
13222 * queue and process all the entries on that completion queue, rearm the
13223 * completion queue, and then return.
13224 *
13225 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013226static void
James Smart67d12732012-08-03 12:36:13 -040013227lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13228 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013229{
James Smart67d12732012-08-03 12:36:13 -040013230 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013231 uint16_t cqid;
13232
James Smart4f774512009-05-22 14:52:35 -040013233 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013234 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013235
James Smart4f774512009-05-22 14:52:35 -040013236 list_for_each_entry(childq, &speq->child_list, list) {
13237 if (childq->queue_id == cqid) {
13238 cq = childq;
13239 break;
13240 }
13241 }
13242 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013243 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13244 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13245 "0365 Slow-path CQ identifier "
13246 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013247 return;
James Smart4f774512009-05-22 14:52:35 -040013248 }
13249
James Smart895427b2017-02-12 13:52:30 -080013250 /* Save EQ associated with this CQ */
13251 cq->assoc_qp = speq;
13252
Dick Kennedyf485c182017-09-29 17:34:34 -070013253 if (!queue_work(phba->wq, &cq->spwork))
13254 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13255 "0390 Cannot schedule soft IRQ "
13256 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13257 cqid, cq->queue_id, smp_processor_id());
13258}
13259
13260/**
13261 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13262 * @phba: Pointer to HBA context object.
13263 *
13264 * This routine process a event queue entry from the slow-path event queue.
13265 * It will check the MajorCode and MinorCode to determine this is for a
13266 * completion event on a completion queue, if not, an error shall be logged
13267 * and just return. Otherwise, it will get to the corresponding completion
13268 * queue and process all the entries on that completion queue, rearm the
13269 * completion queue, and then return.
13270 *
13271 **/
13272static void
13273lpfc_sli4_sp_process_cq(struct work_struct *work)
13274{
13275 struct lpfc_queue *cq =
13276 container_of(work, struct lpfc_queue, spwork);
13277 struct lpfc_hba *phba = cq->phba;
13278 struct lpfc_cqe *cqe;
13279 bool workposted = false;
13280 int ccount = 0;
13281
James Smart4f774512009-05-22 14:52:35 -040013282 /* Process all the entries to the CQ */
13283 switch (cq->type) {
13284 case LPFC_MCQ:
13285 while ((cqe = lpfc_sli4_cq_get(cq))) {
13286 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013287 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013288 break;
James Smartb84daac2012-08-03 12:35:13 -040013289 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040013290 }
13291 break;
13292 case LPFC_WCQ:
13293 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013294 if (cq->subtype == LPFC_FCP ||
13295 cq->subtype == LPFC_NVME) {
13296#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13297 if (phba->ktime_on)
13298 cq->isr_timestamp = ktime_get_ns();
13299 else
13300 cq->isr_timestamp = 0;
13301#endif
James Smart895427b2017-02-12 13:52:30 -080013302 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040013303 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013304 } else {
James Smart05580562011-05-24 11:40:48 -040013305 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13306 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013307 }
Dick Kennedyf485c182017-09-29 17:34:34 -070013308 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013309 break;
James Smart4f774512009-05-22 14:52:35 -040013310 }
James Smartb84daac2012-08-03 12:35:13 -040013311
13312 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013313 if (ccount > cq->CQ_max_cqe)
13314 cq->CQ_max_cqe = ccount;
James Smart4f774512009-05-22 14:52:35 -040013315 break;
13316 default:
13317 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13318 "0370 Invalid completion queue type (%d)\n",
13319 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013320 return;
James Smart4f774512009-05-22 14:52:35 -040013321 }
13322
13323 /* Catch the no cq entry condition, log an error */
Dick Kennedyf485c182017-09-29 17:34:34 -070013324 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013325 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13326 "0371 No entry from the CQ: identifier "
13327 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13328
13329 /* In any case, flash and re-arm the RCQ */
James Smartb71413d2018-02-22 08:18:40 -080013330 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013331
13332 /* wake up worker thread if there are works to be done */
13333 if (workposted)
13334 lpfc_worker_wake_up(phba);
13335}
13336
13337/**
13338 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013339 * @phba: Pointer to HBA context object.
13340 * @cq: Pointer to associated CQ
13341 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013342 *
13343 * This routine process a fast-path work queue completion entry from fast-path
13344 * event queue for FCP command response completion.
13345 **/
13346static void
James Smart2a76a282012-08-03 12:35:54 -040013347lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013348 struct lpfc_wcqe_complete *wcqe)
13349{
James Smart2a76a282012-08-03 12:35:54 -040013350 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013351 struct lpfc_iocbq *cmdiocbq;
13352 struct lpfc_iocbq irspiocbq;
13353 unsigned long iflags;
13354
James Smart4f774512009-05-22 14:52:35 -040013355 /* Check for response status */
13356 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13357 /* If resource errors reported from HBA, reduce queue
13358 * depth of the SCSI device.
13359 */
James Smarte3d2b802012-08-14 14:25:43 -040013360 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13361 IOSTAT_LOCAL_REJECT)) &&
13362 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13363 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013364 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013365
James Smart4f774512009-05-22 14:52:35 -040013366 /* Log the error status */
13367 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13368 "0373 FCP complete error: status=x%x, "
13369 "hw_status=x%x, total_data_specified=%d, "
13370 "parameter=x%x, word3=x%x\n",
13371 bf_get(lpfc_wcqe_c_status, wcqe),
13372 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13373 wcqe->total_data_placed, wcqe->parameter,
13374 wcqe->word3);
13375 }
13376
13377 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013378 spin_lock_irqsave(&pring->ring_lock, iflags);
13379 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013380 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13381 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013382 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013383 if (unlikely(!cmdiocbq)) {
13384 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13385 "0374 FCP complete with no corresponding "
13386 "cmdiocb: iotag (%d)\n",
13387 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13388 return;
13389 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013390#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13391 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13392#endif
James Smart895427b2017-02-12 13:52:30 -080013393 if (cmdiocbq->iocb_cmpl == NULL) {
13394 if (cmdiocbq->wqe_cmpl) {
13395 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13396 spin_lock_irqsave(&phba->hbalock, iflags);
13397 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13398 spin_unlock_irqrestore(&phba->hbalock, iflags);
13399 }
13400
13401 /* Pass the cmd_iocb and the wcqe to the upper layer */
13402 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13403 return;
13404 }
James Smart4f774512009-05-22 14:52:35 -040013405 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13406 "0375 FCP cmdiocb not callback function "
13407 "iotag: (%d)\n",
13408 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13409 return;
13410 }
13411
13412 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013413 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013414
James Smart0f65ff62010-02-26 14:14:23 -050013415 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13416 spin_lock_irqsave(&phba->hbalock, iflags);
13417 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13418 spin_unlock_irqrestore(&phba->hbalock, iflags);
13419 }
13420
James Smart4f774512009-05-22 14:52:35 -040013421 /* Pass the cmd_iocb and the rsp state to the upper layer */
13422 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13423}
13424
13425/**
13426 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13427 * @phba: Pointer to HBA context object.
13428 * @cq: Pointer to completion queue.
13429 * @wcqe: Pointer to work-queue completion queue entry.
13430 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013431 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013432 * proper WQ release routine to the slow-path WQ.
13433 **/
13434static void
13435lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13436 struct lpfc_wcqe_release *wcqe)
13437{
13438 struct lpfc_queue *childwq;
13439 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013440 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013441
13442 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013443 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013444 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013445 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013446 lpfc_sli4_wq_release(childwq,
13447 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080013448 if (childwq->q_flag & HBA_NVMET_WQFULL)
13449 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040013450 wqid_matched = true;
13451 break;
13452 }
13453 }
13454 /* Report warning log message if no match found */
13455 if (wqid_matched != true)
13456 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13457 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013458 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013459}
13460
13461/**
James Smart2d7dbc42017-02-12 13:52:35 -080013462 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13463 * @phba: Pointer to HBA context object.
13464 * @rcqe: Pointer to receive-queue completion queue entry.
13465 *
13466 * This routine process a receive-queue completion queue entry.
13467 *
13468 * Return: true if work posted to worker thread, otherwise false.
13469 **/
13470static bool
13471lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13472 struct lpfc_rcqe *rcqe)
13473{
13474 bool workposted = false;
13475 struct lpfc_queue *hrq;
13476 struct lpfc_queue *drq;
13477 struct rqb_dmabuf *dma_buf;
13478 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013479 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013480 uint32_t status, rq_id;
13481 unsigned long iflags;
13482 uint32_t fctl, idx;
13483
13484 if ((phba->nvmet_support == 0) ||
13485 (phba->sli4_hba.nvmet_cqset == NULL))
13486 return workposted;
13487
13488 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13489 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13490 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13491
13492 /* sanity check on queue memory */
13493 if (unlikely(!hrq) || unlikely(!drq))
13494 return workposted;
13495
13496 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13497 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13498 else
13499 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13500
13501 if ((phba->nvmet_support == 0) ||
13502 (rq_id != hrq->queue_id))
13503 return workposted;
13504
13505 status = bf_get(lpfc_rcqe_status, rcqe);
13506 switch (status) {
13507 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13508 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13509 "6126 Receive Frame Truncated!!\n");
James Smart78e1d202017-06-01 21:07:09 -070013510 /* Drop thru */
James Smart2d7dbc42017-02-12 13:52:35 -080013511 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080013512 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013513 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080013514 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13515 if (!dma_buf) {
13516 hrq->RQ_no_buf_found++;
13517 spin_unlock_irqrestore(&phba->hbalock, iflags);
13518 goto out;
13519 }
13520 spin_unlock_irqrestore(&phba->hbalock, iflags);
13521 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013522 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013523 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13524
13525 /* Just some basic sanity checks on FCP Command frame */
13526 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13527 fc_hdr->fh_f_ctl[1] << 8 |
13528 fc_hdr->fh_f_ctl[2]);
13529 if (((fctl &
13530 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13531 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13532 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13533 goto drop;
13534
13535 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13536 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013537 lpfc_nvmet_unsol_fcp_event(
Dick Kennedy66d7ce92017-08-23 16:55:42 -070013538 phba, idx, dma_buf,
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013539 cq->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013540 return false;
13541 }
13542drop:
13543 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13544 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013545 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013546 if (phba->nvmet_support) {
13547 tgtp = phba->targetport->private;
13548 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13549 "6401 RQE Error x%x, posted %d err_cnt "
13550 "%d: %x %x %x\n",
13551 status, hrq->RQ_buf_posted,
13552 hrq->RQ_no_posted_buf,
13553 atomic_read(&tgtp->rcv_fcp_cmd_in),
13554 atomic_read(&tgtp->rcv_fcp_cmd_out),
13555 atomic_read(&tgtp->xmt_fcp_release));
13556 }
13557 /* fallthrough */
13558
13559 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013560 hrq->RQ_no_posted_buf++;
13561 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013562 break;
13563 }
13564out:
13565 return workposted;
13566}
13567
13568/**
James Smart895427b2017-02-12 13:52:30 -080013569 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013570 * @cq: Pointer to the completion queue.
13571 * @eqe: Pointer to fast-path completion queue entry.
13572 *
13573 * This routine process a fast-path work queue completion entry from fast-path
13574 * event queue for FCP command response completion.
13575 **/
13576static int
James Smart895427b2017-02-12 13:52:30 -080013577lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013578 struct lpfc_cqe *cqe)
13579{
13580 struct lpfc_wcqe_release wcqe;
13581 bool workposted = false;
13582
13583 /* Copy the work queue CQE and convert endian order if needed */
13584 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13585
13586 /* Check and process for different type of WCQE and dispatch */
13587 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13588 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013589 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013590 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013591 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013592 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013593 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13594 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13595 (struct lpfc_wcqe_complete *)&wcqe);
13596 if (cq->subtype == LPFC_NVME_LS)
13597 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013598 (struct lpfc_wcqe_complete *)&wcqe);
13599 break;
13600 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013601 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013602 /* Process the WQ release event */
13603 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13604 (struct lpfc_wcqe_release *)&wcqe);
13605 break;
13606 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013607 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013608 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013609 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013610 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13611 (struct sli4_wcqe_xri_aborted *)&wcqe);
13612 break;
James Smart895427b2017-02-12 13:52:30 -080013613 case CQE_CODE_RECEIVE_V1:
13614 case CQE_CODE_RECEIVE:
13615 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013616 if (cq->subtype == LPFC_NVMET) {
13617 workposted = lpfc_sli4_nvmet_handle_rcqe(
13618 phba, cq, (struct lpfc_rcqe *)&wcqe);
13619 }
James Smart895427b2017-02-12 13:52:30 -080013620 break;
James Smart4f774512009-05-22 14:52:35 -040013621 default:
13622 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013623 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013624 bf_get(lpfc_wcqe_c_code, &wcqe));
13625 break;
13626 }
13627 return workposted;
13628}
13629
13630/**
James Smart67d12732012-08-03 12:36:13 -040013631 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040013632 * @phba: Pointer to HBA context object.
13633 * @eqe: Pointer to fast-path event queue entry.
13634 *
13635 * This routine process a event queue entry from the fast-path event queue.
13636 * It will check the MajorCode and MinorCode to determine this is for a
13637 * completion event on a completion queue, if not, an error shall be logged
13638 * and just return. Otherwise, it will get to the corresponding completion
13639 * queue and process all the entries on the completion queue, rearm the
13640 * completion queue, and then return.
13641 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013642static void
James Smart67d12732012-08-03 12:36:13 -040013643lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13644 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040013645{
James Smart895427b2017-02-12 13:52:30 -080013646 struct lpfc_queue *cq = NULL;
James Smart2d7dbc42017-02-12 13:52:35 -080013647 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040013648
James Smartcb5172e2010-03-15 11:25:07 -040013649 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040013650 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040013651 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040013652 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040013653 bf_get_le32(lpfc_eqe_major_code, eqe),
13654 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070013655 return;
James Smart4f774512009-05-22 14:52:35 -040013656 }
13657
James Smart67d12732012-08-03 12:36:13 -040013658 /* Get the reference to the corresponding CQ */
13659 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13660
James Smart2d7dbc42017-02-12 13:52:35 -080013661 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13662 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13663 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13664 /* Process NVMET unsol rcv */
13665 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13666 goto process_cq;
13667 }
13668 }
13669
James Smart895427b2017-02-12 13:52:30 -080013670 if (phba->sli4_hba.nvme_cq_map &&
13671 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
James Smartf358dd02017-02-12 13:52:34 -080013672 /* Process NVME / NVMET command completion */
James Smart895427b2017-02-12 13:52:30 -080013673 cq = phba->sli4_hba.nvme_cq[qidx];
13674 goto process_cq;
13675 }
13676
13677 if (phba->sli4_hba.fcp_cq_map &&
13678 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13679 /* Process FCP command completion */
13680 cq = phba->sli4_hba.fcp_cq[qidx];
13681 goto process_cq;
13682 }
13683
13684 if (phba->sli4_hba.nvmels_cq &&
13685 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13686 /* Process NVME unsol rcv */
13687 cq = phba->sli4_hba.nvmels_cq;
13688 }
13689
13690 /* Otherwise this is a Slow path event */
13691 if (cq == NULL) {
Dick Kennedyf485c182017-09-29 17:34:34 -070013692 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13693 return;
James Smart67d12732012-08-03 12:36:13 -040013694 }
13695
James Smart895427b2017-02-12 13:52:30 -080013696process_cq:
James Smart4f774512009-05-22 14:52:35 -040013697 if (unlikely(cqid != cq->queue_id)) {
13698 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13699 "0368 Miss-matched fast-path completion "
13700 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13701 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070013702 return;
James Smart4f774512009-05-22 14:52:35 -040013703 }
13704
James Smart895427b2017-02-12 13:52:30 -080013705 /* Save EQ associated with this CQ */
13706 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13707
Dick Kennedyf485c182017-09-29 17:34:34 -070013708 if (!queue_work(phba->wq, &cq->irqwork))
13709 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13710 "0363 Cannot schedule soft IRQ "
13711 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13712 cqid, cq->queue_id, smp_processor_id());
13713}
13714
13715/**
13716 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13717 * @phba: Pointer to HBA context object.
13718 * @eqe: Pointer to fast-path event queue entry.
13719 *
13720 * This routine process a event queue entry from the fast-path event queue.
13721 * It will check the MajorCode and MinorCode to determine this is for a
13722 * completion event on a completion queue, if not, an error shall be logged
13723 * and just return. Otherwise, it will get to the corresponding completion
13724 * queue and process all the entries on the completion queue, rearm the
13725 * completion queue, and then return.
13726 **/
13727static void
13728lpfc_sli4_hba_process_cq(struct work_struct *work)
13729{
13730 struct lpfc_queue *cq =
13731 container_of(work, struct lpfc_queue, irqwork);
13732 struct lpfc_hba *phba = cq->phba;
13733 struct lpfc_cqe *cqe;
13734 bool workposted = false;
13735 int ccount = 0;
13736
James Smart4f774512009-05-22 14:52:35 -040013737 /* Process all the entries to the CQ */
13738 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013739#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13740 if (phba->ktime_on)
13741 cq->isr_timestamp = ktime_get_ns();
13742 else
13743 cq->isr_timestamp = 0;
13744#endif
James Smart895427b2017-02-12 13:52:30 -080013745 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013746 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013747 break;
James Smart4f774512009-05-22 14:52:35 -040013748 }
13749
James Smartb84daac2012-08-03 12:35:13 -040013750 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013751 if (ccount > cq->CQ_max_cqe)
13752 cq->CQ_max_cqe = ccount;
13753 cq->assoc_qp->EQ_cqe_cnt += ccount;
James Smartb84daac2012-08-03 12:35:13 -040013754
James Smart4f774512009-05-22 14:52:35 -040013755 /* Catch the no cq entry condition */
Dick Kennedyf485c182017-09-29 17:34:34 -070013756 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013757 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13758 "0369 No entry from fast-path completion "
13759 "queue fcpcqid=%d\n", cq->queue_id);
13760
13761 /* In any case, flash and re-arm the CQ */
James Smartb71413d2018-02-22 08:18:40 -080013762 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013763
13764 /* wake up worker thread if there are works to be done */
13765 if (workposted)
13766 lpfc_worker_wake_up(phba);
13767}
13768
13769static void
13770lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13771{
13772 struct lpfc_eqe *eqe;
13773
13774 /* walk all the EQ entries and drop on the floor */
13775 while ((eqe = lpfc_sli4_eq_get(eq)))
13776 ;
13777
13778 /* Clear and re-arm the EQ */
James Smartb71413d2018-02-22 08:18:40 -080013779 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013780}
13781
James Smart1ba981f2014-02-20 09:56:45 -050013782
13783/**
13784 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13785 * entry
13786 * @phba: Pointer to HBA context object.
13787 * @eqe: Pointer to fast-path event queue entry.
13788 *
13789 * This routine process a event queue entry from the Flash Optimized Fabric
13790 * event queue. It will check the MajorCode and MinorCode to determine this
13791 * is for a completion event on a completion queue, if not, an error shall be
13792 * logged and just return. Otherwise, it will get to the corresponding
13793 * completion queue and process all the entries on the completion queue, rearm
13794 * the completion queue, and then return.
13795 **/
13796static void
13797lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13798{
13799 struct lpfc_queue *cq;
James Smart1ba981f2014-02-20 09:56:45 -050013800 uint16_t cqid;
James Smart1ba981f2014-02-20 09:56:45 -050013801
13802 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13803 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13804 "9147 Not a valid completion "
13805 "event: majorcode=x%x, minorcode=x%x\n",
13806 bf_get_le32(lpfc_eqe_major_code, eqe),
13807 bf_get_le32(lpfc_eqe_minor_code, eqe));
13808 return;
13809 }
13810
13811 /* Get the reference to the corresponding CQ */
13812 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13813
13814 /* Next check for OAS */
13815 cq = phba->sli4_hba.oas_cq;
13816 if (unlikely(!cq)) {
13817 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13818 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13819 "9148 OAS completion queue "
13820 "does not exist\n");
13821 return;
13822 }
13823
13824 if (unlikely(cqid != cq->queue_id)) {
13825 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13826 "9149 Miss-matched fast-path compl "
13827 "queue id: eqcqid=%d, fcpcqid=%d\n",
13828 cqid, cq->queue_id);
13829 return;
13830 }
13831
James Smartd41b65b2017-06-15 22:56:41 -070013832 /* Save EQ associated with this CQ */
13833 cq->assoc_qp = phba->sli4_hba.fof_eq;
13834
Dick Kennedyf485c182017-09-29 17:34:34 -070013835 /* CQ work will be processed on CPU affinitized to this IRQ */
13836 if (!queue_work(phba->wq, &cq->irqwork))
James Smart1ba981f2014-02-20 09:56:45 -050013837 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
Dick Kennedyf485c182017-09-29 17:34:34 -070013838 "0367 Cannot schedule soft IRQ "
13839 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13840 cqid, cq->queue_id, smp_processor_id());
James Smart1ba981f2014-02-20 09:56:45 -050013841}
13842
13843/**
13844 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13845 * @irq: Interrupt number.
13846 * @dev_id: The device context pointer.
13847 *
13848 * This function is directly called from the PCI layer as an interrupt
13849 * service routine when device with SLI-4 interface spec is enabled with
13850 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13851 * IOCB ring event in the HBA. However, when the device is enabled with either
13852 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13853 * device-level interrupt handler. When the PCI slot is in error recovery
13854 * or the HBA is undergoing initialization, the interrupt handler will not
13855 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13856 * the intrrupt context. This function is called without any lock held.
13857 * It gets the hbalock to access and update SLI data structures. Note that,
13858 * the EQ to CQ are one-to-one map such that the EQ index is
13859 * equal to that of CQ index.
13860 *
13861 * This function returns IRQ_HANDLED when interrupt is handled else it
13862 * returns IRQ_NONE.
13863 **/
13864irqreturn_t
13865lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13866{
13867 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013868 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050013869 struct lpfc_queue *eq;
13870 struct lpfc_eqe *eqe;
13871 unsigned long iflag;
13872 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013873
13874 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013875 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13876 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050013877
13878 if (unlikely(!phba))
13879 return IRQ_NONE;
13880
13881 /* Get to the EQ struct associated with this vector */
13882 eq = phba->sli4_hba.fof_eq;
13883 if (unlikely(!eq))
13884 return IRQ_NONE;
13885
13886 /* Check device state for handling interrupt */
13887 if (unlikely(lpfc_intr_state_check(phba))) {
James Smart1ba981f2014-02-20 09:56:45 -050013888 /* Check again for link_state with lock held */
13889 spin_lock_irqsave(&phba->hbalock, iflag);
13890 if (phba->link_state < LPFC_LINK_DOWN)
13891 /* Flush, clear interrupt, and rearm the EQ */
13892 lpfc_sli4_eq_flush(phba, eq);
13893 spin_unlock_irqrestore(&phba->hbalock, iflag);
13894 return IRQ_NONE;
13895 }
13896
13897 /*
13898 * Process all the event on FCP fast-path EQ
13899 */
13900 while ((eqe = lpfc_sli4_eq_get(eq))) {
13901 lpfc_sli4_fof_handle_eqe(phba, eqe);
13902 if (!(++ecount % eq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013903 break;
James Smart1ba981f2014-02-20 09:56:45 -050013904 eq->EQ_processed++;
13905 }
13906
13907 /* Track the max number of EQEs processed in 1 intr */
13908 if (ecount > eq->EQ_max_eqe)
13909 eq->EQ_max_eqe = ecount;
13910
13911
13912 if (unlikely(ecount == 0)) {
13913 eq->EQ_no_entry++;
13914
13915 if (phba->intr_type == MSIX)
13916 /* MSI-X treated interrupt served as no EQ share INT */
13917 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13918 "9145 MSI-X interrupt with no EQE\n");
13919 else {
13920 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13921 "9146 ISR interrupt with no EQE\n");
13922 /* Non MSI-X treated on interrupt as EQ share INT */
13923 return IRQ_NONE;
13924 }
13925 }
13926 /* Always clear and re-arm the fast-path EQ */
James Smartb71413d2018-02-22 08:18:40 -080013927 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -050013928 return IRQ_HANDLED;
13929}
13930
James Smart4f774512009-05-22 14:52:35 -040013931/**
James Smart67d12732012-08-03 12:36:13 -040013932 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040013933 * @irq: Interrupt number.
13934 * @dev_id: The device context pointer.
13935 *
13936 * This function is directly called from the PCI layer as an interrupt
13937 * service routine when device with SLI-4 interface spec is enabled with
13938 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13939 * ring event in the HBA. However, when the device is enabled with either
13940 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13941 * device-level interrupt handler. When the PCI slot is in error recovery
13942 * or the HBA is undergoing initialization, the interrupt handler will not
13943 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13944 * the intrrupt context. This function is called without any lock held.
13945 * It gets the hbalock to access and update SLI data structures. Note that,
13946 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13947 * equal to that of FCP CQ index.
13948 *
James Smart67d12732012-08-03 12:36:13 -040013949 * The link attention and ELS ring attention events are handled
13950 * by the worker thread. The interrupt handler signals the worker thread
13951 * and returns for these events. This function is called without any lock
13952 * held. It gets the hbalock to access and update SLI data structures.
13953 *
James Smart4f774512009-05-22 14:52:35 -040013954 * This function returns IRQ_HANDLED when interrupt is handled else it
13955 * returns IRQ_NONE.
13956 **/
13957irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040013958lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040013959{
13960 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013961 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040013962 struct lpfc_queue *fpeq;
13963 struct lpfc_eqe *eqe;
13964 unsigned long iflag;
13965 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080013966 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040013967
13968 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013969 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13970 phba = hba_eq_hdl->phba;
13971 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040013972
13973 if (unlikely(!phba))
13974 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040013975 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040013976 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013977
13978 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080013979 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050013980 if (unlikely(!fpeq))
13981 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013982
James Smartba20c852012-08-03 12:36:52 -040013983 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013984 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartb71413d2018-02-22 08:18:40 -080013985 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -040013986 else {
James Smart895427b2017-02-12 13:52:30 -080013987 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013988 return IRQ_NONE;
13989 }
13990 }
13991
James Smart4f774512009-05-22 14:52:35 -040013992 /* Check device state for handling interrupt */
13993 if (unlikely(lpfc_intr_state_check(phba))) {
13994 /* Check again for link_state with lock held */
13995 spin_lock_irqsave(&phba->hbalock, iflag);
13996 if (phba->link_state < LPFC_LINK_DOWN)
13997 /* Flush, clear interrupt, and rearm the EQ */
13998 lpfc_sli4_eq_flush(phba, fpeq);
13999 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040014000 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080014001 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040014002 return IRQ_NONE;
14003 }
14004
14005 /*
14006 * Process all the event on FCP fast-path EQ
14007 */
14008 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
Dick Kennedyf485c182017-09-29 17:34:34 -070014009 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
14010 if (!(++ecount % fpeq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070014011 break;
James Smartb84daac2012-08-03 12:35:13 -040014012 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040014013 }
14014
James Smartb84daac2012-08-03 12:35:13 -040014015 /* Track the max number of EQEs processed in 1 intr */
14016 if (ecount > fpeq->EQ_max_eqe)
14017 fpeq->EQ_max_eqe = ecount;
14018
James Smart4f774512009-05-22 14:52:35 -040014019 /* Always clear and re-arm the fast-path EQ */
James Smartb71413d2018-02-22 08:18:40 -080014020 phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014021
14022 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040014023 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040014024
14025 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080014026 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040014027 return IRQ_NONE;
14028 }
14029
James Smart4f774512009-05-22 14:52:35 -040014030 if (phba->intr_type == MSIX)
14031 /* MSI-X treated interrupt served as no EQ share INT */
14032 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14033 "0358 MSI-X interrupt with no EQE\n");
14034 else
14035 /* Non MSI-X treated on interrupt as EQ share INT */
14036 return IRQ_NONE;
14037 }
14038
James Smartba20c852012-08-03 12:36:52 -040014039 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080014040 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14041
James Smart4f774512009-05-22 14:52:35 -040014042 return IRQ_HANDLED;
14043} /* lpfc_sli4_fp_intr_handler */
14044
14045/**
14046 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14047 * @irq: Interrupt number.
14048 * @dev_id: The device context pointer.
14049 *
14050 * This function is the device-level interrupt handler to device with SLI-4
14051 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14052 * interrupt mode is enabled and there is an event in the HBA which requires
14053 * driver attention. This function invokes the slow-path interrupt attention
14054 * handling function and fast-path interrupt attention handling function in
14055 * turn to process the relevant HBA attention events. This function is called
14056 * without any lock held. It gets the hbalock to access and update SLI data
14057 * structures.
14058 *
14059 * This function returns IRQ_HANDLED when interrupt is handled, else it
14060 * returns IRQ_NONE.
14061 **/
14062irqreturn_t
14063lpfc_sli4_intr_handler(int irq, void *dev_id)
14064{
14065 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040014066 irqreturn_t hba_irq_rc;
14067 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080014068 int qidx;
James Smart4f774512009-05-22 14:52:35 -040014069
14070 /* Get the driver's phba structure from the dev_id */
14071 phba = (struct lpfc_hba *)dev_id;
14072
14073 if (unlikely(!phba))
14074 return IRQ_NONE;
14075
14076 /*
James Smart4f774512009-05-22 14:52:35 -040014077 * Invoke fast-path host attention interrupt handling as appropriate.
14078 */
James Smart895427b2017-02-12 13:52:30 -080014079 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040014080 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014081 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040014082 if (hba_irq_rc == IRQ_HANDLED)
14083 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040014084 }
14085
James Smart1ba981f2014-02-20 09:56:45 -050014086 if (phba->cfg_fof) {
14087 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014088 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050014089 if (hba_irq_rc == IRQ_HANDLED)
14090 hba_handled |= true;
14091 }
14092
James Smart67d12732012-08-03 12:36:13 -040014093 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014094} /* lpfc_sli4_intr_handler */
14095
14096/**
14097 * lpfc_sli4_queue_free - free a queue structure and associated memory
14098 * @queue: The queue structure to free.
14099 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040014100 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040014101 * the host resident queue. This function must be called after destroying the
14102 * queue on the HBA.
14103 **/
14104void
14105lpfc_sli4_queue_free(struct lpfc_queue *queue)
14106{
14107 struct lpfc_dmabuf *dmabuf;
14108
14109 if (!queue)
14110 return;
14111
14112 while (!list_empty(&queue->page_list)) {
14113 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14114 list);
James Smart81b96ed2017-11-20 16:00:29 -080014115 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040014116 dmabuf->virt, dmabuf->phys);
14117 kfree(dmabuf);
14118 }
James Smart895427b2017-02-12 13:52:30 -080014119 if (queue->rqbp) {
14120 lpfc_free_rq_buffer(queue->phba, queue);
14121 kfree(queue->rqbp);
14122 }
James Smartd1f525a2017-04-21 16:04:55 -070014123
14124 if (!list_empty(&queue->wq_list))
14125 list_del(&queue->wq_list);
14126
James Smart4f774512009-05-22 14:52:35 -040014127 kfree(queue);
14128 return;
14129}
14130
14131/**
14132 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14133 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080014134 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040014135 * @entry_size: The size of each queue entry for this queue.
14136 * @entry count: The number of entries that this queue will handle.
14137 *
14138 * This function allocates a queue structure and the DMAable memory used for
14139 * the host resident queue. This function must be called before creating the
14140 * queue on the HBA.
14141 **/
14142struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080014143lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14144 uint32_t entry_size, uint32_t entry_count)
James Smart4f774512009-05-22 14:52:35 -040014145{
14146 struct lpfc_queue *queue;
14147 struct lpfc_dmabuf *dmabuf;
14148 int x, total_qe_count;
14149 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040014150 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040014151
James Smartcb5172e2010-03-15 11:25:07 -040014152 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014153 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040014154
James Smart4f774512009-05-22 14:52:35 -040014155 queue = kzalloc(sizeof(struct lpfc_queue) +
14156 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14157 if (!queue)
14158 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040014159 queue->page_count = (ALIGN(entry_size * entry_count,
14160 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080014161
14162 /* If needed, Adjust page count to match the max the adapter supports */
14163 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
14164 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14165
James Smart4f774512009-05-22 14:52:35 -040014166 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080014167 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080014168 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040014169 INIT_LIST_HEAD(&queue->page_list);
14170 INIT_LIST_HEAD(&queue->child_list);
James Smart81b96ed2017-11-20 16:00:29 -080014171
14172 /* Set queue parameters now. If the system cannot provide memory
14173 * resources, the free routine needs to know what was allocated.
14174 */
14175 queue->entry_size = entry_size;
14176 queue->entry_count = entry_count;
14177 queue->page_size = hw_page_size;
14178 queue->phba = phba;
14179
James Smart4f774512009-05-22 14:52:35 -040014180 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14181 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14182 if (!dmabuf)
14183 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040014184 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14185 hw_page_size, &dmabuf->phys,
14186 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040014187 if (!dmabuf->virt) {
14188 kfree(dmabuf);
14189 goto out_fail;
14190 }
14191 dmabuf->buffer_tag = x;
14192 list_add_tail(&dmabuf->list, &queue->page_list);
14193 /* initialize queue's entry array */
14194 dma_pointer = dmabuf->virt;
14195 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040014196 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040014197 total_qe_count++, dma_pointer += entry_size) {
14198 queue->qe[total_qe_count].address = dma_pointer;
14199 }
14200 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014201 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14202 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014203
James Smart64eb4dc2017-05-15 15:20:49 -070014204 /* entry_repost will be set during q creation */
14205
James Smart4f774512009-05-22 14:52:35 -040014206 return queue;
14207out_fail:
14208 lpfc_sli4_queue_free(queue);
14209 return NULL;
14210}
14211
14212/**
James Smart962bc512013-01-03 15:44:00 -050014213 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14214 * @phba: HBA structure that indicates port to create a queue on.
14215 * @pci_barset: PCI BAR set flag.
14216 *
14217 * This function shall perform iomap of the specified PCI BAR address to host
14218 * memory address if not already done so and return it. The returned host
14219 * memory address can be NULL.
14220 */
14221static void __iomem *
14222lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14223{
James Smart962bc512013-01-03 15:44:00 -050014224 if (!phba->pcidev)
14225 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014226
14227 switch (pci_barset) {
14228 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014229 return phba->pci_bar0_memmap_p;
14230 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014231 return phba->pci_bar2_memmap_p;
14232 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014233 return phba->pci_bar4_memmap_p;
14234 default:
14235 break;
14236 }
14237 return NULL;
14238}
14239
14240/**
James Smart895427b2017-02-12 13:52:30 -080014241 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040014242 * @phba: HBA structure that indicates port to create a queue on.
14243 * @startq: The starting FCP EQ to modify
14244 *
14245 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
James Smart43140ca2017-03-04 09:30:34 -080014246 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14247 * updated in one mailbox command.
James Smart173edbb2012-06-12 13:54:50 -040014248 *
14249 * The @phba struct is used to send mailbox command to HBA. The @startq
14250 * is used to get the starting FCP EQ to change.
14251 * This function is asynchronous and will wait for the mailbox
14252 * command to finish before continuing.
14253 *
14254 * On success this function will return a zero. If unable to allocate enough
14255 * memory this function will return -ENOMEM. If the queue create mailbox command
14256 * fails this function will return -ENXIO.
14257 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014258int
James Smart0cf07f842017-06-01 21:07:10 -070014259lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14260 uint32_t numq, uint32_t imax)
James Smart173edbb2012-06-12 13:54:50 -040014261{
14262 struct lpfc_mbx_modify_eq_delay *eq_delay;
14263 LPFC_MBOXQ_t *mbox;
14264 struct lpfc_queue *eq;
14265 int cnt, rc, length, status = 0;
14266 uint32_t shdr_status, shdr_add_status;
James Smart0cf07f842017-06-01 21:07:10 -070014267 uint32_t result, val;
James Smart895427b2017-02-12 13:52:30 -080014268 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014269 union lpfc_sli4_cfg_shdr *shdr;
14270 uint16_t dmult;
14271
James Smart895427b2017-02-12 13:52:30 -080014272 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040014273 return 0;
14274
14275 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14276 if (!mbox)
14277 return -ENOMEM;
14278 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14279 sizeof(struct lpfc_sli4_cfg_mhdr));
14280 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14281 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14282 length, LPFC_SLI4_MBX_EMBED);
14283 eq_delay = &mbox->u.mqe.un.eq_delay;
14284
14285 /* Calculate delay multiper from maximum interrupt per second */
James Smart0cf07f842017-06-01 21:07:10 -070014286 result = imax / phba->io_channel_irqs;
James Smart895427b2017-02-12 13:52:30 -080014287 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040014288 dmult = 0;
14289 else
14290 dmult = LPFC_DMULT_CONST/result - 1;
James Smart0cf07f842017-06-01 21:07:10 -070014291 if (dmult > LPFC_DMULT_MAX)
14292 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014293
14294 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080014295 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14296 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040014297 if (!eq)
14298 continue;
James Smart0cf07f842017-06-01 21:07:10 -070014299 eq->q_mode = imax;
James Smart173edbb2012-06-12 13:54:50 -040014300 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14301 eq_delay->u.request.eq[cnt].phase = 0;
14302 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14303 cnt++;
James Smart0cf07f842017-06-01 21:07:10 -070014304
14305 /* q_mode is only used for auto_imax */
14306 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14307 /* Use EQ Delay Register method for q_mode */
14308
14309 /* Convert for EQ Delay register */
14310 val = phba->cfg_fcp_imax;
14311 if (val) {
14312 /* First, interrupts per sec per EQ */
14313 val = phba->cfg_fcp_imax /
14314 phba->io_channel_irqs;
14315
14316 /* us delay between each interrupt */
14317 val = LPFC_SEC_TO_USEC / val;
14318 }
14319 eq->q_mode = val;
14320 } else {
14321 eq->q_mode = imax;
14322 }
14323
14324 if (cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014325 break;
14326 }
14327 eq_delay->u.request.num_eq = cnt;
14328
14329 mbox->vport = phba->pport;
14330 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14331 mbox->context1 = NULL;
14332 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14333 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14334 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14335 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14336 if (shdr_status || shdr_add_status || rc) {
14337 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14338 "2512 MODIFY_EQ_DELAY mailbox failed with "
14339 "status x%x add_status x%x, mbx status x%x\n",
14340 shdr_status, shdr_add_status, rc);
14341 status = -ENXIO;
14342 }
14343 mempool_free(mbox, phba->mbox_mem_pool);
14344 return status;
14345}
14346
14347/**
James Smart4f774512009-05-22 14:52:35 -040014348 * lpfc_eq_create - Create an Event Queue on the HBA
14349 * @phba: HBA structure that indicates port to create a queue on.
14350 * @eq: The queue structure to use to create the event queue.
14351 * @imax: The maximum interrupt per second limit.
14352 *
14353 * This function creates an event queue, as detailed in @eq, on a port,
14354 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14355 *
14356 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14357 * is used to get the entry count and entry size that are necessary to
14358 * determine the number of pages to allocate and use for this queue. This
14359 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14360 * event queue. This function is asynchronous and will wait for the mailbox
14361 * command to finish before continuing.
14362 *
14363 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014364 * memory this function will return -ENOMEM. If the queue create mailbox command
14365 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014366 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014367int
James Smartee020062012-09-29 11:28:52 -040014368lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014369{
14370 struct lpfc_mbx_eq_create *eq_create;
14371 LPFC_MBOXQ_t *mbox;
14372 int rc, length, status = 0;
14373 struct lpfc_dmabuf *dmabuf;
14374 uint32_t shdr_status, shdr_add_status;
14375 union lpfc_sli4_cfg_shdr *shdr;
14376 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014377 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14378
James Smart2e90f4b2011-12-13 13:22:37 -050014379 /* sanity check on queue memory */
14380 if (!eq)
14381 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014382 if (!phba->sli4_hba.pc_sli4_params.supported)
14383 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014384
14385 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14386 if (!mbox)
14387 return -ENOMEM;
14388 length = (sizeof(struct lpfc_mbx_eq_create) -
14389 sizeof(struct lpfc_sli4_cfg_mhdr));
14390 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14391 LPFC_MBOX_OPCODE_EQ_CREATE,
14392 length, LPFC_SLI4_MBX_EMBED);
14393 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080014394 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014395 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14396 eq->page_count);
14397 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14398 LPFC_EQE_SIZE);
14399 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080014400
14401 /* Use version 2 of CREATE_EQ if eqav is set */
14402 if (phba->sli4_hba.pc_sli4_params.eqav) {
14403 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14404 LPFC_Q_CREATE_VERSION_2);
14405 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14406 phba->sli4_hba.pc_sli4_params.eqav);
14407 }
14408
James Smart2c9c5a02015-04-07 15:07:15 -040014409 /* don't setup delay multiplier using EQ_CREATE */
14410 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014411 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14412 dmult);
14413 switch (eq->entry_count) {
14414 default:
14415 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14416 "0360 Unsupported EQ count. (%d)\n",
14417 eq->entry_count);
14418 if (eq->entry_count < 256)
14419 return -EINVAL;
14420 /* otherwise default to smallest count (drop through) */
14421 case 256:
14422 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14423 LPFC_EQ_CNT_256);
14424 break;
14425 case 512:
14426 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14427 LPFC_EQ_CNT_512);
14428 break;
14429 case 1024:
14430 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14431 LPFC_EQ_CNT_1024);
14432 break;
14433 case 2048:
14434 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14435 LPFC_EQ_CNT_2048);
14436 break;
14437 case 4096:
14438 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14439 LPFC_EQ_CNT_4096);
14440 break;
14441 }
14442 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014443 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014444 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14445 putPaddrLow(dmabuf->phys);
14446 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14447 putPaddrHigh(dmabuf->phys);
14448 }
14449 mbox->vport = phba->pport;
14450 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14451 mbox->context1 = NULL;
14452 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040014453 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14454 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14455 if (shdr_status || shdr_add_status || rc) {
14456 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14457 "2500 EQ_CREATE mailbox failed with "
14458 "status x%x add_status x%x, mbx status x%x\n",
14459 shdr_status, shdr_add_status, rc);
14460 status = -ENXIO;
14461 }
14462 eq->type = LPFC_EQ;
14463 eq->subtype = LPFC_NONE;
14464 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14465 if (eq->queue_id == 0xFFFF)
14466 status = -ENXIO;
14467 eq->host_index = 0;
14468 eq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014469 eq->entry_repost = LPFC_EQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014470
James Smart8fa38512009-07-19 10:01:03 -040014471 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014472 return status;
14473}
14474
14475/**
14476 * lpfc_cq_create - Create a Completion Queue on the HBA
14477 * @phba: HBA structure that indicates port to create a queue on.
14478 * @cq: The queue structure to use to create the completion queue.
14479 * @eq: The event queue to bind this completion queue to.
14480 *
14481 * This function creates a completion queue, as detailed in @wq, on a port,
14482 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14483 *
14484 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14485 * is used to get the entry count and entry size that are necessary to
14486 * determine the number of pages to allocate and use for this queue. The @eq
14487 * is used to indicate which event queue to bind this completion queue to. This
14488 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14489 * completion queue. This function is asynchronous and will wait for the mailbox
14490 * command to finish before continuing.
14491 *
14492 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014493 * memory this function will return -ENOMEM. If the queue create mailbox command
14494 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014495 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014496int
James Smart4f774512009-05-22 14:52:35 -040014497lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14498 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14499{
14500 struct lpfc_mbx_cq_create *cq_create;
14501 struct lpfc_dmabuf *dmabuf;
14502 LPFC_MBOXQ_t *mbox;
14503 int rc, length, status = 0;
14504 uint32_t shdr_status, shdr_add_status;
14505 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014506 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14507
James Smart2e90f4b2011-12-13 13:22:37 -050014508 /* sanity check on queue memory */
14509 if (!cq || !eq)
14510 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014511 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014512 hw_page_size = cq->page_size;
James Smart49198b32010-04-06 15:04:33 -040014513
James Smart4f774512009-05-22 14:52:35 -040014514 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14515 if (!mbox)
14516 return -ENOMEM;
14517 length = (sizeof(struct lpfc_mbx_cq_create) -
14518 sizeof(struct lpfc_sli4_cfg_mhdr));
14519 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14520 LPFC_MBOX_OPCODE_CQ_CREATE,
14521 length, LPFC_SLI4_MBX_EMBED);
14522 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014523 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014524 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14525 cq->page_count);
14526 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14527 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014528 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14529 phba->sli4_hba.pc_sli4_params.cqv);
14530 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080014531 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14532 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014533 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14534 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080014535 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14536 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050014537 } else {
14538 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14539 eq->queue_id);
14540 }
James Smart4f774512009-05-22 14:52:35 -040014541 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014542 case 2048:
14543 case 4096:
14544 if (phba->sli4_hba.pc_sli4_params.cqv ==
14545 LPFC_Q_CREATE_VERSION_2) {
14546 cq_create->u.request.context.lpfc_cq_context_count =
14547 cq->entry_count;
14548 bf_set(lpfc_cq_context_count,
14549 &cq_create->u.request.context,
14550 LPFC_CQ_CNT_WORD7);
14551 break;
14552 }
14553 /* Fall Thru */
James Smart4f774512009-05-22 14:52:35 -040014554 default:
14555 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014556 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014557 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014558 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014559 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014560 if (cq->entry_count < 256) {
14561 status = -EINVAL;
14562 goto out;
14563 }
James Smart4f774512009-05-22 14:52:35 -040014564 /* otherwise default to smallest count (drop through) */
14565 case 256:
14566 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14567 LPFC_CQ_CNT_256);
14568 break;
14569 case 512:
14570 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14571 LPFC_CQ_CNT_512);
14572 break;
14573 case 1024:
14574 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14575 LPFC_CQ_CNT_1024);
14576 break;
14577 }
14578 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080014579 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040014580 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14581 putPaddrLow(dmabuf->phys);
14582 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14583 putPaddrHigh(dmabuf->phys);
14584 }
14585 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14586
14587 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014588 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14589 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14590 if (shdr_status || shdr_add_status || rc) {
14591 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14592 "2501 CQ_CREATE mailbox failed with "
14593 "status x%x add_status x%x, mbx status x%x\n",
14594 shdr_status, shdr_add_status, rc);
14595 status = -ENXIO;
14596 goto out;
14597 }
14598 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14599 if (cq->queue_id == 0xFFFF) {
14600 status = -ENXIO;
14601 goto out;
14602 }
14603 /* link the cq onto the parent eq child list */
14604 list_add_tail(&cq->list, &eq->child_list);
14605 /* Set up completion queue's type and subtype */
14606 cq->type = type;
14607 cq->subtype = subtype;
14608 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014609 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014610 cq->host_index = 0;
14611 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014612 cq->entry_repost = LPFC_CQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014613
James Smart8fa38512009-07-19 10:01:03 -040014614out:
14615 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014616 return status;
14617}
14618
14619/**
James Smart2d7dbc42017-02-12 13:52:35 -080014620 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14621 * @phba: HBA structure that indicates port to create a queue on.
14622 * @cqp: The queue structure array to use to create the completion queues.
14623 * @eqp: The event queue array to bind these completion queues to.
14624 *
14625 * This function creates a set of completion queue, s to support MRQ
14626 * as detailed in @cqp, on a port,
14627 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14628 *
14629 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14630 * is used to get the entry count and entry size that are necessary to
14631 * determine the number of pages to allocate and use for this queue. The @eq
14632 * is used to indicate which event queue to bind this completion queue to. This
14633 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14634 * completion queue. This function is asynchronous and will wait for the mailbox
14635 * command to finish before continuing.
14636 *
14637 * On success this function will return a zero. If unable to allocate enough
14638 * memory this function will return -ENOMEM. If the queue create mailbox command
14639 * fails this function will return -ENXIO.
14640 **/
14641int
14642lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14643 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14644{
14645 struct lpfc_queue *cq;
14646 struct lpfc_queue *eq;
14647 struct lpfc_mbx_cq_create_set *cq_set;
14648 struct lpfc_dmabuf *dmabuf;
14649 LPFC_MBOXQ_t *mbox;
14650 int rc, length, alloclen, status = 0;
14651 int cnt, idx, numcq, page_idx = 0;
14652 uint32_t shdr_status, shdr_add_status;
14653 union lpfc_sli4_cfg_shdr *shdr;
14654 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14655
14656 /* sanity check on queue memory */
14657 numcq = phba->cfg_nvmet_mrq;
14658 if (!cqp || !eqp || !numcq)
14659 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080014660
14661 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14662 if (!mbox)
14663 return -ENOMEM;
14664
14665 length = sizeof(struct lpfc_mbx_cq_create_set);
14666 length += ((numcq * cqp[0]->page_count) *
14667 sizeof(struct dma_address));
14668 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14669 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14670 LPFC_SLI4_MBX_NEMBED);
14671 if (alloclen < length) {
14672 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14673 "3098 Allocated DMA memory size (%d) is "
14674 "less than the requested DMA memory size "
14675 "(%d)\n", alloclen, length);
14676 status = -ENOMEM;
14677 goto out;
14678 }
14679 cq_set = mbox->sge_array->addr[0];
14680 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14681 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14682
14683 for (idx = 0; idx < numcq; idx++) {
14684 cq = cqp[idx];
14685 eq = eqp[idx];
14686 if (!cq || !eq) {
14687 status = -ENOMEM;
14688 goto out;
14689 }
James Smart81b96ed2017-11-20 16:00:29 -080014690 if (!phba->sli4_hba.pc_sli4_params.supported)
14691 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080014692
14693 switch (idx) {
14694 case 0:
14695 bf_set(lpfc_mbx_cq_create_set_page_size,
14696 &cq_set->u.request,
14697 (hw_page_size / SLI4_PAGE_SIZE));
14698 bf_set(lpfc_mbx_cq_create_set_num_pages,
14699 &cq_set->u.request, cq->page_count);
14700 bf_set(lpfc_mbx_cq_create_set_evt,
14701 &cq_set->u.request, 1);
14702 bf_set(lpfc_mbx_cq_create_set_valid,
14703 &cq_set->u.request, 1);
14704 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14705 &cq_set->u.request, 0);
14706 bf_set(lpfc_mbx_cq_create_set_num_cq,
14707 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080014708 bf_set(lpfc_mbx_cq_create_set_autovalid,
14709 &cq_set->u.request,
14710 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080014711 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014712 case 2048:
14713 case 4096:
14714 if (phba->sli4_hba.pc_sli4_params.cqv ==
14715 LPFC_Q_CREATE_VERSION_2) {
14716 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14717 &cq_set->u.request,
14718 cq->entry_count);
14719 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14720 &cq_set->u.request,
14721 LPFC_CQ_CNT_WORD7);
14722 break;
14723 }
14724 /* Fall Thru */
James Smart2d7dbc42017-02-12 13:52:35 -080014725 default:
14726 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14727 "3118 Bad CQ count. (%d)\n",
14728 cq->entry_count);
14729 if (cq->entry_count < 256) {
14730 status = -EINVAL;
14731 goto out;
14732 }
14733 /* otherwise default to smallest (drop thru) */
14734 case 256:
14735 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14736 &cq_set->u.request, LPFC_CQ_CNT_256);
14737 break;
14738 case 512:
14739 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14740 &cq_set->u.request, LPFC_CQ_CNT_512);
14741 break;
14742 case 1024:
14743 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14744 &cq_set->u.request, LPFC_CQ_CNT_1024);
14745 break;
14746 }
14747 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14748 &cq_set->u.request, eq->queue_id);
14749 break;
14750 case 1:
14751 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14752 &cq_set->u.request, eq->queue_id);
14753 break;
14754 case 2:
14755 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14756 &cq_set->u.request, eq->queue_id);
14757 break;
14758 case 3:
14759 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14760 &cq_set->u.request, eq->queue_id);
14761 break;
14762 case 4:
14763 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14764 &cq_set->u.request, eq->queue_id);
14765 break;
14766 case 5:
14767 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14768 &cq_set->u.request, eq->queue_id);
14769 break;
14770 case 6:
14771 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14772 &cq_set->u.request, eq->queue_id);
14773 break;
14774 case 7:
14775 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14776 &cq_set->u.request, eq->queue_id);
14777 break;
14778 case 8:
14779 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14780 &cq_set->u.request, eq->queue_id);
14781 break;
14782 case 9:
14783 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14784 &cq_set->u.request, eq->queue_id);
14785 break;
14786 case 10:
14787 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14788 &cq_set->u.request, eq->queue_id);
14789 break;
14790 case 11:
14791 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14792 &cq_set->u.request, eq->queue_id);
14793 break;
14794 case 12:
14795 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14796 &cq_set->u.request, eq->queue_id);
14797 break;
14798 case 13:
14799 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14800 &cq_set->u.request, eq->queue_id);
14801 break;
14802 case 14:
14803 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14804 &cq_set->u.request, eq->queue_id);
14805 break;
14806 case 15:
14807 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14808 &cq_set->u.request, eq->queue_id);
14809 break;
14810 }
14811
14812 /* link the cq onto the parent eq child list */
14813 list_add_tail(&cq->list, &eq->child_list);
14814 /* Set up completion queue's type and subtype */
14815 cq->type = type;
14816 cq->subtype = subtype;
14817 cq->assoc_qid = eq->queue_id;
14818 cq->host_index = 0;
14819 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014820 cq->entry_repost = LPFC_CQ_REPOST;
James Smart81b96ed2017-11-20 16:00:29 -080014821 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080014822
14823 rc = 0;
14824 list_for_each_entry(dmabuf, &cq->page_list, list) {
14825 memset(dmabuf->virt, 0, hw_page_size);
14826 cnt = page_idx + dmabuf->buffer_tag;
14827 cq_set->u.request.page[cnt].addr_lo =
14828 putPaddrLow(dmabuf->phys);
14829 cq_set->u.request.page[cnt].addr_hi =
14830 putPaddrHigh(dmabuf->phys);
14831 rc++;
14832 }
14833 page_idx += rc;
14834 }
14835
14836 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14837
14838 /* The IOCTL status is embedded in the mailbox subheader. */
14839 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14840 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14841 if (shdr_status || shdr_add_status || rc) {
14842 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14843 "3119 CQ_CREATE_SET mailbox failed with "
14844 "status x%x add_status x%x, mbx status x%x\n",
14845 shdr_status, shdr_add_status, rc);
14846 status = -ENXIO;
14847 goto out;
14848 }
14849 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14850 if (rc == 0xFFFF) {
14851 status = -ENXIO;
14852 goto out;
14853 }
14854
14855 for (idx = 0; idx < numcq; idx++) {
14856 cq = cqp[idx];
14857 cq->queue_id = rc + idx;
14858 }
14859
14860out:
14861 lpfc_sli4_mbox_cmd_free(phba, mbox);
14862 return status;
14863}
14864
14865/**
James Smartb19a0612010-04-06 14:48:51 -040014866 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040014867 * @phba: HBA structure that indicates port to create a queue on.
14868 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040014869 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14870 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040014871 *
James Smartb19a0612010-04-06 14:48:51 -040014872 * This function provides failback (fb) functionality when the
14873 * mq_create_ext fails on older FW generations. It's purpose is identical
14874 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040014875 *
James Smartb19a0612010-04-06 14:48:51 -040014876 * This routine cannot fail as all attributes were previously accessed and
14877 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040014878 **/
James Smartb19a0612010-04-06 14:48:51 -040014879static void
14880lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14881 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040014882{
14883 struct lpfc_mbx_mq_create *mq_create;
14884 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040014885 int length;
James Smart04c68492009-05-22 14:52:52 -040014886
James Smart04c68492009-05-22 14:52:52 -040014887 length = (sizeof(struct lpfc_mbx_mq_create) -
14888 sizeof(struct lpfc_sli4_cfg_mhdr));
14889 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14890 LPFC_MBOX_OPCODE_MQ_CREATE,
14891 length, LPFC_SLI4_MBX_EMBED);
14892 mq_create = &mbox->u.mqe.un.mq_create;
14893 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040014894 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040014895 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040014896 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040014897 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14898 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040014899 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014900 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14901 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040014902 break;
14903 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014904 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14905 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040014906 break;
14907 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014908 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14909 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040014910 break;
14911 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014912 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14913 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040014914 break;
14915 }
14916 list_for_each_entry(dmabuf, &mq->page_list, list) {
14917 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040014918 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040014919 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040014920 putPaddrHigh(dmabuf->phys);
14921 }
14922}
14923
14924/**
14925 * lpfc_mq_create - Create a mailbox Queue on the HBA
14926 * @phba: HBA structure that indicates port to create a queue on.
14927 * @mq: The queue structure to use to create the mailbox queue.
14928 * @cq: The completion queue to associate with this cq.
14929 * @subtype: The queue's subtype.
14930 *
14931 * This function creates a mailbox queue, as detailed in @mq, on a port,
14932 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14933 *
14934 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14935 * is used to get the entry count and entry size that are necessary to
14936 * determine the number of pages to allocate and use for this queue. This
14937 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14938 * mailbox queue. This function is asynchronous and will wait for the mailbox
14939 * command to finish before continuing.
14940 *
14941 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014942 * memory this function will return -ENOMEM. If the queue create mailbox command
14943 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040014944 **/
14945int32_t
14946lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14947 struct lpfc_queue *cq, uint32_t subtype)
14948{
14949 struct lpfc_mbx_mq_create *mq_create;
14950 struct lpfc_mbx_mq_create_ext *mq_create_ext;
14951 struct lpfc_dmabuf *dmabuf;
14952 LPFC_MBOXQ_t *mbox;
14953 int rc, length, status = 0;
14954 uint32_t shdr_status, shdr_add_status;
14955 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014956 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040014957
James Smart2e90f4b2011-12-13 13:22:37 -050014958 /* sanity check on queue memory */
14959 if (!mq || !cq)
14960 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014961 if (!phba->sli4_hba.pc_sli4_params.supported)
14962 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040014963
14964 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14965 if (!mbox)
14966 return -ENOMEM;
14967 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
14968 sizeof(struct lpfc_sli4_cfg_mhdr));
14969 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14970 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
14971 length, LPFC_SLI4_MBX_EMBED);
14972
14973 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050014974 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050014975 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14976 &mq_create_ext->u.request, mq->page_count);
14977 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14978 &mq_create_ext->u.request, 1);
14979 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040014980 &mq_create_ext->u.request, 1);
14981 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14982 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050014983 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14984 &mq_create_ext->u.request, 1);
14985 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14986 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040014987 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014988 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14989 phba->sli4_hba.pc_sli4_params.mqv);
14990 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14991 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14992 cq->queue_id);
14993 else
14994 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14995 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040014996 switch (mq->entry_count) {
14997 default:
14998 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14999 "0362 Unsupported MQ count. (%d)\n",
15000 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015001 if (mq->entry_count < 16) {
15002 status = -EINVAL;
15003 goto out;
15004 }
James Smartb19a0612010-04-06 14:48:51 -040015005 /* otherwise default to smallest count (drop through) */
15006 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015007 bf_set(lpfc_mq_context_ring_size,
15008 &mq_create_ext->u.request.context,
15009 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040015010 break;
15011 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015012 bf_set(lpfc_mq_context_ring_size,
15013 &mq_create_ext->u.request.context,
15014 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040015015 break;
15016 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015017 bf_set(lpfc_mq_context_ring_size,
15018 &mq_create_ext->u.request.context,
15019 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040015020 break;
15021 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015022 bf_set(lpfc_mq_context_ring_size,
15023 &mq_create_ext->u.request.context,
15024 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040015025 break;
15026 }
15027 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015028 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040015029 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15030 putPaddrLow(dmabuf->phys);
15031 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040015032 putPaddrHigh(dmabuf->phys);
15033 }
15034 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040015035 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15036 &mq_create_ext->u.response);
15037 if (rc != MBX_SUCCESS) {
15038 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15039 "2795 MQ_CREATE_EXT failed with "
15040 "status x%x. Failback to MQ_CREATE.\n",
15041 rc);
15042 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15043 mq_create = &mbox->u.mqe.un.mq_create;
15044 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15045 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15046 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15047 &mq_create->u.response);
15048 }
15049
James Smart04c68492009-05-22 14:52:52 -040015050 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040015051 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15052 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15053 if (shdr_status || shdr_add_status || rc) {
15054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15055 "2502 MQ_CREATE mailbox failed with "
15056 "status x%x add_status x%x, mbx status x%x\n",
15057 shdr_status, shdr_add_status, rc);
15058 status = -ENXIO;
15059 goto out;
15060 }
James Smart04c68492009-05-22 14:52:52 -040015061 if (mq->queue_id == 0xFFFF) {
15062 status = -ENXIO;
15063 goto out;
15064 }
15065 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050015066 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040015067 mq->subtype = subtype;
15068 mq->host_index = 0;
15069 mq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070015070 mq->entry_repost = LPFC_MQ_REPOST;
James Smart04c68492009-05-22 14:52:52 -040015071
15072 /* link the mq onto the parent cq child list */
15073 list_add_tail(&mq->list, &cq->child_list);
15074out:
James Smart8fa38512009-07-19 10:01:03 -040015075 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015076 return status;
15077}
15078
15079/**
James Smart4f774512009-05-22 14:52:35 -040015080 * lpfc_wq_create - Create a Work Queue on the HBA
15081 * @phba: HBA structure that indicates port to create a queue on.
15082 * @wq: The queue structure to use to create the work queue.
15083 * @cq: The completion queue to bind this work queue to.
15084 * @subtype: The subtype of the work queue indicating its functionality.
15085 *
15086 * This function creates a work queue, as detailed in @wq, on a port, described
15087 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15088 *
15089 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15090 * is used to get the entry count and entry size that are necessary to
15091 * determine the number of pages to allocate and use for this queue. The @cq
15092 * is used to indicate which completion queue to bind this work queue to. This
15093 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15094 * work queue. This function is asynchronous and will wait for the mailbox
15095 * command to finish before continuing.
15096 *
15097 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015098 * memory this function will return -ENOMEM. If the queue create mailbox command
15099 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015100 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015101int
James Smart4f774512009-05-22 14:52:35 -040015102lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15103 struct lpfc_queue *cq, uint32_t subtype)
15104{
15105 struct lpfc_mbx_wq_create *wq_create;
15106 struct lpfc_dmabuf *dmabuf;
15107 LPFC_MBOXQ_t *mbox;
15108 int rc, length, status = 0;
15109 uint32_t shdr_status, shdr_add_status;
15110 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015111 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050015112 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050015113 void __iomem *bar_memmap_p;
15114 uint32_t db_offset;
15115 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080015116 uint8_t dpp_barset;
15117 uint32_t dpp_offset;
15118 unsigned long pg_addr;
James Smart81b96ed2017-11-20 16:00:29 -080015119 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040015120
James Smart2e90f4b2011-12-13 13:22:37 -050015121 /* sanity check on queue memory */
15122 if (!wq || !cq)
15123 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015124 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015125 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040015126
15127 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15128 if (!mbox)
15129 return -ENOMEM;
15130 length = (sizeof(struct lpfc_mbx_wq_create) -
15131 sizeof(struct lpfc_sli4_cfg_mhdr));
15132 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15133 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15134 length, LPFC_SLI4_MBX_EMBED);
15135 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015136 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015137 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15138 wq->page_count);
15139 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15140 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040015141
15142 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050015143 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15144 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050015145
James Smartc176ffa2018-01-30 15:58:46 -080015146 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15147 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080015148 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15149 else
15150 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15151
James Smart0c651872013-07-15 18:33:23 -040015152
James Smart1351e692018-02-22 08:18:43 -080015153 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15154 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15155 else
15156 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15157
15158 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040015159 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050015160 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15161 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070015162 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15163 LPFC_Q_CREATE_VERSION_1);
15164
James Smart5a6f1332011-03-11 16:05:35 -050015165 switch (wq->entry_size) {
15166 default:
15167 case 64:
15168 bf_set(lpfc_mbx_wq_create_wqe_size,
15169 &wq_create->u.request_1,
15170 LPFC_WQ_WQE_SIZE_64);
15171 break;
15172 case 128:
15173 bf_set(lpfc_mbx_wq_create_wqe_size,
15174 &wq_create->u.request_1,
15175 LPFC_WQ_WQE_SIZE_128);
15176 break;
15177 }
James Smart1351e692018-02-22 08:18:43 -080015178 /* Request DPP by default */
15179 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080015180 bf_set(lpfc_mbx_wq_create_page_size,
15181 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015182 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015183 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015184 break;
15185 default:
James Smart1351e692018-02-22 08:18:43 -080015186 page = wq_create->u.request.page;
15187 break;
James Smart5a6f1332011-03-11 16:05:35 -050015188 }
James Smart0c651872013-07-15 18:33:23 -040015189
James Smart4f774512009-05-22 14:52:35 -040015190 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015191 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015192 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15193 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015194 }
James Smart962bc512013-01-03 15:44:00 -050015195
15196 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15197 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15198
James Smart4f774512009-05-22 14:52:35 -040015199 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15200 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015201 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15202 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15203 if (shdr_status || shdr_add_status || rc) {
15204 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15205 "2503 WQ_CREATE mailbox failed with "
15206 "status x%x add_status x%x, mbx status x%x\n",
15207 shdr_status, shdr_add_status, rc);
15208 status = -ENXIO;
15209 goto out;
15210 }
James Smart1351e692018-02-22 08:18:43 -080015211
15212 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15213 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15214 &wq_create->u.response);
15215 else
15216 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15217 &wq_create->u.response_1);
15218
James Smart4f774512009-05-22 14:52:35 -040015219 if (wq->queue_id == 0xFFFF) {
15220 status = -ENXIO;
15221 goto out;
15222 }
James Smart1351e692018-02-22 08:18:43 -080015223
15224 wq->db_format = LPFC_DB_LIST_FORMAT;
15225 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15226 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15227 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15228 &wq_create->u.response);
15229 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15230 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15231 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15232 "3265 WQ[%d] doorbell format "
15233 "not supported: x%x\n",
15234 wq->queue_id, wq->db_format);
15235 status = -EINVAL;
15236 goto out;
15237 }
15238 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15239 &wq_create->u.response);
15240 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15241 pci_barset);
15242 if (!bar_memmap_p) {
15243 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15244 "3263 WQ[%d] failed to memmap "
15245 "pci barset:x%x\n",
15246 wq->queue_id, pci_barset);
15247 status = -ENOMEM;
15248 goto out;
15249 }
15250 db_offset = wq_create->u.response.doorbell_offset;
15251 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15252 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15253 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15254 "3252 WQ[%d] doorbell offset "
15255 "not supported: x%x\n",
15256 wq->queue_id, db_offset);
15257 status = -EINVAL;
15258 goto out;
15259 }
15260 wq->db_regaddr = bar_memmap_p + db_offset;
15261 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15262 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15263 "format:x%x\n", wq->queue_id,
15264 pci_barset, db_offset, wq->db_format);
15265 } else
15266 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015267 } else {
James Smart1351e692018-02-22 08:18:43 -080015268 /* Check if DPP was honored by the firmware */
15269 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15270 &wq_create->u.response_1);
15271 if (wq->dpp_enable) {
15272 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15273 &wq_create->u.response_1);
15274 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15275 pci_barset);
15276 if (!bar_memmap_p) {
15277 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15278 "3267 WQ[%d] failed to memmap "
15279 "pci barset:x%x\n",
15280 wq->queue_id, pci_barset);
15281 status = -ENOMEM;
15282 goto out;
15283 }
15284 db_offset = wq_create->u.response_1.doorbell_offset;
15285 wq->db_regaddr = bar_memmap_p + db_offset;
15286 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15287 &wq_create->u.response_1);
15288 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15289 &wq_create->u.response_1);
15290 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15291 dpp_barset);
15292 if (!bar_memmap_p) {
15293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15294 "3268 WQ[%d] failed to memmap "
15295 "pci barset:x%x\n",
15296 wq->queue_id, dpp_barset);
15297 status = -ENOMEM;
15298 goto out;
15299 }
15300 dpp_offset = wq_create->u.response_1.dpp_offset;
15301 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15302 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15303 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15304 "dpp_id:x%x dpp_barset:x%x "
15305 "dpp_offset:x%x\n",
15306 wq->queue_id, pci_barset, db_offset,
15307 wq->dpp_id, dpp_barset, dpp_offset);
15308
15309 /* Enable combined writes for DPP aperture */
15310 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15311#ifdef CONFIG_X86
15312 rc = set_memory_wc(pg_addr, 1);
15313 if (rc) {
15314 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15315 "3272 Cannot setup Combined "
15316 "Write on WQ[%d] - disable DPP\n",
15317 wq->queue_id);
15318 phba->cfg_enable_dpp = 0;
15319 }
15320#else
15321 phba->cfg_enable_dpp = 0;
15322#endif
15323 } else
15324 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015325 }
James Smart895427b2017-02-12 13:52:30 -080015326 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15327 if (wq->pring == NULL) {
15328 status = -ENOMEM;
15329 goto out;
15330 }
James Smart4f774512009-05-22 14:52:35 -040015331 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015332 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015333 wq->subtype = subtype;
15334 wq->host_index = 0;
15335 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050015336 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040015337
15338 /* link the wq onto the parent cq child list */
15339 list_add_tail(&wq->list, &cq->child_list);
15340out:
James Smart8fa38512009-07-19 10:01:03 -040015341 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015342 return status;
15343}
15344
15345/**
15346 * lpfc_rq_create - Create a Receive Queue on the HBA
15347 * @phba: HBA structure that indicates port to create a queue on.
15348 * @hrq: The queue structure to use to create the header receive queue.
15349 * @drq: The queue structure to use to create the data receive queue.
15350 * @cq: The completion queue to bind this work queue to.
15351 *
15352 * This function creates a receive buffer queue pair , as detailed in @hrq and
15353 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15354 * to the HBA.
15355 *
15356 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15357 * struct is used to get the entry count that is necessary to determine the
15358 * number of pages to use for this queue. The @cq is used to indicate which
15359 * completion queue to bind received buffers that are posted to these queues to.
15360 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15361 * receive queue pair. This function is asynchronous and will wait for the
15362 * mailbox command to finish before continuing.
15363 *
15364 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015365 * memory this function will return -ENOMEM. If the queue create mailbox command
15366 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015367 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015368int
James Smart4f774512009-05-22 14:52:35 -040015369lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15370 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15371{
15372 struct lpfc_mbx_rq_create *rq_create;
15373 struct lpfc_dmabuf *dmabuf;
15374 LPFC_MBOXQ_t *mbox;
15375 int rc, length, status = 0;
15376 uint32_t shdr_status, shdr_add_status;
15377 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015378 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015379 void __iomem *bar_memmap_p;
15380 uint32_t db_offset;
15381 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015382
James Smart2e90f4b2011-12-13 13:22:37 -050015383 /* sanity check on queue memory */
15384 if (!hrq || !drq || !cq)
15385 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015386 if (!phba->sli4_hba.pc_sli4_params.supported)
15387 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015388
15389 if (hrq->entry_count != drq->entry_count)
15390 return -EINVAL;
15391 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15392 if (!mbox)
15393 return -ENOMEM;
15394 length = (sizeof(struct lpfc_mbx_rq_create) -
15395 sizeof(struct lpfc_sli4_cfg_mhdr));
15396 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15397 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15398 length, LPFC_SLI4_MBX_EMBED);
15399 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015400 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15401 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15402 phba->sli4_hba.pc_sli4_params.rqv);
15403 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15404 bf_set(lpfc_rq_context_rqe_count_1,
15405 &rq_create->u.request.context,
15406 hrq->entry_count);
15407 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015408 bf_set(lpfc_rq_context_rqe_size,
15409 &rq_create->u.request.context,
15410 LPFC_RQE_SIZE_8);
15411 bf_set(lpfc_rq_context_page_size,
15412 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015413 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015414 } else {
15415 switch (hrq->entry_count) {
15416 default:
15417 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15418 "2535 Unsupported RQ count. (%d)\n",
15419 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015420 if (hrq->entry_count < 512) {
15421 status = -EINVAL;
15422 goto out;
15423 }
James Smart5a6f1332011-03-11 16:05:35 -050015424 /* otherwise default to smallest count (drop through) */
15425 case 512:
15426 bf_set(lpfc_rq_context_rqe_count,
15427 &rq_create->u.request.context,
15428 LPFC_RQ_RING_SIZE_512);
15429 break;
15430 case 1024:
15431 bf_set(lpfc_rq_context_rqe_count,
15432 &rq_create->u.request.context,
15433 LPFC_RQ_RING_SIZE_1024);
15434 break;
15435 case 2048:
15436 bf_set(lpfc_rq_context_rqe_count,
15437 &rq_create->u.request.context,
15438 LPFC_RQ_RING_SIZE_2048);
15439 break;
15440 case 4096:
15441 bf_set(lpfc_rq_context_rqe_count,
15442 &rq_create->u.request.context,
15443 LPFC_RQ_RING_SIZE_4096);
15444 break;
15445 }
15446 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15447 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015448 }
15449 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15450 cq->queue_id);
15451 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15452 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015453 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015454 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015455 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15456 putPaddrLow(dmabuf->phys);
15457 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15458 putPaddrHigh(dmabuf->phys);
15459 }
James Smart962bc512013-01-03 15:44:00 -050015460 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15461 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15462
James Smart4f774512009-05-22 14:52:35 -040015463 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15464 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015465 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15466 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15467 if (shdr_status || shdr_add_status || rc) {
15468 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15469 "2504 RQ_CREATE mailbox failed with "
15470 "status x%x add_status x%x, mbx status x%x\n",
15471 shdr_status, shdr_add_status, rc);
15472 status = -ENXIO;
15473 goto out;
15474 }
15475 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15476 if (hrq->queue_id == 0xFFFF) {
15477 status = -ENXIO;
15478 goto out;
15479 }
James Smart962bc512013-01-03 15:44:00 -050015480
15481 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15482 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15483 &rq_create->u.response);
15484 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15485 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15486 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15487 "3262 RQ [%d] doorbell format not "
15488 "supported: x%x\n", hrq->queue_id,
15489 hrq->db_format);
15490 status = -EINVAL;
15491 goto out;
15492 }
15493
15494 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15495 &rq_create->u.response);
15496 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15497 if (!bar_memmap_p) {
15498 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15499 "3269 RQ[%d] failed to memmap pci "
15500 "barset:x%x\n", hrq->queue_id,
15501 pci_barset);
15502 status = -ENOMEM;
15503 goto out;
15504 }
15505
15506 db_offset = rq_create->u.response.doorbell_offset;
15507 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15508 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15510 "3270 RQ[%d] doorbell offset not "
15511 "supported: x%x\n", hrq->queue_id,
15512 db_offset);
15513 status = -EINVAL;
15514 goto out;
15515 }
15516 hrq->db_regaddr = bar_memmap_p + db_offset;
15517 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015518 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15519 "format:x%x\n", hrq->queue_id, pci_barset,
15520 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015521 } else {
15522 hrq->db_format = LPFC_DB_RING_FORMAT;
15523 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15524 }
James Smart4f774512009-05-22 14:52:35 -040015525 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015526 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015527 hrq->subtype = subtype;
15528 hrq->host_index = 0;
15529 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015530 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015531
15532 /* now create the data queue */
15533 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15534 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15535 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015536 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15537 phba->sli4_hba.pc_sli4_params.rqv);
15538 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15539 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015540 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015541 if (subtype == LPFC_NVMET)
15542 rq_create->u.request.context.buffer_size =
15543 LPFC_NVMET_DATA_BUF_SIZE;
15544 else
15545 rq_create->u.request.context.buffer_size =
15546 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015547 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15548 LPFC_RQE_SIZE_8);
15549 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15550 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015551 } else {
15552 switch (drq->entry_count) {
15553 default:
15554 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15555 "2536 Unsupported RQ count. (%d)\n",
15556 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015557 if (drq->entry_count < 512) {
15558 status = -EINVAL;
15559 goto out;
15560 }
James Smart5a6f1332011-03-11 16:05:35 -050015561 /* otherwise default to smallest count (drop through) */
15562 case 512:
15563 bf_set(lpfc_rq_context_rqe_count,
15564 &rq_create->u.request.context,
15565 LPFC_RQ_RING_SIZE_512);
15566 break;
15567 case 1024:
15568 bf_set(lpfc_rq_context_rqe_count,
15569 &rq_create->u.request.context,
15570 LPFC_RQ_RING_SIZE_1024);
15571 break;
15572 case 2048:
15573 bf_set(lpfc_rq_context_rqe_count,
15574 &rq_create->u.request.context,
15575 LPFC_RQ_RING_SIZE_2048);
15576 break;
15577 case 4096:
15578 bf_set(lpfc_rq_context_rqe_count,
15579 &rq_create->u.request.context,
15580 LPFC_RQ_RING_SIZE_4096);
15581 break;
15582 }
James Smart3c603be2017-05-15 15:20:44 -070015583 if (subtype == LPFC_NVMET)
15584 bf_set(lpfc_rq_context_buf_size,
15585 &rq_create->u.request.context,
15586 LPFC_NVMET_DATA_BUF_SIZE);
15587 else
15588 bf_set(lpfc_rq_context_buf_size,
15589 &rq_create->u.request.context,
15590 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015591 }
15592 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15593 cq->queue_id);
15594 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15595 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015596 list_for_each_entry(dmabuf, &drq->page_list, list) {
15597 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15598 putPaddrLow(dmabuf->phys);
15599 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15600 putPaddrHigh(dmabuf->phys);
15601 }
James Smart962bc512013-01-03 15:44:00 -050015602 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15603 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015604 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15605 /* The IOCTL status is embedded in the mailbox subheader. */
15606 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15607 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15608 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15609 if (shdr_status || shdr_add_status || rc) {
15610 status = -ENXIO;
15611 goto out;
15612 }
15613 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15614 if (drq->queue_id == 0xFFFF) {
15615 status = -ENXIO;
15616 goto out;
15617 }
15618 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015619 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015620 drq->subtype = subtype;
15621 drq->host_index = 0;
15622 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015623 drq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015624
15625 /* link the header and data RQs onto the parent cq child list */
15626 list_add_tail(&hrq->list, &cq->child_list);
15627 list_add_tail(&drq->list, &cq->child_list);
15628
15629out:
James Smart8fa38512009-07-19 10:01:03 -040015630 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015631 return status;
15632}
15633
15634/**
James Smart2d7dbc42017-02-12 13:52:35 -080015635 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15636 * @phba: HBA structure that indicates port to create a queue on.
15637 * @hrqp: The queue structure array to use to create the header receive queues.
15638 * @drqp: The queue structure array to use to create the data receive queues.
15639 * @cqp: The completion queue array to bind these receive queues to.
15640 *
15641 * This function creates a receive buffer queue pair , as detailed in @hrq and
15642 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15643 * to the HBA.
15644 *
15645 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15646 * struct is used to get the entry count that is necessary to determine the
15647 * number of pages to use for this queue. The @cq is used to indicate which
15648 * completion queue to bind received buffers that are posted to these queues to.
15649 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15650 * receive queue pair. This function is asynchronous and will wait for the
15651 * mailbox command to finish before continuing.
15652 *
15653 * On success this function will return a zero. If unable to allocate enough
15654 * memory this function will return -ENOMEM. If the queue create mailbox command
15655 * fails this function will return -ENXIO.
15656 **/
15657int
15658lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15659 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15660 uint32_t subtype)
15661{
15662 struct lpfc_queue *hrq, *drq, *cq;
15663 struct lpfc_mbx_rq_create_v2 *rq_create;
15664 struct lpfc_dmabuf *dmabuf;
15665 LPFC_MBOXQ_t *mbox;
15666 int rc, length, alloclen, status = 0;
15667 int cnt, idx, numrq, page_idx = 0;
15668 uint32_t shdr_status, shdr_add_status;
15669 union lpfc_sli4_cfg_shdr *shdr;
15670 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15671
15672 numrq = phba->cfg_nvmet_mrq;
15673 /* sanity check on array memory */
15674 if (!hrqp || !drqp || !cqp || !numrq)
15675 return -ENODEV;
15676 if (!phba->sli4_hba.pc_sli4_params.supported)
15677 hw_page_size = SLI4_PAGE_SIZE;
15678
15679 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15680 if (!mbox)
15681 return -ENOMEM;
15682
15683 length = sizeof(struct lpfc_mbx_rq_create_v2);
15684 length += ((2 * numrq * hrqp[0]->page_count) *
15685 sizeof(struct dma_address));
15686
15687 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15688 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15689 LPFC_SLI4_MBX_NEMBED);
15690 if (alloclen < length) {
15691 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15692 "3099 Allocated DMA memory size (%d) is "
15693 "less than the requested DMA memory size "
15694 "(%d)\n", alloclen, length);
15695 status = -ENOMEM;
15696 goto out;
15697 }
15698
15699
15700
15701 rq_create = mbox->sge_array->addr[0];
15702 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15703
15704 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15705 cnt = 0;
15706
15707 for (idx = 0; idx < numrq; idx++) {
15708 hrq = hrqp[idx];
15709 drq = drqp[idx];
15710 cq = cqp[idx];
15711
James Smart2d7dbc42017-02-12 13:52:35 -080015712 /* sanity check on queue memory */
15713 if (!hrq || !drq || !cq) {
15714 status = -ENODEV;
15715 goto out;
15716 }
15717
James Smart7aabe842017-03-04 09:30:22 -080015718 if (hrq->entry_count != drq->entry_count) {
15719 status = -EINVAL;
15720 goto out;
15721 }
15722
James Smart2d7dbc42017-02-12 13:52:35 -080015723 if (idx == 0) {
15724 bf_set(lpfc_mbx_rq_create_num_pages,
15725 &rq_create->u.request,
15726 hrq->page_count);
15727 bf_set(lpfc_mbx_rq_create_rq_cnt,
15728 &rq_create->u.request, (numrq * 2));
15729 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15730 1);
15731 bf_set(lpfc_rq_context_base_cq,
15732 &rq_create->u.request.context,
15733 cq->queue_id);
15734 bf_set(lpfc_rq_context_data_size,
15735 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015736 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015737 bf_set(lpfc_rq_context_hdr_size,
15738 &rq_create->u.request.context,
15739 LPFC_HDR_BUF_SIZE);
15740 bf_set(lpfc_rq_context_rqe_count_1,
15741 &rq_create->u.request.context,
15742 hrq->entry_count);
15743 bf_set(lpfc_rq_context_rqe_size,
15744 &rq_create->u.request.context,
15745 LPFC_RQE_SIZE_8);
15746 bf_set(lpfc_rq_context_page_size,
15747 &rq_create->u.request.context,
15748 (PAGE_SIZE/SLI4_PAGE_SIZE));
15749 }
15750 rc = 0;
15751 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15752 memset(dmabuf->virt, 0, hw_page_size);
15753 cnt = page_idx + dmabuf->buffer_tag;
15754 rq_create->u.request.page[cnt].addr_lo =
15755 putPaddrLow(dmabuf->phys);
15756 rq_create->u.request.page[cnt].addr_hi =
15757 putPaddrHigh(dmabuf->phys);
15758 rc++;
15759 }
15760 page_idx += rc;
15761
15762 rc = 0;
15763 list_for_each_entry(dmabuf, &drq->page_list, list) {
15764 memset(dmabuf->virt, 0, hw_page_size);
15765 cnt = page_idx + dmabuf->buffer_tag;
15766 rq_create->u.request.page[cnt].addr_lo =
15767 putPaddrLow(dmabuf->phys);
15768 rq_create->u.request.page[cnt].addr_hi =
15769 putPaddrHigh(dmabuf->phys);
15770 rc++;
15771 }
15772 page_idx += rc;
15773
15774 hrq->db_format = LPFC_DB_RING_FORMAT;
15775 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15776 hrq->type = LPFC_HRQ;
15777 hrq->assoc_qid = cq->queue_id;
15778 hrq->subtype = subtype;
15779 hrq->host_index = 0;
15780 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015781 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015782
15783 drq->db_format = LPFC_DB_RING_FORMAT;
15784 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15785 drq->type = LPFC_DRQ;
15786 drq->assoc_qid = cq->queue_id;
15787 drq->subtype = subtype;
15788 drq->host_index = 0;
15789 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015790 drq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015791
15792 list_add_tail(&hrq->list, &cq->child_list);
15793 list_add_tail(&drq->list, &cq->child_list);
15794 }
15795
15796 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15797 /* The IOCTL status is embedded in the mailbox subheader. */
15798 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15799 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15800 if (shdr_status || shdr_add_status || rc) {
15801 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15802 "3120 RQ_CREATE mailbox failed with "
15803 "status x%x add_status x%x, mbx status x%x\n",
15804 shdr_status, shdr_add_status, rc);
15805 status = -ENXIO;
15806 goto out;
15807 }
15808 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15809 if (rc == 0xFFFF) {
15810 status = -ENXIO;
15811 goto out;
15812 }
15813
15814 /* Initialize all RQs with associated queue id */
15815 for (idx = 0; idx < numrq; idx++) {
15816 hrq = hrqp[idx];
15817 hrq->queue_id = rc + (2 * idx);
15818 drq = drqp[idx];
15819 drq->queue_id = rc + (2 * idx) + 1;
15820 }
15821
15822out:
15823 lpfc_sli4_mbox_cmd_free(phba, mbox);
15824 return status;
15825}
15826
15827/**
James Smart4f774512009-05-22 14:52:35 -040015828 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15829 * @eq: The queue structure associated with the queue to destroy.
15830 *
15831 * This function destroys a queue, as detailed in @eq by sending an mailbox
15832 * command, specific to the type of queue, to the HBA.
15833 *
15834 * The @eq struct is used to get the queue ID of the queue to destroy.
15835 *
15836 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015837 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015838 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015839int
James Smart4f774512009-05-22 14:52:35 -040015840lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15841{
15842 LPFC_MBOXQ_t *mbox;
15843 int rc, length, status = 0;
15844 uint32_t shdr_status, shdr_add_status;
15845 union lpfc_sli4_cfg_shdr *shdr;
15846
James Smart2e90f4b2011-12-13 13:22:37 -050015847 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015848 if (!eq)
15849 return -ENODEV;
15850 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15851 if (!mbox)
15852 return -ENOMEM;
15853 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15854 sizeof(struct lpfc_sli4_cfg_mhdr));
15855 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15856 LPFC_MBOX_OPCODE_EQ_DESTROY,
15857 length, LPFC_SLI4_MBX_EMBED);
15858 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15859 eq->queue_id);
15860 mbox->vport = eq->phba->pport;
15861 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15862
15863 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15864 /* The IOCTL status is embedded in the mailbox subheader. */
15865 shdr = (union lpfc_sli4_cfg_shdr *)
15866 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15867 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15868 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15869 if (shdr_status || shdr_add_status || rc) {
15870 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15871 "2505 EQ_DESTROY mailbox failed with "
15872 "status x%x add_status x%x, mbx status x%x\n",
15873 shdr_status, shdr_add_status, rc);
15874 status = -ENXIO;
15875 }
15876
15877 /* Remove eq from any list */
15878 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040015879 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015880 return status;
15881}
15882
15883/**
15884 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15885 * @cq: The queue structure associated with the queue to destroy.
15886 *
15887 * This function destroys a queue, as detailed in @cq by sending an mailbox
15888 * command, specific to the type of queue, to the HBA.
15889 *
15890 * The @cq struct is used to get the queue ID of the queue to destroy.
15891 *
15892 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015893 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015894 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015895int
James Smart4f774512009-05-22 14:52:35 -040015896lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15897{
15898 LPFC_MBOXQ_t *mbox;
15899 int rc, length, status = 0;
15900 uint32_t shdr_status, shdr_add_status;
15901 union lpfc_sli4_cfg_shdr *shdr;
15902
James Smart2e90f4b2011-12-13 13:22:37 -050015903 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015904 if (!cq)
15905 return -ENODEV;
15906 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15907 if (!mbox)
15908 return -ENOMEM;
15909 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15910 sizeof(struct lpfc_sli4_cfg_mhdr));
15911 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15912 LPFC_MBOX_OPCODE_CQ_DESTROY,
15913 length, LPFC_SLI4_MBX_EMBED);
15914 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15915 cq->queue_id);
15916 mbox->vport = cq->phba->pport;
15917 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15918 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15919 /* The IOCTL status is embedded in the mailbox subheader. */
15920 shdr = (union lpfc_sli4_cfg_shdr *)
15921 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15922 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15923 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15924 if (shdr_status || shdr_add_status || rc) {
15925 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15926 "2506 CQ_DESTROY mailbox failed with "
15927 "status x%x add_status x%x, mbx status x%x\n",
15928 shdr_status, shdr_add_status, rc);
15929 status = -ENXIO;
15930 }
15931 /* Remove cq from any list */
15932 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040015933 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015934 return status;
15935}
15936
15937/**
James Smart04c68492009-05-22 14:52:52 -040015938 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15939 * @qm: The queue structure associated with the queue to destroy.
15940 *
15941 * This function destroys a queue, as detailed in @mq by sending an mailbox
15942 * command, specific to the type of queue, to the HBA.
15943 *
15944 * The @mq struct is used to get the queue ID of the queue to destroy.
15945 *
15946 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015947 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040015948 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015949int
James Smart04c68492009-05-22 14:52:52 -040015950lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15951{
15952 LPFC_MBOXQ_t *mbox;
15953 int rc, length, status = 0;
15954 uint32_t shdr_status, shdr_add_status;
15955 union lpfc_sli4_cfg_shdr *shdr;
15956
James Smart2e90f4b2011-12-13 13:22:37 -050015957 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040015958 if (!mq)
15959 return -ENODEV;
15960 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15961 if (!mbox)
15962 return -ENOMEM;
15963 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15964 sizeof(struct lpfc_sli4_cfg_mhdr));
15965 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15966 LPFC_MBOX_OPCODE_MQ_DESTROY,
15967 length, LPFC_SLI4_MBX_EMBED);
15968 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15969 mq->queue_id);
15970 mbox->vport = mq->phba->pport;
15971 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15972 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15973 /* The IOCTL status is embedded in the mailbox subheader. */
15974 shdr = (union lpfc_sli4_cfg_shdr *)
15975 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15976 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15977 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15978 if (shdr_status || shdr_add_status || rc) {
15979 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15980 "2507 MQ_DESTROY mailbox failed with "
15981 "status x%x add_status x%x, mbx status x%x\n",
15982 shdr_status, shdr_add_status, rc);
15983 status = -ENXIO;
15984 }
15985 /* Remove mq from any list */
15986 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040015987 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015988 return status;
15989}
15990
15991/**
James Smart4f774512009-05-22 14:52:35 -040015992 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15993 * @wq: The queue structure associated with the queue to destroy.
15994 *
15995 * This function destroys a queue, as detailed in @wq by sending an mailbox
15996 * command, specific to the type of queue, to the HBA.
15997 *
15998 * The @wq struct is used to get the queue ID of the queue to destroy.
15999 *
16000 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016001 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016002 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016003int
James Smart4f774512009-05-22 14:52:35 -040016004lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16005{
16006 LPFC_MBOXQ_t *mbox;
16007 int rc, length, status = 0;
16008 uint32_t shdr_status, shdr_add_status;
16009 union lpfc_sli4_cfg_shdr *shdr;
16010
James Smart2e90f4b2011-12-13 13:22:37 -050016011 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016012 if (!wq)
16013 return -ENODEV;
16014 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16015 if (!mbox)
16016 return -ENOMEM;
16017 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16018 sizeof(struct lpfc_sli4_cfg_mhdr));
16019 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16020 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16021 length, LPFC_SLI4_MBX_EMBED);
16022 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16023 wq->queue_id);
16024 mbox->vport = wq->phba->pport;
16025 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16026 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16027 shdr = (union lpfc_sli4_cfg_shdr *)
16028 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16029 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16030 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16031 if (shdr_status || shdr_add_status || rc) {
16032 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16033 "2508 WQ_DESTROY mailbox failed with "
16034 "status x%x add_status x%x, mbx status x%x\n",
16035 shdr_status, shdr_add_status, rc);
16036 status = -ENXIO;
16037 }
16038 /* Remove wq from any list */
16039 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070016040 kfree(wq->pring);
16041 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040016042 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016043 return status;
16044}
16045
16046/**
16047 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16048 * @rq: The queue structure associated with the queue to destroy.
16049 *
16050 * This function destroys a queue, as detailed in @rq by sending an mailbox
16051 * command, specific to the type of queue, to the HBA.
16052 *
16053 * The @rq struct is used to get the queue ID of the queue to destroy.
16054 *
16055 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016056 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016057 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016058int
James Smart4f774512009-05-22 14:52:35 -040016059lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16060 struct lpfc_queue *drq)
16061{
16062 LPFC_MBOXQ_t *mbox;
16063 int rc, length, status = 0;
16064 uint32_t shdr_status, shdr_add_status;
16065 union lpfc_sli4_cfg_shdr *shdr;
16066
James Smart2e90f4b2011-12-13 13:22:37 -050016067 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016068 if (!hrq || !drq)
16069 return -ENODEV;
16070 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16071 if (!mbox)
16072 return -ENOMEM;
16073 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050016074 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040016075 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16076 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16077 length, LPFC_SLI4_MBX_EMBED);
16078 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16079 hrq->queue_id);
16080 mbox->vport = hrq->phba->pport;
16081 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16082 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16083 /* The IOCTL status is embedded in the mailbox subheader. */
16084 shdr = (union lpfc_sli4_cfg_shdr *)
16085 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16086 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16087 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16088 if (shdr_status || shdr_add_status || rc) {
16089 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16090 "2509 RQ_DESTROY mailbox failed with "
16091 "status x%x add_status x%x, mbx status x%x\n",
16092 shdr_status, shdr_add_status, rc);
16093 if (rc != MBX_TIMEOUT)
16094 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16095 return -ENXIO;
16096 }
16097 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16098 drq->queue_id);
16099 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16100 shdr = (union lpfc_sli4_cfg_shdr *)
16101 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16102 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16103 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16104 if (shdr_status || shdr_add_status || rc) {
16105 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16106 "2510 RQ_DESTROY mailbox failed with "
16107 "status x%x add_status x%x, mbx status x%x\n",
16108 shdr_status, shdr_add_status, rc);
16109 status = -ENXIO;
16110 }
16111 list_del_init(&hrq->list);
16112 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040016113 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016114 return status;
16115}
16116
16117/**
16118 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16119 * @phba: The virtual port for which this call being executed.
16120 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16121 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16122 * @xritag: the xritag that ties this io to the SGL pages.
16123 *
16124 * This routine will post the sgl pages for the IO that has the xritag
16125 * that is in the iocbq structure. The xritag is assigned during iocbq
16126 * creation and persists for as long as the driver is loaded.
16127 * if the caller has fewer than 256 scatter gather segments to map then
16128 * pdma_phys_addr1 should be 0.
16129 * If the caller needs to map more than 256 scatter gather segment then
16130 * pdma_phys_addr1 should be a valid physical address.
16131 * physical address for SGLs must be 64 byte aligned.
16132 * If you are going to map 2 SGL's then the first one must have 256 entries
16133 * the second sgl can have between 1 and 256 entries.
16134 *
16135 * Return codes:
16136 * 0 - Success
16137 * -ENXIO, -ENOMEM - Failure
16138 **/
16139int
16140lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16141 dma_addr_t pdma_phys_addr0,
16142 dma_addr_t pdma_phys_addr1,
16143 uint16_t xritag)
16144{
16145 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16146 LPFC_MBOXQ_t *mbox;
16147 int rc;
16148 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040016149 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040016150 union lpfc_sli4_cfg_shdr *shdr;
16151
16152 if (xritag == NO_XRI) {
16153 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16154 "0364 Invalid param:\n");
16155 return -EINVAL;
16156 }
16157
16158 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16159 if (!mbox)
16160 return -ENOMEM;
16161
16162 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16163 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16164 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050016165 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040016166
16167 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16168 &mbox->u.mqe.un.post_sgl_pages;
16169 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16170 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16171
16172 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16173 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16174 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16175 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16176
16177 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16178 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16179 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16180 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16181 if (!phba->sli4_hba.intr_enable)
16182 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040016183 else {
James Smarta183a152011-10-10 21:32:43 -040016184 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040016185 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16186 }
James Smart4f774512009-05-22 14:52:35 -040016187 /* The IOCTL status is embedded in the mailbox subheader. */
16188 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16189 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16190 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16191 if (rc != MBX_TIMEOUT)
16192 mempool_free(mbox, phba->mbox_mem_pool);
16193 if (shdr_status || shdr_add_status || rc) {
16194 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16195 "2511 POST_SGL mailbox failed with "
16196 "status x%x add_status x%x, mbx status x%x\n",
16197 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040016198 }
16199 return 0;
16200}
James Smart4f774512009-05-22 14:52:35 -040016201
16202/**
James Smart88a2cfb2011-07-22 18:36:33 -040016203 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040016204 * @phba: pointer to lpfc hba data structure.
16205 *
16206 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040016207 * HBA consistent with the SLI-4 interface spec. This routine
16208 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16209 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040016210 *
James Smart88a2cfb2011-07-22 18:36:33 -040016211 * Returns
16212 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16213 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16214 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016215static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016216lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16217{
16218 unsigned long xri;
16219
16220 /*
16221 * Fetch the next logical xri. Because this index is logical,
16222 * the driver starts at 0 each time.
16223 */
16224 spin_lock_irq(&phba->hbalock);
16225 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16226 phba->sli4_hba.max_cfg_param.max_xri, 0);
16227 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16228 spin_unlock_irq(&phba->hbalock);
16229 return NO_XRI;
16230 } else {
16231 set_bit(xri, phba->sli4_hba.xri_bmask);
16232 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040016233 }
James Smart6d368e52011-05-24 11:44:12 -040016234 spin_unlock_irq(&phba->hbalock);
16235 return xri;
16236}
16237
16238/**
16239 * lpfc_sli4_free_xri - Release an xri for reuse.
16240 * @phba: pointer to lpfc hba data structure.
16241 *
16242 * This routine is invoked to release an xri to the pool of
16243 * available rpis maintained by the driver.
16244 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016245static void
James Smart6d368e52011-05-24 11:44:12 -040016246__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16247{
16248 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016249 phba->sli4_hba.max_cfg_param.xri_used--;
16250 }
16251}
16252
16253/**
16254 * lpfc_sli4_free_xri - Release an xri for reuse.
16255 * @phba: pointer to lpfc hba data structure.
16256 *
16257 * This routine is invoked to release an xri to the pool of
16258 * available rpis maintained by the driver.
16259 **/
16260void
16261lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16262{
16263 spin_lock_irq(&phba->hbalock);
16264 __lpfc_sli4_free_xri(phba, xri);
16265 spin_unlock_irq(&phba->hbalock);
16266}
16267
16268/**
James Smart4f774512009-05-22 14:52:35 -040016269 * lpfc_sli4_next_xritag - Get an xritag for the io
16270 * @phba: Pointer to HBA context object.
16271 *
16272 * This function gets an xritag for the iocb. If there is no unused xritag
16273 * it will return 0xffff.
16274 * The function returns the allocated xritag if successful, else returns zero.
16275 * Zero is not a valid xritag.
16276 * The caller is not required to hold any lock.
16277 **/
16278uint16_t
16279lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16280{
James Smart6d368e52011-05-24 11:44:12 -040016281 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016282
James Smart6d368e52011-05-24 11:44:12 -040016283 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016284 if (xri_index == NO_XRI)
16285 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16286 "2004 Failed to allocate XRI.last XRITAG is %d"
16287 " Max XRI is %d, Used XRI is %d\n",
16288 xri_index,
16289 phba->sli4_hba.max_cfg_param.max_xri,
16290 phba->sli4_hba.max_cfg_param.xri_used);
16291 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016292}
16293
16294/**
James Smart895427b2017-02-12 13:52:30 -080016295 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016296 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016297 * @post_sgl_list: pointer to els sgl entry list.
16298 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016299 *
16300 * This routine is invoked to post a block of driver's sgl pages to the
16301 * HBA using non-embedded mailbox command. No Lock is held. This routine
16302 * is only called when the driver is loading and after all IO has been
16303 * stopped.
16304 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016305static int
James Smart895427b2017-02-12 13:52:30 -080016306lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016307 struct list_head *post_sgl_list,
16308 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016309{
James Smart8a9d2e82012-05-09 21:16:12 -040016310 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016311 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16312 struct sgl_page_pairs *sgl_pg_pairs;
16313 void *viraddr;
16314 LPFC_MBOXQ_t *mbox;
16315 uint32_t reqlen, alloclen, pg_pairs;
16316 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016317 uint16_t xritag_start = 0;
16318 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016319 uint32_t shdr_status, shdr_add_status;
16320 union lpfc_sli4_cfg_shdr *shdr;
16321
James Smart895427b2017-02-12 13:52:30 -080016322 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016323 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016324 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016325 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016326 "2559 Block sgl registration required DMA "
16327 "size (%d) great than a page\n", reqlen);
16328 return -ENOMEM;
16329 }
James Smart895427b2017-02-12 13:52:30 -080016330
James Smart4f774512009-05-22 14:52:35 -040016331 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016332 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016333 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016334
16335 /* Allocate DMA memory and set up the non-embedded mailbox command */
16336 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16337 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16338 LPFC_SLI4_MBX_NEMBED);
16339
16340 if (alloclen < reqlen) {
16341 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16342 "0285 Allocated DMA memory size (%d) is "
16343 "less than the requested DMA memory "
16344 "size (%d)\n", alloclen, reqlen);
16345 lpfc_sli4_mbox_cmd_free(phba, mbox);
16346 return -ENOMEM;
16347 }
James Smart4f774512009-05-22 14:52:35 -040016348 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016349 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016350 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16351 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16352
James Smart8a9d2e82012-05-09 21:16:12 -040016353 pg_pairs = 0;
16354 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016355 /* Set up the sge entry */
16356 sgl_pg_pairs->sgl_pg0_addr_lo =
16357 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16358 sgl_pg_pairs->sgl_pg0_addr_hi =
16359 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16360 sgl_pg_pairs->sgl_pg1_addr_lo =
16361 cpu_to_le32(putPaddrLow(0));
16362 sgl_pg_pairs->sgl_pg1_addr_hi =
16363 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016364
James Smart4f774512009-05-22 14:52:35 -040016365 /* Keep the first xritag on the list */
16366 if (pg_pairs == 0)
16367 xritag_start = sglq_entry->sli4_xritag;
16368 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016369 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016370 }
James Smart6d368e52011-05-24 11:44:12 -040016371
16372 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016373 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016374 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016375 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016376
James Smart4f774512009-05-22 14:52:35 -040016377 if (!phba->sli4_hba.intr_enable)
16378 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16379 else {
James Smarta183a152011-10-10 21:32:43 -040016380 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016381 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16382 }
16383 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16384 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16385 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16386 if (rc != MBX_TIMEOUT)
16387 lpfc_sli4_mbox_cmd_free(phba, mbox);
16388 if (shdr_status || shdr_add_status || rc) {
16389 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16390 "2513 POST_SGL_BLOCK mailbox command failed "
16391 "status x%x add_status x%x mbx status x%x\n",
16392 shdr_status, shdr_add_status, rc);
16393 rc = -ENXIO;
16394 }
16395 return rc;
16396}
16397
16398/**
16399 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16400 * @phba: pointer to lpfc hba data structure.
16401 * @sblist: pointer to scsi buffer list.
16402 * @count: number of scsi buffers on the list.
16403 *
16404 * This routine is invoked to post a block of @count scsi sgl pages from a
16405 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16406 * No Lock is held.
16407 *
16408 **/
16409int
James Smart8a9d2e82012-05-09 21:16:12 -040016410lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16411 struct list_head *sblist,
16412 int count)
James Smart4f774512009-05-22 14:52:35 -040016413{
16414 struct lpfc_scsi_buf *psb;
16415 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16416 struct sgl_page_pairs *sgl_pg_pairs;
16417 void *viraddr;
16418 LPFC_MBOXQ_t *mbox;
16419 uint32_t reqlen, alloclen, pg_pairs;
16420 uint32_t mbox_tmo;
16421 uint16_t xritag_start = 0;
16422 int rc = 0;
16423 uint32_t shdr_status, shdr_add_status;
16424 dma_addr_t pdma_phys_bpl1;
16425 union lpfc_sli4_cfg_shdr *shdr;
16426
16427 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016428 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016429 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016430 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016431 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16432 "0217 Block sgl registration required DMA "
16433 "size (%d) great than a page\n", reqlen);
16434 return -ENOMEM;
16435 }
16436 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16437 if (!mbox) {
16438 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16439 "0283 Failed to allocate mbox cmd memory\n");
16440 return -ENOMEM;
16441 }
16442
16443 /* Allocate DMA memory and set up the non-embedded mailbox command */
16444 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16445 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16446 LPFC_SLI4_MBX_NEMBED);
16447
16448 if (alloclen < reqlen) {
16449 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16450 "2561 Allocated DMA memory size (%d) is "
16451 "less than the requested DMA memory "
16452 "size (%d)\n", alloclen, reqlen);
16453 lpfc_sli4_mbox_cmd_free(phba, mbox);
16454 return -ENOMEM;
16455 }
James Smart6d368e52011-05-24 11:44:12 -040016456
James Smart4f774512009-05-22 14:52:35 -040016457 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016458 viraddr = mbox->sge_array->addr[0];
16459
16460 /* Set up the SGL pages in the non-embedded DMA pages */
16461 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16462 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16463
16464 pg_pairs = 0;
16465 list_for_each_entry(psb, sblist, list) {
16466 /* Set up the sge entry */
16467 sgl_pg_pairs->sgl_pg0_addr_lo =
16468 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16469 sgl_pg_pairs->sgl_pg0_addr_hi =
16470 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16471 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16472 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16473 else
16474 pdma_phys_bpl1 = 0;
16475 sgl_pg_pairs->sgl_pg1_addr_lo =
16476 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16477 sgl_pg_pairs->sgl_pg1_addr_hi =
16478 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16479 /* Keep the first xritag on the list */
16480 if (pg_pairs == 0)
16481 xritag_start = psb->cur_iocbq.sli4_xritag;
16482 sgl_pg_pairs++;
16483 pg_pairs++;
16484 }
16485 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16486 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16487 /* Perform endian conversion if necessary */
16488 sgl->word0 = cpu_to_le32(sgl->word0);
16489
16490 if (!phba->sli4_hba.intr_enable)
16491 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16492 else {
James Smarta183a152011-10-10 21:32:43 -040016493 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016494 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16495 }
16496 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16497 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16498 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16499 if (rc != MBX_TIMEOUT)
16500 lpfc_sli4_mbox_cmd_free(phba, mbox);
16501 if (shdr_status || shdr_add_status || rc) {
16502 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16503 "2564 POST_SGL_BLOCK mailbox command failed "
16504 "status x%x add_status x%x mbx status x%x\n",
16505 shdr_status, shdr_add_status, rc);
16506 rc = -ENXIO;
16507 }
16508 return rc;
16509}
16510
16511/**
16512 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16513 * @phba: pointer to lpfc_hba struct that the frame was received on
16514 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16515 *
16516 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16517 * valid type of frame that the LPFC driver will handle. This function will
16518 * return a zero if the frame is a valid frame or a non zero value when the
16519 * frame does not pass the check.
16520 **/
16521static int
16522lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16523{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016524 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016525 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016526 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016527
James Smartae9e28f2017-05-15 15:20:51 -070016528#define FC_RCTL_MDS_DIAGS 0xF4
16529
James Smart4f774512009-05-22 14:52:35 -040016530 switch (fc_hdr->fh_r_ctl) {
16531 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16532 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16533 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16534 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16535 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16536 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16537 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16538 case FC_RCTL_DD_CMD_STATUS: /* command status */
16539 case FC_RCTL_ELS_REQ: /* extended link services request */
16540 case FC_RCTL_ELS_REP: /* extended link services reply */
16541 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16542 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16543 case FC_RCTL_BA_NOP: /* basic link service NOP */
16544 case FC_RCTL_BA_ABTS: /* basic link service abort */
16545 case FC_RCTL_BA_RMC: /* remove connection */
16546 case FC_RCTL_BA_ACC: /* basic accept */
16547 case FC_RCTL_BA_RJT: /* basic reject */
16548 case FC_RCTL_BA_PRMT:
16549 case FC_RCTL_ACK_1: /* acknowledge_1 */
16550 case FC_RCTL_ACK_0: /* acknowledge_0 */
16551 case FC_RCTL_P_RJT: /* port reject */
16552 case FC_RCTL_F_RJT: /* fabric reject */
16553 case FC_RCTL_P_BSY: /* port busy */
16554 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16555 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16556 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016557 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016558 case FC_RCTL_END: /* end */
16559 break;
16560 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16561 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16562 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16563 return lpfc_fc_frame_check(phba, fc_hdr);
16564 default:
16565 goto drop;
16566 }
James Smartae9e28f2017-05-15 15:20:51 -070016567
16568#define FC_TYPE_VENDOR_UNIQUE 0xFF
16569
James Smart4f774512009-05-22 14:52:35 -040016570 switch (fc_hdr->fh_type) {
16571 case FC_TYPE_BLS:
16572 case FC_TYPE_ELS:
16573 case FC_TYPE_FCP:
16574 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016575 case FC_TYPE_NVME:
James Smartae9e28f2017-05-15 15:20:51 -070016576 case FC_TYPE_VENDOR_UNIQUE:
James Smart4f774512009-05-22 14:52:35 -040016577 break;
16578 case FC_TYPE_IP:
16579 case FC_TYPE_ILS:
16580 default:
16581 goto drop;
16582 }
James Smart546fc852011-03-11 16:06:29 -050016583
James Smart4f774512009-05-22 14:52:35 -040016584 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016585 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040016586 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070016587 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16588 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16589 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16590 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16591 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016592 return 0;
16593drop:
16594 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016595 "2539 Dropped frame rctl:x%x type:x%x\n",
16596 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040016597 return 1;
16598}
16599
16600/**
16601 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16602 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16603 *
16604 * This function processes the FC header to retrieve the VFI from the VF
16605 * header, if one exists. This function will return the VFI if one exists
16606 * or 0 if no VSAN Header exists.
16607 **/
16608static uint32_t
16609lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16610{
16611 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16612
16613 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16614 return 0;
16615 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16616}
16617
16618/**
16619 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16620 * @phba: Pointer to the HBA structure to search for the vport on
16621 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16622 * @fcfi: The FC Fabric ID that the frame came from
16623 *
16624 * This function searches the @phba for a vport that matches the content of the
16625 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16626 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16627 * returns the matching vport pointer or NULL if unable to match frame to a
16628 * vport.
16629 **/
16630static struct lpfc_vport *
16631lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016632 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016633{
16634 struct lpfc_vport **vports;
16635 struct lpfc_vport *vport = NULL;
16636 int i;
James Smart939723a2012-05-09 21:19:03 -040016637
James Smartbf086112011-08-21 21:48:13 -040016638 if (did == Fabric_DID)
16639 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016640 if ((phba->pport->fc_flag & FC_PT2PT) &&
16641 !(phba->link_state == LPFC_HBA_READY))
16642 return phba->pport;
16643
James Smart4f774512009-05-22 14:52:35 -040016644 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016645 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016646 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16647 if (phba->fcf.fcfi == fcfi &&
16648 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16649 vports[i]->fc_myDID == did) {
16650 vport = vports[i];
16651 break;
16652 }
16653 }
James Smart895427b2017-02-12 13:52:30 -080016654 }
James Smart4f774512009-05-22 14:52:35 -040016655 lpfc_destroy_vport_work_array(phba, vports);
16656 return vport;
16657}
16658
16659/**
James Smart45ed1192009-10-02 15:17:02 -040016660 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16661 * @vport: The vport to work on.
16662 *
16663 * This function updates the receive sequence time stamp for this vport. The
16664 * receive sequence time stamp indicates the time that the last frame of the
16665 * the sequence that has been idle for the longest amount of time was received.
16666 * the driver uses this time stamp to indicate if any received sequences have
16667 * timed out.
16668 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016669static void
James Smart45ed1192009-10-02 15:17:02 -040016670lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16671{
16672 struct lpfc_dmabuf *h_buf;
16673 struct hbq_dmabuf *dmabuf = NULL;
16674
16675 /* get the oldest sequence on the rcv list */
16676 h_buf = list_get_first(&vport->rcv_buffer_list,
16677 struct lpfc_dmabuf, list);
16678 if (!h_buf)
16679 return;
16680 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16681 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16682}
16683
16684/**
16685 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16686 * @vport: The vport that the received sequences were sent to.
16687 *
16688 * This function cleans up all outstanding received sequences. This is called
16689 * by the driver when a link event or user action invalidates all the received
16690 * sequences.
16691 **/
16692void
16693lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16694{
16695 struct lpfc_dmabuf *h_buf, *hnext;
16696 struct lpfc_dmabuf *d_buf, *dnext;
16697 struct hbq_dmabuf *dmabuf = NULL;
16698
16699 /* start with the oldest sequence on the rcv list */
16700 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16701 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16702 list_del_init(&dmabuf->hbuf.list);
16703 list_for_each_entry_safe(d_buf, dnext,
16704 &dmabuf->dbuf.list, list) {
16705 list_del_init(&d_buf->list);
16706 lpfc_in_buf_free(vport->phba, d_buf);
16707 }
16708 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16709 }
16710}
16711
16712/**
16713 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16714 * @vport: The vport that the received sequences were sent to.
16715 *
16716 * This function determines whether any received sequences have timed out by
16717 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16718 * indicates that there is at least one timed out sequence this routine will
16719 * go through the received sequences one at a time from most inactive to most
16720 * active to determine which ones need to be cleaned up. Once it has determined
16721 * that a sequence needs to be cleaned up it will simply free up the resources
16722 * without sending an abort.
16723 **/
16724void
16725lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16726{
16727 struct lpfc_dmabuf *h_buf, *hnext;
16728 struct lpfc_dmabuf *d_buf, *dnext;
16729 struct hbq_dmabuf *dmabuf = NULL;
16730 unsigned long timeout;
16731 int abort_count = 0;
16732
16733 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16734 vport->rcv_buffer_time_stamp);
16735 if (list_empty(&vport->rcv_buffer_list) ||
16736 time_before(jiffies, timeout))
16737 return;
16738 /* start with the oldest sequence on the rcv list */
16739 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16740 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16741 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16742 dmabuf->time_stamp);
16743 if (time_before(jiffies, timeout))
16744 break;
16745 abort_count++;
16746 list_del_init(&dmabuf->hbuf.list);
16747 list_for_each_entry_safe(d_buf, dnext,
16748 &dmabuf->dbuf.list, list) {
16749 list_del_init(&d_buf->list);
16750 lpfc_in_buf_free(vport->phba, d_buf);
16751 }
16752 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16753 }
16754 if (abort_count)
16755 lpfc_update_rcv_time_stamp(vport);
16756}
16757
16758/**
James Smart4f774512009-05-22 14:52:35 -040016759 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16760 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16761 *
16762 * This function searches through the existing incomplete sequences that have
16763 * been sent to this @vport. If the frame matches one of the incomplete
16764 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16765 * make up that sequence. If no sequence is found that matches this frame then
16766 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16767 * This function returns a pointer to the first dmabuf in the sequence list that
16768 * the frame was linked to.
16769 **/
16770static struct hbq_dmabuf *
16771lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16772{
16773 struct fc_frame_header *new_hdr;
16774 struct fc_frame_header *temp_hdr;
16775 struct lpfc_dmabuf *d_buf;
16776 struct lpfc_dmabuf *h_buf;
16777 struct hbq_dmabuf *seq_dmabuf = NULL;
16778 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050016779 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040016780
James Smart4d9ab992009-10-02 15:16:39 -040016781 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016782 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040016783 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050016784
James Smart4f774512009-05-22 14:52:35 -040016785 /* Use the hdr_buf to find the sequence that this frame belongs to */
16786 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16787 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16788 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16789 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16790 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16791 continue;
16792 /* found a pending sequence that matches this frame */
16793 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16794 break;
16795 }
16796 if (!seq_dmabuf) {
16797 /*
16798 * This indicates first frame received for this sequence.
16799 * Queue the buffer on the vport's rcv_buffer_list.
16800 */
16801 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040016802 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016803 return dmabuf;
16804 }
16805 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050016806 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16807 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040016808 list_del_init(&seq_dmabuf->hbuf.list);
16809 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16810 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016811 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016812 return dmabuf;
16813 }
James Smart45ed1192009-10-02 15:17:02 -040016814 /* move this sequence to the tail to indicate a young sequence */
16815 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16816 seq_dmabuf->time_stamp = jiffies;
16817 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050016818 if (list_empty(&seq_dmabuf->dbuf.list)) {
16819 temp_hdr = dmabuf->hbuf.virt;
16820 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16821 return seq_dmabuf;
16822 }
James Smart4f774512009-05-22 14:52:35 -040016823 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050016824 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16825 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040016826 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16827 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16828 /*
16829 * If the frame's sequence count is greater than the frame on
16830 * the list then insert the frame right after this frame
16831 */
James Smarteeead812009-12-21 17:01:23 -050016832 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16833 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040016834 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050016835 found = 1;
16836 break;
James Smart4f774512009-05-22 14:52:35 -040016837 }
James Smart4360ca92015-12-16 18:12:04 -050016838
16839 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16840 break;
16841 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040016842 }
James Smart4360ca92015-12-16 18:12:04 -050016843
16844 if (found)
16845 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016846 return NULL;
16847}
16848
16849/**
James Smart6669f9b2009-10-02 15:16:45 -040016850 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16851 * @vport: pointer to a vitural port
16852 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16853 *
16854 * This function tries to abort from the partially assembed sequence, described
16855 * by the information from basic abbort @dmabuf. It checks to see whether such
16856 * partially assembled sequence held by the driver. If so, it shall free up all
16857 * the frames from the partially assembled sequence.
16858 *
16859 * Return
16860 * true -- if there is matching partially assembled sequence present and all
16861 * the frames freed with the sequence;
16862 * false -- if there is no matching partially assembled sequence present so
16863 * nothing got aborted in the lower layer driver
16864 **/
16865static bool
16866lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16867 struct hbq_dmabuf *dmabuf)
16868{
16869 struct fc_frame_header *new_hdr;
16870 struct fc_frame_header *temp_hdr;
16871 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16872 struct hbq_dmabuf *seq_dmabuf = NULL;
16873
16874 /* Use the hdr_buf to find the sequence that matches this frame */
16875 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16876 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16877 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16878 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16879 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16880 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16881 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16882 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16883 continue;
16884 /* found a pending sequence that matches this frame */
16885 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16886 break;
16887 }
16888
16889 /* Free up all the frames from the partially assembled sequence */
16890 if (seq_dmabuf) {
16891 list_for_each_entry_safe(d_buf, n_buf,
16892 &seq_dmabuf->dbuf.list, list) {
16893 list_del_init(&d_buf->list);
16894 lpfc_in_buf_free(vport->phba, d_buf);
16895 }
16896 return true;
16897 }
16898 return false;
16899}
16900
16901/**
James Smart6dd9e312013-01-03 15:43:37 -050016902 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16903 * @vport: pointer to a vitural port
16904 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16905 *
16906 * This function tries to abort from the assembed sequence from upper level
16907 * protocol, described by the information from basic abbort @dmabuf. It
16908 * checks to see whether such pending context exists at upper level protocol.
16909 * If so, it shall clean up the pending context.
16910 *
16911 * Return
16912 * true -- if there is matching pending context of the sequence cleaned
16913 * at ulp;
16914 * false -- if there is no matching pending context of the sequence present
16915 * at ulp.
16916 **/
16917static bool
16918lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16919{
16920 struct lpfc_hba *phba = vport->phba;
16921 int handled;
16922
16923 /* Accepting abort at ulp with SLI4 only */
16924 if (phba->sli_rev < LPFC_SLI_REV4)
16925 return false;
16926
16927 /* Register all caring upper level protocols to attend abort */
16928 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16929 if (handled)
16930 return true;
16931
16932 return false;
16933}
16934
16935/**
James Smart546fc852011-03-11 16:06:29 -050016936 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040016937 * @phba: Pointer to HBA context object.
16938 * @cmd_iocbq: pointer to the command iocbq structure.
16939 * @rsp_iocbq: pointer to the response iocbq structure.
16940 *
James Smart546fc852011-03-11 16:06:29 -050016941 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040016942 * event. It properly releases the memory allocated to the sequence abort
16943 * accept iocb.
16944 **/
16945static void
James Smart546fc852011-03-11 16:06:29 -050016946lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040016947 struct lpfc_iocbq *cmd_iocbq,
16948 struct lpfc_iocbq *rsp_iocbq)
16949{
James Smart6dd9e312013-01-03 15:43:37 -050016950 struct lpfc_nodelist *ndlp;
16951
16952 if (cmd_iocbq) {
16953 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16954 lpfc_nlp_put(ndlp);
16955 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016956 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050016957 }
James Smart6b5151f2012-01-18 16:24:06 -050016958
16959 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16960 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16961 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16962 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16963 rsp_iocbq->iocb.ulpStatus,
16964 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040016965}
16966
16967/**
James Smart6d368e52011-05-24 11:44:12 -040016968 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16969 * @phba: Pointer to HBA context object.
16970 * @xri: xri id in transaction.
16971 *
16972 * This function validates the xri maps to the known range of XRIs allocated an
16973 * used by the driver.
16974 **/
James Smart7851fe22011-07-22 18:36:52 -040016975uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016976lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16977 uint16_t xri)
16978{
James Smarta2fc4aef2014-09-03 12:57:55 -040016979 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040016980
16981 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16982 if (xri == phba->sli4_hba.xri_ids[i])
16983 return i;
16984 }
16985 return NO_XRI;
16986}
16987
James Smart6d368e52011-05-24 11:44:12 -040016988/**
James Smart546fc852011-03-11 16:06:29 -050016989 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016990 * @phba: Pointer to HBA context object.
16991 * @fc_hdr: pointer to a FC frame header.
16992 *
James Smart546fc852011-03-11 16:06:29 -050016993 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016994 * event after aborting the sequence handling.
16995 **/
James Smart86c67372017-04-21 16:05:04 -070016996void
James Smart6dd9e312013-01-03 15:43:37 -050016997lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16998 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040016999{
James Smart6dd9e312013-01-03 15:43:37 -050017000 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040017001 struct lpfc_iocbq *ctiocb = NULL;
17002 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040017003 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050017004 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040017005 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050017006 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040017007
17008 if (!lpfc_is_link_up(phba))
17009 return;
17010
17011 sid = sli4_sid_from_fc_hdr(fc_hdr);
17012 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050017013 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040017014
James Smart6dd9e312013-01-03 15:43:37 -050017015 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040017016 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070017017 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050017018 if (!ndlp) {
17019 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17020 "1268 Failed to allocate ndlp for "
17021 "oxid:x%x SID:x%x\n", oxid, sid);
17022 return;
17023 }
James Smart6dd9e312013-01-03 15:43:37 -050017024 /* Put ndlp onto pport node list */
17025 lpfc_enqueue_node(vport, ndlp);
17026 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17027 /* re-setup ndlp without removing from node list */
17028 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17029 if (!ndlp) {
17030 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17031 "3275 Failed to active ndlp found "
17032 "for oxid:x%x SID:x%x\n", oxid, sid);
17033 return;
17034 }
James Smart6669f9b2009-10-02 15:16:45 -040017035 }
17036
James Smart546fc852011-03-11 16:06:29 -050017037 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040017038 ctiocb = lpfc_sli_get_iocbq(phba);
17039 if (!ctiocb)
17040 return;
17041
James Smart5ffc2662009-11-18 15:39:44 -050017042 /* Extract the F_CTL field from FC_HDR */
17043 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17044
James Smart6669f9b2009-10-02 15:16:45 -040017045 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040017046 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050017047 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040017048 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17049 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17050 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17051
17052 /* Fill in the rest of iocb fields */
17053 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17054 icmd->ulpBdeCount = 0;
17055 icmd->ulpLe = 1;
17056 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040017057 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050017058 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017059
James Smart6669f9b2009-10-02 15:16:45 -040017060 ctiocb->iocb_cmpl = NULL;
17061 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050017062 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040017063 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050017064 ctiocb->sli4_xritag = NO_XRI;
17065
James Smartee0f4fe2012-05-09 21:19:14 -040017066 if (fctl & FC_FC_EX_CTX)
17067 /* Exchange responder sent the abort so we
17068 * own the oxid.
17069 */
17070 xri = oxid;
17071 else
17072 xri = rxid;
17073 lxri = lpfc_sli4_xri_inrange(phba, xri);
17074 if (lxri != NO_XRI)
17075 lpfc_set_rrq_active(phba, ndlp, lxri,
17076 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050017077 /* For BA_ABTS from exchange responder, if the logical xri with
17078 * the oxid maps to the FCP XRI range, the port no longer has
17079 * that exchange context, send a BLS_RJT. Override the IOCB for
17080 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050017081 */
James Smart6dd9e312013-01-03 15:43:37 -050017082 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080017083 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050017084 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17085 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17086 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17087 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17088 }
17089
17090 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17091 * the driver no longer has that exchange, send a BLS_RJT. Override
17092 * the IOCB for a BA_RJT.
17093 */
17094 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050017095 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17096 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17097 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17098 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17099 }
James Smart6669f9b2009-10-02 15:16:45 -040017100
James Smart5ffc2662009-11-18 15:39:44 -050017101 if (fctl & FC_FC_EX_CTX) {
17102 /* ABTS sent by responder to CT exchange, construction
17103 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17104 * field and RX_ID from ABTS for RX_ID field.
17105 */
James Smart546fc852011-03-11 16:06:29 -050017106 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050017107 } else {
17108 /* ABTS sent by initiator to CT exchange, construction
17109 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050017110 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050017111 */
James Smart546fc852011-03-11 16:06:29 -050017112 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050017113 }
James Smartf09c3ac2012-03-01 22:33:29 -050017114 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050017115 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050017116
James Smart546fc852011-03-11 16:06:29 -050017117 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050017118 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17119 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17120 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050017121
17122 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17123 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050017124 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17125 "2925 Failed to issue CT ABTS RSP x%x on "
17126 "xri x%x, Data x%x\n",
17127 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17128 phba->link_state);
17129 lpfc_nlp_put(ndlp);
17130 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050017131 lpfc_sli_release_iocbq(phba, ctiocb);
17132 }
James Smart6669f9b2009-10-02 15:16:45 -040017133}
17134
17135/**
17136 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17137 * @vport: Pointer to the vport on which this sequence was received
17138 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17139 *
17140 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17141 * receive sequence is only partially assembed by the driver, it shall abort
17142 * the partially assembled frames for the sequence. Otherwise, if the
17143 * unsolicited receive sequence has been completely assembled and passed to
17144 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17145 * unsolicited sequence has been aborted. After that, it will issue a basic
17146 * accept to accept the abort.
17147 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017148static void
James Smart6669f9b2009-10-02 15:16:45 -040017149lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17150 struct hbq_dmabuf *dmabuf)
17151{
17152 struct lpfc_hba *phba = vport->phba;
17153 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050017154 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050017155 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040017156
James Smart6669f9b2009-10-02 15:16:45 -040017157 /* Make a copy of fc_hdr before the dmabuf being released */
17158 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050017159 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040017160
James Smart5ffc2662009-11-18 15:39:44 -050017161 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050017162 /* ABTS by responder to exchange, no cleanup needed */
17163 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050017164 } else {
James Smart6dd9e312013-01-03 15:43:37 -050017165 /* ABTS by initiator to exchange, need to do cleanup */
17166 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17167 if (aborted == false)
17168 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050017169 }
James Smart6dd9e312013-01-03 15:43:37 -050017170 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17171
James Smart86c67372017-04-21 16:05:04 -070017172 if (phba->nvmet_support) {
17173 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17174 return;
17175 }
17176
James Smart6dd9e312013-01-03 15:43:37 -050017177 /* Respond with BA_ACC or BA_RJT accordingly */
17178 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040017179}
17180
17181/**
James Smart4f774512009-05-22 14:52:35 -040017182 * lpfc_seq_complete - Indicates if a sequence is complete
17183 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17184 *
17185 * This function checks the sequence, starting with the frame described by
17186 * @dmabuf, to see if all the frames associated with this sequence are present.
17187 * the frames associated with this sequence are linked to the @dmabuf using the
17188 * dbuf list. This function looks for two major things. 1) That the first frame
17189 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17190 * set. 3) That there are no holes in the sequence count. The function will
17191 * return 1 when the sequence is complete, otherwise it will return 0.
17192 **/
17193static int
17194lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17195{
17196 struct fc_frame_header *hdr;
17197 struct lpfc_dmabuf *d_buf;
17198 struct hbq_dmabuf *seq_dmabuf;
17199 uint32_t fctl;
17200 int seq_count = 0;
17201
17202 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17203 /* make sure first fame of sequence has a sequence count of zero */
17204 if (hdr->fh_seq_cnt != seq_count)
17205 return 0;
17206 fctl = (hdr->fh_f_ctl[0] << 16 |
17207 hdr->fh_f_ctl[1] << 8 |
17208 hdr->fh_f_ctl[2]);
17209 /* If last frame of sequence we can return success. */
17210 if (fctl & FC_FC_END_SEQ)
17211 return 1;
17212 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17213 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17214 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17215 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050017216 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040017217 return 0;
17218 fctl = (hdr->fh_f_ctl[0] << 16 |
17219 hdr->fh_f_ctl[1] << 8 |
17220 hdr->fh_f_ctl[2]);
17221 /* If last frame of sequence we can return success. */
17222 if (fctl & FC_FC_END_SEQ)
17223 return 1;
17224 }
17225 return 0;
17226}
17227
17228/**
17229 * lpfc_prep_seq - Prep sequence for ULP processing
17230 * @vport: Pointer to the vport on which this sequence was received
17231 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17232 *
17233 * This function takes a sequence, described by a list of frames, and creates
17234 * a list of iocbq structures to describe the sequence. This iocbq list will be
17235 * used to issue to the generic unsolicited sequence handler. This routine
17236 * returns a pointer to the first iocbq in the list. If the function is unable
17237 * to allocate an iocbq then it throw out the received frames that were not
17238 * able to be described and return a pointer to the first iocbq. If unable to
17239 * allocate any iocbqs (including the first) this function will return NULL.
17240 **/
17241static struct lpfc_iocbq *
17242lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17243{
James Smart7851fe22011-07-22 18:36:52 -040017244 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040017245 struct lpfc_dmabuf *d_buf, *n_buf;
17246 struct lpfc_iocbq *first_iocbq, *iocbq;
17247 struct fc_frame_header *fc_hdr;
17248 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040017249 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050017250 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040017251
17252 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17253 /* remove from receive buffer list */
17254 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017255 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017256 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040017257 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040017258 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017259 /* Get an iocbq struct to fill in. */
17260 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17261 if (first_iocbq) {
17262 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040017263 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017264 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080017265 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040017266
17267 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17268 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17269 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17270 first_iocbq->iocb.un.rcvels.parmRo =
17271 sli4_did_from_fc_hdr(fc_hdr);
17272 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17273 } else
17274 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040017275 first_iocbq->iocb.ulpContext = NO_XRI;
17276 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17277 be16_to_cpu(fc_hdr->fh_ox_id);
17278 /* iocbq is prepped for internal consumption. Physical vpi. */
17279 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17280 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040017281 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040017282 tot_len = bf_get(lpfc_rcqe_length,
17283 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17284
James Smart4f774512009-05-22 14:52:35 -040017285 first_iocbq->context2 = &seq_dmabuf->dbuf;
17286 first_iocbq->context3 = NULL;
17287 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040017288 if (tot_len > LPFC_DATA_BUF_SIZE)
17289 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040017290 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040017291 else
17292 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17293
James Smart4f774512009-05-22 14:52:35 -040017294 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040017295
James Smart7851fe22011-07-22 18:36:52 -040017296 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040017297 }
17298 iocbq = first_iocbq;
17299 /*
17300 * Each IOCBq can have two Buffers assigned, so go through the list
17301 * of buffers for this sequence and save two buffers in each IOCBq
17302 */
17303 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17304 if (!iocbq) {
17305 lpfc_in_buf_free(vport->phba, d_buf);
17306 continue;
17307 }
17308 if (!iocbq->context3) {
17309 iocbq->context3 = d_buf;
17310 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040017311 /* We need to get the size out of the right CQE */
17312 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17313 len = bf_get(lpfc_rcqe_length,
17314 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017315 pbde = (struct ulp_bde64 *)
17316 &iocbq->iocb.unsli3.sli3Words[4];
17317 if (len > LPFC_DATA_BUF_SIZE)
17318 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17319 else
17320 pbde->tus.f.bdeSize = len;
17321
James Smart7851fe22011-07-22 18:36:52 -040017322 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17323 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040017324 } else {
17325 iocbq = lpfc_sli_get_iocbq(vport->phba);
17326 if (!iocbq) {
17327 if (first_iocbq) {
17328 first_iocbq->iocb.ulpStatus =
17329 IOSTAT_FCP_RSP_ERROR;
17330 first_iocbq->iocb.un.ulpWord[4] =
17331 IOERR_NO_RESOURCES;
17332 }
17333 lpfc_in_buf_free(vport->phba, d_buf);
17334 continue;
17335 }
James Smart7851fe22011-07-22 18:36:52 -040017336 /* We need to get the size out of the right CQE */
17337 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17338 len = bf_get(lpfc_rcqe_length,
17339 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017340 iocbq->context2 = d_buf;
17341 iocbq->context3 = NULL;
17342 iocbq->iocb.ulpBdeCount = 1;
17343 if (len > LPFC_DATA_BUF_SIZE)
17344 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17345 LPFC_DATA_BUF_SIZE;
17346 else
17347 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17348
James Smart7851fe22011-07-22 18:36:52 -040017349 tot_len += len;
17350 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17351
James Smart4f774512009-05-22 14:52:35 -040017352 iocbq->iocb.un.rcvels.remoteID = sid;
17353 list_add_tail(&iocbq->list, &first_iocbq->list);
17354 }
17355 }
17356 return first_iocbq;
17357}
17358
James Smart6669f9b2009-10-02 15:16:45 -040017359static void
17360lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17361 struct hbq_dmabuf *seq_dmabuf)
17362{
17363 struct fc_frame_header *fc_hdr;
17364 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17365 struct lpfc_hba *phba = vport->phba;
17366
17367 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17368 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17369 if (!iocbq) {
17370 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17371 "2707 Ring %d handler: Failed to allocate "
17372 "iocb Rctl x%x Type x%x received\n",
17373 LPFC_ELS_RING,
17374 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17375 return;
17376 }
17377 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017378 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017379 iocbq, fc_hdr->fh_r_ctl,
17380 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017381 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017382 "2540 Ring %d handler: unexpected Rctl "
17383 "x%x Type x%x received\n",
17384 LPFC_ELS_RING,
17385 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17386
17387 /* Free iocb created in lpfc_prep_seq */
17388 list_for_each_entry_safe(curr_iocb, next_iocb,
17389 &iocbq->list, list) {
17390 list_del_init(&curr_iocb->list);
17391 lpfc_sli_release_iocbq(phba, curr_iocb);
17392 }
17393 lpfc_sli_release_iocbq(phba, iocbq);
17394}
17395
James Smartae9e28f2017-05-15 15:20:51 -070017396static void
17397lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17398 struct lpfc_iocbq *rspiocb)
17399{
17400 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17401
17402 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017403 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017404 kfree(pcmd);
17405 lpfc_sli_release_iocbq(phba, cmdiocb);
17406}
17407
17408static void
17409lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17410 struct hbq_dmabuf *dmabuf)
17411{
17412 struct fc_frame_header *fc_hdr;
17413 struct lpfc_hba *phba = vport->phba;
17414 struct lpfc_iocbq *iocbq = NULL;
17415 union lpfc_wqe *wqe;
17416 struct lpfc_dmabuf *pcmd = NULL;
17417 uint32_t frame_len;
17418 int rc;
17419
17420 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17421 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17422
17423 /* Send the received frame back */
17424 iocbq = lpfc_sli_get_iocbq(phba);
17425 if (!iocbq)
17426 goto exit;
17427
17428 /* Allocate buffer for command payload */
17429 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17430 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020017431 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070017432 &pcmd->phys);
17433 if (!pcmd || !pcmd->virt)
17434 goto exit;
17435
17436 INIT_LIST_HEAD(&pcmd->list);
17437
17438 /* copyin the payload */
17439 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17440
17441 /* fill in BDE's for command */
17442 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17443 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17444 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17445 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17446
17447 iocbq->context2 = pcmd;
17448 iocbq->vport = vport;
17449 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17450 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17451
17452 /*
17453 * Setup rest of the iocb as though it were a WQE
17454 * Build the SEND_FRAME WQE
17455 */
17456 wqe = (union lpfc_wqe *)&iocbq->iocb;
17457
17458 wqe->send_frame.frame_len = frame_len;
17459 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17460 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17461 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17462 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17463 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17464 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17465
17466 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17467 iocbq->iocb.ulpLe = 1;
17468 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17469 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17470 if (rc == IOCB_ERROR)
17471 goto exit;
17472
17473 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17474 return;
17475
17476exit:
17477 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17478 "2023 Unable to process MDS loopback frame\n");
17479 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017480 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017481 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070017482 if (iocbq)
17483 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070017484 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17485}
17486
James Smart4f774512009-05-22 14:52:35 -040017487/**
17488 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17489 * @phba: Pointer to HBA context object.
17490 *
17491 * This function is called with no lock held. This function processes all
17492 * the received buffers and gives it to upper layers when a received buffer
17493 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017494 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017495 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17496 * appropriate receive function when the final frame in a sequence is received.
17497 **/
James Smart4d9ab992009-10-02 15:16:39 -040017498void
17499lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17500 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017501{
James Smart4d9ab992009-10-02 15:16:39 -040017502 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017503 struct fc_frame_header *fc_hdr;
17504 struct lpfc_vport *vport;
17505 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017506 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017507
James Smart4f774512009-05-22 14:52:35 -040017508 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017509 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017510
James Smart4d9ab992009-10-02 15:16:39 -040017511 /* check to see if this a valid type of frame */
17512 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17513 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17514 return;
17515 }
James Smart2ea259e2017-02-12 13:52:27 -080017516
James Smart7851fe22011-07-22 18:36:52 -040017517 if ((bf_get(lpfc_cqe_code,
17518 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17519 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17520 &dmabuf->cq_event.cqe.rcqe_cmpl);
17521 else
17522 fcfi = bf_get(lpfc_rcqe_fcf_id,
17523 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017524
James Smartae9e28f2017-05-15 15:20:51 -070017525 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17526 vport = phba->pport;
17527 /* Handle MDS Loopback frames */
17528 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17529 return;
17530 }
17531
James Smart895427b2017-02-12 13:52:30 -080017532 /* d_id this frame is directed to */
17533 did = sli4_did_from_fc_hdr(fc_hdr);
17534
17535 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017536 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017537 /* throw out the frame */
17538 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17539 return;
17540 }
James Smart939723a2012-05-09 21:19:03 -040017541
James Smart939723a2012-05-09 21:19:03 -040017542 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17543 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17544 (did != Fabric_DID)) {
17545 /*
17546 * Throw out the frame if we are not pt2pt.
17547 * The pt2pt protocol allows for discovery frames
17548 * to be received without a registered VPI.
17549 */
17550 if (!(vport->fc_flag & FC_PT2PT) ||
17551 (phba->link_state == LPFC_HBA_READY)) {
17552 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17553 return;
17554 }
17555 }
17556
James Smart6669f9b2009-10-02 15:16:45 -040017557 /* Handle the basic abort sequence (BA_ABTS) event */
17558 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17559 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17560 return;
17561 }
17562
James Smart4d9ab992009-10-02 15:16:39 -040017563 /* Link this frame */
17564 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17565 if (!seq_dmabuf) {
17566 /* unable to add frame to vport - throw it out */
17567 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17568 return;
17569 }
17570 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017571 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017572 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017573
James Smart6669f9b2009-10-02 15:16:45 -040017574 /* Send the complete sequence to the upper layer protocol */
17575 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017576}
James Smart6fb120a2009-05-22 14:52:59 -040017577
17578/**
17579 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17580 * @phba: pointer to lpfc hba data structure.
17581 *
17582 * This routine is invoked to post rpi header templates to the
17583 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017584 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17585 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017586 *
17587 * This routine does not require any locks. It's usage is expected
17588 * to be driver load or reset recovery when the driver is
17589 * sequential.
17590 *
17591 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017592 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017593 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017594 * When this error occurs, the driver is not guaranteed
17595 * to have any rpi regions posted to the device and
17596 * must either attempt to repost the regions or take a
17597 * fatal error.
17598 **/
17599int
17600lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17601{
17602 struct lpfc_rpi_hdr *rpi_page;
17603 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017604 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017605
James Smart6d368e52011-05-24 11:44:12 -040017606 /* SLI4 ports that support extents do not require RPI headers. */
17607 if (!phba->sli4_hba.rpi_hdrs_in_use)
17608 goto exit;
17609 if (phba->sli4_hba.extents_in_use)
17610 return -EIO;
17611
James Smart6fb120a2009-05-22 14:52:59 -040017612 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017613 /*
17614 * Assign the rpi headers a physical rpi only if the driver
17615 * has not initialized those resources. A port reset only
17616 * needs the headers posted.
17617 */
17618 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17619 LPFC_RPI_RSRC_RDY)
17620 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17621
James Smart6fb120a2009-05-22 14:52:59 -040017622 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17623 if (rc != MBX_SUCCESS) {
17624 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17625 "2008 Error %d posting all rpi "
17626 "headers\n", rc);
17627 rc = -EIO;
17628 break;
17629 }
17630 }
17631
James Smart6d368e52011-05-24 11:44:12 -040017632 exit:
17633 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17634 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017635 return rc;
17636}
17637
17638/**
17639 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17640 * @phba: pointer to lpfc hba data structure.
17641 * @rpi_page: pointer to the rpi memory region.
17642 *
17643 * This routine is invoked to post a single rpi header to the
17644 * HBA consistent with the SLI-4 interface spec. This memory region
17645 * maps up to 64 rpi context regions.
17646 *
17647 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017648 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017649 * -ENOMEM - No available memory
17650 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017651 **/
17652int
17653lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17654{
17655 LPFC_MBOXQ_t *mboxq;
17656 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17657 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017658 uint32_t shdr_status, shdr_add_status;
17659 union lpfc_sli4_cfg_shdr *shdr;
17660
James Smart6d368e52011-05-24 11:44:12 -040017661 /* SLI4 ports that support extents do not require RPI headers. */
17662 if (!phba->sli4_hba.rpi_hdrs_in_use)
17663 return rc;
17664 if (phba->sli4_hba.extents_in_use)
17665 return -EIO;
17666
James Smart6fb120a2009-05-22 14:52:59 -040017667 /* The port is notified of the header region via a mailbox command. */
17668 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17669 if (!mboxq) {
17670 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17671 "2001 Unable to allocate memory for issuing "
17672 "SLI_CONFIG_SPECIAL mailbox command\n");
17673 return -ENOMEM;
17674 }
17675
17676 /* Post all rpi memory regions to the port. */
17677 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040017678 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17679 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17680 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050017681 sizeof(struct lpfc_sli4_cfg_mhdr),
17682 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040017683
17684
17685 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040017686 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17687 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040017688 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17689 hdr_tmpl, rpi_page->page_count);
17690
James Smart6fb120a2009-05-22 14:52:59 -040017691 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17692 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040017693 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040017694 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17695 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17696 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17697 if (rc != MBX_TIMEOUT)
17698 mempool_free(mboxq, phba->mbox_mem_pool);
17699 if (shdr_status || shdr_add_status || rc) {
17700 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17701 "2514 POST_RPI_HDR mailbox failed with "
17702 "status x%x add_status x%x, mbx status x%x\n",
17703 shdr_status, shdr_add_status, rc);
17704 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070017705 } else {
17706 /*
17707 * The next_rpi stores the next logical module-64 rpi value used
17708 * to post physical rpis in subsequent rpi postings.
17709 */
17710 spin_lock_irq(&phba->hbalock);
17711 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17712 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040017713 }
17714 return rc;
17715}
17716
17717/**
17718 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17719 * @phba: pointer to lpfc hba data structure.
17720 *
17721 * This routine is invoked to post rpi header templates to the
17722 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017723 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17724 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017725 *
17726 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017727 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040017728 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17729 **/
17730int
17731lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17732{
James Smart6d368e52011-05-24 11:44:12 -040017733 unsigned long rpi;
17734 uint16_t max_rpi, rpi_limit;
17735 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017736 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040017737 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040017738
James Smart6fb120a2009-05-22 14:52:59 -040017739 /*
James Smart6d368e52011-05-24 11:44:12 -040017740 * Fetch the next logical rpi. Because this index is logical,
17741 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040017742 */
James Smart4902b382013-10-10 12:20:35 -040017743 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040017744 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17745 rpi_limit = phba->sli4_hba.next_rpi;
17746
James Smart6d368e52011-05-24 11:44:12 -040017747 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17748 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040017749 rpi = LPFC_RPI_ALLOC_ERROR;
17750 else {
17751 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17752 phba->sli4_hba.max_cfg_param.rpi_used++;
17753 phba->sli4_hba.rpi_count++;
17754 }
James Smartbe6bb942015-04-07 15:07:22 -040017755 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17756 "0001 rpi:%x max:%x lim:%x\n",
17757 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040017758
17759 /*
17760 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040017761 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040017762 */
17763 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17764 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040017765 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017766 return rpi;
17767 }
17768
17769 /*
James Smart6d368e52011-05-24 11:44:12 -040017770 * RPI header postings are not required for SLI4 ports capable of
17771 * extents.
17772 */
17773 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040017774 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040017775 return rpi;
17776 }
17777
17778 /*
James Smart6fb120a2009-05-22 14:52:59 -040017779 * If the driver is running low on rpi resources, allocate another
17780 * page now. Note that the next_rpi value is used because
17781 * it represents how many are actually in use whereas max_rpi notes
17782 * how many are supported max by the device.
17783 */
James Smart6d368e52011-05-24 11:44:12 -040017784 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040017785 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017786 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17787 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17788 if (!rpi_hdr) {
17789 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17790 "2002 Error Could not grow rpi "
17791 "count\n");
17792 } else {
James Smart6d368e52011-05-24 11:44:12 -040017793 lrpi = rpi_hdr->start_rpi;
17794 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040017795 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17796 }
17797 }
17798
17799 return rpi;
17800}
17801
17802/**
17803 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17804 * @phba: pointer to lpfc hba data structure.
17805 *
17806 * This routine is invoked to release an rpi to the pool of
17807 * available rpis maintained by the driver.
17808 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017809static void
James Smartd7c47992010-06-08 18:31:54 -040017810__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17811{
17812 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17813 phba->sli4_hba.rpi_count--;
17814 phba->sli4_hba.max_cfg_param.rpi_used--;
17815 }
17816}
17817
17818/**
17819 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17820 * @phba: pointer to lpfc hba data structure.
17821 *
17822 * This routine is invoked to release an rpi to the pool of
17823 * available rpis maintained by the driver.
17824 **/
17825void
James Smart6fb120a2009-05-22 14:52:59 -040017826lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17827{
17828 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040017829 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040017830 spin_unlock_irq(&phba->hbalock);
17831}
17832
17833/**
17834 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17835 * @phba: pointer to lpfc hba data structure.
17836 *
17837 * This routine is invoked to remove the memory region that
17838 * provided rpi via a bitmask.
17839 **/
17840void
17841lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17842{
17843 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040017844 kfree(phba->sli4_hba.rpi_ids);
17845 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040017846}
17847
17848/**
17849 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17850 * @phba: pointer to lpfc hba data structure.
17851 *
17852 * This routine is invoked to remove the memory region that
17853 * provided rpi via a bitmask.
17854 **/
17855int
James Smart6b5151f2012-01-18 16:24:06 -050017856lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17857 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040017858{
17859 LPFC_MBOXQ_t *mboxq;
17860 struct lpfc_hba *phba = ndlp->phba;
17861 int rc;
17862
17863 /* The port is notified of the header region via a mailbox command. */
17864 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17865 if (!mboxq)
17866 return -ENOMEM;
17867
17868 /* Post all rpi memory regions to the port. */
17869 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050017870 if (cmpl) {
17871 mboxq->mbox_cmpl = cmpl;
17872 mboxq->context1 = arg;
17873 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050017874 } else
17875 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050017876 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040017877 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17878 if (rc == MBX_NOT_FINISHED) {
17879 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17880 "2010 Resume RPI Mailbox failed "
17881 "status %d, mbxStatus x%x\n", rc,
17882 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17883 mempool_free(mboxq, phba->mbox_mem_pool);
17884 return -EIO;
17885 }
17886 return 0;
17887}
17888
17889/**
17890 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050017891 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040017892 *
James Smart76a95d72010-11-20 23:11:48 -050017893 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040017894 *
17895 * Returns:
17896 * 0 success
17897 * -Evalue otherwise
17898 **/
17899int
James Smart76a95d72010-11-20 23:11:48 -050017900lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040017901{
17902 LPFC_MBOXQ_t *mboxq;
17903 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040017904 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040017905 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050017906 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040017907 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17908 if (!mboxq)
17909 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050017910 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040017911 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040017912 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040017913 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050017914 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040017915 "2022 INIT VPI Mailbox failed "
17916 "status %d, mbxStatus x%x\n", rc,
17917 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040017918 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040017919 }
James Smart6a9c52c2009-10-02 15:16:51 -040017920 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050017921 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040017922
17923 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040017924}
17925
17926/**
17927 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17928 * @phba: pointer to lpfc hba data structure.
17929 * @mboxq: Pointer to mailbox object.
17930 *
17931 * This routine is invoked to manually add a single FCF record. The caller
17932 * must pass a completely initialized FCF_Record. This routine takes
17933 * care of the nonembedded mailbox operations.
17934 **/
17935static void
17936lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17937{
17938 void *virt_addr;
17939 union lpfc_sli4_cfg_shdr *shdr;
17940 uint32_t shdr_status, shdr_add_status;
17941
17942 virt_addr = mboxq->sge_array->addr[0];
17943 /* The IOCTL status is embedded in the mailbox subheader. */
17944 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17945 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17946 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17947
17948 if ((shdr_status || shdr_add_status) &&
17949 (shdr_status != STATUS_FCF_IN_USE))
17950 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17951 "2558 ADD_FCF_RECORD mailbox failed with "
17952 "status x%x add_status x%x\n",
17953 shdr_status, shdr_add_status);
17954
17955 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17956}
17957
17958/**
17959 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17960 * @phba: pointer to lpfc hba data structure.
17961 * @fcf_record: pointer to the initialized fcf record to add.
17962 *
17963 * This routine is invoked to manually add a single FCF record. The caller
17964 * must pass a completely initialized FCF_Record. This routine takes
17965 * care of the nonembedded mailbox operations.
17966 **/
17967int
17968lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17969{
17970 int rc = 0;
17971 LPFC_MBOXQ_t *mboxq;
17972 uint8_t *bytep;
17973 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040017974 struct lpfc_mbx_sge sge;
17975 uint32_t alloc_len, req_len;
17976 uint32_t fcfindex;
17977
17978 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17979 if (!mboxq) {
17980 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17981 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17982 return -ENOMEM;
17983 }
17984
17985 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17986 sizeof(uint32_t);
17987
17988 /* Allocate DMA memory and set up the non-embedded mailbox command */
17989 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17990 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17991 req_len, LPFC_SLI4_MBX_NEMBED);
17992 if (alloc_len < req_len) {
17993 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17994 "2523 Allocated DMA memory size (x%x) is "
17995 "less than the requested DMA memory "
17996 "size (x%x)\n", alloc_len, req_len);
17997 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17998 return -ENOMEM;
17999 }
18000
18001 /*
18002 * Get the first SGE entry from the non-embedded DMA memory. This
18003 * routine only uses a single SGE.
18004 */
18005 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040018006 virt_addr = mboxq->sge_array->addr[0];
18007 /*
18008 * Configure the FCF record for FCFI 0. This is the driver's
18009 * hardcoded default and gets used in nonFIP mode.
18010 */
18011 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18012 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18013 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18014
18015 /*
18016 * Copy the fcf_index and the FCF Record Data. The data starts after
18017 * the FCoE header plus word10. The data copy needs to be endian
18018 * correct.
18019 */
18020 bytep += sizeof(uint32_t);
18021 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18022 mboxq->vport = phba->pport;
18023 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18024 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18025 if (rc == MBX_NOT_FINISHED) {
18026 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18027 "2515 ADD_FCF_RECORD mailbox failed with "
18028 "status 0x%x\n", rc);
18029 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18030 rc = -EIO;
18031 } else
18032 rc = 0;
18033
18034 return rc;
18035}
18036
18037/**
18038 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18039 * @phba: pointer to lpfc hba data structure.
18040 * @fcf_record: pointer to the fcf record to write the default data.
18041 * @fcf_index: FCF table entry index.
18042 *
18043 * This routine is invoked to build the driver's default FCF record. The
18044 * values used are hardcoded. This routine handles memory initialization.
18045 *
18046 **/
18047void
18048lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18049 struct fcf_record *fcf_record,
18050 uint16_t fcf_index)
18051{
18052 memset(fcf_record, 0, sizeof(struct fcf_record));
18053 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18054 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18055 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18056 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18057 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18058 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18059 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18060 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18061 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18062 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18063 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18064 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18065 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040018066 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040018067 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18068 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18069 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18070 /* Set the VLAN bit map */
18071 if (phba->valid_vlan) {
18072 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18073 = 1 << (phba->vlan_id % 8);
18074 }
18075}
18076
18077/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018078 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040018079 * @phba: pointer to lpfc hba data structure.
18080 * @fcf_index: FCF table entry offset.
18081 *
James Smart0c9ab6f2010-02-26 14:15:57 -050018082 * This routine is invoked to scan the entire FCF table by reading FCF
18083 * record and processing it one at a time starting from the @fcf_index
18084 * for initial FCF discovery or fast FCF failover rediscovery.
18085 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018086 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018087 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040018088 **/
18089int
James Smart0c9ab6f2010-02-26 14:15:57 -050018090lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040018091{
18092 int rc = 0, error;
18093 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040018094
James Smart32b97932009-07-19 10:01:21 -040018095 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050018096 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040018097 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18098 if (!mboxq) {
18099 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18100 "2000 Failed to allocate mbox for "
18101 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040018102 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050018103 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018104 }
James Smartecfd03c2010-02-12 14:41:27 -050018105 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050018106 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050018107 if (rc) {
18108 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050018109 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018110 }
James Smartecfd03c2010-02-12 14:41:27 -050018111 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040018112 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050018113 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040018114
18115 spin_lock_irq(&phba->hbalock);
18116 phba->hba_flag |= FCF_TS_INPROG;
18117 spin_unlock_irq(&phba->hbalock);
18118
James Smart6fb120a2009-05-22 14:52:59 -040018119 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050018120 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040018121 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050018122 else {
James Smart38b92ef2010-08-04 16:11:39 -040018123 /* Reset eligible FCF count for new scan */
18124 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040018125 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018126 error = 0;
James Smart32b97932009-07-19 10:01:21 -040018127 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018128fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040018129 if (error) {
18130 if (mboxq)
18131 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040018132 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040018133 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040018134 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040018135 spin_unlock_irq(&phba->hbalock);
18136 }
James Smart6fb120a2009-05-22 14:52:59 -040018137 return error;
18138}
James Smarta0c87cb2009-07-19 10:01:10 -040018139
18140/**
James Smarta93ff372010-10-22 11:06:08 -040018141 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050018142 * @phba: pointer to lpfc hba data structure.
18143 * @fcf_index: FCF table entry offset.
18144 *
18145 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040018146 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050018147 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018148 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018149 * otherwise.
18150 **/
18151int
18152lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18153{
18154 int rc = 0, error;
18155 LPFC_MBOXQ_t *mboxq;
18156
18157 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18158 if (!mboxq) {
18159 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18160 "2763 Failed to allocate mbox for "
18161 "READ_FCF cmd\n");
18162 error = -ENOMEM;
18163 goto fail_fcf_read;
18164 }
18165 /* Construct the read FCF record mailbox command */
18166 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18167 if (rc) {
18168 error = -EINVAL;
18169 goto fail_fcf_read;
18170 }
18171 /* Issue the mailbox command asynchronously */
18172 mboxq->vport = phba->pport;
18173 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18174 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18175 if (rc == MBX_NOT_FINISHED)
18176 error = -EIO;
18177 else
18178 error = 0;
18179
18180fail_fcf_read:
18181 if (error && mboxq)
18182 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18183 return error;
18184}
18185
18186/**
18187 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18188 * @phba: pointer to lpfc hba data structure.
18189 * @fcf_index: FCF table entry offset.
18190 *
18191 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040018192 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050018193 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018194 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018195 * otherwise.
18196 **/
18197int
18198lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18199{
18200 int rc = 0, error;
18201 LPFC_MBOXQ_t *mboxq;
18202
18203 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18204 if (!mboxq) {
18205 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18206 "2758 Failed to allocate mbox for "
18207 "READ_FCF cmd\n");
18208 error = -ENOMEM;
18209 goto fail_fcf_read;
18210 }
18211 /* Construct the read FCF record mailbox command */
18212 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18213 if (rc) {
18214 error = -EINVAL;
18215 goto fail_fcf_read;
18216 }
18217 /* Issue the mailbox command asynchronously */
18218 mboxq->vport = phba->pport;
18219 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18220 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18221 if (rc == MBX_NOT_FINISHED)
18222 error = -EIO;
18223 else
18224 error = 0;
18225
18226fail_fcf_read:
18227 if (error && mboxq)
18228 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18229 return error;
18230}
18231
18232/**
James Smartf5cb5302015-12-16 18:11:52 -050018233 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040018234 * phba pointer to the lpfc_hba struct for this port.
18235 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18236 * routine when the rr_bmask is empty. The FCF indecies are put into the
18237 * rr_bmask based on their priority level. Starting from the highest priority
18238 * to the lowest. The most likely FCF candidate will be in the highest
18239 * priority group. When this routine is called it searches the fcf_pri list for
18240 * next lowest priority group and repopulates the rr_bmask with only those
18241 * fcf_indexes.
18242 * returns:
18243 * 1=success 0=failure
18244 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018245static int
James Smart7d791df2011-07-22 18:37:52 -040018246lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18247{
18248 uint16_t next_fcf_pri;
18249 uint16_t last_index;
18250 struct lpfc_fcf_pri *fcf_pri;
18251 int rc;
18252 int ret = 0;
18253
18254 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18255 LPFC_SLI4_FCF_TBL_INDX_MAX);
18256 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18257 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050018258
18259 /* Verify the priority list has 2 or more entries */
18260 spin_lock_irq(&phba->hbalock);
18261 if (list_empty(&phba->fcf.fcf_pri_list) ||
18262 list_is_singular(&phba->fcf.fcf_pri_list)) {
18263 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040018264 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18265 "3061 Last IDX %d\n", last_index);
18266 return 0; /* Empty rr list */
18267 }
James Smart25626692013-03-01 16:36:54 -050018268 spin_unlock_irq(&phba->hbalock);
18269
James Smart7d791df2011-07-22 18:37:52 -040018270 next_fcf_pri = 0;
18271 /*
18272 * Clear the rr_bmask and set all of the bits that are at this
18273 * priority.
18274 */
18275 memset(phba->fcf.fcf_rr_bmask, 0,
18276 sizeof(*phba->fcf.fcf_rr_bmask));
18277 spin_lock_irq(&phba->hbalock);
18278 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18279 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18280 continue;
18281 /*
18282 * the 1st priority that has not FLOGI failed
18283 * will be the highest.
18284 */
18285 if (!next_fcf_pri)
18286 next_fcf_pri = fcf_pri->fcf_rec.priority;
18287 spin_unlock_irq(&phba->hbalock);
18288 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18289 rc = lpfc_sli4_fcf_rr_index_set(phba,
18290 fcf_pri->fcf_rec.fcf_index);
18291 if (rc)
18292 return 0;
18293 }
18294 spin_lock_irq(&phba->hbalock);
18295 }
18296 /*
18297 * if next_fcf_pri was not set above and the list is not empty then
18298 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070018299 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040018300 */
18301 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18302 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18303 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18304 /*
18305 * the 1st priority that has not FLOGI failed
18306 * will be the highest.
18307 */
18308 if (!next_fcf_pri)
18309 next_fcf_pri = fcf_pri->fcf_rec.priority;
18310 spin_unlock_irq(&phba->hbalock);
18311 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18312 rc = lpfc_sli4_fcf_rr_index_set(phba,
18313 fcf_pri->fcf_rec.fcf_index);
18314 if (rc)
18315 return 0;
18316 }
18317 spin_lock_irq(&phba->hbalock);
18318 }
18319 } else
18320 ret = 1;
18321 spin_unlock_irq(&phba->hbalock);
18322
18323 return ret;
18324}
18325/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018326 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18327 * @phba: pointer to lpfc hba data structure.
18328 *
18329 * This routine is to get the next eligible FCF record index in a round
18330 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040018331 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050018332 * shall be returned, otherwise, the next eligible FCF record's index
18333 * shall be returned.
18334 **/
18335uint16_t
18336lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18337{
18338 uint16_t next_fcf_index;
18339
James Smart421c6622013-01-03 15:44:16 -050018340initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040018341 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050018342 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18343
James Smart7d791df2011-07-22 18:37:52 -040018344next_priority:
James Smart421c6622013-01-03 15:44:16 -050018345 /* Determine the next fcf index to check */
18346 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050018347 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18348 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040018349 next_fcf_index);
18350
James Smart0c9ab6f2010-02-26 14:15:57 -050018351 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018352 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18353 /*
18354 * If we have wrapped then we need to clear the bits that
18355 * have been tested so that we can detect when we should
18356 * change the priority level.
18357 */
James Smart0c9ab6f2010-02-26 14:15:57 -050018358 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18359 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040018360 }
18361
James Smart0c9ab6f2010-02-26 14:15:57 -050018362
James Smart3804dc82010-07-14 15:31:37 -040018363 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018364 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18365 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18366 /*
18367 * If next fcf index is not found check if there are lower
18368 * Priority level fcf's in the fcf_priority list.
18369 * Set up the rr_bmask with all of the avaiable fcf bits
18370 * at that level and continue the selection process.
18371 */
18372 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018373 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018374 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18375 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040018376 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18377 return LPFC_FCOE_FCF_NEXT_NONE;
18378 else {
18379 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18380 "3063 Only FCF available idx %d, flag %x\n",
18381 next_fcf_index,
18382 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18383 return next_fcf_index;
18384 }
James Smart3804dc82010-07-14 15:31:37 -040018385 }
18386
James Smart7d791df2011-07-22 18:37:52 -040018387 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18388 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018389 LPFC_FCF_FLOGI_FAILED) {
18390 if (list_is_singular(&phba->fcf.fcf_pri_list))
18391 return LPFC_FCOE_FCF_NEXT_NONE;
18392
James Smart7d791df2011-07-22 18:37:52 -040018393 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018394 }
James Smart7d791df2011-07-22 18:37:52 -040018395
James Smart3804dc82010-07-14 15:31:37 -040018396 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018397 "2845 Get next roundrobin failover FCF (x%x)\n",
18398 next_fcf_index);
18399
James Smart0c9ab6f2010-02-26 14:15:57 -050018400 return next_fcf_index;
18401}
18402
18403/**
18404 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18405 * @phba: pointer to lpfc hba data structure.
18406 *
18407 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018408 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018409 * does not go beyond the range of the driver allocated bmask dimension
18410 * before setting the bit.
18411 *
18412 * Returns 0 if the index bit successfully set, otherwise, it returns
18413 * -EINVAL.
18414 **/
18415int
18416lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18417{
18418 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18419 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018420 "2610 FCF (x%x) reached driver's book "
18421 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018422 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18423 return -EINVAL;
18424 }
18425 /* Set the eligible FCF record index bmask */
18426 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18427
James Smart3804dc82010-07-14 15:31:37 -040018428 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018429 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018430 "bmask\n", fcf_index);
18431
James Smart0c9ab6f2010-02-26 14:15:57 -050018432 return 0;
18433}
18434
18435/**
James Smart3804dc82010-07-14 15:31:37 -040018436 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018437 * @phba: pointer to lpfc hba data structure.
18438 *
18439 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018440 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018441 * does not go beyond the range of the driver allocated bmask dimension
18442 * before clearing the bit.
18443 **/
18444void
18445lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18446{
James Smart9a803a72013-09-06 12:17:56 -040018447 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018448 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18449 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018450 "2762 FCF (x%x) reached driver's book "
18451 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018452 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18453 return;
18454 }
18455 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018456 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018457 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18458 list) {
James Smart7d791df2011-07-22 18:37:52 -040018459 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18460 list_del_init(&fcf_pri->list);
18461 break;
18462 }
18463 }
18464 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018465 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018466
18467 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018468 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018469 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018470}
18471
18472/**
James Smartecfd03c2010-02-12 14:41:27 -050018473 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18474 * @phba: pointer to lpfc hba data structure.
18475 *
18476 * This routine is the completion routine for the rediscover FCF table mailbox
18477 * command. If the mailbox command returned failure, it will try to stop the
18478 * FCF rediscover wait timer.
18479 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018480static void
James Smartecfd03c2010-02-12 14:41:27 -050018481lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18482{
18483 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18484 uint32_t shdr_status, shdr_add_status;
18485
18486 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18487
18488 shdr_status = bf_get(lpfc_mbox_hdr_status,
18489 &redisc_fcf->header.cfg_shdr.response);
18490 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18491 &redisc_fcf->header.cfg_shdr.response);
18492 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018493 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018494 "2746 Requesting for FCF rediscovery failed "
18495 "status x%x add_status x%x\n",
18496 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018497 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018498 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018499 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018500 spin_unlock_irq(&phba->hbalock);
18501 /*
18502 * CVL event triggered FCF rediscover request failed,
18503 * last resort to re-try current registered FCF entry.
18504 */
18505 lpfc_retry_pport_discovery(phba);
18506 } else {
18507 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018508 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018509 spin_unlock_irq(&phba->hbalock);
18510 /*
18511 * DEAD FCF event triggered FCF rediscover request
18512 * failed, last resort to fail over as a link down
18513 * to FCF registration.
18514 */
18515 lpfc_sli4_fcf_dead_failthrough(phba);
18516 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018517 } else {
18518 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018519 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018520 /*
18521 * Start FCF rediscovery wait timer for pending FCF
18522 * before rescan FCF record table.
18523 */
18524 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018525 }
James Smartecfd03c2010-02-12 14:41:27 -050018526
18527 mempool_free(mbox, phba->mbox_mem_pool);
18528}
18529
18530/**
James Smart3804dc82010-07-14 15:31:37 -040018531 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018532 * @phba: pointer to lpfc hba data structure.
18533 *
18534 * This routine is invoked to request for rediscovery of the entire FCF table
18535 * by the port.
18536 **/
18537int
18538lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18539{
18540 LPFC_MBOXQ_t *mbox;
18541 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18542 int rc, length;
18543
James Smart0c9ab6f2010-02-26 14:15:57 -050018544 /* Cancel retry delay timers to all vports before FCF rediscover */
18545 lpfc_cancel_all_vport_retry_delay_timer(phba);
18546
James Smartecfd03c2010-02-12 14:41:27 -050018547 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18548 if (!mbox) {
18549 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18550 "2745 Failed to allocate mbox for "
18551 "requesting FCF rediscover.\n");
18552 return -ENOMEM;
18553 }
18554
18555 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18556 sizeof(struct lpfc_sli4_cfg_mhdr));
18557 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18558 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18559 length, LPFC_SLI4_MBX_EMBED);
18560
18561 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18562 /* Set count to 0 for invalidating the entire FCF database */
18563 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18564
18565 /* Issue the mailbox command asynchronously */
18566 mbox->vport = phba->pport;
18567 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18568 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18569
18570 if (rc == MBX_NOT_FINISHED) {
18571 mempool_free(mbox, phba->mbox_mem_pool);
18572 return -EIO;
18573 }
18574 return 0;
18575}
18576
18577/**
James Smartfc2b9892010-02-26 14:15:29 -050018578 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18579 * @phba: pointer to lpfc hba data structure.
18580 *
18581 * This function is the failover routine as a last resort to the FCF DEAD
18582 * event when driver failed to perform fast FCF failover.
18583 **/
18584void
18585lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18586{
18587 uint32_t link_state;
18588
18589 /*
18590 * Last resort as FCF DEAD event failover will treat this as
18591 * a link down, but save the link state because we don't want
18592 * it to be changed to Link Down unless it is already down.
18593 */
18594 link_state = phba->link_state;
18595 lpfc_linkdown(phba);
18596 phba->link_state = link_state;
18597
18598 /* Unregister FCF if no devices connected to it */
18599 lpfc_unregister_unused_fcf(phba);
18600}
18601
18602/**
James Smart026abb82011-12-13 13:20:45 -050018603 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018604 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018605 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018606 *
James Smart026abb82011-12-13 13:20:45 -050018607 * This function gets SLI3 port configure region 23 data through memory dump
18608 * mailbox command. When it successfully retrieves data, the size of the data
18609 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018610 **/
James Smart026abb82011-12-13 13:20:45 -050018611static uint32_t
18612lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018613{
18614 LPFC_MBOXQ_t *pmb = NULL;
18615 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018616 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018617 int rc;
18618
James Smart026abb82011-12-13 13:20:45 -050018619 if (!rgn23_data)
18620 return 0;
18621
James Smarta0c87cb2009-07-19 10:01:10 -040018622 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18623 if (!pmb) {
18624 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018625 "2600 failed to allocate mailbox memory\n");
18626 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018627 }
18628 mb = &pmb->u.mb;
18629
James Smarta0c87cb2009-07-19 10:01:10 -040018630 do {
18631 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18632 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18633
18634 if (rc != MBX_SUCCESS) {
18635 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018636 "2601 failed to read config "
18637 "region 23, rc 0x%x Status 0x%x\n",
18638 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018639 mb->un.varDmp.word_cnt = 0;
18640 }
18641 /*
18642 * dump mem may return a zero when finished or we got a
18643 * mailbox error, either way we are done.
18644 */
18645 if (mb->un.varDmp.word_cnt == 0)
18646 break;
18647 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18648 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18649
18650 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018651 rgn23_data + offset,
18652 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018653 offset += mb->un.varDmp.word_cnt;
18654 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18655
James Smart026abb82011-12-13 13:20:45 -050018656 mempool_free(pmb, phba->mbox_mem_pool);
18657 return offset;
18658}
18659
18660/**
18661 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18662 * @phba: pointer to lpfc hba data structure.
18663 * @rgn23_data: pointer to configure region 23 data.
18664 *
18665 * This function gets SLI4 port configure region 23 data through memory dump
18666 * mailbox command. When it successfully retrieves data, the size of the data
18667 * will be returned, otherwise, 0 will be returned.
18668 **/
18669static uint32_t
18670lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18671{
18672 LPFC_MBOXQ_t *mboxq = NULL;
18673 struct lpfc_dmabuf *mp = NULL;
18674 struct lpfc_mqe *mqe;
18675 uint32_t data_length = 0;
18676 int rc;
18677
18678 if (!rgn23_data)
18679 return 0;
18680
18681 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18682 if (!mboxq) {
18683 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18684 "3105 failed to allocate mailbox memory\n");
18685 return 0;
18686 }
18687
18688 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18689 goto out;
18690 mqe = &mboxq->u.mqe;
18691 mp = (struct lpfc_dmabuf *) mboxq->context1;
18692 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18693 if (rc)
18694 goto out;
18695 data_length = mqe->un.mb_words[5];
18696 if (data_length == 0)
18697 goto out;
18698 if (data_length > DMP_RGN23_SIZE) {
18699 data_length = 0;
18700 goto out;
18701 }
18702 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18703out:
18704 mempool_free(mboxq, phba->mbox_mem_pool);
18705 if (mp) {
18706 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18707 kfree(mp);
18708 }
18709 return data_length;
18710}
18711
18712/**
18713 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18714 * @phba: pointer to lpfc hba data structure.
18715 *
18716 * This function read region 23 and parse TLV for port status to
18717 * decide if the user disaled the port. If the TLV indicates the
18718 * port is disabled, the hba_flag is set accordingly.
18719 **/
18720void
18721lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18722{
18723 uint8_t *rgn23_data = NULL;
18724 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18725 uint32_t offset = 0;
18726
18727 /* Get adapter Region 23 data */
18728 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18729 if (!rgn23_data)
18730 goto out;
18731
18732 if (phba->sli_rev < LPFC_SLI_REV4)
18733 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18734 else {
18735 if_type = bf_get(lpfc_sli_intf_if_type,
18736 &phba->sli4_hba.sli_intf);
18737 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18738 goto out;
18739 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18740 }
James Smarta0c87cb2009-07-19 10:01:10 -040018741
18742 if (!data_size)
18743 goto out;
18744
18745 /* Check the region signature first */
18746 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18747 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18748 "2619 Config region 23 has bad signature\n");
18749 goto out;
18750 }
18751 offset += 4;
18752
18753 /* Check the data structure version */
18754 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18755 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18756 "2620 Config region 23 has bad version\n");
18757 goto out;
18758 }
18759 offset += 4;
18760
18761 /* Parse TLV entries in the region */
18762 while (offset < data_size) {
18763 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18764 break;
18765 /*
18766 * If the TLV is not driver specific TLV or driver id is
18767 * not linux driver id, skip the record.
18768 */
18769 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18770 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18771 (rgn23_data[offset + 3] != 0)) {
18772 offset += rgn23_data[offset + 1] * 4 + 4;
18773 continue;
18774 }
18775
18776 /* Driver found a driver specific TLV in the config region */
18777 sub_tlv_len = rgn23_data[offset + 1] * 4;
18778 offset += 4;
18779 tlv_offset = 0;
18780
18781 /*
18782 * Search for configured port state sub-TLV.
18783 */
18784 while ((offset < data_size) &&
18785 (tlv_offset < sub_tlv_len)) {
18786 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18787 offset += 4;
18788 tlv_offset += 4;
18789 break;
18790 }
18791 if (rgn23_data[offset] != PORT_STE_TYPE) {
18792 offset += rgn23_data[offset + 1] * 4 + 4;
18793 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18794 continue;
18795 }
18796
18797 /* This HBA contains PORT_STE configured */
18798 if (!rgn23_data[offset + 2])
18799 phba->hba_flag |= LINK_DISABLED;
18800
18801 goto out;
18802 }
18803 }
James Smart026abb82011-12-13 13:20:45 -050018804
James Smarta0c87cb2009-07-19 10:01:10 -040018805out:
James Smarta0c87cb2009-07-19 10:01:10 -040018806 kfree(rgn23_data);
18807 return;
18808}
James Smart695a8142010-01-26 23:08:03 -050018809
18810/**
James Smart52d52442011-05-24 11:42:45 -040018811 * lpfc_wr_object - write an object to the firmware
18812 * @phba: HBA structure that indicates port to create a queue on.
18813 * @dmabuf_list: list of dmabufs to write to the port.
18814 * @size: the total byte value of the objects to write to the port.
18815 * @offset: the current offset to be used to start the transfer.
18816 *
18817 * This routine will create a wr_object mailbox command to send to the port.
18818 * the mailbox command will be constructed using the dma buffers described in
18819 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18820 * BDEs that the imbedded mailbox can support. The @offset variable will be
18821 * used to indicate the starting offset of the transfer and will also return
18822 * the offset after the write object mailbox has completed. @size is used to
18823 * determine the end of the object and whether the eof bit should be set.
18824 *
18825 * Return 0 is successful and offset will contain the the new offset to use
18826 * for the next write.
18827 * Return negative value for error cases.
18828 **/
18829int
18830lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18831 uint32_t size, uint32_t *offset)
18832{
18833 struct lpfc_mbx_wr_object *wr_object;
18834 LPFC_MBOXQ_t *mbox;
18835 int rc = 0, i = 0;
18836 uint32_t shdr_status, shdr_add_status;
18837 uint32_t mbox_tmo;
18838 union lpfc_sli4_cfg_shdr *shdr;
18839 struct lpfc_dmabuf *dmabuf;
18840 uint32_t written = 0;
18841
18842 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18843 if (!mbox)
18844 return -ENOMEM;
18845
18846 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18847 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18848 sizeof(struct lpfc_mbx_wr_object) -
18849 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18850
18851 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18852 wr_object->u.request.write_offset = *offset;
18853 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18854 wr_object->u.request.object_name[0] =
18855 cpu_to_le32(wr_object->u.request.object_name[0]);
18856 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18857 list_for_each_entry(dmabuf, dmabuf_list, list) {
18858 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18859 break;
18860 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18861 wr_object->u.request.bde[i].addrHigh =
18862 putPaddrHigh(dmabuf->phys);
18863 if (written + SLI4_PAGE_SIZE >= size) {
18864 wr_object->u.request.bde[i].tus.f.bdeSize =
18865 (size - written);
18866 written += (size - written);
18867 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18868 } else {
18869 wr_object->u.request.bde[i].tus.f.bdeSize =
18870 SLI4_PAGE_SIZE;
18871 written += SLI4_PAGE_SIZE;
18872 }
18873 i++;
18874 }
18875 wr_object->u.request.bde_count = i;
18876 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18877 if (!phba->sli4_hba.intr_enable)
18878 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18879 else {
James Smarta183a152011-10-10 21:32:43 -040018880 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040018881 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18882 }
18883 /* The IOCTL status is embedded in the mailbox subheader. */
18884 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18885 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18886 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18887 if (rc != MBX_TIMEOUT)
18888 mempool_free(mbox, phba->mbox_mem_pool);
18889 if (shdr_status || shdr_add_status || rc) {
18890 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18891 "3025 Write Object mailbox failed with "
18892 "status x%x add_status x%x, mbx status x%x\n",
18893 shdr_status, shdr_add_status, rc);
18894 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080018895 *offset = shdr_add_status;
James Smart52d52442011-05-24 11:42:45 -040018896 } else
18897 *offset += wr_object->u.response.actual_write_length;
18898 return rc;
18899}
18900
18901/**
James Smart695a8142010-01-26 23:08:03 -050018902 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18903 * @vport: pointer to vport data structure.
18904 *
18905 * This function iterate through the mailboxq and clean up all REG_LOGIN
18906 * and REG_VPI mailbox commands associated with the vport. This function
18907 * is called when driver want to restart discovery of the vport due to
18908 * a Clear Virtual Link event.
18909 **/
18910void
18911lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18912{
18913 struct lpfc_hba *phba = vport->phba;
18914 LPFC_MBOXQ_t *mb, *nextmb;
18915 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040018916 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040018917 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040018918 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040018919 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050018920 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050018921
James Smartd439d282010-09-29 11:18:45 -040018922 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050018923 spin_lock_irq(&phba->hbalock);
18924 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18925 if (mb->vport != vport)
18926 continue;
18927
18928 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18929 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18930 continue;
18931
James Smartd439d282010-09-29 11:18:45 -040018932 list_del(&mb->list);
18933 list_add_tail(&mb->list, &mbox_cmd_list);
18934 }
18935 /* Clean up active mailbox command with the vport */
18936 mb = phba->sli.mbox_active;
18937 if (mb && (mb->vport == vport)) {
18938 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18939 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18940 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18941 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18942 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18943 /* Put reference count for delayed processing */
18944 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18945 /* Unregister the RPI when mailbox complete */
18946 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18947 }
18948 }
James Smart63e801c2010-11-20 23:14:19 -050018949 /* Cleanup any mailbox completions which are not yet processed */
18950 do {
18951 restart_loop = 0;
18952 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18953 /*
18954 * If this mailox is already processed or it is
18955 * for another vport ignore it.
18956 */
18957 if ((mb->vport != vport) ||
18958 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18959 continue;
18960
18961 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18962 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18963 continue;
18964
18965 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18966 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18967 ndlp = (struct lpfc_nodelist *)mb->context2;
18968 /* Unregister the RPI when mailbox complete */
18969 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18970 restart_loop = 1;
18971 spin_unlock_irq(&phba->hbalock);
18972 spin_lock(shost->host_lock);
18973 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18974 spin_unlock(shost->host_lock);
18975 spin_lock_irq(&phba->hbalock);
18976 break;
18977 }
18978 }
18979 } while (restart_loop);
18980
James Smartd439d282010-09-29 11:18:45 -040018981 spin_unlock_irq(&phba->hbalock);
18982
18983 /* Release the cleaned-up mailbox commands */
18984 while (!list_empty(&mbox_cmd_list)) {
18985 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050018986 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18987 mp = (struct lpfc_dmabuf *) (mb->context1);
18988 if (mp) {
18989 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18990 kfree(mp);
18991 }
James Smart78730cf2010-04-06 15:06:30 -040018992 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040018993 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040018994 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018995 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040018996 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018997 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040018998 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040018999 }
James Smart695a8142010-01-26 23:08:03 -050019000 }
James Smart695a8142010-01-26 23:08:03 -050019001 mempool_free(mb, phba->mbox_mem_pool);
19002 }
James Smartd439d282010-09-29 11:18:45 -040019003
19004 /* Release the ndlp with the cleaned-up active mailbox command */
19005 if (act_mbx_ndlp) {
19006 spin_lock(shost->host_lock);
19007 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19008 spin_unlock(shost->host_lock);
19009 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050019010 }
James Smart695a8142010-01-26 23:08:03 -050019011}
19012
James Smart2a9bf3d2010-06-07 15:24:45 -040019013/**
19014 * lpfc_drain_txq - Drain the txq
19015 * @phba: Pointer to HBA context object.
19016 *
19017 * This function attempt to submit IOCBs on the txq
19018 * to the adapter. For SLI4 adapters, the txq contains
19019 * ELS IOCBs that have been deferred because the there
19020 * are no SGLs. This congestion can occur with large
19021 * vport counts during node discovery.
19022 **/
19023
19024uint32_t
19025lpfc_drain_txq(struct lpfc_hba *phba)
19026{
19027 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080019028 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090019029 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040019030 unsigned long iflags = 0;
19031 char *fail_msg = NULL;
19032 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080019033 union lpfc_wqe128 wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040019034 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040019035
James Smart895427b2017-02-12 13:52:30 -080019036 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070019037 if (unlikely(!pring))
19038 return 0;
James Smart895427b2017-02-12 13:52:30 -080019039
James Smart398d81c2013-05-31 17:04:19 -040019040 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050019041 list_for_each_entry(piocbq, &pring->txq, list) {
19042 txq_cnt++;
19043 }
19044
19045 if (txq_cnt > pring->txq_max)
19046 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019047
James Smart398d81c2013-05-31 17:04:19 -040019048 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019049
James Smart0e9bb8d2013-03-01 16:35:12 -050019050 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040019051 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019052
James Smart19ca7602010-11-20 23:11:55 -050019053 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040019054 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040019055 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040019056 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19057 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050019058 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040019059 break;
19060 }
James Smart895427b2017-02-12 13:52:30 -080019061 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040019062 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050019063 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040019064 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019065 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040019066 }
James Smart0e9bb8d2013-03-01 16:35:12 -050019067 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040019068
19069 /* The xri and iocb resources secured,
19070 * attempt to issue request
19071 */
James Smart6d368e52011-05-24 11:44:12 -040019072 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040019073 piocbq->sli4_xritag = sglq->sli4_xritag;
19074 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19075 fail_msg = "to convert bpl to sgl";
James Smart205e8242018-03-05 12:04:03 -080019076 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019077 fail_msg = "to convert iocb to wqe";
James Smart205e8242018-03-05 12:04:03 -080019078 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019079 fail_msg = " - Wq is full";
19080 else
19081 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19082
19083 if (fail_msg) {
19084 /* Failed means we can't issue and need to cancel */
19085 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19086 "2822 IOCB failed %s iotag 0x%x "
19087 "xri 0x%x\n",
19088 fail_msg,
19089 piocbq->iotag, piocbq->sli4_xritag);
19090 list_add_tail(&piocbq->list, &completions);
19091 }
James Smart398d81c2013-05-31 17:04:19 -040019092 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019093 }
19094
James Smart2a9bf3d2010-06-07 15:24:45 -040019095 /* Cancel all the IOCBs that cannot be issued */
19096 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19097 IOERR_SLI_ABORTED);
19098
James Smart0e9bb8d2013-03-01 16:35:12 -050019099 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019100}
James Smart895427b2017-02-12 13:52:30 -080019101
19102/**
19103 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19104 * @phba: Pointer to HBA context object.
19105 * @pwqe: Pointer to command WQE.
19106 * @sglq: Pointer to the scatter gather queue object.
19107 *
19108 * This routine converts the bpl or bde that is in the WQE
19109 * to a sgl list for the sli4 hardware. The physical address
19110 * of the bpl/bde is converted back to a virtual address.
19111 * If the WQE contains a BPL then the list of BDE's is
19112 * converted to sli4_sge's. If the WQE contains a single
19113 * BDE then it is converted to a single sli_sge.
19114 * The WQE is still in cpu endianness so the contents of
19115 * the bpl can be used without byte swapping.
19116 *
19117 * Returns valid XRI = Success, NO_XRI = Failure.
19118 */
19119static uint16_t
19120lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19121 struct lpfc_sglq *sglq)
19122{
19123 uint16_t xritag = NO_XRI;
19124 struct ulp_bde64 *bpl = NULL;
19125 struct ulp_bde64 bde;
19126 struct sli4_sge *sgl = NULL;
19127 struct lpfc_dmabuf *dmabuf;
James Smart205e8242018-03-05 12:04:03 -080019128 union lpfc_wqe128 *wqe;
James Smart895427b2017-02-12 13:52:30 -080019129 int numBdes = 0;
19130 int i = 0;
19131 uint32_t offset = 0; /* accumulated offset in the sg request list */
19132 int inbound = 0; /* number of sg reply entries inbound from firmware */
19133 uint32_t cmd;
19134
19135 if (!pwqeq || !sglq)
19136 return xritag;
19137
19138 sgl = (struct sli4_sge *)sglq->sgl;
19139 wqe = &pwqeq->wqe;
19140 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19141
19142 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19143 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19144 return sglq->sli4_xritag;
19145 numBdes = pwqeq->rsvd2;
19146 if (numBdes) {
19147 /* The addrHigh and addrLow fields within the WQE
19148 * have not been byteswapped yet so there is no
19149 * need to swap them back.
19150 */
19151 if (pwqeq->context3)
19152 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19153 else
19154 return xritag;
19155
19156 bpl = (struct ulp_bde64 *)dmabuf->virt;
19157 if (!bpl)
19158 return xritag;
19159
19160 for (i = 0; i < numBdes; i++) {
19161 /* Should already be byte swapped. */
19162 sgl->addr_hi = bpl->addrHigh;
19163 sgl->addr_lo = bpl->addrLow;
19164
19165 sgl->word2 = le32_to_cpu(sgl->word2);
19166 if ((i+1) == numBdes)
19167 bf_set(lpfc_sli4_sge_last, sgl, 1);
19168 else
19169 bf_set(lpfc_sli4_sge_last, sgl, 0);
19170 /* swap the size field back to the cpu so we
19171 * can assign it to the sgl.
19172 */
19173 bde.tus.w = le32_to_cpu(bpl->tus.w);
19174 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19175 /* The offsets in the sgl need to be accumulated
19176 * separately for the request and reply lists.
19177 * The request is always first, the reply follows.
19178 */
19179 switch (cmd) {
19180 case CMD_GEN_REQUEST64_WQE:
19181 /* add up the reply sg entries */
19182 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19183 inbound++;
19184 /* first inbound? reset the offset */
19185 if (inbound == 1)
19186 offset = 0;
19187 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19188 bf_set(lpfc_sli4_sge_type, sgl,
19189 LPFC_SGE_TYPE_DATA);
19190 offset += bde.tus.f.bdeSize;
19191 break;
19192 case CMD_FCP_TRSP64_WQE:
19193 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19194 bf_set(lpfc_sli4_sge_type, sgl,
19195 LPFC_SGE_TYPE_DATA);
19196 break;
19197 case CMD_FCP_TSEND64_WQE:
19198 case CMD_FCP_TRECEIVE64_WQE:
19199 bf_set(lpfc_sli4_sge_type, sgl,
19200 bpl->tus.f.bdeFlags);
19201 if (i < 3)
19202 offset = 0;
19203 else
19204 offset += bde.tus.f.bdeSize;
19205 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19206 break;
19207 }
19208 sgl->word2 = cpu_to_le32(sgl->word2);
19209 bpl++;
19210 sgl++;
19211 }
19212 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19213 /* The addrHigh and addrLow fields of the BDE have not
19214 * been byteswapped yet so they need to be swapped
19215 * before putting them in the sgl.
19216 */
19217 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19218 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19219 sgl->word2 = le32_to_cpu(sgl->word2);
19220 bf_set(lpfc_sli4_sge_last, sgl, 1);
19221 sgl->word2 = cpu_to_le32(sgl->word2);
19222 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19223 }
19224 return sglq->sli4_xritag;
19225}
19226
19227/**
19228 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19229 * @phba: Pointer to HBA context object.
19230 * @ring_number: Base sli ring number
19231 * @pwqe: Pointer to command WQE.
19232 **/
19233int
19234lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
19235 struct lpfc_iocbq *pwqe)
19236{
James Smart205e8242018-03-05 12:04:03 -080019237 union lpfc_wqe128 *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080019238 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080019239 struct lpfc_queue *wq;
19240 struct lpfc_sglq *sglq;
19241 struct lpfc_sli_ring *pring;
19242 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070019243 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080019244
19245 /* NVME_LS and NVME_LS ABTS requests. */
19246 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19247 pring = phba->sli4_hba.nvmels_wq->pring;
19248 spin_lock_irqsave(&pring->ring_lock, iflags);
19249 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19250 if (!sglq) {
19251 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19252 return WQE_BUSY;
19253 }
19254 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19255 pwqe->sli4_xritag = sglq->sli4_xritag;
19256 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19257 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19258 return WQE_ERROR;
19259 }
19260 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19261 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070019262 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19263 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019264 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019265 return ret;
James Smart895427b2017-02-12 13:52:30 -080019266 }
Dick Kennedycd22d602017-08-23 16:55:35 -070019267
James Smart895427b2017-02-12 13:52:30 -080019268 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19269 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19270 return 0;
19271 }
19272
19273 /* NVME_FCREQ and NVME_ABTS requests */
19274 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19275 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19276 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19277
19278 spin_lock_irqsave(&pring->ring_lock, iflags);
19279 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19280 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19281 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019282 ret = lpfc_sli4_wq_put(wq, wqe);
19283 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019284 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019285 return ret;
James Smart895427b2017-02-12 13:52:30 -080019286 }
19287 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19288 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19289 return 0;
19290 }
19291
James Smartf358dd02017-02-12 13:52:34 -080019292 /* NVMET requests */
19293 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19294 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19295 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19296
19297 spin_lock_irqsave(&pring->ring_lock, iflags);
19298 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070019299 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080019300 if (pwqe->sli4_xritag == NO_XRI) {
19301 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19302 pwqe->sli4_xritag = sglq->sli4_xritag;
19303 }
19304 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19305 pwqe->sli4_xritag);
19306 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19307 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19308 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019309 ret = lpfc_sli4_wq_put(wq, wqe);
19310 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080019311 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019312 return ret;
James Smartf358dd02017-02-12 13:52:34 -080019313 }
19314 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19315 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19316 return 0;
19317 }
James Smart895427b2017-02-12 13:52:30 -080019318 return WQE_ERROR;
19319}