blob: 4ce3ca6f4b79f9ad49aef79f6347caf3bd2be20e [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 Smart4f774512009-05-22 14:52:35 -0400112lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
113{
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 */
143 tmp = (uint8_t *)wqe;
144 for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
145 writeq(*((uint64_t *)(tmp + i)), q->dpp_regaddr + i);
146 }
147 /* ensure WQE bcopy and DPP flushed before doorbell write */
James Smart6b3b3bd2016-12-19 15:07:30 -0800148 wmb();
James Smart4f774512009-05-22 14:52:35 -0400149
150 /* Update the host index before invoking device */
151 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400152
153 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400154
155 /* Ring Doorbell */
156 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500157 if (q->db_format == LPFC_DB_LIST_FORMAT) {
James Smart1351e692018-02-22 08:18:43 -0800158 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
159 bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
160 bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
161 bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
162 q->dpp_id);
163 bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
164 q->queue_id);
165 } else {
166 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
167 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
168 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
169 }
James Smart962bc512013-01-03 15:44:00 -0500170 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
171 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
172 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
173 } else {
174 return -EINVAL;
175 }
176 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400177
178 return 0;
179}
180
181/**
182 * lpfc_sli4_wq_release - Updates internal hba index for WQ
183 * @q: The Work Queue to operate on.
184 * @index: The index to advance the hba index to.
185 *
186 * This routine will update the HBA index of a queue to reflect consumption of
187 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
188 * an entry the host calls this function to update the queue's internal
189 * pointers. This routine returns the number of entries that were consumed by
190 * the HBA.
191 **/
192static uint32_t
193lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
194{
195 uint32_t released = 0;
196
James Smart2e90f4b2011-12-13 13:22:37 -0500197 /* sanity check on queue memory */
198 if (unlikely(!q))
199 return 0;
200
James Smart4f774512009-05-22 14:52:35 -0400201 if (q->hba_index == index)
202 return 0;
203 do {
204 q->hba_index = ((q->hba_index + 1) % q->entry_count);
205 released++;
206 } while (q->hba_index != index);
207 return released;
208}
209
210/**
211 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
212 * @q: The Mailbox Queue to operate on.
213 * @wqe: The Mailbox Queue Entry to put on the Work queue.
214 *
215 * This routine will copy the contents of @mqe to the next available entry on
216 * the @q. This function will then ring the Work Queue Doorbell to signal the
217 * HBA to start processing the Work Queue Entry. This function returns 0 if
218 * successful. If no entries are available on @q then this function will return
219 * -ENOMEM.
220 * The caller is expected to hold the hbalock when calling this routine.
221 **/
222static uint32_t
223lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
224{
James Smart2e90f4b2011-12-13 13:22:37 -0500225 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400226 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400227
James Smart2e90f4b2011-12-13 13:22:37 -0500228 /* sanity check on queue memory */
229 if (unlikely(!q))
230 return -ENOMEM;
231 temp_mqe = q->qe[q->host_index].mqe;
232
James Smart4f774512009-05-22 14:52:35 -0400233 /* If the host has not yet processed the next entry then we are done */
234 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
235 return -ENOMEM;
236 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
237 /* Save off the mailbox pointer for completion */
238 q->phba->mbox = (MAILBOX_t *)temp_mqe;
239
240 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400241 q->host_index = ((q->host_index + 1) % q->entry_count);
242
243 /* Ring Doorbell */
244 doorbell.word0 = 0;
245 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
246 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
247 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400248 return 0;
249}
250
251/**
252 * lpfc_sli4_mq_release - Updates internal hba index for MQ
253 * @q: The Mailbox Queue to operate on.
254 *
255 * This routine will update the HBA index of a queue to reflect consumption of
256 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
257 * an entry the host calls this function to update the queue's internal
258 * pointers. This routine returns the number of entries that were consumed by
259 * the HBA.
260 **/
261static uint32_t
262lpfc_sli4_mq_release(struct lpfc_queue *q)
263{
James Smart2e90f4b2011-12-13 13:22:37 -0500264 /* sanity check on queue memory */
265 if (unlikely(!q))
266 return 0;
267
James Smart4f774512009-05-22 14:52:35 -0400268 /* Clear the mailbox pointer for completion */
269 q->phba->mbox = NULL;
270 q->hba_index = ((q->hba_index + 1) % q->entry_count);
271 return 1;
272}
273
274/**
275 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
276 * @q: The Event Queue to get the first valid EQE from
277 *
278 * This routine will get the first valid Event Queue Entry from @q, update
279 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
280 * the Queue (no more work to do), or the Queue is full of EQEs that have been
281 * processed, but not popped back to the HBA then this routine will return NULL.
282 **/
283static struct lpfc_eqe *
284lpfc_sli4_eq_get(struct lpfc_queue *q)
285{
James Smart7365f6f2018-02-22 08:18:46 -0800286 struct lpfc_hba *phba;
James Smart2e90f4b2011-12-13 13:22:37 -0500287 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400288 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500289
290 /* sanity check on queue memory */
291 if (unlikely(!q))
292 return NULL;
James Smart7365f6f2018-02-22 08:18:46 -0800293 phba = q->phba;
James Smart2e90f4b2011-12-13 13:22:37 -0500294 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400295
296 /* If the next EQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800297 if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400298 return NULL;
299 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400300 idx = ((q->hba_index + 1) % q->entry_count);
301 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400302 return NULL;
303
James Smart027140e2012-08-03 12:35:44 -0400304 q->hba_index = idx;
James Smart7365f6f2018-02-22 08:18:46 -0800305 /* if the index wrapped around, toggle the valid bit */
306 if (phba->sli4_hba.pc_sli4_params.eqav && !q->hba_index)
307 q->qe_valid = (q->qe_valid) ? 0 : 1;
308
James Smart27f344e2014-05-07 17:16:46 -0400309
310 /*
311 * insert barrier for instruction interlock : data from the hardware
312 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800313 * upon. Speculative instructions were allowing a bcopy at the start
314 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
315 * after our return, to copy data before the valid bit check above
316 * was done. As such, some of the copied data was stale. The barrier
317 * ensures the check is before any data is copied.
James Smart27f344e2014-05-07 17:16:46 -0400318 */
319 mb();
James Smart4f774512009-05-22 14:52:35 -0400320 return eqe;
321}
322
323/**
James Smartba20c852012-08-03 12:36:52 -0400324 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
325 * @q: The Event Queue to disable interrupts
326 *
327 **/
James Smartb71413d2018-02-22 08:18:40 -0800328inline void
James Smartba20c852012-08-03 12:36:52 -0400329lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
330{
331 struct lpfc_register doorbell;
332
333 doorbell.word0 = 0;
334 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
335 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
336 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
337 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
338 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800339 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smartba20c852012-08-03 12:36:52 -0400340}
341
342/**
James Smart27d6ac02018-02-22 08:18:42 -0800343 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
344 * @q: The Event Queue to disable interrupts
345 *
346 **/
347inline void
348lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
349{
350 struct lpfc_register doorbell;
351
352 doorbell.word0 = 0;
353 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
354 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
355 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
356 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
357 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
358 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
359}
360
361/**
James Smart4f774512009-05-22 14:52:35 -0400362 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
363 * @q: The Event Queue that the host has completed processing for.
364 * @arm: Indicates whether the host wants to arms this CQ.
365 *
366 * This routine will mark all Event Queue Entries on @q, from the last
367 * known completed entry to the last entry that was processed, as completed
368 * by clearing the valid bit for each completion queue entry. Then it will
369 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
370 * The internal host index in the @q will be updated by this routine to indicate
371 * that the host has finished processing the entries. The @arm parameter
372 * indicates that the queue should be rearmed when ringing the doorbell.
373 *
374 * This function will return the number of EQEs that were popped.
375 **/
376uint32_t
377lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
378{
379 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800380 struct lpfc_hba *phba;
James Smart4f774512009-05-22 14:52:35 -0400381 struct lpfc_eqe *temp_eqe;
382 struct lpfc_register doorbell;
383
James Smart2e90f4b2011-12-13 13:22:37 -0500384 /* sanity check on queue memory */
385 if (unlikely(!q))
386 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800387 phba = q->phba;
James Smart2e90f4b2011-12-13 13:22:37 -0500388
James Smart4f774512009-05-22 14:52:35 -0400389 /* while there are valid entries */
390 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800391 if (!phba->sli4_hba.pc_sli4_params.eqav) {
392 temp_eqe = q->qe[q->host_index].eqe;
393 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
394 }
James Smart4f774512009-05-22 14:52:35 -0400395 released++;
396 q->host_index = ((q->host_index + 1) % q->entry_count);
397 }
398 if (unlikely(released == 0 && !arm))
399 return 0;
400
401 /* ring doorbell for number popped */
402 doorbell.word0 = 0;
403 if (arm) {
404 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
405 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
406 }
407 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
408 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500409 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
410 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
411 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800412 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500413 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
414 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
James Smart9dd35422018-02-22 08:18:41 -0800415 readl(q->phba->sli4_hba.EQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400416 return released;
417}
418
419/**
James Smart27d6ac02018-02-22 08:18:42 -0800420 * lpfc_sli4_if6_eq_release - Indicates the host has finished processing an EQ
421 * @q: The Event Queue that the host has completed processing for.
422 * @arm: Indicates whether the host wants to arms this CQ.
423 *
424 * This routine will mark all Event Queue Entries on @q, from the last
425 * known completed entry to the last entry that was processed, as completed
426 * by clearing the valid bit for each completion queue entry. Then it will
427 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
428 * The internal host index in the @q will be updated by this routine to indicate
429 * that the host has finished processing the entries. The @arm parameter
430 * indicates that the queue should be rearmed when ringing the doorbell.
431 *
432 * This function will return the number of EQEs that were popped.
433 **/
434uint32_t
435lpfc_sli4_if6_eq_release(struct lpfc_queue *q, bool arm)
436{
437 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800438 struct lpfc_hba *phba;
James Smart27d6ac02018-02-22 08:18:42 -0800439 struct lpfc_eqe *temp_eqe;
440 struct lpfc_register doorbell;
441
442 /* sanity check on queue memory */
443 if (unlikely(!q))
444 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800445 phba = q->phba;
James Smart27d6ac02018-02-22 08:18:42 -0800446
447 /* while there are valid entries */
448 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800449 if (!phba->sli4_hba.pc_sli4_params.eqav) {
450 temp_eqe = q->qe[q->host_index].eqe;
451 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
452 }
James Smart27d6ac02018-02-22 08:18:42 -0800453 released++;
454 q->host_index = ((q->host_index + 1) % q->entry_count);
455 }
456 if (unlikely(released == 0 && !arm))
457 return 0;
458
459 /* ring doorbell for number popped */
460 doorbell.word0 = 0;
461 if (arm)
462 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
463 bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, released);
464 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
465 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
466 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
467 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
468 readl(q->phba->sli4_hba.EQDBregaddr);
469 return released;
470}
471
472/**
James Smart4f774512009-05-22 14:52:35 -0400473 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
474 * @q: The Completion Queue to get the first valid CQE from
475 *
476 * This routine will get the first valid Completion Queue Entry from @q, update
477 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
478 * the Queue (no more work to do), or the Queue is full of CQEs that have been
479 * processed, but not popped back to the HBA then this routine will return NULL.
480 **/
481static struct lpfc_cqe *
482lpfc_sli4_cq_get(struct lpfc_queue *q)
483{
James Smart7365f6f2018-02-22 08:18:46 -0800484 struct lpfc_hba *phba;
James Smart4f774512009-05-22 14:52:35 -0400485 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400486 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400487
James Smart2e90f4b2011-12-13 13:22:37 -0500488 /* sanity check on queue memory */
489 if (unlikely(!q))
490 return NULL;
James Smart7365f6f2018-02-22 08:18:46 -0800491 phba = q->phba;
492 cqe = q->qe[q->hba_index].cqe;
James Smart2e90f4b2011-12-13 13:22:37 -0500493
James Smart4f774512009-05-22 14:52:35 -0400494 /* If the next CQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800495 if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400496 return NULL;
497 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400498 idx = ((q->hba_index + 1) % q->entry_count);
499 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400500 return NULL;
501
James Smart027140e2012-08-03 12:35:44 -0400502 q->hba_index = idx;
James Smart7365f6f2018-02-22 08:18:46 -0800503 /* if the index wrapped around, toggle the valid bit */
504 if (phba->sli4_hba.pc_sli4_params.cqav && !q->hba_index)
505 q->qe_valid = (q->qe_valid) ? 0 : 1;
James Smart27f344e2014-05-07 17:16:46 -0400506
507 /*
508 * insert barrier for instruction interlock : data from the hardware
509 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800510 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
511 * instructions allowing action on content before valid bit checked,
512 * add barrier here as well. May not be needed as "content" is a
513 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400514 */
515 mb();
James Smart4f774512009-05-22 14:52:35 -0400516 return cqe;
517}
518
519/**
520 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
521 * @q: The Completion Queue that the host has completed processing for.
522 * @arm: Indicates whether the host wants to arms this CQ.
523 *
524 * This routine will mark all Completion queue entries on @q, from the last
525 * known completed entry to the last entry that was processed, as completed
526 * by clearing the valid bit for each completion queue entry. Then it will
527 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
528 * The internal host index in the @q will be updated by this routine to indicate
529 * that the host has finished processing the entries. The @arm parameter
530 * indicates that the queue should be rearmed when ringing the doorbell.
531 *
532 * This function will return the number of CQEs that were released.
533 **/
534uint32_t
535lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
536{
537 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800538 struct lpfc_hba *phba;
James Smart4f774512009-05-22 14:52:35 -0400539 struct lpfc_cqe *temp_qe;
540 struct lpfc_register doorbell;
541
James Smart2e90f4b2011-12-13 13:22:37 -0500542 /* sanity check on queue memory */
543 if (unlikely(!q))
544 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800545 phba = q->phba;
546
James Smart4f774512009-05-22 14:52:35 -0400547 /* while there are valid entries */
548 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800549 if (!phba->sli4_hba.pc_sli4_params.cqav) {
550 temp_qe = q->qe[q->host_index].cqe;
551 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
552 }
James Smart4f774512009-05-22 14:52:35 -0400553 released++;
554 q->host_index = ((q->host_index + 1) % q->entry_count);
555 }
556 if (unlikely(released == 0 && !arm))
557 return 0;
558
559 /* ring doorbell for number popped */
560 doorbell.word0 = 0;
561 if (arm)
562 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
563 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
564 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500565 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
566 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
567 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800568 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400569 return released;
570}
571
572/**
James Smart27d6ac02018-02-22 08:18:42 -0800573 * lpfc_sli4_if6_cq_release - Indicates the host has finished processing a CQ
574 * @q: The Completion Queue that the host has completed processing for.
575 * @arm: Indicates whether the host wants to arms this CQ.
576 *
577 * This routine will mark all Completion queue entries on @q, from the last
578 * known completed entry to the last entry that was processed, as completed
579 * by clearing the valid bit for each completion queue entry. Then it will
580 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
581 * The internal host index in the @q will be updated by this routine to indicate
582 * that the host has finished processing the entries. The @arm parameter
583 * indicates that the queue should be rearmed when ringing the doorbell.
584 *
585 * This function will return the number of CQEs that were released.
586 **/
587uint32_t
588lpfc_sli4_if6_cq_release(struct lpfc_queue *q, bool arm)
589{
590 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800591 struct lpfc_hba *phba;
James Smart27d6ac02018-02-22 08:18:42 -0800592 struct lpfc_cqe *temp_qe;
593 struct lpfc_register doorbell;
594
595 /* sanity check on queue memory */
596 if (unlikely(!q))
597 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800598 phba = q->phba;
599
James Smart27d6ac02018-02-22 08:18:42 -0800600 /* while there are valid entries */
601 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800602 if (!phba->sli4_hba.pc_sli4_params.cqav) {
603 temp_qe = q->qe[q->host_index].cqe;
604 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
605 }
James Smart27d6ac02018-02-22 08:18:42 -0800606 released++;
607 q->host_index = ((q->host_index + 1) % q->entry_count);
608 }
609 if (unlikely(released == 0 && !arm))
610 return 0;
611
612 /* ring doorbell for number popped */
613 doorbell.word0 = 0;
614 if (arm)
615 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
616 bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, released);
617 bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
618 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
619 return released;
620}
621
622/**
James Smart4f774512009-05-22 14:52:35 -0400623 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
624 * @q: The Header Receive Queue to operate on.
625 * @wqe: The Receive Queue Entry to put on the Receive queue.
626 *
627 * This routine will copy the contents of @wqe to the next available entry on
628 * the @q. This function will then ring the Receive Queue Doorbell to signal the
629 * HBA to start processing the Receive Queue Entry. This function returns the
630 * index that the rqe was copied to if successful. If no entries are available
631 * on @q then this function will return -ENOMEM.
632 * The caller is expected to hold the hbalock when calling this routine.
633 **/
James Smart895427b2017-02-12 13:52:30 -0800634int
James Smart4f774512009-05-22 14:52:35 -0400635lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
636 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
637{
James Smart2e90f4b2011-12-13 13:22:37 -0500638 struct lpfc_rqe *temp_hrqe;
639 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400640 struct lpfc_register doorbell;
James Smartcbc5de12017-12-08 17:18:04 -0800641 int hq_put_index;
642 int dq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400643
James Smart2e90f4b2011-12-13 13:22:37 -0500644 /* sanity check on queue memory */
645 if (unlikely(!hq) || unlikely(!dq))
646 return -ENOMEM;
James Smartcbc5de12017-12-08 17:18:04 -0800647 hq_put_index = hq->host_index;
648 dq_put_index = dq->host_index;
649 temp_hrqe = hq->qe[hq_put_index].rqe;
650 temp_drqe = dq->qe[dq_put_index].rqe;
James Smart2e90f4b2011-12-13 13:22:37 -0500651
James Smart4f774512009-05-22 14:52:35 -0400652 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
653 return -EINVAL;
James Smartcbc5de12017-12-08 17:18:04 -0800654 if (hq_put_index != dq_put_index)
James Smart4f774512009-05-22 14:52:35 -0400655 return -EINVAL;
656 /* If the host has not yet processed the next entry then we are done */
James Smartcbc5de12017-12-08 17:18:04 -0800657 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
James Smart4f774512009-05-22 14:52:35 -0400658 return -EBUSY;
659 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
660 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
661
662 /* Update the host index to point to the next slot */
James Smartcbc5de12017-12-08 17:18:04 -0800663 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
664 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
James Smart61f3d4b2017-05-15 15:20:41 -0700665 hq->RQ_buf_posted++;
James Smart4f774512009-05-22 14:52:35 -0400666
667 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400668 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400669 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500670 if (hq->db_format == LPFC_DB_RING_FORMAT) {
671 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
672 hq->entry_repost);
673 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
674 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
675 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
676 hq->entry_repost);
677 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
678 hq->host_index);
679 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
680 } else {
681 return -EINVAL;
682 }
683 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400684 }
James Smartcbc5de12017-12-08 17:18:04 -0800685 return hq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400686}
687
688/**
689 * lpfc_sli4_rq_release - Updates internal hba index for RQ
690 * @q: The Header Receive Queue to operate on.
691 *
692 * This routine will update the HBA index of a queue to reflect consumption of
693 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
694 * consumed an entry the host calls this function to update the queue's
695 * internal pointers. This routine returns the number of entries that were
696 * consumed by the HBA.
697 **/
698static uint32_t
699lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
700{
James Smart2e90f4b2011-12-13 13:22:37 -0500701 /* sanity check on queue memory */
702 if (unlikely(!hq) || unlikely(!dq))
703 return 0;
704
James Smart4f774512009-05-22 14:52:35 -0400705 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
706 return 0;
707 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
708 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
709 return 1;
710}
711
James Smarte59058c2008-08-24 21:49:00 -0400712/**
James Smart3621a712009-04-06 18:47:14 -0400713 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400714 * @phba: Pointer to HBA context object.
715 * @pring: Pointer to driver SLI ring object.
716 *
717 * This function returns pointer to next command iocb entry
718 * in the command ring. The caller must hold hbalock to prevent
719 * other threads consume the next command iocb.
720 * SLI-2/SLI-3 provide different sized iocbs.
721 **/
James Smarted957682007-06-17 19:56:37 -0500722static inline IOCB_t *
723lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
724{
James Smart7e56aa22012-08-03 12:35:34 -0400725 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
726 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500727}
728
James Smarte59058c2008-08-24 21:49:00 -0400729/**
James Smart3621a712009-04-06 18:47:14 -0400730 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400731 * @phba: Pointer to HBA context object.
732 * @pring: Pointer to driver SLI ring object.
733 *
734 * This function returns pointer to next response iocb entry
735 * in the response ring. The caller must hold hbalock to make sure
736 * that no other thread consume the next response iocb.
737 * SLI-2/SLI-3 provide different sized iocbs.
738 **/
James Smarted957682007-06-17 19:56:37 -0500739static inline IOCB_t *
740lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
741{
James Smart7e56aa22012-08-03 12:35:34 -0400742 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
743 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500744}
745
James Smarte59058c2008-08-24 21:49:00 -0400746/**
James Smart3621a712009-04-06 18:47:14 -0400747 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400748 * @phba: Pointer to HBA context object.
749 *
750 * This function is called with hbalock held. This function
751 * allocates a new driver iocb object from the iocb pool. If the
752 * allocation is successful, it returns pointer to the newly
753 * allocated iocb object else it returns NULL.
754 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400755struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500756__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400757{
758 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
759 struct lpfc_iocbq * iocbq = NULL;
760
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100761 lockdep_assert_held(&phba->hbalock);
762
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400763 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400764 if (iocbq)
765 phba->iocb_cnt++;
766 if (phba->iocb_cnt > phba->iocb_max)
767 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400768 return iocbq;
769}
770
James Smarte59058c2008-08-24 21:49:00 -0400771/**
James Smartda0436e2009-05-22 14:51:39 -0400772 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
773 * @phba: Pointer to HBA context object.
774 * @xritag: XRI value.
775 *
776 * This function clears the sglq pointer from the array of acive
777 * sglq's. The xritag that is passed in is used to index into the
778 * array. Before the xritag can be used it needs to be adjusted
779 * by subtracting the xribase.
780 *
781 * Returns sglq ponter = success, NULL = Failure.
782 **/
James Smart895427b2017-02-12 13:52:30 -0800783struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400784__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
785{
James Smartda0436e2009-05-22 14:51:39 -0400786 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400787
788 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
789 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400790 return sglq;
791}
792
793/**
794 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
795 * @phba: Pointer to HBA context object.
796 * @xritag: XRI value.
797 *
798 * This function returns the sglq pointer from the array of acive
799 * sglq's. The xritag that is passed in is used to index into the
800 * array. Before the xritag can be used it needs to be adjusted
801 * by subtracting the xribase.
802 *
803 * Returns sglq ponter = success, NULL = Failure.
804 **/
James Smart0f65ff62010-02-26 14:14:23 -0500805struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400806__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
807{
James Smartda0436e2009-05-22 14:51:39 -0400808 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400809
810 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400811 return sglq;
812}
813
814/**
James Smart1151e3e2011-02-16 12:39:35 -0500815 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500816 * @phba: Pointer to HBA context object.
817 * @xritag: xri used in this exchange.
818 * @rrq: The RRQ to be cleared.
819 *
James Smart19ca7602010-11-20 23:11:55 -0500820 **/
James Smart1151e3e2011-02-16 12:39:35 -0500821void
822lpfc_clr_rrq_active(struct lpfc_hba *phba,
823 uint16_t xritag,
824 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500825{
James Smart1151e3e2011-02-16 12:39:35 -0500826 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500827
James Smart1151e3e2011-02-16 12:39:35 -0500828 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
829 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500830
831 /* The target DID could have been swapped (cable swap)
832 * we should use the ndlp from the findnode if it is
833 * available.
834 */
James Smart1151e3e2011-02-16 12:39:35 -0500835 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500836 ndlp = rrq->ndlp;
837
James Smart1151e3e2011-02-16 12:39:35 -0500838 if (!ndlp)
839 goto out;
840
James Smartcff261f2013-12-17 20:29:47 -0500841 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500842 rrq->send_rrq = 0;
843 rrq->xritag = 0;
844 rrq->rrq_stop_time = 0;
845 }
James Smart1151e3e2011-02-16 12:39:35 -0500846out:
James Smart19ca7602010-11-20 23:11:55 -0500847 mempool_free(rrq, phba->rrq_pool);
848}
849
850/**
851 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
852 * @phba: Pointer to HBA context object.
853 *
854 * This function is called with hbalock held. This function
855 * Checks if stop_time (ratov from setting rrq active) has
856 * been reached, if it has and the send_rrq flag is set then
857 * it will call lpfc_send_rrq. If the send_rrq flag is not set
858 * then it will just call the routine to clear the rrq and
859 * free the rrq resource.
860 * The timer is set to the next rrq that is going to expire before
861 * leaving the routine.
862 *
863 **/
864void
865lpfc_handle_rrq_active(struct lpfc_hba *phba)
866{
867 struct lpfc_node_rrq *rrq;
868 struct lpfc_node_rrq *nextrrq;
869 unsigned long next_time;
870 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500871 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500872
873 spin_lock_irqsave(&phba->hbalock, iflags);
874 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400875 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500876 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500877 &phba->active_rrq_list, list) {
878 if (time_after(jiffies, rrq->rrq_stop_time))
879 list_move(&rrq->list, &send_rrq);
880 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500881 next_time = rrq->rrq_stop_time;
882 }
883 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500884 if ((!list_empty(&phba->active_rrq_list)) &&
885 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500886 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500887 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
888 list_del(&rrq->list);
889 if (!rrq->send_rrq)
890 /* this call will free the rrq */
891 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
892 else if (lpfc_send_rrq(phba, rrq)) {
893 /* if we send the rrq then the completion handler
894 * will clear the bit in the xribitmap.
895 */
896 lpfc_clr_rrq_active(phba, rrq->xritag,
897 rrq);
898 }
899 }
James Smart19ca7602010-11-20 23:11:55 -0500900}
901
902/**
903 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
904 * @vport: Pointer to vport context object.
905 * @xri: The xri used in the exchange.
906 * @did: The targets DID for this exchange.
907 *
908 * returns NULL = rrq not found in the phba->active_rrq_list.
909 * rrq = rrq for this xri and target.
910 **/
911struct lpfc_node_rrq *
912lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
913{
914 struct lpfc_hba *phba = vport->phba;
915 struct lpfc_node_rrq *rrq;
916 struct lpfc_node_rrq *nextrrq;
917 unsigned long iflags;
918
919 if (phba->sli_rev != LPFC_SLI_REV4)
920 return NULL;
921 spin_lock_irqsave(&phba->hbalock, iflags);
922 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
923 if (rrq->vport == vport && rrq->xritag == xri &&
924 rrq->nlp_DID == did){
925 list_del(&rrq->list);
926 spin_unlock_irqrestore(&phba->hbalock, iflags);
927 return rrq;
928 }
929 }
930 spin_unlock_irqrestore(&phba->hbalock, iflags);
931 return NULL;
932}
933
934/**
935 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
936 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500937 * @ndlp: Pointer to the lpfc_node_list structure.
938 * If ndlp is NULL Remove all active RRQs for this vport from the
939 * phba->active_rrq_list and clear the rrq.
940 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500941 **/
942void
James Smart1151e3e2011-02-16 12:39:35 -0500943lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500944
945{
946 struct lpfc_hba *phba = vport->phba;
947 struct lpfc_node_rrq *rrq;
948 struct lpfc_node_rrq *nextrrq;
949 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500950 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500951
952 if (phba->sli_rev != LPFC_SLI_REV4)
953 return;
James Smart1151e3e2011-02-16 12:39:35 -0500954 if (!ndlp) {
955 lpfc_sli4_vport_delete_els_xri_aborted(vport);
956 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500957 }
James Smart1151e3e2011-02-16 12:39:35 -0500958 spin_lock_irqsave(&phba->hbalock, iflags);
959 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
960 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
961 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500962 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500963
964 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
965 list_del(&rrq->list);
966 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
967 }
James Smart19ca7602010-11-20 23:11:55 -0500968}
969
970/**
James Smart1151e3e2011-02-16 12:39:35 -0500971 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500972 * @phba: Pointer to HBA context object.
973 * @ndlp: Targets nodelist pointer for this exchange.
974 * @xritag the xri in the bitmap to test.
975 *
976 * This function is called with hbalock held. This function
977 * returns 0 = rrq not active for this xri
978 * 1 = rrq is valid for this xri.
979 **/
James Smart1151e3e2011-02-16 12:39:35 -0500980int
981lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500982 uint16_t xritag)
983{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100984 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -0500985 if (!ndlp)
986 return 0;
James Smartcff261f2013-12-17 20:29:47 -0500987 if (!ndlp->active_rrqs_xri_bitmap)
988 return 0;
989 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500990 return 1;
991 else
992 return 0;
993}
994
995/**
996 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
997 * @phba: Pointer to HBA context object.
998 * @ndlp: nodelist pointer for this target.
999 * @xritag: xri used in this exchange.
1000 * @rxid: Remote Exchange ID.
1001 * @send_rrq: Flag used to determine if we should send rrq els cmd.
1002 *
1003 * This function takes the hbalock.
1004 * The active bit is always set in the active rrq xri_bitmap even
1005 * if there is no slot avaiable for the other rrq information.
1006 *
1007 * returns 0 rrq actived for this xri
1008 * < 0 No memory or invalid ndlp.
1009 **/
1010int
1011lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -05001012 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -05001013{
James Smart19ca7602010-11-20 23:11:55 -05001014 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -05001015 struct lpfc_node_rrq *rrq;
1016 int empty;
1017
1018 if (!ndlp)
1019 return -EINVAL;
1020
1021 if (!phba->cfg_enable_rrq)
1022 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001023
1024 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001025 if (phba->pport->load_flag & FC_UNLOADING) {
1026 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1027 goto out;
1028 }
1029
1030 /*
1031 * set the active bit even if there is no mem available.
1032 */
1033 if (NLP_CHK_FREE_REQ(ndlp))
1034 goto out;
1035
1036 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1037 goto out;
1038
James Smartcff261f2013-12-17 20:29:47 -05001039 if (!ndlp->active_rrqs_xri_bitmap)
1040 goto out;
1041
1042 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -05001043 goto out;
1044
James Smart19ca7602010-11-20 23:11:55 -05001045 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001046 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
1047 if (!rrq) {
1048 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1049 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1050 " DID:0x%x Send:%d\n",
1051 xritag, rxid, ndlp->nlp_DID, send_rrq);
1052 return -EINVAL;
1053 }
James Smarte5771b42013-03-01 16:37:14 -05001054 if (phba->cfg_enable_rrq == 1)
1055 rrq->send_rrq = send_rrq;
1056 else
1057 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -05001058 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -04001059 rrq->rrq_stop_time = jiffies +
1060 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -05001061 rrq->ndlp = ndlp;
1062 rrq->nlp_DID = ndlp->nlp_DID;
1063 rrq->vport = ndlp->vport;
1064 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -05001065 spin_lock_irqsave(&phba->hbalock, iflags);
1066 empty = list_empty(&phba->active_rrq_list);
1067 list_add_tail(&rrq->list, &phba->active_rrq_list);
1068 phba->hba_flag |= HBA_RRQ_ACTIVE;
1069 if (empty)
1070 lpfc_worker_wake_up(phba);
1071 spin_unlock_irqrestore(&phba->hbalock, iflags);
1072 return 0;
1073out:
1074 spin_unlock_irqrestore(&phba->hbalock, iflags);
1075 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1076 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1077 " DID:0x%x Send:%d\n",
1078 xritag, rxid, ndlp->nlp_DID, send_rrq);
1079 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001080}
1081
1082/**
James Smart895427b2017-02-12 13:52:30 -08001083 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -04001084 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -05001085 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -04001086 *
James Smartdafe8ce2014-09-03 12:56:40 -04001087 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -04001088 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -04001089 * list is not empty then it is successful, it returns pointer to the newly
1090 * allocated sglq object else it returns NULL.
1091 **/
1092static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -08001093__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -04001094{
James Smart895427b2017-02-12 13:52:30 -08001095 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -04001096 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001097 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001098 struct lpfc_scsi_buf *lpfc_cmd;
1099 struct lpfc_nodelist *ndlp;
1100 int found = 0;
1101
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001102 lockdep_assert_held(&phba->hbalock);
1103
James Smart19ca7602010-11-20 23:11:55 -05001104 if (piocbq->iocb_flag & LPFC_IO_FCP) {
1105 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
1106 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -05001107 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -04001108 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -05001109 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -04001110 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1111 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1112 ndlp = NULL;
1113 else
1114 ndlp = piocbq->context_un.ndlp;
1115 } else {
James Smart19ca7602010-11-20 23:11:55 -05001116 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -04001117 }
James Smart19ca7602010-11-20 23:11:55 -05001118
James Smart895427b2017-02-12 13:52:30 -08001119 spin_lock(&phba->sli4_hba.sgl_list_lock);
1120 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -05001121 start_sglq = sglq;
1122 while (!found) {
1123 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -08001124 break;
James Smart895427b2017-02-12 13:52:30 -08001125 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1126 test_bit(sglq->sli4_lxritag,
1127 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -05001128 /* This xri has an rrq outstanding for this DID.
1129 * put it back in the list and get another xri.
1130 */
James Smart895427b2017-02-12 13:52:30 -08001131 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001132 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -08001133 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -05001134 struct lpfc_sglq, list);
1135 if (sglq == start_sglq) {
James Smart14041bd2017-06-01 21:07:01 -07001136 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001137 sglq = NULL;
1138 break;
1139 } else
1140 continue;
1141 }
1142 sglq->ndlp = ndlp;
1143 found = 1;
James Smart6d368e52011-05-24 11:44:12 -04001144 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -05001145 sglq->state = SGL_ALLOCATED;
1146 }
James Smart895427b2017-02-12 13:52:30 -08001147 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001148 return sglq;
1149}
1150
1151/**
James Smartf358dd02017-02-12 13:52:34 -08001152 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1153 * @phba: Pointer to HBA context object.
1154 * @piocb: Pointer to the iocbq.
1155 *
1156 * This function is called with the sgl_list lock held. This function
1157 * gets a new driver sglq object from the sglq list. If the
1158 * list is not empty then it is successful, it returns pointer to the newly
1159 * allocated sglq object else it returns NULL.
1160 **/
1161struct lpfc_sglq *
1162__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1163{
1164 struct list_head *lpfc_nvmet_sgl_list;
1165 struct lpfc_sglq *sglq = NULL;
1166
1167 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1168
1169 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1170
1171 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1172 if (!sglq)
1173 return NULL;
1174 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1175 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001176 return sglq;
1177}
1178
James Smarte59058c2008-08-24 21:49:00 -04001179/**
James Smart3621a712009-04-06 18:47:14 -04001180 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001181 * @phba: Pointer to HBA context object.
1182 *
1183 * This function is called with no lock held. This function
1184 * allocates a new driver iocb object from the iocb pool. If the
1185 * allocation is successful, it returns pointer to the newly
1186 * allocated iocb object else it returns NULL.
1187 **/
James Smart2e0fef82007-06-17 19:56:36 -05001188struct lpfc_iocbq *
1189lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001190{
James Smart2e0fef82007-06-17 19:56:36 -05001191 struct lpfc_iocbq * iocbq = NULL;
1192 unsigned long iflags;
1193
1194 spin_lock_irqsave(&phba->hbalock, iflags);
1195 iocbq = __lpfc_sli_get_iocbq(phba);
1196 spin_unlock_irqrestore(&phba->hbalock, iflags);
1197 return iocbq;
1198}
1199
James Smarte59058c2008-08-24 21:49:00 -04001200/**
James Smart4f774512009-05-22 14:52:35 -04001201 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1202 * @phba: Pointer to HBA context object.
1203 * @iocbq: Pointer to driver iocb object.
1204 *
1205 * This function is called with hbalock held to release driver
1206 * iocb object to the iocb pool. The iotag in the iocb object
1207 * does not change for each use of the iocb object. This function
1208 * clears all other fields of the iocb object when it is freed.
1209 * The sqlq structure that holds the xritag and phys and virtual
1210 * mappings for the scatter gather list is retrieved from the
1211 * active array of sglq. The get of the sglq pointer also clears
1212 * the entry in the array. If the status of the IO indiactes that
1213 * this IO was aborted then the sglq entry it put on the
1214 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1215 * IO has good status or fails for any other reason then the sglq
James Smart895427b2017-02-12 13:52:30 -08001216 * entry is added to the free list (lpfc_els_sgl_list).
James Smart4f774512009-05-22 14:52:35 -04001217 **/
1218static void
1219__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1220{
1221 struct lpfc_sglq *sglq;
1222 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001223 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001224 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001225
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001226 lockdep_assert_held(&phba->hbalock);
1227
James Smart4f774512009-05-22 14:52:35 -04001228 if (iocbq->sli4_xritag == NO_XRI)
1229 sglq = NULL;
1230 else
James Smart6d368e52011-05-24 11:44:12 -04001231 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1232
James Smart0e9bb8d2013-03-01 16:35:12 -05001233
James Smart4f774512009-05-22 14:52:35 -04001234 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001235 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1236 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1237 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001238 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001239 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001240 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001241 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1242 spin_unlock_irqrestore(
1243 &phba->sli4_hba.sgl_list_lock, iflag);
1244 goto out;
1245 }
1246
James Smart895427b2017-02-12 13:52:30 -08001247 pring = phba->sli4_hba.els_wq->pring;
James Smart4f774512009-05-22 14:52:35 -04001248 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1249 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001250 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1251 iflag);
James Smart341af102010-01-26 23:07:37 -05001252 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001253 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001254 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001255 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001256 } else {
James Smart895427b2017-02-12 13:52:30 -08001257 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1258 iflag);
James Smart4f774512009-05-22 14:52:35 -04001259 sglq->state = SGL_FREED;
1260 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001261 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001262 &phba->sli4_hba.lpfc_els_sgl_list);
1263 spin_unlock_irqrestore(
1264 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001265
1266 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001267 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001268 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001269 }
James Smart4f774512009-05-22 14:52:35 -04001270 }
1271
James Smartf358dd02017-02-12 13:52:34 -08001272out:
James Smart4f774512009-05-22 14:52:35 -04001273 /*
1274 * Clean all volatile data fields, preserve iotag and node struct.
1275 */
1276 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001277 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001278 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001279 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1280 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001281 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1282}
1283
James Smart2a9bf3d2010-06-07 15:24:45 -04001284
James Smart4f774512009-05-22 14:52:35 -04001285/**
James Smart3772a992009-05-22 14:50:54 -04001286 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1287 * @phba: Pointer to HBA context object.
1288 * @iocbq: Pointer to driver iocb object.
1289 *
1290 * This function is called with hbalock held to release driver
1291 * iocb object to the iocb pool. The iotag in the iocb object
1292 * does not change for each use of the iocb object. This function
1293 * clears all other fields of the iocb object when it is freed.
1294 **/
1295static void
1296__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1297{
1298 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1299
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001300 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001301
1302 /*
James Smart3772a992009-05-22 14:50:54 -04001303 * Clean all volatile data fields, preserve iotag and node struct.
1304 */
1305 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1306 iocbq->sli4_xritag = NO_XRI;
1307 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1308}
1309
1310/**
James Smart3621a712009-04-06 18:47:14 -04001311 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001312 * @phba: Pointer to HBA context object.
1313 * @iocbq: Pointer to driver iocb object.
1314 *
1315 * This function is called with hbalock held to release driver
1316 * iocb object to the iocb pool. The iotag in the iocb object
1317 * does not change for each use of the iocb object. This function
1318 * clears all other fields of the iocb object when it is freed.
1319 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001320static void
James Smart2e0fef82007-06-17 19:56:36 -05001321__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1322{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001323 lockdep_assert_held(&phba->hbalock);
1324
James Smart3772a992009-05-22 14:50:54 -04001325 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001326 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001327}
1328
James Smarte59058c2008-08-24 21:49:00 -04001329/**
James Smart3621a712009-04-06 18:47:14 -04001330 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001331 * @phba: Pointer to HBA context object.
1332 * @iocbq: Pointer to driver iocb object.
1333 *
1334 * This function is called with no lock held to release the iocb to
1335 * iocb pool.
1336 **/
James Smart2e0fef82007-06-17 19:56:36 -05001337void
1338lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1339{
1340 unsigned long iflags;
1341
1342 /*
1343 * Clean all volatile data fields, preserve iotag and node struct.
1344 */
1345 spin_lock_irqsave(&phba->hbalock, iflags);
1346 __lpfc_sli_release_iocbq(phba, iocbq);
1347 spin_unlock_irqrestore(&phba->hbalock, iflags);
1348}
1349
James Smarte59058c2008-08-24 21:49:00 -04001350/**
James Smarta257bf92009-04-06 18:48:10 -04001351 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1352 * @phba: Pointer to HBA context object.
1353 * @iocblist: List of IOCBs.
1354 * @ulpstatus: ULP status in IOCB command field.
1355 * @ulpWord4: ULP word-4 in IOCB command field.
1356 *
1357 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1358 * on the list by invoking the complete callback function associated with the
1359 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1360 * fields.
1361 **/
1362void
1363lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1364 uint32_t ulpstatus, uint32_t ulpWord4)
1365{
1366 struct lpfc_iocbq *piocb;
1367
1368 while (!list_empty(iocblist)) {
1369 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001370 if (!piocb->iocb_cmpl)
1371 lpfc_sli_release_iocbq(phba, piocb);
1372 else {
1373 piocb->iocb.ulpStatus = ulpstatus;
1374 piocb->iocb.un.ulpWord[4] = ulpWord4;
1375 (piocb->iocb_cmpl) (phba, piocb, piocb);
1376 }
1377 }
1378 return;
1379}
1380
1381/**
James Smart3621a712009-04-06 18:47:14 -04001382 * lpfc_sli_iocb_cmd_type - Get the iocb type
1383 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001384 *
1385 * This function is called by ring event handler function to get the iocb type.
1386 * This function translates the iocb command to an iocb command type used to
1387 * decide the final disposition of each completed IOCB.
1388 * The function returns
1389 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1390 * LPFC_SOL_IOCB if it is a solicited iocb completion
1391 * LPFC_ABORT_IOCB if it is an abort iocb
1392 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1393 *
1394 * The caller is not required to hold any lock.
1395 **/
dea31012005-04-17 16:05:31 -05001396static lpfc_iocb_type
1397lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1398{
1399 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1400
1401 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1402 return 0;
1403
1404 switch (iocb_cmnd) {
1405 case CMD_XMIT_SEQUENCE_CR:
1406 case CMD_XMIT_SEQUENCE_CX:
1407 case CMD_XMIT_BCAST_CN:
1408 case CMD_XMIT_BCAST_CX:
1409 case CMD_ELS_REQUEST_CR:
1410 case CMD_ELS_REQUEST_CX:
1411 case CMD_CREATE_XRI_CR:
1412 case CMD_CREATE_XRI_CX:
1413 case CMD_GET_RPI_CN:
1414 case CMD_XMIT_ELS_RSP_CX:
1415 case CMD_GET_RPI_CR:
1416 case CMD_FCP_IWRITE_CR:
1417 case CMD_FCP_IWRITE_CX:
1418 case CMD_FCP_IREAD_CR:
1419 case CMD_FCP_IREAD_CX:
1420 case CMD_FCP_ICMND_CR:
1421 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001422 case CMD_FCP_TSEND_CX:
1423 case CMD_FCP_TRSP_CX:
1424 case CMD_FCP_TRECEIVE_CX:
1425 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001426 case CMD_ADAPTER_MSG:
1427 case CMD_ADAPTER_DUMP:
1428 case CMD_XMIT_SEQUENCE64_CR:
1429 case CMD_XMIT_SEQUENCE64_CX:
1430 case CMD_XMIT_BCAST64_CN:
1431 case CMD_XMIT_BCAST64_CX:
1432 case CMD_ELS_REQUEST64_CR:
1433 case CMD_ELS_REQUEST64_CX:
1434 case CMD_FCP_IWRITE64_CR:
1435 case CMD_FCP_IWRITE64_CX:
1436 case CMD_FCP_IREAD64_CR:
1437 case CMD_FCP_IREAD64_CX:
1438 case CMD_FCP_ICMND64_CR:
1439 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001440 case CMD_FCP_TSEND64_CX:
1441 case CMD_FCP_TRSP64_CX:
1442 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001443 case CMD_GEN_REQUEST64_CR:
1444 case CMD_GEN_REQUEST64_CX:
1445 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001446 case DSSCMD_IWRITE64_CR:
1447 case DSSCMD_IWRITE64_CX:
1448 case DSSCMD_IREAD64_CR:
1449 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001450 type = LPFC_SOL_IOCB;
1451 break;
1452 case CMD_ABORT_XRI_CN:
1453 case CMD_ABORT_XRI_CX:
1454 case CMD_CLOSE_XRI_CN:
1455 case CMD_CLOSE_XRI_CX:
1456 case CMD_XRI_ABORTED_CX:
1457 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001458 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001459 type = LPFC_ABORT_IOCB;
1460 break;
1461 case CMD_RCV_SEQUENCE_CX:
1462 case CMD_RCV_ELS_REQ_CX:
1463 case CMD_RCV_SEQUENCE64_CX:
1464 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001465 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001466 case CMD_IOCB_RCV_SEQ64_CX:
1467 case CMD_IOCB_RCV_ELS64_CX:
1468 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001469 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001470 type = LPFC_UNSOL_IOCB;
1471 break;
James Smart3163f722008-02-08 18:50:25 -05001472 case CMD_IOCB_XMIT_MSEQ64_CR:
1473 case CMD_IOCB_XMIT_MSEQ64_CX:
1474 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1475 case CMD_IOCB_RCV_ELS_LIST64_CX:
1476 case CMD_IOCB_CLOSE_EXTENDED_CN:
1477 case CMD_IOCB_ABORT_EXTENDED_CN:
1478 case CMD_IOCB_RET_HBQE64_CN:
1479 case CMD_IOCB_FCP_IBIDIR64_CR:
1480 case CMD_IOCB_FCP_IBIDIR64_CX:
1481 case CMD_IOCB_FCP_ITASKMGT64_CX:
1482 case CMD_IOCB_LOGENTRY_CN:
1483 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1484 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001485 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001486 type = LPFC_UNKNOWN_IOCB;
1487 break;
dea31012005-04-17 16:05:31 -05001488 default:
1489 type = LPFC_UNKNOWN_IOCB;
1490 break;
1491 }
1492
1493 return type;
1494}
1495
James Smarte59058c2008-08-24 21:49:00 -04001496/**
James Smart3621a712009-04-06 18:47:14 -04001497 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001498 * @phba: Pointer to HBA context object.
1499 *
1500 * This function is called from SLI initialization code
1501 * to configure every ring of the HBA's SLI interface. The
1502 * caller is not required to hold any lock. This function issues
1503 * a config_ring mailbox command for each ring.
1504 * This function returns zero if successful else returns a negative
1505 * error code.
1506 **/
dea31012005-04-17 16:05:31 -05001507static int
James Smarted957682007-06-17 19:56:37 -05001508lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001509{
1510 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001511 LPFC_MBOXQ_t *pmb;
1512 MAILBOX_t *pmbox;
1513 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001514
James Smarted957682007-06-17 19:56:37 -05001515 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1516 if (!pmb)
1517 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001518 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001519 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001520 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001521 lpfc_config_ring(phba, i, pmb);
1522 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1523 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001524 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001525 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001526 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1527 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001528 rc, pmbox->mbxCommand,
1529 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001530 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001531 ret = -ENXIO;
1532 break;
dea31012005-04-17 16:05:31 -05001533 }
1534 }
James Smarted957682007-06-17 19:56:37 -05001535 mempool_free(pmb, phba->mbox_mem_pool);
1536 return ret;
dea31012005-04-17 16:05:31 -05001537}
1538
James Smarte59058c2008-08-24 21:49:00 -04001539/**
James Smart3621a712009-04-06 18:47:14 -04001540 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001541 * @phba: Pointer to HBA context object.
1542 * @pring: Pointer to driver SLI ring object.
1543 * @piocb: Pointer to the driver iocb object.
1544 *
1545 * This function is called with hbalock held. The function adds the
1546 * new iocb to txcmplq of the given ring. This function always returns
1547 * 0. If this function is called for ELS ring, this function checks if
1548 * there is a vport associated with the ELS command. This function also
1549 * starts els_tmofunc timer if this is an ELS command.
1550 **/
dea31012005-04-17 16:05:31 -05001551static int
James Smart2e0fef82007-06-17 19:56:36 -05001552lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1553 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001554{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001555 lockdep_assert_held(&phba->hbalock);
1556
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001557 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001558
dea31012005-04-17 16:05:31 -05001559 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001560 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001561
James Smart92d7f7b2007-06-17 19:56:38 -05001562 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1563 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001564 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1565 BUG_ON(!piocb->vport);
1566 if (!(piocb->vport->load_flag & FC_UNLOADING))
1567 mod_timer(&piocb->vport->els_tmofunc,
1568 jiffies +
1569 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1570 }
dea31012005-04-17 16:05:31 -05001571
James Smart2e0fef82007-06-17 19:56:36 -05001572 return 0;
dea31012005-04-17 16:05:31 -05001573}
1574
James Smarte59058c2008-08-24 21:49:00 -04001575/**
James Smart3621a712009-04-06 18:47:14 -04001576 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001577 * @phba: Pointer to HBA context object.
1578 * @pring: Pointer to driver SLI ring object.
1579 *
1580 * This function is called with hbalock held to get next
1581 * iocb in txq of the given ring. If there is any iocb in
1582 * the txq, the function returns first iocb in the list after
1583 * removing the iocb from the list, else it returns NULL.
1584 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001585struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001586lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001587{
dea31012005-04-17 16:05:31 -05001588 struct lpfc_iocbq *cmd_iocb;
1589
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001590 lockdep_assert_held(&phba->hbalock);
1591
James Smart858c9f62007-06-17 19:56:39 -05001592 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001593 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001594}
1595
James Smarte59058c2008-08-24 21:49:00 -04001596/**
James Smart3621a712009-04-06 18:47:14 -04001597 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001598 * @phba: Pointer to HBA context object.
1599 * @pring: Pointer to driver SLI ring object.
1600 *
1601 * This function is called with hbalock held and the caller must post the
1602 * iocb without releasing the lock. If the caller releases the lock,
1603 * iocb slot returned by the function is not guaranteed to be available.
1604 * The function returns pointer to the next available iocb slot if there
1605 * is available slot in the ring, else it returns NULL.
1606 * If the get index of the ring is ahead of the put index, the function
1607 * will post an error attention event to the worker thread to take the
1608 * HBA to offline state.
1609 **/
dea31012005-04-17 16:05:31 -05001610static IOCB_t *
1611lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1612{
James Smart34b02dc2008-08-24 21:49:55 -04001613 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001614 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001615
1616 lockdep_assert_held(&phba->hbalock);
1617
James Smart7e56aa22012-08-03 12:35:34 -04001618 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1619 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1620 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001621
James Smart7e56aa22012-08-03 12:35:34 -04001622 if (unlikely(pring->sli.sli3.local_getidx ==
1623 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001624
James Smart7e56aa22012-08-03 12:35:34 -04001625 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001626
James Smart7e56aa22012-08-03 12:35:34 -04001627 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001628 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001629 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001630 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001631 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001632 pring->sli.sli3.local_getidx,
1633 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001634
James Smart2e0fef82007-06-17 19:56:36 -05001635 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001636 /*
1637 * All error attention handlers are posted to
1638 * worker thread
1639 */
1640 phba->work_ha |= HA_ERATT;
1641 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001642
James Smart5e9d9b82008-06-14 22:52:53 -04001643 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001644
1645 return NULL;
1646 }
1647
James Smart7e56aa22012-08-03 12:35:34 -04001648 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001649 return NULL;
1650 }
1651
James Smarted957682007-06-17 19:56:37 -05001652 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001653}
1654
James Smarte59058c2008-08-24 21:49:00 -04001655/**
James Smart3621a712009-04-06 18:47:14 -04001656 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001657 * @phba: Pointer to HBA context object.
1658 * @iocbq: Pointer to driver iocb object.
1659 *
1660 * This function gets an iotag for the iocb. If there is no unused iotag and
1661 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1662 * array and assigns a new iotag.
1663 * The function returns the allocated iotag if successful, else returns zero.
1664 * Zero is not a valid iotag.
1665 * The caller is not required to hold any lock.
1666 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001667uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001668lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001669{
James Smart2e0fef82007-06-17 19:56:36 -05001670 struct lpfc_iocbq **new_arr;
1671 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001672 size_t new_len;
1673 struct lpfc_sli *psli = &phba->sli;
1674 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001675
James Smart2e0fef82007-06-17 19:56:36 -05001676 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001677 iotag = psli->last_iotag;
1678 if(++iotag < psli->iocbq_lookup_len) {
1679 psli->last_iotag = iotag;
1680 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001681 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001682 iocbq->iotag = iotag;
1683 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001684 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001685 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1686 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001687 spin_unlock_irq(&phba->hbalock);
1688 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001689 GFP_KERNEL);
1690 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001691 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001692 old_arr = psli->iocbq_lookup;
1693 if (new_len <= psli->iocbq_lookup_len) {
1694 /* highly unprobable case */
1695 kfree(new_arr);
1696 iotag = psli->last_iotag;
1697 if(++iotag < psli->iocbq_lookup_len) {
1698 psli->last_iotag = iotag;
1699 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001700 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001701 iocbq->iotag = iotag;
1702 return iotag;
1703 }
James Smart2e0fef82007-06-17 19:56:36 -05001704 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001705 return 0;
1706 }
1707 if (psli->iocbq_lookup)
1708 memcpy(new_arr, old_arr,
1709 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001710 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001711 psli->iocbq_lookup = new_arr;
1712 psli->iocbq_lookup_len = new_len;
1713 psli->last_iotag = iotag;
1714 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001715 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001716 iocbq->iotag = iotag;
1717 kfree(old_arr);
1718 return iotag;
1719 }
James Smart8f6d98d2006-08-01 07:34:00 -04001720 } else
James Smart2e0fef82007-06-17 19:56:36 -05001721 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001722
James Smartbc739052010-08-04 16:11:18 -04001723 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001724 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1725 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001726
James Bottomley604a3e32005-10-29 10:28:33 -05001727 return 0;
dea31012005-04-17 16:05:31 -05001728}
1729
James Smarte59058c2008-08-24 21:49:00 -04001730/**
James Smart3621a712009-04-06 18:47:14 -04001731 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001732 * @phba: Pointer to HBA context object.
1733 * @pring: Pointer to driver SLI ring object.
1734 * @iocb: Pointer to iocb slot in the ring.
1735 * @nextiocb: Pointer to driver iocb object which need to be
1736 * posted to firmware.
1737 *
1738 * This function is called with hbalock held to post a new iocb to
1739 * the firmware. This function copies the new iocb to ring iocb slot and
1740 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1741 * a completion call back for this iocb else the function will free the
1742 * iocb object.
1743 **/
dea31012005-04-17 16:05:31 -05001744static void
1745lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1746 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1747{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001748 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001749 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001750 * Set up an iotag
dea31012005-04-17 16:05:31 -05001751 */
James Bottomley604a3e32005-10-29 10:28:33 -05001752 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001753
James Smarte2a0a9d2008-12-04 22:40:02 -05001754
James Smarta58cbd52007-08-02 11:09:43 -04001755 if (pring->ringno == LPFC_ELS_RING) {
1756 lpfc_debugfs_slow_ring_trc(phba,
1757 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1758 *(((uint32_t *) &nextiocb->iocb) + 4),
1759 *(((uint32_t *) &nextiocb->iocb) + 6),
1760 *(((uint32_t *) &nextiocb->iocb) + 7));
1761 }
1762
dea31012005-04-17 16:05:31 -05001763 /*
1764 * Issue iocb command to adapter
1765 */
James Smart92d7f7b2007-06-17 19:56:38 -05001766 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001767 wmb();
1768 pring->stats.iocb_cmd++;
1769
1770 /*
1771 * If there is no completion routine to call, we can release the
1772 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1773 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1774 */
1775 if (nextiocb->iocb_cmpl)
1776 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001777 else
James Smart2e0fef82007-06-17 19:56:36 -05001778 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001779
1780 /*
1781 * Let the HBA know what IOCB slot will be the next one the
1782 * driver will put a command into.
1783 */
James Smart7e56aa22012-08-03 12:35:34 -04001784 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1785 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001786}
1787
James Smarte59058c2008-08-24 21:49:00 -04001788/**
James Smart3621a712009-04-06 18:47:14 -04001789 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001790 * @phba: Pointer to HBA context object.
1791 * @pring: Pointer to driver SLI ring object.
1792 *
1793 * The caller is not required to hold any lock for calling this function.
1794 * This function updates the chip attention bits for the ring to inform firmware
1795 * that there are pending work to be done for this ring and requests an
1796 * interrupt when there is space available in the ring. This function is
1797 * called when the driver is unable to post more iocbs to the ring due
1798 * to unavailability of space in the ring.
1799 **/
dea31012005-04-17 16:05:31 -05001800static void
James Smart2e0fef82007-06-17 19:56:36 -05001801lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001802{
1803 int ringno = pring->ringno;
1804
1805 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1806
1807 wmb();
1808
1809 /*
1810 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1811 * The HBA will tell us when an IOCB entry is available.
1812 */
1813 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1814 readl(phba->CAregaddr); /* flush */
1815
1816 pring->stats.iocb_cmd_full++;
1817}
1818
James Smarte59058c2008-08-24 21:49:00 -04001819/**
James Smart3621a712009-04-06 18:47:14 -04001820 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001821 * @phba: Pointer to HBA context object.
1822 * @pring: Pointer to driver SLI ring object.
1823 *
1824 * This function updates the chip attention register bit for the
1825 * given ring to inform HBA that there is more work to be done
1826 * in this ring. The caller is not required to hold any lock.
1827 **/
dea31012005-04-17 16:05:31 -05001828static void
James Smart2e0fef82007-06-17 19:56:36 -05001829lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001830{
1831 int ringno = pring->ringno;
1832
1833 /*
1834 * Tell the HBA that there is work to do in this ring.
1835 */
James Smart34b02dc2008-08-24 21:49:55 -04001836 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1837 wmb();
1838 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1839 readl(phba->CAregaddr); /* flush */
1840 }
dea31012005-04-17 16:05:31 -05001841}
1842
James Smarte59058c2008-08-24 21:49:00 -04001843/**
James Smart3621a712009-04-06 18:47:14 -04001844 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001845 * @phba: Pointer to HBA context object.
1846 * @pring: Pointer to driver SLI ring object.
1847 *
1848 * This function is called with hbalock held to post pending iocbs
1849 * in the txq to the firmware. This function is called when driver
1850 * detects space available in the ring.
1851 **/
dea31012005-04-17 16:05:31 -05001852static void
James Smart2e0fef82007-06-17 19:56:36 -05001853lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001854{
1855 IOCB_t *iocb;
1856 struct lpfc_iocbq *nextiocb;
1857
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001858 lockdep_assert_held(&phba->hbalock);
1859
dea31012005-04-17 16:05:31 -05001860 /*
1861 * Check to see if:
1862 * (a) there is anything on the txq to send
1863 * (b) link is up
1864 * (c) link attention events can be processed (fcp ring only)
1865 * (d) IOCB processing is not blocked by the outstanding mbox command.
1866 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001867
1868 if (lpfc_is_link_up(phba) &&
1869 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001870 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001871 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001872
1873 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1874 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1875 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1876
1877 if (iocb)
1878 lpfc_sli_update_ring(phba, pring);
1879 else
1880 lpfc_sli_update_full_ring(phba, pring);
1881 }
1882
1883 return;
1884}
1885
James Smarte59058c2008-08-24 21:49:00 -04001886/**
James Smart3621a712009-04-06 18:47:14 -04001887 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001888 * @phba: Pointer to HBA context object.
1889 * @hbqno: HBQ number.
1890 *
1891 * This function is called with hbalock held to get the next
1892 * available slot for the given HBQ. If there is free slot
1893 * available for the HBQ it will return pointer to the next available
1894 * HBQ entry else it will return NULL.
1895 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001896static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001897lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1898{
1899 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1900
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001901 lockdep_assert_held(&phba->hbalock);
1902
James Smarted957682007-06-17 19:56:37 -05001903 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1904 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1905 hbqp->next_hbqPutIdx = 0;
1906
1907 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001908 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001909 uint32_t getidx = le32_to_cpu(raw_index);
1910
1911 hbqp->local_hbqGetIdx = getidx;
1912
1913 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1914 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001915 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001916 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001917 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001918 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001919 hbqp->entry_count);
1920
1921 phba->link_state = LPFC_HBA_ERROR;
1922 return NULL;
1923 }
1924
1925 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1926 return NULL;
1927 }
1928
James Smart51ef4c22007-08-02 11:10:31 -04001929 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1930 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001931}
1932
James Smarte59058c2008-08-24 21:49:00 -04001933/**
James Smart3621a712009-04-06 18:47:14 -04001934 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001935 * @phba: Pointer to HBA context object.
1936 *
1937 * This function is called with no lock held to free all the
1938 * hbq buffers while uninitializing the SLI interface. It also
1939 * frees the HBQ buffers returned by the firmware but not yet
1940 * processed by the upper layers.
1941 **/
James Smarted957682007-06-17 19:56:37 -05001942void
1943lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1944{
James Smart92d7f7b2007-06-17 19:56:38 -05001945 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1946 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001947 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001948 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001949
James Smart51ef4c22007-08-02 11:10:31 -04001950 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001951 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001952 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001953 for (i = 0; i < hbq_count; ++i) {
1954 list_for_each_entry_safe(dmabuf, next_dmabuf,
1955 &phba->hbqs[i].hbq_buffer_list, list) {
1956 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1957 list_del(&hbq_buf->dbuf.list);
1958 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1959 }
James Smarta8adb832007-10-27 13:37:53 -04001960 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001961 }
James Smart3163f722008-02-08 18:50:25 -05001962
1963 /* Mark the HBQs not in use */
1964 phba->hbq_in_use = 0;
1965 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001966}
1967
James Smarte59058c2008-08-24 21:49:00 -04001968/**
James Smart3621a712009-04-06 18:47:14 -04001969 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001970 * @phba: Pointer to HBA context object.
1971 * @hbqno: HBQ number.
1972 * @hbq_buf: Pointer to HBQ buffer.
1973 *
1974 * This function is called with the hbalock held to post a
1975 * hbq buffer to the firmware. If the function finds an empty
1976 * slot in the HBQ, it will post the buffer. The function will return
1977 * pointer to the hbq entry if it successfully post the buffer
1978 * else it will return NULL.
1979 **/
James Smart3772a992009-05-22 14:50:54 -04001980static int
James Smarted957682007-06-17 19:56:37 -05001981lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001982 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001983{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001984 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04001985 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1986}
1987
1988/**
1989 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1990 * @phba: Pointer to HBA context object.
1991 * @hbqno: HBQ number.
1992 * @hbq_buf: Pointer to HBQ buffer.
1993 *
1994 * This function is called with the hbalock held to post a hbq buffer to the
1995 * firmware. If the function finds an empty slot in the HBQ, it will post the
1996 * buffer and place it on the hbq_buffer_list. The function will return zero if
1997 * it successfully post the buffer else it will return an error.
1998 **/
1999static int
2000lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2001 struct hbq_dmabuf *hbq_buf)
2002{
James Smarted957682007-06-17 19:56:37 -05002003 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05002004 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05002005
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002006 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05002007 /* Get next HBQ entry slot to use */
2008 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2009 if (hbqe) {
2010 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2011
James Smart92d7f7b2007-06-17 19:56:38 -05002012 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2013 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08002014 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05002015 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002016 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2017 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2018 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05002019 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2020 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05002021 /* flush */
James Smarted957682007-06-17 19:56:37 -05002022 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04002023 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04002024 return 0;
2025 } else
2026 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05002027}
2028
James Smart4f774512009-05-22 14:52:35 -04002029/**
2030 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2031 * @phba: Pointer to HBA context object.
2032 * @hbqno: HBQ number.
2033 * @hbq_buf: Pointer to HBQ buffer.
2034 *
2035 * This function is called with the hbalock held to post an RQE to the SLI4
2036 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2037 * the hbq_buffer_list and return zero, otherwise it will return an error.
2038 **/
2039static int
2040lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2041 struct hbq_dmabuf *hbq_buf)
2042{
2043 int rc;
2044 struct lpfc_rqe hrqe;
2045 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08002046 struct lpfc_queue *hrq;
2047 struct lpfc_queue *drq;
2048
2049 if (hbqno != LPFC_ELS_HBQ)
2050 return 1;
2051 hrq = phba->sli4_hba.hdr_rq;
2052 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04002053
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002054 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04002055 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2056 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2057 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2058 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08002059 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04002060 if (rc < 0)
2061 return rc;
James Smart895427b2017-02-12 13:52:30 -08002062 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04002063 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2064 return 0;
2065}
2066
James Smarte59058c2008-08-24 21:49:00 -04002067/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05002068static struct lpfc_hbq_init lpfc_els_hbq = {
2069 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05002070 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05002071 .mask_count = 0,
2072 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04002073 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05002074 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04002075 .init_count = 40,
2076 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05002077};
James Smarted957682007-06-17 19:56:37 -05002078
James Smarte59058c2008-08-24 21:49:00 -04002079/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04002080struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05002081 &lpfc_els_hbq,
2082};
2083
James Smarte59058c2008-08-24 21:49:00 -04002084/**
James Smart3621a712009-04-06 18:47:14 -04002085 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04002086 * @phba: Pointer to HBA context object.
2087 * @hbqno: HBQ number.
2088 * @count: Number of HBQ buffers to be posted.
2089 *
James Smartd7c255b2008-08-24 21:50:00 -04002090 * This function is called with no lock held to post more hbq buffers to the
2091 * given HBQ. The function returns the number of HBQ buffers successfully
2092 * posted.
James Smarte59058c2008-08-24 21:49:00 -04002093 **/
James Smart311464e2007-08-02 11:10:37 -04002094static int
James Smart92d7f7b2007-06-17 19:56:38 -05002095lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2096{
James Smartd7c255b2008-08-24 21:50:00 -04002097 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05002098 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05002099 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04002100 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002101 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04002102 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04002103
James Smartd7c255b2008-08-24 21:50:00 -04002104 if ((phba->hbqs[hbqno].buffer_count + count) >
2105 lpfc_hbq_defs[hbqno]->entry_count)
2106 count = lpfc_hbq_defs[hbqno]->entry_count -
2107 phba->hbqs[hbqno].buffer_count;
2108 if (!count)
2109 return 0;
2110 /* Allocate HBQ entries */
2111 for (i = 0; i < count; i++) {
2112 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2113 if (!hbq_buffer)
2114 break;
2115 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2116 }
James Smart3163f722008-02-08 18:50:25 -05002117 /* Check whether HBQ is still in use */
2118 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002119 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04002120 goto err;
2121 while (!list_empty(&hbq_buf_list)) {
2122 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2123 dbuf.list);
2124 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2125 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04002126 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04002127 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04002128 posted++;
2129 } else
James Smart51ef4c22007-08-02 11:10:31 -04002130 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05002131 }
James Smart3163f722008-02-08 18:50:25 -05002132 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04002133 return posted;
2134err:
2135 spin_unlock_irqrestore(&phba->hbalock, flags);
2136 while (!list_empty(&hbq_buf_list)) {
2137 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2138 dbuf.list);
2139 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2140 }
James Smart92d7f7b2007-06-17 19:56:38 -05002141 return 0;
James Smarted957682007-06-17 19:56:37 -05002142}
2143
James Smarte59058c2008-08-24 21:49:00 -04002144/**
James Smart3621a712009-04-06 18:47:14 -04002145 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04002146 * @phba: Pointer to HBA context object.
2147 * @qno: HBQ number.
2148 *
2149 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04002150 * is called with no lock held. The function returns the number of HBQ entries
2151 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002152 **/
James Smarted957682007-06-17 19:56:37 -05002153int
James Smart92d7f7b2007-06-17 19:56:38 -05002154lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002155{
James Smartdef9c7a2009-12-21 17:02:28 -05002156 if (phba->sli_rev == LPFC_SLI_REV4)
2157 return 0;
2158 else
2159 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2160 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05002161}
2162
James Smarte59058c2008-08-24 21:49:00 -04002163/**
James Smart3621a712009-04-06 18:47:14 -04002164 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002165 * @phba: Pointer to HBA context object.
2166 * @qno: HBQ queue number.
2167 *
2168 * This function is called from SLI initialization code path with
2169 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002170 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002171 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002172static int
James Smart92d7f7b2007-06-17 19:56:38 -05002173lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002174{
James Smartdef9c7a2009-12-21 17:02:28 -05002175 if (phba->sli_rev == LPFC_SLI_REV4)
2176 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002177 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002178 else
2179 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2180 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002181}
2182
James Smarte59058c2008-08-24 21:49:00 -04002183/**
James Smart3772a992009-05-22 14:50:54 -04002184 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2185 * @phba: Pointer to HBA context object.
2186 * @hbqno: HBQ number.
2187 *
2188 * This function removes the first hbq buffer on an hbq list and returns a
2189 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2190 **/
2191static struct hbq_dmabuf *
2192lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2193{
2194 struct lpfc_dmabuf *d_buf;
2195
2196 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2197 if (!d_buf)
2198 return NULL;
2199 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2200}
2201
2202/**
James Smart2d7dbc42017-02-12 13:52:35 -08002203 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2204 * @phba: Pointer to HBA context object.
2205 * @hbqno: HBQ number.
2206 *
2207 * This function removes the first RQ buffer on an RQ buffer list and returns a
2208 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2209 **/
2210static struct rqb_dmabuf *
2211lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2212{
2213 struct lpfc_dmabuf *h_buf;
2214 struct lpfc_rqb *rqbp;
2215
2216 rqbp = hrq->rqbp;
2217 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2218 struct lpfc_dmabuf, list);
2219 if (!h_buf)
2220 return NULL;
2221 rqbp->buffer_count--;
2222 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2223}
2224
2225/**
James Smart3621a712009-04-06 18:47:14 -04002226 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002227 * @phba: Pointer to HBA context object.
2228 * @tag: Tag of the hbq buffer.
2229 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002230 * This function searches for the hbq buffer associated with the given tag in
2231 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2232 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002233 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002234static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002235lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2236{
James Smart92d7f7b2007-06-17 19:56:38 -05002237 struct lpfc_dmabuf *d_buf;
2238 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002239 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002240
James Smart51ef4c22007-08-02 11:10:31 -04002241 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002242 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002243 return NULL;
2244
James Smart3772a992009-05-22 14:50:54 -04002245 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002246 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002247 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002248 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002249 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002250 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002251 }
2252 }
James Smart3772a992009-05-22 14:50:54 -04002253 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002254 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002255 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002256 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002257 return NULL;
James Smarted957682007-06-17 19:56:37 -05002258}
2259
James Smarte59058c2008-08-24 21:49:00 -04002260/**
James Smart3621a712009-04-06 18:47:14 -04002261 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002262 * @phba: Pointer to HBA context object.
2263 * @hbq_buffer: Pointer to HBQ buffer.
2264 *
2265 * This function is called with hbalock. This function gives back
2266 * the hbq buffer to firmware. If the HBQ does not have space to
2267 * post the buffer, it will free the buffer.
2268 **/
James Smarted957682007-06-17 19:56:37 -05002269void
James Smart51ef4c22007-08-02 11:10:31 -04002270lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002271{
2272 uint32_t hbqno;
2273
James Smart51ef4c22007-08-02 11:10:31 -04002274 if (hbq_buffer) {
2275 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002276 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002277 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002278 }
2279}
2280
James Smarte59058c2008-08-24 21:49:00 -04002281/**
James Smart3621a712009-04-06 18:47:14 -04002282 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002283 * @mbxCommand: mailbox command code.
2284 *
2285 * This function is called by the mailbox event handler function to verify
2286 * that the completed mailbox command is a legitimate mailbox command. If the
2287 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2288 * and the mailbox event handler will take the HBA offline.
2289 **/
dea31012005-04-17 16:05:31 -05002290static int
2291lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2292{
2293 uint8_t ret;
2294
2295 switch (mbxCommand) {
2296 case MBX_LOAD_SM:
2297 case MBX_READ_NV:
2298 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002299 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002300 case MBX_RUN_BIU_DIAG:
2301 case MBX_INIT_LINK:
2302 case MBX_DOWN_LINK:
2303 case MBX_CONFIG_LINK:
2304 case MBX_CONFIG_RING:
2305 case MBX_RESET_RING:
2306 case MBX_READ_CONFIG:
2307 case MBX_READ_RCONFIG:
2308 case MBX_READ_SPARM:
2309 case MBX_READ_STATUS:
2310 case MBX_READ_RPI:
2311 case MBX_READ_XRI:
2312 case MBX_READ_REV:
2313 case MBX_READ_LNK_STAT:
2314 case MBX_REG_LOGIN:
2315 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002316 case MBX_CLEAR_LA:
2317 case MBX_DUMP_MEMORY:
2318 case MBX_DUMP_CONTEXT:
2319 case MBX_RUN_DIAGS:
2320 case MBX_RESTART:
2321 case MBX_UPDATE_CFG:
2322 case MBX_DOWN_LOAD:
2323 case MBX_DEL_LD_ENTRY:
2324 case MBX_RUN_PROGRAM:
2325 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002326 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002327 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002328 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002329 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002330 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002331 case MBX_LOAD_AREA:
2332 case MBX_RUN_BIU_DIAG64:
2333 case MBX_CONFIG_PORT:
2334 case MBX_READ_SPARM64:
2335 case MBX_READ_RPI64:
2336 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002337 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002338 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002339 case MBX_SET_DEBUG:
2340 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002341 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002342 case MBX_REG_VPI:
2343 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002344 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002345 case MBX_PORT_CAPABILITIES:
2346 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002347 case MBX_SLI4_CONFIG:
2348 case MBX_SLI4_REQ_FTRS:
2349 case MBX_REG_FCFI:
2350 case MBX_UNREG_FCFI:
2351 case MBX_REG_VFI:
2352 case MBX_UNREG_VFI:
2353 case MBX_INIT_VPI:
2354 case MBX_INIT_VFI:
2355 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002356 case MBX_READ_EVENT_LOG_STATUS:
2357 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002358 case MBX_SECURITY_MGMT:
2359 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002360 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002361 ret = mbxCommand;
2362 break;
2363 default:
2364 ret = MBX_SHUTDOWN;
2365 break;
2366 }
James Smart2e0fef82007-06-17 19:56:36 -05002367 return ret;
dea31012005-04-17 16:05:31 -05002368}
James Smarte59058c2008-08-24 21:49:00 -04002369
2370/**
James Smart3621a712009-04-06 18:47:14 -04002371 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002372 * @phba: Pointer to HBA context object.
2373 * @pmboxq: Pointer to mailbox command.
2374 *
2375 * This is completion handler function for mailbox commands issued from
2376 * lpfc_sli_issue_mbox_wait function. This function is called by the
2377 * mailbox event handler function with no lock held. This function
2378 * will wake up thread waiting on the wait queue pointed by context1
2379 * of the mailbox.
2380 **/
James Smart04c68492009-05-22 14:52:52 -04002381void
James Smart2e0fef82007-06-17 19:56:36 -05002382lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002383{
2384 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002385 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002386
2387 /*
2388 * If pdone_q is empty, the driver thread gave up waiting and
2389 * continued running.
2390 */
James Smart7054a602007-04-25 09:52:34 -04002391 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002392 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002393 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2394 if (pdone_q)
2395 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002396 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002397 return;
2398}
2399
James Smarte59058c2008-08-24 21:49:00 -04002400
2401/**
James Smart3621a712009-04-06 18:47:14 -04002402 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002403 * @phba: Pointer to HBA context object.
2404 * @pmb: Pointer to mailbox object.
2405 *
2406 * This function is the default mailbox completion handler. It
2407 * frees the memory resources associated with the completed mailbox
2408 * command. If the completed command is a REG_LOGIN mailbox command,
2409 * this function will issue a UREG_LOGIN to re-claim the RPI.
2410 **/
dea31012005-04-17 16:05:31 -05002411void
James Smart2e0fef82007-06-17 19:56:36 -05002412lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002413{
James Smartd439d282010-09-29 11:18:45 -04002414 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002415 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002416 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002417 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002418 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002419 int rc;
2420
dea31012005-04-17 16:05:31 -05002421 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002422
dea31012005-04-17 16:05:31 -05002423 if (mp) {
2424 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2425 kfree(mp);
2426 }
James Smart7054a602007-04-25 09:52:34 -04002427
2428 /*
2429 * If a REG_LOGIN succeeded after node is destroyed or node
2430 * is in re-discovery driver need to cleanup the RPI.
2431 */
James Smart2e0fef82007-06-17 19:56:36 -05002432 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002433 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2434 !pmb->u.mb.mbxStatus) {
2435 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002436 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002437 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002438 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002439 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002440 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2441 if (rc != MBX_NOT_FINISHED)
2442 return;
2443 }
2444
James Smart695a8142010-01-26 23:08:03 -05002445 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2446 !(phba->pport->load_flag & FC_UNLOADING) &&
2447 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002448 shost = lpfc_shost_from_vport(vport);
2449 spin_lock_irq(shost->host_lock);
2450 vport->vpi_state |= LPFC_VPI_REGISTERED;
2451 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2452 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002453 }
2454
James Smartd439d282010-09-29 11:18:45 -04002455 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2456 ndlp = (struct lpfc_nodelist *)pmb->context2;
2457 lpfc_nlp_put(ndlp);
2458 pmb->context2 = NULL;
2459 }
2460
James Smartdcf2a4e2010-09-29 11:18:53 -04002461 /* Check security permission status on INIT_LINK mailbox command */
2462 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2463 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2464 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2465 "2860 SLI authentication is required "
2466 "for INIT_LINK but has not done yet\n");
2467
James Smart04c68492009-05-22 14:52:52 -04002468 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2469 lpfc_sli4_mbox_cmd_free(phba, pmb);
2470 else
2471 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002472}
James Smartbe6bb942015-04-07 15:07:22 -04002473 /**
2474 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2475 * @phba: Pointer to HBA context object.
2476 * @pmb: Pointer to mailbox object.
2477 *
2478 * This function is the unreg rpi mailbox completion handler. It
2479 * frees the memory resources associated with the completed mailbox
2480 * command. An additional refrenece is put on the ndlp to prevent
2481 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2482 * the unreg mailbox command completes, this routine puts the
2483 * reference back.
2484 *
2485 **/
2486void
2487lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2488{
2489 struct lpfc_vport *vport = pmb->vport;
2490 struct lpfc_nodelist *ndlp;
2491
2492 ndlp = pmb->context1;
2493 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2494 if (phba->sli_rev == LPFC_SLI_REV4 &&
2495 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002496 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002497 LPFC_SLI_INTF_IF_TYPE_2)) {
2498 if (ndlp) {
2499 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2500 "0010 UNREG_LOGIN vpi:%x "
2501 "rpi:%x DID:%x map:%x %p\n",
2502 vport->vpi, ndlp->nlp_rpi,
2503 ndlp->nlp_DID,
2504 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002505 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002506 lpfc_nlp_put(ndlp);
2507 }
2508 }
2509 }
2510
2511 mempool_free(pmb, phba->mbox_mem_pool);
2512}
dea31012005-04-17 16:05:31 -05002513
James Smarte59058c2008-08-24 21:49:00 -04002514/**
James Smart3621a712009-04-06 18:47:14 -04002515 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002516 * @phba: Pointer to HBA context object.
2517 *
2518 * This function is called with no lock held. This function processes all
2519 * the completed mailbox commands and gives it to upper layers. The interrupt
2520 * service routine processes mailbox completion interrupt and adds completed
2521 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2522 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2523 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2524 * function returns the mailbox commands to the upper layer by calling the
2525 * completion handler function of each mailbox.
2526 **/
dea31012005-04-17 16:05:31 -05002527int
James Smart2e0fef82007-06-17 19:56:36 -05002528lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002529{
James Smart92d7f7b2007-06-17 19:56:38 -05002530 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002531 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002532 int rc;
2533 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002534
2535 phba->sli.slistat.mbox_event++;
2536
James Smart92d7f7b2007-06-17 19:56:38 -05002537 /* Get all completed mailboxe buffers into the cmplq */
2538 spin_lock_irq(&phba->hbalock);
2539 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2540 spin_unlock_irq(&phba->hbalock);
2541
dea31012005-04-17 16:05:31 -05002542 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002543 do {
2544 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2545 if (pmb == NULL)
2546 break;
2547
James Smart04c68492009-05-22 14:52:52 -04002548 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002549
James Smart858c9f62007-06-17 19:56:39 -05002550 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2551 if (pmb->vport) {
2552 lpfc_debugfs_disc_trc(pmb->vport,
2553 LPFC_DISC_TRC_MBOX_VPORT,
2554 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2555 (uint32_t)pmbox->mbxCommand,
2556 pmbox->un.varWords[0],
2557 pmbox->un.varWords[1]);
2558 }
2559 else {
2560 lpfc_debugfs_disc_trc(phba->pport,
2561 LPFC_DISC_TRC_MBOX,
2562 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2563 (uint32_t)pmbox->mbxCommand,
2564 pmbox->un.varWords[0],
2565 pmbox->un.varWords[1]);
2566 }
2567 }
2568
dea31012005-04-17 16:05:31 -05002569 /*
2570 * It is a fatal error if unknown mbox command completion.
2571 */
2572 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2573 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002574 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002575 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002576 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002577 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002578 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002579 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002580 lpfc_sli_config_mbox_subsys_get(phba,
2581 pmb),
2582 lpfc_sli_config_mbox_opcode_get(phba,
2583 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002584 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002585 phba->work_hs = HS_FFER3;
2586 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002587 continue;
dea31012005-04-17 16:05:31 -05002588 }
2589
dea31012005-04-17 16:05:31 -05002590 if (pmbox->mbxStatus) {
2591 phba->sli.slistat.mbox_stat_err++;
2592 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2593 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002594 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002595 LOG_MBOX | LOG_SLI,
2596 "(%d):0305 Mbox cmd cmpl "
2597 "error - RETRYing Data: x%x "
2598 "(x%x/x%x) x%x x%x x%x\n",
2599 pmb->vport ? pmb->vport->vpi : 0,
2600 pmbox->mbxCommand,
2601 lpfc_sli_config_mbox_subsys_get(phba,
2602 pmb),
2603 lpfc_sli_config_mbox_opcode_get(phba,
2604 pmb),
2605 pmbox->mbxStatus,
2606 pmbox->un.varWords[0],
2607 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002608 pmbox->mbxStatus = 0;
2609 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002610 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002611 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002612 continue;
dea31012005-04-17 16:05:31 -05002613 }
2614 }
2615
2616 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002617 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002618 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002619 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2620 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002621 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002622 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002623 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2624 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002625 pmb->mbox_cmpl,
2626 *((uint32_t *) pmbox),
2627 pmbox->un.varWords[0],
2628 pmbox->un.varWords[1],
2629 pmbox->un.varWords[2],
2630 pmbox->un.varWords[3],
2631 pmbox->un.varWords[4],
2632 pmbox->un.varWords[5],
2633 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002634 pmbox->un.varWords[7],
2635 pmbox->un.varWords[8],
2636 pmbox->un.varWords[9],
2637 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002638
James Smart92d7f7b2007-06-17 19:56:38 -05002639 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002640 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002641 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002642 return 0;
dea31012005-04-17 16:05:31 -05002643}
James Smart92d7f7b2007-06-17 19:56:38 -05002644
James Smarte59058c2008-08-24 21:49:00 -04002645/**
James Smart3621a712009-04-06 18:47:14 -04002646 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002647 * @phba: Pointer to HBA context object.
2648 * @pring: Pointer to driver SLI ring object.
2649 * @tag: buffer tag.
2650 *
2651 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2652 * is set in the tag the buffer is posted for a particular exchange,
2653 * the function will return the buffer without replacing the buffer.
2654 * If the buffer is for unsolicited ELS or CT traffic, this function
2655 * returns the buffer and also posts another buffer to the firmware.
2656 **/
James Smart76bb24e2007-10-27 13:38:00 -04002657static struct lpfc_dmabuf *
2658lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002659 struct lpfc_sli_ring *pring,
2660 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002661{
James Smart9f1e1b52008-12-04 22:39:40 -05002662 struct hbq_dmabuf *hbq_entry;
2663
James Smart76bb24e2007-10-27 13:38:00 -04002664 if (tag & QUE_BUFTAG_BIT)
2665 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002666 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2667 if (!hbq_entry)
2668 return NULL;
2669 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002670}
James Smart57127f12007-10-27 13:37:05 -04002671
James Smart3772a992009-05-22 14:50:54 -04002672/**
2673 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2674 * @phba: Pointer to HBA context object.
2675 * @pring: Pointer to driver SLI ring object.
2676 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2677 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2678 * @fch_type: the type for the first frame of the sequence.
2679 *
2680 * This function is called with no lock held. This function uses the r_ctl and
2681 * type of the received sequence to find the correct callback function to call
2682 * to process the sequence.
2683 **/
2684static int
2685lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2686 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2687 uint32_t fch_type)
2688{
2689 int i;
2690
James Smartf358dd02017-02-12 13:52:34 -08002691 switch (fch_type) {
2692 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002693 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002694 return 1;
2695 default:
2696 break;
2697 }
2698
James Smart3772a992009-05-22 14:50:54 -04002699 /* unSolicited Responses */
2700 if (pring->prt[0].profile) {
2701 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2702 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2703 saveq);
2704 return 1;
2705 }
2706 /* We must search, based on rctl / type
2707 for the right routine */
2708 for (i = 0; i < pring->num_mask; i++) {
2709 if ((pring->prt[i].rctl == fch_r_ctl) &&
2710 (pring->prt[i].type == fch_type)) {
2711 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2712 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2713 (phba, pring, saveq);
2714 return 1;
2715 }
2716 }
2717 return 0;
2718}
James Smarte59058c2008-08-24 21:49:00 -04002719
2720/**
James Smart3621a712009-04-06 18:47:14 -04002721 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002722 * @phba: Pointer to HBA context object.
2723 * @pring: Pointer to driver SLI ring object.
2724 * @saveq: Pointer to the unsolicited iocb.
2725 *
2726 * This function is called with no lock held by the ring event handler
2727 * when there is an unsolicited iocb posted to the response ring by the
2728 * firmware. This function gets the buffer associated with the iocbs
2729 * and calls the event handler for the ring. This function handles both
2730 * qring buffers and hbq buffers.
2731 * When the function returns 1 the caller can free the iocb object otherwise
2732 * upper layer functions will free the iocb objects.
2733 **/
dea31012005-04-17 16:05:31 -05002734static int
2735lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2736 struct lpfc_iocbq *saveq)
2737{
2738 IOCB_t * irsp;
2739 WORD5 * w5p;
2740 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002741 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002742 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002743
dea31012005-04-17 16:05:31 -05002744 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002745
2746 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2747 if (pring->lpfc_sli_rcv_async_status)
2748 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2749 else
2750 lpfc_printf_log(phba,
2751 KERN_WARNING,
2752 LOG_SLI,
2753 "0316 Ring %d handler: unexpected "
2754 "ASYNC_STATUS iocb received evt_code "
2755 "0x%x\n",
2756 pring->ringno,
2757 irsp->un.asyncstat.evt_code);
2758 return 1;
2759 }
2760
James Smart3163f722008-02-08 18:50:25 -05002761 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2762 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2763 if (irsp->ulpBdeCount > 0) {
2764 dmzbuf = lpfc_sli_get_buff(phba, pring,
2765 irsp->un.ulpWord[3]);
2766 lpfc_in_buf_free(phba, dmzbuf);
2767 }
2768
2769 if (irsp->ulpBdeCount > 1) {
2770 dmzbuf = lpfc_sli_get_buff(phba, pring,
2771 irsp->unsli3.sli3Words[3]);
2772 lpfc_in_buf_free(phba, dmzbuf);
2773 }
2774
2775 if (irsp->ulpBdeCount > 2) {
2776 dmzbuf = lpfc_sli_get_buff(phba, pring,
2777 irsp->unsli3.sli3Words[7]);
2778 lpfc_in_buf_free(phba, dmzbuf);
2779 }
2780
2781 return 1;
2782 }
2783
James Smart92d7f7b2007-06-17 19:56:38 -05002784 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002785 if (irsp->ulpBdeCount != 0) {
2786 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002787 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002788 if (!saveq->context2)
2789 lpfc_printf_log(phba,
2790 KERN_ERR,
2791 LOG_SLI,
2792 "0341 Ring %d Cannot find buffer for "
2793 "an unsolicited iocb. tag 0x%x\n",
2794 pring->ringno,
2795 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002796 }
2797 if (irsp->ulpBdeCount == 2) {
2798 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002799 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002800 if (!saveq->context3)
2801 lpfc_printf_log(phba,
2802 KERN_ERR,
2803 LOG_SLI,
2804 "0342 Ring %d Cannot find buffer for an"
2805 " unsolicited iocb. tag 0x%x\n",
2806 pring->ringno,
2807 irsp->unsli3.sli3Words[7]);
2808 }
2809 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002810 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002811 if (irsp->ulpBdeCount != 0) {
2812 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2813 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002814 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002815 lpfc_printf_log(phba,
2816 KERN_ERR,
2817 LOG_SLI,
2818 "0343 Ring %d Cannot find "
2819 "buffer for an unsolicited iocb"
2820 ". tag 0x%x\n", pring->ringno,
2821 irsp->un.ulpWord[3]);
2822 }
2823 if (irsp->ulpBdeCount == 2) {
2824 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2825 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002826 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002827 lpfc_printf_log(phba,
2828 KERN_ERR,
2829 LOG_SLI,
2830 "0344 Ring %d Cannot find "
2831 "buffer for an unsolicited "
2832 "iocb. tag 0x%x\n",
2833 pring->ringno,
2834 irsp->unsli3.sli3Words[7]);
2835 }
2836 }
James Smart92d7f7b2007-06-17 19:56:38 -05002837 }
James Smart9c2face2008-01-11 01:53:18 -05002838 if (irsp->ulpBdeCount != 0 &&
2839 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2840 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2841 int found = 0;
2842
2843 /* search continue save q for same XRI */
2844 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002845 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2846 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002847 list_add_tail(&saveq->list, &iocbq->list);
2848 found = 1;
2849 break;
2850 }
2851 }
2852 if (!found)
2853 list_add_tail(&saveq->clist,
2854 &pring->iocb_continue_saveq);
2855 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2856 list_del_init(&iocbq->clist);
2857 saveq = iocbq;
2858 irsp = &(saveq->iocb);
2859 } else
2860 return 0;
2861 }
2862 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2863 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2864 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002865 Rctl = FC_RCTL_ELS_REQ;
2866 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002867 } else {
2868 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2869 Rctl = w5p->hcsw.Rctl;
2870 Type = w5p->hcsw.Type;
2871
2872 /* Firmware Workaround */
2873 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2874 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2875 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002876 Rctl = FC_RCTL_ELS_REQ;
2877 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002878 w5p->hcsw.Rctl = Rctl;
2879 w5p->hcsw.Type = Type;
2880 }
2881 }
James Smart92d7f7b2007-06-17 19:56:38 -05002882
James Smart3772a992009-05-22 14:50:54 -04002883 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002884 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002885 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002886 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002887 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002888
James Smart92d7f7b2007-06-17 19:56:38 -05002889 return 1;
dea31012005-04-17 16:05:31 -05002890}
2891
James Smarte59058c2008-08-24 21:49:00 -04002892/**
James Smart3621a712009-04-06 18:47:14 -04002893 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002894 * @phba: Pointer to HBA context object.
2895 * @pring: Pointer to driver SLI ring object.
2896 * @prspiocb: Pointer to response iocb object.
2897 *
2898 * This function looks up the iocb_lookup table to get the command iocb
2899 * corresponding to the given response iocb using the iotag of the
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002900 * response iocb. This function is called with the hbalock held
2901 * for sli3 devices or the ring_lock for sli4 devices.
James Smarte59058c2008-08-24 21:49:00 -04002902 * This function returns the command iocb object if it finds the command
2903 * iocb else returns NULL.
2904 **/
dea31012005-04-17 16:05:31 -05002905static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002906lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2907 struct lpfc_sli_ring *pring,
2908 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002909{
dea31012005-04-17 16:05:31 -05002910 struct lpfc_iocbq *cmd_iocb = NULL;
2911 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002912 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002913
James Bottomley604a3e32005-10-29 10:28:33 -05002914 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002915
James Bottomley604a3e32005-10-29 10:28:33 -05002916 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2917 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002918 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002919 /* remove from txcmpl queue list */
2920 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002921 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart89533e92016-10-13 15:06:15 -07002922 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002923 }
dea31012005-04-17 16:05:31 -05002924 }
2925
dea31012005-04-17 16:05:31 -05002926 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002927 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05002928 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002929 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002930 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002931 return NULL;
2932}
2933
James Smarte59058c2008-08-24 21:49:00 -04002934/**
James Smart3772a992009-05-22 14:50:54 -04002935 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2936 * @phba: Pointer to HBA context object.
2937 * @pring: Pointer to driver SLI ring object.
2938 * @iotag: IOCB tag.
2939 *
2940 * This function looks up the iocb_lookup table to get the command iocb
2941 * corresponding to the given iotag. This function is called with the
2942 * hbalock held.
2943 * This function returns the command iocb object if it finds the command
2944 * iocb else returns NULL.
2945 **/
2946static struct lpfc_iocbq *
2947lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2948 struct lpfc_sli_ring *pring, uint16_t iotag)
2949{
James Smart895427b2017-02-12 13:52:30 -08002950 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04002951
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002952 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002953 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2954 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002955 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2956 /* remove from txcmpl queue list */
2957 list_del_init(&cmd_iocb->list);
2958 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002959 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002960 }
James Smart3772a992009-05-22 14:50:54 -04002961 }
James Smart89533e92016-10-13 15:06:15 -07002962
James Smart3772a992009-05-22 14:50:54 -04002963 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08002964 "0372 iotag x%x lookup error: max iotag (x%x) "
2965 "iocb_flag x%x\n",
2966 iotag, phba->sli.last_iotag,
2967 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04002968 return NULL;
2969}
2970
2971/**
James Smart3621a712009-04-06 18:47:14 -04002972 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002973 * @phba: Pointer to HBA context object.
2974 * @pring: Pointer to driver SLI ring object.
2975 * @saveq: Pointer to the response iocb to be processed.
2976 *
2977 * This function is called by the ring event handler for non-fcp
2978 * rings when there is a new response iocb in the response ring.
2979 * The caller is not required to hold any locks. This function
2980 * gets the command iocb associated with the response iocb and
2981 * calls the completion handler for the command iocb. If there
2982 * is no completion handler, the function will free the resources
2983 * associated with command iocb. If the response iocb is for
2984 * an already aborted command iocb, the status of the completion
2985 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2986 * This function always returns 1.
2987 **/
dea31012005-04-17 16:05:31 -05002988static int
James Smart2e0fef82007-06-17 19:56:36 -05002989lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002990 struct lpfc_iocbq *saveq)
2991{
James Smart2e0fef82007-06-17 19:56:36 -05002992 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002993 int rc = 1;
2994 unsigned long iflag;
2995
2996 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002997 if (phba->sli_rev == LPFC_SLI_REV4)
2998 spin_lock_irqsave(&pring->ring_lock, iflag);
2999 else
3000 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003001 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003002 if (phba->sli_rev == LPFC_SLI_REV4)
3003 spin_unlock_irqrestore(&pring->ring_lock, iflag);
3004 else
3005 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart2e0fef82007-06-17 19:56:36 -05003006
dea31012005-04-17 16:05:31 -05003007 if (cmdiocbp) {
3008 if (cmdiocbp->iocb_cmpl) {
3009 /*
James Smartea2151b2008-09-07 11:52:10 -04003010 * If an ELS command failed send an event to mgmt
3011 * application.
3012 */
3013 if (saveq->iocb.ulpStatus &&
3014 (pring->ringno == LPFC_ELS_RING) &&
3015 (cmdiocbp->iocb.ulpCommand ==
3016 CMD_ELS_REQUEST64_CR))
3017 lpfc_send_els_failure_event(phba,
3018 cmdiocbp, saveq);
3019
3020 /*
dea31012005-04-17 16:05:31 -05003021 * Post all ELS completions to the worker thread.
3022 * All other are passed to the completion callback.
3023 */
3024 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05003025 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3026 (cmdiocbp->iocb_flag &
3027 LPFC_DRIVER_ABORTED)) {
3028 spin_lock_irqsave(&phba->hbalock,
3029 iflag);
James Smart07951072007-04-25 09:51:38 -04003030 cmdiocbp->iocb_flag &=
3031 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05003032 spin_unlock_irqrestore(&phba->hbalock,
3033 iflag);
James Smart07951072007-04-25 09:51:38 -04003034 saveq->iocb.ulpStatus =
3035 IOSTAT_LOCAL_REJECT;
3036 saveq->iocb.un.ulpWord[4] =
3037 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05003038
3039 /* Firmware could still be in progress
3040 * of DMAing payload, so don't free data
3041 * buffer till after a hbeat.
3042 */
James Smart341af102010-01-26 23:07:37 -05003043 spin_lock_irqsave(&phba->hbalock,
3044 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05003045 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05003046 spin_unlock_irqrestore(&phba->hbalock,
3047 iflag);
3048 }
James Smart0f65ff62010-02-26 14:14:23 -05003049 if (phba->sli_rev == LPFC_SLI_REV4) {
3050 if (saveq->iocb_flag &
3051 LPFC_EXCHANGE_BUSY) {
3052 /* Set cmdiocb flag for the
3053 * exchange busy so sgl (xri)
3054 * will not be released until
3055 * the abort xri is received
3056 * from hba.
3057 */
3058 spin_lock_irqsave(
3059 &phba->hbalock, iflag);
3060 cmdiocbp->iocb_flag |=
3061 LPFC_EXCHANGE_BUSY;
3062 spin_unlock_irqrestore(
3063 &phba->hbalock, iflag);
3064 }
3065 if (cmdiocbp->iocb_flag &
3066 LPFC_DRIVER_ABORTED) {
3067 /*
3068 * Clear LPFC_DRIVER_ABORTED
3069 * bit in case it was driver
3070 * initiated abort.
3071 */
3072 spin_lock_irqsave(
3073 &phba->hbalock, iflag);
3074 cmdiocbp->iocb_flag &=
3075 ~LPFC_DRIVER_ABORTED;
3076 spin_unlock_irqrestore(
3077 &phba->hbalock, iflag);
3078 cmdiocbp->iocb.ulpStatus =
3079 IOSTAT_LOCAL_REJECT;
3080 cmdiocbp->iocb.un.ulpWord[4] =
3081 IOERR_ABORT_REQUESTED;
3082 /*
3083 * For SLI4, irsiocb contains
3084 * NO_XRI in sli_xritag, it
3085 * shall not affect releasing
3086 * sgl (xri) process.
3087 */
3088 saveq->iocb.ulpStatus =
3089 IOSTAT_LOCAL_REJECT;
3090 saveq->iocb.un.ulpWord[4] =
3091 IOERR_SLI_ABORTED;
3092 spin_lock_irqsave(
3093 &phba->hbalock, iflag);
3094 saveq->iocb_flag |=
3095 LPFC_DELAY_MEM_FREE;
3096 spin_unlock_irqrestore(
3097 &phba->hbalock, iflag);
3098 }
James Smart07951072007-04-25 09:51:38 -04003099 }
dea31012005-04-17 16:05:31 -05003100 }
James Smart2e0fef82007-06-17 19:56:36 -05003101 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003102 } else
3103 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003104 } else {
3105 /*
3106 * Unknown initiating command based on the response iotag.
3107 * This could be the case on the ELS ring because of
3108 * lpfc_els_abort().
3109 */
3110 if (pring->ringno != LPFC_ELS_RING) {
3111 /*
3112 * Ring <ringno> handler: unexpected completion IoTag
3113 * <IoTag>
3114 */
James Smarta257bf92009-04-06 18:48:10 -04003115 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003116 "0322 Ring %d handler: "
3117 "unexpected completion IoTag x%x "
3118 "Data: x%x x%x x%x x%x\n",
3119 pring->ringno,
3120 saveq->iocb.ulpIoTag,
3121 saveq->iocb.ulpStatus,
3122 saveq->iocb.un.ulpWord[4],
3123 saveq->iocb.ulpCommand,
3124 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003125 }
3126 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003127
dea31012005-04-17 16:05:31 -05003128 return rc;
3129}
3130
James Smarte59058c2008-08-24 21:49:00 -04003131/**
James Smart3621a712009-04-06 18:47:14 -04003132 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003133 * @phba: Pointer to HBA context object.
3134 * @pring: Pointer to driver SLI ring object.
3135 *
3136 * This function is called from the iocb ring event handlers when
3137 * put pointer is ahead of the get pointer for a ring. This function signal
3138 * an error attention condition to the worker thread and the worker
3139 * thread will transition the HBA to offline state.
3140 **/
James Smart2e0fef82007-06-17 19:56:36 -05003141static void
3142lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003143{
James Smart34b02dc2008-08-24 21:49:55 -04003144 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003145 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003146 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003147 * rsp ring <portRspMax>
3148 */
3149 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003150 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003151 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003152 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003153 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003154
James Smart2e0fef82007-06-17 19:56:36 -05003155 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003156
3157 /*
3158 * All error attention handlers are posted to
3159 * worker thread
3160 */
3161 phba->work_ha |= HA_ERATT;
3162 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003163
James Smart5e9d9b82008-06-14 22:52:53 -04003164 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003165
3166 return;
3167}
3168
James Smarte59058c2008-08-24 21:49:00 -04003169/**
James Smart3621a712009-04-06 18:47:14 -04003170 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04003171 * @ptr: Pointer to address of HBA context object.
3172 *
3173 * This function is invoked by the Error Attention polling timer when the
3174 * timer times out. It will check the SLI Error Attention register for
3175 * possible attention events. If so, it will post an Error Attention event
3176 * and wake up worker thread to process it. Otherwise, it will set up the
3177 * Error Attention polling timer for the next poll.
3178 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003179void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003180{
3181 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003182 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003183 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003184
Kees Cookf22eb4d2017-09-06 20:24:26 -07003185 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003186
James Smartaa6fbb72012-08-03 12:36:03 -04003187 /* Here we will also keep track of interrupts per sec of the hba */
3188 sli_intr = phba->sli.slistat.sli_intr;
3189
3190 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3191 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3192 sli_intr);
3193 else
3194 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3195
James Smart65791f12016-07-06 12:35:56 -07003196 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3197 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003198 phba->sli.slistat.sli_ips = cnt;
3199
3200 phba->sli.slistat.sli_prev_intr = sli_intr;
3201
James Smart93996272008-08-24 21:50:30 -04003202 /* Check chip HA register for error event */
3203 eratt = lpfc_sli_check_eratt(phba);
3204
3205 if (eratt)
3206 /* Tell the worker thread there is work to do */
3207 lpfc_worker_wake_up(phba);
3208 else
3209 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003210 mod_timer(&phba->eratt_poll,
3211 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003212 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003213 return;
3214}
3215
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003216
James Smarte59058c2008-08-24 21:49:00 -04003217/**
James Smart3621a712009-04-06 18:47:14 -04003218 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003219 * @phba: Pointer to HBA context object.
3220 * @pring: Pointer to driver SLI ring object.
3221 * @mask: Host attention register mask for this ring.
3222 *
3223 * This function is called from the interrupt context when there is a ring
3224 * event for the fcp ring. The caller does not hold any lock.
3225 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003226 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003227 * LE bit set. The function will call the completion handler of the command iocb
3228 * if the response iocb indicates a completion for a command iocb or it is
3229 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3230 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003231 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003232 * to check it explicitly.
3233 */
3234int
James Smart2e0fef82007-06-17 19:56:36 -05003235lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3236 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003237{
James Smart34b02dc2008-08-24 21:49:55 -04003238 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003239 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003240 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003241 struct lpfc_iocbq *cmdiocbq = NULL;
3242 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003243 uint32_t status;
3244 uint32_t portRspPut, portRspMax;
3245 int rc = 1;
3246 lpfc_iocb_type type;
3247 unsigned long iflag;
3248 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003249
James Smart2e0fef82007-06-17 19:56:36 -05003250 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003251 pring->stats.iocb_event++;
3252
dea31012005-04-17 16:05:31 -05003253 /*
3254 * The next available response entry should never exceed the maximum
3255 * entries. If it does, treat it as an adapter hardware error.
3256 */
James Smart7e56aa22012-08-03 12:35:34 -04003257 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003258 portRspPut = le32_to_cpu(pgp->rspPutInx);
3259 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003260 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003261 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003262 return 1;
3263 }
James Smart45ed1192009-10-02 15:17:02 -04003264 if (phba->fcp_ring_in_use) {
3265 spin_unlock_irqrestore(&phba->hbalock, iflag);
3266 return 1;
3267 } else
3268 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003269
3270 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003271 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003272 /*
3273 * Fetch an entry off the ring and copy it into a local data
3274 * structure. The copy involves a byte-swap since the
3275 * network byte order and pci byte orders are different.
3276 */
James Smarted957682007-06-17 19:56:37 -05003277 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003278 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003279
James Smart7e56aa22012-08-03 12:35:34 -04003280 if (++pring->sli.sli3.rspidx >= portRspMax)
3281 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003282
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003283 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3284 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003285 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003286 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003287 irsp = &rspiocbq.iocb;
3288
dea31012005-04-17 16:05:31 -05003289 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3290 pring->stats.iocb_rsp++;
3291 rsp_cmpl++;
3292
3293 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003294 /*
3295 * If resource errors reported from HBA, reduce
3296 * queuedepths of the SCSI device.
3297 */
3298 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003299 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3300 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003301 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003302 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003303 spin_lock_irqsave(&phba->hbalock, iflag);
3304 }
3305
dea31012005-04-17 16:05:31 -05003306 /* Rsp ring <ringno> error: IOCB */
3307 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003308 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003309 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003310 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003311 irsp->un.ulpWord[0],
3312 irsp->un.ulpWord[1],
3313 irsp->un.ulpWord[2],
3314 irsp->un.ulpWord[3],
3315 irsp->un.ulpWord[4],
3316 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003317 *(uint32_t *)&irsp->un1,
3318 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003319 }
3320
3321 switch (type) {
3322 case LPFC_ABORT_IOCB:
3323 case LPFC_SOL_IOCB:
3324 /*
3325 * Idle exchange closed via ABTS from port. No iocb
3326 * resources need to be recovered.
3327 */
3328 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003329 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003330 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003331 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003332 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003333 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003334 break;
3335 }
3336
James Bottomley604a3e32005-10-29 10:28:33 -05003337 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3338 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003339 if (unlikely(!cmdiocbq))
3340 break;
3341 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3342 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3343 if (cmdiocbq->iocb_cmpl) {
3344 spin_unlock_irqrestore(&phba->hbalock, iflag);
3345 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3346 &rspiocbq);
3347 spin_lock_irqsave(&phba->hbalock, iflag);
3348 }
dea31012005-04-17 16:05:31 -05003349 break;
James Smarta4bc3372006-12-02 13:34:16 -05003350 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003351 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003352 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003353 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003354 break;
dea31012005-04-17 16:05:31 -05003355 default:
3356 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3357 char adaptermsg[LPFC_MAX_ADPTMSG];
3358 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3359 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3360 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003361 dev_warn(&((phba->pcidev)->dev),
3362 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003363 phba->brd_no, adaptermsg);
3364 } else {
3365 /* Unknown IOCB command */
3366 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003367 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003368 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003369 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003370 irsp->ulpStatus,
3371 irsp->ulpIoTag,
3372 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003373 }
3374 break;
3375 }
3376
3377 /*
3378 * The response IOCB has been processed. Update the ring
3379 * pointer in SLIM. If the port response put pointer has not
3380 * been updated, sync the pgp->rspPutInx and fetch the new port
3381 * response put pointer.
3382 */
James Smart7e56aa22012-08-03 12:35:34 -04003383 writel(pring->sli.sli3.rspidx,
3384 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003385
James Smart7e56aa22012-08-03 12:35:34 -04003386 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003387 portRspPut = le32_to_cpu(pgp->rspPutInx);
3388 }
3389
3390 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3391 pring->stats.iocb_rsp_full++;
3392 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3393 writel(status, phba->CAregaddr);
3394 readl(phba->CAregaddr);
3395 }
3396 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3397 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3398 pring->stats.iocb_cmd_empty++;
3399
3400 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003401 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003402 lpfc_sli_resume_iocb(phba, pring);
3403
3404 if ((pring->lpfc_sli_cmd_available))
3405 (pring->lpfc_sli_cmd_available) (phba, pring);
3406
3407 }
3408
James Smart45ed1192009-10-02 15:17:02 -04003409 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003410 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003411 return rc;
3412}
3413
James Smarte59058c2008-08-24 21:49:00 -04003414/**
James Smart3772a992009-05-22 14:50:54 -04003415 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3416 * @phba: Pointer to HBA context object.
3417 * @pring: Pointer to driver SLI ring object.
3418 * @rspiocbp: Pointer to driver response IOCB object.
3419 *
3420 * This function is called from the worker thread when there is a slow-path
3421 * response IOCB to process. This function chains all the response iocbs until
3422 * seeing the iocb with the LE bit set. The function will call
3423 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3424 * completion of a command iocb. The function will call the
3425 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3426 * The function frees the resources or calls the completion handler if this
3427 * iocb is an abort completion. The function returns NULL when the response
3428 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3429 * this function shall chain the iocb on to the iocb_continueq and return the
3430 * response iocb passed in.
3431 **/
3432static struct lpfc_iocbq *
3433lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3434 struct lpfc_iocbq *rspiocbp)
3435{
3436 struct lpfc_iocbq *saveq;
3437 struct lpfc_iocbq *cmdiocbp;
3438 struct lpfc_iocbq *next_iocb;
3439 IOCB_t *irsp = NULL;
3440 uint32_t free_saveq;
3441 uint8_t iocb_cmd_type;
3442 lpfc_iocb_type type;
3443 unsigned long iflag;
3444 int rc;
3445
3446 spin_lock_irqsave(&phba->hbalock, iflag);
3447 /* First add the response iocb to the countinueq list */
3448 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3449 pring->iocb_continueq_cnt++;
3450
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003451 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003452 irsp = &rspiocbp->iocb;
3453 if (irsp->ulpLe) {
3454 /*
3455 * By default, the driver expects to free all resources
3456 * associated with this iocb completion.
3457 */
3458 free_saveq = 1;
3459 saveq = list_get_first(&pring->iocb_continueq,
3460 struct lpfc_iocbq, list);
3461 irsp = &(saveq->iocb);
3462 list_del_init(&pring->iocb_continueq);
3463 pring->iocb_continueq_cnt = 0;
3464
3465 pring->stats.iocb_rsp++;
3466
3467 /*
3468 * If resource errors reported from HBA, reduce
3469 * queuedepths of the SCSI device.
3470 */
3471 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003472 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3473 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003474 spin_unlock_irqrestore(&phba->hbalock, iflag);
3475 phba->lpfc_rampdown_queue_depth(phba);
3476 spin_lock_irqsave(&phba->hbalock, iflag);
3477 }
3478
3479 if (irsp->ulpStatus) {
3480 /* Rsp ring <ringno> error: IOCB */
3481 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3482 "0328 Rsp Ring %d error: "
3483 "IOCB Data: "
3484 "x%x x%x x%x x%x "
3485 "x%x x%x x%x x%x "
3486 "x%x x%x x%x x%x "
3487 "x%x x%x x%x x%x\n",
3488 pring->ringno,
3489 irsp->un.ulpWord[0],
3490 irsp->un.ulpWord[1],
3491 irsp->un.ulpWord[2],
3492 irsp->un.ulpWord[3],
3493 irsp->un.ulpWord[4],
3494 irsp->un.ulpWord[5],
3495 *(((uint32_t *) irsp) + 6),
3496 *(((uint32_t *) irsp) + 7),
3497 *(((uint32_t *) irsp) + 8),
3498 *(((uint32_t *) irsp) + 9),
3499 *(((uint32_t *) irsp) + 10),
3500 *(((uint32_t *) irsp) + 11),
3501 *(((uint32_t *) irsp) + 12),
3502 *(((uint32_t *) irsp) + 13),
3503 *(((uint32_t *) irsp) + 14),
3504 *(((uint32_t *) irsp) + 15));
3505 }
3506
3507 /*
3508 * Fetch the IOCB command type and call the correct completion
3509 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3510 * get freed back to the lpfc_iocb_list by the discovery
3511 * kernel thread.
3512 */
3513 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3514 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3515 switch (type) {
3516 case LPFC_SOL_IOCB:
3517 spin_unlock_irqrestore(&phba->hbalock, iflag);
3518 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3519 spin_lock_irqsave(&phba->hbalock, iflag);
3520 break;
3521
3522 case LPFC_UNSOL_IOCB:
3523 spin_unlock_irqrestore(&phba->hbalock, iflag);
3524 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3525 spin_lock_irqsave(&phba->hbalock, iflag);
3526 if (!rc)
3527 free_saveq = 0;
3528 break;
3529
3530 case LPFC_ABORT_IOCB:
3531 cmdiocbp = NULL;
3532 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3533 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3534 saveq);
3535 if (cmdiocbp) {
3536 /* Call the specified completion routine */
3537 if (cmdiocbp->iocb_cmpl) {
3538 spin_unlock_irqrestore(&phba->hbalock,
3539 iflag);
3540 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3541 saveq);
3542 spin_lock_irqsave(&phba->hbalock,
3543 iflag);
3544 } else
3545 __lpfc_sli_release_iocbq(phba,
3546 cmdiocbp);
3547 }
3548 break;
3549
3550 case LPFC_UNKNOWN_IOCB:
3551 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3552 char adaptermsg[LPFC_MAX_ADPTMSG];
3553 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3554 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3555 MAX_MSG_DATA);
3556 dev_warn(&((phba->pcidev)->dev),
3557 "lpfc%d: %s\n",
3558 phba->brd_no, adaptermsg);
3559 } else {
3560 /* Unknown IOCB command */
3561 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3562 "0335 Unknown IOCB "
3563 "command Data: x%x "
3564 "x%x x%x x%x\n",
3565 irsp->ulpCommand,
3566 irsp->ulpStatus,
3567 irsp->ulpIoTag,
3568 irsp->ulpContext);
3569 }
3570 break;
3571 }
3572
3573 if (free_saveq) {
3574 list_for_each_entry_safe(rspiocbp, next_iocb,
3575 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003576 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003577 __lpfc_sli_release_iocbq(phba, rspiocbp);
3578 }
3579 __lpfc_sli_release_iocbq(phba, saveq);
3580 }
3581 rspiocbp = NULL;
3582 }
3583 spin_unlock_irqrestore(&phba->hbalock, iflag);
3584 return rspiocbp;
3585}
3586
3587/**
3588 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003589 * @phba: Pointer to HBA context object.
3590 * @pring: Pointer to driver SLI ring object.
3591 * @mask: Host attention register mask for this ring.
3592 *
James Smart3772a992009-05-22 14:50:54 -04003593 * This routine wraps the actual slow_ring event process routine from the
3594 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003595 **/
James Smart3772a992009-05-22 14:50:54 -04003596void
James Smart2e0fef82007-06-17 19:56:36 -05003597lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3598 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003599{
James Smart3772a992009-05-22 14:50:54 -04003600 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3601}
3602
3603/**
3604 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3605 * @phba: Pointer to HBA context object.
3606 * @pring: Pointer to driver SLI ring object.
3607 * @mask: Host attention register mask for this ring.
3608 *
3609 * This function is called from the worker thread when there is a ring event
3610 * for non-fcp rings. The caller does not hold any lock. The function will
3611 * remove each response iocb in the response ring and calls the handle
3612 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3613 **/
3614static void
3615lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3616 struct lpfc_sli_ring *pring, uint32_t mask)
3617{
James Smart34b02dc2008-08-24 21:49:55 -04003618 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003619 IOCB_t *entry;
3620 IOCB_t *irsp = NULL;
3621 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003622 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003623 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003624 uint32_t status;
dea31012005-04-17 16:05:31 -05003625
James Smart34b02dc2008-08-24 21:49:55 -04003626 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003627 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003628 pring->stats.iocb_event++;
3629
dea31012005-04-17 16:05:31 -05003630 /*
3631 * The next available response entry should never exceed the maximum
3632 * entries. If it does, treat it as an adapter hardware error.
3633 */
James Smart7e56aa22012-08-03 12:35:34 -04003634 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003635 portRspPut = le32_to_cpu(pgp->rspPutInx);
3636 if (portRspPut >= portRspMax) {
3637 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003638 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003639 * rsp ring <portRspMax>
3640 */
James Smarted957682007-06-17 19:56:37 -05003641 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003642 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003643 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003644 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003645
James Smart2e0fef82007-06-17 19:56:36 -05003646 phba->link_state = LPFC_HBA_ERROR;
3647 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003648
3649 phba->work_hs = HS_FFER3;
3650 lpfc_handle_eratt(phba);
3651
James Smart3772a992009-05-22 14:50:54 -04003652 return;
dea31012005-04-17 16:05:31 -05003653 }
3654
3655 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003656 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003657 /*
3658 * Build a completion list and call the appropriate handler.
3659 * The process is to get the next available response iocb, get
3660 * a free iocb from the list, copy the response data into the
3661 * free iocb, insert to the continuation list, and update the
3662 * next response index to slim. This process makes response
3663 * iocb's in the ring available to DMA as fast as possible but
3664 * pays a penalty for a copy operation. Since the iocb is
3665 * only 32 bytes, this penalty is considered small relative to
3666 * the PCI reads for register values and a slim write. When
3667 * the ulpLe field is set, the entire Command has been
3668 * received.
3669 */
James Smarted957682007-06-17 19:56:37 -05003670 entry = lpfc_resp_iocb(phba, pring);
3671
James Smart858c9f62007-06-17 19:56:39 -05003672 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003673 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003674 if (rspiocbp == NULL) {
3675 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003676 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003677 break;
3678 }
3679
James Smarted957682007-06-17 19:56:37 -05003680 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3681 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003682 irsp = &rspiocbp->iocb;
3683
James Smart7e56aa22012-08-03 12:35:34 -04003684 if (++pring->sli.sli3.rspidx >= portRspMax)
3685 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003686
James Smarta58cbd52007-08-02 11:09:43 -04003687 if (pring->ringno == LPFC_ELS_RING) {
3688 lpfc_debugfs_slow_ring_trc(phba,
3689 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3690 *(((uint32_t *) irsp) + 4),
3691 *(((uint32_t *) irsp) + 6),
3692 *(((uint32_t *) irsp) + 7));
3693 }
3694
James Smart7e56aa22012-08-03 12:35:34 -04003695 writel(pring->sli.sli3.rspidx,
3696 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003697
James Smart3772a992009-05-22 14:50:54 -04003698 spin_unlock_irqrestore(&phba->hbalock, iflag);
3699 /* Handle the response IOCB */
3700 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3701 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003702
3703 /*
3704 * If the port response put pointer has not been updated, sync
3705 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3706 * response put pointer.
3707 */
James Smart7e56aa22012-08-03 12:35:34 -04003708 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003709 portRspPut = le32_to_cpu(pgp->rspPutInx);
3710 }
James Smart7e56aa22012-08-03 12:35:34 -04003711 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003712
James Smart92d7f7b2007-06-17 19:56:38 -05003713 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003714 /* At least one response entry has been freed */
3715 pring->stats.iocb_rsp_full++;
3716 /* SET RxRE_RSP in Chip Att register */
3717 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3718 writel(status, phba->CAregaddr);
3719 readl(phba->CAregaddr); /* flush */
3720 }
3721 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3722 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3723 pring->stats.iocb_cmd_empty++;
3724
3725 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003726 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003727 lpfc_sli_resume_iocb(phba, pring);
3728
3729 if ((pring->lpfc_sli_cmd_available))
3730 (pring->lpfc_sli_cmd_available) (phba, pring);
3731
3732 }
3733
James Smart2e0fef82007-06-17 19:56:36 -05003734 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003735 return;
dea31012005-04-17 16:05:31 -05003736}
3737
James Smarte59058c2008-08-24 21:49:00 -04003738/**
James Smart4f774512009-05-22 14:52:35 -04003739 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3740 * @phba: Pointer to HBA context object.
3741 * @pring: Pointer to driver SLI ring object.
3742 * @mask: Host attention register mask for this ring.
3743 *
3744 * This function is called from the worker thread when there is a pending
3745 * ELS response iocb on the driver internal slow-path response iocb worker
3746 * queue. The caller does not hold any lock. The function will remove each
3747 * response iocb from the response worker queue and calls the handle
3748 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3749 **/
3750static void
3751lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3752 struct lpfc_sli_ring *pring, uint32_t mask)
3753{
3754 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003755 struct hbq_dmabuf *dmabuf;
3756 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003757 unsigned long iflag;
3758
James Smart45ed1192009-10-02 15:17:02 -04003759 spin_lock_irqsave(&phba->hbalock, iflag);
3760 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3761 spin_unlock_irqrestore(&phba->hbalock, iflag);
3762 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003763 /* Get the response iocb from the head of work queue */
3764 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003765 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003766 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003767 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003768
3769 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3770 case CQE_CODE_COMPL_WQE:
3771 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3772 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003773 /* Translate ELS WCQE to response IOCBQ */
3774 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3775 irspiocbq);
3776 if (irspiocbq)
3777 lpfc_sli_sp_handle_rspiocb(phba, pring,
3778 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003779 break;
3780 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003781 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003782 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3783 cq_event);
3784 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3785 break;
3786 default:
3787 break;
3788 }
James Smart4f774512009-05-22 14:52:35 -04003789 }
3790}
3791
3792/**
James Smart3621a712009-04-06 18:47:14 -04003793 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003794 * @phba: Pointer to HBA context object.
3795 * @pring: Pointer to driver SLI ring object.
3796 *
3797 * This function aborts all iocbs in the given ring and frees all the iocb
3798 * objects in txq. This function issues an abort iocb for all the iocb commands
3799 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3800 * the return of this function. The caller is not required to hold any locks.
3801 **/
James Smart2e0fef82007-06-17 19:56:36 -05003802void
dea31012005-04-17 16:05:31 -05003803lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3804{
James Smart2534ba72007-04-25 09:52:20 -04003805 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003806 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003807
James Smart92d7f7b2007-06-17 19:56:38 -05003808 if (pring->ringno == LPFC_ELS_RING) {
3809 lpfc_fabric_abort_hba(phba);
3810 }
3811
dea31012005-04-17 16:05:31 -05003812 /* Error everything on txq and txcmplq
3813 * First do the txq.
3814 */
James Smartdb55fba2014-04-04 13:52:02 -04003815 if (phba->sli_rev >= LPFC_SLI_REV4) {
3816 spin_lock_irq(&pring->ring_lock);
3817 list_splice_init(&pring->txq, &completions);
3818 pring->txq_cnt = 0;
3819 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003820
James Smartdb55fba2014-04-04 13:52:02 -04003821 spin_lock_irq(&phba->hbalock);
3822 /* Next issue ABTS for everything on the txcmplq */
3823 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3824 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3825 spin_unlock_irq(&phba->hbalock);
3826 } else {
3827 spin_lock_irq(&phba->hbalock);
3828 list_splice_init(&pring->txq, &completions);
3829 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003830
James Smartdb55fba2014-04-04 13:52:02 -04003831 /* Next issue ABTS for everything on the txcmplq */
3832 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3833 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3834 spin_unlock_irq(&phba->hbalock);
3835 }
James Smart2534ba72007-04-25 09:52:20 -04003836
James Smarta257bf92009-04-06 18:48:10 -04003837 /* Cancel all the IOCBs from the completions list */
3838 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3839 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003840}
3841
James Smarte59058c2008-08-24 21:49:00 -04003842/**
James Smart895427b2017-02-12 13:52:30 -08003843 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3844 * @phba: Pointer to HBA context object.
3845 * @pring: Pointer to driver SLI ring object.
3846 *
3847 * This function aborts all iocbs in the given ring and frees all the iocb
3848 * objects in txq. This function issues an abort iocb for all the iocb commands
3849 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3850 * the return of this function. The caller is not required to hold any locks.
3851 **/
3852void
3853lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3854{
3855 LIST_HEAD(completions);
3856 struct lpfc_iocbq *iocb, *next_iocb;
3857
3858 if (pring->ringno == LPFC_ELS_RING)
3859 lpfc_fabric_abort_hba(phba);
3860
3861 spin_lock_irq(&phba->hbalock);
3862 /* Next issue ABTS for everything on the txcmplq */
3863 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3864 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3865 spin_unlock_irq(&phba->hbalock);
3866}
3867
3868
3869/**
James Smartdb55fba2014-04-04 13:52:02 -04003870 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3871 * @phba: Pointer to HBA context object.
3872 * @pring: Pointer to driver SLI ring object.
3873 *
3874 * This function aborts all iocbs in FCP rings and frees all the iocb
3875 * objects in txq. This function issues an abort iocb for all the iocb commands
3876 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3877 * the return of this function. The caller is not required to hold any locks.
3878 **/
3879void
3880lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3881{
3882 struct lpfc_sli *psli = &phba->sli;
3883 struct lpfc_sli_ring *pring;
3884 uint32_t i;
3885
3886 /* Look on all the FCP Rings for the iotag */
3887 if (phba->sli_rev >= LPFC_SLI_REV4) {
3888 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003889 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003890 lpfc_sli_abort_iocb_ring(phba, pring);
3891 }
3892 } else {
James Smart895427b2017-02-12 13:52:30 -08003893 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003894 lpfc_sli_abort_iocb_ring(phba, pring);
3895 }
3896}
3897
James Smart895427b2017-02-12 13:52:30 -08003898/**
3899 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3900 * @phba: Pointer to HBA context object.
3901 *
3902 * This function aborts all wqes in NVME rings. This function issues an
3903 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3904 * the txcmplq is not guaranteed to complete before the return of this
3905 * function. The caller is not required to hold any locks.
3906 **/
3907void
3908lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3909{
3910 struct lpfc_sli_ring *pring;
3911 uint32_t i;
3912
3913 if (phba->sli_rev < LPFC_SLI_REV4)
3914 return;
3915
3916 /* Abort all IO on each NVME ring. */
3917 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3918 pring = phba->sli4_hba.nvme_wq[i]->pring;
3919 lpfc_sli_abort_wqe_ring(phba, pring);
3920 }
3921}
3922
James Smartdb55fba2014-04-04 13:52:02 -04003923
3924/**
James Smart3621a712009-04-06 18:47:14 -04003925 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003926 * @phba: Pointer to HBA context object.
3927 *
3928 * This function flushes all iocbs in the fcp ring and frees all the iocb
3929 * objects in txq and txcmplq. This function will not issue abort iocbs
3930 * for all the iocb commands in txcmplq, they will just be returned with
3931 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3932 * slot has been permanently disabled.
3933 **/
3934void
3935lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3936{
3937 LIST_HEAD(txq);
3938 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003939 struct lpfc_sli *psli = &phba->sli;
3940 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003941 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08003942 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04003943
3944 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003945 /* Indicate the I/O queues are flushed */
3946 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003947 spin_unlock_irq(&phba->hbalock);
3948
James Smartdb55fba2014-04-04 13:52:02 -04003949 /* Look on all the FCP Rings for the iotag */
3950 if (phba->sli_rev >= LPFC_SLI_REV4) {
3951 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003952 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smarta8e497d2008-08-24 21:50:11 -04003953
James Smartdb55fba2014-04-04 13:52:02 -04003954 spin_lock_irq(&pring->ring_lock);
3955 /* Retrieve everything on txq */
3956 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08003957 list_for_each_entry_safe(piocb, next_iocb,
3958 &pring->txcmplq, list)
3959 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04003960 /* Retrieve everything on the txcmplq */
3961 list_splice_init(&pring->txcmplq, &txcmplq);
3962 pring->txq_cnt = 0;
3963 pring->txcmplq_cnt = 0;
3964 spin_unlock_irq(&pring->ring_lock);
3965
3966 /* Flush the txq */
3967 lpfc_sli_cancel_iocbs(phba, &txq,
3968 IOSTAT_LOCAL_REJECT,
3969 IOERR_SLI_DOWN);
3970 /* Flush the txcmpq */
3971 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3972 IOSTAT_LOCAL_REJECT,
3973 IOERR_SLI_DOWN);
3974 }
3975 } else {
James Smart895427b2017-02-12 13:52:30 -08003976 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003977
3978 spin_lock_irq(&phba->hbalock);
3979 /* Retrieve everything on txq */
3980 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08003981 list_for_each_entry_safe(piocb, next_iocb,
3982 &pring->txcmplq, list)
3983 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04003984 /* Retrieve everything on the txcmplq */
3985 list_splice_init(&pring->txcmplq, &txcmplq);
3986 pring->txq_cnt = 0;
3987 pring->txcmplq_cnt = 0;
3988 spin_unlock_irq(&phba->hbalock);
3989
3990 /* Flush the txq */
3991 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3992 IOERR_SLI_DOWN);
3993 /* Flush the txcmpq */
3994 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3995 IOERR_SLI_DOWN);
3996 }
James Smarta8e497d2008-08-24 21:50:11 -04003997}
3998
3999/**
James Smart895427b2017-02-12 13:52:30 -08004000 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
4001 * @phba: Pointer to HBA context object.
4002 *
4003 * This function flushes all wqes in the nvme rings and frees all resources
4004 * in the txcmplq. This function does not issue abort wqes for the IO
4005 * commands in txcmplq, they will just be returned with
4006 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4007 * slot has been permanently disabled.
4008 **/
4009void
4010lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
4011{
4012 LIST_HEAD(txcmplq);
4013 struct lpfc_sli_ring *pring;
4014 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004015 struct lpfc_iocbq *piocb, *next_iocb;
James Smart895427b2017-02-12 13:52:30 -08004016
4017 if (phba->sli_rev < LPFC_SLI_REV4)
4018 return;
4019
4020 /* Hint to other driver operations that a flush is in progress. */
4021 spin_lock_irq(&phba->hbalock);
4022 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
4023 spin_unlock_irq(&phba->hbalock);
4024
4025 /* Cycle through all NVME rings and complete each IO with
4026 * a local driver reason code. This is a flush so no
4027 * abort exchange to FW.
4028 */
4029 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
4030 pring = phba->sli4_hba.nvme_wq[i]->pring;
4031
James Smart895427b2017-02-12 13:52:30 -08004032 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -08004033 list_for_each_entry_safe(piocb, next_iocb,
4034 &pring->txcmplq, list)
4035 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4036 /* Retrieve everything on the txcmplq */
James Smart895427b2017-02-12 13:52:30 -08004037 list_splice_init(&pring->txcmplq, &txcmplq);
4038 pring->txcmplq_cnt = 0;
4039 spin_unlock_irq(&pring->ring_lock);
4040
4041 /* Flush the txcmpq &&&PAE */
4042 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4043 IOSTAT_LOCAL_REJECT,
4044 IOERR_SLI_DOWN);
4045 }
4046}
4047
4048/**
James Smart3772a992009-05-22 14:50:54 -04004049 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004050 * @phba: Pointer to HBA context object.
4051 * @mask: Bit mask to be checked.
4052 *
4053 * This function reads the host status register and compares
4054 * with the provided bit mask to check if HBA completed
4055 * the restart. This function will wait in a loop for the
4056 * HBA to complete restart. If the HBA does not restart within
4057 * 15 iterations, the function will reset the HBA again. The
4058 * function returns 1 when HBA fail to restart otherwise returns
4059 * zero.
4060 **/
James Smart3772a992009-05-22 14:50:54 -04004061static int
4062lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004063{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004064 uint32_t status;
4065 int i = 0;
4066 int retval = 0;
dea31012005-04-17 16:05:31 -05004067
Jamie Wellnitz41415862006-02-28 19:25:27 -05004068 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004069 if (lpfc_readl(phba->HSregaddr, &status))
4070 return 1;
dea31012005-04-17 16:05:31 -05004071
Jamie Wellnitz41415862006-02-28 19:25:27 -05004072 /*
4073 * Check status register every 100ms for 5 retries, then every
4074 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4075 * every 2.5 sec for 4.
4076 * Break our of the loop if errors occurred during init.
4077 */
4078 while (((status & mask) != mask) &&
4079 !(status & HS_FFERM) &&
4080 i++ < 20) {
dea31012005-04-17 16:05:31 -05004081
Jamie Wellnitz41415862006-02-28 19:25:27 -05004082 if (i <= 5)
4083 msleep(10);
4084 else if (i <= 10)
4085 msleep(500);
4086 else
4087 msleep(2500);
dea31012005-04-17 16:05:31 -05004088
Jamie Wellnitz41415862006-02-28 19:25:27 -05004089 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004090 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004091 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004092 lpfc_sli_brdrestart(phba);
4093 }
4094 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004095 if (lpfc_readl(phba->HSregaddr, &status)) {
4096 retval = 1;
4097 break;
4098 }
dea31012005-04-17 16:05:31 -05004099 }
dea31012005-04-17 16:05:31 -05004100
Jamie Wellnitz41415862006-02-28 19:25:27 -05004101 /* Check to see if any errors occurred during init */
4102 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05004103 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4104 "2751 Adapter failed to restart, "
4105 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4106 status,
4107 readl(phba->MBslimaddr + 0xa8),
4108 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004109 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004110 retval = 1;
4111 }
dea31012005-04-17 16:05:31 -05004112
Jamie Wellnitz41415862006-02-28 19:25:27 -05004113 return retval;
dea31012005-04-17 16:05:31 -05004114}
4115
James Smartda0436e2009-05-22 14:51:39 -04004116/**
4117 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4118 * @phba: Pointer to HBA context object.
4119 * @mask: Bit mask to be checked.
4120 *
4121 * This function checks the host status register to check if HBA is
4122 * ready. This function will wait in a loop for the HBA to be ready
4123 * If the HBA is not ready , the function will will reset the HBA PCI
4124 * function again. The function returns 1 when HBA fail to be ready
4125 * otherwise returns zero.
4126 **/
4127static int
4128lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4129{
4130 uint32_t status;
4131 int retval = 0;
4132
4133 /* Read the HBA Host Status Register */
4134 status = lpfc_sli4_post_status_check(phba);
4135
4136 if (status) {
4137 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4138 lpfc_sli_brdrestart(phba);
4139 status = lpfc_sli4_post_status_check(phba);
4140 }
4141
4142 /* Check to see if any errors occurred during init */
4143 if (status) {
4144 phba->link_state = LPFC_HBA_ERROR;
4145 retval = 1;
4146 } else
4147 phba->sli4_hba.intr_enable = 0;
4148
4149 return retval;
4150}
4151
4152/**
4153 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4154 * @phba: Pointer to HBA context object.
4155 * @mask: Bit mask to be checked.
4156 *
4157 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4158 * from the API jump table function pointer from the lpfc_hba struct.
4159 **/
4160int
4161lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4162{
4163 return phba->lpfc_sli_brdready(phba, mask);
4164}
4165
James Smart92908312006-03-07 15:04:13 -05004166#define BARRIER_TEST_PATTERN (0xdeadbeef)
4167
James Smarte59058c2008-08-24 21:49:00 -04004168/**
James Smart3621a712009-04-06 18:47:14 -04004169 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004170 * @phba: Pointer to HBA context object.
4171 *
James Smart1b511972011-12-13 13:23:09 -05004172 * This function is called before resetting an HBA. This function is called
4173 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004174 **/
James Smart2e0fef82007-06-17 19:56:36 -05004175void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004176{
James Smart65a29c12006-07-06 15:50:50 -04004177 uint32_t __iomem *resp_buf;
4178 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004179 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004180 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004181 int i;
4182 uint8_t hdrtype;
4183
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004184 lockdep_assert_held(&phba->hbalock);
4185
James Smart92908312006-03-07 15:04:13 -05004186 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4187 if (hdrtype != 0x80 ||
4188 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4189 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4190 return;
4191
4192 /*
4193 * Tell the other part of the chip to suspend temporarily all
4194 * its DMA activity.
4195 */
James Smart65a29c12006-07-06 15:50:50 -04004196 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004197
4198 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004199 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4200 return;
James Smart92908312006-03-07 15:04:13 -05004201 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4202 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004203 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004204
James Smart9940b972011-03-11 16:06:12 -05004205 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4206 return;
4207 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004208 /* Clear Chip error bit */
4209 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004210 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004211 }
4212
4213 mbox = 0;
4214 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4215 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4216
4217 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004218 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004219 writel(mbox, mbox_buf);
4220
James Smart9940b972011-03-11 16:06:12 -05004221 for (i = 0; i < 50; i++) {
4222 if (lpfc_readl((resp_buf + 1), &resp_data))
4223 return;
4224 if (resp_data != ~(BARRIER_TEST_PATTERN))
4225 mdelay(1);
4226 else
4227 break;
4228 }
4229 resp_data = 0;
4230 if (lpfc_readl((resp_buf + 1), &resp_data))
4231 return;
4232 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004233 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004234 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004235 goto restore_hc;
4236 else
4237 goto clear_errat;
4238 }
4239
4240 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004241 resp_data = 0;
4242 for (i = 0; i < 500; i++) {
4243 if (lpfc_readl(resp_buf, &resp_data))
4244 return;
4245 if (resp_data != mbox)
4246 mdelay(1);
4247 else
4248 break;
4249 }
James Smart92908312006-03-07 15:04:13 -05004250
4251clear_errat:
4252
James Smart9940b972011-03-11 16:06:12 -05004253 while (++i < 500) {
4254 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4255 return;
4256 if (!(ha_copy & HA_ERATT))
4257 mdelay(1);
4258 else
4259 break;
4260 }
James Smart92908312006-03-07 15:04:13 -05004261
4262 if (readl(phba->HAregaddr) & HA_ERATT) {
4263 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004264 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004265 }
4266
4267restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004268 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004269 writel(hc_copy, phba->HCregaddr);
4270 readl(phba->HCregaddr); /* flush */
4271}
4272
James Smarte59058c2008-08-24 21:49:00 -04004273/**
James Smart3621a712009-04-06 18:47:14 -04004274 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004275 * @phba: Pointer to HBA context object.
4276 *
4277 * This function issues a kill_board mailbox command and waits for
4278 * the error attention interrupt. This function is called for stopping
4279 * the firmware processing. The caller is not required to hold any
4280 * locks. This function calls lpfc_hba_down_post function to free
4281 * any pending commands after the kill. The function will return 1 when it
4282 * fails to kill the board else will return 0.
4283 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004284int
James Smart2e0fef82007-06-17 19:56:36 -05004285lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004286{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004287 struct lpfc_sli *psli;
4288 LPFC_MBOXQ_t *pmb;
4289 uint32_t status;
4290 uint32_t ha_copy;
4291 int retval;
4292 int i = 0;
4293
4294 psli = &phba->sli;
4295
4296 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004297 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004298 "0329 Kill HBA Data: x%x x%x\n",
4299 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004300
James Smart98c9ea52007-10-27 13:37:33 -04004301 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4302 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004303 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004304
4305 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004306 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004307 if (lpfc_readl(phba->HCregaddr, &status)) {
4308 spin_unlock_irq(&phba->hbalock);
4309 mempool_free(pmb, phba->mbox_mem_pool);
4310 return 1;
4311 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004312 status &= ~HC_ERINT_ENA;
4313 writel(status, phba->HCregaddr);
4314 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004315 phba->link_flag |= LS_IGNORE_ERATT;
4316 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004317
4318 lpfc_kill_board(phba, pmb);
4319 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4320 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4321
4322 if (retval != MBX_SUCCESS) {
4323 if (retval != MBX_BUSY)
4324 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004325 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4326 "2752 KILL_BOARD command failed retval %d\n",
4327 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004328 spin_lock_irq(&phba->hbalock);
4329 phba->link_flag &= ~LS_IGNORE_ERATT;
4330 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004331 return 1;
4332 }
4333
James Smartf4b4c682009-05-22 14:53:12 -04004334 spin_lock_irq(&phba->hbalock);
4335 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4336 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004337
Jamie Wellnitz41415862006-02-28 19:25:27 -05004338 mempool_free(pmb, phba->mbox_mem_pool);
4339
4340 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4341 * attention every 100ms for 3 seconds. If we don't get ERATT after
4342 * 3 seconds we still set HBA_ERROR state because the status of the
4343 * board is now undefined.
4344 */
James Smart9940b972011-03-11 16:06:12 -05004345 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4346 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004347 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4348 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004349 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4350 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004351 }
4352
4353 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004354 if (ha_copy & HA_ERATT) {
4355 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004356 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004357 }
James Smart2e0fef82007-06-17 19:56:36 -05004358 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004359 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004360 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004361 phba->link_flag &= ~LS_IGNORE_ERATT;
4362 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004363
Jamie Wellnitz41415862006-02-28 19:25:27 -05004364 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004365 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004366
James Smart2e0fef82007-06-17 19:56:36 -05004367 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004368}
4369
James Smarte59058c2008-08-24 21:49:00 -04004370/**
James Smart3772a992009-05-22 14:50:54 -04004371 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004372 * @phba: Pointer to HBA context object.
4373 *
4374 * This function resets the HBA by writing HC_INITFF to the control
4375 * register. After the HBA resets, this function resets all the iocb ring
4376 * indices. This function disables PCI layer parity checking during
4377 * the reset.
4378 * This function returns 0 always.
4379 * The caller is not required to hold any locks.
4380 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004381int
James Smart2e0fef82007-06-17 19:56:36 -05004382lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004383{
4384 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004385 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004386 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004387 int i;
dea31012005-04-17 16:05:31 -05004388
Jamie Wellnitz41415862006-02-28 19:25:27 -05004389 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004390
Jamie Wellnitz41415862006-02-28 19:25:27 -05004391 /* Reset HBA */
4392 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004393 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004394 (phba->pport) ? phba->pport->port_state : 0,
4395 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004396
4397 /* perform board reset */
4398 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004399 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004400 if (phba->pport) {
4401 phba->pport->fc_myDID = 0;
4402 phba->pport->fc_prevDID = 0;
4403 }
dea31012005-04-17 16:05:31 -05004404
Jamie Wellnitz41415862006-02-28 19:25:27 -05004405 /* Turn off parity checking and serr during the physical reset */
4406 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4407 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4408 (cfg_value &
4409 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4410
James Smart3772a992009-05-22 14:50:54 -04004411 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4412
Jamie Wellnitz41415862006-02-28 19:25:27 -05004413 /* Now toggle INITFF bit in the Host Control Register */
4414 writel(HC_INITFF, phba->HCregaddr);
4415 mdelay(1);
4416 readl(phba->HCregaddr); /* flush */
4417 writel(0, phba->HCregaddr);
4418 readl(phba->HCregaddr); /* flush */
4419
4420 /* Restore PCI cmd register */
4421 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004422
4423 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004424 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004425 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004426 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004427 pring->sli.sli3.rspidx = 0;
4428 pring->sli.sli3.next_cmdidx = 0;
4429 pring->sli.sli3.local_getidx = 0;
4430 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004431 pring->missbufcnt = 0;
4432 }
dea31012005-04-17 16:05:31 -05004433
James Smart2e0fef82007-06-17 19:56:36 -05004434 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004435 return 0;
4436}
4437
James Smarte59058c2008-08-24 21:49:00 -04004438/**
James Smartda0436e2009-05-22 14:51:39 -04004439 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4440 * @phba: Pointer to HBA context object.
4441 *
4442 * This function resets a SLI4 HBA. This function disables PCI layer parity
4443 * checking during resets the device. The caller is not required to hold
4444 * any locks.
4445 *
4446 * This function returns 0 always.
4447 **/
4448int
4449lpfc_sli4_brdreset(struct lpfc_hba *phba)
4450{
4451 struct lpfc_sli *psli = &phba->sli;
4452 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004453 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004454
4455 /* Reset HBA */
4456 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004457 "0295 Reset HBA Data: x%x x%x x%x\n",
4458 phba->pport->port_state, psli->sli_flag,
4459 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004460
4461 /* perform board reset */
4462 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004463 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004464 phba->pport->fc_myDID = 0;
4465 phba->pport->fc_prevDID = 0;
4466
James Smartda0436e2009-05-22 14:51:39 -04004467 spin_lock_irq(&phba->hbalock);
4468 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4469 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004470 spin_unlock_irq(&phba->hbalock);
4471
James Smart02936352014-04-04 13:52:12 -04004472 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4473 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4474 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4475 return rc;
4476 }
4477
James Smartda0436e2009-05-22 14:51:39 -04004478 /* Now physically reset the device */
4479 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4480 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004481
4482 /* Turn off parity checking and serr during the physical reset */
4483 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4484 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4485 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4486
James Smart88318812012-09-29 11:29:29 -04004487 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004488 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004489
James Smartbe858b62010-12-15 17:57:20 -05004490 /* Restore PCI cmd register */
4491 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4492
James Smart27b01b82012-05-09 21:19:44 -04004493 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004494}
4495
4496/**
4497 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004498 * @phba: Pointer to HBA context object.
4499 *
4500 * This function is called in the SLI initialization code path to
4501 * restart the HBA. The caller is not required to hold any lock.
4502 * This function writes MBX_RESTART mailbox command to the SLIM and
4503 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4504 * function to free any pending commands. The function enables
4505 * POST only during the first initialization. The function returns zero.
4506 * The function does not guarantee completion of MBX_RESTART mailbox
4507 * command before the return of this function.
4508 **/
James Smartda0436e2009-05-22 14:51:39 -04004509static int
4510lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004511{
4512 MAILBOX_t *mb;
4513 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004514 volatile uint32_t word0;
4515 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004516 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004517
James Smart2e0fef82007-06-17 19:56:36 -05004518 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004519
James Smart0d878412009-10-02 15:16:56 -04004520 /* Take PCIe device Advanced Error Reporting (AER) state */
4521 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4522
Jamie Wellnitz41415862006-02-28 19:25:27 -05004523 psli = &phba->sli;
4524
4525 /* Restart HBA */
4526 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004527 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004528 (phba->pport) ? phba->pport->port_state : 0,
4529 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004530
4531 word0 = 0;
4532 mb = (MAILBOX_t *) &word0;
4533 mb->mbxCommand = MBX_RESTART;
4534 mb->mbxHc = 1;
4535
James Smart92908312006-03-07 15:04:13 -05004536 lpfc_reset_barrier(phba);
4537
Jamie Wellnitz41415862006-02-28 19:25:27 -05004538 to_slim = phba->MBslimaddr;
4539 writel(*(uint32_t *) mb, to_slim);
4540 readl(to_slim); /* flush */
4541
4542 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004543 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004544 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004545 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004546 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004547 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004548 writel(*(uint32_t *) mb, to_slim);
4549 readl(to_slim); /* flush */
4550
4551 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004552 if (phba->pport)
4553 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004554 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004555 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004556 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004557
James Smart64ba8812006-08-02 15:24:34 -04004558 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4559 psli->stats_start = get_seconds();
4560
James Smarteaf15d52008-12-04 22:39:29 -05004561 /* Give the INITFF and Post time to settle. */
4562 mdelay(100);
dea31012005-04-17 16:05:31 -05004563
James Smart0d878412009-10-02 15:16:56 -04004564 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4565 if (hba_aer_enabled)
4566 pci_disable_pcie_error_reporting(phba->pcidev);
4567
Jamie Wellnitz41415862006-02-28 19:25:27 -05004568 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004569
4570 return 0;
4571}
4572
James Smarte59058c2008-08-24 21:49:00 -04004573/**
James Smartda0436e2009-05-22 14:51:39 -04004574 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4575 * @phba: Pointer to HBA context object.
4576 *
4577 * This function is called in the SLI initialization code path to restart
4578 * a SLI4 HBA. The caller is not required to hold any lock.
4579 * At the end of the function, it calls lpfc_hba_down_post function to
4580 * free any pending commands.
4581 **/
4582static int
4583lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4584{
4585 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004586 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004587 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004588
4589 /* Restart HBA */
4590 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4591 "0296 Restart HBA Data: x%x x%x\n",
4592 phba->pport->port_state, psli->sli_flag);
4593
James Smart75baf692010-06-08 18:31:21 -04004594 /* Take PCIe device Advanced Error Reporting (AER) state */
4595 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4596
James Smart27b01b82012-05-09 21:19:44 -04004597 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004598
4599 spin_lock_irq(&phba->hbalock);
4600 phba->pport->stopped = 0;
4601 phba->link_state = LPFC_INIT_START;
4602 phba->hba_flag = 0;
4603 spin_unlock_irq(&phba->hbalock);
4604
4605 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4606 psli->stats_start = get_seconds();
4607
James Smart75baf692010-06-08 18:31:21 -04004608 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4609 if (hba_aer_enabled)
4610 pci_disable_pcie_error_reporting(phba->pcidev);
4611
James Smartda0436e2009-05-22 14:51:39 -04004612 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004613 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004614
James Smart27b01b82012-05-09 21:19:44 -04004615 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004616}
4617
4618/**
4619 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4620 * @phba: Pointer to HBA context object.
4621 *
4622 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4623 * API jump table function pointer from the lpfc_hba struct.
4624**/
4625int
4626lpfc_sli_brdrestart(struct lpfc_hba *phba)
4627{
4628 return phba->lpfc_sli_brdrestart(phba);
4629}
4630
4631/**
James Smart3621a712009-04-06 18:47:14 -04004632 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004633 * @phba: Pointer to HBA context object.
4634 *
4635 * This function is called after a HBA restart to wait for successful
4636 * restart of the HBA. Successful restart of the HBA is indicated by
4637 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4638 * iteration, the function will restart the HBA again. The function returns
4639 * zero if HBA successfully restarted else returns negative error code.
4640 **/
James Smart4492b732017-04-27 15:08:26 -07004641int
dea31012005-04-17 16:05:31 -05004642lpfc_sli_chipset_init(struct lpfc_hba *phba)
4643{
4644 uint32_t status, i = 0;
4645
4646 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004647 if (lpfc_readl(phba->HSregaddr, &status))
4648 return -EIO;
dea31012005-04-17 16:05:31 -05004649
4650 /* Check status register to see what current state is */
4651 i = 0;
4652 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4653
James Smartdcf2a4e2010-09-29 11:18:53 -04004654 /* Check every 10ms for 10 retries, then every 100ms for 90
4655 * retries, then every 1 sec for 50 retires for a total of
4656 * ~60 seconds before reset the board again and check every
4657 * 1 sec for 50 retries. The up to 60 seconds before the
4658 * board ready is required by the Falcon FIPS zeroization
4659 * complete, and any reset the board in between shall cause
4660 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004661 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004662 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004663 /* Adapter failed to init, timeout, status reg
4664 <status> */
James Smarted957682007-06-17 19:56:37 -05004665 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004666 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004667 "timeout, status reg x%x, "
4668 "FW Data: A8 x%x AC x%x\n", status,
4669 readl(phba->MBslimaddr + 0xa8),
4670 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004671 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004672 return -ETIMEDOUT;
4673 }
4674
4675 /* Check to see if any errors occurred during init */
4676 if (status & HS_FFERM) {
4677 /* ERROR: During chipset initialization */
4678 /* Adapter failed to init, chipset, status reg
4679 <status> */
James Smarted957682007-06-17 19:56:37 -05004680 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004681 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004682 "chipset, status reg x%x, "
4683 "FW Data: A8 x%x AC x%x\n", status,
4684 readl(phba->MBslimaddr + 0xa8),
4685 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004686 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004687 return -EIO;
4688 }
4689
James Smartdcf2a4e2010-09-29 11:18:53 -04004690 if (i <= 10)
dea31012005-04-17 16:05:31 -05004691 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004692 else if (i <= 100)
4693 msleep(100);
4694 else
4695 msleep(1000);
dea31012005-04-17 16:05:31 -05004696
James Smartdcf2a4e2010-09-29 11:18:53 -04004697 if (i == 150) {
4698 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004699 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004700 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004701 }
4702 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004703 if (lpfc_readl(phba->HSregaddr, &status))
4704 return -EIO;
dea31012005-04-17 16:05:31 -05004705 }
4706
4707 /* Check to see if any errors occurred during init */
4708 if (status & HS_FFERM) {
4709 /* ERROR: During chipset initialization */
4710 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004711 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004712 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004713 "status reg x%x, "
4714 "FW Data: A8 x%x AC x%x\n", status,
4715 readl(phba->MBslimaddr + 0xa8),
4716 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004717 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004718 return -EIO;
4719 }
4720
4721 /* Clear all interrupt enable conditions */
4722 writel(0, phba->HCregaddr);
4723 readl(phba->HCregaddr); /* flush */
4724
4725 /* setup host attn register */
4726 writel(0xffffffff, phba->HAregaddr);
4727 readl(phba->HAregaddr); /* flush */
4728 return 0;
4729}
4730
James Smarte59058c2008-08-24 21:49:00 -04004731/**
James Smart3621a712009-04-06 18:47:14 -04004732 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004733 *
4734 * This function calculates and returns the number of HBQs required to be
4735 * configured.
4736 **/
James Smart78b2d852007-08-02 11:10:21 -04004737int
James Smarted957682007-06-17 19:56:37 -05004738lpfc_sli_hbq_count(void)
4739{
James Smart92d7f7b2007-06-17 19:56:38 -05004740 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004741}
4742
James Smarte59058c2008-08-24 21:49:00 -04004743/**
James Smart3621a712009-04-06 18:47:14 -04004744 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004745 *
4746 * This function adds the number of hbq entries in every HBQ to get
4747 * the total number of hbq entries required for the HBA and returns
4748 * the total count.
4749 **/
James Smarted957682007-06-17 19:56:37 -05004750static int
4751lpfc_sli_hbq_entry_count(void)
4752{
4753 int hbq_count = lpfc_sli_hbq_count();
4754 int count = 0;
4755 int i;
4756
4757 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004758 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004759 return count;
4760}
4761
James Smarte59058c2008-08-24 21:49:00 -04004762/**
James Smart3621a712009-04-06 18:47:14 -04004763 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004764 *
4765 * This function calculates amount of memory required for all hbq entries
4766 * to be configured and returns the total memory required.
4767 **/
dea31012005-04-17 16:05:31 -05004768int
James Smarted957682007-06-17 19:56:37 -05004769lpfc_sli_hbq_size(void)
4770{
4771 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4772}
4773
James Smarte59058c2008-08-24 21:49:00 -04004774/**
James Smart3621a712009-04-06 18:47:14 -04004775 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004776 * @phba: Pointer to HBA context object.
4777 *
4778 * This function is called during the SLI initialization to configure
4779 * all the HBQs and post buffers to the HBQ. The caller is not
4780 * required to hold any locks. This function will return zero if successful
4781 * else it will return negative error code.
4782 **/
James Smarted957682007-06-17 19:56:37 -05004783static int
4784lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4785{
4786 int hbq_count = lpfc_sli_hbq_count();
4787 LPFC_MBOXQ_t *pmb;
4788 MAILBOX_t *pmbox;
4789 uint32_t hbqno;
4790 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004791
James Smart92d7f7b2007-06-17 19:56:38 -05004792 /* Get a Mailbox buffer to setup mailbox
4793 * commands for HBA initialization
4794 */
James Smarted957682007-06-17 19:56:37 -05004795 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4796
4797 if (!pmb)
4798 return -ENOMEM;
4799
James Smart04c68492009-05-22 14:52:52 -04004800 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004801
4802 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4803 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004804 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004805
4806 hbq_entry_index = 0;
4807 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4808 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4809 phba->hbqs[hbqno].hbqPutIdx = 0;
4810 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4811 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004812 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004813 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4814 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004815 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4816
4817 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4818 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4819 mbxStatus <status>, ring <num> */
4820
4821 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004822 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004823 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004824 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004825 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004826 pmbox->mbxStatus, hbqno);
4827
4828 phba->link_state = LPFC_HBA_ERROR;
4829 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004830 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004831 }
4832 }
4833 phba->hbq_count = hbq_count;
4834
James Smarted957682007-06-17 19:56:37 -05004835 mempool_free(pmb, phba->mbox_mem_pool);
4836
James Smart92d7f7b2007-06-17 19:56:38 -05004837 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004838 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4839 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004840 return 0;
4841}
4842
James Smarte59058c2008-08-24 21:49:00 -04004843/**
James Smart4f774512009-05-22 14:52:35 -04004844 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4845 * @phba: Pointer to HBA context object.
4846 *
4847 * This function is called during the SLI initialization to configure
4848 * all the HBQs and post buffers to the HBQ. The caller is not
4849 * required to hold any locks. This function will return zero if successful
4850 * else it will return negative error code.
4851 **/
4852static int
4853lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4854{
4855 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004856 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4857 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004858 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004859 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004860 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004861 return 0;
4862}
4863
4864/**
James Smart3621a712009-04-06 18:47:14 -04004865 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004866 * @phba: Pointer to HBA context object.
4867 * @sli_mode: sli mode - 2/3
4868 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004869 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004870 * to issue config_port mailbox command. This function restarts the
4871 * HBA firmware and issues a config_port mailbox command to configure
4872 * the SLI interface in the sli mode specified by sli_mode
4873 * variable. The caller is not required to hold any locks.
4874 * The function returns 0 if successful, else returns negative error
4875 * code.
4876 **/
James Smart93996272008-08-24 21:50:30 -04004877int
4878lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004879{
4880 LPFC_MBOXQ_t *pmb;
4881 uint32_t resetcount = 0, rc = 0, done = 0;
4882
4883 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4884 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004885 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004886 return -ENOMEM;
4887 }
4888
James Smarted957682007-06-17 19:56:37 -05004889 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004890 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004891 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004892 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004893 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004894 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004895 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004896 rc = lpfc_sli_chipset_init(phba);
4897 if (rc)
4898 break;
4899
James Smart2e0fef82007-06-17 19:56:36 -05004900 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004901 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004902 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004903 resetcount++;
4904
James Smarted957682007-06-17 19:56:37 -05004905 /* Call pre CONFIG_PORT mailbox command initialization. A
4906 * value of 0 means the call was successful. Any other
4907 * nonzero value is a failure, but if ERESTART is returned,
4908 * the driver may reset the HBA and try again.
4909 */
dea31012005-04-17 16:05:31 -05004910 rc = lpfc_config_port_prep(phba);
4911 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004912 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004913 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004914 } else if (rc)
dea31012005-04-17 16:05:31 -05004915 break;
James Smart6d368e52011-05-24 11:44:12 -04004916
James Smart2e0fef82007-06-17 19:56:36 -05004917 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004918 lpfc_config_port(phba, pmb);
4919 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004920 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4921 LPFC_SLI3_HBQ_ENABLED |
4922 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004923 LPFC_SLI3_BG_ENABLED |
4924 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004925 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004926 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004927 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004928 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004929 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004930 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004931 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004932 spin_unlock_irq(&phba->hbalock);
4933 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004934 } else {
4935 /* Allow asynchronous mailbox command to go through */
4936 spin_lock_irq(&phba->hbalock);
4937 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4938 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004939 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004940
4941 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4942 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4943 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4944 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004945 }
dea31012005-04-17 16:05:31 -05004946 }
James Smarted957682007-06-17 19:56:37 -05004947 if (!done) {
4948 rc = -EINVAL;
4949 goto do_prep_failed;
4950 }
James Smart04c68492009-05-22 14:52:52 -04004951 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4952 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004953 rc = -ENXIO;
4954 goto do_prep_failed;
4955 }
James Smart04c68492009-05-22 14:52:52 -04004956 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004957 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004958 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4959 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4960 phba->max_vpi : phba->max_vports;
4961
James Smart34b02dc2008-08-24 21:49:55 -04004962 } else
4963 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004964 phba->fips_level = 0;
4965 phba->fips_spec_rev = 0;
4966 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004967 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004968 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4969 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4970 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4971 "2850 Security Crypto Active. FIPS x%d "
4972 "(Spec Rev: x%d)",
4973 phba->fips_level, phba->fips_spec_rev);
4974 }
4975 if (pmb->u.mb.un.varCfgPort.sec_err) {
4976 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4977 "2856 Config Port Security Crypto "
4978 "Error: x%x ",
4979 pmb->u.mb.un.varCfgPort.sec_err);
4980 }
James Smart04c68492009-05-22 14:52:52 -04004981 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004982 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004983 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004984 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004985
4986 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4987 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004988
4989 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004990 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004991 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4992 else
4993 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4994 "0443 Adapter did not grant "
4995 "BlockGuard\n");
4996 }
James Smart34b02dc2008-08-24 21:49:55 -04004997 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004998 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004999 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04005000 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05005001 }
James Smart92d7f7b2007-06-17 19:56:38 -05005002do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05005003 mempool_free(pmb, phba->mbox_mem_pool);
5004 return rc;
5005}
5006
James Smarte59058c2008-08-24 21:49:00 -04005007
5008/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08005009 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04005010 * @phba: Pointer to HBA context object.
5011 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005012 * This function is the main SLI initialization function. This function
5013 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04005014 * error attention handler code. Caller is not required to hold any
5015 * locks. This function issues config_port mailbox command to configure
5016 * the SLI, setup iocb rings and HBQ rings. In the end the function
5017 * calls the config_port_post function to issue init_link mailbox
5018 * command and to start the discovery. The function will return zero
5019 * if successful, else it will return negative error code.
5020 **/
James Smarted957682007-06-17 19:56:37 -05005021int
5022lpfc_sli_hba_setup(struct lpfc_hba *phba)
5023{
5024 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04005025 int mode = 3, i;
5026 int longs;
James Smarted957682007-06-17 19:56:37 -05005027
James Smart12247e82016-07-06 12:36:09 -07005028 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05005029 case 2:
James Smart78b2d852007-08-02 11:10:21 -04005030 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05005031 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005032 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05005033 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07005034 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05005035 break;
5036 }
James Smarted957682007-06-17 19:56:37 -05005037 mode = 2;
5038 break;
5039 case 0:
5040 case 3:
5041 break;
5042 default:
James Smart92d7f7b2007-06-17 19:56:38 -05005043 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005044 "1819 Unrecognized sli_mode parameter: %d.\n",
5045 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05005046
5047 break;
5048 }
James Smartb5c53952016-03-31 14:12:30 -07005049 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005050
James Smart93996272008-08-24 21:50:30 -04005051 rc = lpfc_sli_config_port(phba, mode);
5052
James Smart12247e82016-07-06 12:36:09 -07005053 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05005054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005055 "1820 Unable to select SLI-3. "
5056 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05005057 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04005058 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07005059 else if (rc && mode == 2)
5060 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05005061 if (rc)
dea31012005-04-17 16:05:31 -05005062 goto lpfc_sli_hba_setup_error;
5063
James Smart0d878412009-10-02 15:16:56 -04005064 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5065 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5066 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5067 if (!rc) {
5068 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5069 "2709 This device supports "
5070 "Advanced Error Reporting (AER)\n");
5071 spin_lock_irq(&phba->hbalock);
5072 phba->hba_flag |= HBA_AER_ENABLED;
5073 spin_unlock_irq(&phba->hbalock);
5074 } else {
5075 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5076 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005077 "Advanced Error Reporting (AER): %d\n",
5078 rc);
James Smart0d878412009-10-02 15:16:56 -04005079 phba->cfg_aer_support = 0;
5080 }
5081 }
5082
James Smarted957682007-06-17 19:56:37 -05005083 if (phba->sli_rev == 3) {
5084 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5085 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005086 } else {
5087 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5088 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005089 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005090 }
5091
5092 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005093 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5094 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005095 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005096
5097 if (rc)
5098 goto lpfc_sli_hba_setup_error;
5099
James Smart6d368e52011-05-24 11:44:12 -04005100 /* Initialize VPIs. */
5101 if (phba->sli_rev == LPFC_SLI_REV3) {
5102 /*
5103 * The VPI bitmask and physical ID array are allocated
5104 * and initialized once only - at driver load. A port
5105 * reset doesn't need to reinitialize this memory.
5106 */
5107 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5108 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5109 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
5110 GFP_KERNEL);
5111 if (!phba->vpi_bmask) {
5112 rc = -ENOMEM;
5113 goto lpfc_sli_hba_setup_error;
5114 }
5115
5116 phba->vpi_ids = kzalloc(
5117 (phba->max_vpi+1) * sizeof(uint16_t),
5118 GFP_KERNEL);
5119 if (!phba->vpi_ids) {
5120 kfree(phba->vpi_bmask);
5121 rc = -ENOMEM;
5122 goto lpfc_sli_hba_setup_error;
5123 }
5124 for (i = 0; i < phba->max_vpi; i++)
5125 phba->vpi_ids[i] = i;
5126 }
5127 }
5128
James Smart93996272008-08-24 21:50:30 -04005129 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005130 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5131 rc = lpfc_sli_hbq_setup(phba);
5132 if (rc)
5133 goto lpfc_sli_hba_setup_error;
5134 }
James Smart04c68492009-05-22 14:52:52 -04005135 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005136 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005137 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005138
5139 rc = lpfc_config_port_post(phba);
5140 if (rc)
5141 goto lpfc_sli_hba_setup_error;
5142
James Smarted957682007-06-17 19:56:37 -05005143 return rc;
5144
James Smart92d7f7b2007-06-17 19:56:38 -05005145lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005146 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05005147 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005148 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005149 return rc;
5150}
5151
James Smartda0436e2009-05-22 14:51:39 -04005152/**
5153 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5154 * @phba: Pointer to HBA context object.
5155 * @mboxq: mailbox pointer.
5156 * This function issue a dump mailbox command to read config region
5157 * 23 and parse the records in the region and populate driver
5158 * data structure.
5159 **/
5160static int
James Smartff78d8f2011-12-13 13:21:35 -05005161lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005162{
James Smartff78d8f2011-12-13 13:21:35 -05005163 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005164 struct lpfc_dmabuf *mp;
5165 struct lpfc_mqe *mqe;
5166 uint32_t data_length;
5167 int rc;
5168
5169 /* Program the default value of vlan_id and fc_map */
5170 phba->valid_vlan = 0;
5171 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5172 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5173 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5174
James Smartff78d8f2011-12-13 13:21:35 -05005175 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5176 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005177 return -ENOMEM;
5178
James Smartff78d8f2011-12-13 13:21:35 -05005179 mqe = &mboxq->u.mqe;
5180 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5181 rc = -ENOMEM;
5182 goto out_free_mboxq;
5183 }
5184
James Smartda0436e2009-05-22 14:51:39 -04005185 mp = (struct lpfc_dmabuf *) mboxq->context1;
5186 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5187
5188 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5189 "(%d):2571 Mailbox cmd x%x Status x%x "
5190 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5191 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5192 "CQ: x%x x%x x%x x%x\n",
5193 mboxq->vport ? mboxq->vport->vpi : 0,
5194 bf_get(lpfc_mqe_command, mqe),
5195 bf_get(lpfc_mqe_status, mqe),
5196 mqe->un.mb_words[0], mqe->un.mb_words[1],
5197 mqe->un.mb_words[2], mqe->un.mb_words[3],
5198 mqe->un.mb_words[4], mqe->un.mb_words[5],
5199 mqe->un.mb_words[6], mqe->un.mb_words[7],
5200 mqe->un.mb_words[8], mqe->un.mb_words[9],
5201 mqe->un.mb_words[10], mqe->un.mb_words[11],
5202 mqe->un.mb_words[12], mqe->un.mb_words[13],
5203 mqe->un.mb_words[14], mqe->un.mb_words[15],
5204 mqe->un.mb_words[16], mqe->un.mb_words[50],
5205 mboxq->mcqe.word0,
5206 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5207 mboxq->mcqe.trailer);
5208
5209 if (rc) {
5210 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5211 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005212 rc = -EIO;
5213 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005214 }
5215 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005216 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005217 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5218 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005219 rc = -EIO;
5220 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005221 }
James Smartda0436e2009-05-22 14:51:39 -04005222
5223 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5224 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5225 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005226 rc = 0;
5227
5228out_free_mboxq:
5229 mempool_free(mboxq, phba->mbox_mem_pool);
5230 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005231}
5232
5233/**
5234 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5235 * @phba: pointer to lpfc hba data structure.
5236 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5237 * @vpd: pointer to the memory to hold resulting port vpd data.
5238 * @vpd_size: On input, the number of bytes allocated to @vpd.
5239 * On output, the number of data bytes in @vpd.
5240 *
5241 * This routine executes a READ_REV SLI4 mailbox command. In
5242 * addition, this routine gets the port vpd data.
5243 *
5244 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005245 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005246 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005247 **/
5248static int
5249lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5250 uint8_t *vpd, uint32_t *vpd_size)
5251{
5252 int rc = 0;
5253 uint32_t dma_size;
5254 struct lpfc_dmabuf *dmabuf;
5255 struct lpfc_mqe *mqe;
5256
5257 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5258 if (!dmabuf)
5259 return -ENOMEM;
5260
5261 /*
5262 * Get a DMA buffer for the vpd data resulting from the READ_REV
5263 * mailbox command.
5264 */
5265 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04005266 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5267 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005268 if (!dmabuf->virt) {
5269 kfree(dmabuf);
5270 return -ENOMEM;
5271 }
James Smartda0436e2009-05-22 14:51:39 -04005272
5273 /*
5274 * The SLI4 implementation of READ_REV conflicts at word1,
5275 * bits 31:16 and SLI4 adds vpd functionality not present
5276 * in SLI3. This code corrects the conflicts.
5277 */
5278 lpfc_read_rev(phba, mboxq);
5279 mqe = &mboxq->u.mqe;
5280 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5281 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5282 mqe->un.read_rev.word1 &= 0x0000FFFF;
5283 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5284 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5285
5286 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5287 if (rc) {
5288 dma_free_coherent(&phba->pcidev->dev, dma_size,
5289 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005290 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005291 return -EIO;
5292 }
5293
James Smartda0436e2009-05-22 14:51:39 -04005294 /*
5295 * The available vpd length cannot be bigger than the
5296 * DMA buffer passed to the port. Catch the less than
5297 * case and update the caller's size.
5298 */
5299 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5300 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5301
James Smartd7c47992010-06-08 18:31:54 -04005302 memcpy(vpd, dmabuf->virt, *vpd_size);
5303
James Smartda0436e2009-05-22 14:51:39 -04005304 dma_free_coherent(&phba->pcidev->dev, dma_size,
5305 dmabuf->virt, dmabuf->phys);
5306 kfree(dmabuf);
5307 return 0;
5308}
5309
5310/**
James Smartcd1c8302011-10-10 21:33:25 -04005311 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5312 * @phba: pointer to lpfc hba data structure.
5313 *
5314 * This routine retrieves SLI4 device physical port name this PCI function
5315 * is attached to.
5316 *
5317 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005318 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005319 * otherwise - failed to retrieve physical port name
5320 **/
5321static int
5322lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5323{
5324 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005325 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5326 struct lpfc_controller_attribute *cntl_attr;
5327 struct lpfc_mbx_get_port_name *get_port_name;
5328 void *virtaddr = NULL;
5329 uint32_t alloclen, reqlen;
5330 uint32_t shdr_status, shdr_add_status;
5331 union lpfc_sli4_cfg_shdr *shdr;
5332 char cport_name = 0;
5333 int rc;
5334
5335 /* We assume nothing at this point */
5336 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5337 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5338
5339 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5340 if (!mboxq)
5341 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005342 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005343 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5344 lpfc_sli4_read_config(phba);
5345 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5346 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005347
5348 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5349 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5350 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5351 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5352 LPFC_SLI4_MBX_NEMBED);
5353 if (alloclen < reqlen) {
5354 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5355 "3084 Allocated DMA memory size (%d) is "
5356 "less than the requested DMA memory size "
5357 "(%d)\n", alloclen, reqlen);
5358 rc = -ENOMEM;
5359 goto out_free_mboxq;
5360 }
5361 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5362 virtaddr = mboxq->sge_array->addr[0];
5363 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5364 shdr = &mbx_cntl_attr->cfg_shdr;
5365 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5366 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5367 if (shdr_status || shdr_add_status || rc) {
5368 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5369 "3085 Mailbox x%x (x%x/x%x) failed, "
5370 "rc:x%x, status:x%x, add_status:x%x\n",
5371 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5372 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5373 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5374 rc, shdr_status, shdr_add_status);
5375 rc = -ENXIO;
5376 goto out_free_mboxq;
5377 }
5378 cntl_attr = &mbx_cntl_attr->cntl_attr;
5379 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5380 phba->sli4_hba.lnk_info.lnk_tp =
5381 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5382 phba->sli4_hba.lnk_info.lnk_no =
5383 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5384 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5385 "3086 lnk_type:%d, lnk_numb:%d\n",
5386 phba->sli4_hba.lnk_info.lnk_tp,
5387 phba->sli4_hba.lnk_info.lnk_no);
5388
5389retrieve_ppname:
5390 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5391 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5392 sizeof(struct lpfc_mbx_get_port_name) -
5393 sizeof(struct lpfc_sli4_cfg_mhdr),
5394 LPFC_SLI4_MBX_EMBED);
5395 get_port_name = &mboxq->u.mqe.un.get_port_name;
5396 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5397 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5398 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5399 phba->sli4_hba.lnk_info.lnk_tp);
5400 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5401 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5402 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5403 if (shdr_status || shdr_add_status || rc) {
5404 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5405 "3087 Mailbox x%x (x%x/x%x) failed: "
5406 "rc:x%x, status:x%x, add_status:x%x\n",
5407 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5408 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5409 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5410 rc, shdr_status, shdr_add_status);
5411 rc = -ENXIO;
5412 goto out_free_mboxq;
5413 }
5414 switch (phba->sli4_hba.lnk_info.lnk_no) {
5415 case LPFC_LINK_NUMBER_0:
5416 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5417 &get_port_name->u.response);
5418 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5419 break;
5420 case LPFC_LINK_NUMBER_1:
5421 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5422 &get_port_name->u.response);
5423 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5424 break;
5425 case LPFC_LINK_NUMBER_2:
5426 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5427 &get_port_name->u.response);
5428 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5429 break;
5430 case LPFC_LINK_NUMBER_3:
5431 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5432 &get_port_name->u.response);
5433 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5434 break;
5435 default:
5436 break;
5437 }
5438
5439 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5440 phba->Port[0] = cport_name;
5441 phba->Port[1] = '\0';
5442 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5443 "3091 SLI get port name: %s\n", phba->Port);
5444 }
5445
5446out_free_mboxq:
5447 if (rc != MBX_TIMEOUT) {
5448 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5449 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5450 else
5451 mempool_free(mboxq, phba->mbox_mem_pool);
5452 }
5453 return rc;
5454}
5455
5456/**
James Smartda0436e2009-05-22 14:51:39 -04005457 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5458 * @phba: pointer to lpfc hba data structure.
5459 *
5460 * This routine is called to explicitly arm the SLI4 device's completion and
5461 * event queues
5462 **/
5463static void
5464lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5465{
James Smart895427b2017-02-12 13:52:30 -08005466 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08005467 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartda0436e2009-05-22 14:51:39 -04005468
James Smartb71413d2018-02-22 08:18:40 -08005469 sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
5470 sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
5471 if (sli4_hba->nvmels_cq)
5472 sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
James Smart895427b2017-02-12 13:52:30 -08005473 LPFC_QUEUE_REARM);
5474
James Smartb71413d2018-02-22 08:18:40 -08005475 if (sli4_hba->fcp_cq)
James Smart895427b2017-02-12 13:52:30 -08005476 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005477 sli4_hba->sli4_cq_release(sli4_hba->fcp_cq[qidx],
James Smart895427b2017-02-12 13:52:30 -08005478 LPFC_QUEUE_REARM);
5479
James Smartb71413d2018-02-22 08:18:40 -08005480 if (sli4_hba->nvme_cq)
James Smart895427b2017-02-12 13:52:30 -08005481 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005482 sli4_hba->sli4_cq_release(sli4_hba->nvme_cq[qidx],
James Smart895427b2017-02-12 13:52:30 -08005483 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005484
James Smartf38fa0b2014-04-04 13:52:21 -04005485 if (phba->cfg_fof)
James Smartb71413d2018-02-22 08:18:40 -08005486 sli4_hba->sli4_cq_release(sli4_hba->oas_cq, LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005487
James Smartb71413d2018-02-22 08:18:40 -08005488 if (sli4_hba->hba_eq)
James Smart895427b2017-02-12 13:52:30 -08005489 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005490 sli4_hba->sli4_eq_release(sli4_hba->hba_eq[qidx],
5491 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005492
James Smart2d7dbc42017-02-12 13:52:35 -08005493 if (phba->nvmet_support) {
5494 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smartb71413d2018-02-22 08:18:40 -08005495 sli4_hba->sli4_cq_release(
5496 sli4_hba->nvmet_cqset[qidx],
James Smart2d7dbc42017-02-12 13:52:35 -08005497 LPFC_QUEUE_REARM);
5498 }
James Smart2e90f4b2011-12-13 13:22:37 -05005499 }
James Smart1ba981f2014-02-20 09:56:45 -05005500
5501 if (phba->cfg_fof)
James Smartb71413d2018-02-22 08:18:40 -08005502 sli4_hba->sli4_eq_release(sli4_hba->fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005503}
5504
5505/**
James Smart6d368e52011-05-24 11:44:12 -04005506 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5507 * @phba: Pointer to HBA context object.
5508 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005509 * @extnt_count: buffer to hold port available extent count.
5510 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005511 *
James Smartb76f2dc2011-07-22 18:37:42 -04005512 * This function calls the port and retrievs the number of available
5513 * extents and their size for a particular extent type.
5514 *
5515 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005516 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005517int
James Smart6d368e52011-05-24 11:44:12 -04005518lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5519 uint16_t *extnt_count, uint16_t *extnt_size)
5520{
5521 int rc = 0;
5522 uint32_t length;
5523 uint32_t mbox_tmo;
5524 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5525 LPFC_MBOXQ_t *mbox;
5526
5527 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5528 if (!mbox)
5529 return -ENOMEM;
5530
5531 /* Find out how many extents are available for this resource type */
5532 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5533 sizeof(struct lpfc_sli4_cfg_mhdr));
5534 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5535 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5536 length, LPFC_SLI4_MBX_EMBED);
5537
5538 /* Send an extents count of 0 - the GET doesn't use it. */
5539 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5540 LPFC_SLI4_MBX_EMBED);
5541 if (unlikely(rc)) {
5542 rc = -EIO;
5543 goto err_exit;
5544 }
5545
5546 if (!phba->sli4_hba.intr_enable)
5547 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5548 else {
James Smarta183a152011-10-10 21:32:43 -04005549 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005550 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5551 }
5552 if (unlikely(rc)) {
5553 rc = -EIO;
5554 goto err_exit;
5555 }
5556
5557 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5558 if (bf_get(lpfc_mbox_hdr_status,
5559 &rsrc_info->header.cfg_shdr.response)) {
5560 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5561 "2930 Failed to get resource extents "
5562 "Status 0x%x Add'l Status 0x%x\n",
5563 bf_get(lpfc_mbox_hdr_status,
5564 &rsrc_info->header.cfg_shdr.response),
5565 bf_get(lpfc_mbox_hdr_add_status,
5566 &rsrc_info->header.cfg_shdr.response));
5567 rc = -EIO;
5568 goto err_exit;
5569 }
5570
5571 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5572 &rsrc_info->u.rsp);
5573 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5574 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005575
5576 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5577 "3162 Retrieved extents type-%d from port: count:%d, "
5578 "size:%d\n", type, *extnt_count, *extnt_size);
5579
5580err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005581 mempool_free(mbox, phba->mbox_mem_pool);
5582 return rc;
5583}
5584
5585/**
5586 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5587 * @phba: Pointer to HBA context object.
5588 * @type: The extent type to check.
5589 *
5590 * This function reads the current available extents from the port and checks
5591 * if the extent count or extent size has changed since the last access.
5592 * Callers use this routine post port reset to understand if there is a
5593 * extent reprovisioning requirement.
5594 *
5595 * Returns:
5596 * -Error: error indicates problem.
5597 * 1: Extent count or size has changed.
5598 * 0: No changes.
5599 **/
5600static int
5601lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5602{
5603 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5604 uint16_t size_diff, rsrc_ext_size;
5605 int rc = 0;
5606 struct lpfc_rsrc_blks *rsrc_entry;
5607 struct list_head *rsrc_blk_list = NULL;
5608
5609 size_diff = 0;
5610 curr_ext_cnt = 0;
5611 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5612 &rsrc_ext_cnt,
5613 &rsrc_ext_size);
5614 if (unlikely(rc))
5615 return -EIO;
5616
5617 switch (type) {
5618 case LPFC_RSC_TYPE_FCOE_RPI:
5619 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5620 break;
5621 case LPFC_RSC_TYPE_FCOE_VPI:
5622 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5623 break;
5624 case LPFC_RSC_TYPE_FCOE_XRI:
5625 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5626 break;
5627 case LPFC_RSC_TYPE_FCOE_VFI:
5628 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5629 break;
5630 default:
5631 break;
5632 }
5633
5634 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5635 curr_ext_cnt++;
5636 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5637 size_diff++;
5638 }
5639
5640 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5641 rc = 1;
5642
5643 return rc;
5644}
5645
5646/**
5647 * lpfc_sli4_cfg_post_extnts -
5648 * @phba: Pointer to HBA context object.
5649 * @extnt_cnt - number of available extents.
5650 * @type - the extent type (rpi, xri, vfi, vpi).
5651 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5652 * @mbox - pointer to the caller's allocated mailbox structure.
5653 *
5654 * This function executes the extents allocation request. It also
5655 * takes care of the amount of memory needed to allocate or get the
5656 * allocated extents. It is the caller's responsibility to evaluate
5657 * the response.
5658 *
5659 * Returns:
5660 * -Error: Error value describes the condition found.
5661 * 0: if successful
5662 **/
5663static int
James Smart8a9d2e82012-05-09 21:16:12 -04005664lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005665 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5666{
5667 int rc = 0;
5668 uint32_t req_len;
5669 uint32_t emb_len;
5670 uint32_t alloc_len, mbox_tmo;
5671
5672 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005673 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005674
5675 /*
5676 * Calculate the size of an embedded mailbox. The uint32_t
5677 * accounts for extents-specific word.
5678 */
5679 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5680 sizeof(uint32_t);
5681
5682 /*
5683 * Presume the allocation and response will fit into an embedded
5684 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5685 */
5686 *emb = LPFC_SLI4_MBX_EMBED;
5687 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005688 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005689 sizeof(union lpfc_sli4_cfg_shdr) +
5690 sizeof(uint32_t);
5691 *emb = LPFC_SLI4_MBX_NEMBED;
5692 }
5693
5694 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5695 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5696 req_len, *emb);
5697 if (alloc_len < req_len) {
5698 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005699 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005700 "less than the requested DMA memory "
5701 "size (x%x)\n", alloc_len, req_len);
5702 return -ENOMEM;
5703 }
James Smart8a9d2e82012-05-09 21:16:12 -04005704 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005705 if (unlikely(rc))
5706 return -EIO;
5707
5708 if (!phba->sli4_hba.intr_enable)
5709 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5710 else {
James Smarta183a152011-10-10 21:32:43 -04005711 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005712 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5713 }
5714
5715 if (unlikely(rc))
5716 rc = -EIO;
5717 return rc;
5718}
5719
5720/**
5721 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5722 * @phba: Pointer to HBA context object.
5723 * @type: The resource extent type to allocate.
5724 *
5725 * This function allocates the number of elements for the specified
5726 * resource type.
5727 **/
5728static int
5729lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5730{
5731 bool emb = false;
5732 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5733 uint16_t rsrc_id, rsrc_start, j, k;
5734 uint16_t *ids;
5735 int i, rc;
5736 unsigned long longs;
5737 unsigned long *bmask;
5738 struct lpfc_rsrc_blks *rsrc_blks;
5739 LPFC_MBOXQ_t *mbox;
5740 uint32_t length;
5741 struct lpfc_id_range *id_array = NULL;
5742 void *virtaddr = NULL;
5743 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5744 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5745 struct list_head *ext_blk_list;
5746
5747 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5748 &rsrc_cnt,
5749 &rsrc_size);
5750 if (unlikely(rc))
5751 return -EIO;
5752
5753 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5754 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5755 "3009 No available Resource Extents "
5756 "for resource type 0x%x: Count: 0x%x, "
5757 "Size 0x%x\n", type, rsrc_cnt,
5758 rsrc_size);
5759 return -ENOMEM;
5760 }
5761
James Smart8a9d2e82012-05-09 21:16:12 -04005762 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5763 "2903 Post resource extents type-0x%x: "
5764 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005765
5766 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5767 if (!mbox)
5768 return -ENOMEM;
5769
James Smart8a9d2e82012-05-09 21:16:12 -04005770 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005771 if (unlikely(rc)) {
5772 rc = -EIO;
5773 goto err_exit;
5774 }
5775
5776 /*
5777 * Figure out where the response is located. Then get local pointers
5778 * to the response data. The port does not guarantee to respond to
5779 * all extents counts request so update the local variable with the
5780 * allocated count from the port.
5781 */
5782 if (emb == LPFC_SLI4_MBX_EMBED) {
5783 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5784 id_array = &rsrc_ext->u.rsp.id[0];
5785 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5786 } else {
5787 virtaddr = mbox->sge_array->addr[0];
5788 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5789 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5790 id_array = &n_rsrc->id;
5791 }
5792
5793 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5794 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5795
5796 /*
5797 * Based on the resource size and count, correct the base and max
5798 * resource values.
5799 */
5800 length = sizeof(struct lpfc_rsrc_blks);
5801 switch (type) {
5802 case LPFC_RSC_TYPE_FCOE_RPI:
5803 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5804 sizeof(unsigned long),
5805 GFP_KERNEL);
5806 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5807 rc = -ENOMEM;
5808 goto err_exit;
5809 }
5810 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5811 sizeof(uint16_t),
5812 GFP_KERNEL);
5813 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5814 kfree(phba->sli4_hba.rpi_bmask);
5815 rc = -ENOMEM;
5816 goto err_exit;
5817 }
5818
5819 /*
5820 * The next_rpi was initialized with the maximum available
5821 * count but the port may allocate a smaller number. Catch
5822 * that case and update the next_rpi.
5823 */
5824 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5825
5826 /* Initialize local ptrs for common extent processing later. */
5827 bmask = phba->sli4_hba.rpi_bmask;
5828 ids = phba->sli4_hba.rpi_ids;
5829 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5830 break;
5831 case LPFC_RSC_TYPE_FCOE_VPI:
5832 phba->vpi_bmask = kzalloc(longs *
5833 sizeof(unsigned long),
5834 GFP_KERNEL);
5835 if (unlikely(!phba->vpi_bmask)) {
5836 rc = -ENOMEM;
5837 goto err_exit;
5838 }
5839 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5840 sizeof(uint16_t),
5841 GFP_KERNEL);
5842 if (unlikely(!phba->vpi_ids)) {
5843 kfree(phba->vpi_bmask);
5844 rc = -ENOMEM;
5845 goto err_exit;
5846 }
5847
5848 /* Initialize local ptrs for common extent processing later. */
5849 bmask = phba->vpi_bmask;
5850 ids = phba->vpi_ids;
5851 ext_blk_list = &phba->lpfc_vpi_blk_list;
5852 break;
5853 case LPFC_RSC_TYPE_FCOE_XRI:
5854 phba->sli4_hba.xri_bmask = kzalloc(longs *
5855 sizeof(unsigned long),
5856 GFP_KERNEL);
5857 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5858 rc = -ENOMEM;
5859 goto err_exit;
5860 }
James Smart8a9d2e82012-05-09 21:16:12 -04005861 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005862 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5863 sizeof(uint16_t),
5864 GFP_KERNEL);
5865 if (unlikely(!phba->sli4_hba.xri_ids)) {
5866 kfree(phba->sli4_hba.xri_bmask);
5867 rc = -ENOMEM;
5868 goto err_exit;
5869 }
5870
5871 /* Initialize local ptrs for common extent processing later. */
5872 bmask = phba->sli4_hba.xri_bmask;
5873 ids = phba->sli4_hba.xri_ids;
5874 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5875 break;
5876 case LPFC_RSC_TYPE_FCOE_VFI:
5877 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5878 sizeof(unsigned long),
5879 GFP_KERNEL);
5880 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5881 rc = -ENOMEM;
5882 goto err_exit;
5883 }
5884 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5885 sizeof(uint16_t),
5886 GFP_KERNEL);
5887 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5888 kfree(phba->sli4_hba.vfi_bmask);
5889 rc = -ENOMEM;
5890 goto err_exit;
5891 }
5892
5893 /* Initialize local ptrs for common extent processing later. */
5894 bmask = phba->sli4_hba.vfi_bmask;
5895 ids = phba->sli4_hba.vfi_ids;
5896 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5897 break;
5898 default:
5899 /* Unsupported Opcode. Fail call. */
5900 id_array = NULL;
5901 bmask = NULL;
5902 ids = NULL;
5903 ext_blk_list = NULL;
5904 goto err_exit;
5905 }
5906
5907 /*
5908 * Complete initializing the extent configuration with the
5909 * allocated ids assigned to this function. The bitmask serves
5910 * as an index into the array and manages the available ids. The
5911 * array just stores the ids communicated to the port via the wqes.
5912 */
5913 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5914 if ((i % 2) == 0)
5915 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5916 &id_array[k]);
5917 else
5918 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5919 &id_array[k]);
5920
5921 rsrc_blks = kzalloc(length, GFP_KERNEL);
5922 if (unlikely(!rsrc_blks)) {
5923 rc = -ENOMEM;
5924 kfree(bmask);
5925 kfree(ids);
5926 goto err_exit;
5927 }
5928 rsrc_blks->rsrc_start = rsrc_id;
5929 rsrc_blks->rsrc_size = rsrc_size;
5930 list_add_tail(&rsrc_blks->list, ext_blk_list);
5931 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08005932 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart6d368e52011-05-24 11:44:12 -04005933 phba->sli4_hba.scsi_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08005934 lpfc_sli4_get_iocb_cnt(phba);
5935 phba->sli4_hba.nvme_xri_start =
5936 phba->sli4_hba.scsi_xri_start +
5937 phba->sli4_hba.scsi_xri_max;
5938 }
James Smart6d368e52011-05-24 11:44:12 -04005939
5940 while (rsrc_id < (rsrc_start + rsrc_size)) {
5941 ids[j] = rsrc_id;
5942 rsrc_id++;
5943 j++;
5944 }
5945 /* Entire word processed. Get next word.*/
5946 if ((i % 2) == 1)
5947 k++;
5948 }
5949 err_exit:
5950 lpfc_sli4_mbox_cmd_free(phba, mbox);
5951 return rc;
5952}
5953
James Smart895427b2017-02-12 13:52:30 -08005954
5955
James Smart6d368e52011-05-24 11:44:12 -04005956/**
5957 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5958 * @phba: Pointer to HBA context object.
5959 * @type: the extent's type.
5960 *
5961 * This function deallocates all extents of a particular resource type.
5962 * SLI4 does not allow for deallocating a particular extent range. It
5963 * is the caller's responsibility to release all kernel memory resources.
5964 **/
5965static int
5966lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5967{
5968 int rc;
5969 uint32_t length, mbox_tmo = 0;
5970 LPFC_MBOXQ_t *mbox;
5971 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5972 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5973
5974 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5975 if (!mbox)
5976 return -ENOMEM;
5977
5978 /*
5979 * This function sends an embedded mailbox because it only sends the
5980 * the resource type. All extents of this type are released by the
5981 * port.
5982 */
5983 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5984 sizeof(struct lpfc_sli4_cfg_mhdr));
5985 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5986 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5987 length, LPFC_SLI4_MBX_EMBED);
5988
5989 /* Send an extents count of 0 - the dealloc doesn't use it. */
5990 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5991 LPFC_SLI4_MBX_EMBED);
5992 if (unlikely(rc)) {
5993 rc = -EIO;
5994 goto out_free_mbox;
5995 }
5996 if (!phba->sli4_hba.intr_enable)
5997 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5998 else {
James Smarta183a152011-10-10 21:32:43 -04005999 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006000 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6001 }
6002 if (unlikely(rc)) {
6003 rc = -EIO;
6004 goto out_free_mbox;
6005 }
6006
6007 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6008 if (bf_get(lpfc_mbox_hdr_status,
6009 &dealloc_rsrc->header.cfg_shdr.response)) {
6010 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6011 "2919 Failed to release resource extents "
6012 "for type %d - Status 0x%x Add'l Status 0x%x. "
6013 "Resource memory not released.\n",
6014 type,
6015 bf_get(lpfc_mbox_hdr_status,
6016 &dealloc_rsrc->header.cfg_shdr.response),
6017 bf_get(lpfc_mbox_hdr_add_status,
6018 &dealloc_rsrc->header.cfg_shdr.response));
6019 rc = -EIO;
6020 goto out_free_mbox;
6021 }
6022
6023 /* Release kernel memory resources for the specific type. */
6024 switch (type) {
6025 case LPFC_RSC_TYPE_FCOE_VPI:
6026 kfree(phba->vpi_bmask);
6027 kfree(phba->vpi_ids);
6028 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6029 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6030 &phba->lpfc_vpi_blk_list, list) {
6031 list_del_init(&rsrc_blk->list);
6032 kfree(rsrc_blk);
6033 }
James Smart16a3a202013-04-17 20:14:38 -04006034 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006035 break;
6036 case LPFC_RSC_TYPE_FCOE_XRI:
6037 kfree(phba->sli4_hba.xri_bmask);
6038 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006039 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6040 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6041 list_del_init(&rsrc_blk->list);
6042 kfree(rsrc_blk);
6043 }
6044 break;
6045 case LPFC_RSC_TYPE_FCOE_VFI:
6046 kfree(phba->sli4_hba.vfi_bmask);
6047 kfree(phba->sli4_hba.vfi_ids);
6048 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6049 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6050 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6051 list_del_init(&rsrc_blk->list);
6052 kfree(rsrc_blk);
6053 }
6054 break;
6055 case LPFC_RSC_TYPE_FCOE_RPI:
6056 /* RPI bitmask and physical id array are cleaned up earlier. */
6057 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6058 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6059 list_del_init(&rsrc_blk->list);
6060 kfree(rsrc_blk);
6061 }
6062 break;
6063 default:
6064 break;
6065 }
6066
6067 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6068
6069 out_free_mbox:
6070 mempool_free(mbox, phba->mbox_mem_pool);
6071 return rc;
6072}
6073
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006074static void
James Smart7bdedb32016-07-06 12:36:00 -07006075lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6076 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006077{
James Smart65791f12016-07-06 12:35:56 -07006078 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07006079
James Smart65791f12016-07-06 12:35:56 -07006080 len = sizeof(struct lpfc_mbx_set_feature) -
6081 sizeof(struct lpfc_sli4_cfg_mhdr);
6082 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6083 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6084 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006085
James Smart7bdedb32016-07-06 12:36:00 -07006086 switch (feature) {
6087 case LPFC_SET_UE_RECOVERY:
6088 bf_set(lpfc_mbx_set_feature_UER,
6089 &mbox->u.mqe.un.set_feature, 1);
6090 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6091 mbox->u.mqe.un.set_feature.param_len = 8;
6092 break;
6093 case LPFC_SET_MDS_DIAGS:
6094 bf_set(lpfc_mbx_set_feature_mds,
6095 &mbox->u.mqe.un.set_feature, 1);
6096 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006097 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006098 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6099 mbox->u.mqe.un.set_feature.param_len = 8;
6100 break;
James Smart65791f12016-07-06 12:35:56 -07006101 }
James Smart7bdedb32016-07-06 12:36:00 -07006102
6103 return;
James Smart65791f12016-07-06 12:35:56 -07006104}
6105
James Smart6d368e52011-05-24 11:44:12 -04006106/**
6107 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6108 * @phba: Pointer to HBA context object.
6109 *
6110 * This function allocates all SLI4 resource identifiers.
6111 **/
6112int
6113lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6114{
6115 int i, rc, error = 0;
6116 uint16_t count, base;
6117 unsigned long longs;
6118
James Smartff78d8f2011-12-13 13:21:35 -05006119 if (!phba->sli4_hba.rpi_hdrs_in_use)
6120 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04006121 if (phba->sli4_hba.extents_in_use) {
6122 /*
6123 * The port supports resource extents. The XRI, VPI, VFI, RPI
6124 * resource extent count must be read and allocated before
6125 * provisioning the resource id arrays.
6126 */
6127 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6128 LPFC_IDX_RSRC_RDY) {
6129 /*
6130 * Extent-based resources are set - the driver could
6131 * be in a port reset. Figure out if any corrective
6132 * actions need to be taken.
6133 */
6134 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6135 LPFC_RSC_TYPE_FCOE_VFI);
6136 if (rc != 0)
6137 error++;
6138 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6139 LPFC_RSC_TYPE_FCOE_VPI);
6140 if (rc != 0)
6141 error++;
6142 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6143 LPFC_RSC_TYPE_FCOE_XRI);
6144 if (rc != 0)
6145 error++;
6146 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6147 LPFC_RSC_TYPE_FCOE_RPI);
6148 if (rc != 0)
6149 error++;
6150
6151 /*
6152 * It's possible that the number of resources
6153 * provided to this port instance changed between
6154 * resets. Detect this condition and reallocate
6155 * resources. Otherwise, there is no action.
6156 */
6157 if (error) {
6158 lpfc_printf_log(phba, KERN_INFO,
6159 LOG_MBOX | LOG_INIT,
6160 "2931 Detected extent resource "
6161 "change. Reallocating all "
6162 "extents.\n");
6163 rc = lpfc_sli4_dealloc_extent(phba,
6164 LPFC_RSC_TYPE_FCOE_VFI);
6165 rc = lpfc_sli4_dealloc_extent(phba,
6166 LPFC_RSC_TYPE_FCOE_VPI);
6167 rc = lpfc_sli4_dealloc_extent(phba,
6168 LPFC_RSC_TYPE_FCOE_XRI);
6169 rc = lpfc_sli4_dealloc_extent(phba,
6170 LPFC_RSC_TYPE_FCOE_RPI);
6171 } else
6172 return 0;
6173 }
6174
6175 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6176 if (unlikely(rc))
6177 goto err_exit;
6178
6179 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6180 if (unlikely(rc))
6181 goto err_exit;
6182
6183 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6184 if (unlikely(rc))
6185 goto err_exit;
6186
6187 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6188 if (unlikely(rc))
6189 goto err_exit;
6190 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6191 LPFC_IDX_RSRC_RDY);
6192 return rc;
6193 } else {
6194 /*
6195 * The port does not support resource extents. The XRI, VPI,
6196 * VFI, RPI resource ids were determined from READ_CONFIG.
6197 * Just allocate the bitmasks and provision the resource id
6198 * arrays. If a port reset is active, the resources don't
6199 * need any action - just exit.
6200 */
6201 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006202 LPFC_IDX_RSRC_RDY) {
6203 lpfc_sli4_dealloc_resource_identifiers(phba);
6204 lpfc_sli4_remove_rpis(phba);
6205 }
James Smart6d368e52011-05-24 11:44:12 -04006206 /* RPIs. */
6207 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006208 if (count <= 0) {
6209 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6210 "3279 Invalid provisioning of "
6211 "rpi:%d\n", count);
6212 rc = -EINVAL;
6213 goto err_exit;
6214 }
James Smart6d368e52011-05-24 11:44:12 -04006215 base = phba->sli4_hba.max_cfg_param.rpi_base;
6216 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6217 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6218 sizeof(unsigned long),
6219 GFP_KERNEL);
6220 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6221 rc = -ENOMEM;
6222 goto err_exit;
6223 }
6224 phba->sli4_hba.rpi_ids = kzalloc(count *
6225 sizeof(uint16_t),
6226 GFP_KERNEL);
6227 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6228 rc = -ENOMEM;
6229 goto free_rpi_bmask;
6230 }
6231
6232 for (i = 0; i < count; i++)
6233 phba->sli4_hba.rpi_ids[i] = base + i;
6234
6235 /* VPIs. */
6236 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006237 if (count <= 0) {
6238 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6239 "3280 Invalid provisioning of "
6240 "vpi:%d\n", count);
6241 rc = -EINVAL;
6242 goto free_rpi_ids;
6243 }
James Smart6d368e52011-05-24 11:44:12 -04006244 base = phba->sli4_hba.max_cfg_param.vpi_base;
6245 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6246 phba->vpi_bmask = kzalloc(longs *
6247 sizeof(unsigned long),
6248 GFP_KERNEL);
6249 if (unlikely(!phba->vpi_bmask)) {
6250 rc = -ENOMEM;
6251 goto free_rpi_ids;
6252 }
6253 phba->vpi_ids = kzalloc(count *
6254 sizeof(uint16_t),
6255 GFP_KERNEL);
6256 if (unlikely(!phba->vpi_ids)) {
6257 rc = -ENOMEM;
6258 goto free_vpi_bmask;
6259 }
6260
6261 for (i = 0; i < count; i++)
6262 phba->vpi_ids[i] = base + i;
6263
6264 /* XRIs. */
6265 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006266 if (count <= 0) {
6267 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6268 "3281 Invalid provisioning of "
6269 "xri:%d\n", count);
6270 rc = -EINVAL;
6271 goto free_vpi_ids;
6272 }
James Smart6d368e52011-05-24 11:44:12 -04006273 base = phba->sli4_hba.max_cfg_param.xri_base;
6274 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6275 phba->sli4_hba.xri_bmask = kzalloc(longs *
6276 sizeof(unsigned long),
6277 GFP_KERNEL);
6278 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6279 rc = -ENOMEM;
6280 goto free_vpi_ids;
6281 }
James Smart41899be2012-03-01 22:34:19 -05006282 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006283 phba->sli4_hba.xri_ids = kzalloc(count *
6284 sizeof(uint16_t),
6285 GFP_KERNEL);
6286 if (unlikely(!phba->sli4_hba.xri_ids)) {
6287 rc = -ENOMEM;
6288 goto free_xri_bmask;
6289 }
6290
6291 for (i = 0; i < count; i++)
6292 phba->sli4_hba.xri_ids[i] = base + i;
6293
6294 /* VFIs. */
6295 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006296 if (count <= 0) {
6297 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6298 "3282 Invalid provisioning of "
6299 "vfi:%d\n", count);
6300 rc = -EINVAL;
6301 goto free_xri_ids;
6302 }
James Smart6d368e52011-05-24 11:44:12 -04006303 base = phba->sli4_hba.max_cfg_param.vfi_base;
6304 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6305 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6306 sizeof(unsigned long),
6307 GFP_KERNEL);
6308 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6309 rc = -ENOMEM;
6310 goto free_xri_ids;
6311 }
6312 phba->sli4_hba.vfi_ids = kzalloc(count *
6313 sizeof(uint16_t),
6314 GFP_KERNEL);
6315 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6316 rc = -ENOMEM;
6317 goto free_vfi_bmask;
6318 }
6319
6320 for (i = 0; i < count; i++)
6321 phba->sli4_hba.vfi_ids[i] = base + i;
6322
6323 /*
6324 * Mark all resources ready. An HBA reset doesn't need
6325 * to reset the initialization.
6326 */
6327 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6328 LPFC_IDX_RSRC_RDY);
6329 return 0;
6330 }
6331
6332 free_vfi_bmask:
6333 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006334 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006335 free_xri_ids:
6336 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006337 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006338 free_xri_bmask:
6339 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006340 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006341 free_vpi_ids:
6342 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006343 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006344 free_vpi_bmask:
6345 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006346 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006347 free_rpi_ids:
6348 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006349 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006350 free_rpi_bmask:
6351 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006352 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006353 err_exit:
6354 return rc;
6355}
6356
6357/**
6358 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6359 * @phba: Pointer to HBA context object.
6360 *
6361 * This function allocates the number of elements for the specified
6362 * resource type.
6363 **/
6364int
6365lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6366{
6367 if (phba->sli4_hba.extents_in_use) {
6368 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6369 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6370 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6371 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6372 } else {
6373 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006374 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006375 kfree(phba->vpi_ids);
6376 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6377 kfree(phba->sli4_hba.xri_bmask);
6378 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006379 kfree(phba->sli4_hba.vfi_bmask);
6380 kfree(phba->sli4_hba.vfi_ids);
6381 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6382 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6383 }
6384
6385 return 0;
6386}
6387
6388/**
James Smartb76f2dc2011-07-22 18:37:42 -04006389 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6390 * @phba: Pointer to HBA context object.
6391 * @type: The resource extent type.
6392 * @extnt_count: buffer to hold port extent count response
6393 * @extnt_size: buffer to hold port extent size response.
6394 *
6395 * This function calls the port to read the host allocated extents
6396 * for a particular type.
6397 **/
6398int
6399lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6400 uint16_t *extnt_cnt, uint16_t *extnt_size)
6401{
6402 bool emb;
6403 int rc = 0;
6404 uint16_t curr_blks = 0;
6405 uint32_t req_len, emb_len;
6406 uint32_t alloc_len, mbox_tmo;
6407 struct list_head *blk_list_head;
6408 struct lpfc_rsrc_blks *rsrc_blk;
6409 LPFC_MBOXQ_t *mbox;
6410 void *virtaddr = NULL;
6411 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6412 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6413 union lpfc_sli4_cfg_shdr *shdr;
6414
6415 switch (type) {
6416 case LPFC_RSC_TYPE_FCOE_VPI:
6417 blk_list_head = &phba->lpfc_vpi_blk_list;
6418 break;
6419 case LPFC_RSC_TYPE_FCOE_XRI:
6420 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6421 break;
6422 case LPFC_RSC_TYPE_FCOE_VFI:
6423 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6424 break;
6425 case LPFC_RSC_TYPE_FCOE_RPI:
6426 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6427 break;
6428 default:
6429 return -EIO;
6430 }
6431
6432 /* Count the number of extents currently allocatd for this type. */
6433 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6434 if (curr_blks == 0) {
6435 /*
6436 * The GET_ALLOCATED mailbox does not return the size,
6437 * just the count. The size should be just the size
6438 * stored in the current allocated block and all sizes
6439 * for an extent type are the same so set the return
6440 * value now.
6441 */
6442 *extnt_size = rsrc_blk->rsrc_size;
6443 }
6444 curr_blks++;
6445 }
6446
James Smartb76f2dc2011-07-22 18:37:42 -04006447 /*
6448 * Calculate the size of an embedded mailbox. The uint32_t
6449 * accounts for extents-specific word.
6450 */
6451 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6452 sizeof(uint32_t);
6453
6454 /*
6455 * Presume the allocation and response will fit into an embedded
6456 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6457 */
6458 emb = LPFC_SLI4_MBX_EMBED;
6459 req_len = emb_len;
6460 if (req_len > emb_len) {
6461 req_len = curr_blks * sizeof(uint16_t) +
6462 sizeof(union lpfc_sli4_cfg_shdr) +
6463 sizeof(uint32_t);
6464 emb = LPFC_SLI4_MBX_NEMBED;
6465 }
6466
6467 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6468 if (!mbox)
6469 return -ENOMEM;
6470 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6471
6472 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6473 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6474 req_len, emb);
6475 if (alloc_len < req_len) {
6476 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6477 "2983 Allocated DMA memory size (x%x) is "
6478 "less than the requested DMA memory "
6479 "size (x%x)\n", alloc_len, req_len);
6480 rc = -ENOMEM;
6481 goto err_exit;
6482 }
6483 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6484 if (unlikely(rc)) {
6485 rc = -EIO;
6486 goto err_exit;
6487 }
6488
6489 if (!phba->sli4_hba.intr_enable)
6490 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6491 else {
James Smarta183a152011-10-10 21:32:43 -04006492 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006493 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6494 }
6495
6496 if (unlikely(rc)) {
6497 rc = -EIO;
6498 goto err_exit;
6499 }
6500
6501 /*
6502 * Figure out where the response is located. Then get local pointers
6503 * to the response data. The port does not guarantee to respond to
6504 * all extents counts request so update the local variable with the
6505 * allocated count from the port.
6506 */
6507 if (emb == LPFC_SLI4_MBX_EMBED) {
6508 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6509 shdr = &rsrc_ext->header.cfg_shdr;
6510 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6511 } else {
6512 virtaddr = mbox->sge_array->addr[0];
6513 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6514 shdr = &n_rsrc->cfg_shdr;
6515 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6516 }
6517
6518 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6519 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6520 "2984 Failed to read allocated resources "
6521 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6522 type,
6523 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6524 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6525 rc = -EIO;
6526 goto err_exit;
6527 }
6528 err_exit:
6529 lpfc_sli4_mbox_cmd_free(phba, mbox);
6530 return rc;
6531}
6532
6533/**
James Smart0ef69962017-04-21 16:04:50 -07006534 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006535 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006536 * @pring: Pointer to driver SLI ring object.
6537 * @sgl_list: linked link of sgl buffers to post
6538 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006539 *
James Smart895427b2017-02-12 13:52:30 -08006540 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006541 * repost them to the port by using SGL block post. This is needed after a
6542 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006543 * of buffer sgls which contains contiguous xris and uses the non-embedded
6544 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006545 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6546 * mailbox command for posting.
6547 *
6548 * Returns: 0 = success, non-zero failure.
6549 **/
6550static int
James Smart895427b2017-02-12 13:52:30 -08006551lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6552 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006553{
6554 struct lpfc_sglq *sglq_entry = NULL;
6555 struct lpfc_sglq *sglq_entry_next = NULL;
6556 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006557 int status, total_cnt;
6558 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006559 int last_xritag = NO_XRI;
6560 LIST_HEAD(prep_sgl_list);
6561 LIST_HEAD(blck_sgl_list);
6562 LIST_HEAD(allc_sgl_list);
6563 LIST_HEAD(post_sgl_list);
6564 LIST_HEAD(free_sgl_list);
6565
James Smart38c20672013-03-01 16:37:44 -05006566 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006567 spin_lock(&phba->sli4_hba.sgl_list_lock);
6568 list_splice_init(sgl_list, &allc_sgl_list);
6569 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006570 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006571
James Smart895427b2017-02-12 13:52:30 -08006572 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006573 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6574 &allc_sgl_list, list) {
6575 list_del_init(&sglq_entry->list);
6576 block_cnt++;
6577 if ((last_xritag != NO_XRI) &&
6578 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6579 /* a hole in xri block, form a sgl posting block */
6580 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6581 post_cnt = block_cnt - 1;
6582 /* prepare list for next posting block */
6583 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6584 block_cnt = 1;
6585 } else {
6586 /* prepare list for next posting block */
6587 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6588 /* enough sgls for non-embed sgl mbox command */
6589 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6590 list_splice_init(&prep_sgl_list,
6591 &blck_sgl_list);
6592 post_cnt = block_cnt;
6593 block_cnt = 0;
6594 }
6595 }
6596 num_posted++;
6597
6598 /* keep track of last sgl's xritag */
6599 last_xritag = sglq_entry->sli4_xritag;
6600
James Smart895427b2017-02-12 13:52:30 -08006601 /* end of repost sgl list condition for buffers */
6602 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006603 if (post_cnt == 0) {
6604 list_splice_init(&prep_sgl_list,
6605 &blck_sgl_list);
6606 post_cnt = block_cnt;
6607 } else if (block_cnt == 1) {
6608 status = lpfc_sli4_post_sgl(phba,
6609 sglq_entry->phys, 0,
6610 sglq_entry->sli4_xritag);
6611 if (!status) {
6612 /* successful, put sgl to posted list */
6613 list_add_tail(&sglq_entry->list,
6614 &post_sgl_list);
6615 } else {
6616 /* Failure, put sgl to free list */
6617 lpfc_printf_log(phba, KERN_WARNING,
6618 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006619 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006620 "sgl, xritag:x%x\n",
6621 sglq_entry->sli4_xritag);
6622 list_add_tail(&sglq_entry->list,
6623 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006624 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006625 }
6626 }
6627 }
6628
6629 /* continue until a nembed page worth of sgls */
6630 if (post_cnt == 0)
6631 continue;
6632
James Smart895427b2017-02-12 13:52:30 -08006633 /* post the buffer list sgls as a block */
6634 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6635 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006636
6637 if (!status) {
6638 /* success, put sgl list to posted sgl list */
6639 list_splice_init(&blck_sgl_list, &post_sgl_list);
6640 } else {
6641 /* Failure, put sgl list to free sgl list */
6642 sglq_entry_first = list_first_entry(&blck_sgl_list,
6643 struct lpfc_sglq,
6644 list);
6645 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006646 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04006647 "xritag:x%x-x%x\n",
6648 sglq_entry_first->sli4_xritag,
6649 (sglq_entry_first->sli4_xritag +
6650 post_cnt - 1));
6651 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006652 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006653 }
6654
6655 /* don't reset xirtag due to hole in xri block */
6656 if (block_cnt == 0)
6657 last_xritag = NO_XRI;
6658
James Smart895427b2017-02-12 13:52:30 -08006659 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04006660 post_cnt = 0;
6661 }
6662
James Smart895427b2017-02-12 13:52:30 -08006663 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04006664 lpfc_free_sgl_list(phba, &free_sgl_list);
6665
James Smart895427b2017-02-12 13:52:30 -08006666 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04006667 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006668 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006669 spin_lock(&phba->sli4_hba.sgl_list_lock);
6670 list_splice_init(&post_sgl_list, sgl_list);
6671 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006672 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006673 } else {
6674 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006675 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006676 return -EIO;
6677 }
James Smart895427b2017-02-12 13:52:30 -08006678
6679 /* return the number of XRIs actually posted */
6680 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006681}
6682
James Smart61bda8f2016-10-13 15:06:05 -07006683void
6684lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6685{
6686 uint32_t len;
6687
6688 len = sizeof(struct lpfc_mbx_set_host_data) -
6689 sizeof(struct lpfc_sli4_cfg_mhdr);
6690 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6691 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6692 LPFC_SLI4_MBX_EMBED);
6693
6694 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08006695 mbox->u.mqe.un.set_host_data.param_len =
6696 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07006697 snprintf(mbox->u.mqe.un.set_host_data.data,
6698 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6699 "Linux %s v"LPFC_DRIVER_VERSION,
6700 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6701}
6702
James Smarta8cf5df2017-05-15 15:20:46 -07006703int
James Smart6c621a22017-05-15 15:20:45 -07006704lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07006705 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07006706{
6707 int rc, i;
6708 struct lpfc_rqe hrqe;
6709 struct lpfc_rqe drqe;
6710 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08006711 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07006712 struct rqb_dmabuf *rqb_buffer;
6713 LIST_HEAD(rqb_buf_list);
6714
James Smart411de512018-01-30 15:58:52 -08006715 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07006716 rqbp = hrq->rqbp;
6717 for (i = 0; i < count; i++) {
6718 /* IF RQ is already full, don't bother */
6719 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6720 break;
6721 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6722 if (!rqb_buffer)
6723 break;
6724 rqb_buffer->hrq = hrq;
6725 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07006726 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07006727 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6728 }
6729 while (!list_empty(&rqb_buf_list)) {
6730 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6731 hbuf.list);
6732
6733 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6734 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6735 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6736 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6737 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6738 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08006739 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6740 "6421 Cannot post to HRQ %d: %x %x %x "
6741 "DRQ %x %x\n",
6742 hrq->queue_id,
6743 hrq->host_index,
6744 hrq->hba_index,
6745 hrq->entry_count,
6746 drq->host_index,
6747 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07006748 rqbp->rqb_free_buffer(phba, rqb_buffer);
6749 } else {
6750 list_add_tail(&rqb_buffer->hbuf.list,
6751 &rqbp->rqb_buffer_list);
6752 rqbp->buffer_count++;
6753 }
6754 }
James Smart411de512018-01-30 15:58:52 -08006755 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07006756 return 1;
6757}
6758
James Smart8a9d2e82012-05-09 21:16:12 -04006759/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08006760 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04006761 * @phba: Pointer to HBA context object.
6762 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08006763 * This function is the main SLI4 device initialization PCI function. This
6764 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04006765 * HBA error attention handler code. Caller is not required to hold any
6766 * locks.
6767 **/
6768int
6769lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6770{
James Smart6c621a22017-05-15 15:20:45 -07006771 int rc, i, cnt;
James Smartda0436e2009-05-22 14:51:39 -04006772 LPFC_MBOXQ_t *mboxq;
6773 struct lpfc_mqe *mqe;
6774 uint8_t *vpd;
6775 uint32_t vpd_size;
6776 uint32_t ftr_rsp = 0;
6777 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6778 struct lpfc_vport *vport = phba->pport;
6779 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08006780 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04006781
6782 /* Perform a PCI function reset to start from clean */
6783 rc = lpfc_pci_function_reset(phba);
6784 if (unlikely(rc))
6785 return -ENODEV;
6786
6787 /* Check the HBA Host Status Register for readyness */
6788 rc = lpfc_sli4_post_status_check(phba);
6789 if (unlikely(rc))
6790 return -ENODEV;
6791 else {
6792 spin_lock_irq(&phba->hbalock);
6793 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6794 spin_unlock_irq(&phba->hbalock);
6795 }
6796
6797 /*
6798 * Allocate a single mailbox container for initializing the
6799 * port.
6800 */
6801 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6802 if (!mboxq)
6803 return -ENOMEM;
6804
James Smartda0436e2009-05-22 14:51:39 -04006805 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006806 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006807 vpd = kzalloc(vpd_size, GFP_KERNEL);
6808 if (!vpd) {
6809 rc = -ENOMEM;
6810 goto out_free_mbox;
6811 }
6812
6813 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006814 if (unlikely(rc)) {
6815 kfree(vpd);
6816 goto out_free_mbox;
6817 }
James Smart572709e2013-07-15 18:32:43 -04006818
James Smartda0436e2009-05-22 14:51:39 -04006819 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006820 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006821 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006822 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006823 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6824 } else {
James Smart76a95d72010-11-20 23:11:48 -05006825 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006826 }
James Smart45ed1192009-10-02 15:17:02 -04006827
6828 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6829 LPFC_DCBX_CEE_MODE)
6830 phba->hba_flag |= HBA_FIP_SUPPORT;
6831 else
6832 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6833
James Smart4f2e66c2012-05-09 21:17:07 -04006834 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6835
James Smartc31098c2011-04-16 11:03:33 -04006836 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006837 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6838 "0376 READ_REV Error. SLI Level %d "
6839 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006840 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006841 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006842 kfree(vpd);
6843 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006844 }
James Smartcd1c8302011-10-10 21:33:25 -04006845
6846 /*
James Smartff78d8f2011-12-13 13:21:35 -05006847 * Continue initialization with default values even if driver failed
6848 * to read FCoE param config regions, only read parameters if the
6849 * board is FCoE
6850 */
6851 if (phba->hba_flag & HBA_FCOE_MODE &&
6852 lpfc_sli4_read_fcoe_params(phba))
6853 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6854 "2570 Failed to read FCoE parameters\n");
6855
6856 /*
James Smartcd1c8302011-10-10 21:33:25 -04006857 * Retrieve sli4 device physical port name, failure of doing it
6858 * is considered as non-fatal.
6859 */
6860 rc = lpfc_sli4_retrieve_pport_name(phba);
6861 if (!rc)
6862 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6863 "3080 Successful retrieving SLI4 device "
6864 "physical port name: %s.\n", phba->Port);
6865
James Smartda0436e2009-05-22 14:51:39 -04006866 /*
6867 * Evaluate the read rev and vpd data. Populate the driver
6868 * state with the results. If this routine fails, the failure
6869 * is not fatal as the driver will use generic values.
6870 */
6871 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6872 if (unlikely(!rc)) {
6873 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6874 "0377 Error %d parsing vpd. "
6875 "Using defaults.\n", rc);
6876 rc = 0;
6877 }
James Smart76a95d72010-11-20 23:11:48 -05006878 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006879
James Smartf1126682009-06-10 17:22:44 -04006880 /* Save information as VPD data */
6881 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6882 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
James Smart4e565cf2018-02-22 08:18:50 -08006883
6884 /*
6885 * This is because first G7 ASIC doesn't support the standard
6886 * 0x5a NVME cmd descriptor type/subtype
6887 */
6888 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6889 LPFC_SLI_INTF_IF_TYPE_6) &&
6890 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
6891 (phba->vpd.rev.smRev == 0) &&
6892 (phba->cfg_nvme_embed_cmd == 1))
6893 phba->cfg_nvme_embed_cmd = 0;
6894
James Smartf1126682009-06-10 17:22:44 -04006895 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6896 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6897 &mqe->un.read_rev);
6898 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6899 &mqe->un.read_rev);
6900 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6901 &mqe->un.read_rev);
6902 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6903 &mqe->un.read_rev);
6904 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6905 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6906 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6907 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6908 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6909 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6910 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6911 "(%d):0380 READ_REV Status x%x "
6912 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6913 mboxq->vport ? mboxq->vport->vpi : 0,
6914 bf_get(lpfc_mqe_status, mqe),
6915 phba->vpd.rev.opFwName,
6916 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6917 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006918
James Smart572709e2013-07-15 18:32:43 -04006919 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6920 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6921 if (phba->pport->cfg_lun_queue_depth > rc) {
6922 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6923 "3362 LUN queue depth changed from %d to %d\n",
6924 phba->pport->cfg_lun_queue_depth, rc);
6925 phba->pport->cfg_lun_queue_depth = rc;
6926 }
6927
James Smart65791f12016-07-06 12:35:56 -07006928 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006929 LPFC_SLI_INTF_IF_TYPE_0) {
6930 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6931 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6932 if (rc == MBX_SUCCESS) {
6933 phba->hba_flag |= HBA_RECOVERABLE_UE;
6934 /* Set 1Sec interval to detect UE */
6935 phba->eratt_poll_interval = 1;
6936 phba->sli4_hba.ue_to_sr = bf_get(
6937 lpfc_mbx_set_feature_UESR,
6938 &mboxq->u.mqe.un.set_feature);
6939 phba->sli4_hba.ue_to_rp = bf_get(
6940 lpfc_mbx_set_feature_UERP,
6941 &mboxq->u.mqe.un.set_feature);
6942 }
6943 }
6944
6945 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6946 /* Enable MDS Diagnostics only if the SLI Port supports it */
6947 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6948 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6949 if (rc != MBX_SUCCESS)
6950 phba->mds_diags_support = 0;
6951 }
James Smart572709e2013-07-15 18:32:43 -04006952
James Smartda0436e2009-05-22 14:51:39 -04006953 /*
6954 * Discover the port's supported feature set and match it against the
6955 * hosts requests.
6956 */
6957 lpfc_request_features(phba, mboxq);
6958 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6959 if (unlikely(rc)) {
6960 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006961 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006962 }
6963
6964 /*
6965 * The port must support FCP initiator mode as this is the
6966 * only mode running in the host.
6967 */
6968 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6969 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6970 "0378 No support for fcpi mode.\n");
6971 ftr_rsp++;
6972 }
James Smart0bc2b7c2018-02-22 08:18:48 -08006973
6974 /* Performance Hints are ONLY for FCoE */
6975 if (phba->hba_flag & HBA_FCOE_MODE) {
6976 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6977 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6978 else
6979 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
6980 }
6981
James Smartda0436e2009-05-22 14:51:39 -04006982 /*
6983 * If the port cannot support the host's requested features
6984 * then turn off the global config parameters to disable the
6985 * feature in the driver. This is not a fatal error.
6986 */
James Smartbf086112011-08-21 21:48:13 -04006987 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6988 if (phba->cfg_enable_bg) {
6989 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6990 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6991 else
6992 ftr_rsp++;
6993 }
James Smartda0436e2009-05-22 14:51:39 -04006994
6995 if (phba->max_vpi && phba->cfg_enable_npiv &&
6996 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6997 ftr_rsp++;
6998
6999 if (ftr_rsp) {
7000 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7001 "0379 Feature Mismatch Data: x%08x %08x "
7002 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7003 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7004 phba->cfg_enable_npiv, phba->max_vpi);
7005 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7006 phba->cfg_enable_bg = 0;
7007 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7008 phba->cfg_enable_npiv = 0;
7009 }
7010
7011 /* These SLI3 features are assumed in SLI4 */
7012 spin_lock_irq(&phba->hbalock);
7013 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7014 spin_unlock_irq(&phba->hbalock);
7015
James Smart6d368e52011-05-24 11:44:12 -04007016 /*
7017 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
7018 * calls depends on these resources to complete port setup.
7019 */
7020 rc = lpfc_sli4_alloc_resource_identifiers(phba);
7021 if (rc) {
7022 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7023 "2920 Failed to alloc Resource IDs "
7024 "rc = x%x\n", rc);
7025 goto out_free_mbox;
7026 }
7027
James Smart61bda8f2016-10-13 15:06:05 -07007028 lpfc_set_host_data(phba, mboxq);
7029
7030 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7031 if (rc) {
7032 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7033 "2134 Failed to set host os driver version %x",
7034 rc);
7035 }
7036
James Smartda0436e2009-05-22 14:51:39 -04007037 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05007038 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7039 if (rc) {
7040 phba->link_state = LPFC_HBA_ERROR;
7041 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05007042 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05007043 }
7044
James Smartda0436e2009-05-22 14:51:39 -04007045 mboxq->vport = vport;
7046 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7047 mp = (struct lpfc_dmabuf *) mboxq->context1;
7048 if (rc == MBX_SUCCESS) {
7049 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7050 rc = 0;
7051 }
7052
7053 /*
7054 * This memory was allocated by the lpfc_read_sparam routine. Release
7055 * it to the mbuf pool.
7056 */
7057 lpfc_mbuf_free(phba, mp->virt, mp->phys);
7058 kfree(mp);
7059 mboxq->context1 = NULL;
7060 if (unlikely(rc)) {
7061 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7062 "0382 READ_SPARAM command failed "
7063 "status %d, mbxStatus x%x\n",
7064 rc, bf_get(lpfc_mqe_status, mqe));
7065 phba->link_state = LPFC_HBA_ERROR;
7066 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007067 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007068 }
7069
James Smart05580562011-05-24 11:40:48 -04007070 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04007071
7072 /* Update the fc_host data structures with new wwn. */
7073 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7074 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7075
James Smart895427b2017-02-12 13:52:30 -08007076 /* Create all the SLI4 queues */
7077 rc = lpfc_sli4_queue_create(phba);
7078 if (rc) {
7079 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7080 "3089 Failed to allocate queues\n");
7081 rc = -ENODEV;
7082 goto out_free_mbox;
7083 }
7084 /* Set up all the queues to the device */
7085 rc = lpfc_sli4_queue_setup(phba);
7086 if (unlikely(rc)) {
7087 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7088 "0381 Error %d during queue setup.\n ", rc);
7089 goto out_stop_timers;
7090 }
7091 /* Initialize the driver internal SLI layer lists. */
7092 lpfc_sli4_setup(phba);
7093 lpfc_sli4_queue_init(phba);
7094
7095 /* update host els xri-sgl sizes and mappings */
7096 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007097 if (unlikely(rc)) {
7098 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7099 "1400 Failed to update xri-sgl size and "
7100 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08007101 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007102 }
7103
James Smart8a9d2e82012-05-09 21:16:12 -04007104 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08007105 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7106 phba->sli4_hba.els_xri_cnt);
7107 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04007108 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7109 "0582 Error %d during els sgl post "
7110 "operation\n", rc);
7111 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007112 goto out_destroy_queue;
7113 }
7114 phba->sli4_hba.els_xri_cnt = rc;
7115
James Smartf358dd02017-02-12 13:52:34 -08007116 if (phba->nvmet_support) {
7117 /* update host nvmet xri-sgl sizes and mappings */
7118 rc = lpfc_sli4_nvmet_sgl_update(phba);
7119 if (unlikely(rc)) {
7120 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7121 "6308 Failed to update nvmet-sgl size "
7122 "and mapping: %d\n", rc);
7123 goto out_destroy_queue;
7124 }
7125
7126 /* register the nvmet sgl pool to the port */
7127 rc = lpfc_sli4_repost_sgl_list(
7128 phba,
7129 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7130 phba->sli4_hba.nvmet_xri_cnt);
7131 if (unlikely(rc < 0)) {
7132 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7133 "3117 Error %d during nvmet "
7134 "sgl post\n", rc);
7135 rc = -ENODEV;
7136 goto out_destroy_queue;
7137 }
7138 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07007139
7140 cnt = phba->cfg_iocb_cnt * 1024;
7141 /* We need 1 iocbq for every SGL, for IO processing */
7142 cnt += phba->sli4_hba.nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08007143 } else {
James Smart895427b2017-02-12 13:52:30 -08007144 /* update host scsi xri-sgl sizes and mappings */
7145 rc = lpfc_sli4_scsi_sgl_update(phba);
7146 if (unlikely(rc)) {
7147 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7148 "6309 Failed to update scsi-sgl size "
7149 "and mapping: %d\n", rc);
7150 goto out_destroy_queue;
7151 }
7152
7153 /* update host nvme xri-sgl sizes and mappings */
7154 rc = lpfc_sli4_nvme_sgl_update(phba);
7155 if (unlikely(rc)) {
7156 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7157 "6082 Failed to update nvme-sgl size "
7158 "and mapping: %d\n", rc);
7159 goto out_destroy_queue;
7160 }
James Smart6c621a22017-05-15 15:20:45 -07007161
7162 cnt = phba->cfg_iocb_cnt * 1024;
James Smart11e644e2017-06-15 22:56:48 -07007163 }
7164
7165 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07007166 /* Initialize and populate the iocb list per host */
7167 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007168 "2821 initialize iocb list %d total %d\n",
James Smart6c621a22017-05-15 15:20:45 -07007169 phba->cfg_iocb_cnt, cnt);
7170 rc = lpfc_init_iocb_list(phba, cnt);
7171 if (rc) {
7172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007173 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07007174 goto out_destroy_queue;
7175 }
James Smart8a9d2e82012-05-09 21:16:12 -04007176 }
7177
James Smart11e644e2017-06-15 22:56:48 -07007178 if (phba->nvmet_support)
7179 lpfc_nvmet_create_targetport(phba);
7180
James Smart2d7dbc42017-02-12 13:52:35 -08007181 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007182 /* Post initial buffers to all RQs created */
7183 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7184 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7185 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7186 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7187 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007188 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007189 rqbp->buffer_count = 0;
7190
James Smart2d7dbc42017-02-12 13:52:35 -08007191 lpfc_post_rq_buffer(
7192 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7193 phba->sli4_hba.nvmet_mrq_data[i],
James Smarta8cf5df2017-05-15 15:20:46 -07007194 LPFC_NVMET_RQE_DEF_COUNT, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007195 }
7196 }
7197
James Smart895427b2017-02-12 13:52:30 -08007198 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
7199 /* register the allocated scsi sgl pool to the port */
7200 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
7201 if (unlikely(rc)) {
7202 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7203 "0383 Error %d during scsi sgl post "
7204 "operation\n", rc);
7205 /* Some Scsi buffers were moved to abort scsi list */
7206 /* A pci function reset will repost them */
7207 rc = -ENODEV;
7208 goto out_destroy_queue;
7209 }
James Smartda0436e2009-05-22 14:51:39 -04007210 }
7211
James Smart01649562017-02-12 13:52:32 -08007212 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7213 (phba->nvmet_support == 0)) {
7214
7215 /* register the allocated nvme sgl pool to the port */
7216 rc = lpfc_repost_nvme_sgl_list(phba);
7217 if (unlikely(rc)) {
7218 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7219 "6116 Error %d during nvme sgl post "
7220 "operation\n", rc);
7221 /* Some NVME buffers were moved to abort nvme list */
7222 /* A pci function reset will repost them */
7223 rc = -ENODEV;
7224 goto out_destroy_queue;
7225 }
James Smartda0436e2009-05-22 14:51:39 -04007226 }
7227
7228 /* Post the rpi header region to the device. */
7229 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7230 if (unlikely(rc)) {
7231 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7232 "0393 Error %d during rpi post operation\n",
7233 rc);
7234 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007235 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007236 }
James Smart97f2ecf2012-03-01 22:35:23 -05007237 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007238
James Smart895427b2017-02-12 13:52:30 -08007239 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007240 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007241 /*
7242 * The FC Port needs to register FCFI (index 0)
7243 */
7244 lpfc_reg_fcfi(phba, mboxq);
7245 mboxq->vport = phba->pport;
7246 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7247 if (rc != MBX_SUCCESS)
7248 goto out_unset_queue;
7249 rc = 0;
7250 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7251 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007252 } else {
7253 /* We are a NVME Target mode with MRQ > 1 */
7254
7255 /* First register the FCFI */
7256 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7257 mboxq->vport = phba->pport;
7258 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7259 if (rc != MBX_SUCCESS)
7260 goto out_unset_queue;
7261 rc = 0;
7262 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7263 &mboxq->u.mqe.un.reg_fcfi_mrq);
7264
7265 /* Next register the MRQs */
7266 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7267 mboxq->vport = phba->pport;
7268 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7269 if (rc != MBX_SUCCESS)
7270 goto out_unset_queue;
7271 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007272 }
7273 /* Check if the port is configured to be disabled */
7274 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007275 }
7276
7277 /* Arm the CQs and then EQs on device */
7278 lpfc_sli4_arm_cqeq_intr(phba);
7279
7280 /* Indicate device interrupt mode */
7281 phba->sli4_hba.intr_enable = 1;
7282
7283 /* Allow asynchronous mailbox command to go through */
7284 spin_lock_irq(&phba->hbalock);
7285 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7286 spin_unlock_irq(&phba->hbalock);
7287
7288 /* Post receive buffers to the device */
7289 lpfc_sli4_rb_setup(phba);
7290
James Smartfc2b9892010-02-26 14:15:29 -05007291 /* Reset HBA FCF states after HBA reset */
7292 phba->fcf.fcf_flag = 0;
7293 phba->fcf.current_rec.flag = 0;
7294
James Smartda0436e2009-05-22 14:51:39 -04007295 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007296 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007297 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007298
7299 /* Start heart beat timer */
7300 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007301 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007302 phba->hb_outstanding = 0;
7303 phba->last_completion_time = jiffies;
7304
7305 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007306 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007307 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007308
James Smart75baf692010-06-08 18:31:21 -04007309 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7310 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7311 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7312 if (!rc) {
7313 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7314 "2829 This device supports "
7315 "Advanced Error Reporting (AER)\n");
7316 spin_lock_irq(&phba->hbalock);
7317 phba->hba_flag |= HBA_AER_ENABLED;
7318 spin_unlock_irq(&phba->hbalock);
7319 } else {
7320 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7321 "2830 This device does not support "
7322 "Advanced Error Reporting (AER)\n");
7323 phba->cfg_aer_support = 0;
7324 }
James Smart0a96e972011-07-22 18:37:28 -04007325 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007326 }
7327
James Smartda0436e2009-05-22 14:51:39 -04007328 /*
7329 * The port is ready, set the host's link state to LINK_DOWN
7330 * in preparation for link interrupts.
7331 */
James Smartda0436e2009-05-22 14:51:39 -04007332 spin_lock_irq(&phba->hbalock);
7333 phba->link_state = LPFC_LINK_DOWN;
7334 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05007335 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7336 (phba->hba_flag & LINK_DISABLED)) {
7337 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7338 "3103 Adapter Link is disabled.\n");
7339 lpfc_down_link(phba, mboxq);
7340 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7341 if (rc != MBX_SUCCESS) {
7342 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7343 "3104 Adapter failed to issue "
7344 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7345 goto out_unset_queue;
7346 }
7347 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007348 /* don't perform init_link on SLI4 FC port loopback test */
7349 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7350 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7351 if (rc)
7352 goto out_unset_queue;
7353 }
James Smart5350d872011-10-10 21:33:49 -04007354 }
7355 mempool_free(mboxq, phba->mbox_mem_pool);
7356 return rc;
James Smart76a95d72010-11-20 23:11:48 -05007357out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007358 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007359 lpfc_sli4_queue_unset(phba);
7360out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007361 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007362 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007363out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007364 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007365out_free_mbox:
7366 mempool_free(mboxq, phba->mbox_mem_pool);
7367 return rc;
7368}
James Smarte59058c2008-08-24 21:49:00 -04007369
7370/**
James Smart3621a712009-04-06 18:47:14 -04007371 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007372 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007373 *
James Smarte59058c2008-08-24 21:49:00 -04007374 * This is the callback function for mailbox timer. The mailbox
7375 * timer is armed when a new mailbox command is issued and the timer
7376 * is deleted when the mailbox complete. The function is called by
7377 * the kernel timer code when a mailbox does not complete within
7378 * expected time. This function wakes up the worker thread to
7379 * process the mailbox timeout and returns. All the processing is
7380 * done by the worker thread function lpfc_mbox_timeout_handler.
7381 **/
dea31012005-04-17 16:05:31 -05007382void
Kees Cookf22eb4d2017-09-06 20:24:26 -07007383lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05007384{
Kees Cookf22eb4d2017-09-06 20:24:26 -07007385 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05007386 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007387 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007388
James Smart2e0fef82007-06-17 19:56:36 -05007389 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007390 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007391 if (!tmo_posted)
7392 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7393 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7394
James Smart5e9d9b82008-06-14 22:52:53 -04007395 if (!tmo_posted)
7396 lpfc_worker_wake_up(phba);
7397 return;
dea31012005-04-17 16:05:31 -05007398}
7399
James Smarte8d3c3b2013-10-10 12:21:30 -04007400/**
7401 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7402 * are pending
7403 * @phba: Pointer to HBA context object.
7404 *
7405 * This function checks if any mailbox completions are present on the mailbox
7406 * completion queue.
7407 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007408static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007409lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7410{
7411
7412 uint32_t idx;
7413 struct lpfc_queue *mcq;
7414 struct lpfc_mcqe *mcqe;
7415 bool pending_completions = false;
James Smart7365f6f2018-02-22 08:18:46 -08007416 uint8_t qe_valid;
James Smarte8d3c3b2013-10-10 12:21:30 -04007417
7418 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7419 return false;
7420
7421 /* Check for completions on mailbox completion queue */
7422
7423 mcq = phba->sli4_hba.mbx_cq;
7424 idx = mcq->hba_index;
James Smart7365f6f2018-02-22 08:18:46 -08007425 qe_valid = mcq->qe_valid;
7426 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe) == qe_valid) {
James Smarte8d3c3b2013-10-10 12:21:30 -04007427 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7428 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7429 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7430 pending_completions = true;
7431 break;
7432 }
7433 idx = (idx + 1) % mcq->entry_count;
7434 if (mcq->hba_index == idx)
7435 break;
James Smart7365f6f2018-02-22 08:18:46 -08007436
7437 /* if the index wrapped around, toggle the valid bit */
7438 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7439 qe_valid = (qe_valid) ? 0 : 1;
James Smarte8d3c3b2013-10-10 12:21:30 -04007440 }
7441 return pending_completions;
7442
7443}
7444
7445/**
7446 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7447 * that were missed.
7448 * @phba: Pointer to HBA context object.
7449 *
7450 * For sli4, it is possible to miss an interrupt. As such mbox completions
7451 * maybe missed causing erroneous mailbox timeouts to occur. This function
7452 * checks to see if mbox completions are on the mailbox completion queue
7453 * and will process all the completions associated with the eq for the
7454 * mailbox completion queue.
7455 **/
7456bool
7457lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7458{
James Smartb71413d2018-02-22 08:18:40 -08007459 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04007460 uint32_t eqidx;
7461 struct lpfc_queue *fpeq = NULL;
7462 struct lpfc_eqe *eqe;
7463 bool mbox_pending;
7464
7465 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7466 return false;
7467
7468 /* Find the eq associated with the mcq */
7469
James Smartb71413d2018-02-22 08:18:40 -08007470 if (sli4_hba->hba_eq)
James Smart895427b2017-02-12 13:52:30 -08007471 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
James Smartb71413d2018-02-22 08:18:40 -08007472 if (sli4_hba->hba_eq[eqidx]->queue_id ==
7473 sli4_hba->mbx_cq->assoc_qid) {
7474 fpeq = sli4_hba->hba_eq[eqidx];
James Smarte8d3c3b2013-10-10 12:21:30 -04007475 break;
7476 }
7477 if (!fpeq)
7478 return false;
7479
7480 /* Turn off interrupts from this EQ */
7481
James Smartb71413d2018-02-22 08:18:40 -08007482 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04007483
7484 /* Check to see if a mbox completion is pending */
7485
7486 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7487
7488 /*
7489 * If a mbox completion is pending, process all the events on EQ
7490 * associated with the mbox completion queue (this could include
7491 * mailbox commands, async events, els commands, receive queue data
7492 * and fcp commands)
7493 */
7494
7495 if (mbox_pending)
7496 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7497 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7498 fpeq->EQ_processed++;
7499 }
7500
7501 /* Always clear and re-arm the EQ */
7502
James Smartb71413d2018-02-22 08:18:40 -08007503 sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04007504
7505 return mbox_pending;
7506
7507}
James Smarte59058c2008-08-24 21:49:00 -04007508
7509/**
James Smart3621a712009-04-06 18:47:14 -04007510 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007511 * @phba: Pointer to HBA context object.
7512 *
7513 * This function is called from worker thread when a mailbox command times out.
7514 * The caller is not required to hold any locks. This function will reset the
7515 * HBA and recover all the pending commands.
7516 **/
dea31012005-04-17 16:05:31 -05007517void
7518lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7519{
James Smart2e0fef82007-06-17 19:56:36 -05007520 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007521 MAILBOX_t *mb = NULL;
7522
James Smart1dcb58e2007-04-25 09:51:30 -04007523 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007524
James Smarte8d3c3b2013-10-10 12:21:30 -04007525 /* If the mailbox completed, process the completion and return */
7526 if (lpfc_sli4_process_missed_mbox_completions(phba))
7527 return;
7528
James Smarteb016562014-09-03 12:58:06 -04007529 if (pmbox != NULL)
7530 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007531 /* Check the pmbox pointer first. There is a race condition
7532 * between the mbox timeout handler getting executed in the
7533 * worklist and the mailbox actually completing. When this
7534 * race condition occurs, the mbox_active will be NULL.
7535 */
7536 spin_lock_irq(&phba->hbalock);
7537 if (pmbox == NULL) {
7538 lpfc_printf_log(phba, KERN_WARNING,
7539 LOG_MBOX | LOG_SLI,
7540 "0353 Active Mailbox cleared - mailbox timeout "
7541 "exiting\n");
7542 spin_unlock_irq(&phba->hbalock);
7543 return;
7544 }
7545
dea31012005-04-17 16:05:31 -05007546 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007547 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007548 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007549 mb->mbxCommand,
7550 phba->pport->port_state,
7551 phba->sli.sli_flag,
7552 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007553 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007554
James Smart1dcb58e2007-04-25 09:51:30 -04007555 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7556 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007557 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007558 */
James Smart2e0fef82007-06-17 19:56:36 -05007559 spin_lock_irq(&phba->pport->work_port_lock);
7560 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7561 spin_unlock_irq(&phba->pport->work_port_lock);
7562 spin_lock_irq(&phba->hbalock);
7563 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007564 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007565 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007566
James Smartdb55fba2014-04-04 13:52:02 -04007567 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007568
7569 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007570 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007571
7572 /* Reset the HBA device */
7573 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007574}
7575
James Smarte59058c2008-08-24 21:49:00 -04007576/**
James Smart3772a992009-05-22 14:50:54 -04007577 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007578 * @phba: Pointer to HBA context object.
7579 * @pmbox: Pointer to mailbox object.
7580 * @flag: Flag indicating how the mailbox need to be processed.
7581 *
7582 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007583 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7584 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007585 * The mailbox command can be submitted in polling mode, in which case
7586 * this function will wait in a polling loop for the completion of the
7587 * mailbox.
7588 * If the mailbox is submitted in no_wait mode (not polling) the
7589 * function will submit the command and returns immediately without waiting
7590 * for the mailbox completion. The no_wait is supported only when HBA
7591 * is in SLI2/SLI3 mode - interrupts are enabled.
7592 * The SLI interface allows only one mailbox pending at a time. If the
7593 * mailbox is issued in polling mode and there is already a mailbox
7594 * pending, then the function will return an error. If the mailbox is issued
7595 * in NO_WAIT mode and there is a mailbox pending already, the function
7596 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7597 * The sli layer owns the mailbox object until the completion of mailbox
7598 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7599 * return codes the caller owns the mailbox command after the return of
7600 * the function.
7601 **/
James Smart3772a992009-05-22 14:50:54 -04007602static int
7603lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7604 uint32_t flag)
dea31012005-04-17 16:05:31 -05007605{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007606 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007607 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007608 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007609 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007610 int i;
James Smart09372822008-01-11 01:52:54 -05007611 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007612 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007613 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007614 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007615 int processing_queue = 0;
7616
7617 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7618 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007619 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007620 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007621 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7622 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7623 return MBX_SUCCESS;
7624 }
James Smart58da1ff2008-04-07 10:15:56 -04007625 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007626 pmbox = lpfc_mbox_get(phba);
7627 if (!pmbox) {
7628 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7629 return MBX_SUCCESS;
7630 }
7631 }
dea31012005-04-17 16:05:31 -05007632
James Smarted957682007-06-17 19:56:37 -05007633 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007634 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007635 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007636 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007637 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007638 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007639 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007640 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007641 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007642 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007643 }
7644 }
7645
Linas Vepstas8d63f372007-02-14 14:28:36 -06007646 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007647 if (unlikely(pci_channel_offline(phba->pcidev))) {
7648 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7649 goto out_not_finished;
7650 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007651
James Smarta257bf92009-04-06 18:48:10 -04007652 /* If HBA has a deferred error attention, fail the iocb. */
7653 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7654 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7655 goto out_not_finished;
7656 }
7657
dea31012005-04-17 16:05:31 -05007658 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007659
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007660 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007661 status = MBX_SUCCESS;
7662
James Smart2e0fef82007-06-17 19:56:36 -05007663 if (phba->link_state == LPFC_HBA_ERROR) {
7664 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007665
7666 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007667 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7668 "(%d):0311 Mailbox command x%x cannot "
7669 "issue Data: x%x x%x\n",
7670 pmbox->vport ? pmbox->vport->vpi : 0,
7671 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007672 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007673 }
7674
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007675 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007676 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7677 !(hc_copy & HC_MBINT_ENA)) {
7678 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7679 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007680 "(%d):2528 Mailbox command x%x cannot "
7681 "issue Data: x%x x%x\n",
7682 pmbox->vport ? pmbox->vport->vpi : 0,
7683 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007684 goto out_not_finished;
7685 }
James Smart92908312006-03-07 15:04:13 -05007686 }
7687
dea31012005-04-17 16:05:31 -05007688 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7689 /* Polling for a mbox command when another one is already active
7690 * is not allowed in SLI. Also, the driver must have established
7691 * SLI2 mode to queue and process multiple mbox commands.
7692 */
7693
7694 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007695 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007696
7697 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007698 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7699 "(%d):2529 Mailbox command x%x "
7700 "cannot issue Data: x%x x%x\n",
7701 pmbox->vport ? pmbox->vport->vpi : 0,
7702 pmbox->u.mb.mbxCommand,
7703 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007704 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007705 }
7706
James Smart3772a992009-05-22 14:50:54 -04007707 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007708 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007709 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007710 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7711 "(%d):2530 Mailbox command x%x "
7712 "cannot issue Data: x%x x%x\n",
7713 pmbox->vport ? pmbox->vport->vpi : 0,
7714 pmbox->u.mb.mbxCommand,
7715 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007716 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007717 }
7718
dea31012005-04-17 16:05:31 -05007719 /* Another mailbox command is still being processed, queue this
7720 * command to be processed later.
7721 */
7722 lpfc_mbox_put(phba, pmbox);
7723
7724 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007725 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007726 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007727 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007728 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07007729 mbx->mbxCommand,
7730 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007731 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007732
7733 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007734 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007735
James Smart858c9f62007-06-17 19:56:39 -05007736 if (pmbox->vport) {
7737 lpfc_debugfs_disc_trc(pmbox->vport,
7738 LPFC_DISC_TRC_MBOX_VPORT,
7739 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007740 (uint32_t)mbx->mbxCommand,
7741 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007742 }
7743 else {
7744 lpfc_debugfs_disc_trc(phba->pport,
7745 LPFC_DISC_TRC_MBOX,
7746 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007747 (uint32_t)mbx->mbxCommand,
7748 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007749 }
7750
James Smart2e0fef82007-06-17 19:56:36 -05007751 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007752 }
7753
dea31012005-04-17 16:05:31 -05007754 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7755
7756 /* If we are not polling, we MUST be in SLI2 mode */
7757 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007758 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007759 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007760 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007761 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007762 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007763 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7764 "(%d):2531 Mailbox command x%x "
7765 "cannot issue Data: x%x x%x\n",
7766 pmbox->vport ? pmbox->vport->vpi : 0,
7767 pmbox->u.mb.mbxCommand,
7768 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007769 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007770 }
7771 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007772 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7773 1000);
7774 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007775 }
7776
7777 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007778 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007779 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007780 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007781 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07007782 mbx->mbxCommand,
7783 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007784 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007785
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007786 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007787 if (pmbox->vport) {
7788 lpfc_debugfs_disc_trc(pmbox->vport,
7789 LPFC_DISC_TRC_MBOX_VPORT,
7790 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007791 (uint32_t)mbx->mbxCommand,
7792 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007793 }
7794 else {
7795 lpfc_debugfs_disc_trc(phba->pport,
7796 LPFC_DISC_TRC_MBOX,
7797 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007798 (uint32_t)mbx->mbxCommand,
7799 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007800 }
7801 }
7802
dea31012005-04-17 16:05:31 -05007803 psli->slistat.mbox_cmd++;
7804 evtctr = psli->slistat.mbox_event;
7805
7806 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007807 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007808
James Smart3772a992009-05-22 14:50:54 -04007809 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007810 /* Populate mbox extension offset word. */
7811 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007812 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007813 = (uint8_t *)phba->mbox_ext
7814 - (uint8_t *)phba->mbox;
7815 }
7816
7817 /* Copy the mailbox extension data */
7818 if (pmbox->in_ext_byte_len && pmbox->context2) {
7819 lpfc_sli_pcimem_bcopy(pmbox->context2,
7820 (uint8_t *)phba->mbox_ext,
7821 pmbox->in_ext_byte_len);
7822 }
7823 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007824 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007825 } else {
James Smart7a470272010-03-15 11:25:20 -04007826 /* Populate mbox extension offset word. */
7827 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007828 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007829 = MAILBOX_HBA_EXT_OFFSET;
7830
7831 /* Copy the mailbox extension data */
James Smart895427b2017-02-12 13:52:30 -08007832 if (pmbox->in_ext_byte_len && pmbox->context2)
James Smart7a470272010-03-15 11:25:20 -04007833 lpfc_memcpy_to_slim(phba->MBslimaddr +
7834 MAILBOX_HBA_EXT_OFFSET,
7835 pmbox->context2, pmbox->in_ext_byte_len);
7836
James Smart895427b2017-02-12 13:52:30 -08007837 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007838 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08007839 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7840 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007841
7842 /* First copy mbox command data to HBA SLIM, skip past first
7843 word */
7844 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007845 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007846 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7847
7848 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007849 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007850 to_slim = phba->MBslimaddr;
7851 writel(ldata, to_slim);
7852 readl(to_slim); /* flush */
7853
James Smart895427b2017-02-12 13:52:30 -08007854 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007855 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007856 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007857 }
7858
7859 wmb();
dea31012005-04-17 16:05:31 -05007860
7861 switch (flag) {
7862 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007863 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007864 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007865 /* Interrupt board to do it */
7866 writel(CA_MBATT, phba->CAregaddr);
7867 readl(phba->CAregaddr); /* flush */
7868 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007869 break;
7870
7871 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007872 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007873 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007874 /* Interrupt board to do it */
7875 writel(CA_MBATT, phba->CAregaddr);
7876 readl(phba->CAregaddr); /* flush */
7877
James Smart3772a992009-05-22 14:50:54 -04007878 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007879 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007880 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007881 word0 = le32_to_cpu(word0);
7882 } else {
7883 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007884 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7885 spin_unlock_irqrestore(&phba->hbalock,
7886 drvr_flag);
7887 goto out_not_finished;
7888 }
dea31012005-04-17 16:05:31 -05007889 }
7890
7891 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007892 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7893 spin_unlock_irqrestore(&phba->hbalock,
7894 drvr_flag);
7895 goto out_not_finished;
7896 }
James Smarta183a152011-10-10 21:32:43 -04007897 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7898 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007899 i = 0;
dea31012005-04-17 16:05:31 -05007900 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007901 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7902 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007903 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007904 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007905 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007906 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007907 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007908 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007909 }
7910
7911 /* Check if we took a mbox interrupt while we were
7912 polling */
7913 if (((word0 & OWN_CHIP) != OWN_CHIP)
7914 && (evtctr != psli->slistat.mbox_event))
7915 break;
7916
James Smart09372822008-01-11 01:52:54 -05007917 if (i++ > 10) {
7918 spin_unlock_irqrestore(&phba->hbalock,
7919 drvr_flag);
7920 msleep(1);
7921 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7922 }
dea31012005-04-17 16:05:31 -05007923
James Smart3772a992009-05-22 14:50:54 -04007924 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007925 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007926 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007927 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007928 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007929 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007930 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007931 /* Check real SLIM for any errors */
7932 slimword0 = readl(phba->MBslimaddr);
7933 slimmb = (MAILBOX_t *) & slimword0;
7934 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7935 && slimmb->mbxStatus) {
7936 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007937 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007938 word0 = slimword0;
7939 }
7940 }
7941 } else {
7942 /* First copy command data */
7943 word0 = readl(phba->MBslimaddr);
7944 }
7945 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007946 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7947 spin_unlock_irqrestore(&phba->hbalock,
7948 drvr_flag);
7949 goto out_not_finished;
7950 }
dea31012005-04-17 16:05:31 -05007951 }
7952
James Smart3772a992009-05-22 14:50:54 -04007953 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007954 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08007955 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7956 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007957 /* Copy the mailbox extension data */
7958 if (pmbox->out_ext_byte_len && pmbox->context2) {
7959 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7960 pmbox->context2,
7961 pmbox->out_ext_byte_len);
7962 }
dea31012005-04-17 16:05:31 -05007963 } else {
7964 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007965 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08007966 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007967 /* Copy the mailbox extension data */
7968 if (pmbox->out_ext_byte_len && pmbox->context2) {
7969 lpfc_memcpy_from_slim(pmbox->context2,
7970 phba->MBslimaddr +
7971 MAILBOX_HBA_EXT_OFFSET,
7972 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007973 }
7974 }
7975
7976 writel(HA_MBATT, phba->HAregaddr);
7977 readl(phba->HAregaddr); /* flush */
7978
7979 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007980 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007981 }
7982
James Smart2e0fef82007-06-17 19:56:36 -05007983 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7984 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007985
7986out_not_finished:
7987 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007988 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007989 lpfc_mbox_cmpl_put(phba, pmbox);
7990 }
7991 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007992}
7993
James Smarte59058c2008-08-24 21:49:00 -04007994/**
James Smartf1126682009-06-10 17:22:44 -04007995 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7996 * @phba: Pointer to HBA context object.
7997 *
7998 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7999 * the driver internal pending mailbox queue. It will then try to wait out the
8000 * possible outstanding mailbox command before return.
8001 *
8002 * Returns:
8003 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8004 * the outstanding mailbox command timed out.
8005 **/
8006static int
8007lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8008{
8009 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04008010 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04008011 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04008012
8013 /* Mark the asynchronous mailbox command posting as blocked */
8014 spin_lock_irq(&phba->hbalock);
8015 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04008016 /* Determine how long we might wait for the active mailbox
8017 * command to be gracefully completed by firmware.
8018 */
James Smarta183a152011-10-10 21:32:43 -04008019 if (phba->sli.mbox_active)
8020 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8021 phba->sli.mbox_active) *
8022 1000) + jiffies;
8023 spin_unlock_irq(&phba->hbalock);
8024
James Smarte8d3c3b2013-10-10 12:21:30 -04008025 /* Make sure the mailbox is really active */
8026 if (timeout)
8027 lpfc_sli4_process_missed_mbox_completions(phba);
8028
James Smartf1126682009-06-10 17:22:44 -04008029 /* Wait for the outstnading mailbox command to complete */
8030 while (phba->sli.mbox_active) {
8031 /* Check active mailbox complete status every 2ms */
8032 msleep(2);
8033 if (time_after(jiffies, timeout)) {
8034 /* Timeout, marked the outstanding cmd not complete */
8035 rc = 1;
8036 break;
8037 }
8038 }
8039
8040 /* Can not cleanly block async mailbox command, fails it */
8041 if (rc) {
8042 spin_lock_irq(&phba->hbalock);
8043 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8044 spin_unlock_irq(&phba->hbalock);
8045 }
8046 return rc;
8047}
8048
8049/**
8050 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8051 * @phba: Pointer to HBA context object.
8052 *
8053 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8054 * commands from the driver internal pending mailbox queue. It makes sure
8055 * that there is no outstanding mailbox command before resuming posting
8056 * asynchronous mailbox commands. If, for any reason, there is outstanding
8057 * mailbox command, it will try to wait it out before resuming asynchronous
8058 * mailbox command posting.
8059 **/
8060static void
8061lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8062{
8063 struct lpfc_sli *psli = &phba->sli;
8064
8065 spin_lock_irq(&phba->hbalock);
8066 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8067 /* Asynchronous mailbox posting is not blocked, do nothing */
8068 spin_unlock_irq(&phba->hbalock);
8069 return;
8070 }
8071
8072 /* Outstanding synchronous mailbox command is guaranteed to be done,
8073 * successful or timeout, after timing-out the outstanding mailbox
8074 * command shall always be removed, so just unblock posting async
8075 * mailbox command and resume
8076 */
8077 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8078 spin_unlock_irq(&phba->hbalock);
8079
8080 /* wake up worker thread to post asynchronlous mailbox command */
8081 lpfc_worker_wake_up(phba);
8082}
8083
8084/**
James Smart2d843ed2012-09-29 11:29:06 -04008085 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8086 * @phba: Pointer to HBA context object.
8087 * @mboxq: Pointer to mailbox object.
8088 *
8089 * The function waits for the bootstrap mailbox register ready bit from
8090 * port for twice the regular mailbox command timeout value.
8091 *
8092 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8093 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8094 **/
8095static int
8096lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8097{
8098 uint32_t db_ready;
8099 unsigned long timeout;
8100 struct lpfc_register bmbx_reg;
8101
8102 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8103 * 1000) + jiffies;
8104
8105 do {
8106 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8107 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8108 if (!db_ready)
8109 msleep(2);
8110
8111 if (time_after(jiffies, timeout))
8112 return MBXERR_ERROR;
8113 } while (!db_ready);
8114
8115 return 0;
8116}
8117
8118/**
James Smartda0436e2009-05-22 14:51:39 -04008119 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8120 * @phba: Pointer to HBA context object.
8121 * @mboxq: Pointer to mailbox object.
8122 *
8123 * The function posts a mailbox to the port. The mailbox is expected
8124 * to be comletely filled in and ready for the port to operate on it.
8125 * This routine executes a synchronous completion operation on the
8126 * mailbox by polling for its completion.
8127 *
8128 * The caller must not be holding any locks when calling this routine.
8129 *
8130 * Returns:
8131 * MBX_SUCCESS - mailbox posted successfully
8132 * Any of the MBX error values.
8133 **/
8134static int
8135lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8136{
8137 int rc = MBX_SUCCESS;
8138 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04008139 uint32_t mcqe_status;
8140 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04008141 struct lpfc_sli *psli = &phba->sli;
8142 struct lpfc_mqe *mb = &mboxq->u.mqe;
8143 struct lpfc_bmbx_create *mbox_rgn;
8144 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04008145
8146 /*
8147 * Only one mailbox can be active to the bootstrap mailbox region
8148 * at a time and there is no queueing provided.
8149 */
8150 spin_lock_irqsave(&phba->hbalock, iflag);
8151 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8152 spin_unlock_irqrestore(&phba->hbalock, iflag);
8153 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008154 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008155 "cannot issue Data: x%x x%x\n",
8156 mboxq->vport ? mboxq->vport->vpi : 0,
8157 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008158 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8159 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008160 psli->sli_flag, MBX_POLL);
8161 return MBXERR_ERROR;
8162 }
8163 /* The server grabs the token and owns it until release */
8164 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8165 phba->sli.mbox_active = mboxq;
8166 spin_unlock_irqrestore(&phba->hbalock, iflag);
8167
James Smart2d843ed2012-09-29 11:29:06 -04008168 /* wait for bootstrap mbox register for readyness */
8169 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8170 if (rc)
8171 goto exit;
8172
James Smartda0436e2009-05-22 14:51:39 -04008173 /*
8174 * Initialize the bootstrap memory region to avoid stale data areas
8175 * in the mailbox post. Then copy the caller's mailbox contents to
8176 * the bmbx mailbox region.
8177 */
8178 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8179 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
8180 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8181 sizeof(struct lpfc_mqe));
8182
8183 /* Post the high mailbox dma address to the port and wait for ready. */
8184 dma_address = &phba->sli4_hba.bmbx.dma_address;
8185 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8186
James Smart2d843ed2012-09-29 11:29:06 -04008187 /* wait for bootstrap mbox register for hi-address write done */
8188 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8189 if (rc)
8190 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008191
8192 /* Post the low mailbox dma address to the port. */
8193 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008194
James Smart2d843ed2012-09-29 11:29:06 -04008195 /* wait for bootstrap mbox register for low 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 /*
8201 * Read the CQ to ensure the mailbox has completed.
8202 * If so, update the mailbox status so that the upper layers
8203 * can complete the request normally.
8204 */
8205 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8206 sizeof(struct lpfc_mqe));
8207 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
8208 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8209 sizeof(struct lpfc_mcqe));
8210 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008211 /*
8212 * When the CQE status indicates a failure and the mailbox status
8213 * indicates success then copy the CQE status into the mailbox status
8214 * (and prefix it with x4000).
8215 */
James Smartda0436e2009-05-22 14:51:39 -04008216 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008217 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8218 bf_set(lpfc_mqe_status, mb,
8219 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008220 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008221 } else
8222 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008223
8224 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008225 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008226 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8227 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008228 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8229 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8230 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008231 bf_get(lpfc_mqe_status, mb),
8232 mb->un.mb_words[0], mb->un.mb_words[1],
8233 mb->un.mb_words[2], mb->un.mb_words[3],
8234 mb->un.mb_words[4], mb->un.mb_words[5],
8235 mb->un.mb_words[6], mb->un.mb_words[7],
8236 mb->un.mb_words[8], mb->un.mb_words[9],
8237 mb->un.mb_words[10], mb->un.mb_words[11],
8238 mb->un.mb_words[12], mboxq->mcqe.word0,
8239 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8240 mboxq->mcqe.trailer);
8241exit:
8242 /* We are holding the token, no needed for lock when release */
8243 spin_lock_irqsave(&phba->hbalock, iflag);
8244 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8245 phba->sli.mbox_active = NULL;
8246 spin_unlock_irqrestore(&phba->hbalock, iflag);
8247 return rc;
8248}
8249
8250/**
8251 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8252 * @phba: Pointer to HBA context object.
8253 * @pmbox: Pointer to mailbox object.
8254 * @flag: Flag indicating how the mailbox need to be processed.
8255 *
8256 * This function is called by discovery code and HBA management code to submit
8257 * a mailbox command to firmware with SLI-4 interface spec.
8258 *
8259 * Return codes the caller owns the mailbox command after the return of the
8260 * function.
8261 **/
8262static int
8263lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8264 uint32_t flag)
8265{
8266 struct lpfc_sli *psli = &phba->sli;
8267 unsigned long iflags;
8268 int rc;
8269
James Smartb76f2dc2011-07-22 18:37:42 -04008270 /* dump from issue mailbox command if setup */
8271 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8272
James Smart8fa38512009-07-19 10:01:03 -04008273 rc = lpfc_mbox_dev_check(phba);
8274 if (unlikely(rc)) {
8275 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008276 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008277 "cannot issue Data: x%x x%x\n",
8278 mboxq->vport ? mboxq->vport->vpi : 0,
8279 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008280 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8281 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008282 psli->sli_flag, flag);
8283 goto out_not_finished;
8284 }
8285
James Smartda0436e2009-05-22 14:51:39 -04008286 /* Detect polling mode and jump to a handler */
8287 if (!phba->sli4_hba.intr_enable) {
8288 if (flag == MBX_POLL)
8289 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8290 else
8291 rc = -EIO;
8292 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008293 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008294 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008295 "(x%x/x%x) failure: "
8296 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8297 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008298 mboxq->vport ? mboxq->vport->vpi : 0,
8299 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008300 lpfc_sli_config_mbox_subsys_get(phba,
8301 mboxq),
8302 lpfc_sli_config_mbox_opcode_get(phba,
8303 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008304 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8305 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8306 bf_get(lpfc_mcqe_ext_status,
8307 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008308 psli->sli_flag, flag);
8309 return rc;
8310 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008311 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8312 "(%d):2542 Try to issue mailbox command "
James Smart7365f6f2018-02-22 08:18:46 -08008313 "x%x (x%x/x%x) synchronously ahead of async "
James Smartf1126682009-06-10 17:22:44 -04008314 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008315 mboxq->vport ? mboxq->vport->vpi : 0,
8316 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008317 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8318 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008319 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008320 /* Try to block the asynchronous mailbox posting */
8321 rc = lpfc_sli4_async_mbox_block(phba);
8322 if (!rc) {
8323 /* Successfully blocked, now issue sync mbox cmd */
8324 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8325 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008326 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008327 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008328 "(%d):2597 Sync Mailbox command "
8329 "x%x (x%x/x%x) failure: "
8330 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8331 "Data: x%x x%x\n,",
8332 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008333 mboxq->u.mb.mbxCommand,
8334 lpfc_sli_config_mbox_subsys_get(phba,
8335 mboxq),
8336 lpfc_sli_config_mbox_opcode_get(phba,
8337 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008338 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8339 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8340 bf_get(lpfc_mcqe_ext_status,
8341 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008342 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008343 /* Unblock the async mailbox posting afterward */
8344 lpfc_sli4_async_mbox_unblock(phba);
8345 }
8346 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008347 }
8348
8349 /* Now, interrupt mode asynchrous mailbox command */
8350 rc = lpfc_mbox_cmd_check(phba, mboxq);
8351 if (rc) {
8352 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008353 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008354 "cannot issue Data: x%x x%x\n",
8355 mboxq->vport ? mboxq->vport->vpi : 0,
8356 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008357 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8358 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008359 psli->sli_flag, flag);
8360 goto out_not_finished;
8361 }
James Smartda0436e2009-05-22 14:51:39 -04008362
8363 /* Put the mailbox command to the driver internal FIFO */
8364 psli->slistat.mbox_busy++;
8365 spin_lock_irqsave(&phba->hbalock, iflags);
8366 lpfc_mbox_put(phba, mboxq);
8367 spin_unlock_irqrestore(&phba->hbalock, iflags);
8368 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8369 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008370 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008371 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8372 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008373 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8374 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008375 phba->pport->port_state,
8376 psli->sli_flag, MBX_NOWAIT);
8377 /* Wake up worker thread to transport mailbox command from head */
8378 lpfc_worker_wake_up(phba);
8379
8380 return MBX_BUSY;
8381
8382out_not_finished:
8383 return MBX_NOT_FINISHED;
8384}
8385
8386/**
8387 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8388 * @phba: Pointer to HBA context object.
8389 *
8390 * This function is called by worker thread to send a mailbox command to
8391 * SLI4 HBA firmware.
8392 *
8393 **/
8394int
8395lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8396{
8397 struct lpfc_sli *psli = &phba->sli;
8398 LPFC_MBOXQ_t *mboxq;
8399 int rc = MBX_SUCCESS;
8400 unsigned long iflags;
8401 struct lpfc_mqe *mqe;
8402 uint32_t mbx_cmnd;
8403
8404 /* Check interrupt mode before post async mailbox command */
8405 if (unlikely(!phba->sli4_hba.intr_enable))
8406 return MBX_NOT_FINISHED;
8407
8408 /* Check for mailbox command service token */
8409 spin_lock_irqsave(&phba->hbalock, iflags);
8410 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8411 spin_unlock_irqrestore(&phba->hbalock, iflags);
8412 return MBX_NOT_FINISHED;
8413 }
8414 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8415 spin_unlock_irqrestore(&phba->hbalock, iflags);
8416 return MBX_NOT_FINISHED;
8417 }
8418 if (unlikely(phba->sli.mbox_active)) {
8419 spin_unlock_irqrestore(&phba->hbalock, iflags);
8420 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8421 "0384 There is pending active mailbox cmd\n");
8422 return MBX_NOT_FINISHED;
8423 }
8424 /* Take the mailbox command service token */
8425 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8426
8427 /* Get the next mailbox command from head of queue */
8428 mboxq = lpfc_mbox_get(phba);
8429
8430 /* If no more mailbox command waiting for post, we're done */
8431 if (!mboxq) {
8432 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8433 spin_unlock_irqrestore(&phba->hbalock, iflags);
8434 return MBX_SUCCESS;
8435 }
8436 phba->sli.mbox_active = mboxq;
8437 spin_unlock_irqrestore(&phba->hbalock, iflags);
8438
8439 /* Check device readiness for posting mailbox command */
8440 rc = lpfc_mbox_dev_check(phba);
8441 if (unlikely(rc))
8442 /* Driver clean routine will clean up pending mailbox */
8443 goto out_not_finished;
8444
8445 /* Prepare the mbox command to be posted */
8446 mqe = &mboxq->u.mqe;
8447 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8448
8449 /* Start timer for the mbox_tmo and log some mailbox post messages */
8450 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008451 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008452
8453 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008454 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008455 "x%x x%x\n",
8456 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008457 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8458 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008459 phba->pport->port_state, psli->sli_flag);
8460
8461 if (mbx_cmnd != MBX_HEARTBEAT) {
8462 if (mboxq->vport) {
8463 lpfc_debugfs_disc_trc(mboxq->vport,
8464 LPFC_DISC_TRC_MBOX_VPORT,
8465 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8466 mbx_cmnd, mqe->un.mb_words[0],
8467 mqe->un.mb_words[1]);
8468 } else {
8469 lpfc_debugfs_disc_trc(phba->pport,
8470 LPFC_DISC_TRC_MBOX,
8471 "MBOX Send: cmd:x%x mb:x%x x%x",
8472 mbx_cmnd, mqe->un.mb_words[0],
8473 mqe->un.mb_words[1]);
8474 }
8475 }
8476 psli->slistat.mbox_cmd++;
8477
8478 /* Post the mailbox command to the port */
8479 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8480 if (rc != MBX_SUCCESS) {
8481 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008482 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008483 "cannot issue Data: x%x x%x\n",
8484 mboxq->vport ? mboxq->vport->vpi : 0,
8485 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008486 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8487 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008488 psli->sli_flag, MBX_NOWAIT);
8489 goto out_not_finished;
8490 }
8491
8492 return rc;
8493
8494out_not_finished:
8495 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008496 if (phba->sli.mbox_active) {
8497 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8498 __lpfc_mbox_cmpl_put(phba, mboxq);
8499 /* Release the token */
8500 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8501 phba->sli.mbox_active = NULL;
8502 }
James Smartda0436e2009-05-22 14:51:39 -04008503 spin_unlock_irqrestore(&phba->hbalock, iflags);
8504
8505 return MBX_NOT_FINISHED;
8506}
8507
8508/**
8509 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8510 * @phba: Pointer to HBA context object.
8511 * @pmbox: Pointer to mailbox object.
8512 * @flag: Flag indicating how the mailbox need to be processed.
8513 *
8514 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8515 * the API jump table function pointer from the lpfc_hba struct.
8516 *
8517 * Return codes the caller owns the mailbox command after the return of the
8518 * function.
8519 **/
8520int
8521lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8522{
8523 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8524}
8525
8526/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008527 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008528 * @phba: The hba struct for which this call is being executed.
8529 * @dev_grp: The HBA PCI-Device group number.
8530 *
8531 * This routine sets up the mbox interface API function jump table in @phba
8532 * struct.
8533 * Returns: 0 - success, -ENODEV - failure.
8534 **/
8535int
8536lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8537{
8538
8539 switch (dev_grp) {
8540 case LPFC_PCI_DEV_LP:
8541 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8542 phba->lpfc_sli_handle_slow_ring_event =
8543 lpfc_sli_handle_slow_ring_event_s3;
8544 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8545 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8546 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8547 break;
8548 case LPFC_PCI_DEV_OC:
8549 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8550 phba->lpfc_sli_handle_slow_ring_event =
8551 lpfc_sli_handle_slow_ring_event_s4;
8552 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8553 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8554 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8555 break;
8556 default:
8557 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8558 "1420 Invalid HBA PCI-device group: 0x%x\n",
8559 dev_grp);
8560 return -ENODEV;
8561 break;
8562 }
8563 return 0;
8564}
8565
8566/**
James Smart3621a712009-04-06 18:47:14 -04008567 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008568 * @phba: Pointer to HBA context object.
8569 * @pring: Pointer to driver SLI ring object.
8570 * @piocb: Pointer to address of newly added command iocb.
8571 *
8572 * This function is called with hbalock held to add a command
8573 * iocb to the txq when SLI layer cannot submit the command iocb
8574 * to the ring.
8575 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008576void
James Smart92d7f7b2007-06-17 19:56:38 -05008577__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008578 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008579{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008580 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008581 /* Insert the caller's iocb in the txq tail for later processing. */
8582 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008583}
8584
James Smarte59058c2008-08-24 21:49:00 -04008585/**
James Smart3621a712009-04-06 18:47:14 -04008586 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008587 * @phba: Pointer to HBA context object.
8588 * @pring: Pointer to driver SLI ring object.
8589 * @piocb: Pointer to address of newly added command iocb.
8590 *
8591 * This function is called with hbalock held before a new
8592 * iocb is submitted to the firmware. This function checks
8593 * txq to flush the iocbs in txq to Firmware before
8594 * submitting new iocbs to the Firmware.
8595 * If there are iocbs in the txq which need to be submitted
8596 * to firmware, lpfc_sli_next_iocb returns the first element
8597 * of the txq after dequeuing it from txq.
8598 * If there is no iocb in the txq then the function will return
8599 * *piocb and *piocb is set to NULL. Caller needs to check
8600 * *piocb to find if there are more commands in the txq.
8601 **/
dea31012005-04-17 16:05:31 -05008602static struct lpfc_iocbq *
8603lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008604 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008605{
8606 struct lpfc_iocbq * nextiocb;
8607
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008608 lockdep_assert_held(&phba->hbalock);
8609
dea31012005-04-17 16:05:31 -05008610 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8611 if (!nextiocb) {
8612 nextiocb = *piocb;
8613 *piocb = NULL;
8614 }
8615
8616 return nextiocb;
8617}
8618
James Smarte59058c2008-08-24 21:49:00 -04008619/**
James Smart3772a992009-05-22 14:50:54 -04008620 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008621 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008622 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008623 * @piocb: Pointer to command iocb.
8624 * @flag: Flag indicating if this command can be put into txq.
8625 *
James Smart3772a992009-05-22 14:50:54 -04008626 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8627 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8628 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8629 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8630 * this function allows only iocbs for posting buffers. This function finds
8631 * next available slot in the command ring and posts the command to the
8632 * available slot and writes the port attention register to request HBA start
8633 * processing new iocb. If there is no slot available in the ring and
8634 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8635 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008636 *
James Smart3772a992009-05-22 14:50:54 -04008637 * This function is called with hbalock held. The function will return success
8638 * after it successfully submit the iocb to firmware or after adding to the
8639 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008640 **/
James Smart98c9ea52007-10-27 13:37:33 -04008641static int
James Smart3772a992009-05-22 14:50:54 -04008642__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008643 struct lpfc_iocbq *piocb, uint32_t flag)
8644{
8645 struct lpfc_iocbq *nextiocb;
8646 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08008647 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05008648
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008649 lockdep_assert_held(&phba->hbalock);
8650
James Smart92d7f7b2007-06-17 19:56:38 -05008651 if (piocb->iocb_cmpl && (!piocb->vport) &&
8652 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8653 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8654 lpfc_printf_log(phba, KERN_ERR,
8655 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008656 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008657 piocb->iocb.ulpCommand);
8658 dump_stack();
8659 return IOCB_ERROR;
8660 }
8661
8662
Linas Vepstas8d63f372007-02-14 14:28:36 -06008663 /* If the PCI channel is in offline state, do not post iocbs. */
8664 if (unlikely(pci_channel_offline(phba->pcidev)))
8665 return IOCB_ERROR;
8666
James Smarta257bf92009-04-06 18:48:10 -04008667 /* If HBA has a deferred error attention, fail the iocb. */
8668 if (unlikely(phba->hba_flag & DEFER_ERATT))
8669 return IOCB_ERROR;
8670
dea31012005-04-17 16:05:31 -05008671 /*
8672 * We should never get an IOCB if we are in a < LINK_DOWN state
8673 */
James Smart2e0fef82007-06-17 19:56:36 -05008674 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008675 return IOCB_ERROR;
8676
8677 /*
8678 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008679 * outstanding event.
dea31012005-04-17 16:05:31 -05008680 */
James Smart0b727fe2007-10-27 13:37:25 -04008681 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008682 goto iocb_busy;
8683
James Smart2e0fef82007-06-17 19:56:36 -05008684 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008685 /*
James Smart2680eea2007-04-25 09:52:55 -04008686 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008687 * can be issued if the link is not up.
8688 */
8689 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008690 case CMD_GEN_REQUEST64_CR:
8691 case CMD_GEN_REQUEST64_CX:
8692 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8693 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008694 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008695 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8696 MENLO_TRANSPORT_TYPE))
8697
8698 goto iocb_busy;
8699 break;
dea31012005-04-17 16:05:31 -05008700 case CMD_QUE_RING_BUF_CN:
8701 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008702 /*
8703 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8704 * completion, iocb_cmpl MUST be 0.
8705 */
8706 if (piocb->iocb_cmpl)
8707 piocb->iocb_cmpl = NULL;
8708 /*FALLTHROUGH*/
8709 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008710 case CMD_CLOSE_XRI_CN:
8711 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008712 break;
8713 default:
8714 goto iocb_busy;
8715 }
8716
8717 /*
8718 * For FCP commands, we must be in a state where we can process link
8719 * attention events.
8720 */
James Smart895427b2017-02-12 13:52:30 -08008721 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05008722 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008723 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008724 }
dea31012005-04-17 16:05:31 -05008725
dea31012005-04-17 16:05:31 -05008726 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8727 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8728 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8729
8730 if (iocb)
8731 lpfc_sli_update_ring(phba, pring);
8732 else
8733 lpfc_sli_update_full_ring(phba, pring);
8734
8735 if (!piocb)
8736 return IOCB_SUCCESS;
8737
8738 goto out_busy;
8739
8740 iocb_busy:
8741 pring->stats.iocb_cmd_delay++;
8742
8743 out_busy:
8744
8745 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008746 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008747 return IOCB_SUCCESS;
8748 }
8749
8750 return IOCB_BUSY;
8751}
8752
James Smart3772a992009-05-22 14:50:54 -04008753/**
James Smart4f774512009-05-22 14:52:35 -04008754 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8755 * @phba: Pointer to HBA context object.
8756 * @piocb: Pointer to command iocb.
8757 * @sglq: Pointer to the scatter gather queue object.
8758 *
8759 * This routine converts the bpl or bde that is in the IOCB
8760 * to a sgl list for the sli4 hardware. The physical address
8761 * of the bpl/bde is converted back to a virtual address.
8762 * If the IOCB contains a BPL then the list of BDE's is
8763 * converted to sli4_sge's. If the IOCB contains a single
8764 * BDE then it is converted to a single sli_sge.
8765 * The IOCB is still in cpu endianess so the contents of
8766 * the bpl can be used without byte swapping.
8767 *
8768 * Returns valid XRI = Success, NO_XRI = Failure.
8769**/
8770static uint16_t
8771lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8772 struct lpfc_sglq *sglq)
8773{
8774 uint16_t xritag = NO_XRI;
8775 struct ulp_bde64 *bpl = NULL;
8776 struct ulp_bde64 bde;
8777 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008778 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008779 IOCB_t *icmd;
8780 int numBdes = 0;
8781 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008782 uint32_t offset = 0; /* accumulated offset in the sg request list */
8783 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008784
8785 if (!piocbq || !sglq)
8786 return xritag;
8787
8788 sgl = (struct sli4_sge *)sglq->sgl;
8789 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008790 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8791 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008792 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8793 numBdes = icmd->un.genreq64.bdl.bdeSize /
8794 sizeof(struct ulp_bde64);
8795 /* The addrHigh and addrLow fields within the IOCB
8796 * have not been byteswapped yet so there is no
8797 * need to swap them back.
8798 */
James Smart1b511972011-12-13 13:23:09 -05008799 if (piocbq->context3)
8800 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8801 else
8802 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008803
James Smart1b511972011-12-13 13:23:09 -05008804 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008805 if (!bpl)
8806 return xritag;
8807
8808 for (i = 0; i < numBdes; i++) {
8809 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008810 sgl->addr_hi = bpl->addrHigh;
8811 sgl->addr_lo = bpl->addrLow;
8812
James Smart05580562011-05-24 11:40:48 -04008813 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008814 if ((i+1) == numBdes)
8815 bf_set(lpfc_sli4_sge_last, sgl, 1);
8816 else
8817 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008818 /* swap the size field back to the cpu so we
8819 * can assign it to the sgl.
8820 */
8821 bde.tus.w = le32_to_cpu(bpl->tus.w);
8822 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008823 /* The offsets in the sgl need to be accumulated
8824 * separately for the request and reply lists.
8825 * The request is always first, the reply follows.
8826 */
8827 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8828 /* add up the reply sg entries */
8829 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8830 inbound++;
8831 /* first inbound? reset the offset */
8832 if (inbound == 1)
8833 offset = 0;
8834 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008835 bf_set(lpfc_sli4_sge_type, sgl,
8836 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008837 offset += bde.tus.f.bdeSize;
8838 }
James Smart546fc852011-03-11 16:06:29 -05008839 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008840 bpl++;
8841 sgl++;
8842 }
8843 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8844 /* The addrHigh and addrLow fields of the BDE have not
8845 * been byteswapped yet so they need to be swapped
8846 * before putting them in the sgl.
8847 */
8848 sgl->addr_hi =
8849 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8850 sgl->addr_lo =
8851 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008852 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008853 bf_set(lpfc_sli4_sge_last, sgl, 1);
8854 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008855 sgl->sge_len =
8856 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008857 }
8858 return sglq->sli4_xritag;
8859}
8860
8861/**
James Smart4f774512009-05-22 14:52:35 -04008862 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8863 * @phba: Pointer to HBA context object.
8864 * @piocb: Pointer to command iocb.
8865 * @wqe: Pointer to the work queue entry.
8866 *
8867 * This routine converts the iocb command to its Work Queue Entry
8868 * equivalent. The wqe pointer should not have any fields set when
8869 * this routine is called because it will memcpy over them.
8870 * This routine does not set the CQ_ID or the WQEC bits in the
8871 * wqe.
8872 *
8873 * Returns: 0 = Success, IOCB_ERROR = Failure.
8874 **/
8875static int
8876lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8877 union lpfc_wqe *wqe)
8878{
James Smart5ffc2662009-11-18 15:39:44 -05008879 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008880 uint8_t ct = 0;
8881 uint32_t fip;
8882 uint32_t abort_tag;
8883 uint8_t command_type = ELS_COMMAND_NON_FIP;
8884 uint8_t cmnd;
8885 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008886 uint16_t abrt_iotag;
8887 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008888 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008889 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008890 int numBdes, i;
8891 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008892 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008893 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008894 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008895
James Smart45ed1192009-10-02 15:17:02 -04008896 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008897 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008898 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008899 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008900 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008901 command_type = ELS_COMMAND_FIP;
8902 else
8903 command_type = ELS_COMMAND_NON_FIP;
8904
James Smartb5c53952016-03-31 14:12:30 -07008905 if (phba->fcp_embed_io)
8906 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008907 /* Some of the fields are in the right position already */
8908 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartae9e28f2017-05-15 15:20:51 -07008909 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8910 /* The ct field has moved so reset */
8911 wqe->generic.wqe_com.word7 = 0;
8912 wqe->generic.wqe_com.word10 = 0;
8913 }
James Smartb5c53952016-03-31 14:12:30 -07008914
8915 abort_tag = (uint32_t) iocbq->iotag;
8916 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008917 /* words0-2 bpl convert bde */
8918 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008919 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8920 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008921 bpl = (struct ulp_bde64 *)
8922 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8923 if (!bpl)
8924 return IOCB_ERROR;
8925
8926 /* Should already be byte swapped. */
8927 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8928 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8929 /* swap the size field back to the cpu so we
8930 * can assign it to the sgl.
8931 */
8932 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008933 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8934 total_len = 0;
8935 for (i = 0; i < numBdes; i++) {
8936 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8937 total_len += bde.tus.f.bdeSize;
8938 }
James Smart4f774512009-05-22 14:52:35 -04008939 } else
James Smart5ffc2662009-11-18 15:39:44 -05008940 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008941
8942 iocbq->iocb.ulpIoTag = iocbq->iotag;
8943 cmnd = iocbq->iocb.ulpCommand;
8944
8945 switch (iocbq->iocb.ulpCommand) {
8946 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008947 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8948 ndlp = iocbq->context_un.ndlp;
8949 else
8950 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008951 if (!iocbq->iocb.ulpLe) {
8952 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8953 "2007 Only Limited Edition cmd Format"
8954 " supported 0x%x\n",
8955 iocbq->iocb.ulpCommand);
8956 return IOCB_ERROR;
8957 }
James Smartff78d8f2011-12-13 13:21:35 -05008958
James Smart5ffc2662009-11-18 15:39:44 -05008959 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008960 /* Els_reguest64 has a TMO */
8961 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8962 iocbq->iocb.ulpTimeout);
8963 /* Need a VF for word 4 set the vf bit*/
8964 bf_set(els_req64_vf, &wqe->els_req, 0);
8965 /* And a VFID for word 12 */
8966 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008967 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008968 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8969 iocbq->iocb.ulpContext);
8970 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8971 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008972 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008973 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008974 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8975 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008976 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8977 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008978 if_type = bf_get(lpfc_sli_intf_if_type,
8979 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08008980 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008981 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008982 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008983 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008984 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008985 *pcmd == ELS_CMD_PLOGI)) {
8986 bf_set(els_req64_sp, &wqe->els_req, 1);
8987 bf_set(els_req64_sid, &wqe->els_req,
8988 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008989 if ((*pcmd == ELS_CMD_FLOGI) &&
8990 !(phba->fc_topology ==
8991 LPFC_TOPOLOGY_LOOP))
8992 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008993 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8994 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008995 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008996 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008997 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8998 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8999 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9000 }
James Smartc8685952009-11-18 15:39:16 -05009001 }
James Smart6d368e52011-05-24 11:44:12 -04009002 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9003 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009004 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9005 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9006 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9007 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9008 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9009 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009010 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04009011 break;
James Smart5ffc2662009-11-18 15:39:44 -05009012 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04009013 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9014 iocbq->iocb.un.ulpWord[3]);
9015 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009016 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05009017 /* The entire sequence is transmitted for this IOCB */
9018 xmit_len = total_len;
9019 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05009020 if (phba->link_flag & LS_LOOPBACK_MODE)
9021 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04009022 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04009023 /* word3 iocb=io_tag32 wqe=reserved */
9024 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04009025 /* word4 relative_offset memcpy */
9026 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009027 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9028 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9029 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9030 LPFC_WQE_IOD_WRITE);
9031 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9032 LPFC_WQE_LENLOC_WORD12);
9033 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05009034 wqe->xmit_sequence.xmit_len = xmit_len;
9035 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009036 break;
James Smart4f774512009-05-22 14:52:35 -04009037 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04009038 /* word3 iocb=iotag32 wqe=seq_payload_len */
9039 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009040 /* word4 iocb=rsvd wqe=rsvd */
9041 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9042 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04009043 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009044 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04009045 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9046 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9047 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9048 LPFC_WQE_LENLOC_WORD3);
9049 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04009050 break;
James Smart4f774512009-05-22 14:52:35 -04009051 case CMD_FCP_IWRITE64_CR:
9052 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04009053 /* word3 iocb=iotag wqe=payload_offset_len */
9054 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009055 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9056 xmit_len + sizeof(struct fcp_rsp));
9057 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9058 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009059 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9060 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9061 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9062 iocbq->iocb.ulpFCP2Rcvy);
9063 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9064 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009065 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9066 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9067 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009068 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009069 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009070 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9071 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009072 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9073 if (iocbq->priority) {
9074 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9075 (iocbq->priority << 1));
9076 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009077 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9078 (phba->cfg_XLanePriority << 1));
9079 }
9080 }
James Smartb5c53952016-03-31 14:12:30 -07009081 /* Note, word 10 is already initialized to 0 */
9082
James Smart0bc2b7c2018-02-22 08:18:48 -08009083 /* Don't set PBDE for Perf hints, just fcp_embed_pbde */
9084 if (phba->fcp_embed_pbde)
9085 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9086 else
9087 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9088
James Smartb5c53952016-03-31 14:12:30 -07009089 if (phba->fcp_embed_io) {
9090 struct lpfc_scsi_buf *lpfc_cmd;
9091 struct sli4_sge *sgl;
9092 union lpfc_wqe128 *wqe128;
9093 struct fcp_cmnd *fcp_cmnd;
9094 uint32_t *ptr;
9095
9096 /* 128 byte wqe support here */
9097 wqe128 = (union lpfc_wqe128 *)wqe;
9098
9099 lpfc_cmd = iocbq->context1;
9100 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9101 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9102
9103 /* Word 0-2 - FCP_CMND */
9104 wqe128->generic.bde.tus.f.bdeFlags =
9105 BUFF_TYPE_BDE_IMMED;
9106 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
9107 wqe128->generic.bde.addrHigh = 0;
9108 wqe128->generic.bde.addrLow = 88; /* Word 22 */
9109
9110 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
James Smart4e565cf2018-02-22 08:18:50 -08009111 bf_set(wqe_dbde, &wqe128->fcp_iwrite.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009112
9113 /* Word 22-29 FCP CMND Payload */
9114 ptr = &wqe128->words[22];
9115 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9116 }
James Smart7851fe22011-07-22 18:36:52 -04009117 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04009118 case CMD_FCP_IREAD64_CR:
9119 /* word3 iocb=iotag wqe=payload_offset_len */
9120 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009121 bf_set(payload_offset_len, &wqe->fcp_iread,
9122 xmit_len + sizeof(struct fcp_rsp));
9123 bf_set(cmd_buff_len, &wqe->fcp_iread,
9124 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009125 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9126 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9127 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9128 iocbq->iocb.ulpFCP2Rcvy);
9129 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04009130 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009131 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9132 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9133 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009134 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009135 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009136 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9137 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009138 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9139 if (iocbq->priority) {
9140 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9141 (iocbq->priority << 1));
9142 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009143 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9144 (phba->cfg_XLanePriority << 1));
9145 }
9146 }
James Smartb5c53952016-03-31 14:12:30 -07009147 /* Note, word 10 is already initialized to 0 */
9148
James Smart0bc2b7c2018-02-22 08:18:48 -08009149 /* Don't set PBDE for Perf hints, just fcp_embed_pbde */
9150 if (phba->fcp_embed_pbde)
9151 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9152 else
9153 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9154
James Smartb5c53952016-03-31 14:12:30 -07009155 if (phba->fcp_embed_io) {
9156 struct lpfc_scsi_buf *lpfc_cmd;
9157 struct sli4_sge *sgl;
9158 union lpfc_wqe128 *wqe128;
9159 struct fcp_cmnd *fcp_cmnd;
9160 uint32_t *ptr;
9161
9162 /* 128 byte wqe support here */
9163 wqe128 = (union lpfc_wqe128 *)wqe;
9164
9165 lpfc_cmd = iocbq->context1;
9166 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9167 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9168
9169 /* Word 0-2 - FCP_CMND */
9170 wqe128->generic.bde.tus.f.bdeFlags =
9171 BUFF_TYPE_BDE_IMMED;
9172 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
9173 wqe128->generic.bde.addrHigh = 0;
9174 wqe128->generic.bde.addrLow = 88; /* Word 22 */
9175
9176 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
James Smart4e565cf2018-02-22 08:18:50 -08009177 bf_set(wqe_dbde, &wqe128->fcp_iread.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009178
9179 /* Word 22-29 FCP CMND Payload */
9180 ptr = &wqe128->words[22];
9181 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9182 }
James Smart7851fe22011-07-22 18:36:52 -04009183 break;
James Smartf1126682009-06-10 17:22:44 -04009184 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04009185 /* word3 iocb=iotag wqe=payload_offset_len */
9186 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9187 bf_set(payload_offset_len, &wqe->fcp_icmd,
9188 xmit_len + sizeof(struct fcp_rsp));
9189 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9190 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009191 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04009192 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04009193 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009194 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9195 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9196 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9197 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9198 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04009199 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9200 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009201 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9202 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009203 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9204 if (iocbq->priority) {
9205 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9206 (iocbq->priority << 1));
9207 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009208 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9209 (phba->cfg_XLanePriority << 1));
9210 }
9211 }
James Smartb5c53952016-03-31 14:12:30 -07009212 /* Note, word 10 is already initialized to 0 */
9213
9214 if (phba->fcp_embed_io) {
9215 struct lpfc_scsi_buf *lpfc_cmd;
9216 struct sli4_sge *sgl;
9217 union lpfc_wqe128 *wqe128;
9218 struct fcp_cmnd *fcp_cmnd;
9219 uint32_t *ptr;
9220
9221 /* 128 byte wqe support here */
9222 wqe128 = (union lpfc_wqe128 *)wqe;
9223
9224 lpfc_cmd = iocbq->context1;
9225 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9226 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9227
9228 /* Word 0-2 - FCP_CMND */
9229 wqe128->generic.bde.tus.f.bdeFlags =
9230 BUFF_TYPE_BDE_IMMED;
9231 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
9232 wqe128->generic.bde.addrHigh = 0;
9233 wqe128->generic.bde.addrLow = 88; /* Word 22 */
9234
9235 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
James Smart4e565cf2018-02-22 08:18:50 -08009236 bf_set(wqe_dbde, &wqe128->fcp_icmd.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009237
9238 /* Word 22-29 FCP CMND Payload */
9239 ptr = &wqe128->words[22];
9240 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9241 }
James Smart7851fe22011-07-22 18:36:52 -04009242 break;
James Smart4f774512009-05-22 14:52:35 -04009243 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009244 /* For this command calculate the xmit length of the
9245 * request bde.
9246 */
9247 xmit_len = 0;
9248 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9249 sizeof(struct ulp_bde64);
9250 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009251 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009252 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9253 break;
James Smart63e801c2010-11-20 23:14:19 -05009254 xmit_len += bde.tus.f.bdeSize;
9255 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009256 /* word3 iocb=IO_TAG wqe=request_payload_len */
9257 wqe->gen_req.request_payload_len = xmit_len;
9258 /* word4 iocb=parameter wqe=relative_offset memcpy */
9259 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009260 /* word6 context tag copied in memcpy */
9261 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9262 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9263 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9264 "2015 Invalid CT %x command 0x%x\n",
9265 ct, iocbq->iocb.ulpCommand);
9266 return IOCB_ERROR;
9267 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009268 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9269 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9270 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9271 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9272 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9273 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9274 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9275 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009276 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009277 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009278 break;
James Smart4f774512009-05-22 14:52:35 -04009279 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009280 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009281 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009282 /* word3 iocb=iotag32 wqe=response_payload_len */
9283 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009284 /* word4 */
9285 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009286 /* word5 iocb=rsvd wge=did */
9287 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009288 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9289
9290 if_type = bf_get(lpfc_sli_intf_if_type,
9291 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009292 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -04009293 if (iocbq->vport->fc_flag & FC_PT2PT) {
9294 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9295 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9296 iocbq->vport->fc_myDID);
9297 if (iocbq->vport->fc_myDID == Fabric_DID) {
9298 bf_set(wqe_els_did,
9299 &wqe->xmit_els_rsp.wqe_dest, 0);
9300 }
9301 }
9302 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009303 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9304 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9305 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9306 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009307 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009308 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009309 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009310 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009311 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9312 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9313 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9314 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9315 LPFC_WQE_LENLOC_WORD3);
9316 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009317 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9318 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009319 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9320 iocbq->context2)->virt);
9321 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009322 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9323 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009324 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009325 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9326 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009327 phba->vpi_ids[phba->pport->vpi]);
9328 }
James Smart4f774512009-05-22 14:52:35 -04009329 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009330 break;
James Smart4f774512009-05-22 14:52:35 -04009331 case CMD_CLOSE_XRI_CN:
9332 case CMD_ABORT_XRI_CN:
9333 case CMD_ABORT_XRI_CX:
9334 /* words 0-2 memcpy should be 0 rserved */
9335 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009336 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9337 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9338 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9339 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9340 } else
9341 fip = 0;
9342
9343 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009344 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009345 * The link is down, or the command was ELS_FIP
9346 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009347 * on the wire.
9348 */
9349 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9350 else
9351 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9352 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009353 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9354 wqe->abort_cmd.rsrvd5 = 0;
9355 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009356 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9357 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009358 /*
9359 * The abort handler will send us CMD_ABORT_XRI_CN or
9360 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9361 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009362 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9363 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9364 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9365 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009366 cmnd = CMD_ABORT_XRI_CX;
9367 command_type = OTHER_COMMAND;
9368 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009369 break;
James Smart6669f9b2009-10-02 15:16:45 -04009370 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009371 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009372 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009373 * we re-construct this WQE here based on information in
9374 * iocbq from scratch.
9375 */
9376 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009377 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009378 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009379 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9380 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009381 LPFC_ABTS_UNSOL_INT) {
9382 /* ABTS sent by initiator to CT exchange, the
9383 * RX_ID field will be filled with the newly
9384 * allocated responder XRI.
9385 */
9386 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9387 iocbq->sli4_xritag);
9388 } else {
9389 /* ABTS sent by responder to CT exchange, the
9390 * RX_ID field will be filled with the responder
9391 * RX_ID from ABTS.
9392 */
9393 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009394 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009395 }
James Smart6669f9b2009-10-02 15:16:45 -04009396 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9397 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009398
9399 /* Use CT=VPI */
9400 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9401 ndlp->nlp_DID);
9402 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9403 iocbq->iocb.ulpContext);
9404 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009405 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009406 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009407 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9408 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9409 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009410 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9411 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009412 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9413 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9414 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9415 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9416 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9417 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9418 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9419 }
9420
James Smart7851fe22011-07-22 18:36:52 -04009421 break;
James Smartae9e28f2017-05-15 15:20:51 -07009422 case CMD_SEND_FRAME:
9423 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9424 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9425 return 0;
James Smart4f774512009-05-22 14:52:35 -04009426 case CMD_XRI_ABORTED_CX:
9427 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009428 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9429 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9430 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9431 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9432 default:
9433 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9434 "2014 Invalid command 0x%x\n",
9435 iocbq->iocb.ulpCommand);
9436 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009437 break;
James Smart4f774512009-05-22 14:52:35 -04009438 }
James Smart6d368e52011-05-24 11:44:12 -04009439
James Smart8012cc32012-10-31 14:44:49 -04009440 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9441 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9442 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9443 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9444 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9445 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9446 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9447 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009448 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9449 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9450 wqe->generic.wqe_com.abort_tag = abort_tag;
9451 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9452 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9453 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9454 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009455 return 0;
9456}
9457
9458/**
9459 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9460 * @phba: Pointer to HBA context object.
9461 * @ring_number: SLI ring number to issue iocb on.
9462 * @piocb: Pointer to command iocb.
9463 * @flag: Flag indicating if this command can be put into txq.
9464 *
9465 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9466 * an iocb command to an HBA with SLI-4 interface spec.
9467 *
9468 * This function is called with hbalock held. The function will return success
9469 * after it successfully submit the iocb to firmware or after adding to the
9470 * txq.
9471 **/
9472static int
9473__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9474 struct lpfc_iocbq *piocb, uint32_t flag)
9475{
9476 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07009477 union lpfc_wqe *wqe;
9478 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05009479 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009480 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009481
James Smart895427b2017-02-12 13:52:30 -08009482 /* Get the WQ */
9483 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9484 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9485 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9486 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9487 else
9488 wq = phba->sli4_hba.oas_wq;
9489 } else {
9490 wq = phba->sli4_hba.els_wq;
9491 }
9492
9493 /* Get corresponding ring */
9494 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009495
James Smartb5c53952016-03-31 14:12:30 -07009496 /*
9497 * The WQE can be either 64 or 128 bytes,
9498 * so allocate space on the stack assuming the largest.
9499 */
9500 wqe = (union lpfc_wqe *)&wqe128;
9501
James Smart895427b2017-02-12 13:52:30 -08009502 lockdep_assert_held(&phba->hbalock);
9503
James Smart4f774512009-05-22 14:52:35 -04009504 if (piocb->sli4_xritag == NO_XRI) {
9505 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009506 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009507 sglq = NULL;
9508 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009509 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009510 if (!(flag & SLI_IOCB_RET_IOCB)) {
9511 __lpfc_sli_ringtx_put(phba,
9512 pring, piocb);
9513 return IOCB_SUCCESS;
9514 } else {
9515 return IOCB_BUSY;
9516 }
9517 } else {
James Smart895427b2017-02-12 13:52:30 -08009518 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009519 if (!sglq) {
9520 if (!(flag & SLI_IOCB_RET_IOCB)) {
9521 __lpfc_sli_ringtx_put(phba,
9522 pring,
9523 piocb);
9524 return IOCB_SUCCESS;
9525 } else
9526 return IOCB_BUSY;
9527 }
9528 }
James Smart4f774512009-05-22 14:52:35 -04009529 }
James Smart2ea259e2017-02-12 13:52:27 -08009530 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009531 /* These IO's already have an XRI and a mapped sgl. */
9532 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009533 else {
James Smart6d368e52011-05-24 11:44:12 -04009534 /*
9535 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009536 * sglq is on the active list
9537 */
James Smartedccdc12013-01-03 15:43:45 -05009538 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009539 if (!sglq)
9540 return IOCB_ERROR;
9541 }
9542
9543 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009544 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009545 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009546 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009547 return IOCB_ERROR;
9548 }
9549
James Smartb5c53952016-03-31 14:12:30 -07009550 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04009551 return IOCB_ERROR;
9552
James Smart895427b2017-02-12 13:52:30 -08009553 if (lpfc_sli4_wq_put(wq, wqe))
9554 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009555 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9556
9557 return 0;
9558}
9559
9560/**
James Smart3772a992009-05-22 14:50:54 -04009561 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9562 *
9563 * This routine wraps the actual lockless version for issusing IOCB function
9564 * pointer from the lpfc_hba struct.
9565 *
9566 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009567 * IOCB_ERROR - Error
9568 * IOCB_SUCCESS - Success
9569 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009570 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009571int
James Smart3772a992009-05-22 14:50:54 -04009572__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9573 struct lpfc_iocbq *piocb, uint32_t flag)
9574{
9575 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9576}
9577
9578/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009579 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009580 * @phba: The hba struct for which this call is being executed.
9581 * @dev_grp: The HBA PCI-Device group number.
9582 *
9583 * This routine sets up the SLI interface API function jump table in @phba
9584 * struct.
9585 * Returns: 0 - success, -ENODEV - failure.
9586 **/
9587int
9588lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9589{
9590
9591 switch (dev_grp) {
9592 case LPFC_PCI_DEV_LP:
9593 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9594 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9595 break;
James Smart4f774512009-05-22 14:52:35 -04009596 case LPFC_PCI_DEV_OC:
9597 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9598 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9599 break;
James Smart3772a992009-05-22 14:50:54 -04009600 default:
9601 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9602 "1419 Invalid HBA PCI-device group: 0x%x\n",
9603 dev_grp);
9604 return -ENODEV;
9605 break;
9606 }
9607 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9608 return 0;
9609}
James Smart92d7f7b2007-06-17 19:56:38 -05009610
James Smarta1efe162015-05-21 13:55:20 -04009611/**
James Smart895427b2017-02-12 13:52:30 -08009612 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009613 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009614 * @piocb: Pointer to command iocb.
9615 *
James Smart895427b2017-02-12 13:52:30 -08009616 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9617 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009618 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009619 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009620 */
James Smart895427b2017-02-12 13:52:30 -08009621struct lpfc_sli_ring *
9622lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009623{
James Smart895427b2017-02-12 13:52:30 -08009624 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smart8b0dff12015-05-22 10:42:38 -04009625 if (!(phba->cfg_fof) ||
James Smart895427b2017-02-12 13:52:30 -08009626 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
James Smart8b0dff12015-05-22 10:42:38 -04009627 if (unlikely(!phba->sli4_hba.fcp_wq))
James Smart895427b2017-02-12 13:52:30 -08009628 return NULL;
James Smart8b0dff12015-05-22 10:42:38 -04009629 /*
James Smart895427b2017-02-12 13:52:30 -08009630 * for abort iocb hba_wqidx should already
James Smart8b0dff12015-05-22 10:42:38 -04009631 * be setup based on what work queue we used.
9632 */
Dick Kennedy8e036a92017-09-29 17:34:35 -07009633 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart895427b2017-02-12 13:52:30 -08009634 piocb->hba_wqidx =
James Smart8b0dff12015-05-22 10:42:38 -04009635 lpfc_sli4_scmd_to_wqidx_distr(phba,
9636 piocb->context1);
Dick Kennedy8e036a92017-09-29 17:34:35 -07009637 piocb->hba_wqidx = piocb->hba_wqidx %
9638 phba->cfg_fcp_io_channel;
9639 }
James Smart895427b2017-02-12 13:52:30 -08009640 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
James Smart8b0dff12015-05-22 10:42:38 -04009641 } else {
9642 if (unlikely(!phba->sli4_hba.oas_wq))
James Smart895427b2017-02-12 13:52:30 -08009643 return NULL;
9644 piocb->hba_wqidx = 0;
9645 return phba->sli4_hba.oas_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009646 }
James Smart895427b2017-02-12 13:52:30 -08009647 } else {
9648 if (unlikely(!phba->sli4_hba.els_wq))
9649 return NULL;
9650 piocb->hba_wqidx = 0;
9651 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009652 }
James Smart9bd2bff52014-09-03 12:57:30 -04009653}
9654
James Smarte59058c2008-08-24 21:49:00 -04009655/**
James Smart3621a712009-04-06 18:47:14 -04009656 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009657 * @phba: Pointer to HBA context object.
9658 * @pring: Pointer to driver SLI ring object.
9659 * @piocb: Pointer to command iocb.
9660 * @flag: Flag indicating if this command can be put into txq.
9661 *
9662 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9663 * function. This function gets the hbalock and calls
9664 * __lpfc_sli_issue_iocb function and will return the error returned
9665 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9666 * functions which do not hold hbalock.
9667 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009668int
James Smart3772a992009-05-22 14:50:54 -04009669lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009670 struct lpfc_iocbq *piocb, uint32_t flag)
9671{
James Smart895427b2017-02-12 13:52:30 -08009672 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009673 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009674 struct lpfc_queue *fpeq;
9675 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009676 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009677 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009678
James Smart7e56aa22012-08-03 12:35:34 -04009679 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08009680 pring = lpfc_sli4_calc_ring(phba, piocb);
9681 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -04009682 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -04009683
James Smart9bd2bff52014-09-03 12:57:30 -04009684 spin_lock_irqsave(&pring->ring_lock, iflags);
9685 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9686 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009687
James Smart9bd2bff52014-09-03 12:57:30 -04009688 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -08009689 idx = piocb->hba_wqidx;
9690 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009691
James Smart895427b2017-02-12 13:52:30 -08009692 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009693
James Smart9bd2bff52014-09-03 12:57:30 -04009694 /* Get associated EQ with this index */
9695 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009696
James Smart9bd2bff52014-09-03 12:57:30 -04009697 /* Turn off interrupts from this EQ */
James Smartb71413d2018-02-22 08:18:40 -08009698 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009699
James Smart9bd2bff52014-09-03 12:57:30 -04009700 /*
9701 * Process all the events on FCP EQ
9702 */
9703 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9704 lpfc_sli4_hba_handle_eqe(phba,
9705 eqe, idx);
9706 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009707 }
James Smartba20c852012-08-03 12:36:52 -04009708
James Smart9bd2bff52014-09-03 12:57:30 -04009709 /* Always clear and re-arm the EQ */
James Smartb71413d2018-02-22 08:18:40 -08009710 phba->sli4_hba.sli4_eq_release(fpeq,
James Smart9bd2bff52014-09-03 12:57:30 -04009711 LPFC_QUEUE_REARM);
9712 }
James Smart895427b2017-02-12 13:52:30 -08009713 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009714 }
James Smart7e56aa22012-08-03 12:35:34 -04009715 } else {
9716 /* For now, SLI2/3 will still use hbalock */
9717 spin_lock_irqsave(&phba->hbalock, iflags);
9718 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9719 spin_unlock_irqrestore(&phba->hbalock, iflags);
9720 }
James Smart92d7f7b2007-06-17 19:56:38 -05009721 return rc;
9722}
9723
James Smarte59058c2008-08-24 21:49:00 -04009724/**
James Smart3621a712009-04-06 18:47:14 -04009725 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009726 * @phba: Pointer to HBA context object.
9727 *
9728 * This function is called while driver attaches with the
9729 * HBA to setup the extra ring. The extra ring is used
9730 * only when driver needs to support target mode functionality
9731 * or IP over FC functionalities.
9732 *
James Smart895427b2017-02-12 13:52:30 -08009733 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04009734 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009735static int
9736lpfc_extra_ring_setup( struct lpfc_hba *phba)
9737{
9738 struct lpfc_sli *psli;
9739 struct lpfc_sli_ring *pring;
9740
9741 psli = &phba->sli;
9742
9743 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009744
9745 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -08009746 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -04009747 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9748 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9749 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9750 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009751
James Smarta4bc3372006-12-02 13:34:16 -05009752 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -08009753 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -05009754
James Smart7e56aa22012-08-03 12:35:34 -04009755 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9756 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9757 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9758 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009759
9760 /* Setup default profile for this ring */
9761 pring->iotag_max = 4096;
9762 pring->num_mask = 1;
9763 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009764 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9765 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009766 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9767 return 0;
9768}
9769
James Smartcb69f7d2011-12-13 13:21:57 -05009770/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9771 * @phba: Pointer to HBA context object.
9772 * @iocbq: Pointer to iocb object.
9773 *
9774 * The async_event handler calls this routine when it receives
9775 * an ASYNC_STATUS_CN event from the port. The port generates
9776 * this event when an Abort Sequence request to an rport fails
9777 * twice in succession. The abort could be originated by the
9778 * driver or by the port. The ABTS could have been for an ELS
9779 * or FCP IO. The port only generates this event when an ABTS
9780 * fails to complete after one retry.
9781 */
9782static void
9783lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9784 struct lpfc_iocbq *iocbq)
9785{
9786 struct lpfc_nodelist *ndlp = NULL;
9787 uint16_t rpi = 0, vpi = 0;
9788 struct lpfc_vport *vport = NULL;
9789
9790 /* The rpi in the ulpContext is vport-sensitive. */
9791 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9792 rpi = iocbq->iocb.ulpContext;
9793
9794 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9795 "3092 Port generated ABTS async event "
9796 "on vpi %d rpi %d status 0x%x\n",
9797 vpi, rpi, iocbq->iocb.ulpStatus);
9798
9799 vport = lpfc_find_vport_by_vpid(phba, vpi);
9800 if (!vport)
9801 goto err_exit;
9802 ndlp = lpfc_findnode_rpi(vport, rpi);
9803 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9804 goto err_exit;
9805
9806 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9807 lpfc_sli_abts_recover_port(vport, ndlp);
9808 return;
9809
9810 err_exit:
9811 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9812 "3095 Event Context not found, no "
9813 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9814 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9815 vpi, rpi);
9816}
9817
9818/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9819 * @phba: pointer to HBA context object.
9820 * @ndlp: nodelist pointer for the impacted rport.
9821 * @axri: pointer to the wcqe containing the failed exchange.
9822 *
9823 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9824 * port. The port generates this event when an abort exchange request to an
9825 * rport fails twice in succession with no reply. The abort could be originated
9826 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9827 */
9828void
9829lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9830 struct lpfc_nodelist *ndlp,
9831 struct sli4_wcqe_xri_aborted *axri)
9832{
9833 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009834 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009835
James Smart6b5151f2012-01-18 16:24:06 -05009836 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009837 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9838 "3115 Node Context not found, driver "
9839 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009840 return;
9841 }
9842
James Smartcb69f7d2011-12-13 13:21:57 -05009843 vport = ndlp->vport;
9844 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9845 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009846 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009847 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009848 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009849 bf_get(lpfc_wcqe_xa_status, axri),
9850 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009851
James Smart5c1db2a2012-03-01 22:34:36 -05009852 /*
9853 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9854 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9855 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9856 */
James Smarte3d2b802012-08-14 14:25:43 -04009857 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009858 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9859 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009860 lpfc_sli_abts_recover_port(vport, ndlp);
9861}
9862
James Smarte59058c2008-08-24 21:49:00 -04009863/**
James Smart3621a712009-04-06 18:47:14 -04009864 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009865 * @phba: Pointer to HBA context object.
9866 * @pring: Pointer to driver SLI ring object.
9867 * @iocbq: Pointer to iocb object.
9868 *
9869 * This function is called by the slow ring event handler
9870 * function when there is an ASYNC event iocb in the ring.
9871 * This function is called with no lock held.
9872 * Currently this function handles only temperature related
9873 * ASYNC events. The function decodes the temperature sensor
9874 * event message and posts events for the management applications.
9875 **/
James Smart98c9ea52007-10-27 13:37:33 -04009876static void
James Smart57127f12007-10-27 13:37:05 -04009877lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9878 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9879{
9880 IOCB_t *icmd;
9881 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009882 struct temp_event temp_event_data;
9883 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009884 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009885
9886 icmd = &iocbq->iocb;
9887 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009888
James Smartcb69f7d2011-12-13 13:21:57 -05009889 switch (evt_code) {
9890 case ASYNC_TEMP_WARN:
9891 case ASYNC_TEMP_SAFE:
9892 temp_event_data.data = (uint32_t) icmd->ulpContext;
9893 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9894 if (evt_code == ASYNC_TEMP_WARN) {
9895 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9896 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9897 "0347 Adapter is very hot, please take "
9898 "corrective action. temperature : %d Celsius\n",
9899 (uint32_t) icmd->ulpContext);
9900 } else {
9901 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9902 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9903 "0340 Adapter temperature is OK now. "
9904 "temperature : %d Celsius\n",
9905 (uint32_t) icmd->ulpContext);
9906 }
9907
9908 /* Send temperature change event to applications */
9909 shost = lpfc_shost_from_vport(phba->pport);
9910 fc_host_post_vendor_event(shost, fc_get_event_number(),
9911 sizeof(temp_event_data), (char *) &temp_event_data,
9912 LPFC_NL_VENDOR_ID);
9913 break;
9914 case ASYNC_STATUS_CN:
9915 lpfc_sli_abts_err_handler(phba, iocbq);
9916 break;
9917 default:
James Smarta257bf92009-04-06 18:48:10 -04009918 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009919 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009920 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009921 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009922 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9923 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9924 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9925 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009926 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009927 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9928 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9929 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9930 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9931
James Smartcb69f7d2011-12-13 13:21:57 -05009932 break;
James Smart57127f12007-10-27 13:37:05 -04009933 }
James Smart57127f12007-10-27 13:37:05 -04009934}
9935
9936
James Smarte59058c2008-08-24 21:49:00 -04009937/**
James Smart895427b2017-02-12 13:52:30 -08009938 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009939 * @phba: Pointer to HBA context object.
9940 *
9941 * lpfc_sli_setup sets up rings of the SLI interface with
9942 * number of iocbs per ring and iotags. This function is
9943 * called while driver attach to the HBA and before the
9944 * interrupts are enabled. So there is no need for locking.
9945 *
9946 * This function always returns 0.
9947 **/
dea31012005-04-17 16:05:31 -05009948int
James Smart895427b2017-02-12 13:52:30 -08009949lpfc_sli4_setup(struct lpfc_hba *phba)
9950{
9951 struct lpfc_sli_ring *pring;
9952
9953 pring = phba->sli4_hba.els_wq->pring;
9954 pring->num_mask = LPFC_MAX_RING_MASK;
9955 pring->prt[0].profile = 0; /* Mask 0 */
9956 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9957 pring->prt[0].type = FC_TYPE_ELS;
9958 pring->prt[0].lpfc_sli_rcv_unsol_event =
9959 lpfc_els_unsol_event;
9960 pring->prt[1].profile = 0; /* Mask 1 */
9961 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9962 pring->prt[1].type = FC_TYPE_ELS;
9963 pring->prt[1].lpfc_sli_rcv_unsol_event =
9964 lpfc_els_unsol_event;
9965 pring->prt[2].profile = 0; /* Mask 2 */
9966 /* NameServer Inquiry */
9967 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9968 /* NameServer */
9969 pring->prt[2].type = FC_TYPE_CT;
9970 pring->prt[2].lpfc_sli_rcv_unsol_event =
9971 lpfc_ct_unsol_event;
9972 pring->prt[3].profile = 0; /* Mask 3 */
9973 /* NameServer response */
9974 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9975 /* NameServer */
9976 pring->prt[3].type = FC_TYPE_CT;
9977 pring->prt[3].lpfc_sli_rcv_unsol_event =
9978 lpfc_ct_unsol_event;
9979 return 0;
9980}
9981
9982/**
9983 * lpfc_sli_setup - SLI ring setup function
9984 * @phba: Pointer to HBA context object.
9985 *
9986 * lpfc_sli_setup sets up rings of the SLI interface with
9987 * number of iocbs per ring and iotags. This function is
9988 * called while driver attach to the HBA and before the
9989 * interrupts are enabled. So there is no need for locking.
9990 *
9991 * This function always returns 0. SLI3 only.
9992 **/
9993int
dea31012005-04-17 16:05:31 -05009994lpfc_sli_setup(struct lpfc_hba *phba)
9995{
James Smarted957682007-06-17 19:56:37 -05009996 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009997 struct lpfc_sli *psli = &phba->sli;
9998 struct lpfc_sli_ring *pring;
9999
James Smart2a76a282012-08-03 12:35:54 -040010000 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -050010001 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -050010002
James Bottomley604a3e32005-10-29 10:28:33 -050010003 psli->iocbq_lookup = NULL;
10004 psli->iocbq_lookup_len = 0;
10005 psli->last_iotag = 0;
10006
dea31012005-04-17 16:05:31 -050010007 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010008 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010009 switch (i) {
10010 case LPFC_FCP_RING: /* ring 0 - FCP */
10011 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010012 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10013 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10014 pring->sli.sli3.numCiocb +=
10015 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10016 pring->sli.sli3.numRiocb +=
10017 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10018 pring->sli.sli3.numCiocb +=
10019 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10020 pring->sli.sli3.numRiocb +=
10021 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10022 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010023 SLI3_IOCB_CMD_SIZE :
10024 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010025 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010026 SLI3_IOCB_RSP_SIZE :
10027 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010028 pring->iotag_ctr = 0;
10029 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -050010030 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -050010031 pring->fast_iotag = pring->iotag_max;
10032 pring->num_mask = 0;
10033 break;
James Smarta4bc3372006-12-02 13:34:16 -050010034 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -050010035 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010036 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10037 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10038 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010039 SLI3_IOCB_CMD_SIZE :
10040 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010041 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010042 SLI3_IOCB_RSP_SIZE :
10043 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -050010044 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -050010045 pring->num_mask = 0;
10046 break;
10047 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10048 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010049 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10050 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10051 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010052 SLI3_IOCB_CMD_SIZE :
10053 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010054 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010055 SLI3_IOCB_RSP_SIZE :
10056 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010057 pring->fast_iotag = 0;
10058 pring->iotag_ctr = 0;
10059 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -040010060 pring->lpfc_sli_rcv_async_status =
10061 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -040010062 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -050010063 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -040010064 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10065 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010066 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010067 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010068 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -040010069 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10070 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010071 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010072 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010073 pring->prt[2].profile = 0; /* Mask 2 */
10074 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -040010075 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -050010076 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010077 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010078 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010079 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010080 pring->prt[3].profile = 0; /* Mask 3 */
10081 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -040010082 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -050010083 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010084 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010085 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010086 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010087 break;
10088 }
James Smart7e56aa22012-08-03 12:35:34 -040010089 totiocbsize += (pring->sli.sli3.numCiocb *
10090 pring->sli.sli3.sizeCiocb) +
10091 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050010092 }
James Smarted957682007-06-17 19:56:37 -050010093 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050010094 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040010095 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10096 "SLI2 SLIM Data: x%x x%lx\n",
10097 phba->brd_no, totiocbsize,
10098 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050010099 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010100 if (phba->cfg_multi_ring_support == 2)
10101 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050010102
10103 return 0;
10104}
10105
James Smarte59058c2008-08-24 21:49:00 -040010106/**
James Smart895427b2017-02-12 13:52:30 -080010107 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040010108 * @phba: Pointer to HBA context object.
10109 *
James Smart895427b2017-02-12 13:52:30 -080010110 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040010111 * ring. This function also initializes ring indices of each ring.
10112 * This function is called during the initialization of the SLI
10113 * interface of an HBA.
10114 * This function is called with no lock held and always returns
10115 * 1.
10116 **/
James Smart895427b2017-02-12 13:52:30 -080010117void
10118lpfc_sli4_queue_init(struct lpfc_hba *phba)
10119{
10120 struct lpfc_sli *psli;
10121 struct lpfc_sli_ring *pring;
10122 int i;
10123
10124 psli = &phba->sli;
10125 spin_lock_irq(&phba->hbalock);
10126 INIT_LIST_HEAD(&psli->mboxq);
10127 INIT_LIST_HEAD(&psli->mboxq_cmpl);
10128 /* Initialize list headers for txq and txcmplq as double linked lists */
10129 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
10130 pring = phba->sli4_hba.fcp_wq[i]->pring;
10131 pring->flag = 0;
10132 pring->ringno = LPFC_FCP_RING;
10133 INIT_LIST_HEAD(&pring->txq);
10134 INIT_LIST_HEAD(&pring->txcmplq);
10135 INIT_LIST_HEAD(&pring->iocb_continueq);
10136 spin_lock_init(&pring->ring_lock);
10137 }
10138 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
10139 pring = phba->sli4_hba.nvme_wq[i]->pring;
10140 pring->flag = 0;
10141 pring->ringno = LPFC_FCP_RING;
10142 INIT_LIST_HEAD(&pring->txq);
10143 INIT_LIST_HEAD(&pring->txcmplq);
10144 INIT_LIST_HEAD(&pring->iocb_continueq);
10145 spin_lock_init(&pring->ring_lock);
10146 }
10147 pring = phba->sli4_hba.els_wq->pring;
10148 pring->flag = 0;
10149 pring->ringno = LPFC_ELS_RING;
10150 INIT_LIST_HEAD(&pring->txq);
10151 INIT_LIST_HEAD(&pring->txcmplq);
10152 INIT_LIST_HEAD(&pring->iocb_continueq);
10153 spin_lock_init(&pring->ring_lock);
10154
10155 if (phba->cfg_nvme_io_channel) {
10156 pring = phba->sli4_hba.nvmels_wq->pring;
10157 pring->flag = 0;
10158 pring->ringno = LPFC_ELS_RING;
10159 INIT_LIST_HEAD(&pring->txq);
10160 INIT_LIST_HEAD(&pring->txcmplq);
10161 INIT_LIST_HEAD(&pring->iocb_continueq);
10162 spin_lock_init(&pring->ring_lock);
10163 }
10164
10165 if (phba->cfg_fof) {
10166 pring = phba->sli4_hba.oas_wq->pring;
10167 pring->flag = 0;
10168 pring->ringno = LPFC_FCP_RING;
10169 INIT_LIST_HEAD(&pring->txq);
10170 INIT_LIST_HEAD(&pring->txcmplq);
10171 INIT_LIST_HEAD(&pring->iocb_continueq);
10172 spin_lock_init(&pring->ring_lock);
10173 }
10174
10175 spin_unlock_irq(&phba->hbalock);
10176}
10177
10178/**
10179 * lpfc_sli_queue_init - Queue initialization function
10180 * @phba: Pointer to HBA context object.
10181 *
10182 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10183 * ring. This function also initializes ring indices of each ring.
10184 * This function is called during the initialization of the SLI
10185 * interface of an HBA.
10186 * This function is called with no lock held and always returns
10187 * 1.
10188 **/
10189void
10190lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010191{
10192 struct lpfc_sli *psli;
10193 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050010194 int i;
dea31012005-04-17 16:05:31 -050010195
10196 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050010197 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010198 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050010199 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050010200 /* Initialize list headers for txq and txcmplq as double linked lists */
10201 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010202 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010203 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040010204 pring->sli.sli3.next_cmdidx = 0;
10205 pring->sli.sli3.local_getidx = 0;
10206 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050010207 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050010208 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050010209 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080010210 pring->flag = 0;
10211 INIT_LIST_HEAD(&pring->txq);
10212 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010213 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010214 }
James Smart2e0fef82007-06-17 19:56:36 -050010215 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010216}
10217
James Smarte59058c2008-08-24 21:49:00 -040010218/**
James Smart04c68492009-05-22 14:52:52 -040010219 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10220 * @phba: Pointer to HBA context object.
10221 *
10222 * This routine flushes the mailbox command subsystem. It will unconditionally
10223 * flush all the mailbox commands in the three possible stages in the mailbox
10224 * command sub-system: pending mailbox command queue; the outstanding mailbox
10225 * command; and completed mailbox command queue. It is caller's responsibility
10226 * to make sure that the driver is in the proper state to flush the mailbox
10227 * command sub-system. Namely, the posting of mailbox commands into the
10228 * pending mailbox command queue from the various clients must be stopped;
10229 * either the HBA is in a state that it will never works on the outstanding
10230 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10231 * mailbox command has been completed.
10232 **/
10233static void
10234lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10235{
10236 LIST_HEAD(completions);
10237 struct lpfc_sli *psli = &phba->sli;
10238 LPFC_MBOXQ_t *pmb;
10239 unsigned long iflag;
10240
10241 /* Flush all the mailbox commands in the mbox system */
10242 spin_lock_irqsave(&phba->hbalock, iflag);
10243 /* The pending mailbox command queue */
10244 list_splice_init(&phba->sli.mboxq, &completions);
10245 /* The outstanding active mailbox command */
10246 if (psli->mbox_active) {
10247 list_add_tail(&psli->mbox_active->list, &completions);
10248 psli->mbox_active = NULL;
10249 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10250 }
10251 /* The completed mailbox command queue */
10252 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10253 spin_unlock_irqrestore(&phba->hbalock, iflag);
10254
10255 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10256 while (!list_empty(&completions)) {
10257 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10258 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10259 if (pmb->mbox_cmpl)
10260 pmb->mbox_cmpl(phba, pmb);
10261 }
10262}
10263
10264/**
James Smart3621a712009-04-06 18:47:14 -040010265 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010266 * @vport: Pointer to virtual port object.
10267 *
10268 * lpfc_sli_host_down is called to clean up the resources
10269 * associated with a vport before destroying virtual
10270 * port data structures.
10271 * This function does following operations:
10272 * - Free discovery resources associated with this virtual
10273 * port.
10274 * - Free iocbs associated with this virtual port in
10275 * the txq.
10276 * - Send abort for all iocb commands associated with this
10277 * vport in txcmplq.
10278 *
10279 * This function is called with no lock held and always returns 1.
10280 **/
dea31012005-04-17 16:05:31 -050010281int
James Smart92d7f7b2007-06-17 19:56:38 -050010282lpfc_sli_host_down(struct lpfc_vport *vport)
10283{
James Smart858c9f62007-06-17 19:56:39 -050010284 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010285 struct lpfc_hba *phba = vport->phba;
10286 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010287 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010288 struct lpfc_sli_ring *pring;
10289 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010290 int i;
10291 unsigned long flags = 0;
10292 uint16_t prev_pring_flag;
10293
10294 lpfc_cleanup_discovery_resources(vport);
10295
10296 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010297
James Smart895427b2017-02-12 13:52:30 -080010298 /*
10299 * Error everything on the txq since these iocbs
10300 * have not been given to the FW yet.
10301 * Also issue ABTS for everything on the txcmplq
10302 */
10303 if (phba->sli_rev != LPFC_SLI_REV4) {
10304 for (i = 0; i < psli->num_rings; i++) {
10305 pring = &psli->sli3_ring[i];
10306 prev_pring_flag = pring->flag;
10307 /* Only slow rings */
10308 if (pring->ringno == LPFC_ELS_RING) {
10309 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10310 /* Set the lpfc data pending flag */
10311 set_bit(LPFC_DATA_READY, &phba->data_flags);
10312 }
10313 list_for_each_entry_safe(iocb, next_iocb,
10314 &pring->txq, list) {
10315 if (iocb->vport != vport)
10316 continue;
10317 list_move_tail(&iocb->list, &completions);
10318 }
10319 list_for_each_entry_safe(iocb, next_iocb,
10320 &pring->txcmplq, list) {
10321 if (iocb->vport != vport)
10322 continue;
10323 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10324 }
10325 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010326 }
James Smart895427b2017-02-12 13:52:30 -080010327 } else {
10328 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10329 pring = qp->pring;
10330 if (!pring)
10331 continue;
10332 if (pring == phba->sli4_hba.els_wq->pring) {
10333 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10334 /* Set the lpfc data pending flag */
10335 set_bit(LPFC_DATA_READY, &phba->data_flags);
10336 }
10337 prev_pring_flag = pring->flag;
10338 spin_lock_irq(&pring->ring_lock);
10339 list_for_each_entry_safe(iocb, next_iocb,
10340 &pring->txq, list) {
10341 if (iocb->vport != vport)
10342 continue;
10343 list_move_tail(&iocb->list, &completions);
10344 }
10345 spin_unlock_irq(&pring->ring_lock);
10346 list_for_each_entry_safe(iocb, next_iocb,
10347 &pring->txcmplq, list) {
10348 if (iocb->vport != vport)
10349 continue;
10350 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10351 }
10352 pring->flag = prev_pring_flag;
10353 }
James Smart92d7f7b2007-06-17 19:56:38 -050010354 }
James Smart92d7f7b2007-06-17 19:56:38 -050010355 spin_unlock_irqrestore(&phba->hbalock, flags);
10356
James Smarta257bf92009-04-06 18:48:10 -040010357 /* Cancel all the IOCBs from the completions list */
10358 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10359 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010360 return 1;
10361}
10362
James Smarte59058c2008-08-24 21:49:00 -040010363/**
James Smart3621a712009-04-06 18:47:14 -040010364 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010365 * @phba: Pointer to HBA context object.
10366 *
10367 * This function cleans up all iocb, buffers, mailbox commands
10368 * while shutting down the HBA. This function is called with no
10369 * lock held and always returns 1.
10370 * This function does the following to cleanup driver resources:
10371 * - Free discovery resources for each virtual port
10372 * - Cleanup any pending fabric iocbs
10373 * - Iterate through the iocb txq and free each entry
10374 * in the list.
10375 * - Free up any buffer posted to the HBA
10376 * - Free mailbox commands in the mailbox queue.
10377 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010378int
James Smart2e0fef82007-06-17 19:56:36 -050010379lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010380{
James Smart2534ba72007-04-25 09:52:20 -040010381 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010382 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010383 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010384 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010385 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010386 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010387 int i;
10388
10389 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010390 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010391
dea31012005-04-17 16:05:31 -050010392 lpfc_hba_down_prep(phba);
10393
James Smart92d7f7b2007-06-17 19:56:38 -050010394 lpfc_fabric_abort_hba(phba);
10395
James Smart2e0fef82007-06-17 19:56:36 -050010396 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010397
James Smart895427b2017-02-12 13:52:30 -080010398 /*
10399 * Error everything on the txq since these iocbs
10400 * have not been given to the FW yet.
10401 */
10402 if (phba->sli_rev != LPFC_SLI_REV4) {
10403 for (i = 0; i < psli->num_rings; i++) {
10404 pring = &psli->sli3_ring[i];
10405 /* Only slow rings */
10406 if (pring->ringno == LPFC_ELS_RING) {
10407 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10408 /* Set the lpfc data pending flag */
10409 set_bit(LPFC_DATA_READY, &phba->data_flags);
10410 }
10411 list_splice_init(&pring->txq, &completions);
10412 }
10413 } else {
10414 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10415 pring = qp->pring;
10416 if (!pring)
10417 continue;
10418 spin_lock_irq(&pring->ring_lock);
10419 list_splice_init(&pring->txq, &completions);
10420 spin_unlock_irq(&pring->ring_lock);
10421 if (pring == phba->sli4_hba.els_wq->pring) {
10422 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10423 /* Set the lpfc data pending flag */
10424 set_bit(LPFC_DATA_READY, &phba->data_flags);
10425 }
10426 }
dea31012005-04-17 16:05:31 -050010427 }
James Smart2e0fef82007-06-17 19:56:36 -050010428 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010429
James Smarta257bf92009-04-06 18:48:10 -040010430 /* Cancel all the IOCBs from the completions list */
10431 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10432 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010433
James Smart0ff10d42008-01-11 01:52:36 -050010434 spin_lock_irqsave(&phba->hbalock, flags);
10435 list_splice_init(&phba->elsbuf, &completions);
10436 phba->elsbuf_cnt = 0;
10437 phba->elsbuf_prev_cnt = 0;
10438 spin_unlock_irqrestore(&phba->hbalock, flags);
10439
10440 while (!list_empty(&completions)) {
10441 list_remove_head(&completions, buf_ptr,
10442 struct lpfc_dmabuf, list);
10443 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10444 kfree(buf_ptr);
10445 }
10446
dea31012005-04-17 16:05:31 -050010447 /* Return any active mbox cmds */
10448 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010449
James Smartda0436e2009-05-22 14:51:39 -040010450 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010451 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010452 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010453
James Smartda0436e2009-05-22 14:51:39 -040010454 return 1;
10455}
James Smart92d7f7b2007-06-17 19:56:38 -050010456
James Smartda0436e2009-05-22 14:51:39 -040010457/**
James Smart3621a712009-04-06 18:47:14 -040010458 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010459 * @srcp: Source memory pointer.
10460 * @destp: Destination memory pointer.
10461 * @cnt: Number of words required to be copied.
10462 *
10463 * This function is used for copying data between driver memory
10464 * and the SLI memory. This function also changes the endianness
10465 * of each word if native endianness is different from SLI
10466 * endianness. This function can be called with or without
10467 * lock.
10468 **/
dea31012005-04-17 16:05:31 -050010469void
10470lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10471{
10472 uint32_t *src = srcp;
10473 uint32_t *dest = destp;
10474 uint32_t ldata;
10475 int i;
10476
10477 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10478 ldata = *src;
10479 ldata = le32_to_cpu(ldata);
10480 *dest = ldata;
10481 src++;
10482 dest++;
10483 }
10484}
10485
James Smarte59058c2008-08-24 21:49:00 -040010486
10487/**
James Smarta0c87cb2009-07-19 10:01:10 -040010488 * lpfc_sli_bemem_bcopy - SLI memory copy function
10489 * @srcp: Source memory pointer.
10490 * @destp: Destination memory pointer.
10491 * @cnt: Number of words required to be copied.
10492 *
10493 * This function is used for copying data between a data structure
10494 * with big endian representation to local endianness.
10495 * This function can be called with or without lock.
10496 **/
10497void
10498lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10499{
10500 uint32_t *src = srcp;
10501 uint32_t *dest = destp;
10502 uint32_t ldata;
10503 int i;
10504
10505 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10506 ldata = *src;
10507 ldata = be32_to_cpu(ldata);
10508 *dest = ldata;
10509 src++;
10510 dest++;
10511 }
10512}
10513
10514/**
James Smart3621a712009-04-06 18:47:14 -040010515 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010516 * @phba: Pointer to HBA context object.
10517 * @pring: Pointer to driver SLI ring object.
10518 * @mp: Pointer to driver buffer object.
10519 *
10520 * This function is called with no lock held.
10521 * It always return zero after adding the buffer to the postbufq
10522 * buffer list.
10523 **/
dea31012005-04-17 16:05:31 -050010524int
James Smart2e0fef82007-06-17 19:56:36 -050010525lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10526 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010527{
10528 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10529 later */
James Smart2e0fef82007-06-17 19:56:36 -050010530 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010531 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010532 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010533 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010534 return 0;
10535}
10536
James Smarte59058c2008-08-24 21:49:00 -040010537/**
James Smart3621a712009-04-06 18:47:14 -040010538 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010539 * @phba: Pointer to HBA context object.
10540 *
10541 * When HBQ is enabled, buffers are searched based on tags. This function
10542 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10543 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10544 * does not conflict with tags of buffer posted for unsolicited events.
10545 * The function returns the allocated tag. The function is called with
10546 * no locks held.
10547 **/
James Smart76bb24e2007-10-27 13:38:00 -040010548uint32_t
10549lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10550{
10551 spin_lock_irq(&phba->hbalock);
10552 phba->buffer_tag_count++;
10553 /*
10554 * Always set the QUE_BUFTAG_BIT to distiguish between
10555 * a tag assigned by HBQ.
10556 */
10557 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10558 spin_unlock_irq(&phba->hbalock);
10559 return phba->buffer_tag_count;
10560}
10561
James Smarte59058c2008-08-24 21:49:00 -040010562/**
James Smart3621a712009-04-06 18:47:14 -040010563 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010564 * @phba: Pointer to HBA context object.
10565 * @pring: Pointer to driver SLI ring object.
10566 * @tag: Buffer tag.
10567 *
10568 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10569 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10570 * iocb is posted to the response ring with the tag of the buffer.
10571 * This function searches the pring->postbufq list using the tag
10572 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10573 * iocb. If the buffer is found then lpfc_dmabuf object of the
10574 * buffer is returned to the caller else NULL is returned.
10575 * This function is called with no lock held.
10576 **/
James Smart76bb24e2007-10-27 13:38:00 -040010577struct lpfc_dmabuf *
10578lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10579 uint32_t tag)
10580{
10581 struct lpfc_dmabuf *mp, *next_mp;
10582 struct list_head *slp = &pring->postbufq;
10583
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010584 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010585 spin_lock_irq(&phba->hbalock);
10586 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10587 if (mp->buffer_tag == tag) {
10588 list_del_init(&mp->list);
10589 pring->postbufq_cnt--;
10590 spin_unlock_irq(&phba->hbalock);
10591 return mp;
10592 }
10593 }
10594
10595 spin_unlock_irq(&phba->hbalock);
10596 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010597 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010598 "ring %d Data x%lx x%p x%p x%x\n",
10599 pring->ringno, (unsigned long) tag,
10600 slp->next, slp->prev, pring->postbufq_cnt);
10601
10602 return NULL;
10603}
dea31012005-04-17 16:05:31 -050010604
James Smarte59058c2008-08-24 21:49:00 -040010605/**
James Smart3621a712009-04-06 18:47:14 -040010606 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010607 * @phba: Pointer to HBA context object.
10608 * @pring: Pointer to driver SLI ring object.
10609 * @phys: DMA address of the buffer.
10610 *
10611 * This function searches the buffer list using the dma_address
10612 * of unsolicited event to find the driver's lpfc_dmabuf object
10613 * corresponding to the dma_address. The function returns the
10614 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10615 * This function is called by the ct and els unsolicited event
10616 * handlers to get the buffer associated with the unsolicited
10617 * event.
10618 *
10619 * This function is called with no lock held.
10620 **/
dea31012005-04-17 16:05:31 -050010621struct lpfc_dmabuf *
10622lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10623 dma_addr_t phys)
10624{
10625 struct lpfc_dmabuf *mp, *next_mp;
10626 struct list_head *slp = &pring->postbufq;
10627
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010628 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010629 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010630 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10631 if (mp->phys == phys) {
10632 list_del_init(&mp->list);
10633 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010634 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010635 return mp;
10636 }
10637 }
10638
James Smart2e0fef82007-06-17 19:56:36 -050010639 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010640 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010641 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010642 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010643 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010644 slp->next, slp->prev, pring->postbufq_cnt);
10645 return NULL;
10646}
10647
James Smarte59058c2008-08-24 21:49:00 -040010648/**
James Smart3621a712009-04-06 18:47:14 -040010649 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010650 * @phba: Pointer to HBA context object.
10651 * @cmdiocb: Pointer to driver command iocb object.
10652 * @rspiocb: Pointer to driver response iocb object.
10653 *
10654 * This function is the completion handler for the abort iocbs for
10655 * ELS commands. This function is called from the ELS ring event
10656 * handler with no lock held. This function frees memory resources
10657 * associated with the abort iocb.
10658 **/
dea31012005-04-17 16:05:31 -050010659static void
James Smart2e0fef82007-06-17 19:56:36 -050010660lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10661 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050010662{
James Smart2e0fef82007-06-17 19:56:36 -050010663 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040010664 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050010665 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040010666
10667 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050010668
10669 /*
10670 * Assume that the port already completed and returned, or
10671 * will return the iocb. Just Log the message.
10672 */
James Smart2680eea2007-04-25 09:52:55 -040010673 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10674 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10675
James Smart2e0fef82007-06-17 19:56:36 -050010676 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040010677 if (phba->sli_rev < LPFC_SLI_REV4) {
10678 if (abort_iotag != 0 &&
10679 abort_iotag <= phba->sli.last_iotag)
10680 abort_iocb =
10681 phba->sli.iocbq_lookup[abort_iotag];
10682 } else
10683 /* For sli4 the abort_tag is the XRI,
10684 * so the abort routine puts the iotag of the iocb
10685 * being aborted in the context field of the abort
10686 * IOCB.
10687 */
10688 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040010689
James Smart2a9bf3d2010-06-07 15:24:45 -040010690 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10691 "0327 Cannot abort els iocb %p "
10692 "with tag %x context %x, abort status %x, "
10693 "abort code %x\n",
10694 abort_iocb, abort_iotag, abort_context,
10695 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040010696
James Smartff78d8f2011-12-13 13:21:35 -050010697 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040010698 }
James Bottomley604a3e32005-10-29 10:28:33 -050010699 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050010700 return;
10701}
10702
James Smarte59058c2008-08-24 21:49:00 -040010703/**
James Smart3621a712009-04-06 18:47:14 -040010704 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040010705 * @phba: Pointer to HBA context object.
10706 * @cmdiocb: Pointer to driver command iocb object.
10707 * @rspiocb: Pointer to driver response iocb object.
10708 *
10709 * The function is called from SLI ring event handler with no
10710 * lock held. This function is the completion handler for ELS commands
10711 * which are aborted. The function frees memory resources used for
10712 * the aborted ELS commands.
10713 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010714static void
10715lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10716 struct lpfc_iocbq *rspiocb)
10717{
10718 IOCB_t *irsp = &rspiocb->iocb;
10719
10720 /* ELS cmd tag <ulpIoTag> completes */
10721 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040010722 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050010723 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010724 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050010725 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050010726 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10727 lpfc_ct_free_iocb(phba, cmdiocb);
10728 else
10729 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050010730 return;
10731}
10732
James Smarte59058c2008-08-24 21:49:00 -040010733/**
James Smart5af5eee2010-10-22 11:06:38 -040010734 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040010735 * @phba: Pointer to HBA context object.
10736 * @pring: Pointer to driver SLI ring object.
10737 * @cmdiocb: Pointer to driver command iocb object.
10738 *
James Smart5af5eee2010-10-22 11:06:38 -040010739 * This function issues an abort iocb for the provided command iocb down to
10740 * the port. Other than the case the outstanding command iocb is an abort
10741 * request, this function issues abort out unconditionally. This function is
10742 * called with hbalock held. The function returns 0 when it fails due to
10743 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040010744 **/
James Smart5af5eee2010-10-22 11:06:38 -040010745static int
10746lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010747 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050010748{
James Smart2e0fef82007-06-17 19:56:36 -050010749 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010750 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050010751 IOCB_t *icmd = NULL;
10752 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010753 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040010754 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -040010755
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010756 lockdep_assert_held(&phba->hbalock);
10757
James Smart92d7f7b2007-06-17 19:56:38 -050010758 /*
10759 * There are certain command types we don't want to abort. And we
10760 * don't want to abort commands that are already in the process of
10761 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010762 */
10763 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010764 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010765 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10766 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010767 return 0;
10768
dea31012005-04-17 16:05:31 -050010769 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010770 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010771 if (abtsiocbp == NULL)
10772 return 0;
dea31012005-04-17 16:05:31 -050010773
James Smart07951072007-04-25 09:51:38 -040010774 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010775 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010776 */
10777 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10778
dea31012005-04-17 16:05:31 -050010779 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010780 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10781 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010782 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010783 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010784 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10785 }
James Smartda0436e2009-05-22 14:51:39 -040010786 else
10787 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010788 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010789 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010790
James Smart5ffc2662009-11-18 15:39:44 -050010791 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010792 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010793 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10794 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010795 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10796 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010797
James Smart2e0fef82007-06-17 19:56:36 -050010798 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010799 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10800 else
10801 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10802
10803 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010804 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010805
James Smarte8b62012007-08-02 11:10:09 -040010806 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10807 "0339 Abort xri x%x, original iotag x%x, "
10808 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010809 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010810 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010811 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010812
10813 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010814 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10815 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010816 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040010817 /* Note: both hbalock and ring_lock need to be set here */
10818 spin_lock_irqsave(&pring->ring_lock, iflags);
10819 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10820 abtsiocbp, 0);
10821 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10822 } else {
10823 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10824 abtsiocbp, 0);
10825 }
James Smart07951072007-04-25 09:51:38 -040010826
James Smartd7c255b2008-08-24 21:50:00 -040010827 if (retval)
10828 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010829
10830 /*
10831 * Caller to this routine should check for IOCB_ERROR
10832 * and handle it properly. This routine no longer removes
10833 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10834 */
10835 return retval;
10836}
10837
10838/**
10839 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10840 * @phba: Pointer to HBA context object.
10841 * @pring: Pointer to driver SLI ring object.
10842 * @cmdiocb: Pointer to driver command iocb object.
10843 *
10844 * This function issues an abort iocb for the provided command iocb. In case
10845 * of unloading, the abort iocb will not be issued to commands on the ELS
10846 * ring. Instead, the callback function shall be changed to those commands
10847 * so that nothing happens when them finishes. This function is called with
10848 * hbalock held. The function returns 0 when the command iocb is an abort
10849 * request.
10850 **/
10851int
10852lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10853 struct lpfc_iocbq *cmdiocb)
10854{
10855 struct lpfc_vport *vport = cmdiocb->vport;
10856 int retval = IOCB_ERROR;
10857 IOCB_t *icmd = NULL;
10858
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010859 lockdep_assert_held(&phba->hbalock);
10860
James Smart5af5eee2010-10-22 11:06:38 -040010861 /*
10862 * There are certain command types we don't want to abort. And we
10863 * don't want to abort commands that are already in the process of
10864 * being aborted.
10865 */
10866 icmd = &cmdiocb->iocb;
10867 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10868 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10869 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10870 return 0;
10871
Dick Kennedy1234a6d2017-09-29 17:34:29 -070010872 if (!pring) {
10873 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10874 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10875 else
10876 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10877 goto abort_iotag_exit;
10878 }
10879
James Smart5af5eee2010-10-22 11:06:38 -040010880 /*
10881 * If we're unloading, don't abort iocb on the ELS ring, but change
10882 * the callback so that nothing happens when it finishes.
10883 */
10884 if ((vport->load_flag & FC_UNLOADING) &&
10885 (pring->ringno == LPFC_ELS_RING)) {
10886 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10887 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10888 else
10889 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10890 goto abort_iotag_exit;
10891 }
10892
10893 /* Now, we try to issue the abort to the cmdiocb out */
10894 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10895
James Smart07951072007-04-25 09:51:38 -040010896abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010897 /*
10898 * Caller to this routine should check for IOCB_ERROR
10899 * and handle it properly. This routine no longer removes
10900 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010901 */
James Smart2e0fef82007-06-17 19:56:36 -050010902 return retval;
dea31012005-04-17 16:05:31 -050010903}
10904
James Smarte59058c2008-08-24 21:49:00 -040010905/**
James Smart895427b2017-02-12 13:52:30 -080010906 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10907 * @phba: Pointer to HBA context object.
10908 * @pring: Pointer to driver SLI ring object.
10909 * @cmdiocb: Pointer to driver command iocb object.
10910 *
10911 * This function issues an abort iocb for the provided command iocb down to
10912 * the port. Other than the case the outstanding command iocb is an abort
10913 * request, this function issues abort out unconditionally. This function is
10914 * called with hbalock held. The function returns 0 when it fails due to
10915 * memory allocation failure or when the command iocb is an abort request.
10916 **/
10917static int
10918lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10919 struct lpfc_iocbq *cmdiocb)
10920{
10921 struct lpfc_vport *vport = cmdiocb->vport;
10922 struct lpfc_iocbq *abtsiocbp;
10923 union lpfc_wqe *abts_wqe;
10924 int retval;
10925
10926 /*
10927 * There are certain command types we don't want to abort. And we
10928 * don't want to abort commands that are already in the process of
10929 * being aborted.
10930 */
10931 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10932 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10933 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10934 return 0;
10935
10936 /* issue ABTS for this io based on iotag */
10937 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10938 if (abtsiocbp == NULL)
10939 return 0;
10940
10941 /* This signals the response to set the correct status
10942 * before calling the completion handler
10943 */
10944 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10945
10946 /* Complete prepping the abort wqe and issue to the FW. */
10947 abts_wqe = &abtsiocbp->wqe;
10948 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10949 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10950
10951 /* Explicitly set reserved fields to zero.*/
10952 abts_wqe->abort_cmd.rsrvd4 = 0;
10953 abts_wqe->abort_cmd.rsrvd5 = 0;
10954
10955 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10956 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10957 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10958
10959 /* word 7 */
10960 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10961 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10962 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10963 cmdiocb->iocb.ulpClass);
10964
10965 /* word 8 - tell the FW to abort the IO associated with this
10966 * outstanding exchange ID.
10967 */
10968 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10969
10970 /* word 9 - this is the iotag for the abts_wqe completion. */
10971 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10972 abtsiocbp->iotag);
10973
10974 /* word 10 */
10975 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10976 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10977 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10978
10979 /* word 11 */
10980 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10981 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10982 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10983
10984 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10985 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10986 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080010987 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart895427b2017-02-12 13:52:30 -080010988 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
Dick Kennedycd22d602017-08-23 16:55:35 -070010989 if (retval) {
James Smart895427b2017-02-12 13:52:30 -080010990 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10991 "6147 Failed abts issue_wqe with status x%x "
10992 "for oxid x%x\n",
10993 retval, cmdiocb->sli4_xritag);
10994 lpfc_sli_release_iocbq(phba, abtsiocbp);
10995 return retval;
10996 }
10997
10998 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10999 "6148 Drv Abort NVME Request Issued for "
11000 "ox_id x%x on reqtag x%x\n",
11001 cmdiocb->sli4_xritag,
11002 abtsiocbp->iotag);
11003
11004 return retval;
11005}
11006
11007/**
James Smart5af5eee2010-10-22 11:06:38 -040011008 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11009 * @phba: pointer to lpfc HBA data structure.
11010 *
11011 * This routine will abort all pending and outstanding iocbs to an HBA.
11012 **/
11013void
11014lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11015{
11016 struct lpfc_sli *psli = &phba->sli;
11017 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080011018 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011019 int i;
11020
James Smart895427b2017-02-12 13:52:30 -080011021 if (phba->sli_rev != LPFC_SLI_REV4) {
11022 for (i = 0; i < psli->num_rings; i++) {
11023 pring = &psli->sli3_ring[i];
11024 lpfc_sli_abort_iocb_ring(phba, pring);
11025 }
11026 return;
11027 }
11028 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11029 pring = qp->pring;
11030 if (!pring)
11031 continue;
James Smartdb55fba2014-04-04 13:52:02 -040011032 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040011033 }
11034}
11035
11036/**
James Smart3621a712009-04-06 18:47:14 -040011037 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040011038 * @iocbq: Pointer to driver iocb object.
11039 * @vport: Pointer to driver virtual port object.
11040 * @tgt_id: SCSI ID of the target.
11041 * @lun_id: LUN ID of the scsi device.
11042 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11043 *
James Smart3621a712009-04-06 18:47:14 -040011044 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040011045 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11046 * 0 if the filtering criteria is met for the given iocb and will return
11047 * 1 if the filtering criteria is not met.
11048 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11049 * given iocb is for the SCSI device specified by vport, tgt_id and
11050 * lun_id parameter.
11051 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11052 * given iocb is for the SCSI target specified by vport and tgt_id
11053 * parameters.
11054 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11055 * given iocb is for the SCSI host associated with the given vport.
11056 * This function is called with no locks held.
11057 **/
dea31012005-04-17 16:05:31 -050011058static int
James Smart51ef4c22007-08-02 11:10:31 -040011059lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11060 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011061 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011062{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011063 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011064 int rc = 1;
11065
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011066 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
11067 return rc;
11068
James Smart51ef4c22007-08-02 11:10:31 -040011069 if (iocbq->vport != vport)
11070 return rc;
11071
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011072 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011073
James Smart495a7142008-06-14 22:52:59 -040011074 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050011075 return rc;
11076
11077 switch (ctx_cmd) {
11078 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040011079 if ((lpfc_cmd->rdata->pnode) &&
11080 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11081 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050011082 rc = 0;
11083 break;
11084 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040011085 if ((lpfc_cmd->rdata->pnode) &&
11086 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050011087 rc = 0;
11088 break;
dea31012005-04-17 16:05:31 -050011089 case LPFC_CTX_HOST:
11090 rc = 0;
11091 break;
11092 default:
11093 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070011094 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050011095 break;
11096 }
11097
11098 return rc;
11099}
11100
James Smarte59058c2008-08-24 21:49:00 -040011101/**
James Smart3621a712009-04-06 18:47:14 -040011102 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040011103 * @vport: Pointer to virtual port.
11104 * @tgt_id: SCSI ID of the target.
11105 * @lun_id: LUN ID of the scsi device.
11106 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11107 *
11108 * This function returns number of FCP commands pending for the vport.
11109 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11110 * commands pending on the vport associated with SCSI device specified
11111 * by tgt_id and lun_id parameters.
11112 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11113 * commands pending on the vport associated with SCSI target specified
11114 * by tgt_id parameter.
11115 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11116 * commands pending on the vport.
11117 * This function returns the number of iocbs which satisfy the filter.
11118 * This function is called without any lock held.
11119 **/
dea31012005-04-17 16:05:31 -050011120int
James Smart51ef4c22007-08-02 11:10:31 -040011121lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11122 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011123{
James Smart51ef4c22007-08-02 11:10:31 -040011124 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011125 struct lpfc_iocbq *iocbq;
11126 int sum, i;
dea31012005-04-17 16:05:31 -050011127
Johannes Thumshirn31979002016-07-18 16:06:03 +020011128 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011129 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11130 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011131
James Smart51ef4c22007-08-02 11:10:31 -040011132 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11133 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011134 sum++;
dea31012005-04-17 16:05:31 -050011135 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020011136 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011137
dea31012005-04-17 16:05:31 -050011138 return sum;
11139}
11140
James Smarte59058c2008-08-24 21:49:00 -040011141/**
James Smart3621a712009-04-06 18:47:14 -040011142 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040011143 * @phba: Pointer to HBA context object
11144 * @cmdiocb: Pointer to command iocb object.
11145 * @rspiocb: Pointer to response iocb object.
11146 *
11147 * This function is called when an aborted FCP iocb completes. This
11148 * function is called by the ring event handler with no lock held.
11149 * This function frees the iocb.
11150 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011151void
James Smart2e0fef82007-06-17 19:56:36 -050011152lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11153 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011154{
James Smartcb69f7d2011-12-13 13:21:57 -050011155 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040011156 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050011157 "original iotag x%x, abort cmd iotag x%x "
11158 "status 0x%x, reason 0x%x\n",
11159 cmdiocb->iocb.un.acxri.abortContextTag,
11160 cmdiocb->iocb.un.acxri.abortIoTag,
11161 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11162 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050011163 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011164 return;
11165}
11166
James Smarte59058c2008-08-24 21:49:00 -040011167/**
James Smart3621a712009-04-06 18:47:14 -040011168 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040011169 * @vport: Pointer to virtual port.
11170 * @pring: Pointer to driver SLI ring object.
11171 * @tgt_id: SCSI ID of the target.
11172 * @lun_id: LUN ID of the scsi device.
11173 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11174 *
11175 * This function sends an abort command for every SCSI command
11176 * associated with the given virtual port pending on the ring
11177 * filtered by lpfc_sli_validate_fcp_iocb function.
11178 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11179 * FCP iocbs associated with lun specified by tgt_id and lun_id
11180 * parameters
11181 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11182 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11183 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11184 * FCP iocbs associated with virtual port.
11185 * This function returns number of iocbs it failed to abort.
11186 * This function is called with no locks held.
11187 **/
dea31012005-04-17 16:05:31 -050011188int
James Smart51ef4c22007-08-02 11:10:31 -040011189lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11190 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050011191{
James Smart51ef4c22007-08-02 11:10:31 -040011192 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011193 struct lpfc_iocbq *iocbq;
11194 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070011195 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050011196 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050011197 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011198 int i;
dea31012005-04-17 16:05:31 -050011199
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011200 for (i = 1; i <= phba->sli.last_iotag; i++) {
11201 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011202
James Smart51ef4c22007-08-02 11:10:31 -040011203 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011204 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011205 continue;
11206
James Smartafbd8d82013-09-06 12:22:13 -040011207 /*
11208 * If the iocbq is already being aborted, don't take a second
11209 * action, but do count it.
11210 */
11211 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11212 continue;
11213
dea31012005-04-17 16:05:31 -050011214 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011215 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011216 if (abtsiocb == NULL) {
11217 errcnt++;
11218 continue;
11219 }
dea31012005-04-17 16:05:31 -050011220
James Smartafbd8d82013-09-06 12:22:13 -040011221 /* indicate the IO is being aborted by the driver. */
11222 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11223
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011224 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011225 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11226 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011227 if (phba->sli_rev == LPFC_SLI_REV4)
11228 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11229 else
11230 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011231 abtsiocb->iocb.ulpLe = 1;
11232 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011233 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011234
James Smart5ffc2662009-11-18 15:39:44 -050011235 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011236 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011237 if (iocbq->iocb_flag & LPFC_IO_FCP)
11238 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011239 if (iocbq->iocb_flag & LPFC_IO_FOF)
11240 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011241
James Smart2e0fef82007-06-17 19:56:36 -050011242 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011243 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11244 else
11245 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11246
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011247 /* Setup callback routine and issue the command. */
11248 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011249 if (phba->sli_rev == LPFC_SLI_REV4) {
11250 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11251 if (!pring_s4)
11252 continue;
11253 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11254 abtsiocb, 0);
11255 } else
11256 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11257 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011258 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011259 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011260 errcnt++;
11261 continue;
11262 }
11263 }
11264
11265 return errcnt;
11266}
11267
James Smarte59058c2008-08-24 21:49:00 -040011268/**
James Smart98912dda2014-04-04 13:52:31 -040011269 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11270 * @vport: Pointer to virtual port.
11271 * @pring: Pointer to driver SLI ring object.
11272 * @tgt_id: SCSI ID of the target.
11273 * @lun_id: LUN ID of the scsi device.
11274 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11275 *
11276 * This function sends an abort command for every SCSI command
11277 * associated with the given virtual port pending on the ring
11278 * filtered by lpfc_sli_validate_fcp_iocb function.
11279 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11280 * FCP iocbs associated with lun specified by tgt_id and lun_id
11281 * parameters
11282 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11283 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11284 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11285 * FCP iocbs associated with virtual port.
11286 * This function returns number of iocbs it aborted .
11287 * This function is called with no locks held right after a taskmgmt
11288 * command is sent.
11289 **/
11290int
11291lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11292 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11293{
11294 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040011295 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011296 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011297 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011298 struct lpfc_iocbq *iocbq;
11299 IOCB_t *icmd;
11300 int sum, i, ret_val;
11301 unsigned long iflags;
11302 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040011303
11304 spin_lock_irq(&phba->hbalock);
11305
11306 /* all I/Os are in process of being flushed */
11307 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11308 spin_unlock_irq(&phba->hbalock);
11309 return 0;
11310 }
11311 sum = 0;
11312
11313 for (i = 1; i <= phba->sli.last_iotag; i++) {
11314 iocbq = phba->sli.iocbq_lookup[i];
11315
11316 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11317 cmd) != 0)
11318 continue;
11319
11320 /*
11321 * If the iocbq is already being aborted, don't take a second
11322 * action, but do count it.
11323 */
11324 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11325 continue;
11326
11327 /* issue ABTS for this IOCB based on iotag */
11328 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11329 if (abtsiocbq == NULL)
11330 continue;
11331
11332 icmd = &iocbq->iocb;
11333 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11334 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11335 if (phba->sli_rev == LPFC_SLI_REV4)
11336 abtsiocbq->iocb.un.acxri.abortIoTag =
11337 iocbq->sli4_xritag;
11338 else
11339 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11340 abtsiocbq->iocb.ulpLe = 1;
11341 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11342 abtsiocbq->vport = vport;
11343
11344 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011345 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011346 if (iocbq->iocb_flag & LPFC_IO_FCP)
11347 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011348 if (iocbq->iocb_flag & LPFC_IO_FOF)
11349 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011350
James Smart8c50d252014-09-03 12:58:16 -040011351 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11352 ndlp = lpfc_cmd->rdata->pnode;
11353
11354 if (lpfc_is_link_up(phba) &&
11355 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011356 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11357 else
11358 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11359
11360 /* Setup callback routine and issue the command. */
11361 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11362
11363 /*
11364 * Indicate the IO is being aborted by the driver and set
11365 * the caller's flag into the aborted IO.
11366 */
11367 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11368
11369 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011370 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11371 if (pring_s4 == NULL)
11372 continue;
James Smart98912dda2014-04-04 13:52:31 -040011373 /* Note: both hbalock and ring_lock must be set here */
11374 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11375 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11376 abtsiocbq, 0);
11377 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11378 } else {
11379 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11380 abtsiocbq, 0);
11381 }
11382
11383
11384 if (ret_val == IOCB_ERROR)
11385 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11386 else
11387 sum++;
11388 }
11389 spin_unlock_irq(&phba->hbalock);
11390 return sum;
11391}
11392
11393/**
James Smart3621a712009-04-06 18:47:14 -040011394 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011395 * @phba: Pointer to HBA context object.
11396 * @cmdiocbq: Pointer to command iocb.
11397 * @rspiocbq: Pointer to response iocb.
11398 *
11399 * This function is the completion handler for iocbs issued using
11400 * lpfc_sli_issue_iocb_wait function. This function is called by the
11401 * ring event handler function without any lock held. This function
11402 * can be called from both worker thread context and interrupt
11403 * context. This function also can be called from other thread which
11404 * cleans up the SLI layer objects.
11405 * This function copy the contents of the response iocb to the
11406 * response iocb memory object provided by the caller of
11407 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11408 * sleeps for the iocb completion.
11409 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011410static void
11411lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11412 struct lpfc_iocbq *cmdiocbq,
11413 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011414{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011415 wait_queue_head_t *pdone_q;
11416 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050011417 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011418
James Smart2e0fef82007-06-17 19:56:36 -050011419 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011420 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11421
11422 /*
11423 * A time out has occurred for the iocb. If a time out
11424 * completion handler has been supplied, call it. Otherwise,
11425 * just free the iocbq.
11426 */
11427
11428 spin_unlock_irqrestore(&phba->hbalock, iflags);
11429 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11430 cmdiocbq->wait_iocb_cmpl = NULL;
11431 if (cmdiocbq->iocb_cmpl)
11432 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11433 else
11434 lpfc_sli_release_iocbq(phba, cmdiocbq);
11435 return;
11436 }
11437
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011438 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11439 if (cmdiocbq->context2 && rspiocbq)
11440 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11441 &rspiocbq->iocb, sizeof(IOCB_t));
11442
James Smart0f65ff62010-02-26 14:14:23 -050011443 /* Set the exchange busy flag for task management commands */
11444 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11445 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11446 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11447 cur_iocbq);
11448 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11449 }
11450
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011451 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011452 if (pdone_q)
11453 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011454 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011455 return;
11456}
11457
James Smarte59058c2008-08-24 21:49:00 -040011458/**
James Smartd11e31d2009-06-10 17:23:06 -040011459 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11460 * @phba: Pointer to HBA context object..
11461 * @piocbq: Pointer to command iocb.
11462 * @flag: Flag to test.
11463 *
11464 * This routine grabs the hbalock and then test the iocb_flag to
11465 * see if the passed in flag is set.
11466 * Returns:
11467 * 1 if flag is set.
11468 * 0 if flag is not set.
11469 **/
11470static int
11471lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11472 struct lpfc_iocbq *piocbq, uint32_t flag)
11473{
11474 unsigned long iflags;
11475 int ret;
11476
11477 spin_lock_irqsave(&phba->hbalock, iflags);
11478 ret = piocbq->iocb_flag & flag;
11479 spin_unlock_irqrestore(&phba->hbalock, iflags);
11480 return ret;
11481
11482}
11483
11484/**
James Smart3621a712009-04-06 18:47:14 -040011485 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011486 * @phba: Pointer to HBA context object..
11487 * @pring: Pointer to sli ring.
11488 * @piocb: Pointer to command iocb.
11489 * @prspiocbq: Pointer to response iocb.
11490 * @timeout: Timeout in number of seconds.
11491 *
11492 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011493 * iocb to complete. The iocb_cmpl field of the shall be used
11494 * to handle iocbs which time out. If the field is NULL, the
11495 * function shall free the iocbq structure. If more clean up is
11496 * needed, the caller is expected to provide a completion function
11497 * that will provide the needed clean up. If the iocb command is
11498 * not completed within timeout seconds, the function will either
11499 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11500 * completion function set in the iocb_cmpl field and then return
11501 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11502 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011503 * The function waits for the iocb completion using an
11504 * non-interruptible wait.
11505 * This function will sleep while waiting for iocb completion.
11506 * So, this function should not be called from any context which
11507 * does not allow sleeping. Due to the same reason, this function
11508 * cannot be called with interrupt disabled.
11509 * This function assumes that the iocb completions occur while
11510 * this function sleep. So, this function cannot be called from
11511 * the thread which process iocb completion for this ring.
11512 * This function clears the iocb_flag of the iocb object before
11513 * issuing the iocb and the iocb completion handler sets this
11514 * flag and wakes this thread when the iocb completes.
11515 * The contents of the response iocb will be copied to prspiocbq
11516 * by the completion handler when the command completes.
11517 * This function returns IOCB_SUCCESS when success.
11518 * This function is called with no lock held.
11519 **/
dea31012005-04-17 16:05:31 -050011520int
James Smart2e0fef82007-06-17 19:56:36 -050011521lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011522 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011523 struct lpfc_iocbq *piocb,
11524 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011525 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011526{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011527 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011528 long timeleft, timeout_req = 0;
11529 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011530 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011531 struct lpfc_iocbq *iocb;
11532 int txq_cnt = 0;
11533 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011534 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011535 unsigned long iflags;
11536 bool iocb_completed = true;
11537
James Smart895427b2017-02-12 13:52:30 -080011538 if (phba->sli_rev >= LPFC_SLI_REV4)
11539 pring = lpfc_sli4_calc_ring(phba, piocb);
11540 else
11541 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011542 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011543 * If the caller has provided a response iocbq buffer, then context2
11544 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011545 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011546 if (prspiocbq) {
11547 if (piocb->context2)
11548 return IOCB_ERROR;
11549 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011550 }
11551
James Smart5a0916b2013-07-15 18:31:42 -040011552 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011553 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11554 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011555 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011556
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011557 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011558 if (lpfc_readl(phba->HCregaddr, &creg_val))
11559 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011560 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11561 writel(creg_val, phba->HCregaddr);
11562 readl(phba->HCregaddr); /* flush */
11563 }
11564
James Smart2a9bf3d2010-06-07 15:24:45 -040011565 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11566 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011567 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011568 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011569 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011570 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011571 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011572 spin_lock_irqsave(&phba->hbalock, iflags);
11573 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011574
James Smart5a0916b2013-07-15 18:31:42 -040011575 /*
11576 * IOCB timed out. Inform the wake iocb wait
11577 * completion function and set local status
11578 */
11579
11580 iocb_completed = false;
11581 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11582 }
11583 spin_unlock_irqrestore(&phba->hbalock, iflags);
11584 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011585 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011586 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011587 /* Note: we are not indicating if the IOCB has a success
11588 * status or not - that's for the caller to check.
11589 * IOCB_SUCCESS means just that the command was sent and
11590 * completed. Not that it completed successfully.
11591 * */
James Smart7054a602007-04-25 09:52:34 -040011592 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011593 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011594 "0338 IOCB wait timeout error - no "
11595 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011596 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011597 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011598 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011599 "0330 IOCB wake NOT set, "
11600 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011601 timeout, (timeleft / jiffies));
11602 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011603 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011604 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011605 if (phba->cfg_log_verbose & LOG_SLI) {
11606 list_for_each_entry(iocb, &pring->txq, list) {
11607 txq_cnt++;
11608 }
11609 list_for_each_entry(iocb, &pring->txcmplq, list) {
11610 txcmplq_cnt++;
11611 }
11612 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11613 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11614 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11615 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011616 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011617 } else {
11618 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011619 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011620 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011621 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011622 }
11623
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011624 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011625 if (lpfc_readl(phba->HCregaddr, &creg_val))
11626 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011627 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11628 writel(creg_val, phba->HCregaddr);
11629 readl(phba->HCregaddr); /* flush */
11630 }
11631
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011632 if (prspiocbq)
11633 piocb->context2 = NULL;
11634
11635 piocb->context_un.wait_queue = NULL;
11636 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011637 return retval;
11638}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011639
James Smarte59058c2008-08-24 21:49:00 -040011640/**
James Smart3621a712009-04-06 18:47:14 -040011641 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011642 * @phba: Pointer to HBA context object.
11643 * @pmboxq: Pointer to driver mailbox object.
11644 * @timeout: Timeout in number of seconds.
11645 *
11646 * This function issues the mailbox to firmware and waits for the
11647 * mailbox command to complete. If the mailbox command is not
11648 * completed within timeout seconds, it returns MBX_TIMEOUT.
11649 * The function waits for the mailbox completion using an
11650 * interruptible wait. If the thread is woken up due to a
11651 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11652 * should not free the mailbox resources, if this function returns
11653 * MBX_TIMEOUT.
11654 * This function will sleep while waiting for mailbox completion.
11655 * So, this function should not be called from any context which
11656 * does not allow sleeping. Due to the same reason, this function
11657 * cannot be called with interrupt disabled.
11658 * This function assumes that the mailbox completion occurs while
11659 * this function sleep. So, this function cannot be called from
11660 * the worker thread which processes mailbox completion.
11661 * This function is called in the context of HBA management
11662 * applications.
11663 * This function returns MBX_SUCCESS when successful.
11664 * This function is called with no lock held.
11665 **/
dea31012005-04-17 16:05:31 -050011666int
James Smart2e0fef82007-06-17 19:56:36 -050011667lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011668 uint32_t timeout)
11669{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011670 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040011671 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050011672 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011673 unsigned long flag;
dea31012005-04-17 16:05:31 -050011674
James Smartb230b8a2013-05-31 17:05:27 -040011675 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040011676 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040011677 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050011678
James Smart495a7142008-06-14 22:52:59 -040011679 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011680 /* setup wake call as IOCB callback */
11681 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11682 /* setup context field to pass wait_queue pointer to wake function */
11683 pmboxq->context1 = &done_q;
11684
dea31012005-04-17 16:05:31 -050011685 /* now issue the command */
11686 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011687 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040011688 wait_event_interruptible_timeout(done_q,
11689 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040011690 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011691
James Smart858c9f62007-06-17 19:56:39 -050011692 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011693 /* restore the possible extended buffer for free resource */
11694 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040011695 /*
11696 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11697 * else do not free the resources.
11698 */
James Smartd7c47992010-06-08 18:31:54 -040011699 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011700 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011701 } else {
James Smart7054a602007-04-25 09:52:34 -040011702 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011703 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11704 }
11705 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011706 } else {
11707 /* restore the possible extended buffer for free resource */
11708 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050011709 }
11710
dea31012005-04-17 16:05:31 -050011711 return retval;
11712}
11713
James Smarte59058c2008-08-24 21:49:00 -040011714/**
James Smart3772a992009-05-22 14:50:54 -040011715 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011716 * @phba: Pointer to HBA context.
11717 *
James Smart3772a992009-05-22 14:50:54 -040011718 * This function is called to shutdown the driver's mailbox sub-system.
11719 * It first marks the mailbox sub-system is in a block state to prevent
11720 * the asynchronous mailbox command from issued off the pending mailbox
11721 * command queue. If the mailbox command sub-system shutdown is due to
11722 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11723 * the mailbox sub-system flush routine to forcefully bring down the
11724 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11725 * as with offline or HBA function reset), this routine will wait for the
11726 * outstanding mailbox command to complete before invoking the mailbox
11727 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040011728 **/
James Smart3772a992009-05-22 14:50:54 -040011729void
James Smart618a5232012-06-12 13:54:36 -040011730lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040011731{
James Smart3772a992009-05-22 14:50:54 -040011732 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040011733 unsigned long timeout;
11734
James Smart618a5232012-06-12 13:54:36 -040011735 if (mbx_action == LPFC_MBX_NO_WAIT) {
11736 /* delay 100ms for port state */
11737 msleep(100);
11738 lpfc_sli_mbox_sys_flush(phba);
11739 return;
11740 }
James Smarta183a152011-10-10 21:32:43 -040011741 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050011742
James Smart3772a992009-05-22 14:50:54 -040011743 spin_lock_irq(&phba->hbalock);
11744 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040011745
11746 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040011747 /* Determine how long we might wait for the active mailbox
11748 * command to be gracefully completed by firmware.
11749 */
James Smarta183a152011-10-10 21:32:43 -040011750 if (phba->sli.mbox_active)
11751 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11752 phba->sli.mbox_active) *
11753 1000) + jiffies;
11754 spin_unlock_irq(&phba->hbalock);
11755
James Smart3772a992009-05-22 14:50:54 -040011756 while (phba->sli.mbox_active) {
11757 /* Check active mailbox complete status every 2ms */
11758 msleep(2);
11759 if (time_after(jiffies, timeout))
11760 /* Timeout, let the mailbox flush routine to
11761 * forcefully release active mailbox command
11762 */
11763 break;
11764 }
James Smartd7069f02012-03-01 22:36:29 -050011765 } else
11766 spin_unlock_irq(&phba->hbalock);
11767
James Smart3772a992009-05-22 14:50:54 -040011768 lpfc_sli_mbox_sys_flush(phba);
11769}
11770
11771/**
11772 * lpfc_sli_eratt_read - read sli-3 error attention events
11773 * @phba: Pointer to HBA context.
11774 *
11775 * This function is called to read the SLI3 device error attention registers
11776 * for possible error attention events. The caller must hold the hostlock
11777 * with spin_lock_irq().
11778 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011779 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040011780 * Register and returns 0 otherwise.
11781 **/
11782static int
11783lpfc_sli_eratt_read(struct lpfc_hba *phba)
11784{
James Smarted957682007-06-17 19:56:37 -050011785 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040011786
James Smart3772a992009-05-22 14:50:54 -040011787 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050011788 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11789 goto unplug_err;
11790
James Smart3772a992009-05-22 14:50:54 -040011791 if (ha_copy & HA_ERATT) {
11792 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050011793 if (lpfc_sli_read_hs(phba))
11794 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040011795
James Smart3772a992009-05-22 14:50:54 -040011796 /* Check if there is a deferred error condition is active */
11797 if ((HS_FFER1 & phba->work_hs) &&
11798 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011799 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040011800 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040011801 /* Clear all interrupt enable conditions */
11802 writel(0, phba->HCregaddr);
11803 readl(phba->HCregaddr);
11804 }
11805
11806 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040011807 phba->work_ha |= HA_ERATT;
11808 /* Indicate polling handles this ERATT */
11809 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011810 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011811 }
James Smart3772a992009-05-22 14:50:54 -040011812 return 0;
James Smart9940b972011-03-11 16:06:12 -050011813
11814unplug_err:
11815 /* Set the driver HS work bitmap */
11816 phba->work_hs |= UNPLUG_ERR;
11817 /* Set the driver HA work bitmap */
11818 phba->work_ha |= HA_ERATT;
11819 /* Indicate polling handles this ERATT */
11820 phba->hba_flag |= HBA_ERATT_HANDLED;
11821 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011822}
11823
James Smarte59058c2008-08-24 21:49:00 -040011824/**
James Smartda0436e2009-05-22 14:51:39 -040011825 * lpfc_sli4_eratt_read - read sli-4 error attention events
11826 * @phba: Pointer to HBA context.
11827 *
11828 * This function is called to read the SLI4 device error attention registers
11829 * for possible error attention events. The caller must hold the hostlock
11830 * with spin_lock_irq().
11831 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011832 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040011833 * Register and returns 0 otherwise.
11834 **/
11835static int
11836lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11837{
11838 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050011839 uint32_t if_type, portsmphr;
11840 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040011841
James Smart2fcee4b2010-12-15 17:57:46 -050011842 /*
11843 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040011844 * registers for error attention. This can be changed later.
11845 */
James Smart2fcee4b2010-12-15 17:57:46 -050011846 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11847 switch (if_type) {
11848 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050011849 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11850 &uerr_sta_lo) ||
11851 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11852 &uerr_sta_hi)) {
11853 phba->work_hs |= UNPLUG_ERR;
11854 phba->work_ha |= HA_ERATT;
11855 phba->hba_flag |= HBA_ERATT_HANDLED;
11856 return 1;
11857 }
James Smart2fcee4b2010-12-15 17:57:46 -050011858 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11859 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11860 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11861 "1423 HBA Unrecoverable error: "
11862 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11863 "ue_mask_lo_reg=0x%x, "
11864 "ue_mask_hi_reg=0x%x\n",
11865 uerr_sta_lo, uerr_sta_hi,
11866 phba->sli4_hba.ue_mask_lo,
11867 phba->sli4_hba.ue_mask_hi);
11868 phba->work_status[0] = uerr_sta_lo;
11869 phba->work_status[1] = uerr_sta_hi;
11870 phba->work_ha |= HA_ERATT;
11871 phba->hba_flag |= HBA_ERATT_HANDLED;
11872 return 1;
11873 }
11874 break;
11875 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080011876 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050011877 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11878 &portstat_reg.word0) ||
11879 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11880 &portsmphr)){
11881 phba->work_hs |= UNPLUG_ERR;
11882 phba->work_ha |= HA_ERATT;
11883 phba->hba_flag |= HBA_ERATT_HANDLED;
11884 return 1;
11885 }
James Smart2fcee4b2010-12-15 17:57:46 -050011886 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11887 phba->work_status[0] =
11888 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11889 phba->work_status[1] =
11890 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11891 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011892 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011893 "port status reg 0x%x, "
11894 "port smphr reg 0x%x, "
11895 "error 1=0x%x, error 2=0x%x\n",
11896 portstat_reg.word0,
11897 portsmphr,
11898 phba->work_status[0],
11899 phba->work_status[1]);
11900 phba->work_ha |= HA_ERATT;
11901 phba->hba_flag |= HBA_ERATT_HANDLED;
11902 return 1;
11903 }
11904 break;
11905 case LPFC_SLI_INTF_IF_TYPE_1:
11906 default:
James Smarta747c9c2009-11-18 15:41:10 -050011907 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011908 "2886 HBA Error Attention on unsupported "
11909 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011910 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011911 }
James Smart2fcee4b2010-12-15 17:57:46 -050011912
James Smartda0436e2009-05-22 14:51:39 -040011913 return 0;
11914}
11915
11916/**
James Smart3621a712009-04-06 18:47:14 -040011917 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011918 * @phba: Pointer to HBA context.
11919 *
James Smart3772a992009-05-22 14:50:54 -040011920 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011921 * error attention register bit for error attention events.
11922 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011923 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011924 * Register and returns 0 otherwise.
11925 **/
11926int
11927lpfc_sli_check_eratt(struct lpfc_hba *phba)
11928{
11929 uint32_t ha_copy;
11930
11931 /* If somebody is waiting to handle an eratt, don't process it
11932 * here. The brdkill function will do this.
11933 */
11934 if (phba->link_flag & LS_IGNORE_ERATT)
11935 return 0;
11936
11937 /* Check if interrupt handler handles this ERATT */
11938 spin_lock_irq(&phba->hbalock);
11939 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11940 /* Interrupt handler has handled ERATT */
11941 spin_unlock_irq(&phba->hbalock);
11942 return 0;
11943 }
11944
James Smarta257bf92009-04-06 18:48:10 -040011945 /*
11946 * If there is deferred error attention, do not check for error
11947 * attention
11948 */
11949 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11950 spin_unlock_irq(&phba->hbalock);
11951 return 0;
11952 }
11953
James Smart3772a992009-05-22 14:50:54 -040011954 /* If PCI channel is offline, don't process it */
11955 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011956 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011957 return 0;
11958 }
11959
11960 switch (phba->sli_rev) {
11961 case LPFC_SLI_REV2:
11962 case LPFC_SLI_REV3:
11963 /* Read chip Host Attention (HA) register */
11964 ha_copy = lpfc_sli_eratt_read(phba);
11965 break;
James Smartda0436e2009-05-22 14:51:39 -040011966 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011967 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011968 ha_copy = lpfc_sli4_eratt_read(phba);
11969 break;
James Smart3772a992009-05-22 14:50:54 -040011970 default:
11971 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11972 "0299 Invalid SLI revision (%d)\n",
11973 phba->sli_rev);
11974 ha_copy = 0;
11975 break;
James Smart93996272008-08-24 21:50:30 -040011976 }
11977 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011978
11979 return ha_copy;
11980}
11981
11982/**
11983 * lpfc_intr_state_check - Check device state for interrupt handling
11984 * @phba: Pointer to HBA context.
11985 *
11986 * This inline routine checks whether a device or its PCI slot is in a state
11987 * that the interrupt should be handled.
11988 *
11989 * This function returns 0 if the device or the PCI slot is in a state that
11990 * interrupt should be handled, otherwise -EIO.
11991 */
11992static inline int
11993lpfc_intr_state_check(struct lpfc_hba *phba)
11994{
11995 /* If the pci channel is offline, ignore all the interrupts */
11996 if (unlikely(pci_channel_offline(phba->pcidev)))
11997 return -EIO;
11998
11999 /* Update device level interrupt statistics */
12000 phba->sli.slistat.sli_intr++;
12001
12002 /* Ignore all interrupts during initialization. */
12003 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12004 return -EIO;
12005
James Smart93996272008-08-24 21:50:30 -040012006 return 0;
12007}
12008
12009/**
James Smart3772a992009-05-22 14:50:54 -040012010 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040012011 * @irq: Interrupt number.
12012 * @dev_id: The device context pointer.
12013 *
James Smart93996272008-08-24 21:50:30 -040012014 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012015 * service routine when device with SLI-3 interface spec is enabled with
12016 * MSI-X multi-message interrupt mode and there are slow-path events in
12017 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12018 * interrupt mode, this function is called as part of the device-level
12019 * interrupt handler. When the PCI slot is in error recovery or the HBA
12020 * is undergoing initialization, the interrupt handler will not process
12021 * the interrupt. The link attention and ELS ring attention events are
12022 * handled by the worker thread. The interrupt handler signals the worker
12023 * thread and returns for these events. This function is called without
12024 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040012025 * structures.
12026 *
12027 * This function returns IRQ_HANDLED when interrupt is handled else it
12028 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040012029 **/
dea31012005-04-17 16:05:31 -050012030irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012031lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050012032{
James Smart2e0fef82007-06-17 19:56:36 -050012033 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050012034 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050012035 uint32_t work_ha_copy;
12036 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012037 unsigned long iflag;
dea31012005-04-17 16:05:31 -050012038 uint32_t control;
12039
James Smart92d7f7b2007-06-17 19:56:38 -050012040 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050012041 struct lpfc_vport *vport;
12042 struct lpfc_nodelist *ndlp;
12043 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050012044 LPFC_MBOXQ_t *pmb;
12045 int rc;
12046
dea31012005-04-17 16:05:31 -050012047 /*
12048 * Get the driver's phba structure from the dev_id and
12049 * assume the HBA is not interrupting.
12050 */
James Smart93996272008-08-24 21:50:30 -040012051 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050012052
12053 if (unlikely(!phba))
12054 return IRQ_NONE;
12055
dea31012005-04-17 16:05:31 -050012056 /*
James Smart93996272008-08-24 21:50:30 -040012057 * Stuff needs to be attented to when this function is invoked as an
12058 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012059 */
James Smart93996272008-08-24 21:50:30 -040012060 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012061 /* Check device state for handling interrupt */
12062 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012063 return IRQ_NONE;
12064 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050012065 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012066 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12067 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040012068 /* If somebody is waiting to handle an eratt don't process it
12069 * here. The brdkill function will do this.
12070 */
12071 if (phba->link_flag & LS_IGNORE_ERATT)
12072 ha_copy &= ~HA_ERATT;
12073 /* Check the need for handling ERATT in interrupt handler */
12074 if (ha_copy & HA_ERATT) {
12075 if (phba->hba_flag & HBA_ERATT_HANDLED)
12076 /* ERATT polling has handled ERATT */
12077 ha_copy &= ~HA_ERATT;
12078 else
12079 /* Indicate interrupt handler handles ERATT */
12080 phba->hba_flag |= HBA_ERATT_HANDLED;
12081 }
James Smarta257bf92009-04-06 18:48:10 -040012082
12083 /*
12084 * If there is deferred error attention, do not check for any
12085 * interrupt.
12086 */
12087 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012088 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012089 return IRQ_NONE;
12090 }
12091
James Smart93996272008-08-24 21:50:30 -040012092 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050012093 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12094 goto unplug_error;
12095
James Smarta747c9c2009-11-18 15:41:10 -050012096 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12097 HC_LAINT_ENA | HC_ERINT_ENA),
12098 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012099 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12100 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012101 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012102 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012103 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012104 } else
12105 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050012106
dea31012005-04-17 16:05:31 -050012107 work_ha_copy = ha_copy & phba->work_ha_mask;
12108
James Smart93996272008-08-24 21:50:30 -040012109 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050012110 if (work_ha_copy & HA_LATT) {
12111 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12112 /*
12113 * Turn off Link Attention interrupts
12114 * until CLEAR_LA done
12115 */
James Smart5b75da22008-12-04 22:39:35 -050012116 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012117 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050012118 if (lpfc_readl(phba->HCregaddr, &control))
12119 goto unplug_error;
dea31012005-04-17 16:05:31 -050012120 control &= ~HC_LAINT_ENA;
12121 writel(control, phba->HCregaddr);
12122 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012123 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012124 }
12125 else
12126 work_ha_copy &= ~HA_LATT;
12127 }
12128
James Smart93996272008-08-24 21:50:30 -040012129 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050012130 /*
12131 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12132 * the only slow ring.
12133 */
12134 status = (work_ha_copy &
12135 (HA_RXMASK << (4*LPFC_ELS_RING)));
12136 status >>= (4*LPFC_ELS_RING);
12137 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050012138 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012139 if (lpfc_readl(phba->HCregaddr, &control))
12140 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040012141
12142 lpfc_debugfs_slow_ring_trc(phba,
12143 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12144 control, status,
12145 (uint32_t)phba->sli.slistat.sli_intr);
12146
James Smart858c9f62007-06-17 19:56:39 -050012147 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040012148 lpfc_debugfs_slow_ring_trc(phba,
12149 "ISR Disable ring:"
12150 "pwork:x%x hawork:x%x wait:x%x",
12151 phba->work_ha, work_ha_copy,
12152 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012153 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012154
James Smart858c9f62007-06-17 19:56:39 -050012155 control &=
12156 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050012157 writel(control, phba->HCregaddr);
12158 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050012159 }
James Smarta58cbd52007-08-02 11:09:43 -040012160 else {
12161 lpfc_debugfs_slow_ring_trc(phba,
12162 "ISR slow ring: pwork:"
12163 "x%x hawork:x%x wait:x%x",
12164 phba->work_ha, work_ha_copy,
12165 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012166 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012167 }
James Smart5b75da22008-12-04 22:39:35 -050012168 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012169 }
12170 }
James Smart5b75da22008-12-04 22:39:35 -050012171 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012172 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050012173 if (lpfc_sli_read_hs(phba))
12174 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040012175 /*
12176 * Check if there is a deferred error condition
12177 * is active
12178 */
12179 if ((HS_FFER1 & phba->work_hs) &&
12180 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012181 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12182 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040012183 phba->hba_flag |= DEFER_ERATT;
12184 /* Clear all interrupt enable conditions */
12185 writel(0, phba->HCregaddr);
12186 readl(phba->HCregaddr);
12187 }
12188 }
12189
James Smart93996272008-08-24 21:50:30 -040012190 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050012191 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040012192 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040012193 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050012194 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050012195
12196 /* First check out the status word */
12197 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12198 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050012199 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050012200 /*
12201 * Stray Mailbox Interrupt, mbxCommand <cmd>
12202 * mbxStatus <status>
12203 */
James Smart09372822008-01-11 01:52:54 -050012204 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012205 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012206 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012207 "Interrupt mbxCommand x%x "
12208 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012209 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012210 pmbox->mbxCommand,
12211 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012212 /* clear mailbox attention bit */
12213 work_ha_copy &= ~HA_MBATT;
12214 } else {
James Smart97eab632008-04-07 10:16:05 -040012215 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012216 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012217 phba->last_completion_time = jiffies;
12218 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012219 if (pmb->mbox_cmpl) {
12220 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12221 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012222 if (pmb->out_ext_byte_len &&
12223 pmb->context2)
12224 lpfc_sli_pcimem_bcopy(
12225 phba->mbox_ext,
12226 pmb->context2,
12227 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012228 }
James Smart09372822008-01-11 01:52:54 -050012229 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12230 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12231
12232 lpfc_debugfs_disc_trc(vport,
12233 LPFC_DISC_TRC_MBOX_VPORT,
12234 "MBOX dflt rpi: : "
12235 "status:x%x rpi:x%x",
12236 (uint32_t)pmbox->mbxStatus,
12237 pmbox->un.varWords[0], 0);
12238
12239 if (!pmbox->mbxStatus) {
12240 mp = (struct lpfc_dmabuf *)
12241 (pmb->context1);
12242 ndlp = (struct lpfc_nodelist *)
12243 pmb->context2;
12244
12245 /* Reg_LOGIN of dflt RPI was
12246 * successful. new lets get
12247 * rid of the RPI using the
12248 * same mbox buffer.
12249 */
12250 lpfc_unreg_login(phba,
12251 vport->vpi,
12252 pmbox->un.varWords[0],
12253 pmb);
12254 pmb->mbox_cmpl =
12255 lpfc_mbx_cmpl_dflt_rpi;
12256 pmb->context1 = mp;
12257 pmb->context2 = ndlp;
12258 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012259 rc = lpfc_sli_issue_mbox(phba,
12260 pmb,
12261 MBX_NOWAIT);
12262 if (rc != MBX_BUSY)
12263 lpfc_printf_log(phba,
12264 KERN_ERR,
12265 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012266 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012267 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012268 if (rc != MBX_NOT_FINISHED)
12269 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012270 }
12271 }
James Smart5b75da22008-12-04 22:39:35 -050012272 spin_lock_irqsave(
12273 &phba->pport->work_port_lock,
12274 iflag);
James Smart09372822008-01-11 01:52:54 -050012275 phba->pport->work_port_events &=
12276 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012277 spin_unlock_irqrestore(
12278 &phba->pport->work_port_lock,
12279 iflag);
James Smart09372822008-01-11 01:52:54 -050012280 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012281 }
James Smart97eab632008-04-07 10:16:05 -040012282 } else
James Smart5b75da22008-12-04 22:39:35 -050012283 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012284
James Smart92d7f7b2007-06-17 19:56:38 -050012285 if ((work_ha_copy & HA_MBATT) &&
12286 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012287send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012288 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012289 do {
12290 rc = lpfc_sli_issue_mbox(phba, NULL,
12291 MBX_NOWAIT);
12292 } while (rc == MBX_NOT_FINISHED);
12293 if (rc != MBX_SUCCESS)
12294 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12295 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012296 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012297 }
12298
James Smart5b75da22008-12-04 22:39:35 -050012299 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012300 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012301 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012302 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012303 }
James Smart93996272008-08-24 21:50:30 -040012304 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012305unplug_error:
12306 spin_unlock_irqrestore(&phba->hbalock, iflag);
12307 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012308
James Smart3772a992009-05-22 14:50:54 -040012309} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012310
12311/**
James Smart3772a992009-05-22 14:50:54 -040012312 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012313 * @irq: Interrupt number.
12314 * @dev_id: The device context pointer.
12315 *
12316 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012317 * service routine when device with SLI-3 interface spec is enabled with
12318 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12319 * ring event in the HBA. However, when the device is enabled with either
12320 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12321 * device-level interrupt handler. When the PCI slot is in error recovery
12322 * or the HBA is undergoing initialization, the interrupt handler will not
12323 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12324 * the intrrupt context. This function is called without any lock held.
12325 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012326 *
12327 * This function returns IRQ_HANDLED when interrupt is handled else it
12328 * returns IRQ_NONE.
12329 **/
12330irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012331lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012332{
12333 struct lpfc_hba *phba;
12334 uint32_t ha_copy;
12335 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012336 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012337 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012338
12339 /* Get the driver's phba structure from the dev_id and
12340 * assume the HBA is not interrupting.
12341 */
12342 phba = (struct lpfc_hba *) dev_id;
12343
12344 if (unlikely(!phba))
12345 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012346
12347 /*
James Smart93996272008-08-24 21:50:30 -040012348 * Stuff needs to be attented to when this function is invoked as an
12349 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012350 */
James Smart93996272008-08-24 21:50:30 -040012351 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012352 /* Check device state for handling interrupt */
12353 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012354 return IRQ_NONE;
12355 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012356 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12357 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012358 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012359 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012360 /*
12361 * If there is deferred error attention, do not check for
12362 * any interrupt.
12363 */
12364 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012365 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012366 return IRQ_NONE;
12367 }
James Smart93996272008-08-24 21:50:30 -040012368 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12369 phba->HAregaddr);
12370 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012371 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012372 } else
12373 ha_copy = phba->ha_copy;
12374
12375 /*
12376 * Process all events on FCP ring. Take the optimized path for FCP IO.
12377 */
12378 ha_copy &= ~(phba->work_ha_mask);
12379
12380 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012381 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012382 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012383 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012384 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012385
12386 if (phba->cfg_multi_ring_support == 2) {
12387 /*
James Smart93996272008-08-24 21:50:30 -040012388 * Process all events on extra ring. Take the optimized path
12389 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012390 */
James Smart93996272008-08-24 21:50:30 -040012391 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012392 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012393 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012394 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012395 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012396 status);
12397 }
12398 }
dea31012005-04-17 16:05:31 -050012399 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012400} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012401
James Smart93996272008-08-24 21:50:30 -040012402/**
James Smart3772a992009-05-22 14:50:54 -040012403 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012404 * @irq: Interrupt number.
12405 * @dev_id: The device context pointer.
12406 *
James Smart3772a992009-05-22 14:50:54 -040012407 * This function is the HBA device-level interrupt handler to device with
12408 * SLI-3 interface spec, called from the PCI layer when either MSI or
12409 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12410 * requires driver attention. This function invokes the slow-path interrupt
12411 * attention handling function and fast-path interrupt attention handling
12412 * function in turn to process the relevant HBA attention events. This
12413 * function is called without any lock held. It gets the hbalock to access
12414 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012415 *
12416 * This function returns IRQ_HANDLED when interrupt is handled, else it
12417 * returns IRQ_NONE.
12418 **/
12419irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012420lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012421{
12422 struct lpfc_hba *phba;
12423 irqreturn_t sp_irq_rc, fp_irq_rc;
12424 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012425 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012426
12427 /*
12428 * Get the driver's phba structure from the dev_id and
12429 * assume the HBA is not interrupting.
12430 */
12431 phba = (struct lpfc_hba *) dev_id;
12432
12433 if (unlikely(!phba))
12434 return IRQ_NONE;
12435
James Smart3772a992009-05-22 14:50:54 -040012436 /* Check device state for handling interrupt */
12437 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012438 return IRQ_NONE;
12439
12440 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012441 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12442 spin_unlock(&phba->hbalock);
12443 return IRQ_HANDLED;
12444 }
12445
James Smart93996272008-08-24 21:50:30 -040012446 if (unlikely(!phba->ha_copy)) {
12447 spin_unlock(&phba->hbalock);
12448 return IRQ_NONE;
12449 } else if (phba->ha_copy & HA_ERATT) {
12450 if (phba->hba_flag & HBA_ERATT_HANDLED)
12451 /* ERATT polling has handled ERATT */
12452 phba->ha_copy &= ~HA_ERATT;
12453 else
12454 /* Indicate interrupt handler handles ERATT */
12455 phba->hba_flag |= HBA_ERATT_HANDLED;
12456 }
12457
James Smarta257bf92009-04-06 18:48:10 -040012458 /*
12459 * If there is deferred error attention, do not check for any interrupt.
12460 */
12461 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012462 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012463 return IRQ_NONE;
12464 }
12465
James Smart93996272008-08-24 21:50:30 -040012466 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012467 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12468 spin_unlock(&phba->hbalock);
12469 return IRQ_HANDLED;
12470 }
James Smarta747c9c2009-11-18 15:41:10 -050012471 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12472 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12473 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012474 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012475 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012476 readl(phba->HAregaddr); /* flush */
12477 spin_unlock(&phba->hbalock);
12478
12479 /*
12480 * Invokes slow-path host attention interrupt handling as appropriate.
12481 */
12482
12483 /* status of events with mailbox and link attention */
12484 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12485
12486 /* status of events with ELS ring */
12487 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12488 status2 >>= (4*LPFC_ELS_RING);
12489
12490 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012491 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012492 else
12493 sp_irq_rc = IRQ_NONE;
12494
12495 /*
12496 * Invoke fast-path host attention interrupt handling as appropriate.
12497 */
12498
12499 /* status of events with FCP ring */
12500 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12501 status1 >>= (4*LPFC_FCP_RING);
12502
12503 /* status of events with extra ring */
12504 if (phba->cfg_multi_ring_support == 2) {
12505 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12506 status2 >>= (4*LPFC_EXTRA_RING);
12507 } else
12508 status2 = 0;
12509
12510 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012511 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012512 else
12513 fp_irq_rc = IRQ_NONE;
12514
12515 /* Return device-level interrupt handling status */
12516 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012517} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012518
12519/**
12520 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12521 * @phba: pointer to lpfc hba data structure.
12522 *
12523 * This routine is invoked by the worker thread to process all the pending
12524 * SLI4 FCP abort XRI events.
12525 **/
12526void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12527{
12528 struct lpfc_cq_event *cq_event;
12529
12530 /* First, declare the fcp xri abort event has been handled */
12531 spin_lock_irq(&phba->hbalock);
12532 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12533 spin_unlock_irq(&phba->hbalock);
12534 /* Now, handle all the fcp xri abort events */
12535 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12536 /* Get the first event from the head of the event queue */
12537 spin_lock_irq(&phba->hbalock);
12538 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12539 cq_event, struct lpfc_cq_event, list);
12540 spin_unlock_irq(&phba->hbalock);
12541 /* Notify aborted XRI for FCP work queue */
12542 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12543 /* Free the event processed back to the free pool */
12544 lpfc_sli4_cq_event_release(phba, cq_event);
12545 }
12546}
12547
12548/**
12549 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12550 * @phba: pointer to lpfc hba data structure.
12551 *
12552 * This routine is invoked by the worker thread to process all the pending
12553 * SLI4 els abort xri events.
12554 **/
12555void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12556{
12557 struct lpfc_cq_event *cq_event;
12558
12559 /* First, declare the els xri abort event has been handled */
12560 spin_lock_irq(&phba->hbalock);
12561 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12562 spin_unlock_irq(&phba->hbalock);
12563 /* Now, handle all the els xri abort events */
12564 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12565 /* Get the first event from the head of the event queue */
12566 spin_lock_irq(&phba->hbalock);
12567 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12568 cq_event, struct lpfc_cq_event, list);
12569 spin_unlock_irq(&phba->hbalock);
12570 /* Notify aborted XRI for ELS work queue */
12571 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12572 /* Free the event processed back to the free pool */
12573 lpfc_sli4_cq_event_release(phba, cq_event);
12574 }
12575}
12576
James Smart341af102010-01-26 23:07:37 -050012577/**
12578 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12579 * @phba: pointer to lpfc hba data structure
12580 * @pIocbIn: pointer to the rspiocbq
12581 * @pIocbOut: pointer to the cmdiocbq
12582 * @wcqe: pointer to the complete wcqe
12583 *
12584 * This routine transfers the fields of a command iocbq to a response iocbq
12585 * by copying all the IOCB fields from command iocbq and transferring the
12586 * completion status information from the complete wcqe.
12587 **/
James Smart4f774512009-05-22 14:52:35 -040012588static void
James Smart341af102010-01-26 23:07:37 -050012589lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12590 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012591 struct lpfc_iocbq *pIocbOut,
12592 struct lpfc_wcqe_complete *wcqe)
12593{
James Smartaf227412013-10-10 12:23:10 -040012594 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012595 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012596 uint32_t status, max_response;
12597 struct lpfc_dmabuf *dmabuf;
12598 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012599 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12600
12601 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12602 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012603 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012604 status = bf_get(lpfc_wcqe_c_status, wcqe);
12605 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012606 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12607 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12608 pIocbIn->iocb.un.fcpi.fcpi_parm =
12609 pIocbOut->iocb.un.fcpi.fcpi_parm -
12610 wcqe->total_data_placed;
12611 else
12612 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012613 else {
James Smart4f774512009-05-22 14:52:35 -040012614 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012615 switch (pIocbOut->iocb.ulpCommand) {
12616 case CMD_ELS_REQUEST64_CR:
12617 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12618 bpl = (struct ulp_bde64 *)dmabuf->virt;
12619 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12620 max_response = bde.tus.f.bdeSize;
12621 break;
12622 case CMD_GEN_REQUEST64_CR:
12623 max_response = 0;
12624 if (!pIocbOut->context3)
12625 break;
12626 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12627 sizeof(struct ulp_bde64);
12628 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12629 bpl = (struct ulp_bde64 *)dmabuf->virt;
12630 for (i = 0; i < numBdes; i++) {
12631 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12632 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12633 max_response += bde.tus.f.bdeSize;
12634 }
12635 break;
12636 default:
12637 max_response = wcqe->total_data_placed;
12638 break;
12639 }
12640 if (max_response < wcqe->total_data_placed)
12641 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12642 else
12643 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12644 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012645 }
James Smart341af102010-01-26 23:07:37 -050012646
James Smartacd68592012-01-18 16:25:09 -050012647 /* Convert BG errors for completion status */
12648 if (status == CQE_STATUS_DI_ERROR) {
12649 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12650
12651 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12652 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12653 else
12654 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12655
12656 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12657 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12658 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12659 BGS_GUARD_ERR_MASK;
12660 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12661 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12662 BGS_APPTAG_ERR_MASK;
12663 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12664 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12665 BGS_REFTAG_ERR_MASK;
12666
12667 /* Check to see if there was any good data before the error */
12668 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12669 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12670 BGS_HI_WATER_MARK_PRESENT_MASK;
12671 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12672 wcqe->total_data_placed;
12673 }
12674
12675 /*
12676 * Set ALL the error bits to indicate we don't know what
12677 * type of error it is.
12678 */
12679 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12680 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12681 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12682 BGS_GUARD_ERR_MASK);
12683 }
12684
James Smart341af102010-01-26 23:07:37 -050012685 /* Pick up HBA exchange busy condition */
12686 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12687 spin_lock_irqsave(&phba->hbalock, iflags);
12688 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12689 spin_unlock_irqrestore(&phba->hbalock, iflags);
12690 }
James Smart4f774512009-05-22 14:52:35 -040012691}
12692
12693/**
James Smart45ed1192009-10-02 15:17:02 -040012694 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12695 * @phba: Pointer to HBA context object.
12696 * @wcqe: Pointer to work-queue completion queue entry.
12697 *
12698 * This routine handles an ELS work-queue completion event and construct
12699 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12700 * discovery engine to handle.
12701 *
12702 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12703 **/
12704static struct lpfc_iocbq *
12705lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12706 struct lpfc_iocbq *irspiocbq)
12707{
James Smart895427b2017-02-12 13:52:30 -080012708 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012709 struct lpfc_iocbq *cmdiocbq;
12710 struct lpfc_wcqe_complete *wcqe;
12711 unsigned long iflags;
12712
James Smart895427b2017-02-12 13:52:30 -080012713 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070012714 if (unlikely(!pring))
12715 return NULL;
James Smart895427b2017-02-12 13:52:30 -080012716
James Smart45ed1192009-10-02 15:17:02 -040012717 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012718 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012719 pring->stats.iocb_event++;
12720 /* Look up the ELS command IOCB and create pseudo response IOCB */
12721 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12722 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040012723 if (unlikely(!cmdiocbq)) {
Dick Kennedy401bb412017-09-29 17:34:28 -070012724 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012725 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12726 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070012727 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12728 wcqe->word0, wcqe->total_data_placed,
12729 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040012730 lpfc_sli_release_iocbq(phba, irspiocbq);
12731 return NULL;
12732 }
12733
Dick Kennedy401bb412017-09-29 17:34:28 -070012734 /* Put the iocb back on the txcmplq */
12735 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12736 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12737
James Smart45ed1192009-10-02 15:17:02 -040012738 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012739 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040012740
12741 return irspiocbq;
12742}
12743
James Smart8a5ca102017-11-20 16:00:30 -080012744inline struct lpfc_cq_event *
12745lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12746{
12747 struct lpfc_cq_event *cq_event;
12748
12749 /* Allocate a new internal CQ_EVENT entry */
12750 cq_event = lpfc_sli4_cq_event_alloc(phba);
12751 if (!cq_event) {
12752 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12753 "0602 Failed to alloc CQ_EVENT entry\n");
12754 return NULL;
12755 }
12756
12757 /* Move the CQE into the event */
12758 memcpy(&cq_event->cqe, entry, size);
12759 return cq_event;
12760}
12761
James Smart45ed1192009-10-02 15:17:02 -040012762/**
James Smart04c68492009-05-22 14:52:52 -040012763 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12764 * @phba: Pointer to HBA context object.
12765 * @cqe: Pointer to mailbox completion queue entry.
12766 *
12767 * This routine process a mailbox completion queue entry with asynchrous
12768 * event.
12769 *
12770 * Return: true if work posted to worker thread, otherwise false.
12771 **/
12772static bool
12773lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12774{
12775 struct lpfc_cq_event *cq_event;
12776 unsigned long iflags;
12777
12778 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12779 "0392 Async Event: word0:x%x, word1:x%x, "
12780 "word2:x%x, word3:x%x\n", mcqe->word0,
12781 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12782
James Smart8a5ca102017-11-20 16:00:30 -080012783 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12784 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040012785 return false;
James Smart04c68492009-05-22 14:52:52 -040012786 spin_lock_irqsave(&phba->hbalock, iflags);
12787 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12788 /* Set the async event flag */
12789 phba->hba_flag |= ASYNC_EVENT;
12790 spin_unlock_irqrestore(&phba->hbalock, iflags);
12791
12792 return true;
12793}
12794
12795/**
12796 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12797 * @phba: Pointer to HBA context object.
12798 * @cqe: Pointer to mailbox completion queue entry.
12799 *
12800 * This routine process a mailbox completion queue entry with mailbox
12801 * completion event.
12802 *
12803 * Return: true if work posted to worker thread, otherwise false.
12804 **/
12805static bool
12806lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12807{
12808 uint32_t mcqe_status;
12809 MAILBOX_t *mbox, *pmbox;
12810 struct lpfc_mqe *mqe;
12811 struct lpfc_vport *vport;
12812 struct lpfc_nodelist *ndlp;
12813 struct lpfc_dmabuf *mp;
12814 unsigned long iflags;
12815 LPFC_MBOXQ_t *pmb;
12816 bool workposted = false;
12817 int rc;
12818
12819 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12820 if (!bf_get(lpfc_trailer_completed, mcqe))
12821 goto out_no_mqe_complete;
12822
12823 /* Get the reference to the active mbox command */
12824 spin_lock_irqsave(&phba->hbalock, iflags);
12825 pmb = phba->sli.mbox_active;
12826 if (unlikely(!pmb)) {
12827 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12828 "1832 No pending MBOX command to handle\n");
12829 spin_unlock_irqrestore(&phba->hbalock, iflags);
12830 goto out_no_mqe_complete;
12831 }
12832 spin_unlock_irqrestore(&phba->hbalock, iflags);
12833 mqe = &pmb->u.mqe;
12834 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12835 mbox = phba->mbox;
12836 vport = pmb->vport;
12837
12838 /* Reset heartbeat timer */
12839 phba->last_completion_time = jiffies;
12840 del_timer(&phba->sli.mbox_tmo);
12841
12842 /* Move mbox data to caller's mailbox region, do endian swapping */
12843 if (pmb->mbox_cmpl && mbox)
12844 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040012845
James Smart73d91e52011-10-10 21:32:10 -040012846 /*
12847 * For mcqe errors, conditionally move a modified error code to
12848 * the mbox so that the error will not be missed.
12849 */
12850 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12851 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12852 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12853 bf_set(lpfc_mqe_status, mqe,
12854 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12855 }
James Smart04c68492009-05-22 14:52:52 -040012856 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12857 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12858 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12859 "MBOX dflt rpi: status:x%x rpi:x%x",
12860 mcqe_status,
12861 pmbox->un.varWords[0], 0);
12862 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12863 mp = (struct lpfc_dmabuf *)(pmb->context1);
12864 ndlp = (struct lpfc_nodelist *)pmb->context2;
12865 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12866 * RID of the PPI using the same mbox buffer.
12867 */
12868 lpfc_unreg_login(phba, vport->vpi,
12869 pmbox->un.varWords[0], pmb);
12870 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12871 pmb->context1 = mp;
12872 pmb->context2 = ndlp;
12873 pmb->vport = vport;
12874 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12875 if (rc != MBX_BUSY)
12876 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12877 LOG_SLI, "0385 rc should "
12878 "have been MBX_BUSY\n");
12879 if (rc != MBX_NOT_FINISHED)
12880 goto send_current_mbox;
12881 }
12882 }
12883 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12884 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12885 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12886
12887 /* There is mailbox completion work to do */
12888 spin_lock_irqsave(&phba->hbalock, iflags);
12889 __lpfc_mbox_cmpl_put(phba, pmb);
12890 phba->work_ha |= HA_MBATT;
12891 spin_unlock_irqrestore(&phba->hbalock, iflags);
12892 workposted = true;
12893
12894send_current_mbox:
12895 spin_lock_irqsave(&phba->hbalock, iflags);
12896 /* Release the mailbox command posting token */
12897 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12898 /* Setting active mailbox pointer need to be in sync to flag clear */
12899 phba->sli.mbox_active = NULL;
12900 spin_unlock_irqrestore(&phba->hbalock, iflags);
12901 /* Wake up worker thread to post the next pending mailbox command */
12902 lpfc_worker_wake_up(phba);
12903out_no_mqe_complete:
12904 if (bf_get(lpfc_trailer_consumed, mcqe))
12905 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12906 return workposted;
12907}
12908
12909/**
12910 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12911 * @phba: Pointer to HBA context object.
12912 * @cqe: Pointer to mailbox completion queue entry.
12913 *
12914 * This routine process a mailbox completion queue entry, it invokes the
12915 * proper mailbox complete handling or asynchrous event handling routine
12916 * according to the MCQE's async bit.
12917 *
12918 * Return: true if work posted to worker thread, otherwise false.
12919 **/
12920static bool
12921lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12922{
12923 struct lpfc_mcqe mcqe;
12924 bool workposted;
12925
12926 /* Copy the mailbox MCQE and convert endian order as needed */
12927 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12928
12929 /* Invoke the proper event handling routine */
12930 if (!bf_get(lpfc_trailer_async, &mcqe))
12931 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12932 else
12933 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12934 return workposted;
12935}
12936
12937/**
James Smart4f774512009-05-22 14:52:35 -040012938 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12939 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012940 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012941 * @wcqe: Pointer to work-queue completion queue entry.
12942 *
12943 * This routine handles an ELS work-queue completion event.
12944 *
12945 * Return: true if work posted to worker thread, otherwise false.
12946 **/
12947static bool
James Smart2a76a282012-08-03 12:35:54 -040012948lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012949 struct lpfc_wcqe_complete *wcqe)
12950{
James Smart4f774512009-05-22 14:52:35 -040012951 struct lpfc_iocbq *irspiocbq;
12952 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012953 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012954 int txq_cnt = 0;
12955 int txcmplq_cnt = 0;
12956 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012957
James Smart45ed1192009-10-02 15:17:02 -040012958 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012959 irspiocbq = lpfc_sli_get_iocbq(phba);
12960 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012961 if (!list_empty(&pring->txq))
12962 txq_cnt++;
12963 if (!list_empty(&pring->txcmplq))
12964 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012965 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012966 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12967 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012968 txq_cnt, phba->iocb_cnt,
12969 fcp_txcmplq_cnt,
12970 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012971 return false;
James Smart4f774512009-05-22 14:52:35 -040012972 }
James Smart4f774512009-05-22 14:52:35 -040012973
James Smart45ed1192009-10-02 15:17:02 -040012974 /* Save off the slow-path queue event for work thread to process */
12975 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012976 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012977 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012978 &phba->sli4_hba.sp_queue_event);
12979 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012980 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012981
James Smart45ed1192009-10-02 15:17:02 -040012982 return true;
James Smart4f774512009-05-22 14:52:35 -040012983}
12984
12985/**
12986 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12987 * @phba: Pointer to HBA context object.
12988 * @wcqe: Pointer to work-queue completion queue entry.
12989 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080012990 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040012991 * proper WQ release routine to the slow-path WQ.
12992 **/
12993static void
12994lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12995 struct lpfc_wcqe_release *wcqe)
12996{
James Smart2e90f4b2011-12-13 13:22:37 -050012997 /* sanity check on queue memory */
12998 if (unlikely(!phba->sli4_hba.els_wq))
12999 return;
James Smart4f774512009-05-22 14:52:35 -040013000 /* Check for the slow-path ELS work queue */
13001 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13002 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13003 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13004 else
13005 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13006 "2579 Slow-path wqe consume event carries "
13007 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13008 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13009 phba->sli4_hba.els_wq->queue_id);
13010}
13011
13012/**
13013 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13014 * @phba: Pointer to HBA context object.
13015 * @cq: Pointer to a WQ completion queue.
13016 * @wcqe: Pointer to work-queue completion queue entry.
13017 *
13018 * This routine handles an XRI abort event.
13019 *
13020 * Return: true if work posted to worker thread, otherwise false.
13021 **/
13022static bool
13023lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13024 struct lpfc_queue *cq,
13025 struct sli4_wcqe_xri_aborted *wcqe)
13026{
13027 bool workposted = false;
13028 struct lpfc_cq_event *cq_event;
13029 unsigned long iflags;
13030
James Smart4f774512009-05-22 14:52:35 -040013031 switch (cq->subtype) {
13032 case LPFC_FCP:
James Smart8a5ca102017-11-20 16:00:30 -080013033 cq_event = lpfc_cq_event_setup(
13034 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13035 if (!cq_event)
13036 return false;
James Smart4f774512009-05-22 14:52:35 -040013037 spin_lock_irqsave(&phba->hbalock, iflags);
13038 list_add_tail(&cq_event->list,
13039 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
13040 /* Set the fcp xri abort event flag */
13041 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
13042 spin_unlock_irqrestore(&phba->hbalock, iflags);
13043 workposted = true;
13044 break;
James Smart422c4cb2017-11-20 16:00:32 -080013045 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040013046 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080013047 cq_event = lpfc_cq_event_setup(
13048 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13049 if (!cq_event)
13050 return false;
James Smart4f774512009-05-22 14:52:35 -040013051 spin_lock_irqsave(&phba->hbalock, iflags);
13052 list_add_tail(&cq_event->list,
13053 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13054 /* Set the els xri abort event flag */
13055 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13056 spin_unlock_irqrestore(&phba->hbalock, iflags);
13057 workposted = true;
13058 break;
James Smart318083a2017-03-04 09:30:30 -080013059 case LPFC_NVME:
James Smart8a5ca102017-11-20 16:00:30 -080013060 /* Notify aborted XRI for NVME work queue */
13061 if (phba->nvmet_support)
13062 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13063 else
13064 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
13065
13066 workposted = false;
James Smart318083a2017-03-04 09:30:30 -080013067 break;
James Smart4f774512009-05-22 14:52:35 -040013068 default:
13069 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080013070 "0603 Invalid CQ subtype %d: "
13071 "%08x %08x %08x %08x\n",
13072 cq->subtype, wcqe->word0, wcqe->parameter,
13073 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013074 workposted = false;
13075 break;
13076 }
13077 return workposted;
13078}
13079
13080/**
James Smart4d9ab992009-10-02 15:16:39 -040013081 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040013082 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040013083 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013084 *
James Smart4d9ab992009-10-02 15:16:39 -040013085 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013086 *
13087 * Return: true if work posted to worker thread, otherwise false.
13088 **/
13089static bool
James Smart4d9ab992009-10-02 15:16:39 -040013090lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13091{
13092 bool workposted = false;
James Smart895427b2017-02-12 13:52:30 -080013093 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040013094 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13095 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070013096 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040013097 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040013098 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040013099 unsigned long iflags;
13100
James Smart2e90f4b2011-12-13 13:22:37 -050013101 /* sanity check on queue memory */
13102 if (unlikely(!hrq) || unlikely(!drq))
13103 return workposted;
13104
James Smart7851fe22011-07-22 18:36:52 -040013105 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13106 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13107 else
13108 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13109 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040013110 goto out;
13111
13112 status = bf_get(lpfc_rcqe_status, rcqe);
13113 switch (status) {
13114 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13115 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13116 "2537 Receive Frame Truncated!!\n");
13117 case FC_STATUS_RQ_SUCCESS:
13118 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013119 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040013120 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13121 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040013122 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040013123 spin_unlock_irqrestore(&phba->hbalock, iflags);
13124 goto out;
13125 }
James Smartb84daac2012-08-03 12:35:13 -040013126 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013127 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040013128 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080013129
13130 /* If a NVME LS event (type 0x28), treat it as Fast path */
13131 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13132
James Smart4d9ab992009-10-02 15:16:39 -040013133 /* save off the frame for the word thread to process */
13134 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013135 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040013136 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040013137 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040013138 spin_unlock_irqrestore(&phba->hbalock, iflags);
13139 workposted = true;
13140 break;
James Smart4d9ab992009-10-02 15:16:39 -040013141 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013142 if (phba->nvmet_support) {
13143 tgtp = phba->targetport->private;
13144 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13145 "6402 RQE Error x%x, posted %d err_cnt "
13146 "%d: %x %x %x\n",
13147 status, hrq->RQ_buf_posted,
13148 hrq->RQ_no_posted_buf,
13149 atomic_read(&tgtp->rcv_fcp_cmd_in),
13150 atomic_read(&tgtp->rcv_fcp_cmd_out),
13151 atomic_read(&tgtp->xmt_fcp_release));
13152 }
13153 /* fallthrough */
13154
13155 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040013156 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040013157 /* Post more buffers if possible */
13158 spin_lock_irqsave(&phba->hbalock, iflags);
13159 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13160 spin_unlock_irqrestore(&phba->hbalock, iflags);
13161 workposted = true;
13162 break;
13163 }
13164out:
13165 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040013166}
13167
13168/**
13169 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13170 * @phba: Pointer to HBA context object.
13171 * @cq: Pointer to the completion queue.
13172 * @wcqe: Pointer to a completion queue entry.
13173 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013174 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040013175 * entry.
13176 *
13177 * Return: true if work posted to worker thread, otherwise false.
13178 **/
13179static bool
13180lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013181 struct lpfc_cqe *cqe)
13182{
James Smart45ed1192009-10-02 15:17:02 -040013183 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040013184 bool workposted = false;
13185
13186 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040013187 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013188
13189 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040013190 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040013191 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040013192 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040013193 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040013194 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013195 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013196 break;
13197 case CQE_CODE_RELEASE_WQE:
13198 /* Process the WQ release event */
13199 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013200 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013201 break;
13202 case CQE_CODE_XRI_ABORTED:
13203 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013204 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013205 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013206 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013207 break;
James Smart4d9ab992009-10-02 15:16:39 -040013208 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013209 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013210 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013211 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013212 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013213 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013214 break;
James Smart4f774512009-05-22 14:52:35 -040013215 default:
13216 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13217 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013218 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013219 break;
13220 }
13221 return workposted;
13222}
13223
13224/**
James Smart4f774512009-05-22 14:52:35 -040013225 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13226 * @phba: Pointer to HBA context object.
13227 * @eqe: Pointer to fast-path event queue entry.
13228 *
13229 * This routine process a event queue entry from the slow-path event queue.
13230 * It will check the MajorCode and MinorCode to determine this is for a
13231 * completion event on a completion queue, if not, an error shall be logged
13232 * and just return. Otherwise, it will get to the corresponding completion
13233 * queue and process all the entries on that completion queue, rearm the
13234 * completion queue, and then return.
13235 *
13236 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013237static void
James Smart67d12732012-08-03 12:36:13 -040013238lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13239 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013240{
James Smart67d12732012-08-03 12:36:13 -040013241 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013242 uint16_t cqid;
13243
James Smart4f774512009-05-22 14:52:35 -040013244 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013245 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013246
James Smart4f774512009-05-22 14:52:35 -040013247 list_for_each_entry(childq, &speq->child_list, list) {
13248 if (childq->queue_id == cqid) {
13249 cq = childq;
13250 break;
13251 }
13252 }
13253 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013254 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13255 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13256 "0365 Slow-path CQ identifier "
13257 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013258 return;
James Smart4f774512009-05-22 14:52:35 -040013259 }
13260
James Smart895427b2017-02-12 13:52:30 -080013261 /* Save EQ associated with this CQ */
13262 cq->assoc_qp = speq;
13263
Dick Kennedyf485c182017-09-29 17:34:34 -070013264 if (!queue_work(phba->wq, &cq->spwork))
13265 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13266 "0390 Cannot schedule soft IRQ "
13267 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13268 cqid, cq->queue_id, smp_processor_id());
13269}
13270
13271/**
13272 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13273 * @phba: Pointer to HBA context object.
13274 *
13275 * This routine process a event queue entry from the slow-path event queue.
13276 * It will check the MajorCode and MinorCode to determine this is for a
13277 * completion event on a completion queue, if not, an error shall be logged
13278 * and just return. Otherwise, it will get to the corresponding completion
13279 * queue and process all the entries on that completion queue, rearm the
13280 * completion queue, and then return.
13281 *
13282 **/
13283static void
13284lpfc_sli4_sp_process_cq(struct work_struct *work)
13285{
13286 struct lpfc_queue *cq =
13287 container_of(work, struct lpfc_queue, spwork);
13288 struct lpfc_hba *phba = cq->phba;
13289 struct lpfc_cqe *cqe;
13290 bool workposted = false;
13291 int ccount = 0;
13292
James Smart4f774512009-05-22 14:52:35 -040013293 /* Process all the entries to the CQ */
13294 switch (cq->type) {
13295 case LPFC_MCQ:
13296 while ((cqe = lpfc_sli4_cq_get(cq))) {
13297 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013298 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013299 break;
James Smartb84daac2012-08-03 12:35:13 -040013300 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040013301 }
13302 break;
13303 case LPFC_WCQ:
13304 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013305 if (cq->subtype == LPFC_FCP ||
13306 cq->subtype == LPFC_NVME) {
13307#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13308 if (phba->ktime_on)
13309 cq->isr_timestamp = ktime_get_ns();
13310 else
13311 cq->isr_timestamp = 0;
13312#endif
James Smart895427b2017-02-12 13:52:30 -080013313 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040013314 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013315 } else {
James Smart05580562011-05-24 11:40:48 -040013316 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13317 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013318 }
Dick Kennedyf485c182017-09-29 17:34:34 -070013319 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013320 break;
James Smart4f774512009-05-22 14:52:35 -040013321 }
James Smartb84daac2012-08-03 12:35:13 -040013322
13323 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013324 if (ccount > cq->CQ_max_cqe)
13325 cq->CQ_max_cqe = ccount;
James Smart4f774512009-05-22 14:52:35 -040013326 break;
13327 default:
13328 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13329 "0370 Invalid completion queue type (%d)\n",
13330 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013331 return;
James Smart4f774512009-05-22 14:52:35 -040013332 }
13333
13334 /* Catch the no cq entry condition, log an error */
Dick Kennedyf485c182017-09-29 17:34:34 -070013335 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013336 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13337 "0371 No entry from the CQ: identifier "
13338 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13339
13340 /* In any case, flash and re-arm the RCQ */
James Smartb71413d2018-02-22 08:18:40 -080013341 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013342
13343 /* wake up worker thread if there are works to be done */
13344 if (workposted)
13345 lpfc_worker_wake_up(phba);
13346}
13347
13348/**
13349 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013350 * @phba: Pointer to HBA context object.
13351 * @cq: Pointer to associated CQ
13352 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013353 *
13354 * This routine process a fast-path work queue completion entry from fast-path
13355 * event queue for FCP command response completion.
13356 **/
13357static void
James Smart2a76a282012-08-03 12:35:54 -040013358lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013359 struct lpfc_wcqe_complete *wcqe)
13360{
James Smart2a76a282012-08-03 12:35:54 -040013361 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013362 struct lpfc_iocbq *cmdiocbq;
13363 struct lpfc_iocbq irspiocbq;
13364 unsigned long iflags;
13365
James Smart4f774512009-05-22 14:52:35 -040013366 /* Check for response status */
13367 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13368 /* If resource errors reported from HBA, reduce queue
13369 * depth of the SCSI device.
13370 */
James Smarte3d2b802012-08-14 14:25:43 -040013371 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13372 IOSTAT_LOCAL_REJECT)) &&
13373 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13374 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013375 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013376
James Smart4f774512009-05-22 14:52:35 -040013377 /* Log the error status */
13378 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13379 "0373 FCP complete error: status=x%x, "
13380 "hw_status=x%x, total_data_specified=%d, "
13381 "parameter=x%x, word3=x%x\n",
13382 bf_get(lpfc_wcqe_c_status, wcqe),
13383 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13384 wcqe->total_data_placed, wcqe->parameter,
13385 wcqe->word3);
13386 }
13387
13388 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013389 spin_lock_irqsave(&pring->ring_lock, iflags);
13390 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013391 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13392 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013393 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013394 if (unlikely(!cmdiocbq)) {
13395 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13396 "0374 FCP complete with no corresponding "
13397 "cmdiocb: iotag (%d)\n",
13398 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13399 return;
13400 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013401#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13402 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13403#endif
James Smart895427b2017-02-12 13:52:30 -080013404 if (cmdiocbq->iocb_cmpl == NULL) {
13405 if (cmdiocbq->wqe_cmpl) {
13406 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13407 spin_lock_irqsave(&phba->hbalock, iflags);
13408 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13409 spin_unlock_irqrestore(&phba->hbalock, iflags);
13410 }
13411
13412 /* Pass the cmd_iocb and the wcqe to the upper layer */
13413 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13414 return;
13415 }
James Smart4f774512009-05-22 14:52:35 -040013416 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13417 "0375 FCP cmdiocb not callback function "
13418 "iotag: (%d)\n",
13419 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13420 return;
13421 }
13422
13423 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013424 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013425
James Smart0f65ff62010-02-26 14:14:23 -050013426 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13427 spin_lock_irqsave(&phba->hbalock, iflags);
13428 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13429 spin_unlock_irqrestore(&phba->hbalock, iflags);
13430 }
13431
James Smart4f774512009-05-22 14:52:35 -040013432 /* Pass the cmd_iocb and the rsp state to the upper layer */
13433 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13434}
13435
13436/**
13437 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13438 * @phba: Pointer to HBA context object.
13439 * @cq: Pointer to completion queue.
13440 * @wcqe: Pointer to work-queue completion queue entry.
13441 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013442 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013443 * proper WQ release routine to the slow-path WQ.
13444 **/
13445static void
13446lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13447 struct lpfc_wcqe_release *wcqe)
13448{
13449 struct lpfc_queue *childwq;
13450 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013451 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013452
13453 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013454 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013455 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013456 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013457 lpfc_sli4_wq_release(childwq,
13458 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080013459 if (childwq->q_flag & HBA_NVMET_WQFULL)
13460 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040013461 wqid_matched = true;
13462 break;
13463 }
13464 }
13465 /* Report warning log message if no match found */
13466 if (wqid_matched != true)
13467 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13468 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013469 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013470}
13471
13472/**
James Smart2d7dbc42017-02-12 13:52:35 -080013473 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13474 * @phba: Pointer to HBA context object.
13475 * @rcqe: Pointer to receive-queue completion queue entry.
13476 *
13477 * This routine process a receive-queue completion queue entry.
13478 *
13479 * Return: true if work posted to worker thread, otherwise false.
13480 **/
13481static bool
13482lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13483 struct lpfc_rcqe *rcqe)
13484{
13485 bool workposted = false;
13486 struct lpfc_queue *hrq;
13487 struct lpfc_queue *drq;
13488 struct rqb_dmabuf *dma_buf;
13489 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013490 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013491 uint32_t status, rq_id;
13492 unsigned long iflags;
13493 uint32_t fctl, idx;
13494
13495 if ((phba->nvmet_support == 0) ||
13496 (phba->sli4_hba.nvmet_cqset == NULL))
13497 return workposted;
13498
13499 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13500 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13501 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13502
13503 /* sanity check on queue memory */
13504 if (unlikely(!hrq) || unlikely(!drq))
13505 return workposted;
13506
13507 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13508 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13509 else
13510 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13511
13512 if ((phba->nvmet_support == 0) ||
13513 (rq_id != hrq->queue_id))
13514 return workposted;
13515
13516 status = bf_get(lpfc_rcqe_status, rcqe);
13517 switch (status) {
13518 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13519 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13520 "6126 Receive Frame Truncated!!\n");
James Smart78e1d202017-06-01 21:07:09 -070013521 /* Drop thru */
James Smart2d7dbc42017-02-12 13:52:35 -080013522 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080013523 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013524 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080013525 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13526 if (!dma_buf) {
13527 hrq->RQ_no_buf_found++;
13528 spin_unlock_irqrestore(&phba->hbalock, iflags);
13529 goto out;
13530 }
13531 spin_unlock_irqrestore(&phba->hbalock, iflags);
13532 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013533 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013534 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13535
13536 /* Just some basic sanity checks on FCP Command frame */
13537 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13538 fc_hdr->fh_f_ctl[1] << 8 |
13539 fc_hdr->fh_f_ctl[2]);
13540 if (((fctl &
13541 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13542 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13543 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13544 goto drop;
13545
13546 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13547 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013548 lpfc_nvmet_unsol_fcp_event(
Dick Kennedy66d7ce92017-08-23 16:55:42 -070013549 phba, idx, dma_buf,
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013550 cq->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013551 return false;
13552 }
13553drop:
13554 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13555 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013556 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013557 if (phba->nvmet_support) {
13558 tgtp = phba->targetport->private;
13559 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13560 "6401 RQE Error x%x, posted %d err_cnt "
13561 "%d: %x %x %x\n",
13562 status, hrq->RQ_buf_posted,
13563 hrq->RQ_no_posted_buf,
13564 atomic_read(&tgtp->rcv_fcp_cmd_in),
13565 atomic_read(&tgtp->rcv_fcp_cmd_out),
13566 atomic_read(&tgtp->xmt_fcp_release));
13567 }
13568 /* fallthrough */
13569
13570 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013571 hrq->RQ_no_posted_buf++;
13572 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013573 break;
13574 }
13575out:
13576 return workposted;
13577}
13578
13579/**
James Smart895427b2017-02-12 13:52:30 -080013580 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013581 * @cq: Pointer to the completion queue.
13582 * @eqe: Pointer to fast-path completion queue entry.
13583 *
13584 * This routine process a fast-path work queue completion entry from fast-path
13585 * event queue for FCP command response completion.
13586 **/
13587static int
James Smart895427b2017-02-12 13:52:30 -080013588lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013589 struct lpfc_cqe *cqe)
13590{
13591 struct lpfc_wcqe_release wcqe;
13592 bool workposted = false;
13593
13594 /* Copy the work queue CQE and convert endian order if needed */
13595 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13596
13597 /* Check and process for different type of WCQE and dispatch */
13598 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13599 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013600 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013601 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013602 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013603 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013604 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13605 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13606 (struct lpfc_wcqe_complete *)&wcqe);
13607 if (cq->subtype == LPFC_NVME_LS)
13608 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013609 (struct lpfc_wcqe_complete *)&wcqe);
13610 break;
13611 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013612 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013613 /* Process the WQ release event */
13614 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13615 (struct lpfc_wcqe_release *)&wcqe);
13616 break;
13617 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013618 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013619 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013620 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013621 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13622 (struct sli4_wcqe_xri_aborted *)&wcqe);
13623 break;
James Smart895427b2017-02-12 13:52:30 -080013624 case CQE_CODE_RECEIVE_V1:
13625 case CQE_CODE_RECEIVE:
13626 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013627 if (cq->subtype == LPFC_NVMET) {
13628 workposted = lpfc_sli4_nvmet_handle_rcqe(
13629 phba, cq, (struct lpfc_rcqe *)&wcqe);
13630 }
James Smart895427b2017-02-12 13:52:30 -080013631 break;
James Smart4f774512009-05-22 14:52:35 -040013632 default:
13633 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013634 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013635 bf_get(lpfc_wcqe_c_code, &wcqe));
13636 break;
13637 }
13638 return workposted;
13639}
13640
13641/**
James Smart67d12732012-08-03 12:36:13 -040013642 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040013643 * @phba: Pointer to HBA context object.
13644 * @eqe: Pointer to fast-path event queue entry.
13645 *
13646 * This routine process a event queue entry from the fast-path event queue.
13647 * It will check the MajorCode and MinorCode to determine this is for a
13648 * completion event on a completion queue, if not, an error shall be logged
13649 * and just return. Otherwise, it will get to the corresponding completion
13650 * queue and process all the entries on the completion queue, rearm the
13651 * completion queue, and then return.
13652 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013653static void
James Smart67d12732012-08-03 12:36:13 -040013654lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13655 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040013656{
James Smart895427b2017-02-12 13:52:30 -080013657 struct lpfc_queue *cq = NULL;
James Smart2d7dbc42017-02-12 13:52:35 -080013658 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040013659
James Smartcb5172e2010-03-15 11:25:07 -040013660 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040013661 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040013662 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040013663 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040013664 bf_get_le32(lpfc_eqe_major_code, eqe),
13665 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070013666 return;
James Smart4f774512009-05-22 14:52:35 -040013667 }
13668
James Smart67d12732012-08-03 12:36:13 -040013669 /* Get the reference to the corresponding CQ */
13670 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13671
James Smart2d7dbc42017-02-12 13:52:35 -080013672 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13673 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13674 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13675 /* Process NVMET unsol rcv */
13676 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13677 goto process_cq;
13678 }
13679 }
13680
James Smart895427b2017-02-12 13:52:30 -080013681 if (phba->sli4_hba.nvme_cq_map &&
13682 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
James Smartf358dd02017-02-12 13:52:34 -080013683 /* Process NVME / NVMET command completion */
James Smart895427b2017-02-12 13:52:30 -080013684 cq = phba->sli4_hba.nvme_cq[qidx];
13685 goto process_cq;
13686 }
13687
13688 if (phba->sli4_hba.fcp_cq_map &&
13689 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13690 /* Process FCP command completion */
13691 cq = phba->sli4_hba.fcp_cq[qidx];
13692 goto process_cq;
13693 }
13694
13695 if (phba->sli4_hba.nvmels_cq &&
13696 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13697 /* Process NVME unsol rcv */
13698 cq = phba->sli4_hba.nvmels_cq;
13699 }
13700
13701 /* Otherwise this is a Slow path event */
13702 if (cq == NULL) {
Dick Kennedyf485c182017-09-29 17:34:34 -070013703 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13704 return;
James Smart67d12732012-08-03 12:36:13 -040013705 }
13706
James Smart895427b2017-02-12 13:52:30 -080013707process_cq:
James Smart4f774512009-05-22 14:52:35 -040013708 if (unlikely(cqid != cq->queue_id)) {
13709 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13710 "0368 Miss-matched fast-path completion "
13711 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13712 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070013713 return;
James Smart4f774512009-05-22 14:52:35 -040013714 }
13715
James Smart895427b2017-02-12 13:52:30 -080013716 /* Save EQ associated with this CQ */
13717 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13718
Dick Kennedyf485c182017-09-29 17:34:34 -070013719 if (!queue_work(phba->wq, &cq->irqwork))
13720 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13721 "0363 Cannot schedule soft IRQ "
13722 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13723 cqid, cq->queue_id, smp_processor_id());
13724}
13725
13726/**
13727 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13728 * @phba: Pointer to HBA context object.
13729 * @eqe: Pointer to fast-path event queue entry.
13730 *
13731 * This routine process a event queue entry from the fast-path event queue.
13732 * It will check the MajorCode and MinorCode to determine this is for a
13733 * completion event on a completion queue, if not, an error shall be logged
13734 * and just return. Otherwise, it will get to the corresponding completion
13735 * queue and process all the entries on the completion queue, rearm the
13736 * completion queue, and then return.
13737 **/
13738static void
13739lpfc_sli4_hba_process_cq(struct work_struct *work)
13740{
13741 struct lpfc_queue *cq =
13742 container_of(work, struct lpfc_queue, irqwork);
13743 struct lpfc_hba *phba = cq->phba;
13744 struct lpfc_cqe *cqe;
13745 bool workposted = false;
13746 int ccount = 0;
13747
James Smart4f774512009-05-22 14:52:35 -040013748 /* Process all the entries to the CQ */
13749 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013750#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13751 if (phba->ktime_on)
13752 cq->isr_timestamp = ktime_get_ns();
13753 else
13754 cq->isr_timestamp = 0;
13755#endif
James Smart895427b2017-02-12 13:52:30 -080013756 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013757 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013758 break;
James Smart4f774512009-05-22 14:52:35 -040013759 }
13760
James Smartb84daac2012-08-03 12:35:13 -040013761 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013762 if (ccount > cq->CQ_max_cqe)
13763 cq->CQ_max_cqe = ccount;
13764 cq->assoc_qp->EQ_cqe_cnt += ccount;
James Smartb84daac2012-08-03 12:35:13 -040013765
James Smart4f774512009-05-22 14:52:35 -040013766 /* Catch the no cq entry condition */
Dick Kennedyf485c182017-09-29 17:34:34 -070013767 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013768 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13769 "0369 No entry from fast-path completion "
13770 "queue fcpcqid=%d\n", cq->queue_id);
13771
13772 /* In any case, flash and re-arm the CQ */
James Smartb71413d2018-02-22 08:18:40 -080013773 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013774
13775 /* wake up worker thread if there are works to be done */
13776 if (workposted)
13777 lpfc_worker_wake_up(phba);
13778}
13779
13780static void
13781lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13782{
13783 struct lpfc_eqe *eqe;
13784
13785 /* walk all the EQ entries and drop on the floor */
13786 while ((eqe = lpfc_sli4_eq_get(eq)))
13787 ;
13788
13789 /* Clear and re-arm the EQ */
James Smartb71413d2018-02-22 08:18:40 -080013790 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013791}
13792
James Smart1ba981f2014-02-20 09:56:45 -050013793
13794/**
13795 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13796 * entry
13797 * @phba: Pointer to HBA context object.
13798 * @eqe: Pointer to fast-path event queue entry.
13799 *
13800 * This routine process a event queue entry from the Flash Optimized Fabric
13801 * event queue. It will check the MajorCode and MinorCode to determine this
13802 * is for a completion event on a completion queue, if not, an error shall be
13803 * logged and just return. Otherwise, it will get to the corresponding
13804 * completion queue and process all the entries on the completion queue, rearm
13805 * the completion queue, and then return.
13806 **/
13807static void
13808lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13809{
13810 struct lpfc_queue *cq;
James Smart1ba981f2014-02-20 09:56:45 -050013811 uint16_t cqid;
James Smart1ba981f2014-02-20 09:56:45 -050013812
13813 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13814 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13815 "9147 Not a valid completion "
13816 "event: majorcode=x%x, minorcode=x%x\n",
13817 bf_get_le32(lpfc_eqe_major_code, eqe),
13818 bf_get_le32(lpfc_eqe_minor_code, eqe));
13819 return;
13820 }
13821
13822 /* Get the reference to the corresponding CQ */
13823 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13824
13825 /* Next check for OAS */
13826 cq = phba->sli4_hba.oas_cq;
13827 if (unlikely(!cq)) {
13828 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13829 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13830 "9148 OAS completion queue "
13831 "does not exist\n");
13832 return;
13833 }
13834
13835 if (unlikely(cqid != cq->queue_id)) {
13836 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13837 "9149 Miss-matched fast-path compl "
13838 "queue id: eqcqid=%d, fcpcqid=%d\n",
13839 cqid, cq->queue_id);
13840 return;
13841 }
13842
James Smartd41b65b2017-06-15 22:56:41 -070013843 /* Save EQ associated with this CQ */
13844 cq->assoc_qp = phba->sli4_hba.fof_eq;
13845
Dick Kennedyf485c182017-09-29 17:34:34 -070013846 /* CQ work will be processed on CPU affinitized to this IRQ */
13847 if (!queue_work(phba->wq, &cq->irqwork))
James Smart1ba981f2014-02-20 09:56:45 -050013848 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
Dick Kennedyf485c182017-09-29 17:34:34 -070013849 "0367 Cannot schedule soft IRQ "
13850 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13851 cqid, cq->queue_id, smp_processor_id());
James Smart1ba981f2014-02-20 09:56:45 -050013852}
13853
13854/**
13855 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13856 * @irq: Interrupt number.
13857 * @dev_id: The device context pointer.
13858 *
13859 * This function is directly called from the PCI layer as an interrupt
13860 * service routine when device with SLI-4 interface spec is enabled with
13861 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13862 * IOCB ring event in the HBA. However, when the device is enabled with either
13863 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13864 * device-level interrupt handler. When the PCI slot is in error recovery
13865 * or the HBA is undergoing initialization, the interrupt handler will not
13866 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13867 * the intrrupt context. This function is called without any lock held.
13868 * It gets the hbalock to access and update SLI data structures. Note that,
13869 * the EQ to CQ are one-to-one map such that the EQ index is
13870 * equal to that of CQ index.
13871 *
13872 * This function returns IRQ_HANDLED when interrupt is handled else it
13873 * returns IRQ_NONE.
13874 **/
13875irqreturn_t
13876lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13877{
13878 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013879 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050013880 struct lpfc_queue *eq;
13881 struct lpfc_eqe *eqe;
13882 unsigned long iflag;
13883 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013884
13885 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013886 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13887 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050013888
13889 if (unlikely(!phba))
13890 return IRQ_NONE;
13891
13892 /* Get to the EQ struct associated with this vector */
13893 eq = phba->sli4_hba.fof_eq;
13894 if (unlikely(!eq))
13895 return IRQ_NONE;
13896
13897 /* Check device state for handling interrupt */
13898 if (unlikely(lpfc_intr_state_check(phba))) {
James Smart1ba981f2014-02-20 09:56:45 -050013899 /* Check again for link_state with lock held */
13900 spin_lock_irqsave(&phba->hbalock, iflag);
13901 if (phba->link_state < LPFC_LINK_DOWN)
13902 /* Flush, clear interrupt, and rearm the EQ */
13903 lpfc_sli4_eq_flush(phba, eq);
13904 spin_unlock_irqrestore(&phba->hbalock, iflag);
13905 return IRQ_NONE;
13906 }
13907
13908 /*
13909 * Process all the event on FCP fast-path EQ
13910 */
13911 while ((eqe = lpfc_sli4_eq_get(eq))) {
13912 lpfc_sli4_fof_handle_eqe(phba, eqe);
13913 if (!(++ecount % eq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013914 break;
James Smart1ba981f2014-02-20 09:56:45 -050013915 eq->EQ_processed++;
13916 }
13917
13918 /* Track the max number of EQEs processed in 1 intr */
13919 if (ecount > eq->EQ_max_eqe)
13920 eq->EQ_max_eqe = ecount;
13921
13922
13923 if (unlikely(ecount == 0)) {
13924 eq->EQ_no_entry++;
13925
13926 if (phba->intr_type == MSIX)
13927 /* MSI-X treated interrupt served as no EQ share INT */
13928 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13929 "9145 MSI-X interrupt with no EQE\n");
13930 else {
13931 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13932 "9146 ISR interrupt with no EQE\n");
13933 /* Non MSI-X treated on interrupt as EQ share INT */
13934 return IRQ_NONE;
13935 }
13936 }
13937 /* Always clear and re-arm the fast-path EQ */
James Smartb71413d2018-02-22 08:18:40 -080013938 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -050013939 return IRQ_HANDLED;
13940}
13941
James Smart4f774512009-05-22 14:52:35 -040013942/**
James Smart67d12732012-08-03 12:36:13 -040013943 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040013944 * @irq: Interrupt number.
13945 * @dev_id: The device context pointer.
13946 *
13947 * This function is directly called from the PCI layer as an interrupt
13948 * service routine when device with SLI-4 interface spec is enabled with
13949 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13950 * ring event in the HBA. However, when the device is enabled with either
13951 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13952 * device-level interrupt handler. When the PCI slot is in error recovery
13953 * or the HBA is undergoing initialization, the interrupt handler will not
13954 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13955 * the intrrupt context. This function is called without any lock held.
13956 * It gets the hbalock to access and update SLI data structures. Note that,
13957 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13958 * equal to that of FCP CQ index.
13959 *
James Smart67d12732012-08-03 12:36:13 -040013960 * The link attention and ELS ring attention events are handled
13961 * by the worker thread. The interrupt handler signals the worker thread
13962 * and returns for these events. This function is called without any lock
13963 * held. It gets the hbalock to access and update SLI data structures.
13964 *
James Smart4f774512009-05-22 14:52:35 -040013965 * This function returns IRQ_HANDLED when interrupt is handled else it
13966 * returns IRQ_NONE.
13967 **/
13968irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040013969lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040013970{
13971 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013972 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040013973 struct lpfc_queue *fpeq;
13974 struct lpfc_eqe *eqe;
13975 unsigned long iflag;
13976 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080013977 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040013978
13979 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013980 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13981 phba = hba_eq_hdl->phba;
13982 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040013983
13984 if (unlikely(!phba))
13985 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040013986 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040013987 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013988
13989 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080013990 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050013991 if (unlikely(!fpeq))
13992 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013993
James Smartba20c852012-08-03 12:36:52 -040013994 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013995 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartb71413d2018-02-22 08:18:40 -080013996 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -040013997 else {
James Smart895427b2017-02-12 13:52:30 -080013998 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013999 return IRQ_NONE;
14000 }
14001 }
14002
James Smart4f774512009-05-22 14:52:35 -040014003 /* Check device state for handling interrupt */
14004 if (unlikely(lpfc_intr_state_check(phba))) {
14005 /* Check again for link_state with lock held */
14006 spin_lock_irqsave(&phba->hbalock, iflag);
14007 if (phba->link_state < LPFC_LINK_DOWN)
14008 /* Flush, clear interrupt, and rearm the EQ */
14009 lpfc_sli4_eq_flush(phba, fpeq);
14010 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040014011 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080014012 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040014013 return IRQ_NONE;
14014 }
14015
14016 /*
14017 * Process all the event on FCP fast-path EQ
14018 */
14019 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
Dick Kennedyf485c182017-09-29 17:34:34 -070014020 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
14021 if (!(++ecount % fpeq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070014022 break;
James Smartb84daac2012-08-03 12:35:13 -040014023 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040014024 }
14025
James Smartb84daac2012-08-03 12:35:13 -040014026 /* Track the max number of EQEs processed in 1 intr */
14027 if (ecount > fpeq->EQ_max_eqe)
14028 fpeq->EQ_max_eqe = ecount;
14029
James Smart4f774512009-05-22 14:52:35 -040014030 /* Always clear and re-arm the fast-path EQ */
James Smartb71413d2018-02-22 08:18:40 -080014031 phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014032
14033 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040014034 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040014035
14036 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080014037 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040014038 return IRQ_NONE;
14039 }
14040
James Smart4f774512009-05-22 14:52:35 -040014041 if (phba->intr_type == MSIX)
14042 /* MSI-X treated interrupt served as no EQ share INT */
14043 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14044 "0358 MSI-X interrupt with no EQE\n");
14045 else
14046 /* Non MSI-X treated on interrupt as EQ share INT */
14047 return IRQ_NONE;
14048 }
14049
James Smartba20c852012-08-03 12:36:52 -040014050 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080014051 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14052
James Smart4f774512009-05-22 14:52:35 -040014053 return IRQ_HANDLED;
14054} /* lpfc_sli4_fp_intr_handler */
14055
14056/**
14057 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14058 * @irq: Interrupt number.
14059 * @dev_id: The device context pointer.
14060 *
14061 * This function is the device-level interrupt handler to device with SLI-4
14062 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14063 * interrupt mode is enabled and there is an event in the HBA which requires
14064 * driver attention. This function invokes the slow-path interrupt attention
14065 * handling function and fast-path interrupt attention handling function in
14066 * turn to process the relevant HBA attention events. This function is called
14067 * without any lock held. It gets the hbalock to access and update SLI data
14068 * structures.
14069 *
14070 * This function returns IRQ_HANDLED when interrupt is handled, else it
14071 * returns IRQ_NONE.
14072 **/
14073irqreturn_t
14074lpfc_sli4_intr_handler(int irq, void *dev_id)
14075{
14076 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040014077 irqreturn_t hba_irq_rc;
14078 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080014079 int qidx;
James Smart4f774512009-05-22 14:52:35 -040014080
14081 /* Get the driver's phba structure from the dev_id */
14082 phba = (struct lpfc_hba *)dev_id;
14083
14084 if (unlikely(!phba))
14085 return IRQ_NONE;
14086
14087 /*
James Smart4f774512009-05-22 14:52:35 -040014088 * Invoke fast-path host attention interrupt handling as appropriate.
14089 */
James Smart895427b2017-02-12 13:52:30 -080014090 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040014091 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014092 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040014093 if (hba_irq_rc == IRQ_HANDLED)
14094 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040014095 }
14096
James Smart1ba981f2014-02-20 09:56:45 -050014097 if (phba->cfg_fof) {
14098 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014099 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050014100 if (hba_irq_rc == IRQ_HANDLED)
14101 hba_handled |= true;
14102 }
14103
James Smart67d12732012-08-03 12:36:13 -040014104 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014105} /* lpfc_sli4_intr_handler */
14106
14107/**
14108 * lpfc_sli4_queue_free - free a queue structure and associated memory
14109 * @queue: The queue structure to free.
14110 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040014111 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040014112 * the host resident queue. This function must be called after destroying the
14113 * queue on the HBA.
14114 **/
14115void
14116lpfc_sli4_queue_free(struct lpfc_queue *queue)
14117{
14118 struct lpfc_dmabuf *dmabuf;
14119
14120 if (!queue)
14121 return;
14122
14123 while (!list_empty(&queue->page_list)) {
14124 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14125 list);
James Smart81b96ed2017-11-20 16:00:29 -080014126 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040014127 dmabuf->virt, dmabuf->phys);
14128 kfree(dmabuf);
14129 }
James Smart895427b2017-02-12 13:52:30 -080014130 if (queue->rqbp) {
14131 lpfc_free_rq_buffer(queue->phba, queue);
14132 kfree(queue->rqbp);
14133 }
James Smartd1f525a2017-04-21 16:04:55 -070014134
14135 if (!list_empty(&queue->wq_list))
14136 list_del(&queue->wq_list);
14137
James Smart4f774512009-05-22 14:52:35 -040014138 kfree(queue);
14139 return;
14140}
14141
14142/**
14143 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14144 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080014145 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040014146 * @entry_size: The size of each queue entry for this queue.
14147 * @entry count: The number of entries that this queue will handle.
14148 *
14149 * This function allocates a queue structure and the DMAable memory used for
14150 * the host resident queue. This function must be called before creating the
14151 * queue on the HBA.
14152 **/
14153struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080014154lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14155 uint32_t entry_size, uint32_t entry_count)
James Smart4f774512009-05-22 14:52:35 -040014156{
14157 struct lpfc_queue *queue;
14158 struct lpfc_dmabuf *dmabuf;
14159 int x, total_qe_count;
14160 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040014161 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040014162
James Smartcb5172e2010-03-15 11:25:07 -040014163 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014164 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040014165
James Smart4f774512009-05-22 14:52:35 -040014166 queue = kzalloc(sizeof(struct lpfc_queue) +
14167 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14168 if (!queue)
14169 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040014170 queue->page_count = (ALIGN(entry_size * entry_count,
14171 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080014172
14173 /* If needed, Adjust page count to match the max the adapter supports */
14174 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
14175 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14176
James Smart4f774512009-05-22 14:52:35 -040014177 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080014178 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080014179 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040014180 INIT_LIST_HEAD(&queue->page_list);
14181 INIT_LIST_HEAD(&queue->child_list);
James Smart81b96ed2017-11-20 16:00:29 -080014182
14183 /* Set queue parameters now. If the system cannot provide memory
14184 * resources, the free routine needs to know what was allocated.
14185 */
14186 queue->entry_size = entry_size;
14187 queue->entry_count = entry_count;
14188 queue->page_size = hw_page_size;
14189 queue->phba = phba;
14190
James Smart4f774512009-05-22 14:52:35 -040014191 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14192 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14193 if (!dmabuf)
14194 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040014195 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14196 hw_page_size, &dmabuf->phys,
14197 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040014198 if (!dmabuf->virt) {
14199 kfree(dmabuf);
14200 goto out_fail;
14201 }
14202 dmabuf->buffer_tag = x;
14203 list_add_tail(&dmabuf->list, &queue->page_list);
14204 /* initialize queue's entry array */
14205 dma_pointer = dmabuf->virt;
14206 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040014207 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040014208 total_qe_count++, dma_pointer += entry_size) {
14209 queue->qe[total_qe_count].address = dma_pointer;
14210 }
14211 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014212 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14213 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014214
James Smart64eb4dc2017-05-15 15:20:49 -070014215 /* entry_repost will be set during q creation */
14216
James Smart4f774512009-05-22 14:52:35 -040014217 return queue;
14218out_fail:
14219 lpfc_sli4_queue_free(queue);
14220 return NULL;
14221}
14222
14223/**
James Smart962bc512013-01-03 15:44:00 -050014224 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14225 * @phba: HBA structure that indicates port to create a queue on.
14226 * @pci_barset: PCI BAR set flag.
14227 *
14228 * This function shall perform iomap of the specified PCI BAR address to host
14229 * memory address if not already done so and return it. The returned host
14230 * memory address can be NULL.
14231 */
14232static void __iomem *
14233lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14234{
James Smart962bc512013-01-03 15:44:00 -050014235 if (!phba->pcidev)
14236 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014237
14238 switch (pci_barset) {
14239 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014240 return phba->pci_bar0_memmap_p;
14241 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014242 return phba->pci_bar2_memmap_p;
14243 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014244 return phba->pci_bar4_memmap_p;
14245 default:
14246 break;
14247 }
14248 return NULL;
14249}
14250
14251/**
James Smart895427b2017-02-12 13:52:30 -080014252 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040014253 * @phba: HBA structure that indicates port to create a queue on.
14254 * @startq: The starting FCP EQ to modify
14255 *
14256 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
James Smart43140ca2017-03-04 09:30:34 -080014257 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14258 * updated in one mailbox command.
James Smart173edbb2012-06-12 13:54:50 -040014259 *
14260 * The @phba struct is used to send mailbox command to HBA. The @startq
14261 * is used to get the starting FCP EQ to change.
14262 * This function is asynchronous and will wait for the mailbox
14263 * command to finish before continuing.
14264 *
14265 * On success this function will return a zero. If unable to allocate enough
14266 * memory this function will return -ENOMEM. If the queue create mailbox command
14267 * fails this function will return -ENXIO.
14268 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014269int
James Smart0cf07f842017-06-01 21:07:10 -070014270lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14271 uint32_t numq, uint32_t imax)
James Smart173edbb2012-06-12 13:54:50 -040014272{
14273 struct lpfc_mbx_modify_eq_delay *eq_delay;
14274 LPFC_MBOXQ_t *mbox;
14275 struct lpfc_queue *eq;
14276 int cnt, rc, length, status = 0;
14277 uint32_t shdr_status, shdr_add_status;
James Smart0cf07f842017-06-01 21:07:10 -070014278 uint32_t result, val;
James Smart895427b2017-02-12 13:52:30 -080014279 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014280 union lpfc_sli4_cfg_shdr *shdr;
14281 uint16_t dmult;
14282
James Smart895427b2017-02-12 13:52:30 -080014283 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040014284 return 0;
14285
14286 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14287 if (!mbox)
14288 return -ENOMEM;
14289 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14290 sizeof(struct lpfc_sli4_cfg_mhdr));
14291 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14292 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14293 length, LPFC_SLI4_MBX_EMBED);
14294 eq_delay = &mbox->u.mqe.un.eq_delay;
14295
14296 /* Calculate delay multiper from maximum interrupt per second */
James Smart0cf07f842017-06-01 21:07:10 -070014297 result = imax / phba->io_channel_irqs;
James Smart895427b2017-02-12 13:52:30 -080014298 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040014299 dmult = 0;
14300 else
14301 dmult = LPFC_DMULT_CONST/result - 1;
James Smart0cf07f842017-06-01 21:07:10 -070014302 if (dmult > LPFC_DMULT_MAX)
14303 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014304
14305 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080014306 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14307 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040014308 if (!eq)
14309 continue;
James Smart0cf07f842017-06-01 21:07:10 -070014310 eq->q_mode = imax;
James Smart173edbb2012-06-12 13:54:50 -040014311 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14312 eq_delay->u.request.eq[cnt].phase = 0;
14313 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14314 cnt++;
James Smart0cf07f842017-06-01 21:07:10 -070014315
14316 /* q_mode is only used for auto_imax */
14317 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14318 /* Use EQ Delay Register method for q_mode */
14319
14320 /* Convert for EQ Delay register */
14321 val = phba->cfg_fcp_imax;
14322 if (val) {
14323 /* First, interrupts per sec per EQ */
14324 val = phba->cfg_fcp_imax /
14325 phba->io_channel_irqs;
14326
14327 /* us delay between each interrupt */
14328 val = LPFC_SEC_TO_USEC / val;
14329 }
14330 eq->q_mode = val;
14331 } else {
14332 eq->q_mode = imax;
14333 }
14334
14335 if (cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014336 break;
14337 }
14338 eq_delay->u.request.num_eq = cnt;
14339
14340 mbox->vport = phba->pport;
14341 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14342 mbox->context1 = NULL;
14343 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14344 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14345 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14346 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14347 if (shdr_status || shdr_add_status || rc) {
14348 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14349 "2512 MODIFY_EQ_DELAY mailbox failed with "
14350 "status x%x add_status x%x, mbx status x%x\n",
14351 shdr_status, shdr_add_status, rc);
14352 status = -ENXIO;
14353 }
14354 mempool_free(mbox, phba->mbox_mem_pool);
14355 return status;
14356}
14357
14358/**
James Smart4f774512009-05-22 14:52:35 -040014359 * lpfc_eq_create - Create an Event Queue on the HBA
14360 * @phba: HBA structure that indicates port to create a queue on.
14361 * @eq: The queue structure to use to create the event queue.
14362 * @imax: The maximum interrupt per second limit.
14363 *
14364 * This function creates an event queue, as detailed in @eq, on a port,
14365 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14366 *
14367 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14368 * is used to get the entry count and entry size that are necessary to
14369 * determine the number of pages to allocate and use for this queue. This
14370 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14371 * event queue. This function is asynchronous and will wait for the mailbox
14372 * command to finish before continuing.
14373 *
14374 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014375 * memory this function will return -ENOMEM. If the queue create mailbox command
14376 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014377 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014378int
James Smartee020062012-09-29 11:28:52 -040014379lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014380{
14381 struct lpfc_mbx_eq_create *eq_create;
14382 LPFC_MBOXQ_t *mbox;
14383 int rc, length, status = 0;
14384 struct lpfc_dmabuf *dmabuf;
14385 uint32_t shdr_status, shdr_add_status;
14386 union lpfc_sli4_cfg_shdr *shdr;
14387 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014388 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14389
James Smart2e90f4b2011-12-13 13:22:37 -050014390 /* sanity check on queue memory */
14391 if (!eq)
14392 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014393 if (!phba->sli4_hba.pc_sli4_params.supported)
14394 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014395
14396 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14397 if (!mbox)
14398 return -ENOMEM;
14399 length = (sizeof(struct lpfc_mbx_eq_create) -
14400 sizeof(struct lpfc_sli4_cfg_mhdr));
14401 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14402 LPFC_MBOX_OPCODE_EQ_CREATE,
14403 length, LPFC_SLI4_MBX_EMBED);
14404 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080014405 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014406 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14407 eq->page_count);
14408 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14409 LPFC_EQE_SIZE);
14410 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080014411
14412 /* Use version 2 of CREATE_EQ if eqav is set */
14413 if (phba->sli4_hba.pc_sli4_params.eqav) {
14414 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14415 LPFC_Q_CREATE_VERSION_2);
14416 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14417 phba->sli4_hba.pc_sli4_params.eqav);
14418 }
14419
James Smart2c9c5a02015-04-07 15:07:15 -040014420 /* don't setup delay multiplier using EQ_CREATE */
14421 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014422 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14423 dmult);
14424 switch (eq->entry_count) {
14425 default:
14426 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14427 "0360 Unsupported EQ count. (%d)\n",
14428 eq->entry_count);
14429 if (eq->entry_count < 256)
14430 return -EINVAL;
14431 /* otherwise default to smallest count (drop through) */
14432 case 256:
14433 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14434 LPFC_EQ_CNT_256);
14435 break;
14436 case 512:
14437 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14438 LPFC_EQ_CNT_512);
14439 break;
14440 case 1024:
14441 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14442 LPFC_EQ_CNT_1024);
14443 break;
14444 case 2048:
14445 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14446 LPFC_EQ_CNT_2048);
14447 break;
14448 case 4096:
14449 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14450 LPFC_EQ_CNT_4096);
14451 break;
14452 }
14453 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014454 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014455 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14456 putPaddrLow(dmabuf->phys);
14457 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14458 putPaddrHigh(dmabuf->phys);
14459 }
14460 mbox->vport = phba->pport;
14461 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14462 mbox->context1 = NULL;
14463 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040014464 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14465 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14466 if (shdr_status || shdr_add_status || rc) {
14467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14468 "2500 EQ_CREATE mailbox failed with "
14469 "status x%x add_status x%x, mbx status x%x\n",
14470 shdr_status, shdr_add_status, rc);
14471 status = -ENXIO;
14472 }
14473 eq->type = LPFC_EQ;
14474 eq->subtype = LPFC_NONE;
14475 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14476 if (eq->queue_id == 0xFFFF)
14477 status = -ENXIO;
14478 eq->host_index = 0;
14479 eq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014480 eq->entry_repost = LPFC_EQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014481
James Smart8fa38512009-07-19 10:01:03 -040014482 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014483 return status;
14484}
14485
14486/**
14487 * lpfc_cq_create - Create a Completion Queue on the HBA
14488 * @phba: HBA structure that indicates port to create a queue on.
14489 * @cq: The queue structure to use to create the completion queue.
14490 * @eq: The event queue to bind this completion queue to.
14491 *
14492 * This function creates a completion queue, as detailed in @wq, on a port,
14493 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14494 *
14495 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14496 * is used to get the entry count and entry size that are necessary to
14497 * determine the number of pages to allocate and use for this queue. The @eq
14498 * is used to indicate which event queue to bind this completion queue to. This
14499 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14500 * completion queue. This function is asynchronous and will wait for the mailbox
14501 * command to finish before continuing.
14502 *
14503 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014504 * memory this function will return -ENOMEM. If the queue create mailbox command
14505 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014506 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014507int
James Smart4f774512009-05-22 14:52:35 -040014508lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14509 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14510{
14511 struct lpfc_mbx_cq_create *cq_create;
14512 struct lpfc_dmabuf *dmabuf;
14513 LPFC_MBOXQ_t *mbox;
14514 int rc, length, status = 0;
14515 uint32_t shdr_status, shdr_add_status;
14516 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014517 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14518
James Smart2e90f4b2011-12-13 13:22:37 -050014519 /* sanity check on queue memory */
14520 if (!cq || !eq)
14521 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014522 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014523 hw_page_size = cq->page_size;
James Smart49198b32010-04-06 15:04:33 -040014524
James Smart4f774512009-05-22 14:52:35 -040014525 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14526 if (!mbox)
14527 return -ENOMEM;
14528 length = (sizeof(struct lpfc_mbx_cq_create) -
14529 sizeof(struct lpfc_sli4_cfg_mhdr));
14530 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14531 LPFC_MBOX_OPCODE_CQ_CREATE,
14532 length, LPFC_SLI4_MBX_EMBED);
14533 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014534 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014535 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14536 cq->page_count);
14537 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14538 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014539 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14540 phba->sli4_hba.pc_sli4_params.cqv);
14541 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080014542 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14543 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014544 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14545 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080014546 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14547 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050014548 } else {
14549 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14550 eq->queue_id);
14551 }
James Smart4f774512009-05-22 14:52:35 -040014552 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014553 case 2048:
14554 case 4096:
14555 if (phba->sli4_hba.pc_sli4_params.cqv ==
14556 LPFC_Q_CREATE_VERSION_2) {
14557 cq_create->u.request.context.lpfc_cq_context_count =
14558 cq->entry_count;
14559 bf_set(lpfc_cq_context_count,
14560 &cq_create->u.request.context,
14561 LPFC_CQ_CNT_WORD7);
14562 break;
14563 }
14564 /* Fall Thru */
James Smart4f774512009-05-22 14:52:35 -040014565 default:
14566 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014567 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014568 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014569 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014570 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014571 if (cq->entry_count < 256) {
14572 status = -EINVAL;
14573 goto out;
14574 }
James Smart4f774512009-05-22 14:52:35 -040014575 /* otherwise default to smallest count (drop through) */
14576 case 256:
14577 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14578 LPFC_CQ_CNT_256);
14579 break;
14580 case 512:
14581 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14582 LPFC_CQ_CNT_512);
14583 break;
14584 case 1024:
14585 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14586 LPFC_CQ_CNT_1024);
14587 break;
14588 }
14589 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080014590 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040014591 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14592 putPaddrLow(dmabuf->phys);
14593 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14594 putPaddrHigh(dmabuf->phys);
14595 }
14596 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14597
14598 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014599 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14600 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14601 if (shdr_status || shdr_add_status || rc) {
14602 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14603 "2501 CQ_CREATE mailbox failed with "
14604 "status x%x add_status x%x, mbx status x%x\n",
14605 shdr_status, shdr_add_status, rc);
14606 status = -ENXIO;
14607 goto out;
14608 }
14609 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14610 if (cq->queue_id == 0xFFFF) {
14611 status = -ENXIO;
14612 goto out;
14613 }
14614 /* link the cq onto the parent eq child list */
14615 list_add_tail(&cq->list, &eq->child_list);
14616 /* Set up completion queue's type and subtype */
14617 cq->type = type;
14618 cq->subtype = subtype;
14619 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014620 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014621 cq->host_index = 0;
14622 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014623 cq->entry_repost = LPFC_CQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014624
James Smart8fa38512009-07-19 10:01:03 -040014625out:
14626 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014627 return status;
14628}
14629
14630/**
James Smart2d7dbc42017-02-12 13:52:35 -080014631 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14632 * @phba: HBA structure that indicates port to create a queue on.
14633 * @cqp: The queue structure array to use to create the completion queues.
14634 * @eqp: The event queue array to bind these completion queues to.
14635 *
14636 * This function creates a set of completion queue, s to support MRQ
14637 * as detailed in @cqp, on a port,
14638 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14639 *
14640 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14641 * is used to get the entry count and entry size that are necessary to
14642 * determine the number of pages to allocate and use for this queue. The @eq
14643 * is used to indicate which event queue to bind this completion queue to. This
14644 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14645 * completion queue. This function is asynchronous and will wait for the mailbox
14646 * command to finish before continuing.
14647 *
14648 * On success this function will return a zero. If unable to allocate enough
14649 * memory this function will return -ENOMEM. If the queue create mailbox command
14650 * fails this function will return -ENXIO.
14651 **/
14652int
14653lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14654 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14655{
14656 struct lpfc_queue *cq;
14657 struct lpfc_queue *eq;
14658 struct lpfc_mbx_cq_create_set *cq_set;
14659 struct lpfc_dmabuf *dmabuf;
14660 LPFC_MBOXQ_t *mbox;
14661 int rc, length, alloclen, status = 0;
14662 int cnt, idx, numcq, page_idx = 0;
14663 uint32_t shdr_status, shdr_add_status;
14664 union lpfc_sli4_cfg_shdr *shdr;
14665 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14666
14667 /* sanity check on queue memory */
14668 numcq = phba->cfg_nvmet_mrq;
14669 if (!cqp || !eqp || !numcq)
14670 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080014671
14672 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14673 if (!mbox)
14674 return -ENOMEM;
14675
14676 length = sizeof(struct lpfc_mbx_cq_create_set);
14677 length += ((numcq * cqp[0]->page_count) *
14678 sizeof(struct dma_address));
14679 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14680 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14681 LPFC_SLI4_MBX_NEMBED);
14682 if (alloclen < length) {
14683 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14684 "3098 Allocated DMA memory size (%d) is "
14685 "less than the requested DMA memory size "
14686 "(%d)\n", alloclen, length);
14687 status = -ENOMEM;
14688 goto out;
14689 }
14690 cq_set = mbox->sge_array->addr[0];
14691 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14692 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14693
14694 for (idx = 0; idx < numcq; idx++) {
14695 cq = cqp[idx];
14696 eq = eqp[idx];
14697 if (!cq || !eq) {
14698 status = -ENOMEM;
14699 goto out;
14700 }
James Smart81b96ed2017-11-20 16:00:29 -080014701 if (!phba->sli4_hba.pc_sli4_params.supported)
14702 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080014703
14704 switch (idx) {
14705 case 0:
14706 bf_set(lpfc_mbx_cq_create_set_page_size,
14707 &cq_set->u.request,
14708 (hw_page_size / SLI4_PAGE_SIZE));
14709 bf_set(lpfc_mbx_cq_create_set_num_pages,
14710 &cq_set->u.request, cq->page_count);
14711 bf_set(lpfc_mbx_cq_create_set_evt,
14712 &cq_set->u.request, 1);
14713 bf_set(lpfc_mbx_cq_create_set_valid,
14714 &cq_set->u.request, 1);
14715 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14716 &cq_set->u.request, 0);
14717 bf_set(lpfc_mbx_cq_create_set_num_cq,
14718 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080014719 bf_set(lpfc_mbx_cq_create_set_autovalid,
14720 &cq_set->u.request,
14721 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080014722 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014723 case 2048:
14724 case 4096:
14725 if (phba->sli4_hba.pc_sli4_params.cqv ==
14726 LPFC_Q_CREATE_VERSION_2) {
14727 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14728 &cq_set->u.request,
14729 cq->entry_count);
14730 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14731 &cq_set->u.request,
14732 LPFC_CQ_CNT_WORD7);
14733 break;
14734 }
14735 /* Fall Thru */
James Smart2d7dbc42017-02-12 13:52:35 -080014736 default:
14737 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14738 "3118 Bad CQ count. (%d)\n",
14739 cq->entry_count);
14740 if (cq->entry_count < 256) {
14741 status = -EINVAL;
14742 goto out;
14743 }
14744 /* otherwise default to smallest (drop thru) */
14745 case 256:
14746 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14747 &cq_set->u.request, LPFC_CQ_CNT_256);
14748 break;
14749 case 512:
14750 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14751 &cq_set->u.request, LPFC_CQ_CNT_512);
14752 break;
14753 case 1024:
14754 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14755 &cq_set->u.request, LPFC_CQ_CNT_1024);
14756 break;
14757 }
14758 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14759 &cq_set->u.request, eq->queue_id);
14760 break;
14761 case 1:
14762 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14763 &cq_set->u.request, eq->queue_id);
14764 break;
14765 case 2:
14766 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14767 &cq_set->u.request, eq->queue_id);
14768 break;
14769 case 3:
14770 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14771 &cq_set->u.request, eq->queue_id);
14772 break;
14773 case 4:
14774 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14775 &cq_set->u.request, eq->queue_id);
14776 break;
14777 case 5:
14778 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14779 &cq_set->u.request, eq->queue_id);
14780 break;
14781 case 6:
14782 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14783 &cq_set->u.request, eq->queue_id);
14784 break;
14785 case 7:
14786 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14787 &cq_set->u.request, eq->queue_id);
14788 break;
14789 case 8:
14790 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14791 &cq_set->u.request, eq->queue_id);
14792 break;
14793 case 9:
14794 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14795 &cq_set->u.request, eq->queue_id);
14796 break;
14797 case 10:
14798 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14799 &cq_set->u.request, eq->queue_id);
14800 break;
14801 case 11:
14802 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14803 &cq_set->u.request, eq->queue_id);
14804 break;
14805 case 12:
14806 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14807 &cq_set->u.request, eq->queue_id);
14808 break;
14809 case 13:
14810 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14811 &cq_set->u.request, eq->queue_id);
14812 break;
14813 case 14:
14814 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14815 &cq_set->u.request, eq->queue_id);
14816 break;
14817 case 15:
14818 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14819 &cq_set->u.request, eq->queue_id);
14820 break;
14821 }
14822
14823 /* link the cq onto the parent eq child list */
14824 list_add_tail(&cq->list, &eq->child_list);
14825 /* Set up completion queue's type and subtype */
14826 cq->type = type;
14827 cq->subtype = subtype;
14828 cq->assoc_qid = eq->queue_id;
14829 cq->host_index = 0;
14830 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014831 cq->entry_repost = LPFC_CQ_REPOST;
James Smart81b96ed2017-11-20 16:00:29 -080014832 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080014833
14834 rc = 0;
14835 list_for_each_entry(dmabuf, &cq->page_list, list) {
14836 memset(dmabuf->virt, 0, hw_page_size);
14837 cnt = page_idx + dmabuf->buffer_tag;
14838 cq_set->u.request.page[cnt].addr_lo =
14839 putPaddrLow(dmabuf->phys);
14840 cq_set->u.request.page[cnt].addr_hi =
14841 putPaddrHigh(dmabuf->phys);
14842 rc++;
14843 }
14844 page_idx += rc;
14845 }
14846
14847 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14848
14849 /* The IOCTL status is embedded in the mailbox subheader. */
14850 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14851 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14852 if (shdr_status || shdr_add_status || rc) {
14853 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14854 "3119 CQ_CREATE_SET mailbox failed with "
14855 "status x%x add_status x%x, mbx status x%x\n",
14856 shdr_status, shdr_add_status, rc);
14857 status = -ENXIO;
14858 goto out;
14859 }
14860 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14861 if (rc == 0xFFFF) {
14862 status = -ENXIO;
14863 goto out;
14864 }
14865
14866 for (idx = 0; idx < numcq; idx++) {
14867 cq = cqp[idx];
14868 cq->queue_id = rc + idx;
14869 }
14870
14871out:
14872 lpfc_sli4_mbox_cmd_free(phba, mbox);
14873 return status;
14874}
14875
14876/**
James Smartb19a0612010-04-06 14:48:51 -040014877 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040014878 * @phba: HBA structure that indicates port to create a queue on.
14879 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040014880 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14881 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040014882 *
James Smartb19a0612010-04-06 14:48:51 -040014883 * This function provides failback (fb) functionality when the
14884 * mq_create_ext fails on older FW generations. It's purpose is identical
14885 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040014886 *
James Smartb19a0612010-04-06 14:48:51 -040014887 * This routine cannot fail as all attributes were previously accessed and
14888 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040014889 **/
James Smartb19a0612010-04-06 14:48:51 -040014890static void
14891lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14892 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040014893{
14894 struct lpfc_mbx_mq_create *mq_create;
14895 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040014896 int length;
James Smart04c68492009-05-22 14:52:52 -040014897
James Smart04c68492009-05-22 14:52:52 -040014898 length = (sizeof(struct lpfc_mbx_mq_create) -
14899 sizeof(struct lpfc_sli4_cfg_mhdr));
14900 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14901 LPFC_MBOX_OPCODE_MQ_CREATE,
14902 length, LPFC_SLI4_MBX_EMBED);
14903 mq_create = &mbox->u.mqe.un.mq_create;
14904 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040014905 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040014906 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040014907 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040014908 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14909 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040014910 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014911 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14912 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040014913 break;
14914 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014915 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14916 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040014917 break;
14918 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014919 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14920 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040014921 break;
14922 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014923 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14924 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040014925 break;
14926 }
14927 list_for_each_entry(dmabuf, &mq->page_list, list) {
14928 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040014929 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040014930 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040014931 putPaddrHigh(dmabuf->phys);
14932 }
14933}
14934
14935/**
14936 * lpfc_mq_create - Create a mailbox Queue on the HBA
14937 * @phba: HBA structure that indicates port to create a queue on.
14938 * @mq: The queue structure to use to create the mailbox queue.
14939 * @cq: The completion queue to associate with this cq.
14940 * @subtype: The queue's subtype.
14941 *
14942 * This function creates a mailbox queue, as detailed in @mq, on a port,
14943 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14944 *
14945 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14946 * is used to get the entry count and entry size that are necessary to
14947 * determine the number of pages to allocate and use for this queue. This
14948 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14949 * mailbox queue. This function is asynchronous and will wait for the mailbox
14950 * command to finish before continuing.
14951 *
14952 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014953 * memory this function will return -ENOMEM. If the queue create mailbox command
14954 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040014955 **/
14956int32_t
14957lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14958 struct lpfc_queue *cq, uint32_t subtype)
14959{
14960 struct lpfc_mbx_mq_create *mq_create;
14961 struct lpfc_mbx_mq_create_ext *mq_create_ext;
14962 struct lpfc_dmabuf *dmabuf;
14963 LPFC_MBOXQ_t *mbox;
14964 int rc, length, status = 0;
14965 uint32_t shdr_status, shdr_add_status;
14966 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014967 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040014968
James Smart2e90f4b2011-12-13 13:22:37 -050014969 /* sanity check on queue memory */
14970 if (!mq || !cq)
14971 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014972 if (!phba->sli4_hba.pc_sli4_params.supported)
14973 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040014974
14975 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14976 if (!mbox)
14977 return -ENOMEM;
14978 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
14979 sizeof(struct lpfc_sli4_cfg_mhdr));
14980 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14981 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
14982 length, LPFC_SLI4_MBX_EMBED);
14983
14984 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050014985 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050014986 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14987 &mq_create_ext->u.request, mq->page_count);
14988 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14989 &mq_create_ext->u.request, 1);
14990 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040014991 &mq_create_ext->u.request, 1);
14992 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14993 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050014994 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14995 &mq_create_ext->u.request, 1);
14996 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14997 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040014998 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014999 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15000 phba->sli4_hba.pc_sli4_params.mqv);
15001 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15002 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15003 cq->queue_id);
15004 else
15005 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15006 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040015007 switch (mq->entry_count) {
15008 default:
15009 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15010 "0362 Unsupported MQ count. (%d)\n",
15011 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015012 if (mq->entry_count < 16) {
15013 status = -EINVAL;
15014 goto out;
15015 }
James Smartb19a0612010-04-06 14:48:51 -040015016 /* otherwise default to smallest count (drop through) */
15017 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015018 bf_set(lpfc_mq_context_ring_size,
15019 &mq_create_ext->u.request.context,
15020 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040015021 break;
15022 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015023 bf_set(lpfc_mq_context_ring_size,
15024 &mq_create_ext->u.request.context,
15025 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040015026 break;
15027 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015028 bf_set(lpfc_mq_context_ring_size,
15029 &mq_create_ext->u.request.context,
15030 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040015031 break;
15032 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015033 bf_set(lpfc_mq_context_ring_size,
15034 &mq_create_ext->u.request.context,
15035 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040015036 break;
15037 }
15038 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015039 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040015040 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15041 putPaddrLow(dmabuf->phys);
15042 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040015043 putPaddrHigh(dmabuf->phys);
15044 }
15045 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040015046 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15047 &mq_create_ext->u.response);
15048 if (rc != MBX_SUCCESS) {
15049 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15050 "2795 MQ_CREATE_EXT failed with "
15051 "status x%x. Failback to MQ_CREATE.\n",
15052 rc);
15053 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15054 mq_create = &mbox->u.mqe.un.mq_create;
15055 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15056 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15057 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15058 &mq_create->u.response);
15059 }
15060
James Smart04c68492009-05-22 14:52:52 -040015061 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040015062 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15063 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15064 if (shdr_status || shdr_add_status || rc) {
15065 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15066 "2502 MQ_CREATE mailbox failed with "
15067 "status x%x add_status x%x, mbx status x%x\n",
15068 shdr_status, shdr_add_status, rc);
15069 status = -ENXIO;
15070 goto out;
15071 }
James Smart04c68492009-05-22 14:52:52 -040015072 if (mq->queue_id == 0xFFFF) {
15073 status = -ENXIO;
15074 goto out;
15075 }
15076 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050015077 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040015078 mq->subtype = subtype;
15079 mq->host_index = 0;
15080 mq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070015081 mq->entry_repost = LPFC_MQ_REPOST;
James Smart04c68492009-05-22 14:52:52 -040015082
15083 /* link the mq onto the parent cq child list */
15084 list_add_tail(&mq->list, &cq->child_list);
15085out:
James Smart8fa38512009-07-19 10:01:03 -040015086 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015087 return status;
15088}
15089
15090/**
James Smart4f774512009-05-22 14:52:35 -040015091 * lpfc_wq_create - Create a Work Queue on the HBA
15092 * @phba: HBA structure that indicates port to create a queue on.
15093 * @wq: The queue structure to use to create the work queue.
15094 * @cq: The completion queue to bind this work queue to.
15095 * @subtype: The subtype of the work queue indicating its functionality.
15096 *
15097 * This function creates a work queue, as detailed in @wq, on a port, described
15098 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15099 *
15100 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15101 * is used to get the entry count and entry size that are necessary to
15102 * determine the number of pages to allocate and use for this queue. The @cq
15103 * is used to indicate which completion queue to bind this work queue to. This
15104 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15105 * work queue. This function is asynchronous and will wait for the mailbox
15106 * command to finish before continuing.
15107 *
15108 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015109 * memory this function will return -ENOMEM. If the queue create mailbox command
15110 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015111 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015112int
James Smart4f774512009-05-22 14:52:35 -040015113lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15114 struct lpfc_queue *cq, uint32_t subtype)
15115{
15116 struct lpfc_mbx_wq_create *wq_create;
15117 struct lpfc_dmabuf *dmabuf;
15118 LPFC_MBOXQ_t *mbox;
15119 int rc, length, status = 0;
15120 uint32_t shdr_status, shdr_add_status;
15121 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015122 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050015123 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050015124 void __iomem *bar_memmap_p;
15125 uint32_t db_offset;
15126 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080015127 uint8_t dpp_barset;
15128 uint32_t dpp_offset;
15129 unsigned long pg_addr;
James Smart81b96ed2017-11-20 16:00:29 -080015130 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040015131
James Smart2e90f4b2011-12-13 13:22:37 -050015132 /* sanity check on queue memory */
15133 if (!wq || !cq)
15134 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015135 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015136 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040015137
15138 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15139 if (!mbox)
15140 return -ENOMEM;
15141 length = (sizeof(struct lpfc_mbx_wq_create) -
15142 sizeof(struct lpfc_sli4_cfg_mhdr));
15143 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15144 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15145 length, LPFC_SLI4_MBX_EMBED);
15146 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015147 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015148 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15149 wq->page_count);
15150 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15151 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040015152
15153 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050015154 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15155 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050015156
James Smartc176ffa2018-01-30 15:58:46 -080015157 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15158 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080015159 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15160 else
15161 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15162
James Smart0c651872013-07-15 18:33:23 -040015163
James Smart1351e692018-02-22 08:18:43 -080015164 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15165 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15166 else
15167 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15168
15169 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040015170 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050015171 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15172 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070015173 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15174 LPFC_Q_CREATE_VERSION_1);
15175
James Smart5a6f1332011-03-11 16:05:35 -050015176 switch (wq->entry_size) {
15177 default:
15178 case 64:
15179 bf_set(lpfc_mbx_wq_create_wqe_size,
15180 &wq_create->u.request_1,
15181 LPFC_WQ_WQE_SIZE_64);
15182 break;
15183 case 128:
15184 bf_set(lpfc_mbx_wq_create_wqe_size,
15185 &wq_create->u.request_1,
15186 LPFC_WQ_WQE_SIZE_128);
15187 break;
15188 }
James Smart1351e692018-02-22 08:18:43 -080015189 /* Request DPP by default */
15190 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080015191 bf_set(lpfc_mbx_wq_create_page_size,
15192 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015193 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015194 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015195 break;
15196 default:
James Smart1351e692018-02-22 08:18:43 -080015197 page = wq_create->u.request.page;
15198 break;
James Smart5a6f1332011-03-11 16:05:35 -050015199 }
James Smart0c651872013-07-15 18:33:23 -040015200
James Smart4f774512009-05-22 14:52:35 -040015201 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015202 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015203 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15204 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015205 }
James Smart962bc512013-01-03 15:44:00 -050015206
15207 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15208 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15209
James Smart4f774512009-05-22 14:52:35 -040015210 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15211 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015212 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15213 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15214 if (shdr_status || shdr_add_status || rc) {
15215 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15216 "2503 WQ_CREATE mailbox failed with "
15217 "status x%x add_status x%x, mbx status x%x\n",
15218 shdr_status, shdr_add_status, rc);
15219 status = -ENXIO;
15220 goto out;
15221 }
James Smart1351e692018-02-22 08:18:43 -080015222
15223 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15224 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15225 &wq_create->u.response);
15226 else
15227 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15228 &wq_create->u.response_1);
15229
James Smart4f774512009-05-22 14:52:35 -040015230 if (wq->queue_id == 0xFFFF) {
15231 status = -ENXIO;
15232 goto out;
15233 }
James Smart1351e692018-02-22 08:18:43 -080015234
15235 wq->db_format = LPFC_DB_LIST_FORMAT;
15236 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15237 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15238 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15239 &wq_create->u.response);
15240 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15241 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15242 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15243 "3265 WQ[%d] doorbell format "
15244 "not supported: x%x\n",
15245 wq->queue_id, wq->db_format);
15246 status = -EINVAL;
15247 goto out;
15248 }
15249 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15250 &wq_create->u.response);
15251 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15252 pci_barset);
15253 if (!bar_memmap_p) {
15254 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15255 "3263 WQ[%d] failed to memmap "
15256 "pci barset:x%x\n",
15257 wq->queue_id, pci_barset);
15258 status = -ENOMEM;
15259 goto out;
15260 }
15261 db_offset = wq_create->u.response.doorbell_offset;
15262 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15263 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15264 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15265 "3252 WQ[%d] doorbell offset "
15266 "not supported: x%x\n",
15267 wq->queue_id, db_offset);
15268 status = -EINVAL;
15269 goto out;
15270 }
15271 wq->db_regaddr = bar_memmap_p + db_offset;
15272 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15273 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15274 "format:x%x\n", wq->queue_id,
15275 pci_barset, db_offset, wq->db_format);
15276 } else
15277 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015278 } else {
James Smart1351e692018-02-22 08:18:43 -080015279 /* Check if DPP was honored by the firmware */
15280 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15281 &wq_create->u.response_1);
15282 if (wq->dpp_enable) {
15283 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15284 &wq_create->u.response_1);
15285 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15286 pci_barset);
15287 if (!bar_memmap_p) {
15288 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15289 "3267 WQ[%d] failed to memmap "
15290 "pci barset:x%x\n",
15291 wq->queue_id, pci_barset);
15292 status = -ENOMEM;
15293 goto out;
15294 }
15295 db_offset = wq_create->u.response_1.doorbell_offset;
15296 wq->db_regaddr = bar_memmap_p + db_offset;
15297 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15298 &wq_create->u.response_1);
15299 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15300 &wq_create->u.response_1);
15301 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15302 dpp_barset);
15303 if (!bar_memmap_p) {
15304 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15305 "3268 WQ[%d] failed to memmap "
15306 "pci barset:x%x\n",
15307 wq->queue_id, dpp_barset);
15308 status = -ENOMEM;
15309 goto out;
15310 }
15311 dpp_offset = wq_create->u.response_1.dpp_offset;
15312 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15313 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15314 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15315 "dpp_id:x%x dpp_barset:x%x "
15316 "dpp_offset:x%x\n",
15317 wq->queue_id, pci_barset, db_offset,
15318 wq->dpp_id, dpp_barset, dpp_offset);
15319
15320 /* Enable combined writes for DPP aperture */
15321 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15322#ifdef CONFIG_X86
15323 rc = set_memory_wc(pg_addr, 1);
15324 if (rc) {
15325 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15326 "3272 Cannot setup Combined "
15327 "Write on WQ[%d] - disable DPP\n",
15328 wq->queue_id);
15329 phba->cfg_enable_dpp = 0;
15330 }
15331#else
15332 phba->cfg_enable_dpp = 0;
15333#endif
15334 } else
15335 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015336 }
James Smart895427b2017-02-12 13:52:30 -080015337 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15338 if (wq->pring == NULL) {
15339 status = -ENOMEM;
15340 goto out;
15341 }
James Smart4f774512009-05-22 14:52:35 -040015342 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015343 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015344 wq->subtype = subtype;
15345 wq->host_index = 0;
15346 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050015347 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040015348
15349 /* link the wq onto the parent cq child list */
15350 list_add_tail(&wq->list, &cq->child_list);
15351out:
James Smart8fa38512009-07-19 10:01:03 -040015352 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015353 return status;
15354}
15355
15356/**
15357 * lpfc_rq_create - Create a Receive Queue on the HBA
15358 * @phba: HBA structure that indicates port to create a queue on.
15359 * @hrq: The queue structure to use to create the header receive queue.
15360 * @drq: The queue structure to use to create the data receive queue.
15361 * @cq: The completion queue to bind this work queue to.
15362 *
15363 * This function creates a receive buffer queue pair , as detailed in @hrq and
15364 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15365 * to the HBA.
15366 *
15367 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15368 * struct is used to get the entry count that is necessary to determine the
15369 * number of pages to use for this queue. The @cq is used to indicate which
15370 * completion queue to bind received buffers that are posted to these queues to.
15371 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15372 * receive queue pair. This function is asynchronous and will wait for the
15373 * mailbox command to finish before continuing.
15374 *
15375 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015376 * memory this function will return -ENOMEM. If the queue create mailbox command
15377 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015378 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015379int
James Smart4f774512009-05-22 14:52:35 -040015380lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15381 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15382{
15383 struct lpfc_mbx_rq_create *rq_create;
15384 struct lpfc_dmabuf *dmabuf;
15385 LPFC_MBOXQ_t *mbox;
15386 int rc, length, status = 0;
15387 uint32_t shdr_status, shdr_add_status;
15388 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015389 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015390 void __iomem *bar_memmap_p;
15391 uint32_t db_offset;
15392 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015393
James Smart2e90f4b2011-12-13 13:22:37 -050015394 /* sanity check on queue memory */
15395 if (!hrq || !drq || !cq)
15396 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015397 if (!phba->sli4_hba.pc_sli4_params.supported)
15398 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015399
15400 if (hrq->entry_count != drq->entry_count)
15401 return -EINVAL;
15402 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15403 if (!mbox)
15404 return -ENOMEM;
15405 length = (sizeof(struct lpfc_mbx_rq_create) -
15406 sizeof(struct lpfc_sli4_cfg_mhdr));
15407 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15408 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15409 length, LPFC_SLI4_MBX_EMBED);
15410 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015411 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15412 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15413 phba->sli4_hba.pc_sli4_params.rqv);
15414 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15415 bf_set(lpfc_rq_context_rqe_count_1,
15416 &rq_create->u.request.context,
15417 hrq->entry_count);
15418 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015419 bf_set(lpfc_rq_context_rqe_size,
15420 &rq_create->u.request.context,
15421 LPFC_RQE_SIZE_8);
15422 bf_set(lpfc_rq_context_page_size,
15423 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015424 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015425 } else {
15426 switch (hrq->entry_count) {
15427 default:
15428 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15429 "2535 Unsupported RQ count. (%d)\n",
15430 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015431 if (hrq->entry_count < 512) {
15432 status = -EINVAL;
15433 goto out;
15434 }
James Smart5a6f1332011-03-11 16:05:35 -050015435 /* otherwise default to smallest count (drop through) */
15436 case 512:
15437 bf_set(lpfc_rq_context_rqe_count,
15438 &rq_create->u.request.context,
15439 LPFC_RQ_RING_SIZE_512);
15440 break;
15441 case 1024:
15442 bf_set(lpfc_rq_context_rqe_count,
15443 &rq_create->u.request.context,
15444 LPFC_RQ_RING_SIZE_1024);
15445 break;
15446 case 2048:
15447 bf_set(lpfc_rq_context_rqe_count,
15448 &rq_create->u.request.context,
15449 LPFC_RQ_RING_SIZE_2048);
15450 break;
15451 case 4096:
15452 bf_set(lpfc_rq_context_rqe_count,
15453 &rq_create->u.request.context,
15454 LPFC_RQ_RING_SIZE_4096);
15455 break;
15456 }
15457 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15458 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015459 }
15460 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15461 cq->queue_id);
15462 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15463 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015464 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015465 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015466 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15467 putPaddrLow(dmabuf->phys);
15468 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15469 putPaddrHigh(dmabuf->phys);
15470 }
James Smart962bc512013-01-03 15:44:00 -050015471 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15472 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15473
James Smart4f774512009-05-22 14:52:35 -040015474 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15475 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015476 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15477 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15478 if (shdr_status || shdr_add_status || rc) {
15479 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15480 "2504 RQ_CREATE mailbox failed with "
15481 "status x%x add_status x%x, mbx status x%x\n",
15482 shdr_status, shdr_add_status, rc);
15483 status = -ENXIO;
15484 goto out;
15485 }
15486 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15487 if (hrq->queue_id == 0xFFFF) {
15488 status = -ENXIO;
15489 goto out;
15490 }
James Smart962bc512013-01-03 15:44:00 -050015491
15492 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15493 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15494 &rq_create->u.response);
15495 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15496 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15498 "3262 RQ [%d] doorbell format not "
15499 "supported: x%x\n", hrq->queue_id,
15500 hrq->db_format);
15501 status = -EINVAL;
15502 goto out;
15503 }
15504
15505 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15506 &rq_create->u.response);
15507 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15508 if (!bar_memmap_p) {
15509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15510 "3269 RQ[%d] failed to memmap pci "
15511 "barset:x%x\n", hrq->queue_id,
15512 pci_barset);
15513 status = -ENOMEM;
15514 goto out;
15515 }
15516
15517 db_offset = rq_create->u.response.doorbell_offset;
15518 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15519 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15520 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15521 "3270 RQ[%d] doorbell offset not "
15522 "supported: x%x\n", hrq->queue_id,
15523 db_offset);
15524 status = -EINVAL;
15525 goto out;
15526 }
15527 hrq->db_regaddr = bar_memmap_p + db_offset;
15528 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015529 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15530 "format:x%x\n", hrq->queue_id, pci_barset,
15531 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015532 } else {
15533 hrq->db_format = LPFC_DB_RING_FORMAT;
15534 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15535 }
James Smart4f774512009-05-22 14:52:35 -040015536 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015537 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015538 hrq->subtype = subtype;
15539 hrq->host_index = 0;
15540 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015541 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015542
15543 /* now create the data queue */
15544 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15545 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15546 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015547 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15548 phba->sli4_hba.pc_sli4_params.rqv);
15549 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15550 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015551 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015552 if (subtype == LPFC_NVMET)
15553 rq_create->u.request.context.buffer_size =
15554 LPFC_NVMET_DATA_BUF_SIZE;
15555 else
15556 rq_create->u.request.context.buffer_size =
15557 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015558 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15559 LPFC_RQE_SIZE_8);
15560 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15561 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015562 } else {
15563 switch (drq->entry_count) {
15564 default:
15565 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15566 "2536 Unsupported RQ count. (%d)\n",
15567 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015568 if (drq->entry_count < 512) {
15569 status = -EINVAL;
15570 goto out;
15571 }
James Smart5a6f1332011-03-11 16:05:35 -050015572 /* otherwise default to smallest count (drop through) */
15573 case 512:
15574 bf_set(lpfc_rq_context_rqe_count,
15575 &rq_create->u.request.context,
15576 LPFC_RQ_RING_SIZE_512);
15577 break;
15578 case 1024:
15579 bf_set(lpfc_rq_context_rqe_count,
15580 &rq_create->u.request.context,
15581 LPFC_RQ_RING_SIZE_1024);
15582 break;
15583 case 2048:
15584 bf_set(lpfc_rq_context_rqe_count,
15585 &rq_create->u.request.context,
15586 LPFC_RQ_RING_SIZE_2048);
15587 break;
15588 case 4096:
15589 bf_set(lpfc_rq_context_rqe_count,
15590 &rq_create->u.request.context,
15591 LPFC_RQ_RING_SIZE_4096);
15592 break;
15593 }
James Smart3c603be2017-05-15 15:20:44 -070015594 if (subtype == LPFC_NVMET)
15595 bf_set(lpfc_rq_context_buf_size,
15596 &rq_create->u.request.context,
15597 LPFC_NVMET_DATA_BUF_SIZE);
15598 else
15599 bf_set(lpfc_rq_context_buf_size,
15600 &rq_create->u.request.context,
15601 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015602 }
15603 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15604 cq->queue_id);
15605 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15606 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015607 list_for_each_entry(dmabuf, &drq->page_list, list) {
15608 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15609 putPaddrLow(dmabuf->phys);
15610 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15611 putPaddrHigh(dmabuf->phys);
15612 }
James Smart962bc512013-01-03 15:44:00 -050015613 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15614 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015615 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15616 /* The IOCTL status is embedded in the mailbox subheader. */
15617 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15618 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15619 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15620 if (shdr_status || shdr_add_status || rc) {
15621 status = -ENXIO;
15622 goto out;
15623 }
15624 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15625 if (drq->queue_id == 0xFFFF) {
15626 status = -ENXIO;
15627 goto out;
15628 }
15629 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015630 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015631 drq->subtype = subtype;
15632 drq->host_index = 0;
15633 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015634 drq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015635
15636 /* link the header and data RQs onto the parent cq child list */
15637 list_add_tail(&hrq->list, &cq->child_list);
15638 list_add_tail(&drq->list, &cq->child_list);
15639
15640out:
James Smart8fa38512009-07-19 10:01:03 -040015641 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015642 return status;
15643}
15644
15645/**
James Smart2d7dbc42017-02-12 13:52:35 -080015646 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15647 * @phba: HBA structure that indicates port to create a queue on.
15648 * @hrqp: The queue structure array to use to create the header receive queues.
15649 * @drqp: The queue structure array to use to create the data receive queues.
15650 * @cqp: The completion queue array to bind these receive queues to.
15651 *
15652 * This function creates a receive buffer queue pair , as detailed in @hrq and
15653 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15654 * to the HBA.
15655 *
15656 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15657 * struct is used to get the entry count that is necessary to determine the
15658 * number of pages to use for this queue. The @cq is used to indicate which
15659 * completion queue to bind received buffers that are posted to these queues to.
15660 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15661 * receive queue pair. This function is asynchronous and will wait for the
15662 * mailbox command to finish before continuing.
15663 *
15664 * On success this function will return a zero. If unable to allocate enough
15665 * memory this function will return -ENOMEM. If the queue create mailbox command
15666 * fails this function will return -ENXIO.
15667 **/
15668int
15669lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15670 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15671 uint32_t subtype)
15672{
15673 struct lpfc_queue *hrq, *drq, *cq;
15674 struct lpfc_mbx_rq_create_v2 *rq_create;
15675 struct lpfc_dmabuf *dmabuf;
15676 LPFC_MBOXQ_t *mbox;
15677 int rc, length, alloclen, status = 0;
15678 int cnt, idx, numrq, page_idx = 0;
15679 uint32_t shdr_status, shdr_add_status;
15680 union lpfc_sli4_cfg_shdr *shdr;
15681 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15682
15683 numrq = phba->cfg_nvmet_mrq;
15684 /* sanity check on array memory */
15685 if (!hrqp || !drqp || !cqp || !numrq)
15686 return -ENODEV;
15687 if (!phba->sli4_hba.pc_sli4_params.supported)
15688 hw_page_size = SLI4_PAGE_SIZE;
15689
15690 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15691 if (!mbox)
15692 return -ENOMEM;
15693
15694 length = sizeof(struct lpfc_mbx_rq_create_v2);
15695 length += ((2 * numrq * hrqp[0]->page_count) *
15696 sizeof(struct dma_address));
15697
15698 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15699 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15700 LPFC_SLI4_MBX_NEMBED);
15701 if (alloclen < length) {
15702 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15703 "3099 Allocated DMA memory size (%d) is "
15704 "less than the requested DMA memory size "
15705 "(%d)\n", alloclen, length);
15706 status = -ENOMEM;
15707 goto out;
15708 }
15709
15710
15711
15712 rq_create = mbox->sge_array->addr[0];
15713 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15714
15715 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15716 cnt = 0;
15717
15718 for (idx = 0; idx < numrq; idx++) {
15719 hrq = hrqp[idx];
15720 drq = drqp[idx];
15721 cq = cqp[idx];
15722
James Smart2d7dbc42017-02-12 13:52:35 -080015723 /* sanity check on queue memory */
15724 if (!hrq || !drq || !cq) {
15725 status = -ENODEV;
15726 goto out;
15727 }
15728
James Smart7aabe842017-03-04 09:30:22 -080015729 if (hrq->entry_count != drq->entry_count) {
15730 status = -EINVAL;
15731 goto out;
15732 }
15733
James Smart2d7dbc42017-02-12 13:52:35 -080015734 if (idx == 0) {
15735 bf_set(lpfc_mbx_rq_create_num_pages,
15736 &rq_create->u.request,
15737 hrq->page_count);
15738 bf_set(lpfc_mbx_rq_create_rq_cnt,
15739 &rq_create->u.request, (numrq * 2));
15740 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15741 1);
15742 bf_set(lpfc_rq_context_base_cq,
15743 &rq_create->u.request.context,
15744 cq->queue_id);
15745 bf_set(lpfc_rq_context_data_size,
15746 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015747 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015748 bf_set(lpfc_rq_context_hdr_size,
15749 &rq_create->u.request.context,
15750 LPFC_HDR_BUF_SIZE);
15751 bf_set(lpfc_rq_context_rqe_count_1,
15752 &rq_create->u.request.context,
15753 hrq->entry_count);
15754 bf_set(lpfc_rq_context_rqe_size,
15755 &rq_create->u.request.context,
15756 LPFC_RQE_SIZE_8);
15757 bf_set(lpfc_rq_context_page_size,
15758 &rq_create->u.request.context,
15759 (PAGE_SIZE/SLI4_PAGE_SIZE));
15760 }
15761 rc = 0;
15762 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15763 memset(dmabuf->virt, 0, hw_page_size);
15764 cnt = page_idx + dmabuf->buffer_tag;
15765 rq_create->u.request.page[cnt].addr_lo =
15766 putPaddrLow(dmabuf->phys);
15767 rq_create->u.request.page[cnt].addr_hi =
15768 putPaddrHigh(dmabuf->phys);
15769 rc++;
15770 }
15771 page_idx += rc;
15772
15773 rc = 0;
15774 list_for_each_entry(dmabuf, &drq->page_list, list) {
15775 memset(dmabuf->virt, 0, hw_page_size);
15776 cnt = page_idx + dmabuf->buffer_tag;
15777 rq_create->u.request.page[cnt].addr_lo =
15778 putPaddrLow(dmabuf->phys);
15779 rq_create->u.request.page[cnt].addr_hi =
15780 putPaddrHigh(dmabuf->phys);
15781 rc++;
15782 }
15783 page_idx += rc;
15784
15785 hrq->db_format = LPFC_DB_RING_FORMAT;
15786 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15787 hrq->type = LPFC_HRQ;
15788 hrq->assoc_qid = cq->queue_id;
15789 hrq->subtype = subtype;
15790 hrq->host_index = 0;
15791 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015792 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015793
15794 drq->db_format = LPFC_DB_RING_FORMAT;
15795 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15796 drq->type = LPFC_DRQ;
15797 drq->assoc_qid = cq->queue_id;
15798 drq->subtype = subtype;
15799 drq->host_index = 0;
15800 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015801 drq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015802
15803 list_add_tail(&hrq->list, &cq->child_list);
15804 list_add_tail(&drq->list, &cq->child_list);
15805 }
15806
15807 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15808 /* The IOCTL status is embedded in the mailbox subheader. */
15809 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15810 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15811 if (shdr_status || shdr_add_status || rc) {
15812 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15813 "3120 RQ_CREATE mailbox failed with "
15814 "status x%x add_status x%x, mbx status x%x\n",
15815 shdr_status, shdr_add_status, rc);
15816 status = -ENXIO;
15817 goto out;
15818 }
15819 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15820 if (rc == 0xFFFF) {
15821 status = -ENXIO;
15822 goto out;
15823 }
15824
15825 /* Initialize all RQs with associated queue id */
15826 for (idx = 0; idx < numrq; idx++) {
15827 hrq = hrqp[idx];
15828 hrq->queue_id = rc + (2 * idx);
15829 drq = drqp[idx];
15830 drq->queue_id = rc + (2 * idx) + 1;
15831 }
15832
15833out:
15834 lpfc_sli4_mbox_cmd_free(phba, mbox);
15835 return status;
15836}
15837
15838/**
James Smart4f774512009-05-22 14:52:35 -040015839 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15840 * @eq: The queue structure associated with the queue to destroy.
15841 *
15842 * This function destroys a queue, as detailed in @eq by sending an mailbox
15843 * command, specific to the type of queue, to the HBA.
15844 *
15845 * The @eq struct is used to get the queue ID of the queue to destroy.
15846 *
15847 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015848 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015849 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015850int
James Smart4f774512009-05-22 14:52:35 -040015851lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15852{
15853 LPFC_MBOXQ_t *mbox;
15854 int rc, length, status = 0;
15855 uint32_t shdr_status, shdr_add_status;
15856 union lpfc_sli4_cfg_shdr *shdr;
15857
James Smart2e90f4b2011-12-13 13:22:37 -050015858 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015859 if (!eq)
15860 return -ENODEV;
15861 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15862 if (!mbox)
15863 return -ENOMEM;
15864 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15865 sizeof(struct lpfc_sli4_cfg_mhdr));
15866 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15867 LPFC_MBOX_OPCODE_EQ_DESTROY,
15868 length, LPFC_SLI4_MBX_EMBED);
15869 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15870 eq->queue_id);
15871 mbox->vport = eq->phba->pport;
15872 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15873
15874 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15875 /* The IOCTL status is embedded in the mailbox subheader. */
15876 shdr = (union lpfc_sli4_cfg_shdr *)
15877 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15878 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15879 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15880 if (shdr_status || shdr_add_status || rc) {
15881 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15882 "2505 EQ_DESTROY mailbox failed with "
15883 "status x%x add_status x%x, mbx status x%x\n",
15884 shdr_status, shdr_add_status, rc);
15885 status = -ENXIO;
15886 }
15887
15888 /* Remove eq from any list */
15889 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040015890 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015891 return status;
15892}
15893
15894/**
15895 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15896 * @cq: The queue structure associated with the queue to destroy.
15897 *
15898 * This function destroys a queue, as detailed in @cq by sending an mailbox
15899 * command, specific to the type of queue, to the HBA.
15900 *
15901 * The @cq struct is used to get the queue ID of the queue to destroy.
15902 *
15903 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015904 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015905 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015906int
James Smart4f774512009-05-22 14:52:35 -040015907lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15908{
15909 LPFC_MBOXQ_t *mbox;
15910 int rc, length, status = 0;
15911 uint32_t shdr_status, shdr_add_status;
15912 union lpfc_sli4_cfg_shdr *shdr;
15913
James Smart2e90f4b2011-12-13 13:22:37 -050015914 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015915 if (!cq)
15916 return -ENODEV;
15917 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15918 if (!mbox)
15919 return -ENOMEM;
15920 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15921 sizeof(struct lpfc_sli4_cfg_mhdr));
15922 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15923 LPFC_MBOX_OPCODE_CQ_DESTROY,
15924 length, LPFC_SLI4_MBX_EMBED);
15925 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15926 cq->queue_id);
15927 mbox->vport = cq->phba->pport;
15928 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15929 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15930 /* The IOCTL status is embedded in the mailbox subheader. */
15931 shdr = (union lpfc_sli4_cfg_shdr *)
15932 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15933 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15934 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15935 if (shdr_status || shdr_add_status || rc) {
15936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15937 "2506 CQ_DESTROY mailbox failed with "
15938 "status x%x add_status x%x, mbx status x%x\n",
15939 shdr_status, shdr_add_status, rc);
15940 status = -ENXIO;
15941 }
15942 /* Remove cq from any list */
15943 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040015944 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015945 return status;
15946}
15947
15948/**
James Smart04c68492009-05-22 14:52:52 -040015949 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15950 * @qm: The queue structure associated with the queue to destroy.
15951 *
15952 * This function destroys a queue, as detailed in @mq by sending an mailbox
15953 * command, specific to the type of queue, to the HBA.
15954 *
15955 * The @mq struct is used to get the queue ID of the queue to destroy.
15956 *
15957 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015958 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040015959 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015960int
James Smart04c68492009-05-22 14:52:52 -040015961lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15962{
15963 LPFC_MBOXQ_t *mbox;
15964 int rc, length, status = 0;
15965 uint32_t shdr_status, shdr_add_status;
15966 union lpfc_sli4_cfg_shdr *shdr;
15967
James Smart2e90f4b2011-12-13 13:22:37 -050015968 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040015969 if (!mq)
15970 return -ENODEV;
15971 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15972 if (!mbox)
15973 return -ENOMEM;
15974 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15975 sizeof(struct lpfc_sli4_cfg_mhdr));
15976 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15977 LPFC_MBOX_OPCODE_MQ_DESTROY,
15978 length, LPFC_SLI4_MBX_EMBED);
15979 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15980 mq->queue_id);
15981 mbox->vport = mq->phba->pport;
15982 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15983 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15984 /* The IOCTL status is embedded in the mailbox subheader. */
15985 shdr = (union lpfc_sli4_cfg_shdr *)
15986 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15987 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15988 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15989 if (shdr_status || shdr_add_status || rc) {
15990 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15991 "2507 MQ_DESTROY mailbox failed with "
15992 "status x%x add_status x%x, mbx status x%x\n",
15993 shdr_status, shdr_add_status, rc);
15994 status = -ENXIO;
15995 }
15996 /* Remove mq from any list */
15997 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040015998 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015999 return status;
16000}
16001
16002/**
James Smart4f774512009-05-22 14:52:35 -040016003 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16004 * @wq: The queue structure associated with the queue to destroy.
16005 *
16006 * This function destroys a queue, as detailed in @wq by sending an mailbox
16007 * command, specific to the type of queue, to the HBA.
16008 *
16009 * The @wq struct is used to get the queue ID of the queue to destroy.
16010 *
16011 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016012 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016013 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016014int
James Smart4f774512009-05-22 14:52:35 -040016015lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16016{
16017 LPFC_MBOXQ_t *mbox;
16018 int rc, length, status = 0;
16019 uint32_t shdr_status, shdr_add_status;
16020 union lpfc_sli4_cfg_shdr *shdr;
16021
James Smart2e90f4b2011-12-13 13:22:37 -050016022 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016023 if (!wq)
16024 return -ENODEV;
16025 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16026 if (!mbox)
16027 return -ENOMEM;
16028 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16029 sizeof(struct lpfc_sli4_cfg_mhdr));
16030 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16031 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16032 length, LPFC_SLI4_MBX_EMBED);
16033 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16034 wq->queue_id);
16035 mbox->vport = wq->phba->pport;
16036 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16037 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16038 shdr = (union lpfc_sli4_cfg_shdr *)
16039 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16040 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16041 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16042 if (shdr_status || shdr_add_status || rc) {
16043 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16044 "2508 WQ_DESTROY mailbox failed with "
16045 "status x%x add_status x%x, mbx status x%x\n",
16046 shdr_status, shdr_add_status, rc);
16047 status = -ENXIO;
16048 }
16049 /* Remove wq from any list */
16050 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070016051 kfree(wq->pring);
16052 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040016053 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016054 return status;
16055}
16056
16057/**
16058 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16059 * @rq: The queue structure associated with the queue to destroy.
16060 *
16061 * This function destroys a queue, as detailed in @rq by sending an mailbox
16062 * command, specific to the type of queue, to the HBA.
16063 *
16064 * The @rq struct is used to get the queue ID of the queue to destroy.
16065 *
16066 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016067 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016068 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016069int
James Smart4f774512009-05-22 14:52:35 -040016070lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16071 struct lpfc_queue *drq)
16072{
16073 LPFC_MBOXQ_t *mbox;
16074 int rc, length, status = 0;
16075 uint32_t shdr_status, shdr_add_status;
16076 union lpfc_sli4_cfg_shdr *shdr;
16077
James Smart2e90f4b2011-12-13 13:22:37 -050016078 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016079 if (!hrq || !drq)
16080 return -ENODEV;
16081 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16082 if (!mbox)
16083 return -ENOMEM;
16084 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050016085 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040016086 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16087 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16088 length, LPFC_SLI4_MBX_EMBED);
16089 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16090 hrq->queue_id);
16091 mbox->vport = hrq->phba->pport;
16092 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16093 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16094 /* The IOCTL status is embedded in the mailbox subheader. */
16095 shdr = (union lpfc_sli4_cfg_shdr *)
16096 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16097 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16098 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16099 if (shdr_status || shdr_add_status || rc) {
16100 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16101 "2509 RQ_DESTROY mailbox failed with "
16102 "status x%x add_status x%x, mbx status x%x\n",
16103 shdr_status, shdr_add_status, rc);
16104 if (rc != MBX_TIMEOUT)
16105 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16106 return -ENXIO;
16107 }
16108 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16109 drq->queue_id);
16110 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16111 shdr = (union lpfc_sli4_cfg_shdr *)
16112 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16113 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16114 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16115 if (shdr_status || shdr_add_status || rc) {
16116 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16117 "2510 RQ_DESTROY mailbox failed with "
16118 "status x%x add_status x%x, mbx status x%x\n",
16119 shdr_status, shdr_add_status, rc);
16120 status = -ENXIO;
16121 }
16122 list_del_init(&hrq->list);
16123 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040016124 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016125 return status;
16126}
16127
16128/**
16129 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16130 * @phba: The virtual port for which this call being executed.
16131 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16132 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16133 * @xritag: the xritag that ties this io to the SGL pages.
16134 *
16135 * This routine will post the sgl pages for the IO that has the xritag
16136 * that is in the iocbq structure. The xritag is assigned during iocbq
16137 * creation and persists for as long as the driver is loaded.
16138 * if the caller has fewer than 256 scatter gather segments to map then
16139 * pdma_phys_addr1 should be 0.
16140 * If the caller needs to map more than 256 scatter gather segment then
16141 * pdma_phys_addr1 should be a valid physical address.
16142 * physical address for SGLs must be 64 byte aligned.
16143 * If you are going to map 2 SGL's then the first one must have 256 entries
16144 * the second sgl can have between 1 and 256 entries.
16145 *
16146 * Return codes:
16147 * 0 - Success
16148 * -ENXIO, -ENOMEM - Failure
16149 **/
16150int
16151lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16152 dma_addr_t pdma_phys_addr0,
16153 dma_addr_t pdma_phys_addr1,
16154 uint16_t xritag)
16155{
16156 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16157 LPFC_MBOXQ_t *mbox;
16158 int rc;
16159 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040016160 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040016161 union lpfc_sli4_cfg_shdr *shdr;
16162
16163 if (xritag == NO_XRI) {
16164 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16165 "0364 Invalid param:\n");
16166 return -EINVAL;
16167 }
16168
16169 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16170 if (!mbox)
16171 return -ENOMEM;
16172
16173 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16174 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16175 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050016176 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040016177
16178 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16179 &mbox->u.mqe.un.post_sgl_pages;
16180 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16181 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16182
16183 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16184 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16185 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16186 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16187
16188 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16189 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16190 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16191 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16192 if (!phba->sli4_hba.intr_enable)
16193 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040016194 else {
James Smarta183a152011-10-10 21:32:43 -040016195 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040016196 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16197 }
James Smart4f774512009-05-22 14:52:35 -040016198 /* The IOCTL status is embedded in the mailbox subheader. */
16199 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16200 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16201 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16202 if (rc != MBX_TIMEOUT)
16203 mempool_free(mbox, phba->mbox_mem_pool);
16204 if (shdr_status || shdr_add_status || rc) {
16205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16206 "2511 POST_SGL mailbox failed with "
16207 "status x%x add_status x%x, mbx status x%x\n",
16208 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040016209 }
16210 return 0;
16211}
James Smart4f774512009-05-22 14:52:35 -040016212
16213/**
James Smart88a2cfb2011-07-22 18:36:33 -040016214 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040016215 * @phba: pointer to lpfc hba data structure.
16216 *
16217 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040016218 * HBA consistent with the SLI-4 interface spec. This routine
16219 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16220 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040016221 *
James Smart88a2cfb2011-07-22 18:36:33 -040016222 * Returns
16223 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16224 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16225 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016226static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016227lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16228{
16229 unsigned long xri;
16230
16231 /*
16232 * Fetch the next logical xri. Because this index is logical,
16233 * the driver starts at 0 each time.
16234 */
16235 spin_lock_irq(&phba->hbalock);
16236 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16237 phba->sli4_hba.max_cfg_param.max_xri, 0);
16238 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16239 spin_unlock_irq(&phba->hbalock);
16240 return NO_XRI;
16241 } else {
16242 set_bit(xri, phba->sli4_hba.xri_bmask);
16243 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040016244 }
James Smart6d368e52011-05-24 11:44:12 -040016245 spin_unlock_irq(&phba->hbalock);
16246 return xri;
16247}
16248
16249/**
16250 * lpfc_sli4_free_xri - Release an xri for reuse.
16251 * @phba: pointer to lpfc hba data structure.
16252 *
16253 * This routine is invoked to release an xri to the pool of
16254 * available rpis maintained by the driver.
16255 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016256static void
James Smart6d368e52011-05-24 11:44:12 -040016257__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16258{
16259 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016260 phba->sli4_hba.max_cfg_param.xri_used--;
16261 }
16262}
16263
16264/**
16265 * lpfc_sli4_free_xri - Release an xri for reuse.
16266 * @phba: pointer to lpfc hba data structure.
16267 *
16268 * This routine is invoked to release an xri to the pool of
16269 * available rpis maintained by the driver.
16270 **/
16271void
16272lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16273{
16274 spin_lock_irq(&phba->hbalock);
16275 __lpfc_sli4_free_xri(phba, xri);
16276 spin_unlock_irq(&phba->hbalock);
16277}
16278
16279/**
James Smart4f774512009-05-22 14:52:35 -040016280 * lpfc_sli4_next_xritag - Get an xritag for the io
16281 * @phba: Pointer to HBA context object.
16282 *
16283 * This function gets an xritag for the iocb. If there is no unused xritag
16284 * it will return 0xffff.
16285 * The function returns the allocated xritag if successful, else returns zero.
16286 * Zero is not a valid xritag.
16287 * The caller is not required to hold any lock.
16288 **/
16289uint16_t
16290lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16291{
James Smart6d368e52011-05-24 11:44:12 -040016292 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016293
James Smart6d368e52011-05-24 11:44:12 -040016294 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016295 if (xri_index == NO_XRI)
16296 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16297 "2004 Failed to allocate XRI.last XRITAG is %d"
16298 " Max XRI is %d, Used XRI is %d\n",
16299 xri_index,
16300 phba->sli4_hba.max_cfg_param.max_xri,
16301 phba->sli4_hba.max_cfg_param.xri_used);
16302 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016303}
16304
16305/**
James Smart895427b2017-02-12 13:52:30 -080016306 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016307 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016308 * @post_sgl_list: pointer to els sgl entry list.
16309 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016310 *
16311 * This routine is invoked to post a block of driver's sgl pages to the
16312 * HBA using non-embedded mailbox command. No Lock is held. This routine
16313 * is only called when the driver is loading and after all IO has been
16314 * stopped.
16315 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016316static int
James Smart895427b2017-02-12 13:52:30 -080016317lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016318 struct list_head *post_sgl_list,
16319 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016320{
James Smart8a9d2e82012-05-09 21:16:12 -040016321 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016322 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16323 struct sgl_page_pairs *sgl_pg_pairs;
16324 void *viraddr;
16325 LPFC_MBOXQ_t *mbox;
16326 uint32_t reqlen, alloclen, pg_pairs;
16327 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016328 uint16_t xritag_start = 0;
16329 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016330 uint32_t shdr_status, shdr_add_status;
16331 union lpfc_sli4_cfg_shdr *shdr;
16332
James Smart895427b2017-02-12 13:52:30 -080016333 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016334 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016335 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016336 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016337 "2559 Block sgl registration required DMA "
16338 "size (%d) great than a page\n", reqlen);
16339 return -ENOMEM;
16340 }
James Smart895427b2017-02-12 13:52:30 -080016341
James Smart4f774512009-05-22 14:52:35 -040016342 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016343 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016344 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016345
16346 /* Allocate DMA memory and set up the non-embedded mailbox command */
16347 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16348 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16349 LPFC_SLI4_MBX_NEMBED);
16350
16351 if (alloclen < reqlen) {
16352 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16353 "0285 Allocated DMA memory size (%d) is "
16354 "less than the requested DMA memory "
16355 "size (%d)\n", alloclen, reqlen);
16356 lpfc_sli4_mbox_cmd_free(phba, mbox);
16357 return -ENOMEM;
16358 }
James Smart4f774512009-05-22 14:52:35 -040016359 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016360 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016361 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16362 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16363
James Smart8a9d2e82012-05-09 21:16:12 -040016364 pg_pairs = 0;
16365 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016366 /* Set up the sge entry */
16367 sgl_pg_pairs->sgl_pg0_addr_lo =
16368 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16369 sgl_pg_pairs->sgl_pg0_addr_hi =
16370 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16371 sgl_pg_pairs->sgl_pg1_addr_lo =
16372 cpu_to_le32(putPaddrLow(0));
16373 sgl_pg_pairs->sgl_pg1_addr_hi =
16374 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016375
James Smart4f774512009-05-22 14:52:35 -040016376 /* Keep the first xritag on the list */
16377 if (pg_pairs == 0)
16378 xritag_start = sglq_entry->sli4_xritag;
16379 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016380 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016381 }
James Smart6d368e52011-05-24 11:44:12 -040016382
16383 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016384 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016385 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016386 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016387
James Smart4f774512009-05-22 14:52:35 -040016388 if (!phba->sli4_hba.intr_enable)
16389 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16390 else {
James Smarta183a152011-10-10 21:32:43 -040016391 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016392 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16393 }
16394 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16395 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16396 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16397 if (rc != MBX_TIMEOUT)
16398 lpfc_sli4_mbox_cmd_free(phba, mbox);
16399 if (shdr_status || shdr_add_status || rc) {
16400 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16401 "2513 POST_SGL_BLOCK mailbox command failed "
16402 "status x%x add_status x%x mbx status x%x\n",
16403 shdr_status, shdr_add_status, rc);
16404 rc = -ENXIO;
16405 }
16406 return rc;
16407}
16408
16409/**
16410 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16411 * @phba: pointer to lpfc hba data structure.
16412 * @sblist: pointer to scsi buffer list.
16413 * @count: number of scsi buffers on the list.
16414 *
16415 * This routine is invoked to post a block of @count scsi sgl pages from a
16416 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16417 * No Lock is held.
16418 *
16419 **/
16420int
James Smart8a9d2e82012-05-09 21:16:12 -040016421lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16422 struct list_head *sblist,
16423 int count)
James Smart4f774512009-05-22 14:52:35 -040016424{
16425 struct lpfc_scsi_buf *psb;
16426 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16427 struct sgl_page_pairs *sgl_pg_pairs;
16428 void *viraddr;
16429 LPFC_MBOXQ_t *mbox;
16430 uint32_t reqlen, alloclen, pg_pairs;
16431 uint32_t mbox_tmo;
16432 uint16_t xritag_start = 0;
16433 int rc = 0;
16434 uint32_t shdr_status, shdr_add_status;
16435 dma_addr_t pdma_phys_bpl1;
16436 union lpfc_sli4_cfg_shdr *shdr;
16437
16438 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016439 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016440 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016441 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016442 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16443 "0217 Block sgl registration required DMA "
16444 "size (%d) great than a page\n", reqlen);
16445 return -ENOMEM;
16446 }
16447 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16448 if (!mbox) {
16449 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16450 "0283 Failed to allocate mbox cmd memory\n");
16451 return -ENOMEM;
16452 }
16453
16454 /* Allocate DMA memory and set up the non-embedded mailbox command */
16455 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16456 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16457 LPFC_SLI4_MBX_NEMBED);
16458
16459 if (alloclen < reqlen) {
16460 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16461 "2561 Allocated DMA memory size (%d) is "
16462 "less than the requested DMA memory "
16463 "size (%d)\n", alloclen, reqlen);
16464 lpfc_sli4_mbox_cmd_free(phba, mbox);
16465 return -ENOMEM;
16466 }
James Smart6d368e52011-05-24 11:44:12 -040016467
James Smart4f774512009-05-22 14:52:35 -040016468 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016469 viraddr = mbox->sge_array->addr[0];
16470
16471 /* Set up the SGL pages in the non-embedded DMA pages */
16472 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16473 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16474
16475 pg_pairs = 0;
16476 list_for_each_entry(psb, sblist, list) {
16477 /* Set up the sge entry */
16478 sgl_pg_pairs->sgl_pg0_addr_lo =
16479 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16480 sgl_pg_pairs->sgl_pg0_addr_hi =
16481 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16482 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16483 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16484 else
16485 pdma_phys_bpl1 = 0;
16486 sgl_pg_pairs->sgl_pg1_addr_lo =
16487 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16488 sgl_pg_pairs->sgl_pg1_addr_hi =
16489 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16490 /* Keep the first xritag on the list */
16491 if (pg_pairs == 0)
16492 xritag_start = psb->cur_iocbq.sli4_xritag;
16493 sgl_pg_pairs++;
16494 pg_pairs++;
16495 }
16496 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16497 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16498 /* Perform endian conversion if necessary */
16499 sgl->word0 = cpu_to_le32(sgl->word0);
16500
16501 if (!phba->sli4_hba.intr_enable)
16502 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16503 else {
James Smarta183a152011-10-10 21:32:43 -040016504 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016505 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16506 }
16507 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16508 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16509 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16510 if (rc != MBX_TIMEOUT)
16511 lpfc_sli4_mbox_cmd_free(phba, mbox);
16512 if (shdr_status || shdr_add_status || rc) {
16513 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16514 "2564 POST_SGL_BLOCK mailbox command failed "
16515 "status x%x add_status x%x mbx status x%x\n",
16516 shdr_status, shdr_add_status, rc);
16517 rc = -ENXIO;
16518 }
16519 return rc;
16520}
16521
16522/**
16523 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16524 * @phba: pointer to lpfc_hba struct that the frame was received on
16525 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16526 *
16527 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16528 * valid type of frame that the LPFC driver will handle. This function will
16529 * return a zero if the frame is a valid frame or a non zero value when the
16530 * frame does not pass the check.
16531 **/
16532static int
16533lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16534{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016535 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016536 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016537 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016538
James Smartae9e28f2017-05-15 15:20:51 -070016539#define FC_RCTL_MDS_DIAGS 0xF4
16540
James Smart4f774512009-05-22 14:52:35 -040016541 switch (fc_hdr->fh_r_ctl) {
16542 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16543 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16544 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16545 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16546 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16547 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16548 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16549 case FC_RCTL_DD_CMD_STATUS: /* command status */
16550 case FC_RCTL_ELS_REQ: /* extended link services request */
16551 case FC_RCTL_ELS_REP: /* extended link services reply */
16552 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16553 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16554 case FC_RCTL_BA_NOP: /* basic link service NOP */
16555 case FC_RCTL_BA_ABTS: /* basic link service abort */
16556 case FC_RCTL_BA_RMC: /* remove connection */
16557 case FC_RCTL_BA_ACC: /* basic accept */
16558 case FC_RCTL_BA_RJT: /* basic reject */
16559 case FC_RCTL_BA_PRMT:
16560 case FC_RCTL_ACK_1: /* acknowledge_1 */
16561 case FC_RCTL_ACK_0: /* acknowledge_0 */
16562 case FC_RCTL_P_RJT: /* port reject */
16563 case FC_RCTL_F_RJT: /* fabric reject */
16564 case FC_RCTL_P_BSY: /* port busy */
16565 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16566 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16567 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016568 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016569 case FC_RCTL_END: /* end */
16570 break;
16571 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16572 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16573 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16574 return lpfc_fc_frame_check(phba, fc_hdr);
16575 default:
16576 goto drop;
16577 }
James Smartae9e28f2017-05-15 15:20:51 -070016578
16579#define FC_TYPE_VENDOR_UNIQUE 0xFF
16580
James Smart4f774512009-05-22 14:52:35 -040016581 switch (fc_hdr->fh_type) {
16582 case FC_TYPE_BLS:
16583 case FC_TYPE_ELS:
16584 case FC_TYPE_FCP:
16585 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016586 case FC_TYPE_NVME:
James Smartae9e28f2017-05-15 15:20:51 -070016587 case FC_TYPE_VENDOR_UNIQUE:
James Smart4f774512009-05-22 14:52:35 -040016588 break;
16589 case FC_TYPE_IP:
16590 case FC_TYPE_ILS:
16591 default:
16592 goto drop;
16593 }
James Smart546fc852011-03-11 16:06:29 -050016594
James Smart4f774512009-05-22 14:52:35 -040016595 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016596 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040016597 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070016598 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16599 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16600 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16601 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16602 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016603 return 0;
16604drop:
16605 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016606 "2539 Dropped frame rctl:x%x type:x%x\n",
16607 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040016608 return 1;
16609}
16610
16611/**
16612 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16613 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16614 *
16615 * This function processes the FC header to retrieve the VFI from the VF
16616 * header, if one exists. This function will return the VFI if one exists
16617 * or 0 if no VSAN Header exists.
16618 **/
16619static uint32_t
16620lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16621{
16622 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16623
16624 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16625 return 0;
16626 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16627}
16628
16629/**
16630 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16631 * @phba: Pointer to the HBA structure to search for the vport on
16632 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16633 * @fcfi: The FC Fabric ID that the frame came from
16634 *
16635 * This function searches the @phba for a vport that matches the content of the
16636 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16637 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16638 * returns the matching vport pointer or NULL if unable to match frame to a
16639 * vport.
16640 **/
16641static struct lpfc_vport *
16642lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016643 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016644{
16645 struct lpfc_vport **vports;
16646 struct lpfc_vport *vport = NULL;
16647 int i;
James Smart939723a2012-05-09 21:19:03 -040016648
James Smartbf086112011-08-21 21:48:13 -040016649 if (did == Fabric_DID)
16650 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016651 if ((phba->pport->fc_flag & FC_PT2PT) &&
16652 !(phba->link_state == LPFC_HBA_READY))
16653 return phba->pport;
16654
James Smart4f774512009-05-22 14:52:35 -040016655 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016656 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016657 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16658 if (phba->fcf.fcfi == fcfi &&
16659 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16660 vports[i]->fc_myDID == did) {
16661 vport = vports[i];
16662 break;
16663 }
16664 }
James Smart895427b2017-02-12 13:52:30 -080016665 }
James Smart4f774512009-05-22 14:52:35 -040016666 lpfc_destroy_vport_work_array(phba, vports);
16667 return vport;
16668}
16669
16670/**
James Smart45ed1192009-10-02 15:17:02 -040016671 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16672 * @vport: The vport to work on.
16673 *
16674 * This function updates the receive sequence time stamp for this vport. The
16675 * receive sequence time stamp indicates the time that the last frame of the
16676 * the sequence that has been idle for the longest amount of time was received.
16677 * the driver uses this time stamp to indicate if any received sequences have
16678 * timed out.
16679 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016680static void
James Smart45ed1192009-10-02 15:17:02 -040016681lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16682{
16683 struct lpfc_dmabuf *h_buf;
16684 struct hbq_dmabuf *dmabuf = NULL;
16685
16686 /* get the oldest sequence on the rcv list */
16687 h_buf = list_get_first(&vport->rcv_buffer_list,
16688 struct lpfc_dmabuf, list);
16689 if (!h_buf)
16690 return;
16691 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16692 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16693}
16694
16695/**
16696 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16697 * @vport: The vport that the received sequences were sent to.
16698 *
16699 * This function cleans up all outstanding received sequences. This is called
16700 * by the driver when a link event or user action invalidates all the received
16701 * sequences.
16702 **/
16703void
16704lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16705{
16706 struct lpfc_dmabuf *h_buf, *hnext;
16707 struct lpfc_dmabuf *d_buf, *dnext;
16708 struct hbq_dmabuf *dmabuf = NULL;
16709
16710 /* start with the oldest sequence on the rcv list */
16711 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16712 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16713 list_del_init(&dmabuf->hbuf.list);
16714 list_for_each_entry_safe(d_buf, dnext,
16715 &dmabuf->dbuf.list, list) {
16716 list_del_init(&d_buf->list);
16717 lpfc_in_buf_free(vport->phba, d_buf);
16718 }
16719 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16720 }
16721}
16722
16723/**
16724 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16725 * @vport: The vport that the received sequences were sent to.
16726 *
16727 * This function determines whether any received sequences have timed out by
16728 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16729 * indicates that there is at least one timed out sequence this routine will
16730 * go through the received sequences one at a time from most inactive to most
16731 * active to determine which ones need to be cleaned up. Once it has determined
16732 * that a sequence needs to be cleaned up it will simply free up the resources
16733 * without sending an abort.
16734 **/
16735void
16736lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16737{
16738 struct lpfc_dmabuf *h_buf, *hnext;
16739 struct lpfc_dmabuf *d_buf, *dnext;
16740 struct hbq_dmabuf *dmabuf = NULL;
16741 unsigned long timeout;
16742 int abort_count = 0;
16743
16744 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16745 vport->rcv_buffer_time_stamp);
16746 if (list_empty(&vport->rcv_buffer_list) ||
16747 time_before(jiffies, timeout))
16748 return;
16749 /* start with the oldest sequence on the rcv list */
16750 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16751 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16752 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16753 dmabuf->time_stamp);
16754 if (time_before(jiffies, timeout))
16755 break;
16756 abort_count++;
16757 list_del_init(&dmabuf->hbuf.list);
16758 list_for_each_entry_safe(d_buf, dnext,
16759 &dmabuf->dbuf.list, list) {
16760 list_del_init(&d_buf->list);
16761 lpfc_in_buf_free(vport->phba, d_buf);
16762 }
16763 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16764 }
16765 if (abort_count)
16766 lpfc_update_rcv_time_stamp(vport);
16767}
16768
16769/**
James Smart4f774512009-05-22 14:52:35 -040016770 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16771 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16772 *
16773 * This function searches through the existing incomplete sequences that have
16774 * been sent to this @vport. If the frame matches one of the incomplete
16775 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16776 * make up that sequence. If no sequence is found that matches this frame then
16777 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16778 * This function returns a pointer to the first dmabuf in the sequence list that
16779 * the frame was linked to.
16780 **/
16781static struct hbq_dmabuf *
16782lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16783{
16784 struct fc_frame_header *new_hdr;
16785 struct fc_frame_header *temp_hdr;
16786 struct lpfc_dmabuf *d_buf;
16787 struct lpfc_dmabuf *h_buf;
16788 struct hbq_dmabuf *seq_dmabuf = NULL;
16789 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050016790 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040016791
James Smart4d9ab992009-10-02 15:16:39 -040016792 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016793 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040016794 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050016795
James Smart4f774512009-05-22 14:52:35 -040016796 /* Use the hdr_buf to find the sequence that this frame belongs to */
16797 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16798 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16799 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16800 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16801 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16802 continue;
16803 /* found a pending sequence that matches this frame */
16804 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16805 break;
16806 }
16807 if (!seq_dmabuf) {
16808 /*
16809 * This indicates first frame received for this sequence.
16810 * Queue the buffer on the vport's rcv_buffer_list.
16811 */
16812 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040016813 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016814 return dmabuf;
16815 }
16816 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050016817 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16818 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040016819 list_del_init(&seq_dmabuf->hbuf.list);
16820 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16821 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016822 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016823 return dmabuf;
16824 }
James Smart45ed1192009-10-02 15:17:02 -040016825 /* move this sequence to the tail to indicate a young sequence */
16826 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16827 seq_dmabuf->time_stamp = jiffies;
16828 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050016829 if (list_empty(&seq_dmabuf->dbuf.list)) {
16830 temp_hdr = dmabuf->hbuf.virt;
16831 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16832 return seq_dmabuf;
16833 }
James Smart4f774512009-05-22 14:52:35 -040016834 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050016835 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16836 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040016837 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16838 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16839 /*
16840 * If the frame's sequence count is greater than the frame on
16841 * the list then insert the frame right after this frame
16842 */
James Smarteeead812009-12-21 17:01:23 -050016843 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16844 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040016845 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050016846 found = 1;
16847 break;
James Smart4f774512009-05-22 14:52:35 -040016848 }
James Smart4360ca92015-12-16 18:12:04 -050016849
16850 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16851 break;
16852 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040016853 }
James Smart4360ca92015-12-16 18:12:04 -050016854
16855 if (found)
16856 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016857 return NULL;
16858}
16859
16860/**
James Smart6669f9b2009-10-02 15:16:45 -040016861 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16862 * @vport: pointer to a vitural port
16863 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16864 *
16865 * This function tries to abort from the partially assembed sequence, described
16866 * by the information from basic abbort @dmabuf. It checks to see whether such
16867 * partially assembled sequence held by the driver. If so, it shall free up all
16868 * the frames from the partially assembled sequence.
16869 *
16870 * Return
16871 * true -- if there is matching partially assembled sequence present and all
16872 * the frames freed with the sequence;
16873 * false -- if there is no matching partially assembled sequence present so
16874 * nothing got aborted in the lower layer driver
16875 **/
16876static bool
16877lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16878 struct hbq_dmabuf *dmabuf)
16879{
16880 struct fc_frame_header *new_hdr;
16881 struct fc_frame_header *temp_hdr;
16882 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16883 struct hbq_dmabuf *seq_dmabuf = NULL;
16884
16885 /* Use the hdr_buf to find the sequence that matches this frame */
16886 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16887 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16888 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16889 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16890 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16891 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16892 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16893 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16894 continue;
16895 /* found a pending sequence that matches this frame */
16896 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16897 break;
16898 }
16899
16900 /* Free up all the frames from the partially assembled sequence */
16901 if (seq_dmabuf) {
16902 list_for_each_entry_safe(d_buf, n_buf,
16903 &seq_dmabuf->dbuf.list, list) {
16904 list_del_init(&d_buf->list);
16905 lpfc_in_buf_free(vport->phba, d_buf);
16906 }
16907 return true;
16908 }
16909 return false;
16910}
16911
16912/**
James Smart6dd9e312013-01-03 15:43:37 -050016913 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16914 * @vport: pointer to a vitural port
16915 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16916 *
16917 * This function tries to abort from the assembed sequence from upper level
16918 * protocol, described by the information from basic abbort @dmabuf. It
16919 * checks to see whether such pending context exists at upper level protocol.
16920 * If so, it shall clean up the pending context.
16921 *
16922 * Return
16923 * true -- if there is matching pending context of the sequence cleaned
16924 * at ulp;
16925 * false -- if there is no matching pending context of the sequence present
16926 * at ulp.
16927 **/
16928static bool
16929lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16930{
16931 struct lpfc_hba *phba = vport->phba;
16932 int handled;
16933
16934 /* Accepting abort at ulp with SLI4 only */
16935 if (phba->sli_rev < LPFC_SLI_REV4)
16936 return false;
16937
16938 /* Register all caring upper level protocols to attend abort */
16939 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16940 if (handled)
16941 return true;
16942
16943 return false;
16944}
16945
16946/**
James Smart546fc852011-03-11 16:06:29 -050016947 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040016948 * @phba: Pointer to HBA context object.
16949 * @cmd_iocbq: pointer to the command iocbq structure.
16950 * @rsp_iocbq: pointer to the response iocbq structure.
16951 *
James Smart546fc852011-03-11 16:06:29 -050016952 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040016953 * event. It properly releases the memory allocated to the sequence abort
16954 * accept iocb.
16955 **/
16956static void
James Smart546fc852011-03-11 16:06:29 -050016957lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040016958 struct lpfc_iocbq *cmd_iocbq,
16959 struct lpfc_iocbq *rsp_iocbq)
16960{
James Smart6dd9e312013-01-03 15:43:37 -050016961 struct lpfc_nodelist *ndlp;
16962
16963 if (cmd_iocbq) {
16964 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16965 lpfc_nlp_put(ndlp);
16966 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016967 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050016968 }
James Smart6b5151f2012-01-18 16:24:06 -050016969
16970 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16971 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16972 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16973 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16974 rsp_iocbq->iocb.ulpStatus,
16975 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040016976}
16977
16978/**
James Smart6d368e52011-05-24 11:44:12 -040016979 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16980 * @phba: Pointer to HBA context object.
16981 * @xri: xri id in transaction.
16982 *
16983 * This function validates the xri maps to the known range of XRIs allocated an
16984 * used by the driver.
16985 **/
James Smart7851fe22011-07-22 18:36:52 -040016986uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016987lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16988 uint16_t xri)
16989{
James Smarta2fc4aef2014-09-03 12:57:55 -040016990 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040016991
16992 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16993 if (xri == phba->sli4_hba.xri_ids[i])
16994 return i;
16995 }
16996 return NO_XRI;
16997}
16998
James Smart6d368e52011-05-24 11:44:12 -040016999/**
James Smart546fc852011-03-11 16:06:29 -050017000 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017001 * @phba: Pointer to HBA context object.
17002 * @fc_hdr: pointer to a FC frame header.
17003 *
James Smart546fc852011-03-11 16:06:29 -050017004 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017005 * event after aborting the sequence handling.
17006 **/
James Smart86c67372017-04-21 16:05:04 -070017007void
James Smart6dd9e312013-01-03 15:43:37 -050017008lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17009 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040017010{
James Smart6dd9e312013-01-03 15:43:37 -050017011 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040017012 struct lpfc_iocbq *ctiocb = NULL;
17013 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040017014 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050017015 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040017016 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050017017 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040017018
17019 if (!lpfc_is_link_up(phba))
17020 return;
17021
17022 sid = sli4_sid_from_fc_hdr(fc_hdr);
17023 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050017024 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040017025
James Smart6dd9e312013-01-03 15:43:37 -050017026 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040017027 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070017028 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050017029 if (!ndlp) {
17030 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17031 "1268 Failed to allocate ndlp for "
17032 "oxid:x%x SID:x%x\n", oxid, sid);
17033 return;
17034 }
James Smart6dd9e312013-01-03 15:43:37 -050017035 /* Put ndlp onto pport node list */
17036 lpfc_enqueue_node(vport, ndlp);
17037 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17038 /* re-setup ndlp without removing from node list */
17039 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17040 if (!ndlp) {
17041 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17042 "3275 Failed to active ndlp found "
17043 "for oxid:x%x SID:x%x\n", oxid, sid);
17044 return;
17045 }
James Smart6669f9b2009-10-02 15:16:45 -040017046 }
17047
James Smart546fc852011-03-11 16:06:29 -050017048 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040017049 ctiocb = lpfc_sli_get_iocbq(phba);
17050 if (!ctiocb)
17051 return;
17052
James Smart5ffc2662009-11-18 15:39:44 -050017053 /* Extract the F_CTL field from FC_HDR */
17054 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17055
James Smart6669f9b2009-10-02 15:16:45 -040017056 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040017057 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050017058 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040017059 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17060 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17061 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17062
17063 /* Fill in the rest of iocb fields */
17064 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17065 icmd->ulpBdeCount = 0;
17066 icmd->ulpLe = 1;
17067 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040017068 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050017069 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017070
James Smart6669f9b2009-10-02 15:16:45 -040017071 ctiocb->iocb_cmpl = NULL;
17072 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050017073 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040017074 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050017075 ctiocb->sli4_xritag = NO_XRI;
17076
James Smartee0f4fe2012-05-09 21:19:14 -040017077 if (fctl & FC_FC_EX_CTX)
17078 /* Exchange responder sent the abort so we
17079 * own the oxid.
17080 */
17081 xri = oxid;
17082 else
17083 xri = rxid;
17084 lxri = lpfc_sli4_xri_inrange(phba, xri);
17085 if (lxri != NO_XRI)
17086 lpfc_set_rrq_active(phba, ndlp, lxri,
17087 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050017088 /* For BA_ABTS from exchange responder, if the logical xri with
17089 * the oxid maps to the FCP XRI range, the port no longer has
17090 * that exchange context, send a BLS_RJT. Override the IOCB for
17091 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050017092 */
James Smart6dd9e312013-01-03 15:43:37 -050017093 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080017094 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -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 }
17100
17101 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17102 * the driver no longer has that exchange, send a BLS_RJT. Override
17103 * the IOCB for a BA_RJT.
17104 */
17105 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050017106 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17107 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17108 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17109 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17110 }
James Smart6669f9b2009-10-02 15:16:45 -040017111
James Smart5ffc2662009-11-18 15:39:44 -050017112 if (fctl & FC_FC_EX_CTX) {
17113 /* ABTS sent by responder to CT exchange, construction
17114 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17115 * field and RX_ID from ABTS for RX_ID field.
17116 */
James Smart546fc852011-03-11 16:06:29 -050017117 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050017118 } else {
17119 /* ABTS sent by initiator to CT exchange, construction
17120 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050017121 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050017122 */
James Smart546fc852011-03-11 16:06:29 -050017123 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050017124 }
James Smartf09c3ac2012-03-01 22:33:29 -050017125 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050017126 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050017127
James Smart546fc852011-03-11 16:06:29 -050017128 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050017129 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17130 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17131 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050017132
17133 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17134 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050017135 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17136 "2925 Failed to issue CT ABTS RSP x%x on "
17137 "xri x%x, Data x%x\n",
17138 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17139 phba->link_state);
17140 lpfc_nlp_put(ndlp);
17141 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050017142 lpfc_sli_release_iocbq(phba, ctiocb);
17143 }
James Smart6669f9b2009-10-02 15:16:45 -040017144}
17145
17146/**
17147 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17148 * @vport: Pointer to the vport on which this sequence was received
17149 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17150 *
17151 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17152 * receive sequence is only partially assembed by the driver, it shall abort
17153 * the partially assembled frames for the sequence. Otherwise, if the
17154 * unsolicited receive sequence has been completely assembled and passed to
17155 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17156 * unsolicited sequence has been aborted. After that, it will issue a basic
17157 * accept to accept the abort.
17158 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017159static void
James Smart6669f9b2009-10-02 15:16:45 -040017160lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17161 struct hbq_dmabuf *dmabuf)
17162{
17163 struct lpfc_hba *phba = vport->phba;
17164 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050017165 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050017166 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040017167
James Smart6669f9b2009-10-02 15:16:45 -040017168 /* Make a copy of fc_hdr before the dmabuf being released */
17169 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050017170 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040017171
James Smart5ffc2662009-11-18 15:39:44 -050017172 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050017173 /* ABTS by responder to exchange, no cleanup needed */
17174 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050017175 } else {
James Smart6dd9e312013-01-03 15:43:37 -050017176 /* ABTS by initiator to exchange, need to do cleanup */
17177 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17178 if (aborted == false)
17179 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050017180 }
James Smart6dd9e312013-01-03 15:43:37 -050017181 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17182
James Smart86c67372017-04-21 16:05:04 -070017183 if (phba->nvmet_support) {
17184 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17185 return;
17186 }
17187
James Smart6dd9e312013-01-03 15:43:37 -050017188 /* Respond with BA_ACC or BA_RJT accordingly */
17189 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040017190}
17191
17192/**
James Smart4f774512009-05-22 14:52:35 -040017193 * lpfc_seq_complete - Indicates if a sequence is complete
17194 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17195 *
17196 * This function checks the sequence, starting with the frame described by
17197 * @dmabuf, to see if all the frames associated with this sequence are present.
17198 * the frames associated with this sequence are linked to the @dmabuf using the
17199 * dbuf list. This function looks for two major things. 1) That the first frame
17200 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17201 * set. 3) That there are no holes in the sequence count. The function will
17202 * return 1 when the sequence is complete, otherwise it will return 0.
17203 **/
17204static int
17205lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17206{
17207 struct fc_frame_header *hdr;
17208 struct lpfc_dmabuf *d_buf;
17209 struct hbq_dmabuf *seq_dmabuf;
17210 uint32_t fctl;
17211 int seq_count = 0;
17212
17213 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17214 /* make sure first fame of sequence has a sequence count of zero */
17215 if (hdr->fh_seq_cnt != seq_count)
17216 return 0;
17217 fctl = (hdr->fh_f_ctl[0] << 16 |
17218 hdr->fh_f_ctl[1] << 8 |
17219 hdr->fh_f_ctl[2]);
17220 /* If last frame of sequence we can return success. */
17221 if (fctl & FC_FC_END_SEQ)
17222 return 1;
17223 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17224 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17225 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17226 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050017227 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040017228 return 0;
17229 fctl = (hdr->fh_f_ctl[0] << 16 |
17230 hdr->fh_f_ctl[1] << 8 |
17231 hdr->fh_f_ctl[2]);
17232 /* If last frame of sequence we can return success. */
17233 if (fctl & FC_FC_END_SEQ)
17234 return 1;
17235 }
17236 return 0;
17237}
17238
17239/**
17240 * lpfc_prep_seq - Prep sequence for ULP processing
17241 * @vport: Pointer to the vport on which this sequence was received
17242 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17243 *
17244 * This function takes a sequence, described by a list of frames, and creates
17245 * a list of iocbq structures to describe the sequence. This iocbq list will be
17246 * used to issue to the generic unsolicited sequence handler. This routine
17247 * returns a pointer to the first iocbq in the list. If the function is unable
17248 * to allocate an iocbq then it throw out the received frames that were not
17249 * able to be described and return a pointer to the first iocbq. If unable to
17250 * allocate any iocbqs (including the first) this function will return NULL.
17251 **/
17252static struct lpfc_iocbq *
17253lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17254{
James Smart7851fe22011-07-22 18:36:52 -040017255 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040017256 struct lpfc_dmabuf *d_buf, *n_buf;
17257 struct lpfc_iocbq *first_iocbq, *iocbq;
17258 struct fc_frame_header *fc_hdr;
17259 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040017260 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050017261 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040017262
17263 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17264 /* remove from receive buffer list */
17265 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017266 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017267 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040017268 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040017269 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017270 /* Get an iocbq struct to fill in. */
17271 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17272 if (first_iocbq) {
17273 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040017274 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017275 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080017276 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040017277
17278 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17279 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17280 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17281 first_iocbq->iocb.un.rcvels.parmRo =
17282 sli4_did_from_fc_hdr(fc_hdr);
17283 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17284 } else
17285 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040017286 first_iocbq->iocb.ulpContext = NO_XRI;
17287 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17288 be16_to_cpu(fc_hdr->fh_ox_id);
17289 /* iocbq is prepped for internal consumption. Physical vpi. */
17290 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17291 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040017292 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040017293 tot_len = bf_get(lpfc_rcqe_length,
17294 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17295
James Smart4f774512009-05-22 14:52:35 -040017296 first_iocbq->context2 = &seq_dmabuf->dbuf;
17297 first_iocbq->context3 = NULL;
17298 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040017299 if (tot_len > LPFC_DATA_BUF_SIZE)
17300 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040017301 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040017302 else
17303 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17304
James Smart4f774512009-05-22 14:52:35 -040017305 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040017306
James Smart7851fe22011-07-22 18:36:52 -040017307 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040017308 }
17309 iocbq = first_iocbq;
17310 /*
17311 * Each IOCBq can have two Buffers assigned, so go through the list
17312 * of buffers for this sequence and save two buffers in each IOCBq
17313 */
17314 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17315 if (!iocbq) {
17316 lpfc_in_buf_free(vport->phba, d_buf);
17317 continue;
17318 }
17319 if (!iocbq->context3) {
17320 iocbq->context3 = d_buf;
17321 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040017322 /* We need to get the size out of the right CQE */
17323 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17324 len = bf_get(lpfc_rcqe_length,
17325 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017326 pbde = (struct ulp_bde64 *)
17327 &iocbq->iocb.unsli3.sli3Words[4];
17328 if (len > LPFC_DATA_BUF_SIZE)
17329 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17330 else
17331 pbde->tus.f.bdeSize = len;
17332
James Smart7851fe22011-07-22 18:36:52 -040017333 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17334 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040017335 } else {
17336 iocbq = lpfc_sli_get_iocbq(vport->phba);
17337 if (!iocbq) {
17338 if (first_iocbq) {
17339 first_iocbq->iocb.ulpStatus =
17340 IOSTAT_FCP_RSP_ERROR;
17341 first_iocbq->iocb.un.ulpWord[4] =
17342 IOERR_NO_RESOURCES;
17343 }
17344 lpfc_in_buf_free(vport->phba, d_buf);
17345 continue;
17346 }
James Smart7851fe22011-07-22 18:36:52 -040017347 /* We need to get the size out of the right CQE */
17348 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17349 len = bf_get(lpfc_rcqe_length,
17350 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017351 iocbq->context2 = d_buf;
17352 iocbq->context3 = NULL;
17353 iocbq->iocb.ulpBdeCount = 1;
17354 if (len > LPFC_DATA_BUF_SIZE)
17355 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17356 LPFC_DATA_BUF_SIZE;
17357 else
17358 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17359
James Smart7851fe22011-07-22 18:36:52 -040017360 tot_len += len;
17361 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17362
James Smart4f774512009-05-22 14:52:35 -040017363 iocbq->iocb.un.rcvels.remoteID = sid;
17364 list_add_tail(&iocbq->list, &first_iocbq->list);
17365 }
17366 }
17367 return first_iocbq;
17368}
17369
James Smart6669f9b2009-10-02 15:16:45 -040017370static void
17371lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17372 struct hbq_dmabuf *seq_dmabuf)
17373{
17374 struct fc_frame_header *fc_hdr;
17375 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17376 struct lpfc_hba *phba = vport->phba;
17377
17378 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17379 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17380 if (!iocbq) {
17381 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17382 "2707 Ring %d handler: Failed to allocate "
17383 "iocb Rctl x%x Type x%x received\n",
17384 LPFC_ELS_RING,
17385 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17386 return;
17387 }
17388 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017389 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017390 iocbq, fc_hdr->fh_r_ctl,
17391 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017392 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017393 "2540 Ring %d handler: unexpected Rctl "
17394 "x%x Type x%x received\n",
17395 LPFC_ELS_RING,
17396 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17397
17398 /* Free iocb created in lpfc_prep_seq */
17399 list_for_each_entry_safe(curr_iocb, next_iocb,
17400 &iocbq->list, list) {
17401 list_del_init(&curr_iocb->list);
17402 lpfc_sli_release_iocbq(phba, curr_iocb);
17403 }
17404 lpfc_sli_release_iocbq(phba, iocbq);
17405}
17406
James Smartae9e28f2017-05-15 15:20:51 -070017407static void
17408lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17409 struct lpfc_iocbq *rspiocb)
17410{
17411 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17412
17413 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017414 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017415 kfree(pcmd);
17416 lpfc_sli_release_iocbq(phba, cmdiocb);
17417}
17418
17419static void
17420lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17421 struct hbq_dmabuf *dmabuf)
17422{
17423 struct fc_frame_header *fc_hdr;
17424 struct lpfc_hba *phba = vport->phba;
17425 struct lpfc_iocbq *iocbq = NULL;
17426 union lpfc_wqe *wqe;
17427 struct lpfc_dmabuf *pcmd = NULL;
17428 uint32_t frame_len;
17429 int rc;
17430
17431 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17432 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17433
17434 /* Send the received frame back */
17435 iocbq = lpfc_sli_get_iocbq(phba);
17436 if (!iocbq)
17437 goto exit;
17438
17439 /* Allocate buffer for command payload */
17440 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17441 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020017442 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070017443 &pcmd->phys);
17444 if (!pcmd || !pcmd->virt)
17445 goto exit;
17446
17447 INIT_LIST_HEAD(&pcmd->list);
17448
17449 /* copyin the payload */
17450 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17451
17452 /* fill in BDE's for command */
17453 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17454 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17455 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17456 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17457
17458 iocbq->context2 = pcmd;
17459 iocbq->vport = vport;
17460 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17461 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17462
17463 /*
17464 * Setup rest of the iocb as though it were a WQE
17465 * Build the SEND_FRAME WQE
17466 */
17467 wqe = (union lpfc_wqe *)&iocbq->iocb;
17468
17469 wqe->send_frame.frame_len = frame_len;
17470 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17471 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17472 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17473 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17474 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17475 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17476
17477 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17478 iocbq->iocb.ulpLe = 1;
17479 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17480 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17481 if (rc == IOCB_ERROR)
17482 goto exit;
17483
17484 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17485 return;
17486
17487exit:
17488 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17489 "2023 Unable to process MDS loopback frame\n");
17490 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017491 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017492 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070017493 if (iocbq)
17494 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070017495 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17496}
17497
James Smart4f774512009-05-22 14:52:35 -040017498/**
17499 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17500 * @phba: Pointer to HBA context object.
17501 *
17502 * This function is called with no lock held. This function processes all
17503 * the received buffers and gives it to upper layers when a received buffer
17504 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017505 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017506 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17507 * appropriate receive function when the final frame in a sequence is received.
17508 **/
James Smart4d9ab992009-10-02 15:16:39 -040017509void
17510lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17511 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017512{
James Smart4d9ab992009-10-02 15:16:39 -040017513 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017514 struct fc_frame_header *fc_hdr;
17515 struct lpfc_vport *vport;
17516 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017517 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017518
James Smart4f774512009-05-22 14:52:35 -040017519 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017520 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017521
James Smart4d9ab992009-10-02 15:16:39 -040017522 /* check to see if this a valid type of frame */
17523 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17524 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17525 return;
17526 }
James Smart2ea259e2017-02-12 13:52:27 -080017527
James Smart7851fe22011-07-22 18:36:52 -040017528 if ((bf_get(lpfc_cqe_code,
17529 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17530 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17531 &dmabuf->cq_event.cqe.rcqe_cmpl);
17532 else
17533 fcfi = bf_get(lpfc_rcqe_fcf_id,
17534 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017535
James Smartae9e28f2017-05-15 15:20:51 -070017536 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17537 vport = phba->pport;
17538 /* Handle MDS Loopback frames */
17539 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17540 return;
17541 }
17542
James Smart895427b2017-02-12 13:52:30 -080017543 /* d_id this frame is directed to */
17544 did = sli4_did_from_fc_hdr(fc_hdr);
17545
17546 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017547 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017548 /* throw out the frame */
17549 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17550 return;
17551 }
James Smart939723a2012-05-09 21:19:03 -040017552
James Smart939723a2012-05-09 21:19:03 -040017553 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17554 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17555 (did != Fabric_DID)) {
17556 /*
17557 * Throw out the frame if we are not pt2pt.
17558 * The pt2pt protocol allows for discovery frames
17559 * to be received without a registered VPI.
17560 */
17561 if (!(vport->fc_flag & FC_PT2PT) ||
17562 (phba->link_state == LPFC_HBA_READY)) {
17563 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17564 return;
17565 }
17566 }
17567
James Smart6669f9b2009-10-02 15:16:45 -040017568 /* Handle the basic abort sequence (BA_ABTS) event */
17569 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17570 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17571 return;
17572 }
17573
James Smart4d9ab992009-10-02 15:16:39 -040017574 /* Link this frame */
17575 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17576 if (!seq_dmabuf) {
17577 /* unable to add frame to vport - throw it out */
17578 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17579 return;
17580 }
17581 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017582 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017583 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017584
James Smart6669f9b2009-10-02 15:16:45 -040017585 /* Send the complete sequence to the upper layer protocol */
17586 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017587}
James Smart6fb120a2009-05-22 14:52:59 -040017588
17589/**
17590 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17591 * @phba: pointer to lpfc hba data structure.
17592 *
17593 * This routine is invoked to post rpi header templates to the
17594 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017595 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17596 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017597 *
17598 * This routine does not require any locks. It's usage is expected
17599 * to be driver load or reset recovery when the driver is
17600 * sequential.
17601 *
17602 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017603 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017604 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017605 * When this error occurs, the driver is not guaranteed
17606 * to have any rpi regions posted to the device and
17607 * must either attempt to repost the regions or take a
17608 * fatal error.
17609 **/
17610int
17611lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17612{
17613 struct lpfc_rpi_hdr *rpi_page;
17614 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017615 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017616
James Smart6d368e52011-05-24 11:44:12 -040017617 /* SLI4 ports that support extents do not require RPI headers. */
17618 if (!phba->sli4_hba.rpi_hdrs_in_use)
17619 goto exit;
17620 if (phba->sli4_hba.extents_in_use)
17621 return -EIO;
17622
James Smart6fb120a2009-05-22 14:52:59 -040017623 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017624 /*
17625 * Assign the rpi headers a physical rpi only if the driver
17626 * has not initialized those resources. A port reset only
17627 * needs the headers posted.
17628 */
17629 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17630 LPFC_RPI_RSRC_RDY)
17631 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17632
James Smart6fb120a2009-05-22 14:52:59 -040017633 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17634 if (rc != MBX_SUCCESS) {
17635 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17636 "2008 Error %d posting all rpi "
17637 "headers\n", rc);
17638 rc = -EIO;
17639 break;
17640 }
17641 }
17642
James Smart6d368e52011-05-24 11:44:12 -040017643 exit:
17644 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17645 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017646 return rc;
17647}
17648
17649/**
17650 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17651 * @phba: pointer to lpfc hba data structure.
17652 * @rpi_page: pointer to the rpi memory region.
17653 *
17654 * This routine is invoked to post a single rpi header to the
17655 * HBA consistent with the SLI-4 interface spec. This memory region
17656 * maps up to 64 rpi context regions.
17657 *
17658 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017659 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017660 * -ENOMEM - No available memory
17661 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017662 **/
17663int
17664lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17665{
17666 LPFC_MBOXQ_t *mboxq;
17667 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17668 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017669 uint32_t shdr_status, shdr_add_status;
17670 union lpfc_sli4_cfg_shdr *shdr;
17671
James Smart6d368e52011-05-24 11:44:12 -040017672 /* SLI4 ports that support extents do not require RPI headers. */
17673 if (!phba->sli4_hba.rpi_hdrs_in_use)
17674 return rc;
17675 if (phba->sli4_hba.extents_in_use)
17676 return -EIO;
17677
James Smart6fb120a2009-05-22 14:52:59 -040017678 /* The port is notified of the header region via a mailbox command. */
17679 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17680 if (!mboxq) {
17681 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17682 "2001 Unable to allocate memory for issuing "
17683 "SLI_CONFIG_SPECIAL mailbox command\n");
17684 return -ENOMEM;
17685 }
17686
17687 /* Post all rpi memory regions to the port. */
17688 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040017689 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17690 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17691 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050017692 sizeof(struct lpfc_sli4_cfg_mhdr),
17693 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040017694
17695
17696 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040017697 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17698 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040017699 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17700 hdr_tmpl, rpi_page->page_count);
17701
James Smart6fb120a2009-05-22 14:52:59 -040017702 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17703 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040017704 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040017705 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17706 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17707 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17708 if (rc != MBX_TIMEOUT)
17709 mempool_free(mboxq, phba->mbox_mem_pool);
17710 if (shdr_status || shdr_add_status || rc) {
17711 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17712 "2514 POST_RPI_HDR mailbox failed with "
17713 "status x%x add_status x%x, mbx status x%x\n",
17714 shdr_status, shdr_add_status, rc);
17715 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070017716 } else {
17717 /*
17718 * The next_rpi stores the next logical module-64 rpi value used
17719 * to post physical rpis in subsequent rpi postings.
17720 */
17721 spin_lock_irq(&phba->hbalock);
17722 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17723 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040017724 }
17725 return rc;
17726}
17727
17728/**
17729 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17730 * @phba: pointer to lpfc hba data structure.
17731 *
17732 * This routine is invoked to post rpi header templates to the
17733 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017734 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17735 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017736 *
17737 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017738 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040017739 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17740 **/
17741int
17742lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17743{
James Smart6d368e52011-05-24 11:44:12 -040017744 unsigned long rpi;
17745 uint16_t max_rpi, rpi_limit;
17746 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017747 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040017748 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040017749
James Smart6fb120a2009-05-22 14:52:59 -040017750 /*
James Smart6d368e52011-05-24 11:44:12 -040017751 * Fetch the next logical rpi. Because this index is logical,
17752 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040017753 */
James Smart4902b382013-10-10 12:20:35 -040017754 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040017755 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17756 rpi_limit = phba->sli4_hba.next_rpi;
17757
James Smart6d368e52011-05-24 11:44:12 -040017758 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17759 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040017760 rpi = LPFC_RPI_ALLOC_ERROR;
17761 else {
17762 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17763 phba->sli4_hba.max_cfg_param.rpi_used++;
17764 phba->sli4_hba.rpi_count++;
17765 }
James Smartbe6bb942015-04-07 15:07:22 -040017766 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17767 "0001 rpi:%x max:%x lim:%x\n",
17768 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040017769
17770 /*
17771 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040017772 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040017773 */
17774 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17775 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040017776 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017777 return rpi;
17778 }
17779
17780 /*
James Smart6d368e52011-05-24 11:44:12 -040017781 * RPI header postings are not required for SLI4 ports capable of
17782 * extents.
17783 */
17784 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040017785 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040017786 return rpi;
17787 }
17788
17789 /*
James Smart6fb120a2009-05-22 14:52:59 -040017790 * If the driver is running low on rpi resources, allocate another
17791 * page now. Note that the next_rpi value is used because
17792 * it represents how many are actually in use whereas max_rpi notes
17793 * how many are supported max by the device.
17794 */
James Smart6d368e52011-05-24 11:44:12 -040017795 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040017796 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017797 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17798 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17799 if (!rpi_hdr) {
17800 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17801 "2002 Error Could not grow rpi "
17802 "count\n");
17803 } else {
James Smart6d368e52011-05-24 11:44:12 -040017804 lrpi = rpi_hdr->start_rpi;
17805 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040017806 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17807 }
17808 }
17809
17810 return rpi;
17811}
17812
17813/**
17814 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17815 * @phba: pointer to lpfc hba data structure.
17816 *
17817 * This routine is invoked to release an rpi to the pool of
17818 * available rpis maintained by the driver.
17819 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017820static void
James Smartd7c47992010-06-08 18:31:54 -040017821__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17822{
17823 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17824 phba->sli4_hba.rpi_count--;
17825 phba->sli4_hba.max_cfg_param.rpi_used--;
17826 }
17827}
17828
17829/**
17830 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17831 * @phba: pointer to lpfc hba data structure.
17832 *
17833 * This routine is invoked to release an rpi to the pool of
17834 * available rpis maintained by the driver.
17835 **/
17836void
James Smart6fb120a2009-05-22 14:52:59 -040017837lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17838{
17839 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040017840 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040017841 spin_unlock_irq(&phba->hbalock);
17842}
17843
17844/**
17845 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17846 * @phba: pointer to lpfc hba data structure.
17847 *
17848 * This routine is invoked to remove the memory region that
17849 * provided rpi via a bitmask.
17850 **/
17851void
17852lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17853{
17854 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040017855 kfree(phba->sli4_hba.rpi_ids);
17856 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040017857}
17858
17859/**
17860 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17861 * @phba: pointer to lpfc hba data structure.
17862 *
17863 * This routine is invoked to remove the memory region that
17864 * provided rpi via a bitmask.
17865 **/
17866int
James Smart6b5151f2012-01-18 16:24:06 -050017867lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17868 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040017869{
17870 LPFC_MBOXQ_t *mboxq;
17871 struct lpfc_hba *phba = ndlp->phba;
17872 int rc;
17873
17874 /* The port is notified of the header region via a mailbox command. */
17875 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17876 if (!mboxq)
17877 return -ENOMEM;
17878
17879 /* Post all rpi memory regions to the port. */
17880 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050017881 if (cmpl) {
17882 mboxq->mbox_cmpl = cmpl;
17883 mboxq->context1 = arg;
17884 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050017885 } else
17886 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050017887 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040017888 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17889 if (rc == MBX_NOT_FINISHED) {
17890 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17891 "2010 Resume RPI Mailbox failed "
17892 "status %d, mbxStatus x%x\n", rc,
17893 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17894 mempool_free(mboxq, phba->mbox_mem_pool);
17895 return -EIO;
17896 }
17897 return 0;
17898}
17899
17900/**
17901 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050017902 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040017903 *
James Smart76a95d72010-11-20 23:11:48 -050017904 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040017905 *
17906 * Returns:
17907 * 0 success
17908 * -Evalue otherwise
17909 **/
17910int
James Smart76a95d72010-11-20 23:11:48 -050017911lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040017912{
17913 LPFC_MBOXQ_t *mboxq;
17914 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040017915 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040017916 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050017917 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040017918 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17919 if (!mboxq)
17920 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050017921 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040017922 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040017923 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040017924 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050017925 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040017926 "2022 INIT VPI Mailbox failed "
17927 "status %d, mbxStatus x%x\n", rc,
17928 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040017929 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040017930 }
James Smart6a9c52c2009-10-02 15:16:51 -040017931 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050017932 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040017933
17934 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040017935}
17936
17937/**
17938 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17939 * @phba: pointer to lpfc hba data structure.
17940 * @mboxq: Pointer to mailbox object.
17941 *
17942 * This routine is invoked to manually add a single FCF record. The caller
17943 * must pass a completely initialized FCF_Record. This routine takes
17944 * care of the nonembedded mailbox operations.
17945 **/
17946static void
17947lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17948{
17949 void *virt_addr;
17950 union lpfc_sli4_cfg_shdr *shdr;
17951 uint32_t shdr_status, shdr_add_status;
17952
17953 virt_addr = mboxq->sge_array->addr[0];
17954 /* The IOCTL status is embedded in the mailbox subheader. */
17955 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17956 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17957 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17958
17959 if ((shdr_status || shdr_add_status) &&
17960 (shdr_status != STATUS_FCF_IN_USE))
17961 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17962 "2558 ADD_FCF_RECORD mailbox failed with "
17963 "status x%x add_status x%x\n",
17964 shdr_status, shdr_add_status);
17965
17966 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17967}
17968
17969/**
17970 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17971 * @phba: pointer to lpfc hba data structure.
17972 * @fcf_record: pointer to the initialized fcf record to add.
17973 *
17974 * This routine is invoked to manually add a single FCF record. The caller
17975 * must pass a completely initialized FCF_Record. This routine takes
17976 * care of the nonembedded mailbox operations.
17977 **/
17978int
17979lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17980{
17981 int rc = 0;
17982 LPFC_MBOXQ_t *mboxq;
17983 uint8_t *bytep;
17984 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040017985 struct lpfc_mbx_sge sge;
17986 uint32_t alloc_len, req_len;
17987 uint32_t fcfindex;
17988
17989 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17990 if (!mboxq) {
17991 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17992 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17993 return -ENOMEM;
17994 }
17995
17996 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17997 sizeof(uint32_t);
17998
17999 /* Allocate DMA memory and set up the non-embedded mailbox command */
18000 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18001 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18002 req_len, LPFC_SLI4_MBX_NEMBED);
18003 if (alloc_len < req_len) {
18004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18005 "2523 Allocated DMA memory size (x%x) is "
18006 "less than the requested DMA memory "
18007 "size (x%x)\n", alloc_len, req_len);
18008 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18009 return -ENOMEM;
18010 }
18011
18012 /*
18013 * Get the first SGE entry from the non-embedded DMA memory. This
18014 * routine only uses a single SGE.
18015 */
18016 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040018017 virt_addr = mboxq->sge_array->addr[0];
18018 /*
18019 * Configure the FCF record for FCFI 0. This is the driver's
18020 * hardcoded default and gets used in nonFIP mode.
18021 */
18022 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18023 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18024 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18025
18026 /*
18027 * Copy the fcf_index and the FCF Record Data. The data starts after
18028 * the FCoE header plus word10. The data copy needs to be endian
18029 * correct.
18030 */
18031 bytep += sizeof(uint32_t);
18032 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18033 mboxq->vport = phba->pport;
18034 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18035 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18036 if (rc == MBX_NOT_FINISHED) {
18037 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18038 "2515 ADD_FCF_RECORD mailbox failed with "
18039 "status 0x%x\n", rc);
18040 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18041 rc = -EIO;
18042 } else
18043 rc = 0;
18044
18045 return rc;
18046}
18047
18048/**
18049 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18050 * @phba: pointer to lpfc hba data structure.
18051 * @fcf_record: pointer to the fcf record to write the default data.
18052 * @fcf_index: FCF table entry index.
18053 *
18054 * This routine is invoked to build the driver's default FCF record. The
18055 * values used are hardcoded. This routine handles memory initialization.
18056 *
18057 **/
18058void
18059lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18060 struct fcf_record *fcf_record,
18061 uint16_t fcf_index)
18062{
18063 memset(fcf_record, 0, sizeof(struct fcf_record));
18064 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18065 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18066 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18067 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18068 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18069 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18070 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18071 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18072 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18073 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18074 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18075 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18076 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040018077 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040018078 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18079 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18080 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18081 /* Set the VLAN bit map */
18082 if (phba->valid_vlan) {
18083 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18084 = 1 << (phba->vlan_id % 8);
18085 }
18086}
18087
18088/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018089 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040018090 * @phba: pointer to lpfc hba data structure.
18091 * @fcf_index: FCF table entry offset.
18092 *
James Smart0c9ab6f2010-02-26 14:15:57 -050018093 * This routine is invoked to scan the entire FCF table by reading FCF
18094 * record and processing it one at a time starting from the @fcf_index
18095 * for initial FCF discovery or fast FCF failover rediscovery.
18096 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018097 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018098 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040018099 **/
18100int
James Smart0c9ab6f2010-02-26 14:15:57 -050018101lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040018102{
18103 int rc = 0, error;
18104 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040018105
James Smart32b97932009-07-19 10:01:21 -040018106 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050018107 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040018108 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18109 if (!mboxq) {
18110 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18111 "2000 Failed to allocate mbox for "
18112 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040018113 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050018114 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018115 }
James Smartecfd03c2010-02-12 14:41:27 -050018116 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050018117 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050018118 if (rc) {
18119 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050018120 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018121 }
James Smartecfd03c2010-02-12 14:41:27 -050018122 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040018123 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050018124 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040018125
18126 spin_lock_irq(&phba->hbalock);
18127 phba->hba_flag |= FCF_TS_INPROG;
18128 spin_unlock_irq(&phba->hbalock);
18129
James Smart6fb120a2009-05-22 14:52:59 -040018130 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050018131 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040018132 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050018133 else {
James Smart38b92ef2010-08-04 16:11:39 -040018134 /* Reset eligible FCF count for new scan */
18135 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040018136 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018137 error = 0;
James Smart32b97932009-07-19 10:01:21 -040018138 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018139fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040018140 if (error) {
18141 if (mboxq)
18142 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040018143 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040018144 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040018145 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040018146 spin_unlock_irq(&phba->hbalock);
18147 }
James Smart6fb120a2009-05-22 14:52:59 -040018148 return error;
18149}
James Smarta0c87cb2009-07-19 10:01:10 -040018150
18151/**
James Smarta93ff372010-10-22 11:06:08 -040018152 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050018153 * @phba: pointer to lpfc hba data structure.
18154 * @fcf_index: FCF table entry offset.
18155 *
18156 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040018157 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050018158 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018159 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018160 * otherwise.
18161 **/
18162int
18163lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18164{
18165 int rc = 0, error;
18166 LPFC_MBOXQ_t *mboxq;
18167
18168 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18169 if (!mboxq) {
18170 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18171 "2763 Failed to allocate mbox for "
18172 "READ_FCF cmd\n");
18173 error = -ENOMEM;
18174 goto fail_fcf_read;
18175 }
18176 /* Construct the read FCF record mailbox command */
18177 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18178 if (rc) {
18179 error = -EINVAL;
18180 goto fail_fcf_read;
18181 }
18182 /* Issue the mailbox command asynchronously */
18183 mboxq->vport = phba->pport;
18184 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18185 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18186 if (rc == MBX_NOT_FINISHED)
18187 error = -EIO;
18188 else
18189 error = 0;
18190
18191fail_fcf_read:
18192 if (error && mboxq)
18193 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18194 return error;
18195}
18196
18197/**
18198 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18199 * @phba: pointer to lpfc hba data structure.
18200 * @fcf_index: FCF table entry offset.
18201 *
18202 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040018203 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050018204 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018205 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018206 * otherwise.
18207 **/
18208int
18209lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18210{
18211 int rc = 0, error;
18212 LPFC_MBOXQ_t *mboxq;
18213
18214 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18215 if (!mboxq) {
18216 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18217 "2758 Failed to allocate mbox for "
18218 "READ_FCF cmd\n");
18219 error = -ENOMEM;
18220 goto fail_fcf_read;
18221 }
18222 /* Construct the read FCF record mailbox command */
18223 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18224 if (rc) {
18225 error = -EINVAL;
18226 goto fail_fcf_read;
18227 }
18228 /* Issue the mailbox command asynchronously */
18229 mboxq->vport = phba->pport;
18230 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18231 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18232 if (rc == MBX_NOT_FINISHED)
18233 error = -EIO;
18234 else
18235 error = 0;
18236
18237fail_fcf_read:
18238 if (error && mboxq)
18239 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18240 return error;
18241}
18242
18243/**
James Smartf5cb5302015-12-16 18:11:52 -050018244 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040018245 * phba pointer to the lpfc_hba struct for this port.
18246 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18247 * routine when the rr_bmask is empty. The FCF indecies are put into the
18248 * rr_bmask based on their priority level. Starting from the highest priority
18249 * to the lowest. The most likely FCF candidate will be in the highest
18250 * priority group. When this routine is called it searches the fcf_pri list for
18251 * next lowest priority group and repopulates the rr_bmask with only those
18252 * fcf_indexes.
18253 * returns:
18254 * 1=success 0=failure
18255 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018256static int
James Smart7d791df2011-07-22 18:37:52 -040018257lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18258{
18259 uint16_t next_fcf_pri;
18260 uint16_t last_index;
18261 struct lpfc_fcf_pri *fcf_pri;
18262 int rc;
18263 int ret = 0;
18264
18265 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18266 LPFC_SLI4_FCF_TBL_INDX_MAX);
18267 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18268 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050018269
18270 /* Verify the priority list has 2 or more entries */
18271 spin_lock_irq(&phba->hbalock);
18272 if (list_empty(&phba->fcf.fcf_pri_list) ||
18273 list_is_singular(&phba->fcf.fcf_pri_list)) {
18274 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040018275 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18276 "3061 Last IDX %d\n", last_index);
18277 return 0; /* Empty rr list */
18278 }
James Smart25626692013-03-01 16:36:54 -050018279 spin_unlock_irq(&phba->hbalock);
18280
James Smart7d791df2011-07-22 18:37:52 -040018281 next_fcf_pri = 0;
18282 /*
18283 * Clear the rr_bmask and set all of the bits that are at this
18284 * priority.
18285 */
18286 memset(phba->fcf.fcf_rr_bmask, 0,
18287 sizeof(*phba->fcf.fcf_rr_bmask));
18288 spin_lock_irq(&phba->hbalock);
18289 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18290 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18291 continue;
18292 /*
18293 * the 1st priority that has not FLOGI failed
18294 * will be the highest.
18295 */
18296 if (!next_fcf_pri)
18297 next_fcf_pri = fcf_pri->fcf_rec.priority;
18298 spin_unlock_irq(&phba->hbalock);
18299 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18300 rc = lpfc_sli4_fcf_rr_index_set(phba,
18301 fcf_pri->fcf_rec.fcf_index);
18302 if (rc)
18303 return 0;
18304 }
18305 spin_lock_irq(&phba->hbalock);
18306 }
18307 /*
18308 * if next_fcf_pri was not set above and the list is not empty then
18309 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070018310 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040018311 */
18312 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18313 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18314 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18315 /*
18316 * the 1st priority that has not FLOGI failed
18317 * will be the highest.
18318 */
18319 if (!next_fcf_pri)
18320 next_fcf_pri = fcf_pri->fcf_rec.priority;
18321 spin_unlock_irq(&phba->hbalock);
18322 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18323 rc = lpfc_sli4_fcf_rr_index_set(phba,
18324 fcf_pri->fcf_rec.fcf_index);
18325 if (rc)
18326 return 0;
18327 }
18328 spin_lock_irq(&phba->hbalock);
18329 }
18330 } else
18331 ret = 1;
18332 spin_unlock_irq(&phba->hbalock);
18333
18334 return ret;
18335}
18336/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018337 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18338 * @phba: pointer to lpfc hba data structure.
18339 *
18340 * This routine is to get the next eligible FCF record index in a round
18341 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040018342 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050018343 * shall be returned, otherwise, the next eligible FCF record's index
18344 * shall be returned.
18345 **/
18346uint16_t
18347lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18348{
18349 uint16_t next_fcf_index;
18350
James Smart421c6622013-01-03 15:44:16 -050018351initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040018352 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050018353 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18354
James Smart7d791df2011-07-22 18:37:52 -040018355next_priority:
James Smart421c6622013-01-03 15:44:16 -050018356 /* Determine the next fcf index to check */
18357 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
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,
James Smart3804dc82010-07-14 15:31:37 -040018360 next_fcf_index);
18361
James Smart0c9ab6f2010-02-26 14:15:57 -050018362 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018363 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18364 /*
18365 * If we have wrapped then we need to clear the bits that
18366 * have been tested so that we can detect when we should
18367 * change the priority level.
18368 */
James Smart0c9ab6f2010-02-26 14:15:57 -050018369 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18370 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040018371 }
18372
James Smart0c9ab6f2010-02-26 14:15:57 -050018373
James Smart3804dc82010-07-14 15:31:37 -040018374 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018375 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18376 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18377 /*
18378 * If next fcf index is not found check if there are lower
18379 * Priority level fcf's in the fcf_priority list.
18380 * Set up the rr_bmask with all of the avaiable fcf bits
18381 * at that level and continue the selection process.
18382 */
18383 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018384 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018385 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18386 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040018387 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18388 return LPFC_FCOE_FCF_NEXT_NONE;
18389 else {
18390 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18391 "3063 Only FCF available idx %d, flag %x\n",
18392 next_fcf_index,
18393 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18394 return next_fcf_index;
18395 }
James Smart3804dc82010-07-14 15:31:37 -040018396 }
18397
James Smart7d791df2011-07-22 18:37:52 -040018398 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18399 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018400 LPFC_FCF_FLOGI_FAILED) {
18401 if (list_is_singular(&phba->fcf.fcf_pri_list))
18402 return LPFC_FCOE_FCF_NEXT_NONE;
18403
James Smart7d791df2011-07-22 18:37:52 -040018404 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018405 }
James Smart7d791df2011-07-22 18:37:52 -040018406
James Smart3804dc82010-07-14 15:31:37 -040018407 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018408 "2845 Get next roundrobin failover FCF (x%x)\n",
18409 next_fcf_index);
18410
James Smart0c9ab6f2010-02-26 14:15:57 -050018411 return next_fcf_index;
18412}
18413
18414/**
18415 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18416 * @phba: pointer to lpfc hba data structure.
18417 *
18418 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018419 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018420 * does not go beyond the range of the driver allocated bmask dimension
18421 * before setting the bit.
18422 *
18423 * Returns 0 if the index bit successfully set, otherwise, it returns
18424 * -EINVAL.
18425 **/
18426int
18427lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18428{
18429 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18430 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018431 "2610 FCF (x%x) reached driver's book "
18432 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018433 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18434 return -EINVAL;
18435 }
18436 /* Set the eligible FCF record index bmask */
18437 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18438
James Smart3804dc82010-07-14 15:31:37 -040018439 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018440 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018441 "bmask\n", fcf_index);
18442
James Smart0c9ab6f2010-02-26 14:15:57 -050018443 return 0;
18444}
18445
18446/**
James Smart3804dc82010-07-14 15:31:37 -040018447 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018448 * @phba: pointer to lpfc hba data structure.
18449 *
18450 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018451 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018452 * does not go beyond the range of the driver allocated bmask dimension
18453 * before clearing the bit.
18454 **/
18455void
18456lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18457{
James Smart9a803a72013-09-06 12:17:56 -040018458 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018459 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18460 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018461 "2762 FCF (x%x) reached driver's book "
18462 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018463 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18464 return;
18465 }
18466 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018467 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018468 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18469 list) {
James Smart7d791df2011-07-22 18:37:52 -040018470 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18471 list_del_init(&fcf_pri->list);
18472 break;
18473 }
18474 }
18475 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018476 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018477
18478 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018479 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018480 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018481}
18482
18483/**
James Smartecfd03c2010-02-12 14:41:27 -050018484 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18485 * @phba: pointer to lpfc hba data structure.
18486 *
18487 * This routine is the completion routine for the rediscover FCF table mailbox
18488 * command. If the mailbox command returned failure, it will try to stop the
18489 * FCF rediscover wait timer.
18490 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018491static void
James Smartecfd03c2010-02-12 14:41:27 -050018492lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18493{
18494 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18495 uint32_t shdr_status, shdr_add_status;
18496
18497 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18498
18499 shdr_status = bf_get(lpfc_mbox_hdr_status,
18500 &redisc_fcf->header.cfg_shdr.response);
18501 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18502 &redisc_fcf->header.cfg_shdr.response);
18503 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018504 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018505 "2746 Requesting for FCF rediscovery failed "
18506 "status x%x add_status x%x\n",
18507 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018508 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018509 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018510 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018511 spin_unlock_irq(&phba->hbalock);
18512 /*
18513 * CVL event triggered FCF rediscover request failed,
18514 * last resort to re-try current registered FCF entry.
18515 */
18516 lpfc_retry_pport_discovery(phba);
18517 } else {
18518 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018519 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018520 spin_unlock_irq(&phba->hbalock);
18521 /*
18522 * DEAD FCF event triggered FCF rediscover request
18523 * failed, last resort to fail over as a link down
18524 * to FCF registration.
18525 */
18526 lpfc_sli4_fcf_dead_failthrough(phba);
18527 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018528 } else {
18529 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018530 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018531 /*
18532 * Start FCF rediscovery wait timer for pending FCF
18533 * before rescan FCF record table.
18534 */
18535 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018536 }
James Smartecfd03c2010-02-12 14:41:27 -050018537
18538 mempool_free(mbox, phba->mbox_mem_pool);
18539}
18540
18541/**
James Smart3804dc82010-07-14 15:31:37 -040018542 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018543 * @phba: pointer to lpfc hba data structure.
18544 *
18545 * This routine is invoked to request for rediscovery of the entire FCF table
18546 * by the port.
18547 **/
18548int
18549lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18550{
18551 LPFC_MBOXQ_t *mbox;
18552 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18553 int rc, length;
18554
James Smart0c9ab6f2010-02-26 14:15:57 -050018555 /* Cancel retry delay timers to all vports before FCF rediscover */
18556 lpfc_cancel_all_vport_retry_delay_timer(phba);
18557
James Smartecfd03c2010-02-12 14:41:27 -050018558 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18559 if (!mbox) {
18560 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18561 "2745 Failed to allocate mbox for "
18562 "requesting FCF rediscover.\n");
18563 return -ENOMEM;
18564 }
18565
18566 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18567 sizeof(struct lpfc_sli4_cfg_mhdr));
18568 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18569 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18570 length, LPFC_SLI4_MBX_EMBED);
18571
18572 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18573 /* Set count to 0 for invalidating the entire FCF database */
18574 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18575
18576 /* Issue the mailbox command asynchronously */
18577 mbox->vport = phba->pport;
18578 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18579 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18580
18581 if (rc == MBX_NOT_FINISHED) {
18582 mempool_free(mbox, phba->mbox_mem_pool);
18583 return -EIO;
18584 }
18585 return 0;
18586}
18587
18588/**
James Smartfc2b9892010-02-26 14:15:29 -050018589 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18590 * @phba: pointer to lpfc hba data structure.
18591 *
18592 * This function is the failover routine as a last resort to the FCF DEAD
18593 * event when driver failed to perform fast FCF failover.
18594 **/
18595void
18596lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18597{
18598 uint32_t link_state;
18599
18600 /*
18601 * Last resort as FCF DEAD event failover will treat this as
18602 * a link down, but save the link state because we don't want
18603 * it to be changed to Link Down unless it is already down.
18604 */
18605 link_state = phba->link_state;
18606 lpfc_linkdown(phba);
18607 phba->link_state = link_state;
18608
18609 /* Unregister FCF if no devices connected to it */
18610 lpfc_unregister_unused_fcf(phba);
18611}
18612
18613/**
James Smart026abb82011-12-13 13:20:45 -050018614 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018615 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018616 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018617 *
James Smart026abb82011-12-13 13:20:45 -050018618 * This function gets SLI3 port configure region 23 data through memory dump
18619 * mailbox command. When it successfully retrieves data, the size of the data
18620 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018621 **/
James Smart026abb82011-12-13 13:20:45 -050018622static uint32_t
18623lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018624{
18625 LPFC_MBOXQ_t *pmb = NULL;
18626 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018627 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018628 int rc;
18629
James Smart026abb82011-12-13 13:20:45 -050018630 if (!rgn23_data)
18631 return 0;
18632
James Smarta0c87cb2009-07-19 10:01:10 -040018633 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18634 if (!pmb) {
18635 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018636 "2600 failed to allocate mailbox memory\n");
18637 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018638 }
18639 mb = &pmb->u.mb;
18640
James Smarta0c87cb2009-07-19 10:01:10 -040018641 do {
18642 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18643 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18644
18645 if (rc != MBX_SUCCESS) {
18646 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018647 "2601 failed to read config "
18648 "region 23, rc 0x%x Status 0x%x\n",
18649 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018650 mb->un.varDmp.word_cnt = 0;
18651 }
18652 /*
18653 * dump mem may return a zero when finished or we got a
18654 * mailbox error, either way we are done.
18655 */
18656 if (mb->un.varDmp.word_cnt == 0)
18657 break;
18658 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18659 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18660
18661 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018662 rgn23_data + offset,
18663 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018664 offset += mb->un.varDmp.word_cnt;
18665 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18666
James Smart026abb82011-12-13 13:20:45 -050018667 mempool_free(pmb, phba->mbox_mem_pool);
18668 return offset;
18669}
18670
18671/**
18672 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18673 * @phba: pointer to lpfc hba data structure.
18674 * @rgn23_data: pointer to configure region 23 data.
18675 *
18676 * This function gets SLI4 port configure region 23 data through memory dump
18677 * mailbox command. When it successfully retrieves data, the size of the data
18678 * will be returned, otherwise, 0 will be returned.
18679 **/
18680static uint32_t
18681lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18682{
18683 LPFC_MBOXQ_t *mboxq = NULL;
18684 struct lpfc_dmabuf *mp = NULL;
18685 struct lpfc_mqe *mqe;
18686 uint32_t data_length = 0;
18687 int rc;
18688
18689 if (!rgn23_data)
18690 return 0;
18691
18692 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18693 if (!mboxq) {
18694 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18695 "3105 failed to allocate mailbox memory\n");
18696 return 0;
18697 }
18698
18699 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18700 goto out;
18701 mqe = &mboxq->u.mqe;
18702 mp = (struct lpfc_dmabuf *) mboxq->context1;
18703 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18704 if (rc)
18705 goto out;
18706 data_length = mqe->un.mb_words[5];
18707 if (data_length == 0)
18708 goto out;
18709 if (data_length > DMP_RGN23_SIZE) {
18710 data_length = 0;
18711 goto out;
18712 }
18713 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18714out:
18715 mempool_free(mboxq, phba->mbox_mem_pool);
18716 if (mp) {
18717 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18718 kfree(mp);
18719 }
18720 return data_length;
18721}
18722
18723/**
18724 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18725 * @phba: pointer to lpfc hba data structure.
18726 *
18727 * This function read region 23 and parse TLV for port status to
18728 * decide if the user disaled the port. If the TLV indicates the
18729 * port is disabled, the hba_flag is set accordingly.
18730 **/
18731void
18732lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18733{
18734 uint8_t *rgn23_data = NULL;
18735 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18736 uint32_t offset = 0;
18737
18738 /* Get adapter Region 23 data */
18739 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18740 if (!rgn23_data)
18741 goto out;
18742
18743 if (phba->sli_rev < LPFC_SLI_REV4)
18744 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18745 else {
18746 if_type = bf_get(lpfc_sli_intf_if_type,
18747 &phba->sli4_hba.sli_intf);
18748 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18749 goto out;
18750 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18751 }
James Smarta0c87cb2009-07-19 10:01:10 -040018752
18753 if (!data_size)
18754 goto out;
18755
18756 /* Check the region signature first */
18757 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18758 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18759 "2619 Config region 23 has bad signature\n");
18760 goto out;
18761 }
18762 offset += 4;
18763
18764 /* Check the data structure version */
18765 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18766 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18767 "2620 Config region 23 has bad version\n");
18768 goto out;
18769 }
18770 offset += 4;
18771
18772 /* Parse TLV entries in the region */
18773 while (offset < data_size) {
18774 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18775 break;
18776 /*
18777 * If the TLV is not driver specific TLV or driver id is
18778 * not linux driver id, skip the record.
18779 */
18780 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18781 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18782 (rgn23_data[offset + 3] != 0)) {
18783 offset += rgn23_data[offset + 1] * 4 + 4;
18784 continue;
18785 }
18786
18787 /* Driver found a driver specific TLV in the config region */
18788 sub_tlv_len = rgn23_data[offset + 1] * 4;
18789 offset += 4;
18790 tlv_offset = 0;
18791
18792 /*
18793 * Search for configured port state sub-TLV.
18794 */
18795 while ((offset < data_size) &&
18796 (tlv_offset < sub_tlv_len)) {
18797 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18798 offset += 4;
18799 tlv_offset += 4;
18800 break;
18801 }
18802 if (rgn23_data[offset] != PORT_STE_TYPE) {
18803 offset += rgn23_data[offset + 1] * 4 + 4;
18804 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18805 continue;
18806 }
18807
18808 /* This HBA contains PORT_STE configured */
18809 if (!rgn23_data[offset + 2])
18810 phba->hba_flag |= LINK_DISABLED;
18811
18812 goto out;
18813 }
18814 }
James Smart026abb82011-12-13 13:20:45 -050018815
James Smarta0c87cb2009-07-19 10:01:10 -040018816out:
James Smarta0c87cb2009-07-19 10:01:10 -040018817 kfree(rgn23_data);
18818 return;
18819}
James Smart695a8142010-01-26 23:08:03 -050018820
18821/**
James Smart52d52442011-05-24 11:42:45 -040018822 * lpfc_wr_object - write an object to the firmware
18823 * @phba: HBA structure that indicates port to create a queue on.
18824 * @dmabuf_list: list of dmabufs to write to the port.
18825 * @size: the total byte value of the objects to write to the port.
18826 * @offset: the current offset to be used to start the transfer.
18827 *
18828 * This routine will create a wr_object mailbox command to send to the port.
18829 * the mailbox command will be constructed using the dma buffers described in
18830 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18831 * BDEs that the imbedded mailbox can support. The @offset variable will be
18832 * used to indicate the starting offset of the transfer and will also return
18833 * the offset after the write object mailbox has completed. @size is used to
18834 * determine the end of the object and whether the eof bit should be set.
18835 *
18836 * Return 0 is successful and offset will contain the the new offset to use
18837 * for the next write.
18838 * Return negative value for error cases.
18839 **/
18840int
18841lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18842 uint32_t size, uint32_t *offset)
18843{
18844 struct lpfc_mbx_wr_object *wr_object;
18845 LPFC_MBOXQ_t *mbox;
18846 int rc = 0, i = 0;
18847 uint32_t shdr_status, shdr_add_status;
18848 uint32_t mbox_tmo;
18849 union lpfc_sli4_cfg_shdr *shdr;
18850 struct lpfc_dmabuf *dmabuf;
18851 uint32_t written = 0;
18852
18853 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18854 if (!mbox)
18855 return -ENOMEM;
18856
18857 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18858 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18859 sizeof(struct lpfc_mbx_wr_object) -
18860 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18861
18862 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18863 wr_object->u.request.write_offset = *offset;
18864 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18865 wr_object->u.request.object_name[0] =
18866 cpu_to_le32(wr_object->u.request.object_name[0]);
18867 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18868 list_for_each_entry(dmabuf, dmabuf_list, list) {
18869 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18870 break;
18871 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18872 wr_object->u.request.bde[i].addrHigh =
18873 putPaddrHigh(dmabuf->phys);
18874 if (written + SLI4_PAGE_SIZE >= size) {
18875 wr_object->u.request.bde[i].tus.f.bdeSize =
18876 (size - written);
18877 written += (size - written);
18878 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18879 } else {
18880 wr_object->u.request.bde[i].tus.f.bdeSize =
18881 SLI4_PAGE_SIZE;
18882 written += SLI4_PAGE_SIZE;
18883 }
18884 i++;
18885 }
18886 wr_object->u.request.bde_count = i;
18887 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18888 if (!phba->sli4_hba.intr_enable)
18889 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18890 else {
James Smarta183a152011-10-10 21:32:43 -040018891 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040018892 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18893 }
18894 /* The IOCTL status is embedded in the mailbox subheader. */
18895 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18896 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18897 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18898 if (rc != MBX_TIMEOUT)
18899 mempool_free(mbox, phba->mbox_mem_pool);
18900 if (shdr_status || shdr_add_status || rc) {
18901 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18902 "3025 Write Object mailbox failed with "
18903 "status x%x add_status x%x, mbx status x%x\n",
18904 shdr_status, shdr_add_status, rc);
18905 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080018906 *offset = shdr_add_status;
James Smart52d52442011-05-24 11:42:45 -040018907 } else
18908 *offset += wr_object->u.response.actual_write_length;
18909 return rc;
18910}
18911
18912/**
James Smart695a8142010-01-26 23:08:03 -050018913 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18914 * @vport: pointer to vport data structure.
18915 *
18916 * This function iterate through the mailboxq and clean up all REG_LOGIN
18917 * and REG_VPI mailbox commands associated with the vport. This function
18918 * is called when driver want to restart discovery of the vport due to
18919 * a Clear Virtual Link event.
18920 **/
18921void
18922lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18923{
18924 struct lpfc_hba *phba = vport->phba;
18925 LPFC_MBOXQ_t *mb, *nextmb;
18926 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040018927 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040018928 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040018929 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040018930 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050018931 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050018932
James Smartd439d282010-09-29 11:18:45 -040018933 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050018934 spin_lock_irq(&phba->hbalock);
18935 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18936 if (mb->vport != vport)
18937 continue;
18938
18939 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18940 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18941 continue;
18942
James Smartd439d282010-09-29 11:18:45 -040018943 list_del(&mb->list);
18944 list_add_tail(&mb->list, &mbox_cmd_list);
18945 }
18946 /* Clean up active mailbox command with the vport */
18947 mb = phba->sli.mbox_active;
18948 if (mb && (mb->vport == vport)) {
18949 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18950 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18951 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18952 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18953 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18954 /* Put reference count for delayed processing */
18955 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18956 /* Unregister the RPI when mailbox complete */
18957 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18958 }
18959 }
James Smart63e801c2010-11-20 23:14:19 -050018960 /* Cleanup any mailbox completions which are not yet processed */
18961 do {
18962 restart_loop = 0;
18963 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18964 /*
18965 * If this mailox is already processed or it is
18966 * for another vport ignore it.
18967 */
18968 if ((mb->vport != vport) ||
18969 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18970 continue;
18971
18972 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18973 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18974 continue;
18975
18976 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18977 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18978 ndlp = (struct lpfc_nodelist *)mb->context2;
18979 /* Unregister the RPI when mailbox complete */
18980 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18981 restart_loop = 1;
18982 spin_unlock_irq(&phba->hbalock);
18983 spin_lock(shost->host_lock);
18984 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18985 spin_unlock(shost->host_lock);
18986 spin_lock_irq(&phba->hbalock);
18987 break;
18988 }
18989 }
18990 } while (restart_loop);
18991
James Smartd439d282010-09-29 11:18:45 -040018992 spin_unlock_irq(&phba->hbalock);
18993
18994 /* Release the cleaned-up mailbox commands */
18995 while (!list_empty(&mbox_cmd_list)) {
18996 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050018997 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18998 mp = (struct lpfc_dmabuf *) (mb->context1);
18999 if (mp) {
19000 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19001 kfree(mp);
19002 }
James Smart78730cf2010-04-06 15:06:30 -040019003 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040019004 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040019005 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019006 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040019007 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019008 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040019009 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040019010 }
James Smart695a8142010-01-26 23:08:03 -050019011 }
James Smart695a8142010-01-26 23:08:03 -050019012 mempool_free(mb, phba->mbox_mem_pool);
19013 }
James Smartd439d282010-09-29 11:18:45 -040019014
19015 /* Release the ndlp with the cleaned-up active mailbox command */
19016 if (act_mbx_ndlp) {
19017 spin_lock(shost->host_lock);
19018 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19019 spin_unlock(shost->host_lock);
19020 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050019021 }
James Smart695a8142010-01-26 23:08:03 -050019022}
19023
James Smart2a9bf3d2010-06-07 15:24:45 -040019024/**
19025 * lpfc_drain_txq - Drain the txq
19026 * @phba: Pointer to HBA context object.
19027 *
19028 * This function attempt to submit IOCBs on the txq
19029 * to the adapter. For SLI4 adapters, the txq contains
19030 * ELS IOCBs that have been deferred because the there
19031 * are no SGLs. This congestion can occur with large
19032 * vport counts during node discovery.
19033 **/
19034
19035uint32_t
19036lpfc_drain_txq(struct lpfc_hba *phba)
19037{
19038 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080019039 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090019040 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040019041 unsigned long iflags = 0;
19042 char *fail_msg = NULL;
19043 struct lpfc_sglq *sglq;
James Smart2f077842016-12-19 15:07:29 -080019044 union lpfc_wqe128 wqe128;
19045 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
James Smarta2fc4aef2014-09-03 12:57:55 -040019046 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040019047
James Smart895427b2017-02-12 13:52:30 -080019048 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070019049 if (unlikely(!pring))
19050 return 0;
James Smart895427b2017-02-12 13:52:30 -080019051
James Smart398d81c2013-05-31 17:04:19 -040019052 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050019053 list_for_each_entry(piocbq, &pring->txq, list) {
19054 txq_cnt++;
19055 }
19056
19057 if (txq_cnt > pring->txq_max)
19058 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019059
James Smart398d81c2013-05-31 17:04:19 -040019060 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019061
James Smart0e9bb8d2013-03-01 16:35:12 -050019062 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040019063 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019064
James Smart19ca7602010-11-20 23:11:55 -050019065 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040019066 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040019067 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040019068 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19069 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050019070 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040019071 break;
19072 }
James Smart895427b2017-02-12 13:52:30 -080019073 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040019074 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050019075 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040019076 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019077 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040019078 }
James Smart0e9bb8d2013-03-01 16:35:12 -050019079 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040019080
19081 /* The xri and iocb resources secured,
19082 * attempt to issue request
19083 */
James Smart6d368e52011-05-24 11:44:12 -040019084 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040019085 piocbq->sli4_xritag = sglq->sli4_xritag;
19086 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19087 fail_msg = "to convert bpl to sgl";
James Smart2f077842016-12-19 15:07:29 -080019088 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019089 fail_msg = "to convert iocb to wqe";
James Smart2f077842016-12-19 15:07:29 -080019090 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019091 fail_msg = " - Wq is full";
19092 else
19093 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19094
19095 if (fail_msg) {
19096 /* Failed means we can't issue and need to cancel */
19097 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19098 "2822 IOCB failed %s iotag 0x%x "
19099 "xri 0x%x\n",
19100 fail_msg,
19101 piocbq->iotag, piocbq->sli4_xritag);
19102 list_add_tail(&piocbq->list, &completions);
19103 }
James Smart398d81c2013-05-31 17:04:19 -040019104 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019105 }
19106
James Smart2a9bf3d2010-06-07 15:24:45 -040019107 /* Cancel all the IOCBs that cannot be issued */
19108 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19109 IOERR_SLI_ABORTED);
19110
James Smart0e9bb8d2013-03-01 16:35:12 -050019111 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019112}
James Smart895427b2017-02-12 13:52:30 -080019113
19114/**
19115 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19116 * @phba: Pointer to HBA context object.
19117 * @pwqe: Pointer to command WQE.
19118 * @sglq: Pointer to the scatter gather queue object.
19119 *
19120 * This routine converts the bpl or bde that is in the WQE
19121 * to a sgl list for the sli4 hardware. The physical address
19122 * of the bpl/bde is converted back to a virtual address.
19123 * If the WQE contains a BPL then the list of BDE's is
19124 * converted to sli4_sge's. If the WQE contains a single
19125 * BDE then it is converted to a single sli_sge.
19126 * The WQE is still in cpu endianness so the contents of
19127 * the bpl can be used without byte swapping.
19128 *
19129 * Returns valid XRI = Success, NO_XRI = Failure.
19130 */
19131static uint16_t
19132lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19133 struct lpfc_sglq *sglq)
19134{
19135 uint16_t xritag = NO_XRI;
19136 struct ulp_bde64 *bpl = NULL;
19137 struct ulp_bde64 bde;
19138 struct sli4_sge *sgl = NULL;
19139 struct lpfc_dmabuf *dmabuf;
19140 union lpfc_wqe *wqe;
19141 int numBdes = 0;
19142 int i = 0;
19143 uint32_t offset = 0; /* accumulated offset in the sg request list */
19144 int inbound = 0; /* number of sg reply entries inbound from firmware */
19145 uint32_t cmd;
19146
19147 if (!pwqeq || !sglq)
19148 return xritag;
19149
19150 sgl = (struct sli4_sge *)sglq->sgl;
19151 wqe = &pwqeq->wqe;
19152 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19153
19154 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19155 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19156 return sglq->sli4_xritag;
19157 numBdes = pwqeq->rsvd2;
19158 if (numBdes) {
19159 /* The addrHigh and addrLow fields within the WQE
19160 * have not been byteswapped yet so there is no
19161 * need to swap them back.
19162 */
19163 if (pwqeq->context3)
19164 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19165 else
19166 return xritag;
19167
19168 bpl = (struct ulp_bde64 *)dmabuf->virt;
19169 if (!bpl)
19170 return xritag;
19171
19172 for (i = 0; i < numBdes; i++) {
19173 /* Should already be byte swapped. */
19174 sgl->addr_hi = bpl->addrHigh;
19175 sgl->addr_lo = bpl->addrLow;
19176
19177 sgl->word2 = le32_to_cpu(sgl->word2);
19178 if ((i+1) == numBdes)
19179 bf_set(lpfc_sli4_sge_last, sgl, 1);
19180 else
19181 bf_set(lpfc_sli4_sge_last, sgl, 0);
19182 /* swap the size field back to the cpu so we
19183 * can assign it to the sgl.
19184 */
19185 bde.tus.w = le32_to_cpu(bpl->tus.w);
19186 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19187 /* The offsets in the sgl need to be accumulated
19188 * separately for the request and reply lists.
19189 * The request is always first, the reply follows.
19190 */
19191 switch (cmd) {
19192 case CMD_GEN_REQUEST64_WQE:
19193 /* add up the reply sg entries */
19194 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19195 inbound++;
19196 /* first inbound? reset the offset */
19197 if (inbound == 1)
19198 offset = 0;
19199 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19200 bf_set(lpfc_sli4_sge_type, sgl,
19201 LPFC_SGE_TYPE_DATA);
19202 offset += bde.tus.f.bdeSize;
19203 break;
19204 case CMD_FCP_TRSP64_WQE:
19205 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19206 bf_set(lpfc_sli4_sge_type, sgl,
19207 LPFC_SGE_TYPE_DATA);
19208 break;
19209 case CMD_FCP_TSEND64_WQE:
19210 case CMD_FCP_TRECEIVE64_WQE:
19211 bf_set(lpfc_sli4_sge_type, sgl,
19212 bpl->tus.f.bdeFlags);
19213 if (i < 3)
19214 offset = 0;
19215 else
19216 offset += bde.tus.f.bdeSize;
19217 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19218 break;
19219 }
19220 sgl->word2 = cpu_to_le32(sgl->word2);
19221 bpl++;
19222 sgl++;
19223 }
19224 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19225 /* The addrHigh and addrLow fields of the BDE have not
19226 * been byteswapped yet so they need to be swapped
19227 * before putting them in the sgl.
19228 */
19229 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19230 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19231 sgl->word2 = le32_to_cpu(sgl->word2);
19232 bf_set(lpfc_sli4_sge_last, sgl, 1);
19233 sgl->word2 = cpu_to_le32(sgl->word2);
19234 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19235 }
19236 return sglq->sli4_xritag;
19237}
19238
19239/**
19240 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19241 * @phba: Pointer to HBA context object.
19242 * @ring_number: Base sli ring number
19243 * @pwqe: Pointer to command WQE.
19244 **/
19245int
19246lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
19247 struct lpfc_iocbq *pwqe)
19248{
19249 union lpfc_wqe *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080019250 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080019251 struct lpfc_queue *wq;
19252 struct lpfc_sglq *sglq;
19253 struct lpfc_sli_ring *pring;
19254 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070019255 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080019256
19257 /* NVME_LS and NVME_LS ABTS requests. */
19258 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19259 pring = phba->sli4_hba.nvmels_wq->pring;
19260 spin_lock_irqsave(&pring->ring_lock, iflags);
19261 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19262 if (!sglq) {
19263 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19264 return WQE_BUSY;
19265 }
19266 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19267 pwqe->sli4_xritag = sglq->sli4_xritag;
19268 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19269 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19270 return WQE_ERROR;
19271 }
19272 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19273 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070019274 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19275 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019276 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019277 return ret;
James Smart895427b2017-02-12 13:52:30 -080019278 }
Dick Kennedycd22d602017-08-23 16:55:35 -070019279
James Smart895427b2017-02-12 13:52:30 -080019280 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19281 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19282 return 0;
19283 }
19284
19285 /* NVME_FCREQ and NVME_ABTS requests */
19286 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19287 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19288 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19289
19290 spin_lock_irqsave(&pring->ring_lock, iflags);
19291 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19292 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19293 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019294 ret = lpfc_sli4_wq_put(wq, wqe);
19295 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019296 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019297 return ret;
James Smart895427b2017-02-12 13:52:30 -080019298 }
19299 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19300 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19301 return 0;
19302 }
19303
James Smartf358dd02017-02-12 13:52:34 -080019304 /* NVMET requests */
19305 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19306 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19307 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19308
19309 spin_lock_irqsave(&pring->ring_lock, iflags);
19310 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070019311 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080019312 if (pwqe->sli4_xritag == NO_XRI) {
19313 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19314 pwqe->sli4_xritag = sglq->sli4_xritag;
19315 }
19316 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19317 pwqe->sli4_xritag);
19318 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19319 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19320 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019321 ret = lpfc_sli4_wq_put(wq, wqe);
19322 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080019323 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019324 return ret;
James Smartf358dd02017-02-12 13:52:34 -080019325 }
19326 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19327 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19328 return 0;
19329 }
James Smart895427b2017-02-12 13:52:30 -080019330 return WQE_ERROR;
19331}